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"
29 /* $ModDesc: Provides support for the /watch command */
33 class watchentry : public classbase
40 typedef std::vector<watchentry*> watchlist;
43 class cmd_watch : public command_t
46 cmd_watch() : command_t("WATCH",0,0)
48 this->source = "m_watch.so";
49 syntax = "[C|L|S]|[+|-<nick>]";
52 void Handle (const char** parameters, int pcnt, userrec *user)
56 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
58 watchentry* a = (watchentry*)(*q);
59 if (a->watcher == user)
61 userrec* targ = Srv->FindNick(a->target);
64 user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
68 user->WriteServ("607 %s :End of WATCH list",user->nick);
72 for (int x = 0; x < pcnt; x++)
74 const char *nick = parameters[x];
75 if (!strcasecmp(nick,"C"))
82 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
84 watchentry* a = (watchentry*)(*q);
85 if (a->watcher == user)
95 else if (!strcasecmp(nick,"L"))
97 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
99 watchentry* a = (watchentry*)(*q);
100 if (a->watcher == user)
102 userrec* targ = Srv->FindNick(a->target);
105 user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
109 user->WriteServ("607 %s :End of WATCH list",user->nick);
111 else if (!strcasecmp(nick,"S"))
113 std::string list = "";
114 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
116 watchentry* a = (watchentry*)(*q);
117 if (a->watcher == user)
119 list.append(" ").append(a->target);
122 char* l = (char*)list.c_str();
125 user->WriteServ("606 %s :%s",user->nick,l);
126 user->WriteServ("607 %s :End of WATCH S",user->nick);
128 else if (nick[0] == '-')
130 // removing an item from the list
132 irc::string n1 = nick;
133 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
135 watchentry* b = (watchentry*)(*q);
136 if (b->watcher == user)
138 irc::string n2 = b->target.c_str();
139 userrec* a = Srv->FindNick(b->target);
142 user->WriteServ("602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
146 user->WriteServ("602 %s %s * * 0 :stopped watching",user->nick,b->target.c_str());
157 else if (nick[0] == '+')
160 irc::string n1 = nick;
162 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
164 watchentry* a = (watchentry*)(*q);
165 if (a->watcher == user)
167 irc::string n2 = a->target.c_str();
170 // already on watch list
177 watchentry* w = new watchentry();
180 watches.push_back(w);
181 log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
183 userrec* a = Srv->FindNick(nick);
186 user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
190 user->WriteServ("605 %s %s * * 0 :is offline",user->nick,nick);
199 class Modulewatch : public Module
201 cmd_watch* mycommand;
204 Modulewatch(Server* Me)
208 mycommand = new cmd_watch();
209 Srv->AddCommand(mycommand);
212 void Implements(char* List)
214 List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
217 virtual void OnUserQuit(userrec* user, const std::string &reason)
219 log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
220 irc::string n2 = user->nick;
221 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
223 watchentry* a = (watchentry*)(*q);
224 irc::string n1 = a->target.c_str();
227 log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,a->watcher->nick);
228 a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
235 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
237 watchentry* a = (watchentry*)(*q);
238 if (a->watcher == user)
249 virtual void OnGlobalConnect(userrec* user)
251 irc::string n2 = user->nick;
252 log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
253 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
255 watchentry* a = (watchentry*)(*q);
256 irc::string n1 = a->target.c_str();
259 log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,a->watcher->nick);
260 a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
265 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
267 irc::string n2 = oldnick.c_str();
268 irc::string n3 = user->nick;
269 log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
270 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
272 watchentry* a = (watchentry*)(*q);
273 irc::string n1 = a->target.c_str();
274 // changed from a nick on the watchlist to one that isnt
277 log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),a->watcher->nick);
278 a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
282 // changed from a nick not on notify to one that is
283 log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,a->watcher->nick);
284 a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
289 virtual void On005Numeric(std::string &output)
291 // we don't really have a limit...
292 output = output + " WATCH=999";
295 virtual ~Modulewatch()
299 virtual Version GetVersion()
301 return Version(1,0,0,1,VF_VENDOR);
306 class ModulewatchFactory : public ModuleFactory
313 ~ModulewatchFactory()
317 virtual Module * CreateModule(Server* Me)
319 return new Modulewatch(Me);
325 extern "C" void * init_module( void )
327 return new ModulewatchFactory;