]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
57ca18a8fc22e84e9029cf4719684ed9caf414fe
[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
26 /*
27  * Okay, it's nice that this was documented and all, but I at least understood very little
28  * of it, so I'm going to attempt to explain the data structures in here a bit more.
29  *
30  * For efficiency, many data structures are kept.
31  *
32  * The first is a global list `watchentries':
33  *      hash_map<irc::string, std::deque<User*> >
34  *
35  * That is, if nick 'w00t' is being watched by user pointer 'Brain' and 'Om', <w00t, (Brain, Om)>
36  * will be in the watchentries list.
37  *
38  * The second is that each user has a per-user data structure attached to their user record via Extensible:
39  *      std::map<irc::string, std::string> watchlist;
40  * So, in the above example with w00t watched by Brain and Om, we'd have:
41  *      Brain-
42  *            `- w00t
43  *      Om-
44  *         `- w00t
45  *
46  * Hopefully this helps any brave soul that ventures into this file other than me. :-)
47  *              -- w00t (mar 30, 2008)
48  */
49
50
51 /* This module has been refactored to provide a very efficient (in terms of cpu time)
52  * implementation of /WATCH.
53  *
54  * To improve the efficiency of watch, many lists are kept. The first primary list is
55  * a hash_map of who's being watched by who. For example:
56  *
57  * KEY: Brain   --->  Watched by:  Boo, w00t, Om
58  * KEY: Boo     --->  Watched by:  Brain, w00t
59  *
60  * This is used when we want to tell all the users that are watching someone that
61  * they are now available or no longer available. For example, if the hash was
62  * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
63  * and Om by reading their 'watched by' list. When this occurs, their online status
64  * in each of these users lists (see below) is also updated.
65  *
66  * Each user also has a seperate (smaller) map attached to their User whilst they
67  * have any watch entries, which is managed by class Extensible. When they add or remove
68  * a watch entry from their list, it is inserted here, as well as the main list being
69  * maintained. This map also contains the user's online status. For users that are
70  * offline, the key points at an empty string, and for users that are online, the key
71  * points at a string containing "users-ident users-host users-signon-time". This is
72  * stored in this manner so that we don't have to FindUser() to fetch this info, the
73  * users signon can populate the field for us.
74  *
75  * For example, going again on the example above, this would be w00t's watchlist:
76  *
77  * KEY: Boo    --->  Status: "Boo brains.sexy.babe 535342348"
78  * KEY: Brain  --->  Status: ""
79  *
80  * In this list we can see that Boo is online, and Brain is offline. We can then
81  * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
82  * of the above two data structures, with minimum CPU penalty for doing so.
83  *
84  * In short, the least efficient this ever gets is O(n), and thats only because
85  * there are parts that *must* loop (e.g. telling all users that are watching a
86  * nick that the user online), however this is a *major* improvement over the
87  * 1.0 implementation, which in places had O(n^n) and worse in it, because this
88  * implementation scales based upon the sizes of the watch entries, whereas the
89  * old system would scale (or not as the case may be) according to the total number
90  * of users using WATCH.
91  */
92
93 typedef TR1NS::unordered_map<irc::string, std::deque<User*>, irc::hash> watchentries;
94 typedef std::map<irc::string, std::string> watchlist;
95
96 /* Who's watching each nickname.
97  * NOTE: We do NOT iterate this to display a user's WATCH list!
98  * See the comments above!
99  */
100 watchentries* whos_watching_me;
101
102 class CommandSVSWatch : public Command
103 {
104  public:
105         CommandSVSWatch(Module* Creator) : Command(Creator,"SVSWATCH", 2)
106         {
107                 syntax = "<target> [C|L|S]|[+|-<nick>]";
108                 TRANSLATE2(TR_NICK, TR_TEXT); /* we watch for a nick. not a UID. */
109         }
110
111         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
112         {
113                 if (!user->server->IsULine())
114                         return CMD_FAILURE;
115
116                 User *u = ServerInstance->FindNick(parameters[0]);
117                 if (!u)
118                         return CMD_FAILURE;
119
120                 if (IS_LOCAL(u))
121                 {
122                         ServerInstance->Parser->CallHandler("WATCH", parameters, u);
123                 }
124
125                 return CMD_SUCCESS;
126         }
127
128         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
129         {
130                 User* target = ServerInstance->FindNick(parameters[0]);
131                 if (target)
132                         return ROUTE_OPT_UCAST(target->server);
133                 return ROUTE_LOCALONLY;
134         }
135 };
136
137 /** Handle /WATCH
138  */
139 class CommandWatch : public Command
140 {
141         unsigned int& MAX_WATCH;
142  public:
143         SimpleExtItem<watchlist> ext;
144         CmdResult remove_watch(User* user, const char* nick)
145         {
146                 // removing an item from the list
147                 if (!ServerInstance->IsNick(nick))
148                 {
149                         user->WriteNumeric(942, "%s :Invalid nickname", nick);
150                         return CMD_FAILURE;
151                 }
152
153                 watchlist* wl = ext.get(user);
154                 if (wl)
155                 {
156                         /* Yup, is on my list */
157                         watchlist::iterator n = wl->find(nick);
158
159                         if (!wl)
160                                 return CMD_FAILURE;
161
162                         if (n != wl->end())
163                         {
164                                 if (!n->second.empty())
165                                         user->WriteNumeric(602, "%s %s :stopped watching", n->first.c_str(), n->second.c_str());
166                                 else
167                                         user->WriteNumeric(602, "%s * * 0 :stopped watching", nick);
168
169                                 wl->erase(n);
170                         }
171
172                         if (wl->empty())
173                         {
174                                 ext.unset(user);
175                         }
176
177                         watchentries::iterator x = whos_watching_me->find(nick);
178                         if (x != whos_watching_me->end())
179                         {
180                                 /* People are watching this user, am i one of them? */
181                                 std::deque<User*>::iterator n2 = std::find(x->second.begin(), x->second.end(), user);
182                                 if (n2 != x->second.end())
183                                         /* I'm no longer watching you... */
184                                         x->second.erase(n2);
185
186                                 if (x->second.empty())
187                                         /* nobody else is, either. */
188                                         whos_watching_me->erase(nick);
189                         }
190                 }
191
192                 return CMD_SUCCESS;
193         }
194
195         CmdResult add_watch(User* user, const char* nick)
196         {
197                 if (!ServerInstance->IsNick(nick))
198                 {
199                         user->WriteNumeric(942, "%s :Invalid nickname", nick);
200                         return CMD_FAILURE;
201                 }
202
203                 watchlist* wl = ext.get(user);
204                 if (!wl)
205                 {
206                         wl = new watchlist();
207                         ext.set(user, wl);
208                 }
209
210                 if (wl->size() == MAX_WATCH)
211                 {
212                         user->WriteNumeric(512, "%s :Too many WATCH entries", nick);
213                         return CMD_FAILURE;
214                 }
215
216                 watchlist::iterator n = wl->find(nick);
217                 if (n == wl->end())
218                 {
219                         /* Don't already have the user on my watch list, proceed */
220                         watchentries::iterator x = whos_watching_me->find(nick);
221                         if (x != whos_watching_me->end())
222                         {
223                                 /* People are watching this user, add myself */
224                                 x->second.push_back(user);
225                         }
226                         else
227                         {
228                                 std::deque<User*> newlist;
229                                 newlist.push_back(user);
230                                 (*(whos_watching_me))[nick] = newlist;
231                         }
232
233                         User* target = ServerInstance->FindNick(nick);
234                         if ((target) && (target->registered == REG_ALL))
235                         {
236                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
237                                 user->WriteNumeric(604, "%s %s :is online", nick, (*wl)[nick].c_str());
238                                 if (target->IsAway())
239                                 {
240                                         user->WriteNumeric(609, "%s %s %s %lu :is away", target->nick.c_str(), target->ident.c_str(), target->dhost.c_str(), (unsigned long) target->awaytime);
241                                 }
242                         }
243                         else
244                         {
245                                 (*wl)[nick].clear();
246                                 user->WriteNumeric(605, "%s * * 0 :is offline", nick);
247                         }
248                 }
249
250                 return CMD_SUCCESS;
251         }
252
253         CommandWatch(Module* parent, unsigned int &maxwatch) : Command(parent,"WATCH", 0), MAX_WATCH(maxwatch), ext("watchlist", parent)
254         {
255                 syntax = "[C|L|S]|[+|-<nick>]";
256         }
257
258         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
259         {
260                 if (parameters.empty())
261                 {
262                         watchlist* wl = ext.get(user);
263                         if (wl)
264                         {
265                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
266                                 {
267                                         if (!q->second.empty())
268                                                 user->WriteNumeric(604, "%s %s :is online", q->first.c_str(), q->second.c_str());
269                                 }
270                         }
271                         user->WriteNumeric(607, ":End of WATCH list");
272                 }
273                 else if (parameters.size() > 0)
274                 {
275                         for (int x = 0; x < (int)parameters.size(); x++)
276                         {
277                                 const char *nick = parameters[x].c_str();
278                                 if (!strcasecmp(nick,"C"))
279                                 {
280                                         // watch clear
281                                         watchlist* wl = ext.get(user);
282                                         if (wl)
283                                         {
284                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
285                                                 {
286                                                         watchentries::iterator i2 = whos_watching_me->find(i->first);
287                                                         if (i2 != whos_watching_me->end())
288                                                         {
289                                                                 /* People are watching this user, am i one of them? */
290                                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
291                                                                 if (n != i2->second.end())
292                                                                         /* I'm no longer watching you... */
293                                                                         i2->second.erase(n);
294
295                                                                 if (i2->second.empty())
296                                                                         /* nobody else is, either. */
297                                                                         whos_watching_me->erase(i2);
298                                                         }
299                                                 }
300
301                                                 ext.unset(user);
302                                         }
303                                 }
304                                 else if (!strcasecmp(nick,"L"))
305                                 {
306                                         watchlist* wl = ext.get(user);
307                                         if (wl)
308                                         {
309                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
310                                                 {
311                                                         User* targ = ServerInstance->FindNick(q->first.c_str());
312                                                         if (targ && !q->second.empty())
313                                                         {
314                                                                 user->WriteNumeric(604, "%s %s :is online", q->first.c_str(), q->second.c_str());
315                                                                 if (targ->IsAway())
316                                                                 {
317                                                                         user->WriteNumeric(609, "%s %s %s %lu :is away", targ->nick.c_str(), targ->ident.c_str(), targ->dhost.c_str(), (unsigned long) targ->awaytime);
318                                                                 }
319                                                         }
320                                                         else
321                                                                 user->WriteNumeric(605, "%s * * 0 :is offline", q->first.c_str());
322                                                 }
323                                         }
324                                         user->WriteNumeric(607, ":End of WATCH list");
325                                 }
326                                 else if (!strcasecmp(nick,"S"))
327                                 {
328                                         watchlist* wl = ext.get(user);
329                                         int you_have = 0;
330                                         int youre_on = 0;
331                                         std::string list;
332
333                                         if (wl)
334                                         {
335                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
336                                                         list.append(q->first.c_str()).append(" ");
337                                                 you_have = wl->size();
338                                         }
339
340                                         watchentries::iterator i2 = whos_watching_me->find(user->nick.c_str());
341                                         if (i2 != whos_watching_me->end())
342                                                 youre_on = i2->second.size();
343
344                                         user->WriteNumeric(603, ":You have %d and are on %d WATCH entries", you_have, youre_on);
345                                         user->WriteNumeric(606, ":%s", list.c_str());
346                                         user->WriteNumeric(607, ":End of WATCH S");
347                                 }
348                                 else if (nick[0] == '-')
349                                 {
350                                         nick++;
351                                         remove_watch(user, nick);
352                                 }
353                                 else if (nick[0] == '+')
354                                 {
355                                         nick++;
356                                         add_watch(user, nick);
357                                 }
358                         }
359                 }
360                 return CMD_SUCCESS;
361         }
362 };
363
364 class Modulewatch : public Module
365 {
366         unsigned int maxwatch;
367         CommandWatch cmdw;
368         CommandSVSWatch sw;
369
370  public:
371         Modulewatch()
372                 : maxwatch(32), cmdw(this, maxwatch), sw(this)
373         {
374                 whos_watching_me = new watchentries();
375         }
376
377         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
378         {
379                 maxwatch = ServerInstance->Config->ConfValue("watch")->getInt("maxentries", 32);
380                 if (!maxwatch)
381                         maxwatch = 32;
382         }
383
384         ModResult OnSetAway(User *user, const std::string &awaymsg) CXX11_OVERRIDE
385         {
386                 std::string numeric;
387                 int inum;
388
389                 if (awaymsg.empty())
390                 {
391                         numeric = user->nick + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
392                         inum = 599;
393                 }
394                 else
395                 {
396                         numeric = user->nick + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
397                         inum = 598;
398                 }
399
400                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
401                 if (x != whos_watching_me->end())
402                 {
403                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
404                         {
405                                 (*n)->WriteNumeric(inum, numeric);
406                         }
407                 }
408
409                 return MOD_RES_PASSTHRU;
410         }
411
412         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message) CXX11_OVERRIDE
413         {
414                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
415                 if (x != whos_watching_me->end())
416                 {
417                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
418                         {
419                                 (*n)->WriteNumeric(601, "%s %s %s %lu :went offline", user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) ServerInstance->Time());
420
421                                 watchlist* wl = cmdw.ext.get(*n);
422                                 if (wl)
423                                         /* We were on somebody's notify list, set ourselves offline */
424                                         (*wl)[user->nick.c_str()].clear();
425                         }
426                 }
427
428                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
429                 watchlist* wl = cmdw.ext.get(user);
430                 if (wl)
431                 {
432                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
433                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
434                         {
435                                 watchentries::iterator i2 = whos_watching_me->find(i->first);
436                                 if (i2 != whos_watching_me->end())
437                                 {
438                                                 /* People are watching this user, am i one of them? */
439                                                 std::deque<User*>::iterator n = std::find(i2->second.begin(), i2->second.end(), user);
440                                                 if (n != i2->second.end())
441                                                         /* I'm no longer watching you... */
442                                                         i2->second.erase(n);
443
444                                                 if (i2->second.empty())
445                                                         /* and nobody else is, either. */
446                                                         whos_watching_me->erase(i2);
447                                 }
448                         }
449                 }
450         }
451
452         void OnGarbageCollect()
453         {
454                 watchentries* old_watch = whos_watching_me;
455                 whos_watching_me = new watchentries();
456
457                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
458                         whos_watching_me->insert(*n);
459
460                 delete old_watch;
461         }
462
463         void OnPostConnect(User* user) CXX11_OVERRIDE
464         {
465                 watchentries::iterator x = whos_watching_me->find(user->nick.c_str());
466                 if (x != whos_watching_me->end())
467                 {
468                         for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
469                         {
470                                 (*n)->WriteNumeric(600, "%s %s %s %lu :arrived online", user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
471
472                                 watchlist* wl = cmdw.ext.get(*n);
473                                 if (wl)
474                                         /* We were on somebody's notify list, set ourselves online */
475                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
476                         }
477                 }
478         }
479
480         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
481         {
482                 watchentries::iterator new_offline = whos_watching_me->find(oldnick.c_str());
483                 watchentries::iterator new_online = whos_watching_me->find(user->nick.c_str());
484
485                 if (new_offline != whos_watching_me->end())
486                 {
487                         for (std::deque<User*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
488                         {
489                                 watchlist* wl = cmdw.ext.get(*n);
490                                 if (wl)
491                                 {
492                                         (*n)->WriteNumeric(601, "%s %s %s %lu :went offline", oldnick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age);
493                                         (*wl)[oldnick.c_str()].clear();
494                                 }
495                         }
496                 }
497
498                 if (new_online != whos_watching_me->end())
499                 {
500                         for (std::deque<User*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
501                         {
502                                 watchlist* wl = cmdw.ext.get(*n);
503                                 if (wl)
504                                 {
505                                         (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
506                                         (*n)->WriteNumeric(600, "%s %s :arrived online", user->nick.c_str(), (*wl)[user->nick.c_str()].c_str());
507                                 }
508                         }
509                 }
510         }
511
512         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
513         {
514                 tokens["WATCH"] = ConvToStr(maxwatch);
515         }
516
517         ~Modulewatch()
518         {
519                 delete whos_watching_me;
520         }
521
522         Version GetVersion() CXX11_OVERRIDE
523         {
524                 return Version("Provides support for the /WATCH command", VF_OPTCOMMON | VF_VENDOR);
525         }
526 };
527
528 MODULE_INIT(Modulewatch)