Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blacklist support #269

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -1308,6 +1311,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "getdirectmsgs" && n > 2) ConvertTo<Array>(params[2]);
if (strMethod == "follow" && n > 1) ConvertTo<Array>(params[1]);
if (strMethod == "unfollow" && n > 1) ConvertTo<Array>(params[1]);
if (strMethod == "ignore" && n > 1) ConvertTo<Array>(params[1]);
if (strMethod == "unignore" && n > 1) ConvertTo<Array>(params[1]);
if (strMethod == "listusernamespartial" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "listusernamespartial" && n > 2) ConvertTo<bool>(params[2]);
if (strMethod == "gettrendinghashtags" && n > 0) ConvertTo<boost::int64_t>(params[0]);
Expand Down
3 changes: 3 additions & 0 deletions src/bitcoinrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
88 changes: 86 additions & 2 deletions src/twister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ torrent_handle getTorrentUser(std::string const &username)
return torrent_handle();
}

void stopTorrentUser(std::string const &username) {
boost::shared_ptr<session> ses(m_ses);
if (ses) {
ses->remove_torrent(getTorrentUser(username));
}
}

int torrentLastHave(std::string const &username)
{
torrent_handle h = getTorrentUser(username);
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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 <username> [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 <username> [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 <username>\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))
Expand Down
17 changes: 17 additions & 0 deletions src/twister_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ int saveUserData(std::string const& filename, std::map<std::string,UserData> 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"];
Expand Down Expand Up @@ -200,6 +208,15 @@ int loadUserData(std::string const& filename, std::map<std::string,UserData> &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;
Expand Down
3 changes: 3 additions & 0 deletions src/twister_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct StoredDirectMsg {
// in-memory data per wallet user
struct UserData {
std::set<std::string> m_following;

std::set<std::string> m_blacklist; // allow user to ignore someone's message by adding them(username) to blacklist

// m_directmsg key is the other username
std::map<std::string, std::vector<StoredDirectMsg> > m_directmsg;
};
Expand Down