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";
50 void Handle (char **parameters, int pcnt, userrec *user)
54 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
56 if (q->watcher == user)
58 userrec* targ = Srv->FindNick(q->target);
61 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
65 WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
69 for (int x = 0; x < pcnt; x++)
71 char *nick = parameters[x];
72 if (!strcasecmp(nick,"C"))
79 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
81 if (q->watcher == user)
90 else if (!strcasecmp(nick,"L"))
92 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
94 if (q->watcher == user)
96 userrec* targ = Srv->FindNick(q->target);
99 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
103 WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
105 else if (!strcasecmp(nick,"S"))
107 std::string list = "";
108 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
110 if (q->watcher == user)
112 list = list + " " + q->target;
115 char* l = (char*)list.c_str();
118 WriteServ(user->fd,"606 %s :%s",user->nick,l);
119 WriteServ(user->fd,"607 %s :End of WATCH S",user->nick);
121 else if (nick[0] == '-')
123 // removing an item from the list
125 irc::string n1 = nick;
126 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
128 if (q->watcher == user)
130 irc::string n2 = q->target.c_str();
131 userrec* a = Srv->FindNick(q->target);
134 WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
138 WriteServ(user->fd,"602 %s %s * * 0 :stopped watching",user->nick,q->target.c_str());
148 else if (nick[0] == '+')
151 irc::string n1 = nick;
153 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
155 if (q->watcher == user)
157 irc::string n2 = q->target.c_str();
160 // already on watch list
170 watches.push_back(w);
171 log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
173 userrec* a = Srv->FindNick(nick);
176 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
180 WriteServ(user->fd,"605 %s %s * * 0 :is offline",user->nick,nick);
189 class Modulewatch : public Module
191 cmd_watch* mycommand;
194 Modulewatch(Server* Me)
198 mycommand = new cmd_watch();
199 Srv->AddCommand(mycommand);
202 void Implements(char* List)
204 List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
207 virtual void OnUserQuit(userrec* user, const std::string &reason)
209 log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
210 irc::string n2 = user->nick;
211 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
213 irc::string n1 = q->target.c_str();
216 log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,q->watcher->nick);
217 WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
224 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
226 if (q->watcher == user)
236 virtual void OnGlobalConnect(userrec* user)
238 irc::string n2 = user->nick;
239 log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
240 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
242 irc::string n1 = q->target.c_str();
245 log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,q->watcher->nick);
246 WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age);
251 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
253 irc::string n2 = oldnick.c_str();
254 irc::string n3 = user->nick;
255 log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
256 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
258 irc::string n1 = q->target.c_str();
259 // changed from a nick on the watchlist to one that isnt
262 log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),q->watcher->nick);
263 WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
267 // changed from a nick not on notify to one that is
268 log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,q->watcher->nick);
269 WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age);
274 virtual void On005Numeric(std::string &output)
276 // we don't really have a limit...
277 output = output + " WATCH=999";
280 virtual ~Modulewatch()
284 virtual Version GetVersion()
286 return Version(1,0,0,1,VF_VENDOR);
291 class ModulewatchFactory : public ModuleFactory
298 ~ModulewatchFactory()
302 virtual Module * CreateModule(Server* Me)
304 return new Modulewatch(Me);
310 extern "C" void * init_module( void )
312 return new ModulewatchFactory;