]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_watch.cpp
Annotations
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
index 58d0a4bd78f417274e88638b66b243f704e857cc..09dc8d384b5ee69c44fee633bbfee94bf78deac7 100644 (file)
@@ -22,12 +22,14 @@ using namespace std;
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
-#include "helperfuncs.h"
+
 #include "hashcomp.h"
+#include "inspircd.h"
 
 /* $ModDesc: Provides support for the /watch command */
 
-static Server *Srv;
+
+
 
 class watchentry : public classbase
 {
@@ -42,12 +44,13 @@ watchlist watches;
 class cmd_watch : public command_t
 {
  public:
-       cmd_watch() : command_t("WATCH",0,0)
+       cmd_watch (InspIRCd* Instance) : command_t(Instance,"WATCH",0,0)
        {
                this->source = "m_watch.so";
+               syntax = "[C|L|S]|[+|-<nick>]";
        }
 
-       void Handle (const char** parameters, int pcnt, userrec *user)
+       CmdResult Handle (const char** parameters, int pcnt, userrec *user)
        {
                if (!pcnt)
                {
@@ -56,14 +59,14 @@ class cmd_watch : public command_t
                                watchentry* a = (watchentry*)(*q);
                                if (a->watcher == user)
                                {
-                                       userrec* targ = Srv->FindNick(a->target);
+                                       userrec* targ = ServerInstance->FindNick(a->target);
                                        if (targ)
                                        {
-                                               WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
+                                               user->WriteServ("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);
+                       user->WriteServ("607 %s :End of WATCH list",user->nick);
                }
                else if (pcnt > 0)
                {
@@ -97,14 +100,14 @@ class cmd_watch : public command_t
                                                watchentry* a = (watchentry*)(*q);
                                                if (a->watcher == user)
                                                {
-                                                       userrec* targ = Srv->FindNick(a->target);
+                                                       userrec* targ = ServerInstance->FindNick(a->target);
                                                        if (targ)
                                                        {
-                                                               WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
+                                                               user->WriteServ("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);
+                                       user->WriteServ("607 %s :End of WATCH list",user->nick);
                                }
                                else if (!strcasecmp(nick,"S"))
                                {
@@ -120,13 +123,18 @@ class cmd_watch : public command_t
                                        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);
+                                       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++;
+                                       if (!ServerInstance->IsNick(nick))
+                                       {
+                                               user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
+                                               return CMD_FAILURE;
+                                       }
                                        irc::string n1 = nick;
                                        for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                                        {
@@ -134,14 +142,14 @@ class cmd_watch : public command_t
                                                if (b->watcher == user)
                                                {
                                                        irc::string n2 = b->target.c_str();
-                                                       userrec* a = Srv->FindNick(b->target);
+                                                       userrec* a = ServerInstance->FindNick(b->target);
                                                        if (a)
                                                        {
-                                                               WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
+                                                               user->WriteServ("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,b->target.c_str());
+                                                               user->WriteServ("602 %s %s * * 0 :stopped watching",user->nick,b->target.c_str());
                                                        }
                                                        if (n1 == n2)
                                                        {
@@ -155,6 +163,11 @@ class cmd_watch : public command_t
                                else if (nick[0] == '+')
                                {
                                        nick++;
+                                       if (!ServerInstance->IsNick(nick))
+                                       {
+                                               user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
+                                               return CMD_FAILURE;
+                                       }
                                        irc::string n1 = nick;
                                        bool exists = false;
                                        for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
@@ -176,21 +189,22 @@ class cmd_watch : public command_t
                                                w->watcher = user;
                                                w->target = nick;
                                                watches.push_back(w);
-                                               log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
+                                               ServerInstance->Log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
                                        }
-                                       userrec* a = Srv->FindNick(nick);
+                                       userrec* a = ServerInstance->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);
+                                               user->WriteServ("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);
+                                               user->WriteServ("605 %s %s * * 0 :is offline",user->nick,nick);
                                        }
                                }
                        }
                }
-               return;
+               /* So that spanningtree doesnt pass the WATCH commands to the network! */
+               return CMD_FAILURE;
        }
 };
 
@@ -199,22 +213,22 @@ class Modulewatch : public Module
        cmd_watch* mycommand;
  public:
 
-       Modulewatch(Server* Me)
+       Modulewatch(InspIRCd* Me)
                : Module::Module(Me)
        {
-               Srv = Me;
-               mycommand = new cmd_watch();
-               Srv->AddCommand(mycommand);
+               
+               mycommand = new cmd_watch(ServerInstance);
+               ServerInstance->AddCommand(mycommand);
        }
 
        void Implements(char* List)
        {
-               List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
+               List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
        }
 
        virtual void OnUserQuit(userrec* user, const std::string &reason)
        {
-               log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
+               ServerInstance->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++)
                {
@@ -222,8 +236,8 @@ class Modulewatch : public Module
                        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,a->watcher->nick);
-                               WriteServ(a->watcher->fd,"601 %s %s %s %s %lu :went offline",a->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
+                               ServerInstance->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;
@@ -244,18 +258,18 @@ class Modulewatch : public Module
                }
        }
 
-       virtual void OnGlobalConnect(userrec* user)
+       virtual void OnPostConnect(userrec* user)
        {
                irc::string n2 = user->nick;
-               log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
+               ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
                for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
                {
                        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,a->watcher->nick);
-                               WriteServ(a->watcher->fd,"600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
+                               ServerInstance->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);
                        }
                }
        }
@@ -264,7 +278,7 @@ class Modulewatch : public Module
        {
                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);
+               ServerInstance->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++)
                {
                        watchentry* a = (watchentry*)(*q);
@@ -272,14 +286,14 @@ class Modulewatch : public Module
                        // 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(),a->watcher->nick);
-                               WriteServ(a->watcher->fd,"601 %s %s %s %s %lu :went offline",a->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
+                               ServerInstance->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,a->watcher->nick);
-                               WriteServ(a->watcher->fd,"600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
+                               ServerInstance->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);
                        }
                }
        }       
@@ -312,7 +326,7 @@ class ModulewatchFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule(Server* Me)
+       virtual Module * CreateModule(InspIRCd* Me)
        {
                return new Modulewatch(Me);
        }