X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_callerid.cpp;h=29ba7fd6022f2c113a1e7365519f35df5012115f;hb=f25c4b7a2263f5f3ce9bb41ba56b43c0d3a6d124;hp=80694307e3246c322724d915a89e9f961b03c7d3;hpb=50d763ca7480ecd6df08e137c9f0b8ee286843c9;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_callerid.cpp b/src/modules/m_callerid.cpp index 80694307e..29ba7fd60 100644 --- a/src/modules/m_callerid.cpp +++ b/src/modules/m_callerid.cpp @@ -22,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) { @@ -39,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; @@ -52,20 +56,25 @@ class callerid_data : public classbase } } - std::string ToString(bool displayable) const + std::string ToString(Module* proto) const { std::ostringstream oss; oss << lastnotify; for (std::set::const_iterator i = accepting.begin(); i != accepting.end(); ++i) { // Encode UIDs. - oss << "," << (displayable ? (*i)->nick : (*i)->uuid); + oss << "," << proto->ProtoTranslate(*i); } oss << std::ends; return oss.str(); } }; +/** 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; @@ -92,6 +101,24 @@ void RemoveData(User* who) if (!dat) return; + // 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 *targ = GetData(*it, false); + + if (!targ) + continue; // shouldn't happen, but oh well. + + 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; } @@ -207,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) { @@ -223,12 +251,17 @@ 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) { @@ -246,8 +279,27 @@ public: return false; } - user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str()); 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; } }; @@ -255,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. @@ -269,45 +321,35 @@ private: */ void RemoveFromAllAccepts(User* who) { - for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i) - { - callerid_data* dat = GetData(i->second, false); - - if (!dat) - continue; + // First, find the list of people who have me on accept + callerid_data *userdata = GetData(who, false); + if (!userdata) + return; - std::set::iterator iter = dat->accepting.find(who); + // 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++) + { + callerid_data *dat = *(it); - if (iter == dat->accepting.end()) - continue; + // Find me on their callerid list + std::set::iterator it2 = dat->accepting.find(who); - dat->accepting.erase(iter); + if (it2 != dat->accepting.end()) + dat->accepting.erase(it2); } + + userdata->wholistsme.clear(); } public: - ModuleCallerID(InspIRCd* Me) : Module(Me) + ModuleCallerID(InspIRCd* Me) : Module(Me), mycommand(Me, maxaccepts), myumode(Me) { - OnRehash(NULL, ""); - mycommand = new CommandAccept(ServerInstance, maxaccepts); - myumode = new User_g(ServerInstance); + OnRehash(NULL); - if (!ServerInstance->Modes->AddMode(myumode)) - { - delete mycommand; - delete myumode; + if (!ServerInstance->Modes->AddMode(&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!"); - } + + 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); @@ -315,8 +357,7 @@ public: virtual ~ModuleCallerID() { - ServerInstance->Modes->DelMode(myumode); - delete myumode; + ServerInstance->Modes->DelMode(&myumode); } virtual Version GetVersion() @@ -344,14 +385,18 @@ public: { time_t now = ServerInstance->Time(); /* +g and *not* accepted */ - if (IS_LOCAL(user)) - user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str()); - else - ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 716 " + user->nick + dest->nick + " :is in +g mode (server-side ignore)."); + 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. 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()); + 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; @@ -385,24 +430,21 @@ public: RemoveData(u); } - virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable) + virtual void OnSyncUser(User* user, Module* proto, void* opaque) { - if (extname == "callerid_data") + callerid_data* dat = GetData(user, false); + if (dat) { - callerid_data* dat = GetData(user, false); - if (dat) - { - std::string str = dat->ToString(displayable); - proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str); - } + std::string str = dat->ToString(proto); + proto->ProtoSendMetaData(opaque, user, "callerid_data", str); } } - virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata) + virtual void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) { - if (target_type == TYPE_USER && extname == "callerid_data") + User* u = dynamic_cast(target); + if (u && extname == "callerid_data") { - User* u = (User*)target; callerid_data* dat = new callerid_data(extdata, ServerInstance); u->Extend("callerid_data", dat); } @@ -417,11 +459,11 @@ public: virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) { - RemoveData(user); RemoveFromAllAccepts(user); + RemoveData(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);