]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_callerid.cpp
Strip SUPPORT_IP6LINKS #define
[user/henk/code/inspircd.git] / src / modules / m_callerid.cpp
index 0204ee77f0f0b4312e83fafda501cdea12bd2a7e..a80246d64d0bd0a9ebb28fd51ff3d2bc3a7a2f09 100644 (file)
@@ -2,8 +2,8 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *  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.
@@ -22,8 +22,15 @@ class callerid_data : public classbase
 {
  public:
        time_t lastnotify;
+
+       /** Users I accept messages from
+        */
        std::set<User*> accepting;
 
+       /** Users who list me as accepted
+        */
+       std::list<callerid_data *> 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;
@@ -66,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;
@@ -92,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<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
        {
-               callerid_data* dat = GetData(i->second, false);
-
-               if (!dat)
-                       continue;
+               callerid_data *targ = GetData(*it, false);
 
-               std::set<User*>::iterator iter = dat->accepting.find(who);
+               if (!targ)
+                       continue; // shouldn't happen, but oh well.
 
-               if (iter == dat->accepting.end())
-                       continue;
-
-               dat->accepting.erase(iter);
+               for (std::list<callerid_data *>::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:
@@ -224,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)
                {
@@ -240,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)
                {
@@ -263,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<callerid_data *>::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;
        }
 };
@@ -281,10 +316,35 @@ private:
        bool tracknick; // Allow ACCEPT entries to update with nick changes.
        unsigned int notify_cooldown; // Seconds between notifications.
 
+       /** Removes a user from all accept lists
+        * @param who The user to remove from accepts
+        */
+       void RemoveFromAllAccepts(User* who)
+       {
+               // First, find the list of people who have me on accept
+               callerid_data *userdata = GetData(who, false);
+               if (!userdata)
+                       return;
+
+               // Iterate over the list of people who accept me, and remove all entries
+               for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
+               {
+                       callerid_data *dat = *(it);
+
+                       // Find me on their callerid list
+                       std::set<User *>::iterator it2 = dat->accepting.find(who);
+
+                       if (it2 != dat->accepting.end())
+                               dat->accepting.erase(it2);
+               }
+
+               userdata->wholistsme.clear();
+       }
+
 public:
        ModuleCallerID(InspIRCd* Me) : Module(Me)
        {
-               OnRehash(NULL, "");
+               OnRehash(NULL);
                mycommand = new CommandAccept(ServerInstance, maxaccepts);
                myumode = new User_g(ServerInstance);
 
@@ -338,13 +398,20 @@ public:
 
                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. 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;
@@ -404,17 +471,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);