]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_watch.cpp
Move static map of extensions into ServerInstance, add const-correctness
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
index c78bfa638ee399f984803e8cf8d67bb5e9d68d31..1d2073972131f047deb88b8b014d9a75ccaedf1f 100644 (file)
@@ -103,7 +103,7 @@ watchentries* whos_watching_me;
 class CommandSVSWatch : public Command
 {
  public:
-       CommandSVSWatch (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"SVSWATCH", 0, 2)
+       CommandSVSWatch(Module* Creator) : Command(Creator,"SVSWATCH", 2)
        {
                syntax = "<target> [C|L|S]|[+|-<nick>]";
                TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
@@ -125,6 +125,11 @@ class CommandSVSWatch : public Command
 
                return CMD_SUCCESS;
        }
+
+       RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
+       {
+               return ROUTE_BROADCAST;
+       }
 };
 
 /** Handle /WATCH
@@ -133,6 +138,7 @@ class CommandWatch : public Command
 {
        unsigned int& MAX_WATCH;
  public:
+       SimpleExtItem<watchlist> ext;
        CmdResult remove_watch(User* user, const char* nick)
        {
                // removing an item from the list
@@ -142,8 +148,8 @@ class CommandWatch : public Command
                        return CMD_FAILURE;
                }
 
-               watchlist* wl;
-               if (user->GetExt("watchlist", wl))
+               watchlist* wl = ext.get(user);
+               if (wl)
                {
                        /* Yup, is on my list */
                        watchlist::iterator n = wl->find(nick);
@@ -163,8 +169,7 @@ class CommandWatch : public Command
 
                        if (wl->empty())
                        {
-                               user->Shrink("watchlist");
-                               delete wl;
+                               ext.unset(user);
                        }
 
                        watchentries::iterator x = whos_watching_me->find(nick);
@@ -182,7 +187,7 @@ class CommandWatch : public Command
                        }
                }
 
-               return CMD_LOCALONLY;
+               return CMD_SUCCESS;
        }
 
        CmdResult add_watch(User* user, const char* nick)
@@ -193,11 +198,11 @@ class CommandWatch : public Command
                        return CMD_FAILURE;
                }
 
-               watchlist* wl;
-               if (!user->GetExt("watchlist", wl))
+               watchlist* wl = ext.get(user);
+               if (!wl)
                {
                        wl = new watchlist();
-                       user->Extend("watchlist", wl);
+                       ext.set(user, wl);
                }
 
                if (wl->size() == MAX_WATCH)
@@ -226,13 +231,6 @@ class CommandWatch : public Command
                        User* target = ServerInstance->FindNick(nick);
                        if (target)
                        {
-                               if (target->Visibility && !target->Visibility->VisibleTo(user))
-                               {
-                                       (*wl)[nick] = "";
-                                       user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
-                                       return CMD_FAILURE;
-                               }
-
                                (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
                                user->WriteNumeric(604, "%s %s %s :is online",user->nick.c_str(), nick, (*wl)[nick].c_str());
                                if (IS_AWAY(target))
@@ -247,10 +245,10 @@ class CommandWatch : public Command
                        }
                }
 
-               return CMD_LOCALONLY;
+               return CMD_SUCCESS;
        }
 
-       CommandWatch (InspIRCd* Instance, Module* parent, unsigned int &maxwatch) : Command(Instance,parent,"WATCH",0,0), MAX_WATCH(maxwatch)
+       CommandWatch(Module* parent, unsigned int &maxwatch) : Command(parent,"WATCH", 0), MAX_WATCH(maxwatch), ext("watchlist", parent)
        {
                syntax = "[C|L|S]|[+|-<nick>]";
                TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
@@ -260,8 +258,8 @@ class CommandWatch : public Command
        {
                if (parameters.empty())
                {
-                       watchlist* wl;
-                       if (user->GetExt("watchlist", wl))
+                       watchlist* wl = ext.get(user);
+                       if (wl)
                        {
                                for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
                                {
@@ -279,8 +277,8 @@ class CommandWatch : public Command
                                if (!strcasecmp(nick,"C"))
                                {
                                        // watch clear
-                                       watchlist* wl;
-                                       if (user->GetExt("watchlist", wl))
+                                       watchlist* wl = ext.get(user);
+                                       if (wl)
                                        {
                                                for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
                                                {
@@ -299,14 +297,13 @@ class CommandWatch : public Command
                                                        }
                                                }
 
-                                               delete wl;
-                                               user->Shrink("watchlist");
+                                               ext.unset(user);
                                        }
                                }
                                else if (!strcasecmp(nick,"L"))
                                {
-                                       watchlist* wl;
-                                       if (user->GetExt("watchlist", wl))
+                                       watchlist* wl = ext.get(user);
+                                       if (wl)
                                        {
                                                for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
                                                {
@@ -327,12 +324,12 @@ class CommandWatch : public Command
                                }
                                else if (!strcasecmp(nick,"S"))
                                {
-                                       watchlist* wl;
+                                       watchlist* wl = ext.get(user);
                                        int you_have = 0;
                                        int youre_on = 0;
                                        std::string list;
 
-                                       if (user->GetExt("watchlist", wl))
+                                       if (wl)
                                        {
                                                for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
                                                        list.append(q->first.c_str()).append(" ");
@@ -359,7 +356,7 @@ class CommandWatch : public Command
                                }
                        }
                }
-               return CMD_LOCALONLY;
+               return CMD_SUCCESS;
        }
 };
 
@@ -370,20 +367,21 @@ class Modulewatch : public Module
        CommandSVSWatch sw;
 
  public:
-       Modulewatch(InspIRCd* Me)
-               : Module(Me), maxwatch(32), cmdw(Me, this, maxwatch), sw(Me,this) 
+       Modulewatch()
+               : maxwatch(32), cmdw(this, maxwatch), sw(this) 
        {
                OnRehash(NULL);
                whos_watching_me = new watchentries();
                ServerInstance->AddCommand(&cmdw);
                ServerInstance->AddCommand(&sw);
-               Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
-               ServerInstance->Modules->Attach(eventlist, this, 8);
+               ServerInstance->Extensions.Register(&cmdw.ext);
+               Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
+               ServerInstance->Modules->Attach(eventlist, this, 7);
        }
 
        virtual void OnRehash(User* user)
        {
-               ConfigReader Conf(ServerInstance);
+               ConfigReader Conf;
                maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
                if (!maxwatch)
                        maxwatch = 32;
@@ -410,8 +408,7 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
                        {
-                               if (!user->Visibility || user->Visibility->VisibleTo(user))
-                                       (*n)->WriteNumeric(inum, numeric);
+                               (*n)->WriteNumeric(inum, numeric);
                        }
                }
 
@@ -425,19 +422,18 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
                        {
-                               if (!user->Visibility || user->Visibility->VisibleTo(*n))
-                                       (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str() ,user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) ServerInstance->Time());
+                               (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str() ,user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) ServerInstance->Time());
 
-                               watchlist* wl;
-                               if ((*n)->GetExt("watchlist", wl))
+                               watchlist* wl = cmdw.ext.get(*n);
+                               if (wl)
                                        /* We were on somebody's notify list, set ourselves offline */
                                        (*wl)[user->nick.c_str()] = "";
                        }
                }
 
                /* Now im quitting, if i have a notify list, im no longer watching anyone */
-               watchlist* wl;
-               if (user->GetExt("watchlist", wl))
+               watchlist* wl = cmdw.ext.get(user);
+               if (wl)
                {
                        /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
                        for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
@@ -456,10 +452,6 @@ class Modulewatch : public Module
                                                        whos_watching_me->erase(i2);
                                }
                        }
-
-                       /* User's quitting, we're done with this. */
-                       delete wl;
-                       user->Shrink("watchlist");
                }
        }
 
@@ -474,21 +466,6 @@ class Modulewatch : public Module
                delete old_watch;
        }
 
-       virtual void OnCleanup(int target_type, void* item)
-       {
-               if (target_type == TYPE_USER)
-               {
-                       watchlist* wl;
-                       User* user = (User*)item;
-
-                       if (user->GetExt("watchlist", wl))
-                       {
-                               user->Shrink("watchlist");
-                               delete wl;
-                       }
-               }
-       }
-
        virtual void OnPostConnect(User* user)
        {
                watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
@@ -496,11 +473,10 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
                        {
-                               if (!user->Visibility || user->Visibility->VisibleTo(*n))
-                                       (*n)->WriteNumeric(600, "%s %s %s %s %lu :arrived online", (*n)->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
+                               (*n)->WriteNumeric(600, "%s %s %s %s %lu :arrived online", (*n)->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
 
-                               watchlist* wl;
-                               if ((*n)->GetExt("watchlist", wl))
+                               watchlist* wl = cmdw.ext.get(*n);
+                               if (wl)
                                        /* We were on somebody's notify list, set ourselves online */
                                        (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
                        }
@@ -516,11 +492,10 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
                        {
-                               watchlist* wl;
-                               if ((*n)->GetExt("watchlist", wl))
+                               watchlist* wl = cmdw.ext.get(*n);
+                               if (wl)
                                {
-                                       if (!user->Visibility || user->Visibility->VisibleTo(*n))
-                                               (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str(), oldnick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
+                                       (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str(), oldnick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
                                        (*wl)[oldnick.c_str()] = "";
                                }
                        }
@@ -530,12 +505,11 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
                        {
-                               watchlist* wl;
-                               if ((*n)->GetExt("watchlist", wl))
+                               watchlist* wl = cmdw.ext.get(*n);
+                               if (wl)
                                {
                                        (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
-                                       if (!user->Visibility || user->Visibility->VisibleTo(*n))
-                                               (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
+                                       (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
                                }
                        }
                }
@@ -554,7 +528,7 @@ class Modulewatch : public Module
 
        virtual Version GetVersion()
        {
-               return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
+               return Version("Provides support for the /WATCH command", VF_COMMON | VF_VENDOR, API_VERSION);
        }
 };