]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_watch.cpp
So much stuff changed in this one, i forgot most of it.
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
index bd3a2f707e46e7ff8da895d70db8404327e3517a..8e6b2c50ca5d003ad4420142da70694fdfcebffd 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>
@@ -24,183 +24,209 @@ using namespace std;
 #include "modules.h"
 #include "helperfuncs.h"
 #include "hashcomp.h"
+#include "inspircd.h"
 
 /* $ModDesc: Provides support for the /watch command */
 
-Server *Srv;
+static Server *Srv;
+extern InspIRCd* ServerInstance;
 
-class watchentry
+class watchentry : public classbase
 {
  public:
        userrec* watcher;
        std::string target;
 };
 
-typedef std::vector<watchentry> watchlist;
+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";
+               syntax = "[C|L|S]|[+|-<nick>]";
        }
-       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;
-                                               }
-                                       }
-                               }
-                       }
-                       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"))
-                       {
-                               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);
-                       }
-                       else 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;
-                                                }
+                               watchentry* a = (watchentry*)(*q);
+                               if (a->watcher == user)
+                               {
+                                       userrec* targ = ServerInstance->FindNick(a->target);
+                                       if (targ)
+                                       {
+                                               user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
                                        }
                                }
                        }
-                       else if (nick[0] == '+')
+                       user->WriteServ("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++)
+                                               {
+                                                       watchentry* a = (watchentry*)(*q);
+                                                       if (a->watcher == user)
+                                                       {
+                                                               done = false;
+                                                               watches.erase(q);
+                                                               delete a;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
+                               else if (!strcasecmp(nick,"L"))
                                {
-                                       if (q->watcher == user)
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                                        {
-                                               irc::string n2 = q->target.c_str();
-                                               if (n1 == n2)
+                                               watchentry* a = (watchentry*)(*q);
+                                               if (a->watcher == user)
                                                {
-                                                       // already on watch list
-                                                       exists = true;
+                                                       userrec* targ = ServerInstance->FindNick(a->target);
+                                                       if (targ)
+                                                       {
+                                                               user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
+                                                       }
                                                }
                                        }
+                                       user->WriteServ("607 %s :End of WATCH list",user->nick);
                                }
-                               if (!exists)
+                               else if (!strcasecmp(nick,"S"))
                                {
-                                       watchentry w;
-                                       w.watcher = user;
-                                       w.target = nick;
-                                       watches.push_back(w);
-                                       log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
+                                       std::string list = "";
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                       {
+                                               watchentry* a = (watchentry*)(*q);
+                                               if (a->watcher == user)
+                                               {
+                                                       list.append(" ").append(a->target);
+                                               }
+                                       }
+                                       char* l = (char*)list.c_str();
+                                       if (*l == ' ')
+                                               l++;
+                                       user->WriteServ("606 %s :%s",user->nick,l);
+                                       user->WriteServ("607 %s :End of WATCH S",user->nick);
+                               }
+                               else if (nick[0] == '-')
+                               {
+                                       // removing an item from the list
+                                       nick++;
+                                       irc::string n1 = nick;
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                       {
+                                               watchentry* b = (watchentry*)(*q);
+                                               if (b->watcher == user)
+                                               {
+                                                       irc::string n2 = b->target.c_str();
+                                                       userrec* a = ServerInstance->FindNick(b->target);
+                                                       if (a)
+                                                       {
+                                                               user->WriteServ("602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
+                                                       }
+                                                       else
+                                                       {
+                                                                user->WriteServ("602 %s %s * * 0 :stopped watching",user->nick,b->target.c_str());
+                                                       }
+                                                       if (n1 == n2)
+                                                       {
+                                                               watches.erase(q);
+                                                               delete b;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
+                               else if (nick[0] == '+')
+                               {
+                                       nick++;
+                                       irc::string n1 = nick;
+                                       bool exists = false;
+                                       for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
+                                       {
+                                               watchentry* a = (watchentry*)(*q);
+                                               if (a->watcher == user)
+                                               {
+                                                       irc::string n2 = a->target.c_str();
+                                                       if (n1 == n2)
+                                                       {
+                                                               // already on watch list
+                                                               exists = true;
+                                                       }
+                                               }
+                                       }
+                                       if (!exists)
+                                       {
+                                               watchentry* w = new watchentry();
+                                               w->watcher = user;
+                                               w->target = nick;
+                                               watches.push_back(w);
+                                               log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
+                                       }
+                                       userrec* a = ServerInstance->FindNick(nick);
+                                       if (a)
+                                       {
+                                               user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
+                                       }
+                                       else
+                                       {
+                                               user->WriteServ("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(Server* Me)
                : Module::Module(Me)
        {
                Srv = Me;
-               Srv->AddCommand("WATCH",handle_watch,0,0,"m_watch.so");
+               mycommand = new cmd_watch();
+               Srv->AddCommand(mycommand);
+       }
+
+       void Implements(char* List)
+       {
+               List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
        }
 
-       virtual void OnUserQuit(userrec* user, std::string reason)
+       virtual void OnUserQuit(userrec* user, const std::string &reason)
        {
                log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
                irc::string n2 = user->nick;
                for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                {
-                       irc::string n1 = q->target.c_str();
+                       watchentry* a = (watchentry*)(*q);
+                       irc::string n1 = a->target.c_str();
                        if (n1 == n2)
                        {
-                               log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,q->watcher->nick);
-                               WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
+                               log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,a->watcher->nick);
+                               a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
                        }
                }
                bool done = false;
@@ -209,10 +235,12 @@ class Modulewatch : public Module
                        done = true;
                        for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                        {
-                               if (q->watcher == user)
+                               watchentry* a = (watchentry*)(*q);
+                               if (a->watcher == user)
                                {
                                        done = false;
                                        watches.erase(q);
+                                       delete a;
                                        break;
                                }
                        }
@@ -225,34 +253,36 @@ class Modulewatch : public Module
                log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
                for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                {
-                       irc::string n1 = q->target.c_str();
+                       watchentry* a = (watchentry*)(*q);
+                       irc::string n1 = a->target.c_str();
                        if (n1 == n2)
                        {
-                               log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,q->watcher->nick);
-                               WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age);
+                               log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,a->watcher->nick);
+                               a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
                        }
                }
        }
 
-       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;
                log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
                for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                {
-                       irc::string n1 = q->target.c_str();
+                       watchentry* a = (watchentry*)(*q);
+                       irc::string n1 = a->target.c_str();
                        // changed from a nick on the watchlist to one that isnt
                        if (n1 == n2)
                        {
-                               log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),q->watcher->nick);
-                               WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
+                               log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),a->watcher->nick);
+                               a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
                        }
                        else if (n1 == n3)
                        {
                                // changed from a nick not on notify to one that is
-                               log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,q->watcher->nick);
-                               WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age);
+                               log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,a->watcher->nick);
+                               a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
                        }
                }
        }