1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
16 /* $ModDesc: Provides support for the /WATCH command */
18 /* This module has been refactored to provide a very efficient (in terms of cpu time)
19 * implementation of /WATCH.
21 * To improve the efficiency of watch, many lists are kept. The first primary list is
22 * a hash_map of who's being watched by who. For example:
24 * KEY: Brain ---> Watched by: Boo, w00t, Om
25 * KEY: Boo ---> Watched by: Brain, w00t
27 * This is used when we want to tell all the users that are watching someone that
28 * they are now available or no longer available. For example, if the hash was
29 * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
30 * and Om by reading their 'watched by' list. When this occurs, their online status
31 * in each of these users lists (see below) is also updated.
33 * Each user also has a seperate (smaller) map attached to their User whilst they
34 * have any watch entries, which is managed by class Extensible. When they add or remove
35 * a watch entry from their list, it is inserted here, as well as the main list being
36 * maintained. This map also contains the user's online status. For users that are
37 * offline, the key points at an empty string, and for users that are online, the key
38 * points at a string containing "users-ident users-host users-signon-time". This is
39 * stored in this manner so that we don't have to FindUser() to fetch this info, the
40 * users signon can populate the field for us.
42 * For example, going again on the example above, this would be w00t's watchlist:
44 * KEY: Boo ---> Status: "Boo brains.sexy.babe 535342348"
45 * KEY: Brain ---> Status: ""
47 * In this list we can see that Boo is online, and Brain is offline. We can then
48 * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
49 * of the above two data structures, with minimum CPU penalty for doing so.
51 * In short, the least efficient this ever gets is O(n), and thats only because
52 * there are parts that *must* loop (e.g. telling all users that are watching a
53 * nick that the user online), however this is a *major* improvement over the
54 * 1.0 implementation, which in places had O(n^n) and worse in it, because this
55 * implementation scales based upon the sizes of the watch entries, whereas the
56 * old system would scale (or not as the case may be) according to the total number
57 * of users using WATCH.
61 * Before you start screaming, this definition is only used here, so moving it to a header is pointless.
62 * Yes, it's horrid. Blame cl for being different. -- w00t
65 typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash_compare<irc::string, less<irc::string> > > watchentries;
67 typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash<irc::string> > watchentries;
69 typedef std::map<irc::string, std::string> watchlist;
71 /* Who's watching each nickname.
72 * NOTE: We do NOT iterate this to display a user's WATCH list!
73 * See the comments above!
75 watchentries* whos_watching_me;
79 class CommandWatch : public Command
81 unsigned int& MAX_WATCH;
83 CmdResult remove_watch(User* user, const char* nick)
85 // removing an item from the list
86 if (!ServerInstance->IsNick(nick))
88 user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
93 if (user->GetExt("watchlist", wl))
95 /* Yup, is on my list */
96 watchlist::iterator n = wl->find(nick);
103 if (!n->second.empty())
104 user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
106 user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
113 user->Shrink("watchlist");
117 watchentries::iterator x = whos_watching_me->find(nick);
118 if (x != whos_watching_me->end())
120 /* People are watching this user, am i one of them? */
121 std::deque<User*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
122 if (n != x->second.end())
123 /* I'm no longer watching you... */
126 if (!x->second.size())
127 whos_watching_me->erase(nick);
131 /* This might seem confusing, but we return CMD_FAILURE
132 * to indicate that this message shouldnt be routed across
133 * the network to other linked servers.
138 CmdResult add_watch(User* user, const char* nick)
140 if (!ServerInstance->IsNick(nick))
142 user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
147 if (!user->GetExt("watchlist", wl))
149 wl = new watchlist();
150 user->Extend("watchlist", wl);
153 if (wl->size() == MAX_WATCH)
155 user->WriteServ("512 %s %s :Too many WATCH entries", user->nick, nick);
159 watchlist::iterator n = wl->find(nick);
162 /* Don't already have the user on my watch list, proceed */
163 watchentries::iterator x = whos_watching_me->find(nick);
164 if (x != whos_watching_me->end())
166 /* People are watching this user, add myself */
167 x->second.push_back(user);
171 std::deque<User*> newlist;
172 newlist.push_back(user);
173 (*(whos_watching_me))[nick] = newlist;
176 User* target = ServerInstance->FindNick(nick);
179 if (target->Visibility && !target->Visibility->VisibleTo(user))
182 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
186 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
187 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
192 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
199 CommandWatch (InspIRCd* Instance, unsigned int &maxwatch) : Command(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
201 this->source = "m_watch.so";
202 syntax = "[C|L|S]|[+|-<nick>]";
203 TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
206 CmdResult Handle (const char** parameters, int pcnt, User *user)
211 if (user->GetExt("watchlist", wl))
213 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
215 if (!q->second.empty())
216 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
219 user->WriteServ("607 %s :End of WATCH list",user->nick);
223 for (int x = 0; x < pcnt; x++)
225 const char *nick = parameters[x];
226 if (!strcasecmp(nick,"C"))
230 if (user->GetExt("watchlist", wl))
232 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
234 watchentries::iterator x = whos_watching_me->find(i->first);
235 if (x != whos_watching_me->end())
237 /* People are watching this user, am i one of them? */
238 std::deque<User*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
239 if (n != x->second.end())
240 /* I'm no longer watching you... */
243 if (!x->second.size())
244 whos_watching_me->erase(user->nick);
249 user->Shrink("watchlist");
252 else if (!strcasecmp(nick,"L"))
255 if (user->GetExt("watchlist", wl))
257 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
259 if (!q->second.empty())
260 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
262 user->WriteServ("605 %s %s * * 0 :is offline", user->nick, q->first.c_str());
265 user->WriteServ("607 %s :End of WATCH list",user->nick);
267 else if (!strcasecmp(nick,"S"))
274 if (user->GetExt("watchlist", wl))
276 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
277 list.append(q->first.c_str()).append(" ");
278 you_have = wl->size();
281 watchentries::iterator x = whos_watching_me->find(user->nick);
282 if (x != whos_watching_me->end())
283 youre_on = x->second.size();
285 user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
286 user->WriteServ("606 %s :%s",user->nick, list.c_str());
287 user->WriteServ("607 %s :End of WATCH S",user->nick);
289 else if (nick[0] == '-')
292 remove_watch(user, nick);
294 else if (nick[0] == '+')
297 add_watch(user, nick);
301 /* So that spanningtree doesnt pass the WATCH commands to the network! */
306 class Modulewatch : public Module
308 CommandWatch* mycommand;
309 unsigned int maxwatch;
312 Modulewatch(InspIRCd* Me)
313 : Module(Me), maxwatch(32)
316 whos_watching_me = new watchentries();
317 mycommand = new CommandWatch(ServerInstance, maxwatch);
318 ServerInstance->AddCommand(mycommand);
319 Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric };
320 ServerInstance->Modules->Attach(eventlist, this, 7);
323 virtual void OnRehash(User* user, const std::string ¶meter)
325 ConfigReader Conf(ServerInstance);
326 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
332 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
334 watchentries::iterator x = whos_watching_me->find(user->nick);
335 if (x != whos_watching_me->end())
337 for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
339 if (!user->Visibility || user->Visibility->VisibleTo(user))
340 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
343 if ((*n)->GetExt("watchlist", wl))
344 /* We were on somebody's notify list, set ourselves offline */
345 (*wl)[user->nick] = "";
349 /* Now im quitting, if i have a notify list, im no longer watching anyone */
351 if (user->GetExt("watchlist", wl))
353 /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
354 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
356 watchentries::iterator x = whos_watching_me->find(i->first);
357 if (x != whos_watching_me->end())
359 /* People are watching this user, am i one of them? */
360 std::deque<User*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
361 if (n != x->second.end())
362 /* I'm no longer watching you... */
365 if (!x->second.size())
366 whos_watching_me->erase(user->nick);
370 /* User's quitting, we're done with this. */
372 user->Shrink("watchlist");
376 virtual void OnGarbageCollect()
378 watchentries* old_watch = whos_watching_me;
379 whos_watching_me = new watchentries();
381 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
382 whos_watching_me->insert(*n);
387 virtual void OnCleanup(int target_type, void* item)
389 if (target_type == TYPE_USER)
392 User* user = (User*)item;
394 if (user->GetExt("watchlist", wl))
396 user->Shrink("watchlist");
402 virtual void OnPostConnect(User* user)
404 watchentries::iterator x = whos_watching_me->find(user->nick);
405 if (x != whos_watching_me->end())
407 for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
409 if (!user->Visibility || user->Visibility->VisibleTo(user))
410 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
413 if ((*n)->GetExt("watchlist", wl))
414 /* We were on somebody's notify list, set ourselves online */
415 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
420 virtual void OnUserPostNick(User* user, const std::string &oldnick)
422 watchentries::iterator new_offline = whos_watching_me->find(assign(oldnick));
423 watchentries::iterator new_online = whos_watching_me->find(user->nick);
425 if (new_offline != whos_watching_me->end())
427 for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
430 if ((*n)->GetExt("watchlist", wl))
432 if (!user->Visibility || user->Visibility->VisibleTo(user))
433 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, user->age);
434 (*wl)[oldnick.c_str()] = "";
439 if (new_online != whos_watching_me->end())
441 for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
444 if ((*n)->GetExt("watchlist", wl))
446 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
447 if (!user->Visibility || user->Visibility->VisibleTo(user))
448 (*n)->WriteServ("600 %s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str());
454 virtual void On005Numeric(std::string &output)
456 // we don't really have a limit...
457 output = output + " WATCH=" + ConvToStr(maxwatch);
460 virtual ~Modulewatch()
462 delete whos_watching_me;
465 virtual Version GetVersion()
467 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
471 MODULE_INIT(Modulewatch)