]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
ef0f897d268ce61c1e86062fd969525560698cd3
[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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "hashcomp.h"
18 #include "inspircd.h"
19
20 /* $ModDesc: Provides support for the /WATCH command */
21
22 /* This module has been refactored to provide a very efficient (in terms of cpu time)
23  * implementation of /WATCH.
24  *
25  * To improve the efficiency of watch, many lists are kept. The first primary list is
26  * a hash_map of who's being watched by who. For example:
27  *
28  * KEY: Brain   --->  Watched by:  Boo, w00t, Om
29  * KEY: Boo     --->  Watched by:  Brain, w00t
30  * 
31  * This is used when we want to tell all the users that are watching someone that
32  * they are now available or no longer available. For example, if the hash was
33  * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t
34  * and Om by reading their 'watched by' list. When this occurs, their online status
35  * in each of these users lists (see below) is also updated.
36  *
37  * Each user also has a seperate (smaller) map attached to their userrec whilst they
38  * have any watch entries, which is managed by class Extensible. When they add or remove
39  * a watch entry from their list, it is inserted here, as well as the main list being
40  * maintained. This map also contains the user's online status. For users that are
41  * offline, the key points at an empty string, and for users that are online, the key
42  * points at a string containing "users-ident users-host users-signon-time". This is
43  * stored in this manner so that we don't have to FindUser() to fetch this info, the
44  * users signon can populate the field for us.
45  *
46  * For example, going again on the example above, this would be w00t's watchlist:
47  *
48  * KEY: Boo    --->  Status: "Boo brains.sexy.babe 535342348"
49  * KEY: Brain  --->  Status: ""
50  *
51  * In this list we can see that Boo is online, and Brain is offline. We can then
52  * use this list for 'WATCH L', and 'WATCH S' can be implemented as a combination
53  * of the above two data structures, with minimum CPU penalty for doing so.
54  *
55  * In short, the least efficient this ever gets is O(n), and thats only because
56  * there are parts that *must* loop (e.g. telling all users that are watching a
57  * nick that the user online), however this is a *major* improvement over the
58  * 1.0 implementation, which in places had O(n^n) and worse in it, because this
59  * implementation scales based upon the sizes of the watch entries, whereas the
60  * old system would scale (or not as the case may be) according to the total number
61  * of users using WATCH.
62  */
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         }
200
201         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
202         {
203                 if (!pcnt)
204                 {
205                         watchlist* wl;
206                         if (user->GetExt("watchlist", wl))
207                         {
208                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
209                                 {
210                                         if (!q->second.empty())
211                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
212                                 }
213                         }
214                         user->WriteServ("607 %s :End of WATCH list",user->nick);
215                 }
216                 else if (pcnt > 0)
217                 {
218                         for (int x = 0; x < pcnt; x++)
219                         {
220                                 const char *nick = parameters[x];
221                                 if (!strcasecmp(nick,"C"))
222                                 {
223                                         // watch clear
224                                         watchlist* wl;
225                                         if (user->GetExt("watchlist", wl))
226                                         {
227                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
228                                                 {
229                                                         watchentries::iterator x = whos_watching_me->find(i->first);
230                                                         if (x != whos_watching_me->end())
231                                                         {
232                                                                 /* People are watching this user, am i one of them? */
233                                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
234                                                                 if (n != x->second.end())
235                                                                         /* I'm no longer watching you... */
236                                                                         x->second.erase(n);
237
238                                                                 if (!x->second.size())
239                                                                         whos_watching_me->erase(user->nick);
240                                                         }
241                                                 }
242
243                                                 delete wl;
244                                                 user->Shrink("watchlist");
245                                         }
246                                 }
247                                 else if (!strcasecmp(nick,"L"))
248                                 {
249                                         watchlist* wl;
250                                         if (user->GetExt("watchlist", wl))
251                                         {
252                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
253                                                 {
254                                                         if (!q->second.empty())
255                                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
256                                                         else
257                                                                 user->WriteServ("605 %s %s * * 0 :is offline", user->nick, q->first.c_str());
258                                                 }
259                                         }
260                                         user->WriteServ("607 %s :End of WATCH list",user->nick);
261                                 }
262                                 else if (!strcasecmp(nick,"S"))
263                                 {
264                                         watchlist* wl;
265                                         int you_have = 0;
266                                         int youre_on = 0;
267                                         std::string list;
268
269                                         if (user->GetExt("watchlist", wl))
270                                         {
271                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
272                                                         list.append(q->first.c_str()).append(" ");
273                                                 you_have = wl->size();
274                                         }
275
276                                         watchentries::iterator x = whos_watching_me->find(user->nick);
277                                         if (x != whos_watching_me->end())
278                                                 youre_on = x->second.size();
279
280                                         user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
281                                         user->WriteServ("606 %s :%s",user->nick, list.c_str());
282                                         user->WriteServ("607 %s :End of WATCH S",user->nick);
283                                 }
284                                 else if (nick[0] == '-')
285                                 {
286                                         nick++;
287                                         remove_watch(user, nick);
288                                 }
289                                 else if (nick[0] == '+')
290                                 {
291                                         nick++;
292                                         add_watch(user, nick);
293                                 }
294                         }
295                 }
296                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
297                 return CMD_FAILURE;
298         }
299 };
300
301 class Modulewatch : public Module
302 {
303         cmd_watch* mycommand;
304         unsigned int maxwatch;
305  public:
306
307         Modulewatch(InspIRCd* Me)
308                 : Module(Me), maxwatch(32)
309         {
310                 OnRehash(NULL, "");
311                 whos_watching_me = new watchentries();
312                 mycommand = new cmd_watch(ServerInstance, maxwatch);
313                 ServerInstance->AddCommand(mycommand);
314         }
315
316         virtual void OnRehash(userrec* user, const std::string &parameter)
317         {
318                 ConfigReader Conf(ServerInstance);
319                 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
320                 if (!maxwatch)
321                         maxwatch = 32;
322         }
323
324         void Implements(char* List)
325         {
326                 List[I_OnRehash] = List[I_OnGarbageCollect] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
327         }
328
329         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
330         {
331                 watchentries::iterator x = whos_watching_me->find(user->nick);
332                 if (x != whos_watching_me->end())
333                 {
334                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
335                         {
336                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
337                                         (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
338
339                                 watchlist* wl;
340                                 if ((*n)->GetExt("watchlist", wl))
341                                         /* We were on somebody's notify list, set ourselves offline */
342                                         (*wl)[user->nick] = "";
343                         }
344                 }
345
346                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
347                 watchlist* wl;
348                 if (user->GetExt("watchlist", wl))
349                 {
350                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
351                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
352                         {
353                                 watchentries::iterator x = whos_watching_me->find(i->first);
354                                 if (x != whos_watching_me->end())
355                                 {
356                                                 /* People are watching this user, am i one of them? */
357                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
358                                                 if (n != x->second.end())
359                                                         /* I'm no longer watching you... */
360                                                         x->second.erase(n);
361         
362                                                 if (!x->second.size())
363                                                         whos_watching_me->erase(user->nick);
364                                 }
365                         }
366
367                         /* User's quitting, we're done with this. */
368                         delete wl;
369                 }
370         }
371
372         virtual void OnGarbageCollect()
373         {
374                 watchentries* old_watch = whos_watching_me;
375                 whos_watching_me = new watchentries();
376
377                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
378                         whos_watching_me->insert(*n);
379
380                 delete old_watch;
381         }
382
383         virtual void OnCleanup(int target_type, void* item)
384         {
385                 if (target_type == TYPE_USER)
386                 {
387                         watchlist* wl;
388                         userrec* user = (userrec*)item;
389
390                         if (user->GetExt("watchlist", wl))
391                         {
392                                 user->Shrink("watchlist");
393                                 delete wl;
394                         }
395                 }
396         }
397
398         virtual void OnPostConnect(userrec* user)
399         {
400                 watchentries::iterator x = whos_watching_me->find(user->nick);
401                 if (x != whos_watching_me->end())
402                 {
403                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
404                         {
405                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
406                                         (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
407
408                                 watchlist* wl;
409                                 if ((*n)->GetExt("watchlist", wl))
410                                         /* We were on somebody's notify list, set ourselves online */
411                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
412                         }
413                 }
414         }
415
416         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
417         {
418                 watchentries::iterator new_online = whos_watching_me->find(user->nick);
419                 watchentries::iterator new_offline = whos_watching_me->find(assign(oldnick));
420
421                 if (new_online != whos_watching_me->end())
422                 {
423                         for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
424                         {
425                                 watchlist* wl;
426                                 if ((*n)->GetExt("watchlist", wl))
427                                 {
428                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
429                                         if (!user->Visibility || user->Visibility->VisibleTo(user))
430                                                 (*n)->WriteServ("600 %s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str());
431                                 }
432                         }
433                 }
434
435                 if (new_offline != whos_watching_me->end())
436                 {
437                         for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
438                         {
439                                 watchlist* wl;
440                                 if ((*n)->GetExt("watchlist", wl))
441                                 {
442                                         if (!user->Visibility || user->Visibility->VisibleTo(user))
443                                                 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, user->age);
444                                         (*wl)[user->nick] = "";
445                                 }
446                         }
447                 }
448         }       
449
450         virtual void On005Numeric(std::string &output)
451         {
452                 // we don't really have a limit...
453                 output = output + " WATCH=" + ConvToStr(maxwatch);
454         }
455         
456         virtual ~Modulewatch()
457         {
458                 delete whos_watching_me;
459         }
460         
461         virtual Version GetVersion()
462         {
463                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
464         }
465 };
466
467
468 class ModulewatchFactory : public ModuleFactory
469 {
470  public:
471         ModulewatchFactory()
472         {
473         }
474         
475         ~ModulewatchFactory()
476         {
477         }
478         
479         virtual Module * CreateModule(InspIRCd* Me)
480         {
481                 return new Modulewatch(Me);
482         }
483         
484 };
485
486
487 extern "C" DllExport void * init_module( void )
488 {
489         return new ModulewatchFactory;
490 }
491