X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmodules%2Fm_callerid.cpp;h=1b9492ce14ecdbacd0cf33e0176982acc124c691;hb=b5e220008782b2d538cb8e6e3b1923af0c13fb99;hp=52828fdca6796c35c3cd05bfdf2ada58c713268f;hpb=c512706209dbbf7aff9f129782b8417267808c96;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_callerid.cpp b/src/modules/m_callerid.cpp index 52828fdca..1b9492ce1 100644 --- a/src/modules/m_callerid.cpp +++ b/src/modules/m_callerid.cpp @@ -1,12 +1,19 @@ -#include "inspircd.h" -#include "users.h" -#include "channels.h" -#include "modules.h" +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ +#include "inspircd.h" #include - #include - #include /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */ @@ -15,8 +22,15 @@ class callerid_data : public classbase { public: time_t lastnotify; + + /** Users I accept messages from + */ std::set accepting; + /** Users who list me as accepted + */ + std::list wholistsme; + callerid_data() : lastnotify(0) { } callerid_data(const std::string& str, InspIRCd* ServerInstance) { @@ -32,11 +46,8 @@ class callerid_data : public classbase { continue; } - User* u = ServerInstance->FindUUID(tok); - if (!u) - { - u = ServerInstance->FindNick(tok); - } + + User *u = ServerInstance->FindNick(tok); if (!u) { continue; @@ -49,7 +60,7 @@ class callerid_data : public classbase { std::ostringstream oss; oss << lastnotify; - for (std::set::iterator i = accepting.begin(); i != accepting.end(); ++i) + for (std::set::const_iterator i = accepting.begin(); i != accepting.end(); ++i) { // Encode UIDs. oss << "," << (displayable ? (*i)->nick : (*i)->uuid); @@ -59,6 +70,11 @@ class callerid_data : public classbase } }; +/** Retrieves the callerid information for a given user record + * @param who The user to retrieve callerid information for + * @param extend true to add callerid information if it doesn't already exist, false to return NULL if it doesn't exist + * @return NULL if extend is false and it doesn't exist, a callerid_data instance otherwise. + */ callerid_data* GetData(User* who, bool extend = true) { callerid_data* dat; @@ -85,28 +101,29 @@ void RemoveData(User* who) if (!dat) return; - who->Shrink("callerid_data"); - delete dat; -} - -void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who) -{ - for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i) + // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme. + for (std::set::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++) { - callerid_data* dat = GetData(i->second, false); - - if (!dat) - continue; - - std::set::iterator iter = dat->accepting.find(who); + callerid_data *targ = GetData(*it, false); - if (iter == dat->accepting.end()) - continue; + if (!targ) + continue; // shouldn't happen, but oh well. - dat->accepting.erase(iter); + for (std::list::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++) + { + if (*it2 == dat) + { + targ->wholistsme.erase(it2); + break; + } + } } + + who->Shrink("callerid_data"); + delete dat; } + class User_g : public SimpleUserModeHandler { public: @@ -174,7 +191,8 @@ public: return CMD_SUCCESS; /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can * have a list already setup. */ - bool atleastonechange = false; + + std::string tok = parameters[0]; if (tok == "*") { @@ -187,6 +205,8 @@ public: User* whotoremove = ServerInstance->FindNick(tok.substr(1)); if (whotoremove) return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE); + else + return CMD_FAILURE; } else { @@ -214,6 +234,7 @@ public: bool AddAccept(User* user, User* whotoadd, bool quiet) { + // Add this user to my accept list first, so look me up.. callerid_data* dat = GetData(user, true); if (dat->accepting.size() >= maxaccepts) { @@ -229,11 +250,18 @@ public: return false; } + + // Now, look them up, and add me to their list + callerid_data *targ = GetData(whotoadd, true); + targ->wholistsme.push_back(dat); + + user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str()); return true; } bool RemoveAccept(User* user, User* whotoremove, bool quiet) { + // Remove them from my list, so look up my list.. callerid_data* dat = GetData(user, false); if (!dat) { @@ -250,7 +278,28 @@ public: return false; } + dat->accepting.erase(i); + + // Look up their list to remove me. + callerid_data *dat2 = GetData(whotoremove, false); + if (!dat2) + { + // How the fuck is this possible. + return false; + } + + for (std::list::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++) + { + // Found me! + if (*it == dat) + { + dat2->wholistsme.erase(it); + break; + } + } + + user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str()); return true; } }; @@ -258,8 +307,8 @@ public: class ModuleCallerID : public Module { private: - CommandAccept *mycommand; - User_g* myumode; + CommandAccept mycommand; + User_g myumode; // Configuration variables: unsigned int maxaccepts; // Maximum ACCEPT entries. @@ -267,43 +316,53 @@ private: bool tracknick; // Allow ACCEPT entries to update with nick changes. unsigned int notify_cooldown; // Seconds between notifications. -public: - ModuleCallerID(InspIRCd* Me) : Module(Me) + /** Removes a user from all accept lists + * @param who The user to remove from accepts + */ + void RemoveFromAllAccepts(User* who) { - OnRehash(NULL, ""); - mycommand = new CommandAccept(ServerInstance, maxaccepts); - myumode = new User_g(ServerInstance); + // First, find the list of people who have me on accept + callerid_data *userdata = GetData(who, false); + if (!userdata) + return; - if (!ServerInstance->Modes->AddMode(myumode)) + // Iterate over the list of people who accept me, and remove all entries + for (std::list::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++) { - delete mycommand; - delete myumode; - throw ModuleException("Could not add usermode +g"); - } - try - { - ServerInstance->AddCommand(mycommand); - } - catch (const ModuleException& e) - { - delete mycommand; - delete myumode; - throw ModuleException("Could not add command!"); + callerid_data *dat = *(it); + + // Find me on their callerid list + std::set::iterator it2 = dat->accepting.find(who); + + if (it2 != dat->accepting.end()) + dat->accepting.erase(it2); } + userdata->wholistsme.clear(); + } + +public: + ModuleCallerID(InspIRCd* Me) : Module(Me), mycommand(Me, maxaccepts), myumode(Me) + { + OnRehash(NULL); + + if (!ServerInstance->Modes->AddMode(&myumode)) + throw ModuleException("Could not add usermode +g"); + + ServerInstance->AddCommand(&mycommand); + Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup }; ServerInstance->Modules->Attach(eventlist, this, 7); } virtual ~ModuleCallerID() { - ServerInstance->Modes->DelMode(myumode); - delete myumode; + ServerInstance->Modes->DelMode(&myumode); } virtual Version GetVersion() { - return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION); + return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION); } virtual void On005Numeric(std::string& output) @@ -320,17 +379,24 @@ public: return 0; callerid_data* dat = GetData(dest, true); - std::set::iterator i = dat->accepting.find(dest); + std::set::iterator i = dat->accepting.find(user); if (i == dat->accepting.end()) { - time_t now = time(NULL); + time_t now = ServerInstance->Time(); /* +g and *not* accepted */ user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str()); if (now > (dat->lastnotify + (time_t)notify_cooldown)) { user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str()); - dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str()); + if (IS_LOCAL(dest)) + { + dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str()); + } + else + { + ServerInstance->PI->PushToClient(dest, std::string("::") + ServerInstance->Config->ServerName + " 718 " + dest->nick + " " + user->nick + " " + user->ident + "@" + user->dhost + " :is messaging you, and you have umode +g. Use /ACCEPT +" + user->nick + " to allow."); + } dat->lastnotify = now; } return 1; @@ -390,17 +456,17 @@ public: virtual int OnUserPreNick(User* user, const std::string& newnick) { if (!tracknick) - RemoveFromAllAccepts(ServerInstance, user); + RemoveFromAllAccepts(user); return 0; } virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) { + RemoveFromAllAccepts(user); RemoveData(user); - RemoveFromAllAccepts(ServerInstance, user); } - virtual void OnRehash(User* user, const std::string& parameter) + virtual void OnRehash(User* user) { ConfigReader Conf(ServerInstance); maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);