]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_watch.cpp
Works with the m_testclient test program/suite!
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
index f613acda3665a13eb0026d2daa1871d14d5c4c17..c15609a4d07e11fafffa02ce764884aa33060970 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
  *                       E-mail:
  *                <brain@chatspike.net>
  *               <Craig@chatspike.net>
@@ -27,9 +27,9 @@ using namespace std;
 
 /* $ModDesc: Provides support for the /watch command */
 
-Server *Srv;
+static Server *Srv;
 
-class watchentry
+class watchentry : public classbase
 {
  public:
        userrec* watcher;
@@ -39,157 +39,172 @@ class watchentry
 typedef std::vector<watchentry> watchlist;
 watchlist watches;
 
-void handle_watch(char **parameters, int pcnt, userrec *user)
+class cmd_watch : public command_t
 {
-       if (!pcnt)
+ public:
+       cmd_watch() : command_t("WATCH",0,0)
        {
-                for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
-                {
-                        if (q->watcher == user)
-                        {
-                               userrec* targ = Srv->FindNick(q->target);
-                               if (targ)
-                               {
-                                       WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
-                               }
-                        }
-                }
-               WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
+               this->source = "m_watch.so";
        }
-       else if (pcnt > 0)
+
+       void Handle (const char** parameters, int pcnt, userrec *user)
        {
-               for (int x = 0; x < pcnt; x++)
+               if (!pcnt)
                {
-                       char *nick = parameters[x];
-                       if (!strcasecmp(nick,"C"))
-                       {
-                               // watch clear
-                               bool done = false;
-                               while (!done)
-                               {
-                                       done = true;
-                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
-                                       {
-                                               if (q->watcher == user)
-                                               {
-                                                       done = false;
-                                                       watches.erase(q);
-                                                       break;
-                                               }
-                                       }
-                               }
-                       }
-                       if (!strcasecmp(nick,"L"))
-                       {
-                               for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
-                               {
-                                       if (q->watcher == user)
-                                       {
-                                               userrec* targ = Srv->FindNick(q->target);
-                                               if (targ)
-                                               {
-                                                       WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
-                                               }
-                                       }
-                               }
-                               WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
-                       }
-                       if (!strcasecmp(nick,"S"))
-                       {
-                               std::string list = "";
-                               for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
-                               {
-                                       if (q->watcher == user)
-                                       {
-                                               list = list + " " + q->target;
-                                       }
-                               }
-                               char* l = (char*)list.c_str();
-                               if (*l == ' ')
-                                       l++;
-                               WriteServ(user->fd,"606 %s :%s",user->nick,l);
-                               WriteServ(user->fd,"607 %s :End of WATCH S",user->nick);
-                       }
-                       if (nick[0] == '-')
+                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                        {
-                               // removing an item from the list
-                               nick++;
-                               irc::string n1 = nick;
-                               for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
-                               {
-                                       if (q->watcher == user)
-                                       {
-                                               irc::string n2 = q->target.c_str();
-                                               userrec* a = Srv->FindNick(q->target);
-                                                if (a)
-                                                {
-                                                        WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
-                                                }
-                                                else
-                                                {
-                                                         WriteServ(user->fd,"602 %s %s * * 0 :stopped watching",user->nick,q->target.c_str());
-                                                }
-                                                if (n1 == n2)
-                                                {
-                                                        watches.erase(q);
-                                                        break;
-                                                }
+                               if (q->watcher == user)
+                               {
+                                       userrec* targ = Srv->FindNick(q->target);
+                                       if (targ)
+                                       {
+                                               WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
                                        }
                                }
                        }
-                       else if (nick[0] == '+')
+                       WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
+               }
+               else if (pcnt > 0)
+               {
+                       for (int x = 0; x < pcnt; x++)
                        {
-                               nick++;
-                               irc::string n1 = nick;
-                               bool exists = false;
-                               for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                               const char *nick = parameters[x];
+                               if (!strcasecmp(nick,"C"))
+                               {
+                                       // watch clear
+                                       bool done = false;
+                                       while (!done)
+                                       {
+                                               done = true;
+                                               for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                               {
+                                                       if (q->watcher == user)
+                                                       {
+                                                               done = false;
+                                                               watches.erase(q);
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
+                               else if (!strcasecmp(nick,"L"))
+                               {
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                       {
+                                               if (q->watcher == user)
+                                               {
+                                                       userrec* targ = Srv->FindNick(q->target);
+                                                       if (targ)
+                                                       {
+                                                               WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
+                                                       }
+                                               }
+                                       }
+                                       WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
+                               }
+                               else if (!strcasecmp(nick,"S"))
                                {
-                                       if (q->watcher == user)
+                                       std::string list = "";
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                                        {
-                                               irc::string n2 = q->target.c_str();
-                                               if (n1 == n2)
+                                               if (q->watcher == user)
                                                {
-                                                       // already on watch list
-                                                       exists = true;
+                                                       list = list + " " + q->target;
                                                }
                                        }
+                                       char* l = (char*)list.c_str();
+                                       if (*l == ' ')
+                                               l++;
+                                       WriteServ(user->fd,"606 %s :%s",user->nick,l);
+                                       WriteServ(user->fd,"607 %s :End of WATCH S",user->nick);
                                }
-                               if (!exists)
+                               else if (nick[0] == '-')
                                {
-                                       watchentry w;
-                                       w.watcher = user;
-                                       w.target = nick;
-                                       watches.push_back(w);
-                                       log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
+                                       // removing an item from the list
+                                       nick++;
+                                       irc::string n1 = nick;
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                       {
+                                               if (q->watcher == user)
+                                               {
+                                                       irc::string n2 = q->target.c_str();
+                                                       userrec* a = Srv->FindNick(q->target);
+                                                       if (a)
+                                                       {
+                                                               WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
+                                                       }
+                                                       else
+                                                       {
+                                                                WriteServ(user->fd,"602 %s %s * * 0 :stopped watching",user->nick,q->target.c_str());
+                                                       }
+                                                       if (n1 == n2)
+                                                       {
+                                                               watches.erase(q);
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
+                               else if (nick[0] == '+')
+                               {
+                                       nick++;
+                                       irc::string n1 = nick;
+                                       bool exists = false;
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                       {
+                                               if (q->watcher == user)
+                                               {
+                                                       irc::string n2 = q->target.c_str();
+                                                       if (n1 == n2)
+                                                       {
+                                                               // already on watch list
+                                                               exists = true;
+                                                       }
+                                               }
+                                       }
+                                       if (!exists)
+                                       {
+                                               watchentry w;
+                                               w.watcher = user;
+                                               w.target = nick;
+                                               watches.push_back(w);
+                                               log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
+                                       }
+                                       userrec* a = Srv->FindNick(nick);
+                                       if (a)
+                                       {
+                                               WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
+                                       }
+                                       else
+                                       {
+                                               WriteServ(user->fd,"605 %s %s * * 0 :is offline",user->nick,nick);
+                                       }
                                }
-                                userrec* a = Srv->FindNick(nick);
-                                if (a)
-                                {
-                                        WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
-                                }
-                                else
-                                {
-                                        WriteServ(user->fd,"605 %s %s * * 0 :is offline",user->nick,nick);
-                                }
                        }
                }
+               return;
        }
-       return;
-}
-
+};
 
 class Modulewatch : public Module
 {
-
+       cmd_watch* mycommand;
  public:
 
-       Modulewatch()
+       Modulewatch(Server* Me)
+               : Module::Module(Me)
+       {
+               Srv = Me;
+               mycommand = new cmd_watch();
+               Srv->AddCommand(mycommand);
+       }
+
+       void Implements(char* List)
        {
-               Srv = new Server;
-               Srv->AddCommand("WATCH",handle_watch,0,0,"m_watch.so");
+               List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
        }
 
-       virtual void OnUserQuit(userrec* user)
+       virtual void OnUserQuit(userrec* user, const std::string &reason)
        {
                log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
                irc::string n2 = user->nick;
@@ -233,7 +248,7 @@ class Modulewatch : public Module
                }
        }
 
-       virtual void OnUserPostNick(userrec* user, std::string oldnick)
+       virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
        {
                irc::string n2 = oldnick.c_str();
                irc::string n3 = user->nick;
@@ -264,7 +279,6 @@ class Modulewatch : public Module
        
        virtual ~Modulewatch()
        {
-               delete Srv;
        }
        
        virtual Version GetVersion()
@@ -285,9 +299,9 @@ class ModulewatchFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule()
+       virtual Module * CreateModule(Server* Me)
        {
-               return new Modulewatch;
+               return new Modulewatch(Me);
        }
        
 };