1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
20 /* $ModDesc: Provides support for the /WATCH command */
22 /* This module has been refactored to provide a very efficient (in terms of cpu time)
23 * implementation of /WATCH.
25 * To improve the efficiency of watch, many lists are kept. The first primary list is
26 * a hash_map of who's being watched by who. For example:
28 * KEY: Brain ---> Watched by: Boo, w00t, Om
29 * KEY: Boo ---> Watched by: Brain, w00t
31 * This is used when we want to tell all the users that are watching someone that
32 * they are now available or no longer available. For example, if the hash was
33 * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
34 * and Om by reading their 'watched by' list. When this occurs, their online status
35 * in each of these users lists (see below) is also updated.
37 * Each user also has a seperate (smaller) map attached to their userrec whilst they
38 * have any watch entries, which is managed by class Extensible. When they add or remove
39 * a watch entry from their list, it is inserted here, as well as the main list being
40 * maintained. This map also contains the user's online status. For users that are
41 * offline, the key points at an empty string, and for users that are online, the key
42 * points at a string containing "users-ident users-host users-signon-time". This is
43 * stored in this manner so that we don't have to FindUser() to fetch this info, the
44 * users signon can populate the field for us.
46 * For example, going again on the example above, this would be w00t's watchlist:
48 * KEY: Boo ---> Status: "Boo brains.sexy.babe 535342348"
49 * KEY: Brain ---> Status: ""
51 * In this list we can see that Boo is online, and Brain is offline. We can then
52 * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
53 * of the above two data structures, with minimum CPU penalty for doing so.
55 * In short, the least efficient this ever gets is O(n), and thats only because
56 * there are parts that *must* loop (e.g. telling all users that are watching a
57 * nick that the user online), however this is a *major* improvement over the
58 * 1.0 implementation, which in places had O(n^n) and worse in it, because this
59 * implementation scales based upon the sizes of the watch entries, whereas the
60 * old system would scale (or not as the case may be) according to the total number
61 * of users using WATCH.
64 typedef nspace::hash_map<irc::string, std::deque<userrec*>, nspace::hash<irc::string> > watchentries;
65 typedef std::map<irc::string, std::string> watchlist;
67 /* Who's watching each nickname.
68 * NOTE: We do NOT iterate this to display a user's WATCH list!
69 * See the comments above!
71 watchentries whos_watching_me;
75 class cmd_watch : public command_t
77 unsigned int& MAX_WATCH;
79 CmdResult remove_watch(userrec* user, const char* nick)
81 // removing an item from the list
82 if (!ServerInstance->IsNick(nick))
84 user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
89 if (user->GetExt("watchlist", wl))
91 /* Yup, is on my list */
92 watchlist::iterator n = wl->find(nick);
95 if (!n->second.empty())
96 user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
98 user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
105 user->Shrink("watchlist");
109 watchentries::iterator x = whos_watching_me.find(nick);
110 if (x != whos_watching_me.end())
112 /* People are watching this user, am i one of them? */
113 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
114 if (n != x->second.end())
115 /* I'm no longer watching you... */
118 if (!x->second.size())
119 whos_watching_me.erase(nick);
123 /* This might seem confusing, but we return CMD_FAILURE
124 * to indicate that this message shouldnt be routed across
125 * the network to other linked servers.
130 CmdResult add_watch(userrec* user, const char* nick)
132 if (!ServerInstance->IsNick(nick))
134 user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
139 if (!user->GetExt("watchlist", wl))
141 ServerInstance->Log(DEBUG,"Allocate new watchlist");
142 wl = new watchlist();
143 user->Extend("watchlist", wl);
146 if (wl->size() == MAX_WATCH)
148 user->WriteServ("512 %s %s :Too many WATCH entries", user->nick, nick);
152 watchlist::iterator n = wl->find(nick);
155 ServerInstance->Log(DEBUG,"*** Add to WATCH: '%s'", nick);
156 /* Don't already have the user on my watch list, proceed */
157 watchentries::iterator x = whos_watching_me.find(nick);
158 if (x != whos_watching_me.end())
160 /* People are watching this user, add myself */
161 x->second.push_back(user);
165 std::deque<userrec*> newlist;
166 newlist.push_back(user);
167 whos_watching_me[nick] = newlist;
170 userrec* target = ServerInstance->FindNick(nick);
173 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
174 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
179 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
184 ServerInstance->Log(DEBUG,"*** WATCH entry '%s' already exists!", nick);
190 cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
192 this->source = "m_watch.so";
193 syntax = "[C|L|S]|[+|-<nick>]";
196 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
201 if (user->GetExt("watchlist", wl))
203 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
205 if (!q->second.empty())
206 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
209 user->WriteServ("607 %s :End of WATCH list",user->nick);
213 for (int x = 0; x < pcnt; x++)
215 const char *nick = parameters[x];
216 ServerInstance->Log(DEBUG,"WATCH iterate item '%s'", nick);
217 if (!strcasecmp(nick,"C"))
221 if (user->GetExt("watchlist", wl))
223 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
225 watchentries::iterator x = whos_watching_me.find(i->first);
226 if (x != whos_watching_me.end())
228 /* People are watching this user, am i one of them? */
229 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
230 if (n != x->second.end())
231 /* I'm no longer watching you... */
234 if (!x->second.size())
235 whos_watching_me.erase(user->nick);
240 user->Shrink("watchlist");
243 else if (!strcasecmp(nick,"L"))
246 if (user->GetExt("watchlist", wl))
248 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
250 if (!q->second.empty())
251 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
253 user->WriteServ("605 %s %s * * 0 :is offline", user->nick, q->first.c_str());
256 user->WriteServ("607 %s :End of WATCH list",user->nick);
258 else if (!strcasecmp(nick,"S"))
265 if (user->GetExt("watchlist", wl))
267 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
268 list.append(q->first.c_str()).append(" ");
269 you_have = wl->size();
272 watchentries::iterator x = whos_watching_me.find(user->nick);
273 if (x != whos_watching_me.end())
274 youre_on = x->second.size();
276 user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
277 user->WriteServ("606 %s :%s",user->nick, list.c_str());
278 user->WriteServ("607 %s :End of WATCH S",user->nick);
280 else if (nick[0] == '-')
283 remove_watch(user, nick);
285 else if (nick[0] == '+')
288 add_watch(user, nick);
292 /* So that spanningtree doesnt pass the WATCH commands to the network! */
297 class Modulewatch : public Module
299 cmd_watch* mycommand;
300 unsigned int maxwatch;
303 Modulewatch(InspIRCd* Me)
304 : Module::Module(Me), maxwatch(32)
306 mycommand = new cmd_watch(ServerInstance, maxwatch);
307 ServerInstance->AddCommand(mycommand);
310 void Implements(char* List)
312 List[I_OnCleanup] = List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
315 virtual void OnUserQuit(userrec* user, const std::string &reason)
317 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
318 watchentries::iterator x = whos_watching_me.find(user->nick);
319 if (x != whos_watching_me.end())
321 for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
323 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
325 if ((*n)->GetExt("watchlist", wl))
326 /* We were on somebody's notify list, set ourselves offline */
327 (*wl)[user->nick] = "";
331 /* Now im quitting, if i have a notify list, im no longer watching anyone */
333 if (user->GetExt("watchlist", wl))
335 /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
336 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
338 watchentries::iterator x = whos_watching_me.find(i->first);
339 if (x != whos_watching_me.end())
341 /* People are watching this user, am i one of them? */
342 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
343 if (n != x->second.end())
344 /* I'm no longer watching you... */
347 if (!x->second.size())
348 whos_watching_me.erase(user->nick);
352 /* User's quitting, we're done with this. */
357 virtual void OnCleanup(int target_type, void* item)
359 if (target_type == TYPE_USER)
362 userrec* user = (userrec*)item;
364 if (user->GetExt("watchlist", wl))
366 user->Shrink("watchlist");
372 virtual void OnPostConnect(userrec* user)
374 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
375 watchentries::iterator x = whos_watching_me.find(user->nick);
376 if (x != whos_watching_me.end())
378 for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
380 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
382 if ((*n)->GetExt("watchlist", wl))
383 /* We were on somebody's notify list, set ourselves online */
384 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
389 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
391 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
393 watchentries::iterator new_online = whos_watching_me.find(user->nick);
394 watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick));
396 if (new_online != whos_watching_me.end())
398 for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
401 if ((*n)->GetExt("watchlist", wl))
403 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
404 (*n)->WriteServ("600 %s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str());
409 if (new_offline != whos_watching_me.end())
411 for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
414 if ((*n)->GetExt("watchlist", wl))
416 /*ServerInstance->Log(DEBUG,"nick=%s", (*n)->nick);
417 ServerInstance->Log(DEBUG,"nick2=%s", user->nick);*/
418 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, user->age);
419 (*wl)[user->nick] = "";
425 virtual void On005Numeric(std::string &output)
427 // we don't really have a limit...
428 output = output + " WATCH=32";
431 virtual ~Modulewatch()
435 virtual Version GetVersion()
437 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
442 class ModulewatchFactory : public ModuleFactory
449 ~ModulewatchFactory()
453 virtual Module * CreateModule(InspIRCd* Me)
455 return new Modulewatch(Me);
461 extern "C" void * init_module( void )
463 return new ModulewatchFactory;