diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 8addc2bac..82f6fac64 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -256,6 +256,9 @@ static const CRPCCommand vRPCCommands[] = { "follow", &follow, false, true, false }, { "unfollow", &unfollow, false, true, false }, { "getfollowing", &getfollowing, false, true, false }, + { "ignore", &addtoblacklist, false, true, false }, + { "unignore", &removefromblacklist, false, true, false }, + { "getblacklist", &getblacklist, false, true, false }, { "getlasthave", &getlasthave, false, true, false }, { "getnumpieces", &getnumpieces, false, true, false }, { "listusernamespartial", &listusernamespartial, false, true, true }, @@ -1308,6 +1311,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 2) ConvertTo(params[2]); if (strMethod == "follow" && n > 1) ConvertTo(params[1]); if (strMethod == "unfollow" && n > 1) ConvertTo(params[1]); + if (strMethod == "ignore" && n > 1) ConvertTo(params[1]); + if (strMethod == "unignore" && n > 1) ConvertTo(params[1]); if (strMethod == "listusernamespartial" && n > 1) ConvertTo(params[1]); if (strMethod == "listusernamespartial" && n > 2) ConvertTo(params[2]); if (strMethod == "gettrendinghashtags" && n > 0) ConvertTo(params[0]); diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index b0822e063..ee564cf84 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -209,6 +209,9 @@ extern json_spirit::Value getspammsg(const json_spirit::Array& params, bool fHel extern json_spirit::Value follow(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value unfollow(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getfollowing(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value addtoblacklist(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value removefromblacklist(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value getblacklist(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getlasthave(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getnumpieces(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value listusernamespartial(const json_spirit::Array& params, bool fHelp); diff --git a/src/twister.cpp b/src/twister.cpp index 24a19f213..453ddb240 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -168,6 +168,13 @@ torrent_handle getTorrentUser(std::string const &username) return torrent_handle(); } +void stopTorrentUser(std::string const &username) { + boost::shared_ptr ses(m_ses); + if (ses) { + ses->remove_torrent(getTorrentUser(username)); + } +} + int torrentLastHave(std::string const &username) { torrent_handle h = getTorrentUser(username); @@ -393,7 +400,9 @@ void ThreadWaitExtIP() for (i = m_users.begin(); i != m_users.end(); ++i) { UserData const &data = i->second; BOOST_FOREACH(string username, data.m_following) { - torrentsToStart.insert(username); + if ( !data.m_blacklist.count(username)) { + torrentsToStart.insert(username); + } } } @@ -2050,12 +2059,16 @@ Value unfollow(const Array& params, bool fHelp) string localUser = params[0].get_str(); Array users = params[1].get_array(); - LOCK(cs_twister); for( unsigned int u = 0; u < users.size(); u++ ) { string username = users[u].get_str(); if( m_users.count(localUser) && m_users[localUser].m_following.count(username) ) { + if ( m_users[localUser].m_blacklist.count(username) ) { + stopTorrentUser(username); + } + + LOCK(cs_twister); m_users[localUser].m_following.erase(username); } } @@ -2082,6 +2095,77 @@ Value getfollowing(const Array& params, bool fHelp) return ret; } +Value addtoblacklist(const Array& params, bool fHelp) +{ + if (fHelp || (params.size() != 2)) + throw runtime_error( + "ignore [ignore_username1,ignore_username2,...]\n" + "start ignoring users"); + + string localUser = params[0].get_str(); + Array users = params[1].get_array(); + + for( unsigned int u = 0; u < users.size(); u++ ) { + string username = users[u].get_str(); + + if ( !m_users[localUser].m_following.count(username) ) { + stopTorrentUser(username); + } + + LOCK(cs_twister); + m_users[localUser].m_blacklist.insert(username); + } + + return Value(); +} + +Value removefromblacklist(const Array& params, bool fHelp) +{ + if (fHelp || (params.size() != 2)) + throw runtime_error( + "unignore [unignore_username1,unignore_username2,...]\n" + "stop ignoring users"); + + string localUser = params[0].get_str(); + Array users = params[1].get_array(); + + for( unsigned int u = 0; u < users.size(); u++ ) { + string username = users[u].get_str(); + + if( m_users.count(localUser) && + m_users[localUser].m_blacklist.count(username) ) { + + if( m_users[localUser].m_following.count(username) ) { + startTorrentUser(username, true); + } + + LOCK(cs_twister); + m_users[localUser].m_blacklist.erase(username); + } + } + + return Value(); +} + +Value getblacklist(const Array& params, bool fHelp) +{ + if (fHelp || (params.size() != 1)) + throw runtime_error( + "getblacklist \n" + "get list of users we ignored"); + + string localUser = params[0].get_str(); + + Array ret; + LOCK(cs_twister); + if( m_users.count(localUser) ) { + BOOST_FOREACH(string username, m_users[localUser].m_blacklist) { + ret.push_back(username); + } + } + return ret; +} + Value getlasthave(const Array& params, bool fHelp) { if (fHelp || (params.size() != 1)) diff --git a/src/twister_utils.cpp b/src/twister_utils.cpp index a4a1bc561..e9169659d 100644 --- a/src/twister_utils.cpp +++ b/src/twister_utils.cpp @@ -148,6 +148,14 @@ int saveUserData(std::string const& filename, std::map con } } + if( udata.m_blacklist.size() ) { + entry &userData = userDict[i->first]; + entry &blacklist = userData["blacklist"]; + BOOST_FOREACH( std::string const &n, udata.m_blacklist) { + blacklist.list().push_back(n); + } + } + if( udata.m_directmsg.size() ) { entry &userData = userDict[i->first]; entry &dmDict = userData["dm"]; @@ -200,6 +208,15 @@ int loadUserData(std::string const& filename, std::map &us } } + const lazy_entry *blacklist = userData->dict_find("blacklist"); + if( blacklist ) { + if( blacklist->type() != lazy_entry::list_t ) goto data_error; + + for( int j = 0; j < blacklist->list_size(); j++ ) { + udata.m_blacklist.insert( blacklist->list_string_value_at(j) ); + } + } + const lazy_entry *dmDict = userData->dict_find("dm"); if( dmDict ) { if( dmDict->type() != lazy_entry::dict_t ) goto data_error; diff --git a/src/twister_utils.h b/src/twister_utils.h index 26c4c913c..c67b7c270 100644 --- a/src/twister_utils.h +++ b/src/twister_utils.h @@ -19,6 +19,9 @@ struct StoredDirectMsg { // in-memory data per wallet user struct UserData { std::set m_following; + + std::set m_blacklist; // allow user to ignore someone's message by adding them(username) to blacklist + // m_directmsg key is the other username std::map > m_directmsg; };