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 * ---------------------------------------------------
28 /* $ModDesc: Provides support for the /watch command */
32 class watchentry : public classbase
39 typedef std::vector<watchentry*> watchlist;
44 class cmd_watch : public command_t
47 cmd_watch (InspIRCd* Instance) : command_t(Instance,"WATCH",0,0)
49 this->source = "m_watch.so";
50 syntax = "[C|L|S]|[+|-<nick>]";
53 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
57 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
59 watchentry* a = (watchentry*)(*q);
60 if (a->watcher == user)
62 userrec* targ = ServerInstance->FindNick(a->target);
65 user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
69 user->WriteServ("607 %s :End of WATCH list",user->nick);
73 for (int x = 0; x < pcnt; x++)
75 const char *nick = parameters[x];
76 if (!strcasecmp(nick,"C"))
83 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
85 watchentry* a = (watchentry*)(*q);
86 if (a->watcher == user)
96 else if (!strcasecmp(nick,"L"))
98 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
100 watchentry* a = (watchentry*)(*q);
101 if (a->watcher == user)
103 userrec* targ = ServerInstance->FindNick(a->target);
106 user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
110 user->WriteServ("607 %s :End of WATCH list",user->nick);
112 else if (!strcasecmp(nick,"S"))
114 std::string list = "";
115 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
117 watchentry* a = (watchentry*)(*q);
118 if (a->watcher == user)
120 list.append(" ").append(a->target);
123 char* l = (char*)list.c_str();
126 user->WriteServ("606 %s :%s",user->nick,l);
127 user->WriteServ("607 %s :End of WATCH S",user->nick);
129 else if (nick[0] == '-')
131 // removing an item from the list
133 if (!ServerInstance->IsNick(nick))
135 user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
138 irc::string n1 = nick;
139 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
141 watchentry* b = (watchentry*)(*q);
142 if (b->watcher == user)
144 irc::string n2 = b->target.c_str();
145 userrec* a = ServerInstance->FindNick(b->target);
148 user->WriteServ("602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
152 user->WriteServ("602 %s %s * * 0 :stopped watching",user->nick,b->target.c_str());
163 else if (nick[0] == '+')
166 if (!ServerInstance->IsNick(nick))
168 user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
171 irc::string n1 = nick;
173 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
175 watchentry* a = (watchentry*)(*q);
176 if (a->watcher == user)
178 irc::string n2 = a->target.c_str();
181 // already on watch list
188 watchentry* w = new watchentry();
191 watches.push_back(w);
192 ServerInstance->Log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
194 userrec* a = ServerInstance->FindNick(nick);
197 user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
201 user->WriteServ("605 %s %s * * 0 :is offline",user->nick,nick);
206 /* So that spanningtree doesnt pass the WATCH commands to the network! */
211 class Modulewatch : public Module
213 cmd_watch* mycommand;
216 Modulewatch(InspIRCd* Me)
220 mycommand = new cmd_watch(ServerInstance);
221 ServerInstance->AddCommand(mycommand);
224 void Implements(char* List)
226 List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
229 virtual void OnUserQuit(userrec* user, const std::string &reason)
231 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
232 irc::string n2 = user->nick;
233 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
235 watchentry* a = (watchentry*)(*q);
236 irc::string n1 = a->target.c_str();
239 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,a->watcher->nick);
240 a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
247 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
249 watchentry* a = (watchentry*)(*q);
250 if (a->watcher == user)
261 virtual void OnPostConnect(userrec* user)
263 irc::string n2 = user->nick;
264 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
265 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
267 watchentry* a = (watchentry*)(*q);
268 irc::string n1 = a->target.c_str();
271 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,a->watcher->nick);
272 a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
277 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
279 irc::string n2 = oldnick.c_str();
280 irc::string n3 = user->nick;
281 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
282 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
284 watchentry* a = (watchentry*)(*q);
285 irc::string n1 = a->target.c_str();
286 // changed from a nick on the watchlist to one that isnt
289 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),a->watcher->nick);
290 a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
294 // changed from a nick not on notify to one that is
295 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,a->watcher->nick);
296 a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
301 virtual void On005Numeric(std::string &output)
303 // we don't really have a limit...
304 output = output + " WATCH=999";
307 virtual ~Modulewatch()
311 virtual Version GetVersion()
313 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
318 class ModulewatchFactory : public ModuleFactory
325 ~ModulewatchFactory()
329 virtual Module * CreateModule(InspIRCd* Me)
331 return new Modulewatch(Me);
337 extern "C" void * init_module( void )
339 return new ModulewatchFactory;