1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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 */
20 * Okay, it's nice that this was documented and all, but I at least understood very little
21 * of it, so I'm going to attempt to explain the data structures in here a bit more.
23 * For efficiency, many data structures are kept.
25 * The first is a global list `watchentries':
26 * hash_map<irc::string, std::deque<User*> >
28 * That is, if nick 'w00t' is being watched by user pointer 'Brain' and 'Om', <w00t, (Brain, Om)>
29 * will be in the watchentries list.
31 * The second is that each user has a per-user data structure attached to their user record via Extensible:
32 * std::map<irc::string, std::string> watchlist;
33 * So, in the above example with w00t watched by Brain and Om, we'd have:
39 * Hopefully this helps any brave soul that ventures into this file other than me. :-)
40 * -- w00t (mar 30, 2008)
44 /* This module has been refactored to provide a very efficient (in terms of cpu time)
45 * implementation of /WATCH.
47 * To improve the efficiency of watch, many lists are kept. The first primary list is
48 * a hash_map of who's being watched by who. For example:
50 * KEY: Brain ---> Watched by: Boo, w00t, Om
51 * KEY: Boo ---> Watched by: Brain, w00t
53 * This is used when we want to tell all the users that are watching someone that
54 * they are now available or no longer available. For example, if the hash was
55 * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
56 * and Om by reading their 'watched by' list. When this occurs, their online status
57 * in each of these users lists (see below) is also updated.
59 * Each user also has a seperate (smaller) map attached to their User whilst they
60 * have any watch entries, which is managed by class Extensible. When they add or remove
61 * a watch entry from their list, it is inserted here, as well as the main list being
62 * maintained. This map also contains the user's online status. For users that are
63 * offline, the key points at an empty string, and for users that are online, the key
64 * points at a string containing "users-ident users-host users-signon-time". This is
65 * stored in this manner so that we don't have to FindUser() to fetch this info, the
66 * users signon can populate the field for us.
68 * For example, going again on the example above, this would be w00t's watchlist:
70 * KEY: Boo ---> Status: "Boo brains.sexy.babe 535342348"
71 * KEY: Brain ---> Status: ""
73 * In this list we can see that Boo is online, and Brain is offline. We can then
74 * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
75 * of the above two data structures, with minimum CPU penalty for doing so.
77 * In short, the least efficient this ever gets is O(n), and thats only because
78 * there are parts that *must* loop (e.g. telling all users that are watching a
79 * nick that the user online), however this is a *major* improvement over the
80 * 1.0 implementation, which in places had O(n^n) and worse in it, because this
81 * implementation scales based upon the sizes of the watch entries, whereas the
82 * old system would scale (or not as the case may be) according to the total number
83 * of users using WATCH.
87 * Before you start screaming, this definition is only used here, so moving it to a header is pointless.
88 * Yes, it's horrid. Blame cl for being different. -- w00t
91 typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash_compare<irc::string, std::less<irc::string> > > watchentries;
93 typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash<irc::string> > watchentries;
95 typedef std::map<irc::string, std::string> watchlist;
97 /* Who's watching each nickname.
98 * NOTE: We do NOT iterate this to display a user's WATCH list!
99 * See the comments above!
101 watchentries* whos_watching_me;
103 class CommandSVSWatch : public Command
106 CommandSVSWatch (InspIRCd* Instance) : Command(Instance,"SVSWATCH", 0, 2)
108 this->source = "m_watch.so";
109 syntax = "<target> [C|L|S]|[+|-<nick>]";
110 TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
113 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
115 if (!ServerInstance->ULine(user->server))
118 User *u = ServerInstance->FindNick(parameters[0]);
124 ServerInstance->Parser->CallHandler("WATCH", parameters, u);
133 class CommandWatch : public Command
135 unsigned int& MAX_WATCH;
137 CmdResult remove_watch(User* user, const char* nick)
139 // removing an item from the list
140 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
142 user->WriteNumeric(942, "%s %s :Invalid nickname", user->nick.c_str(), nick);
147 if (user->GetExt("watchlist", wl))
149 /* Yup, is on my list */
150 watchlist::iterator n = wl->find(nick);
157 if (!n->second.empty())
158 user->WriteNumeric(602, "%s %s %s :stopped watching", user->nick.c_str(), n->first.c_str(), n->second.c_str());
160 user->WriteNumeric(602, "%s %s * * 0 :stopped watching", user->nick.c_str(), nick);
167 user->Shrink("watchlist");
171 watchentries::iterator x = whos_watching_me->find(nick);
172 if (x != whos_watching_me->end())
174 /* People are watching this user, am i one of them? */
175 std::deque<User*>::iterator n2 = std::find(x->second.begin(), x->second.end(), user);
176 if (n2 != x->second.end())
177 /* I'm no longer watching you... */
180 if (x->second.empty())
181 /* nobody else is, either. */
182 whos_watching_me->erase(nick);
186 return CMD_LOCALONLY;
189 CmdResult add_watch(User* user, const char* nick)
191 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
193 user->WriteNumeric(942, "%s %s :Invalid nickname",user->nick.c_str(),nick);
198 if (!user->GetExt("watchlist", wl))
200 wl = new watchlist();
201 user->Extend("watchlist", wl);
204 if (wl->size() == MAX_WATCH)
206 user->WriteNumeric(512, "%s %s :Too many WATCH entries", user->nick.c_str(), nick);
210 watchlist::iterator n = wl->find(nick);
213 /* Don't already have the user on my watch list, proceed */
214 watchentries::iterator x = whos_watching_me->find(nick);
215 if (x != whos_watching_me->end())
217 /* People are watching this user, add myself */
218 x->second.push_back(user);
222 std::deque<User*> newlist;
223 newlist.push_back(user);
224 (*(whos_watching_me))[nick] = newlist;
227 User* target = ServerInstance->FindNick(nick);
230 if (target->Visibility && !target->Visibility->VisibleTo(user))
233 user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
237 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
238 user->WriteNumeric(604, "%s %s %s :is online",user->nick.c_str(), nick, (*wl)[nick].c_str());
241 user->WriteNumeric(609, "%s %s %s %s %lu :is away", user->nick.c_str(), target->nick.c_str(), target->ident.c_str(), target->dhost.c_str(), (unsigned long) target->awaytime);
247 user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
251 return CMD_LOCALONLY;
254 CommandWatch (InspIRCd* Instance, unsigned int &maxwatch) : Command(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
256 this->source = "m_watch.so";
257 syntax = "[C|L|S]|[+|-<nick>]";
258 TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
261 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
263 if (parameters.empty())
266 if (user->GetExt("watchlist", wl))
268 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
270 if (!q->second.empty())
271 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
274 user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
276 else if (parameters.size() > 0)
278 for (int x = 0; x < (int)parameters.size(); x++)
280 const char *nick = parameters[x].c_str();
281 if (!strcasecmp(nick,"C"))
285 if (user->GetExt("watchlist", wl))
287 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
289 watchentries::iterator i2 = whos_watching_me->find(i->first);
290 if (i2 != whos_watching_me->end())
292 /* People are watching this user, am i one of them? */
293 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
294 if (n != i2->second.end())
295 /* I'm no longer watching you... */
298 if (i2->second.empty())
299 /* nobody else is, either. */
300 whos_watching_me->erase(i2);
305 user->Shrink("watchlist");
308 else if (!strcasecmp(nick,"L"))
311 if (user->GetExt("watchlist", wl))
313 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
315 if (!q->second.empty())
317 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
318 User *targ = ServerInstance->FindNick(q->first.c_str());
321 user->WriteNumeric(609, "%s %s %s %s %lu :is away", user->nick.c_str(), targ->nick.c_str(), targ->ident.c_str(), targ->dhost.c_str(), (unsigned long) targ->awaytime);
325 user->WriteNumeric(605, "%s %s * * 0 :is offline", user->nick.c_str(), q->first.c_str());
328 user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
330 else if (!strcasecmp(nick,"S"))
337 if (user->GetExt("watchlist", wl))
339 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
340 list.append(q->first.c_str()).append(" ");
341 you_have = wl->size();
344 watchentries::iterator i2 = whos_watching_me->find(user->nick.c_str());
345 if (i2 != whos_watching_me->end())
346 youre_on = i2->second.size();
348 user->WriteNumeric(603, "%s :You have %d and are on %d WATCH entries", user->nick.c_str(), you_have, youre_on);
349 user->WriteNumeric(606, "%s :%s",user->nick.c_str(), list.c_str());
350 user->WriteNumeric(607, "%s :End of WATCH S",user->nick.c_str());
352 else if (nick[0] == '-')
355 remove_watch(user, nick);
357 else if (nick[0] == '+')
360 add_watch(user, nick);
364 return CMD_LOCALONLY;
368 class Modulewatch : public Module
370 CommandWatch* mycommand;
372 unsigned int maxwatch;
375 Modulewatch(InspIRCd* Me)
376 : Module(Me), maxwatch(32)
379 whos_watching_me = new watchentries();
380 mycommand = new CommandWatch(ServerInstance, maxwatch);
381 ServerInstance->AddCommand(mycommand);
382 sw = new CommandSVSWatch(ServerInstance);
383 ServerInstance->AddCommand(sw);
384 Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
385 ServerInstance->Modules->Attach(eventlist, this, 8);
388 virtual void OnRehash(User* user)
390 ConfigReader Conf(ServerInstance);
391 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
396 virtual int OnSetAway(User *user, const std::string &awaymsg)
403 numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
408 numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
412 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
413 if (x != whos_watching_me->end())
415 for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
417 if (!user->Visibility || user->Visibility->VisibleTo(user))
418 (*n)->WriteNumeric(inum, numeric);
425 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
427 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
428 if (x != whos_watching_me->end())
430 for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
432 if (!user->Visibility || user->Visibility->VisibleTo(*n))
433 (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str() ,user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) ServerInstance->Time());
436 if ((*n)->GetExt("watchlist", wl))
437 /* We were on somebody's notify list, set ourselves offline */
438 (*wl)[user->nick.c_str()] = "";
442 /* Now im quitting, if i have a notify list, im no longer watching anyone */
444 if (user->GetExt("watchlist", wl))
446 /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
447 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
449 watchentries::iterator i2 = whos_watching_me->find(i->first);
450 if (i2 != whos_watching_me->end())
452 /* People are watching this user, am i one of them? */
453 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
454 if (n != i2->second.end())
455 /* I'm no longer watching you... */
458 if (i2->second.empty())
459 /* and nobody else is, either. */
460 whos_watching_me->erase(i2);
464 /* User's quitting, we're done with this. */
466 user->Shrink("watchlist");
470 virtual void OnGarbageCollect()
472 watchentries* old_watch = whos_watching_me;
473 whos_watching_me = new watchentries();
475 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
476 whos_watching_me->insert(*n);
481 virtual void OnCleanup(int target_type, void* item)
483 if (target_type == TYPE_USER)
486 User* user = (User*)item;
488 if (user->GetExt("watchlist", wl))
490 user->Shrink("watchlist");
496 virtual void OnPostConnect(User* user)
498 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
499 if (x != whos_watching_me->end())
501 for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
503 if (!user->Visibility || user->Visibility->VisibleTo(*n))
504 (*n)->WriteNumeric(600, "%s %s %s %s %lu :arrived online", (*n)->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
507 if ((*n)->GetExt("watchlist", wl))
508 /* We were on somebody's notify list, set ourselves online */
509 (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
514 virtual void OnUserPostNick(User* user, const std::string &oldnick)
516 watchentries::iterator new_offline = whos_watching_me->find(oldnick.c_str());
517 watchentries::iterator new_online = whos_watching_me->find(user->nick.c_str());
519 if (new_offline != whos_watching_me->end())
521 for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
524 if ((*n)->GetExt("watchlist", wl))
526 if (!user->Visibility || user->Visibility->VisibleTo(*n))
527 (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str(), oldnick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
528 (*wl)[oldnick.c_str()] = "";
533 if (new_online != whos_watching_me->end())
535 for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
538 if ((*n)->GetExt("watchlist", wl))
540 (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
541 if (!user->Visibility || user->Visibility->VisibleTo(*n))
542 (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
548 virtual void On005Numeric(std::string &output)
550 // we don't really have a limit...
551 output = output + " WATCH=" + ConvToStr(maxwatch);
554 virtual ~Modulewatch()
556 delete whos_watching_me;
559 virtual Version GetVersion()
561 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
565 MODULE_INIT(Modulewatch)