]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
PROPAGATE this fix
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides support for the /WATCH command */
17
18 /* This module has been refactored to provide a very efficient (in terms of cpu time)
19  * implementation of /WATCH.
20  *
21  * To improve the efficiency of watch, many lists are kept. The first primary list is
22  * a hash_map of who's being watched by who. For example:
23  *
24  * KEY: Brain   --->  Watched by:  Boo, w00t, Om
25  * KEY: Boo     --->  Watched by:  Brain, w00t
26  * 
27  * This is used when we want to tell all the users that are watching someone that
28  * they are now available or no longer available. For example, if the hash was
29  * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
30  * and Om by reading their 'watched by' list. When this occurs, their online status
31  * in each of these users lists (see below) is also updated.
32  *
33  * Each user also has a seperate (smaller) map attached to their userrec whilst they
34  * have any watch entries, which is managed by class Extensible. When they add or remove
35  * a watch entry from their list, it is inserted here, as well as the main list being
36  * maintained. This map also contains the user's online status. For users that are
37  * offline, the key points at an empty string, and for users that are online, the key
38  * points at a string containing "users-ident users-host users-signon-time". This is
39  * stored in this manner so that we don't have to FindUser() to fetch this info, the
40  * users signon can populate the field for us.
41  *
42  * For example, going again on the example above, this would be w00t's watchlist:
43  *
44  * KEY: Boo    --->  Status: "Boo brains.sexy.babe 535342348"
45  * KEY: Brain  --->  Status: ""
46  *
47  * In this list we can see that Boo is online, and Brain is offline. We can then
48  * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
49  * of the above two data structures, with minimum CPU penalty for doing so.
50  *
51  * In short, the least efficient this ever gets is O(n), and thats only because
52  * there are parts that *must* loop (e.g. telling all users that are watching a
53  * nick that the user online), however this is a *major* improvement over the
54  * 1.0 implementation, which in places had O(n^n) and worse in it, because this
55  * implementation scales based upon the sizes of the watch entries, whereas the
56  * old system would scale (or not as the case may be) according to the total number
57  * of users using WATCH.
58  */
59
60 /*
61  * Before you start screaming, this definition is only used here, so moving it to a header is pointless.
62  * Yes, it's horrid. Blame cl for being different. -- w00t
63  */
64 #ifdef WINDOWS
65 typedef nspace::hash_map<irc::string, std::deque<userrec*>, nspace::hash_compare<irc::string, less<irc::string> > > watchentries;
66 #else
67 typedef nspace::hash_map<irc::string, std::deque<userrec*>, nspace::hash<irc::string> > watchentries;
68 #endif
69 typedef std::map<irc::string, std::string> watchlist;
70
71 /* Who's watching each nickname.
72  * NOTE: We do NOT iterate this to display a user's WATCH list!
73  * See the comments above!
74  */
75 watchentries* whos_watching_me;
76
77 /** Handle /WATCH
78  */
79 class cmd_watch : public command_t
80 {
81         unsigned int& MAX_WATCH;
82  public:
83         CmdResult remove_watch(userrec* user, const char* nick)
84         {
85                 // removing an item from the list
86                 if (!ServerInstance->IsNick(nick))
87                 {
88                         user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
89                         return CMD_FAILURE;
90                 }
91
92                 watchlist* wl;
93                 if (user->GetExt("watchlist", wl))
94                 {
95                         /* Yup, is on my list */
96                         watchlist::iterator n = wl->find(nick);
97                         if (n != wl->end())
98                         {
99                                 if (!n->second.empty())
100                                         user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
101                                 else
102                                         user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
103
104                                 wl->erase(n);
105                         }
106
107                         if (!wl->size())
108                         {
109                                 user->Shrink("watchlist");
110                                 delete wl;
111                         }
112
113                         watchentries::iterator x = whos_watching_me->find(nick);
114                         if (x != whos_watching_me->end())
115                         {
116                                 /* People are watching this user, am i one of them? */
117                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
118                                 if (n != x->second.end())
119                                         /* I'm no longer watching you... */
120                                         x->second.erase(n);
121
122                                 if (!x->second.size())
123                                         whos_watching_me->erase(nick);
124                         }
125                 }
126
127                 /* This might seem confusing, but we return CMD_FAILURE
128                  * to indicate that this message shouldnt be routed across
129                  * the network to other linked servers.
130                  */
131                 return CMD_FAILURE;
132         }
133
134         CmdResult add_watch(userrec* user, const char* nick)
135         {
136                 if (!ServerInstance->IsNick(nick))
137                 {
138                         user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
139                         return CMD_FAILURE;
140                 }
141
142                 watchlist* wl;
143                 if (!user->GetExt("watchlist", wl))
144                 {
145                         wl = new watchlist();
146                         user->Extend("watchlist", wl);
147                 }
148
149                 if (wl->size() == MAX_WATCH)
150                 {
151                         user->WriteServ("512 %s %s :Too many WATCH entries", user->nick, nick);
152                         return CMD_FAILURE;
153                 }
154
155                 watchlist::iterator n = wl->find(nick);
156                 if (n == wl->end())
157                 {
158                         /* Don't already have the user on my watch list, proceed */
159                         watchentries::iterator x = whos_watching_me->find(nick);
160                         if (x != whos_watching_me->end())
161                         {
162                                 /* People are watching this user, add myself */
163                                 x->second.push_back(user);
164                         }
165                         else
166                         {
167                                 std::deque<userrec*> newlist;
168                                 newlist.push_back(user);
169                                 (*(whos_watching_me))[nick] = newlist;
170                         }
171
172                         userrec* target = ServerInstance->FindNick(nick);
173                         if (target)
174                         {
175                                 if (target->Visibility && !target->Visibility->VisibleTo(user))
176                                 {
177                                         (*wl)[nick] = "";
178                                         user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
179                                         return CMD_FAILURE;
180                                 }
181
182                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
183                                 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
184                         }
185                         else
186                         {
187                                 (*wl)[nick] = "";
188                                 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
189                         }
190                 }
191
192                 return CMD_FAILURE;
193         }
194
195         cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
196         {
197                 this->source = "m_watch.so";
198                 syntax = "[C|L|S]|[+|-<nick>]";
199                 TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
200         }
201
202         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
203         {
204                 if (!pcnt)
205                 {
206                         watchlist* wl;
207                         if (user->GetExt("watchlist", wl))
208                         {
209                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
210                                 {
211                                         if (!q->second.empty())
212                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
213                                 }
214                         }
215                         user->WriteServ("607 %s :End of WATCH list",user->nick);
216                 }
217                 else if (pcnt > 0)
218                 {
219                         for (int x = 0; x < pcnt; x++)
220                         {
221                                 const char *nick = parameters[x];
222                                 if (!strcasecmp(nick,"C"))
223                                 {
224                                         // watch clear
225                                         watchlist* wl;
226                                         if (user->GetExt("watchlist", wl))
227                                         {
228                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
229                                                 {
230                                                         watchentries::iterator x = whos_watching_me->find(i->first);
231                                                         if (x != whos_watching_me->end())
232                                                         {
233                                                                 /* People are watching this user, am i one of them? */
234                                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
235                                                                 if (n != x->second.end())
236                                                                         /* I'm no longer watching you... */
237                                                                         x->second.erase(n);
238
239                                                                 if (!x->second.size())
240                                                                         whos_watching_me->erase(user->nick);
241                                                         }
242                                                 }
243
244                                                 delete wl;
245                                                 user->Shrink("watchlist");
246                                         }
247                                 }
248                                 else if (!strcasecmp(nick,"L"))
249                                 {
250                                         watchlist* wl;
251                                         if (user->GetExt("watchlist", wl))
252                                         {
253                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
254                                                 {
255                                                         if (!q->second.empty())
256                                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
257                                                         else
258                                                                 user->WriteServ("605 %s %s * * 0 :is offline", user->nick, q->first.c_str());
259                                                 }
260                                         }
261                                         user->WriteServ("607 %s :End of WATCH list",user->nick);
262                                 }
263                                 else if (!strcasecmp(nick,"S"))
264                                 {
265                                         watchlist* wl;
266                                         int you_have = 0;
267                                         int youre_on = 0;
268                                         std::string list;
269
270                                         if (user->GetExt("watchlist", wl))
271                                         {
272                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
273                                                         list.append(q->first.c_str()).append(" ");
274                                                 you_have = wl->size();
275                                         }
276
277                                         watchentries::iterator x = whos_watching_me->find(user->nick);
278                                         if (x != whos_watching_me->end())
279                                                 youre_on = x->second.size();
280
281                                         user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
282                                         user->WriteServ("606 %s :%s",user->nick, list.c_str());
283                                         user->WriteServ("607 %s :End of WATCH S",user->nick);
284                                 }
285                                 else if (nick[0] == '-')
286                                 {
287                                         nick++;
288                                         remove_watch(user, nick);
289                                 }
290                                 else if (nick[0] == '+')
291                                 {
292                                         nick++;
293                                         add_watch(user, nick);
294                                 }
295                         }
296                 }
297                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
298                 return CMD_FAILURE;
299         }
300 };
301
302 class Modulewatch : public Module
303 {
304         cmd_watch* mycommand;
305         unsigned int maxwatch;
306  public:
307
308         Modulewatch(InspIRCd* Me)
309                 : Module(Me), maxwatch(32)
310         {
311                 OnRehash(NULL, "");
312                 whos_watching_me = new watchentries();
313                 mycommand = new cmd_watch(ServerInstance, maxwatch);
314                 ServerInstance->AddCommand(mycommand);
315         }
316
317         virtual void OnRehash(userrec* user, const std::string &parameter)
318         {
319                 ConfigReader Conf(ServerInstance);
320                 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
321                 if (!maxwatch)
322                         maxwatch = 32;
323         }
324
325         void Implements(char* List)
326         {
327                 List[I_OnRehash] = List[I_OnGarbageCollect] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
328         }
329
330         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
331         {
332                 watchentries::iterator x = whos_watching_me->find(user->nick);
333                 if (x != whos_watching_me->end())
334                 {
335                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
336                         {
337                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
338                                         (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
339
340                                 watchlist* wl;
341                                 if ((*n)->GetExt("watchlist", wl))
342                                         /* We were on somebody's notify list, set ourselves offline */
343                                         (*wl)[user->nick] = "";
344                         }
345                 }
346
347                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
348                 watchlist* wl;
349                 if (user->GetExt("watchlist", wl))
350                 {
351                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
352                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
353                         {
354                                 watchentries::iterator x = whos_watching_me->find(i->first);
355                                 if (x != whos_watching_me->end())
356                                 {
357                                                 /* People are watching this user, am i one of them? */
358                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
359                                                 if (n != x->second.end())
360                                                         /* I'm no longer watching you... */
361                                                         x->second.erase(n);
362         
363                                                 if (!x->second.size())
364                                                         whos_watching_me->erase(user->nick);
365                                 }
366                         }
367
368                         /* User's quitting, we're done with this. */
369                         delete wl;
370                 }
371         }
372
373         virtual void OnGarbageCollect()
374         {
375                 watchentries* old_watch = whos_watching_me;
376                 whos_watching_me = new watchentries();
377
378                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
379                         whos_watching_me->insert(*n);
380
381                 delete old_watch;
382         }
383
384         virtual void OnCleanup(int target_type, void* item)
385         {
386                 if (target_type == TYPE_USER)
387                 {
388                         watchlist* wl;
389                         userrec* user = (userrec*)item;
390
391                         if (user->GetExt("watchlist", wl))
392                         {
393                                 user->Shrink("watchlist");
394                                 delete wl;
395                         }
396                 }
397         }
398
399         virtual void OnPostConnect(userrec* user)
400         {
401                 watchentries::iterator x = whos_watching_me->find(user->nick);
402                 if (x != whos_watching_me->end())
403                 {
404                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
405                         {
406                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
407                                         (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
408
409                                 watchlist* wl;
410                                 if ((*n)->GetExt("watchlist", wl))
411                                         /* We were on somebody's notify list, set ourselves online */
412                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
413                         }
414                 }
415         }
416
417         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
418         {
419                 watchentries::iterator new_offline = whos_watching_me->find(assign(oldnick));
420                 watchentries::iterator new_online = whos_watching_me->find(user->nick);
421
422                 if (new_offline != whos_watching_me->end())
423                 {
424                         for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
425                         {
426                                 watchlist* wl;
427                                 if ((*n)->GetExt("watchlist", wl))
428                                 {
429                                         if (!user->Visibility || user->Visibility->VisibleTo(user))
430                                                 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, user->age);
431                                         (*wl)[oldnick.c_str()] = "";
432                                 }
433                         }
434                 }
435
436                 if (new_online != whos_watching_me->end())
437                 {
438                         for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
439                         {
440                                 watchlist* wl;
441                                 if ((*n)->GetExt("watchlist", wl))
442                                 {
443                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
444                                         if (!user->Visibility || user->Visibility->VisibleTo(user))
445                                                 (*n)->WriteServ("600 %s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str());
446                                 }
447                         }
448                 }
449         }       
450
451         virtual void On005Numeric(std::string &output)
452         {
453                 // we don't really have a limit...
454                 output = output + " WATCH=" + ConvToStr(maxwatch);
455         }
456         
457         virtual ~Modulewatch()
458         {
459                 delete whos_watching_me;
460         }
461         
462         virtual Version GetVersion()
463         {
464                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
465         }
466 };
467
468 MODULE_INIT(Modulewatch)
469