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