1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
25 #include "helperfuncs.h"
28 /* $ModDesc: Provides support for the /watch command */
32 class watchentry : public classbase
39 typedef std::vector<watchentry*> watchlist;
42 class cmd_watch : public command_t
45 cmd_watch() : command_t("WATCH",0,0)
47 this->source = "m_watch.so";
48 syntax = "[C|L|S]|[+|-<nick>]";
51 void Handle (const char** parameters, int pcnt, userrec *user)
55 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
57 watchentry* a = (watchentry*)(*q);
58 if (a->watcher == user)
60 userrec* targ = Srv->FindNick(a->target);
63 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
67 WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
71 for (int x = 0; x < pcnt; x++)
73 const char *nick = parameters[x];
74 if (!strcasecmp(nick,"C"))
81 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
83 watchentry* a = (watchentry*)(*q);
84 if (a->watcher == user)
94 else if (!strcasecmp(nick,"L"))
96 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
98 watchentry* a = (watchentry*)(*q);
99 if (a->watcher == user)
101 userrec* targ = Srv->FindNick(a->target);
104 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
108 WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
110 else if (!strcasecmp(nick,"S"))
112 std::string list = "";
113 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
115 watchentry* a = (watchentry*)(*q);
116 if (a->watcher == user)
118 list.append(" ").append(a->target);
121 char* l = (char*)list.c_str();
124 WriteServ(user->fd,"606 %s :%s",user->nick,l);
125 WriteServ(user->fd,"607 %s :End of WATCH S",user->nick);
127 else if (nick[0] == '-')
129 // removing an item from the list
131 irc::string n1 = nick;
132 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
134 watchentry* b = (watchentry*)(*q);
135 if (b->watcher == user)
137 irc::string n2 = b->target.c_str();
138 userrec* a = Srv->FindNick(b->target);
141 WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
145 WriteServ(user->fd,"602 %s %s * * 0 :stopped watching",user->nick,b->target.c_str());
156 else if (nick[0] == '+')
159 irc::string n1 = nick;
161 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
163 watchentry* a = (watchentry*)(*q);
164 if (a->watcher == user)
166 irc::string n2 = a->target.c_str();
169 // already on watch list
176 watchentry* w = new watchentry();
179 watches.push_back(w);
180 log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
182 userrec* a = Srv->FindNick(nick);
185 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
189 WriteServ(user->fd,"605 %s %s * * 0 :is offline",user->nick,nick);
198 class Modulewatch : public Module
200 cmd_watch* mycommand;
203 Modulewatch(Server* Me)
207 mycommand = new cmd_watch();
208 Srv->AddCommand(mycommand);
211 void Implements(char* List)
213 List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
216 virtual void OnUserQuit(userrec* user, const std::string &reason)
218 log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
219 irc::string n2 = user->nick;
220 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
222 watchentry* a = (watchentry*)(*q);
223 irc::string n1 = a->target.c_str();
226 log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,a->watcher->nick);
227 WriteServ(a->watcher->fd,"601 %s %s %s %s %lu :went offline",a->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
234 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
236 watchentry* a = (watchentry*)(*q);
237 if (a->watcher == user)
248 virtual void OnGlobalConnect(userrec* user)
250 irc::string n2 = user->nick;
251 log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
252 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
254 watchentry* a = (watchentry*)(*q);
255 irc::string n1 = a->target.c_str();
258 log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,a->watcher->nick);
259 WriteServ(a->watcher->fd,"600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
264 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
266 irc::string n2 = oldnick.c_str();
267 irc::string n3 = user->nick;
268 log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
269 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
271 watchentry* a = (watchentry*)(*q);
272 irc::string n1 = a->target.c_str();
273 // changed from a nick on the watchlist to one that isnt
276 log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),a->watcher->nick);
277 WriteServ(a->watcher->fd,"601 %s %s %s %s %lu :went offline",a->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
281 // changed from a nick not on notify to one that is
282 log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,a->watcher->nick);
283 WriteServ(a->watcher->fd,"600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
288 virtual void On005Numeric(std::string &output)
290 // we don't really have a limit...
291 output = output + " WATCH=999";
294 virtual ~Modulewatch()
298 virtual Version GetVersion()
300 return Version(1,0,0,1,VF_VENDOR);
305 class ModulewatchFactory : public ModuleFactory
312 ~ModulewatchFactory()
316 virtual Module * CreateModule(Server* Me)
318 return new Modulewatch(Me);
324 extern "C" void * init_module( void )
326 return new ModulewatchFactory;