From 6b7bccc7767a59f0500c782bf4137e258683237b Mon Sep 17 00:00:00 2001 From: "(null)" Date: Mon, 8 Sep 2014 22:12:14 +0800 Subject: [PATCH 1/4] Add blacklist support --- src/bitcoinrpc.cpp | 5 ++++ src/bitcoinrpc.h | 3 ++ src/twister.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++ src/twister_utils.cpp | 17 +++++++++++ src/twister_utils.h | 3 ++ 5 files changed, 93 insertions(+) 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..620f5c98c 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -2082,6 +2082,71 @@ 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(); + torrent_handle h = startTorrentUser(username, true); + + if( h.is_valid() ) { + 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(); + + 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_blacklist.count(username) ) { + 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; }; From da003e212d52f6b27ef8f9eb52acb89628199f08 Mon Sep 17 00:00:00 2001 From: "(null)" Date: Tue, 9 Sep 2014 00:24:13 +0800 Subject: [PATCH 2/4] Stop torrent ignored users. --- src/twister.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 620f5c98c..54aa447cf 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -393,7 +393,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); + } } } @@ -2094,12 +2096,17 @@ Value addtoblacklist(const Array& params, bool fHelp) for( unsigned int u = 0; u < users.size(); u++ ) { string username = users[u].get_str(); - torrent_handle h = startTorrentUser(username, true); + torrent_handle h = getTorrentUser(username); if( h.is_valid() ) { - LOCK(cs_twister); - m_users[localUser].m_blacklist.insert(username); + boost::shared_ptr ses(m_ses); + if ( ses ) { + ses->remove_torrent(h); + } } + + LOCK(cs_twister); + m_users[localUser].m_blacklist.insert(username); } return Value(); From a2b131f538e1c302f646733a48e0c460a20afaa4 Mon Sep 17 00:00:00 2001 From: "(null)" Date: Tue, 9 Sep 2014 10:27:40 +0800 Subject: [PATCH 3/4] Still torrent followed users. --- src/twister.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 54aa447cf..0b739ff1b 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -159,6 +159,13 @@ torrent_handle startTorrentUser(std::string const &username, bool following) return m_userTorrent[username]; } +void stopTorrentUser(std::string const &username) { + boost::shared_ptr ses(m_ses); + if (ses) { + ses->remove_torrent(getTorrentUser(username)); + } +} + torrent_handle getTorrentUser(std::string const &username) { LOCK(cs_twister); @@ -2052,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); } } @@ -2097,12 +2108,8 @@ Value addtoblacklist(const Array& params, bool fHelp) for( unsigned int u = 0; u < users.size(); u++ ) { string username = users[u].get_str(); - torrent_handle h = getTorrentUser(username); - if( h.is_valid() ) { - boost::shared_ptr ses(m_ses); - if ( ses ) { - ses->remove_torrent(h); - } + if ( !m_users[localUser].m_following.count(username) ) { + stopTorrentUser(username); } LOCK(cs_twister); @@ -2122,12 +2129,17 @@ Value removefromblacklist(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_blacklist.count(username) ) { + + if( m_users[localUser].m_following.count(username) ) { + startTorrentUser(username, true); + } + + LOCK(cs_twister); m_users[localUser].m_blacklist.erase(username); } } From 479cf47f67a5aa4f172f3d61456e6ee183c7b1d0 Mon Sep 17 00:00:00 2001 From: "(null)" Date: Tue, 9 Sep 2014 10:50:28 +0800 Subject: [PATCH 4/4] Fix compile errors --- src/twister.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 0b739ff1b..453ddb240 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -159,13 +159,6 @@ torrent_handle startTorrentUser(std::string const &username, bool following) return m_userTorrent[username]; } -void stopTorrentUser(std::string const &username) { - boost::shared_ptr ses(m_ses); - if (ses) { - ses->remove_torrent(getTorrentUser(username)); - } -} - torrent_handle getTorrentUser(std::string const &username) { LOCK(cs_twister); @@ -175,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);