]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_callerid.cpp
Remove more classbase
[user/henk/code/inspircd.git] / src / modules / m_callerid.cpp
index 873ec7ed24cbac3abcf4930bf31e76d424e94aef..c356a2154cc88470b1d75a3838a01b6973e41a6f 100644 (file)
@@ -18,7 +18,7 @@
 
 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
 
-class callerid_data : public classbase
+class callerid_data
 {
  public:
        time_t lastnotify;
@@ -32,7 +32,7 @@ class callerid_data : public classbase
        std::list<callerid_data *> wholistsme;
 
        callerid_data() : lastnotify(0) { }
-       callerid_data(const std::string& str, InspIRCd* ServerInstance)
+       callerid_data(const std::string& str)
        {
                irc::commasepstream s(str);
                std::string tok;
@@ -56,88 +56,89 @@ class callerid_data : public classbase
                }
        }
 
-       std::string ToString(bool displayable) const
+       std::string ToString(SerializeFormat format) const
        {
                std::ostringstream oss;
                oss << lastnotify;
                for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
                {
+                       User* u = *i;
                        // Encode UIDs.
-                       oss << "," << (displayable ? (*i)->nick : (*i)->uuid);
+                       oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
                }
                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)
+struct CallerIDExtInfo : public ExtensionItem
 {
-       callerid_data* dat;
-       if (who->GetExt("callerid_data", dat))
-               return dat;
-       else
+       CallerIDExtInfo(Module* parent)
+               : ExtensionItem("callerid_data", parent)
        {
-               if (extend)
-               {
-                       dat = new callerid_data;
-                       who->Extend("callerid_data", dat);
-                       return dat;
-               }
-               else
-                       return NULL;
        }
-}
 
-void RemoveData(User* who)
-{
-       callerid_data* dat;
-       who->GetExt("callerid_data", dat);
+       std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
+       {
+               callerid_data* dat = static_cast<callerid_data*>(item);
+               return dat->ToString(format);
+       }
 
-       if (!dat)
-               return;
+       void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
+       {
+               callerid_data* dat = new callerid_data(value);
+               set_raw(container, dat);
+       }
 
-       // 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* get(User* user, bool create)
        {
-               callerid_data *targ = GetData(*it, false);
+               callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
+               if (!dat)
+               {
+                       dat = new callerid_data;
+                       set_raw(user, dat);
+               }
+               return dat;
+       }
 
-               if (!targ)
-                       continue; // shouldn't happen, but oh well.
+       void free(void* item)
+       {
+               callerid_data* dat = static_cast<callerid_data*>(item);
 
-               for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
+               // 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++)
                {
-                       if (*it2 == dat)
+                       callerid_data *targ = this->get(*it, false);
+
+                       if (!targ)
+                               continue; // shouldn't happen, but oh well.
+
+                       for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
                        {
-                               targ->wholistsme.erase(it2);
-                               break;
+                               if (*it2 == dat)
+                               {
+                                       targ->wholistsme.erase(it2);
+                                       break;
+                               }
                        }
                }
        }
-
-       who->Shrink("callerid_data");
-       delete dat;
-}
-
+};
 
 class User_g : public SimpleUserModeHandler
 {
 public:
-       User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
+       User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
 };
 
 class CommandAccept : public Command
 {
-private:
-       unsigned int& maxaccepts;
 public:
-       CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
+       CallerIDExtInfo extInfo;
+       unsigned int maxaccepts;
+       CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
+               extInfo(Creator)
        {
-               source = "m_callerid.so";
                syntax = "{[+|-]<nicks>}|*}";
                TRANSLATE2(TR_CUSTOM, TR_END);
        }
@@ -198,7 +199,7 @@ public:
                {
                        if (IS_LOCAL(user))
                                ListAccept(user);
-                       return CMD_LOCALONLY;
+                       return CMD_SUCCESS;
                }
                else if (tok[0] == '-')
                {
@@ -223,7 +224,7 @@ public:
 
        void ListAccept(User* user)
        {
-               callerid_data* dat = GetData(user, false);
+               callerid_data* dat = extInfo.get(user, false);
                if (dat)
                {
                        for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
@@ -235,7 +236,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);
+               callerid_data* dat = extInfo.get(user, true);
                if (dat->accepting.size() >= maxaccepts)
                {
                        if (!quiet)
@@ -252,7 +253,7 @@ public:
                }
 
                // Now, look them up, and add me to their list
-               callerid_data *targ = GetData(whotoadd, true);
+               callerid_data *targ = extInfo.get(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());
@@ -262,7 +263,7 @@ public:
        bool RemoveAccept(User* user, User* whotoremove, bool quiet)
        {
                // Remove them from my list, so look up my list..
-               callerid_data* dat = GetData(user, false);
+               callerid_data* dat = extInfo.get(user, false);
                if (!dat)
                {
                        if (!quiet)
@@ -282,7 +283,7 @@ public:
                dat->accepting.erase(i);
 
                // Look up their list to remove me.
-               callerid_data *dat2 = GetData(whotoremove, false);
+               callerid_data *dat2 = extInfo.get(whotoremove, false);
                if (!dat2)
                {
                        // How the fuck is this possible.
@@ -307,11 +308,10 @@ public:
 class ModuleCallerID : public Module
 {
 private:
-       CommandAccept *mycommand;
-       User_g* myumode;
+       CommandAccept cmd;
+       User_g myumode;
 
        // Configuration variables:
-       unsigned int maxaccepts; // Maximum ACCEPT entries.
        bool operoverride; // Operators can override callerid.
        bool tracknick; // Allow ACCEPT entries to update with nick changes.
        unsigned int notify_cooldown; // Seconds between notifications.
@@ -322,7 +322,7 @@ private:
        void RemoveFromAllAccepts(User* who)
        {
                // First, find the list of people who have me on accept
-               callerid_data *userdata = GetData(who, false);
+               callerid_data *userdata = cmd.extInfo.get(who, false);
                if (!userdata)
                        return;
 
@@ -342,42 +342,27 @@ private:
        }
 
 public:
-       ModuleCallerID(InspIRCd* Me) : Module(Me)
+       ModuleCallerID() : cmd(this), myumode(this)
        {
                OnRehash(NULL);
-               mycommand = new CommandAccept(ServerInstance, maxaccepts);
-               myumode = new User_g(ServerInstance);
 
-               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!");
-               }
 
-               Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
-               ServerInstance->Modules->Attach(eventlist, this, 7);
+               ServerInstance->AddCommand(&cmd);
+               ServerInstance->Extensions.Register(&cmd.extInfo);
+
+               Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
+               ServerInstance->Modules->Attach(eventlist, this, 6);
        }
 
        virtual ~ModuleCallerID()
        {
-               ServerInstance->Modes->DelMode(myumode);
-               delete myumode;
        }
 
        virtual Version GetVersion()
        {
-               return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
+               return Version("Implementation of callerid (umode +g & /accept, ala hybrid etc)", VF_COMMON | VF_VENDOR);
        }
 
        virtual void On005Numeric(std::string& output)
@@ -385,102 +370,66 @@ public:
                output += " CALLERID=g";
        }
 
-       int PreText(User* user, User* dest, std::string& text, bool notice)
+       ModResult PreText(User* user, User* dest, std::string& text, bool notice)
        {
                if (!dest->IsModeSet('g'))
-                       return 0;
+                       return MOD_RES_PASSTHRU;
 
                if (operoverride && IS_OPER(user))
-                       return 0;
+                       return MOD_RES_PASSTHRU;
 
-               callerid_data* dat = GetData(dest, true);
+               callerid_data* dat = cmd.extInfo.get(dest, true);
                std::set<User*>::iterator i = dat->accepting.find(user);
 
                if (i == dat->accepting.end())
                {
                        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());
+                               ServerInstance->DumpText(dest, ":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
+                                       ServerInstance->Config->ServerName.c_str(), dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
                                dat->lastnotify = now;
                        }
-                       return 1;
+                       return MOD_RES_DENY;
                }
-               return 0;
+               return MOD_RES_PASSTHRU;
        }
 
-       virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
+       virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
        {
                if (IS_LOCAL(user) && target_type == TYPE_USER)
                        return PreText(user, (User*)dest, text, true);
 
-               return 0;
+               return MOD_RES_PASSTHRU;
        }
 
-       virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
+       virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
        {
                if (IS_LOCAL(user) && target_type == TYPE_USER)
                        return PreText(user, (User*)dest, text, true);
 
-               return 0;
-       }
-
-       virtual void OnCleanup(int type, void* item)
-       {
-               if (type != TYPE_USER)
-                       return;
-
-               User* u = (User*)item;
-               /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do.  */
-               RemoveData(u);
-       }
-
-       virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable)
-       {
-               if (extname == "callerid_data")
-               {
-                       callerid_data* dat = GetData(user, false);
-                       if (dat)
-                       {
-                               std::string str = dat->ToString(displayable);
-                               proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str);
-                       }
-               }
-       }
-
-       virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata)
-       {
-               if (target_type == TYPE_USER && extname == "callerid_data")
-               {
-                       User* u = (User*)target;
-                       callerid_data* dat = new callerid_data(extdata, ServerInstance);
-                       u->Extend("callerid_data", dat);
-               }
+               return MOD_RES_PASSTHRU;
        }
 
-       virtual int OnUserPreNick(User* user, const std::string& newnick)
+       ModResult OnUserPreNick(User* user, const std::string& newnick)
        {
                if (!tracknick)
                        RemoveFromAllAccepts(user);
-               return 0;
+               return MOD_RES_PASSTHRU;
        }
 
-       virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
+       void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
        {
                RemoveFromAllAccepts(user);
-               RemoveData(user);
        }
 
        virtual void OnRehash(User* user)
        {
-               ConfigReader Conf(ServerInstance);
-               maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
+               ConfigReader Conf;
+               cmd.maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
                operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
                tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
                notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);