]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
42a4b8705ef7a7a93cdcf6bfb92c3df51999306f
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides support for the /WATCH command */
17
18
19 /*
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.
22  *
23  * For efficiency, many data structures are kept.
24  *
25  * The first is a global list `watchentries':
26  *      hash_map<irc::string, std::deque<User*> >
27  *
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.
30  *
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:
34  *      Brain-
35  *            `- w00t
36  *      Om-
37  *         `- w00t
38  *
39  * Hopefully this helps any brave soul that ventures into this file other than me. :-)
40  *              -- w00t (mar 30, 2008)
41  */
42
43
44 /* This module has been refactored to provide a very efficient (in terms of cpu time)
45  * implementation of /WATCH.
46  *
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:
49  *
50  * KEY: Brain   --->  Watched by:  Boo, w00t, Om
51  * KEY: Boo     --->  Watched by:  Brain, w00t
52  *
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.
58  *
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.
67  *
68  * For example, going again on the example above, this would be w00t's watchlist:
69  *
70  * KEY: Boo    --->  Status: "Boo brains.sexy.babe 535342348"
71  * KEY: Brain  --->  Status: ""
72  *
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.
76  *
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.
84  */
85
86 /*
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
89  */
90 #ifdef WINDOWS
91 typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash_compare<irc::string, std::less<irc::string> > > watchentries;
92 #else
93 typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash<irc::string> > watchentries;
94 #endif
95 typedef std::map<irc::string, std::string> watchlist;
96
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!
100  */
101 watchentries* whos_watching_me;
102
103 class CommandSVSWatch : public Command
104 {
105  public:
106         CommandSVSWatch (InspIRCd* Instance) : Command(Instance,"SVSWATCH", 0, 2)
107         {
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. */
111         }
112
113         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
114         {
115                 if (!ServerInstance->ULine(user->server))
116                         return CMD_FAILURE;
117
118                 User *u = ServerInstance->FindNick(parameters[0]);
119                 if (!u)
120                         return CMD_FAILURE;
121
122                 if (IS_LOCAL(u))
123                 {
124                         ServerInstance->Parser->CallHandler("WATCH", parameters, u);
125                 }
126
127                 return CMD_SUCCESS;
128         }
129 };
130
131 /** Handle /WATCH
132  */
133 class CommandWatch : public Command
134 {
135         unsigned int& MAX_WATCH;
136  public:
137         CmdResult remove_watch(User* user, const char* nick)
138         {
139                 // removing an item from the list
140                 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
141                 {
142                         user->WriteNumeric(942, "%s %s :Invalid nickname", user->nick.c_str(), nick);
143                         return CMD_FAILURE;
144                 }
145
146                 watchlist* wl;
147                 if (user->GetExt("watchlist", wl))
148                 {
149                         /* Yup, is on my list */
150                         watchlist::iterator n = wl->find(nick);
151
152                         if (!wl)
153                                 return CMD_FAILURE;
154
155                         if (n != wl->end())
156                         {
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());
159                                 else
160                                         user->WriteNumeric(602, "%s %s * * 0 :stopped watching", user->nick.c_str(), nick);
161
162                                 wl->erase(n);
163                         }
164
165                         if (wl->empty())
166                         {
167                                 user->Shrink("watchlist");
168                                 delete wl;
169                         }
170
171                         watchentries::iterator x = whos_watching_me->find(nick);
172                         if (x != whos_watching_me->end())
173                         {
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... */
178                                         x->second.erase(n2);
179
180                                 if (x->second.empty())
181                                         /* nobody else is, either. */
182                                         whos_watching_me->erase(nick);
183                         }
184                 }
185
186                 /* This might seem confusing, but we return CMD_FAILURE
187                  * to indicate that this message shouldnt be routed across
188                  * the network to other linked servers.
189                  */
190                 return CMD_FAILURE;
191         }
192
193         CmdResult add_watch(User* user, const char* nick)
194         {
195                 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
196                 {
197                         user->WriteNumeric(942, "%s %s :Invalid nickname",user->nick.c_str(),nick);
198                         return CMD_FAILURE;
199                 }
200
201                 watchlist* wl;
202                 if (!user->GetExt("watchlist", wl))
203                 {
204                         wl = new watchlist();
205                         user->Extend("watchlist", wl);
206                 }
207
208                 if (wl->size() == MAX_WATCH)
209                 {
210                         user->WriteNumeric(512, "%s %s :Too many WATCH entries", user->nick.c_str(), nick);
211                         return CMD_FAILURE;
212                 }
213
214                 watchlist::iterator n = wl->find(nick);
215                 if (n == wl->end())
216                 {
217                         /* Don't already have the user on my watch list, proceed */
218                         watchentries::iterator x = whos_watching_me->find(nick);
219                         if (x != whos_watching_me->end())
220                         {
221                                 /* People are watching this user, add myself */
222                                 x->second.push_back(user);
223                         }
224                         else
225                         {
226                                 std::deque<User*> newlist;
227                                 newlist.push_back(user);
228                                 (*(whos_watching_me))[nick] = newlist;
229                         }
230
231                         User* target = ServerInstance->FindNick(nick);
232                         if (target)
233                         {
234                                 if (target->Visibility && !target->Visibility->VisibleTo(user))
235                                 {
236                                         (*wl)[nick] = "";
237                                         user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
238                                         return CMD_FAILURE;
239                                 }
240
241                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
242                                 user->WriteNumeric(604, "%s %s %s :is online",user->nick.c_str(), nick, (*wl)[nick].c_str());
243                                 if (IS_AWAY(target))
244                                 {
245                                         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);
246                                 }
247                         }
248                         else
249                         {
250                                 (*wl)[nick] = "";
251                                 user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
252                         }
253                 }
254
255                 return CMD_FAILURE;
256         }
257
258         CommandWatch (InspIRCd* Instance, unsigned int &maxwatch) : Command(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
259         {
260                 this->source = "m_watch.so";
261                 syntax = "[C|L|S]|[+|-<nick>]";
262                 TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
263         }
264
265         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
266         {
267                 if (parameters.empty())
268                 {
269                         watchlist* wl;
270                         if (user->GetExt("watchlist", wl))
271                         {
272                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
273                                 {
274                                         if (!q->second.empty())
275                                                 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
276                                 }
277                         }
278                         user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
279                 }
280                 else if (parameters.size() > 0)
281                 {
282                         for (int x = 0; x < (int)parameters.size(); x++)
283                         {
284                                 const char *nick = parameters[x].c_str();
285                                 if (!strcasecmp(nick,"C"))
286                                 {
287                                         // watch clear
288                                         watchlist* wl;
289                                         if (user->GetExt("watchlist", wl))
290                                         {
291                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
292                                                 {
293                                                         watchentries::iterator i2 = whos_watching_me->find(i->first);
294                                                         if (i2 != whos_watching_me->end())
295                                                         {
296                                                                 /* People are watching this user, am i one of them? */
297                                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
298                                                                 if (n != i2->second.end())
299                                                                         /* I'm no longer watching you... */
300                                                                         i2->second.erase(n);
301
302                                                                 if (i2->second.empty())
303                                                                         /* nobody else is, either. */
304                                                                         whos_watching_me->erase(i2);
305                                                         }
306                                                 }
307
308                                                 delete wl;
309                                                 user->Shrink("watchlist");
310                                         }
311                                 }
312                                 else if (!strcasecmp(nick,"L"))
313                                 {
314                                         watchlist* wl;
315                                         if (user->GetExt("watchlist", wl))
316                                         {
317                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
318                                                 {
319                                                         if (!q->second.empty())
320                                                         {
321                                                                 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
322                                                                 User *targ = ServerInstance->FindNick(q->first.c_str());
323                                                                 if (IS_AWAY(targ))
324                                                                 {
325                                                                         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);
326                                                                 }
327                                                         }
328                                                         else
329                                                                 user->WriteNumeric(605, "%s %s * * 0 :is offline", user->nick.c_str(), q->first.c_str());
330                                                 }
331                                         }
332                                         user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
333                                 }
334                                 else if (!strcasecmp(nick,"S"))
335                                 {
336                                         watchlist* wl;
337                                         int you_have = 0;
338                                         int youre_on = 0;
339                                         std::string list;
340
341                                         if (user->GetExt("watchlist", wl))
342                                         {
343                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
344                                                         list.append(q->first.c_str()).append(" ");
345                                                 you_have = wl->size();
346                                         }
347
348                                         watchentries::iterator i2 = whos_watching_me->find(user->nick.c_str());
349                                         if (i2 != whos_watching_me->end())
350                                                 youre_on = i2->second.size();
351
352                                         user->WriteNumeric(603, "%s :You have %d and are on %d WATCH entries", user->nick.c_str(), you_have, youre_on);
353                                         user->WriteNumeric(606, "%s :%s",user->nick.c_str(), list.c_str());
354                                         user->WriteNumeric(607, "%s :End of WATCH S",user->nick.c_str());
355                                 }
356                                 else if (nick[0] == '-')
357                                 {
358                                         nick++;
359                                         remove_watch(user, nick);
360                                 }
361                                 else if (nick[0] == '+')
362                                 {
363                                         nick++;
364                                         add_watch(user, nick);
365                                 }
366                         }
367                 }
368                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
369                 return CMD_FAILURE;
370         }
371 };
372
373 class Modulewatch : public Module
374 {
375         CommandWatch* mycommand;
376         CommandSVSWatch *sw;
377         unsigned int maxwatch;
378
379  public:
380         Modulewatch(InspIRCd* Me)
381                 : Module(Me), maxwatch(32)
382         {
383                 OnRehash(NULL, "");
384                 whos_watching_me = new watchentries();
385                 mycommand = new CommandWatch(ServerInstance, maxwatch);
386                 ServerInstance->AddCommand(mycommand);
387                 sw = new CommandSVSWatch(ServerInstance);
388                 ServerInstance->AddCommand(sw);
389                 Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
390                 ServerInstance->Modules->Attach(eventlist, this, 8);
391         }
392
393         virtual void OnRehash(User* user, const std::string &parameter)
394         {
395                 ConfigReader Conf(ServerInstance);
396                 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
397                 if (!maxwatch)
398                         maxwatch = 32;
399         }
400
401         virtual int OnSetAway(User *user, const std::string &awaymsg)
402         {
403                 std::string numeric;
404                 int inum;
405
406                 if (awaymsg.empty())
407                 {
408                         numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
409                         inum = 599;
410                 }
411                 else
412                 {
413                         numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
414                         inum = 598;
415                 }
416
417                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
418                 if (x != whos_watching_me->end())
419                 {
420                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
421                         {
422                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
423                                         (*n)->WriteNumeric(inum, numeric);
424                         }
425                 }
426
427                 return 0;
428         }
429
430         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
431         {
432                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
433                 if (x != whos_watching_me->end())
434                 {
435                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
436                         {
437                                 if (!user->Visibility || user->Visibility->VisibleTo(*n))
438                                         (*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());
439
440                                 watchlist* wl;
441                                 if ((*n)->GetExt("watchlist", wl))
442                                         /* We were on somebody's notify list, set ourselves offline */
443                                         (*wl)[user->nick.c_str()] = "";
444                         }
445                 }
446
447                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
448                 watchlist* wl;
449                 if (user->GetExt("watchlist", wl))
450                 {
451                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
452                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
453                         {
454                                 watchentries::iterator i2 = whos_watching_me->find(i->first);
455                                 if (i2 != whos_watching_me->end())
456                                 {
457                                                 /* People are watching this user, am i one of them? */
458                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
459                                                 if (n != i2->second.end())
460                                                         /* I'm no longer watching you... */
461                                                         i2->second.erase(n);
462
463                                                 if (i2->second.empty())
464                                                         /* and nobody else is, either. */
465                                                         whos_watching_me->erase(i2);
466                                 }
467                         }
468
469                         /* User's quitting, we're done with this. */
470                         delete wl;
471                         user->Shrink("watchlist");
472                 }
473         }
474
475         virtual void OnGarbageCollect()
476         {
477                 watchentries* old_watch = whos_watching_me;
478                 whos_watching_me = new watchentries();
479
480                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
481                         whos_watching_me->insert(*n);
482
483                 delete old_watch;
484         }
485
486         virtual void OnCleanup(int target_type, void* item)
487         {
488                 if (target_type == TYPE_USER)
489                 {
490                         watchlist* wl;
491                         User* user = (User*)item;
492
493                         if (user->GetExt("watchlist", wl))
494                         {
495                                 user->Shrink("watchlist");
496                                 delete wl;
497                         }
498                 }
499         }
500
501         virtual void OnPostConnect(User* user)
502         {
503                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
504                 if (x != whos_watching_me->end())
505                 {
506                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
507                         {
508                                 if (!user->Visibility || user->Visibility->VisibleTo(*n))
509                                         (*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);
510
511                                 watchlist* wl;
512                                 if ((*n)->GetExt("watchlist", wl))
513                                         /* We were on somebody's notify list, set ourselves online */
514                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
515                         }
516                 }
517         }
518
519         virtual void OnUserPostNick(User* user, const std::string &oldnick)
520         {
521                 watchentries::iterator new_offline = whos_watching_me->find(oldnick.c_str());
522                 watchentries::iterator new_online = whos_watching_me->find(user->nick.c_str());
523
524                 if (new_offline != whos_watching_me->end())
525                 {
526                         for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
527                         {
528                                 watchlist* wl;
529                                 if ((*n)->GetExt("watchlist", wl))
530                                 {
531                                         if (!user->Visibility || user->Visibility->VisibleTo(*n))
532                                                 (*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);
533                                         (*wl)[oldnick.c_str()] = "";
534                                 }
535                         }
536                 }
537
538                 if (new_online != whos_watching_me->end())
539                 {
540                         for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
541                         {
542                                 watchlist* wl;
543                                 if ((*n)->GetExt("watchlist", wl))
544                                 {
545                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
546                                         if (!user->Visibility || user->Visibility->VisibleTo(*n))
547                                                 (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
548                                 }
549                         }
550                 }
551         }
552
553         virtual void On005Numeric(std::string &output)
554         {
555                 // we don't really have a limit...
556                 output = output + " WATCH=" + ConvToStr(maxwatch);
557         }
558
559         virtual ~Modulewatch()
560         {
561                 delete whos_watching_me;
562         }
563
564         virtual Version GetVersion()
565         {
566                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
567         }
568 };
569
570 MODULE_INIT(Modulewatch)
571