1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
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 */
30 /* nickname list of users watching the nick */
31 typedef std::map<irc::string, std::deque<userrec*> > watchentries;
33 /* nickname 'ident host signon', or empty if not online */
34 typedef std::map<irc::string, std::string> watchlist;
36 /* Whos watching each nickname */
37 watchentries whos_watching_me;
41 class cmd_watch : public command_t
43 unsigned int& MAX_WATCH;
45 CmdResult remove_watch(userrec* user, const char* nick)
47 // removing an item from the list
48 if (!ServerInstance->IsNick(nick))
50 user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
55 if (user->GetExt("watchlist", wl))
57 /* Yup, is on my list */
58 watchlist::iterator n = wl->find(nick);
61 if (!n->second.empty())
62 user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
64 user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
71 user->Shrink("watchlist");
75 watchentries::iterator x = whos_watching_me.find(nick);
76 if (x != whos_watching_me.end())
78 /* People are watching this user, am i one of them? */
79 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
80 if (n != x->second.end())
81 /* I'm no longer watching you... */
84 if (!x->second.size())
85 whos_watching_me.erase(nick);
89 /* This might seem confusing, but we return CMD_FAILURE
90 * to indicate that this message shouldnt be routed across
91 * the network to other linked servers.
96 CmdResult add_watch(userrec* user, const char* nick)
98 if (!ServerInstance->IsNick(nick))
100 user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
105 if (!user->GetExt("watchlist", wl))
107 ServerInstance->Log(DEBUG,"Allocate new watchlist");
108 wl = new watchlist();
109 user->Extend("watchlist", wl);
112 if (wl->size() == MAX_WATCH)
114 user->WriteServ("512 %s %s :Too many WATCH entries", user->nick, nick);
118 watchlist::iterator n = wl->find(nick);
121 ServerInstance->Log(DEBUG,"*** Add to WATCH: '%s'", nick);
122 /* Don't already have the user on my watch list, proceed */
123 watchentries::iterator x = whos_watching_me.find(nick);
124 if (x != whos_watching_me.end())
126 /* People are watching this user, add myself */
127 x->second.push_back(user);
131 std::deque<userrec*> newlist;
132 newlist.push_back(user);
133 whos_watching_me[nick] = newlist;
136 userrec* target = ServerInstance->FindNick(nick);
139 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
140 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
145 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
150 ServerInstance->Log(DEBUG,"*** WATCH entry '%s' already exists!", nick);
156 cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
158 this->source = "m_watch.so";
159 syntax = "[C|L|S]|[+|-<nick>]";
162 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
167 if (user->GetExt("watchlist", wl))
169 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
171 if (!q->second.empty())
172 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
175 user->WriteServ("607 %s :End of WATCH list",user->nick);
179 for (int x = 0; x < pcnt; x++)
181 const char *nick = parameters[x];
182 ServerInstance->Log(DEBUG,"WATCH iterate item '%s'", nick);
183 if (!strcasecmp(nick,"C"))
187 if (user->GetExt("watchlist", wl))
189 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
191 watchentries::iterator x = whos_watching_me.find(i->first);
192 if (x != whos_watching_me.end())
194 /* People are watching this user, am i one of them? */
195 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
196 if (n != x->second.end())
197 /* I'm no longer watching you... */
200 if (!x->second.size())
201 whos_watching_me.erase(user->nick);
206 user->Shrink("watchlist");
209 else if (!strcasecmp(nick,"L"))
212 if (user->GetExt("watchlist", wl))
214 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
216 if (!q->second.empty())
217 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
220 user->WriteServ("607 %s :End of WATCH list",user->nick);
222 else if (!strcasecmp(nick,"S"))
229 if (user->GetExt("watchlist", wl))
231 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
232 if (!q->second.empty())
233 list.append(q->first.c_str()).append(" ");
234 you_have = wl->size();
237 watchentries::iterator x = whos_watching_me.find(user->nick);
238 if (x != whos_watching_me.end())
239 youre_on = x->second.size();
241 user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
242 user->WriteServ("606 %s :%s",user->nick, list.c_str());
243 user->WriteServ("607 %s :End of WATCH S",user->nick);
245 else if (nick[0] == '-')
248 remove_watch(user, nick);
250 else if (nick[0] == '+')
253 add_watch(user, nick);
257 /* So that spanningtree doesnt pass the WATCH commands to the network! */
262 class Modulewatch : public Module
264 cmd_watch* mycommand;
265 unsigned int maxwatch;
268 Modulewatch(InspIRCd* Me)
269 : Module::Module(Me), maxwatch(32)
271 mycommand = new cmd_watch(ServerInstance, maxwatch);
272 ServerInstance->AddCommand(mycommand);
275 void Implements(char* List)
277 List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
280 virtual void OnUserQuit(userrec* user, const std::string &reason)
282 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
283 watchentries::iterator x = whos_watching_me.find(user->nick);
284 if (x != whos_watching_me.end())
286 for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
288 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
290 if ((*n)->GetExt("watchlist", wl))
291 /* We were on somebody's notify list, set ourselves offline */
292 (*wl)[user->nick] = "";
296 /* Now im quitting, if i have a notify list, im no longer watching anyone */
298 if (user->GetExt("watchlist", wl))
300 /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
301 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
303 watchentries::iterator x = whos_watching_me.find(i->first);
304 if (x != whos_watching_me.end())
306 /* People are watching this user, am i one of them? */
307 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
308 if (n != x->second.end())
309 /* I'm no longer watching you... */
312 if (!x->second.size())
313 whos_watching_me.erase(user->nick);
319 virtual void OnPostConnect(userrec* user)
321 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
322 watchentries::iterator x = whos_watching_me.find(user->nick);
323 if (x != whos_watching_me.end())
325 for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
327 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
329 if ((*n)->GetExt("watchlist", wl))
330 /* We were on somebody's notify list, set ourselves online */
331 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
336 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
338 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
340 watchentries::iterator new_online = whos_watching_me.find(user->nick);
341 watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick));
343 if (new_online != whos_watching_me.end())
345 for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
348 if ((*n)->GetExt("watchlist", wl))
350 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
351 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, (*wl)[user->nick].c_str());
356 if (new_offline != whos_watching_me.end())
358 for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
361 if ((*n)->GetExt("watchlist", wl))
363 (*n)->WriteServ("601 %s %s %s :went offline", (*n)->nick, (*wl)[user->nick].c_str());
364 (*wl)[user->nick] = "";
370 virtual void On005Numeric(std::string &output)
372 // we don't really have a limit...
373 output = output + " WATCH=32";
376 virtual ~Modulewatch()
380 virtual Version GetVersion()
382 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
387 class ModulewatchFactory : public ModuleFactory
394 ~ModulewatchFactory()
398 virtual Module * CreateModule(InspIRCd* Me)
400 return new Modulewatch(Me);
406 extern "C" void * init_module( void )
408 return new ModulewatchFactory;