]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
6d719d13d93ce795122167c6113824a1b1e86cf1
[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://wiki.inspircd.org/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 #if defined(WINDOWS) && !defined(HASHMAP_DEPRECATED)
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, Module* Creator) : Command(Instance, Creator,"SVSWATCH", 0, 2)
107         {
108                 syntax = "<target> [C|L|S]|[+|-<nick>]";
109                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
110         }
111
112         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
113         {
114                 if (!ServerInstance->ULine(user->server))
115                         return CMD_FAILURE;
116
117                 User *u = ServerInstance->FindNick(parameters[0]);
118                 if (!u)
119                         return CMD_FAILURE;
120
121                 if (IS_LOCAL(u))
122                 {
123                         ServerInstance->Parser->CallHandler("WATCH", parameters, u);
124                 }
125
126                 return CMD_SUCCESS;
127         }
128 };
129
130 /** Handle /WATCH
131  */
132 class CommandWatch : public Command
133 {
134         unsigned int& MAX_WATCH;
135  public:
136         CmdResult remove_watch(User* user, const char* nick)
137         {
138                 // removing an item from the list
139                 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
140                 {
141                         user->WriteNumeric(942, "%s %s :Invalid nickname", user->nick.c_str(), nick);
142                         return CMD_FAILURE;
143                 }
144
145                 watchlist* wl;
146                 if (user->GetExt("watchlist", wl))
147                 {
148                         /* Yup, is on my list */
149                         watchlist::iterator n = wl->find(nick);
150
151                         if (!wl)
152                                 return CMD_FAILURE;
153
154                         if (n != wl->end())
155                         {
156                                 if (!n->second.empty())
157                                         user->WriteNumeric(602, "%s %s %s :stopped watching", user->nick.c_str(), n->first.c_str(), n->second.c_str());
158                                 else
159                                         user->WriteNumeric(602, "%s %s * * 0 :stopped watching", user->nick.c_str(), nick);
160
161                                 wl->erase(n);
162                         }
163
164                         if (wl->empty())
165                         {
166                                 user->Shrink("watchlist");
167                                 delete wl;
168                         }
169
170                         watchentries::iterator x = whos_watching_me->find(nick);
171                         if (x != whos_watching_me->end())
172                         {
173                                 /* People are watching this user, am i one of them? */
174                                 std::deque<User*>::iterator n2 = std::find(x->second.begin(), x->second.end(), user);
175                                 if (n2 != x->second.end())
176                                         /* I'm no longer watching you... */
177                                         x->second.erase(n2);
178
179                                 if (x->second.empty())
180                                         /* nobody else is, either. */
181                                         whos_watching_me->erase(nick);
182                         }
183                 }
184
185                 return CMD_LOCALONLY;
186         }
187
188         CmdResult add_watch(User* user, const char* nick)
189         {
190                 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
191                 {
192                         user->WriteNumeric(942, "%s %s :Invalid nickname",user->nick.c_str(),nick);
193                         return CMD_FAILURE;
194                 }
195
196                 watchlist* wl;
197                 if (!user->GetExt("watchlist", wl))
198                 {
199                         wl = new watchlist();
200                         user->Extend("watchlist", wl);
201                 }
202
203                 if (wl->size() == MAX_WATCH)
204                 {
205                         user->WriteNumeric(512, "%s %s :Too many WATCH entries", user->nick.c_str(), nick);
206                         return CMD_FAILURE;
207                 }
208
209                 watchlist::iterator n = wl->find(nick);
210                 if (n == wl->end())
211                 {
212                         /* Don't already have the user on my watch list, proceed */
213                         watchentries::iterator x = whos_watching_me->find(nick);
214                         if (x != whos_watching_me->end())
215                         {
216                                 /* People are watching this user, add myself */
217                                 x->second.push_back(user);
218                         }
219                         else
220                         {
221                                 std::deque<User*> newlist;
222                                 newlist.push_back(user);
223                                 (*(whos_watching_me))[nick] = newlist;
224                         }
225
226                         User* target = ServerInstance->FindNick(nick);
227                         if (target)
228                         {
229                                 if (target->Visibility && !target->Visibility->VisibleTo(user))
230                                 {
231                                         (*wl)[nick] = "";
232                                         user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
233                                         return CMD_FAILURE;
234                                 }
235
236                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
237                                 user->WriteNumeric(604, "%s %s %s :is online",user->nick.c_str(), nick, (*wl)[nick].c_str());
238                                 if (IS_AWAY(target))
239                                 {
240                                         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);
241                                 }
242                         }
243                         else
244                         {
245                                 (*wl)[nick] = "";
246                                 user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
247                         }
248                 }
249
250                 return CMD_LOCALONLY;
251         }
252
253         CommandWatch (InspIRCd* Instance, Module* parent, unsigned int &maxwatch) : Command(Instance,parent,"WATCH",0,0), MAX_WATCH(maxwatch)
254         {
255                 syntax = "[C|L|S]|[+|-<nick>]";
256                 TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
257         }
258
259         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
260         {
261                 if (parameters.empty())
262                 {
263                         watchlist* wl;
264                         if (user->GetExt("watchlist", wl))
265                         {
266                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
267                                 {
268                                         if (!q->second.empty())
269                                                 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
270                                 }
271                         }
272                         user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
273                 }
274                 else if (parameters.size() > 0)
275                 {
276                         for (int x = 0; x < (int)parameters.size(); x++)
277                         {
278                                 const char *nick = parameters[x].c_str();
279                                 if (!strcasecmp(nick,"C"))
280                                 {
281                                         // watch clear
282                                         watchlist* wl;
283                                         if (user->GetExt("watchlist", wl))
284                                         {
285                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
286                                                 {
287                                                         watchentries::iterator i2 = whos_watching_me->find(i->first);
288                                                         if (i2 != whos_watching_me->end())
289                                                         {
290                                                                 /* People are watching this user, am i one of them? */
291                                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
292                                                                 if (n != i2->second.end())
293                                                                         /* I'm no longer watching you... */
294                                                                         i2->second.erase(n);
295
296                                                                 if (i2->second.empty())
297                                                                         /* nobody else is, either. */
298                                                                         whos_watching_me->erase(i2);
299                                                         }
300                                                 }
301
302                                                 delete wl;
303                                                 user->Shrink("watchlist");
304                                         }
305                                 }
306                                 else if (!strcasecmp(nick,"L"))
307                                 {
308                                         watchlist* wl;
309                                         if (user->GetExt("watchlist", wl))
310                                         {
311                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
312                                                 {
313                                                         if (!q->second.empty())
314                                                         {
315                                                                 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
316                                                                 User *targ = ServerInstance->FindNick(q->first.c_str());
317                                                                 if (IS_AWAY(targ))
318                                                                 {
319                                                                         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);
320                                                                 }
321                                                         }
322                                                         else
323                                                                 user->WriteNumeric(605, "%s %s * * 0 :is offline", user->nick.c_str(), q->first.c_str());
324                                                 }
325                                         }
326                                         user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
327                                 }
328                                 else if (!strcasecmp(nick,"S"))
329                                 {
330                                         watchlist* wl;
331                                         int you_have = 0;
332                                         int youre_on = 0;
333                                         std::string list;
334
335                                         if (user->GetExt("watchlist", wl))
336                                         {
337                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
338                                                         list.append(q->first.c_str()).append(" ");
339                                                 you_have = wl->size();
340                                         }
341
342                                         watchentries::iterator i2 = whos_watching_me->find(user->nick.c_str());
343                                         if (i2 != whos_watching_me->end())
344                                                 youre_on = i2->second.size();
345
346                                         user->WriteNumeric(603, "%s :You have %d and are on %d WATCH entries", user->nick.c_str(), you_have, youre_on);
347                                         user->WriteNumeric(606, "%s :%s",user->nick.c_str(), list.c_str());
348                                         user->WriteNumeric(607, "%s :End of WATCH S",user->nick.c_str());
349                                 }
350                                 else if (nick[0] == '-')
351                                 {
352                                         nick++;
353                                         remove_watch(user, nick);
354                                 }
355                                 else if (nick[0] == '+')
356                                 {
357                                         nick++;
358                                         add_watch(user, nick);
359                                 }
360                         }
361                 }
362                 return CMD_LOCALONLY;
363         }
364 };
365
366 class Modulewatch : public Module
367 {
368         unsigned int maxwatch;
369         CommandWatch cmdw;
370         CommandSVSWatch sw;
371
372  public:
373         Modulewatch(InspIRCd* Me)
374                 : Module(Me), maxwatch(32), cmdw(Me, this, maxwatch), sw(Me,this) 
375         {
376                 OnRehash(NULL);
377                 whos_watching_me = new watchentries();
378                 ServerInstance->AddCommand(&cmdw);
379                 ServerInstance->AddCommand(&sw);
380                 Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
381                 ServerInstance->Modules->Attach(eventlist, this, 8);
382         }
383
384         virtual void OnRehash(User* user)
385         {
386                 ConfigReader Conf(ServerInstance);
387                 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
388                 if (!maxwatch)
389                         maxwatch = 32;
390         }
391
392         virtual int OnSetAway(User *user, const std::string &awaymsg)
393         {
394                 std::string numeric;
395                 int inum;
396
397                 if (awaymsg.empty())
398                 {
399                         numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
400                         inum = 599;
401                 }
402                 else
403                 {
404                         numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
405                         inum = 598;
406                 }
407
408                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
409                 if (x != whos_watching_me->end())
410                 {
411                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
412                         {
413                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
414                                         (*n)->WriteNumeric(inum, numeric);
415                         }
416                 }
417
418                 return 0;
419         }
420
421         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
422         {
423                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
424                 if (x != whos_watching_me->end())
425                 {
426                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
427                         {
428                                 if (!user->Visibility || user->Visibility->VisibleTo(*n))
429                                         (*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());
430
431                                 watchlist* wl;
432                                 if ((*n)->GetExt("watchlist", wl))
433                                         /* We were on somebody's notify list, set ourselves offline */
434                                         (*wl)[user->nick.c_str()] = "";
435                         }
436                 }
437
438                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
439                 watchlist* wl;
440                 if (user->GetExt("watchlist", wl))
441                 {
442                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
443                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
444                         {
445                                 watchentries::iterator i2 = whos_watching_me->find(i->first);
446                                 if (i2 != whos_watching_me->end())
447                                 {
448                                                 /* People are watching this user, am i one of them? */
449                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
450                                                 if (n != i2->second.end())
451                                                         /* I'm no longer watching you... */
452                                                         i2->second.erase(n);
453
454                                                 if (i2->second.empty())
455                                                         /* and nobody else is, either. */
456                                                         whos_watching_me->erase(i2);
457                                 }
458                         }
459
460                         /* User's quitting, we're done with this. */
461                         delete wl;
462                         user->Shrink("watchlist");
463                 }
464         }
465
466         virtual void OnGarbageCollect()
467         {
468                 watchentries* old_watch = whos_watching_me;
469                 whos_watching_me = new watchentries();
470
471                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
472                         whos_watching_me->insert(*n);
473
474                 delete old_watch;
475         }
476
477         virtual void OnCleanup(int target_type, void* item)
478         {
479                 if (target_type == TYPE_USER)
480                 {
481                         watchlist* wl;
482                         User* user = (User*)item;
483
484                         if (user->GetExt("watchlist", wl))
485                         {
486                                 user->Shrink("watchlist");
487                                 delete wl;
488                         }
489                 }
490         }
491
492         virtual void OnPostConnect(User* user)
493         {
494                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
495                 if (x != whos_watching_me->end())
496                 {
497                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
498                         {
499                                 if (!user->Visibility || user->Visibility->VisibleTo(*n))
500                                         (*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);
501
502                                 watchlist* wl;
503                                 if ((*n)->GetExt("watchlist", wl))
504                                         /* We were on somebody's notify list, set ourselves online */
505                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
506                         }
507                 }
508         }
509
510         virtual void OnUserPostNick(User* user, const std::string &oldnick)
511         {
512                 watchentries::iterator new_offline = whos_watching_me->find(oldnick.c_str());
513                 watchentries::iterator new_online = whos_watching_me->find(user->nick.c_str());
514
515                 if (new_offline != whos_watching_me->end())
516                 {
517                         for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
518                         {
519                                 watchlist* wl;
520                                 if ((*n)->GetExt("watchlist", wl))
521                                 {
522                                         if (!user->Visibility || user->Visibility->VisibleTo(*n))
523                                                 (*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);
524                                         (*wl)[oldnick.c_str()] = "";
525                                 }
526                         }
527                 }
528
529                 if (new_online != whos_watching_me->end())
530                 {
531                         for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
532                         {
533                                 watchlist* wl;
534                                 if ((*n)->GetExt("watchlist", wl))
535                                 {
536                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
537                                         if (!user->Visibility || user->Visibility->VisibleTo(*n))
538                                                 (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
539                                 }
540                         }
541                 }
542         }
543
544         virtual void On005Numeric(std::string &output)
545         {
546                 // we don't really have a limit...
547                 output = output + " WATCH=" + ConvToStr(maxwatch);
548         }
549
550         virtual ~Modulewatch()
551         {
552                 delete whos_watching_me;
553         }
554
555         virtual Version GetVersion()
556         {
557                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
558         }
559 };
560
561 MODULE_INIT(Modulewatch)
562