]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
Windows: In-depth cleanup (see details)
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 /* $ModDesc: Provides support for the /WATCH command */
26
27
28 /*
29  * Okay, it's nice that this was documented and all, but I at least understood very little
30  * of it, so I'm going to attempt to explain the data structures in here a bit more.
31  *
32  * For efficiency, many data structures are kept.
33  *
34  * The first is a global list `watchentries':
35  *      hash_map<irc::string, std::deque<User*> >
36  *
37  * That is, if nick 'w00t' is being watched by user pointer 'Brain' and 'Om', <w00t, (Brain, Om)>
38  * will be in the watchentries list.
39  *
40  * The second is that each user has a per-user data structure attached to their user record via Extensible:
41  *      std::map<irc::string, std::string> watchlist;
42  * So, in the above example with w00t watched by Brain and Om, we'd have:
43  *      Brain-
44  *            `- w00t
45  *      Om-
46  *         `- w00t
47  *
48  * Hopefully this helps any brave soul that ventures into this file other than me. :-)
49  *              -- w00t (mar 30, 2008)
50  */
51
52
53 /* This module has been refactored to provide a very efficient (in terms of cpu time)
54  * implementation of /WATCH.
55  *
56  * To improve the efficiency of watch, many lists are kept. The first primary list is
57  * a hash_map of who's being watched by who. For example:
58  *
59  * KEY: Brain   --->  Watched by:  Boo, w00t, Om
60  * KEY: Boo     --->  Watched by:  Brain, w00t
61  *
62  * This is used when we want to tell all the users that are watching someone that
63  * they are now available or no longer available. For example, if the hash was
64  * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
65  * and Om by reading their 'watched by' list. When this occurs, their online status
66  * in each of these users lists (see below) is also updated.
67  *
68  * Each user also has a seperate (smaller) map attached to their User whilst they
69  * have any watch entries, which is managed by class Extensible. When they add or remove
70  * a watch entry from their list, it is inserted here, as well as the main list being
71  * maintained. This map also contains the user's online status. For users that are
72  * offline, the key points at an empty string, and for users that are online, the key
73  * points at a string containing "users-ident users-host users-signon-time". This is
74  * stored in this manner so that we don't have to FindUser() to fetch this info, the
75  * users signon can populate the field for us.
76  *
77  * For example, going again on the example above, this would be w00t's watchlist:
78  *
79  * KEY: Boo    --->  Status: "Boo brains.sexy.babe 535342348"
80  * KEY: Brain  --->  Status: ""
81  *
82  * In this list we can see that Boo is online, and Brain is offline. We can then
83  * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
84  * of the above two data structures, with minimum CPU penalty for doing so.
85  *
86  * In short, the least efficient this ever gets is O(n), and thats only because
87  * there are parts that *must* loop (e.g. telling all users that are watching a
88  * nick that the user online), however this is a *major* improvement over the
89  * 1.0 implementation, which in places had O(n^n) and worse in it, because this
90  * implementation scales based upon the sizes of the watch entries, whereas the
91  * old system would scale (or not as the case may be) according to the total number
92  * of users using WATCH.
93  */
94
95 /*
96  * Before you start screaming, this definition is only used here, so moving it to a header is pointless.
97  * Yes, it's horrid. Blame cl for being different. -- w00t
98  */
99
100 typedef nspace::hash_map<irc::string, std::deque<User*>, irc::hash> watchentries;
101 typedef std::map<irc::string, std::string> watchlist;
102
103 /* Who's watching each nickname.
104  * NOTE: We do NOT iterate this to display a user's WATCH list!
105  * See the comments above!
106  */
107 watchentries* whos_watching_me;
108
109 class CommandSVSWatch : public Command
110 {
111  public:
112         CommandSVSWatch(Module* Creator) : Command(Creator,"SVSWATCH", 2)
113         {
114                 syntax = "<target> [C|L|S]|[+|-<nick>]";
115                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
116         }
117
118         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
119         {
120                 if (!ServerInstance->ULine(user->server))
121                         return CMD_FAILURE;
122
123                 User *u = ServerInstance->FindNick(parameters[0]);
124                 if (!u)
125                         return CMD_FAILURE;
126
127                 if (IS_LOCAL(u))
128                 {
129                         ServerInstance->Parser->CallHandler("WATCH", parameters, u);
130                 }
131
132                 return CMD_SUCCESS;
133         }
134
135         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
136         {
137                 User* target = ServerInstance->FindNick(parameters[0]);
138                 if (target)
139                         return ROUTE_OPT_UCAST(target->server);
140                 return ROUTE_LOCALONLY;
141         }
142 };
143
144 /** Handle /WATCH
145  */
146 class CommandWatch : public Command
147 {
148         unsigned int& MAX_WATCH;
149  public:
150         SimpleExtItem<watchlist> ext;
151         CmdResult remove_watch(User* user, const char* nick)
152         {
153                 // removing an item from the list
154                 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
155                 {
156                         user->WriteNumeric(942, "%s %s :Invalid nickname", user->nick.c_str(), nick);
157                         return CMD_FAILURE;
158                 }
159
160                 watchlist* wl = ext.get(user);
161                 if (wl)
162                 {
163                         /* Yup, is on my list */
164                         watchlist::iterator n = wl->find(nick);
165
166                         if (!wl)
167                                 return CMD_FAILURE;
168
169                         if (n != wl->end())
170                         {
171                                 if (!n->second.empty())
172                                         user->WriteNumeric(602, "%s %s %s :stopped watching", user->nick.c_str(), n->first.c_str(), n->second.c_str());
173                                 else
174                                         user->WriteNumeric(602, "%s %s * * 0 :stopped watching", user->nick.c_str(), nick);
175
176                                 wl->erase(n);
177                         }
178
179                         if (wl->empty())
180                         {
181                                 ext.unset(user);
182                         }
183
184                         watchentries::iterator x = whos_watching_me->find(nick);
185                         if (x != whos_watching_me->end())
186                         {
187                                 /* People are watching this user, am i one of them? */
188                                 std::deque<User*>::iterator n2 = std::find(x->second.begin(), x->second.end(), user);
189                                 if (n2 != x->second.end())
190                                         /* I'm no longer watching you... */
191                                         x->second.erase(n2);
192
193                                 if (x->second.empty())
194                                         /* nobody else is, either. */
195                                         whos_watching_me->erase(nick);
196                         }
197                 }
198
199                 return CMD_SUCCESS;
200         }
201
202         CmdResult add_watch(User* user, const char* nick)
203         {
204                 if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax))
205                 {
206                         user->WriteNumeric(942, "%s %s :Invalid nickname",user->nick.c_str(),nick);
207                         return CMD_FAILURE;
208                 }
209
210                 watchlist* wl = ext.get(user);
211                 if (!wl)
212                 {
213                         wl = new watchlist();
214                         ext.set(user, wl);
215                 }
216
217                 if (wl->size() == MAX_WATCH)
218                 {
219                         user->WriteNumeric(512, "%s %s :Too many WATCH entries", user->nick.c_str(), nick);
220                         return CMD_FAILURE;
221                 }
222
223                 watchlist::iterator n = wl->find(nick);
224                 if (n == wl->end())
225                 {
226                         /* Don't already have the user on my watch list, proceed */
227                         watchentries::iterator x = whos_watching_me->find(nick);
228                         if (x != whos_watching_me->end())
229                         {
230                                 /* People are watching this user, add myself */
231                                 x->second.push_back(user);
232                         }
233                         else
234                         {
235                                 std::deque<User*> newlist;
236                                 newlist.push_back(user);
237                                 (*(whos_watching_me))[nick] = newlist;
238                         }
239
240                         User* target = ServerInstance->FindNick(nick);
241                         if (target)
242                         {
243                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
244                                 user->WriteNumeric(604, "%s %s %s :is online",user->nick.c_str(), nick, (*wl)[nick].c_str());
245                                 if (IS_AWAY(target))
246                                 {
247                                         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);
248                                 }
249                         }
250                         else
251                         {
252                                 (*wl)[nick] = "";
253                                 user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
254                         }
255                 }
256
257                 return CMD_SUCCESS;
258         }
259
260         CommandWatch(Module* parent, unsigned int &maxwatch) : Command(parent,"WATCH", 0), MAX_WATCH(maxwatch), ext("watchlist", parent)
261         {
262                 syntax = "[C|L|S]|[+|-<nick>]";
263                 TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
264         }
265
266         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
267         {
268                 if (parameters.empty())
269                 {
270                         watchlist* wl = ext.get(user);
271                         if (wl)
272                         {
273                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
274                                 {
275                                         if (!q->second.empty())
276                                                 user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str());
277                                 }
278                         }
279                         user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str());
280                 }
281                 else if (parameters.size() > 0)
282                 {
283                         for (int x = 0; x < (int)parameters.size(); x++)
284                         {
285                                 const char *nick = parameters[x].c_str();
286                                 if (!strcasecmp(nick,"C"))
287                                 {
288                                         // watch clear
289                                         watchlist* wl = ext.get(user);
290                                         if (wl)
291                                         {
292                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
293                                                 {
294                                                         watchentries::iterator i2 = whos_watching_me->find(i->first);
295                                                         if (i2 != whos_watching_me->end())
296                                                         {
297                                                                 /* People are watching this user, am i one of them? */
298                                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
299                                                                 if (n != i2->second.end())
300                                                                         /* I'm no longer watching you... */
301                                                                         i2->second.erase(n);
302
303                                                                 if (i2->second.empty())
304                                                                         /* nobody else is, either. */
305                                                                         whos_watching_me->erase(i2);
306                                                         }
307                                                 }
308
309                                                 ext.unset(user);
310                                         }
311                                 }
312                                 else if (!strcasecmp(nick,"L"))
313                                 {
314                                         watchlist* wl = ext.get(user);
315                                         if (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 = ext.get(user);
337                                         int you_have = 0;
338                                         int youre_on = 0;
339                                         std::string list;
340
341                                         if (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                 return CMD_SUCCESS;
369         }
370 };
371
372 class Modulewatch : public Module
373 {
374         unsigned int maxwatch;
375         CommandWatch cmdw;
376         CommandSVSWatch sw;
377
378  public:
379         Modulewatch()
380                 : maxwatch(32), cmdw(this, maxwatch), sw(this) 
381         {
382                 OnRehash(NULL);
383                 whos_watching_me = new watchentries();
384                 ServerInstance->AddCommand(&cmdw);
385                 ServerInstance->AddCommand(&sw);
386                 ServerInstance->Extensions.Register(&cmdw.ext);
387                 Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
388                 ServerInstance->Modules->Attach(eventlist, this, 7);
389         }
390
391         virtual void OnRehash(User* user)
392         {
393                 ConfigReader Conf;
394                 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
395                 if (!maxwatch)
396                         maxwatch = 32;
397         }
398
399         virtual ModResult OnSetAway(User *user, const std::string &awaymsg)
400         {
401                 std::string numeric;
402                 int inum;
403
404                 if (awaymsg.empty())
405                 {
406                         numeric = user->nick + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
407                         inum = 599;
408                 }
409                 else
410                 {
411                         numeric = user->nick + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
412                         inum = 598;
413                 }
414
415                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
416                 if (x != whos_watching_me->end())
417                 {
418                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
419                         {
420                                 (*n)->WriteNumeric(inum, numeric);
421                         }
422                 }
423
424                 return MOD_RES_PASSTHRU;
425         }
426
427         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
428         {
429                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
430                 if (x != whos_watching_me->end())
431                 {
432                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
433                         {
434                                 (*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());
435
436                                 watchlist* wl = cmdw.ext.get(*n);
437                                 if (wl)
438                                         /* We were on somebody's notify list, set ourselves offline */
439                                         (*wl)[user->nick.c_str()] = "";
440                         }
441                 }
442
443                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
444                 watchlist* wl = cmdw.ext.get(user);
445                 if (wl)
446                 {
447                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
448                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
449                         {
450                                 watchentries::iterator i2 = whos_watching_me->find(i->first);
451                                 if (i2 != whos_watching_me->end())
452                                 {
453                                                 /* People are watching this user, am i one of them? */
454                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
455                                                 if (n != i2->second.end())
456                                                         /* I'm no longer watching you... */
457                                                         i2->second.erase(n);
458
459                                                 if (i2->second.empty())
460                                                         /* and nobody else is, either. */
461                                                         whos_watching_me->erase(i2);
462                                 }
463                         }
464                 }
465         }
466
467         virtual void OnGarbageCollect()
468         {
469                 watchentries* old_watch = whos_watching_me;
470                 whos_watching_me = new watchentries();
471
472                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
473                         whos_watching_me->insert(*n);
474
475                 delete old_watch;
476         }
477
478         virtual void OnPostConnect(User* user)
479         {
480                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
481                 if (x != whos_watching_me->end())
482                 {
483                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
484                         {
485                                 (*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);
486
487                                 watchlist* wl = cmdw.ext.get(*n);
488                                 if (wl)
489                                         /* We were on somebody's notify list, set ourselves online */
490                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
491                         }
492                 }
493         }
494
495         virtual void OnUserPostNick(User* user, const std::string &oldnick)
496         {
497                 watchentries::iterator new_offline = whos_watching_me->find(oldnick.c_str());
498                 watchentries::iterator new_online = whos_watching_me->find(user->nick.c_str());
499
500                 if (new_offline != whos_watching_me->end())
501                 {
502                         for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
503                         {
504                                 watchlist* wl = cmdw.ext.get(*n);
505                                 if (wl)
506                                 {
507                                         (*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);
508                                         (*wl)[oldnick.c_str()] = "";
509                                 }
510                         }
511                 }
512
513                 if (new_online != whos_watching_me->end())
514                 {
515                         for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
516                         {
517                                 watchlist* wl = cmdw.ext.get(*n);
518                                 if (wl)
519                                 {
520                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
521                                         (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
522                                 }
523                         }
524                 }
525         }
526
527         virtual void On005Numeric(std::string &output)
528         {
529                 // we don't really have a limit...
530                 output = output + " WATCH=" + ConvToStr(maxwatch);
531         }
532
533         virtual ~Modulewatch()
534         {
535                 delete whos_watching_me;
536         }
537
538         virtual Version GetVersion()
539         {
540                 return Version("Provides support for the /WATCH command", VF_OPTCOMMON | VF_VENDOR);
541         }
542 };
543
544 MODULE_INIT(Modulewatch)
545