]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
Refactor port binding, warning not yet tested fully
[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 typedef nspace::hash_map<irc::string, std::deque<userrec*>, nspace::hash<irc::string> >     watchentries;
65 typedef std::map<irc::string, std::string>                                                  watchlist;
66
67 /* Who's watching each nickname.
68  * NOTE: We do NOT iterate this to display a user's WATCH list!
69  * See the comments above!
70  */
71 watchentries* whos_watching_me;
72
73 /** Handle /WATCH
74  */
75 class cmd_watch : public command_t
76 {
77         unsigned int& MAX_WATCH;
78  public:
79         CmdResult remove_watch(userrec* user, const char* nick)
80         {
81                 // removing an item from the list
82                 if (!ServerInstance->IsNick(nick))
83                 {
84                         user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
85                         return CMD_FAILURE;
86                 }
87
88                 watchlist* wl;
89                 if (user->GetExt("watchlist", wl))
90                 {
91                         /* Yup, is on my list */
92                         watchlist::iterator n = wl->find(nick);
93                         if (n != wl->end())
94                         {
95                                 if (!n->second.empty())
96                                         user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
97                                 else
98                                         user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
99
100                                 wl->erase(n);
101                         }
102
103                         if (!wl->size())
104                         {
105                                 user->Shrink("watchlist");
106                                 delete wl;
107                         }
108
109                         watchentries::iterator x = whos_watching_me->find(nick);
110                         if (x != whos_watching_me->end())
111                         {
112                                 /* People are watching this user, am i one of them? */
113                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
114                                 if (n != x->second.end())
115                                         /* I'm no longer watching you... */
116                                         x->second.erase(n);
117
118                                 if (!x->second.size())
119                                         whos_watching_me->erase(nick);
120                         }
121                 }
122
123                 /* This might seem confusing, but we return CMD_FAILURE
124                  * to indicate that this message shouldnt be routed across
125                  * the network to other linked servers.
126                  */
127                 return CMD_FAILURE;
128         }
129
130         CmdResult add_watch(userrec* user, const char* nick)
131         {
132                 if (!ServerInstance->IsNick(nick))
133                 {
134                         user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
135                         return CMD_FAILURE;
136                 }
137
138                 watchlist* wl;
139                 if (!user->GetExt("watchlist", wl))
140                 {
141                         wl = new watchlist();
142                         user->Extend("watchlist", wl);
143                 }
144
145                 if (wl->size() == MAX_WATCH)
146                 {
147                         user->WriteServ("512 %s %s :Too many WATCH entries", user->nick, nick);
148                         return CMD_FAILURE;
149                 }
150
151                 watchlist::iterator n = wl->find(nick);
152                 if (n == wl->end())
153                 {
154                         /* Don't already have the user on my watch list, proceed */
155                         watchentries::iterator x = whos_watching_me->find(nick);
156                         if (x != whos_watching_me->end())
157                         {
158                                 /* People are watching this user, add myself */
159                                 x->second.push_back(user);
160                         }
161                         else
162                         {
163                                 std::deque<userrec*> newlist;
164                                 newlist.push_back(user);
165                                 (*(whos_watching_me))[nick] = newlist;
166                         }
167
168                         userrec* target = ServerInstance->FindNick(nick);
169                         if (target)
170                         {
171                                 if (target->Visibility && !target->Visibility->VisibleTo(user))
172                                 {
173                                         (*wl)[nick] = "";
174                                         user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
175                                         return CMD_FAILURE;
176                                 }
177
178                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
179                                 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
180                         }
181                         else
182                         {
183                                 (*wl)[nick] = "";
184                                 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
185                         }
186                 }
187
188                 return CMD_FAILURE;
189         }
190
191         cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
192         {
193                 this->source = "m_watch.so";
194                 syntax = "[C|L|S]|[+|-<nick>]";
195         }
196
197         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
198         {
199                 if (!pcnt)
200                 {
201                         watchlist* wl;
202                         if (user->GetExt("watchlist", wl))
203                         {
204                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
205                                 {
206                                         if (!q->second.empty())
207                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
208                                 }
209                         }
210                         user->WriteServ("607 %s :End of WATCH list",user->nick);
211                 }
212                 else if (pcnt > 0)
213                 {
214                         for (int x = 0; x < pcnt; x++)
215                         {
216                                 const char *nick = parameters[x];
217                                 if (!strcasecmp(nick,"C"))
218                                 {
219                                         // watch clear
220                                         watchlist* wl;
221                                         if (user->GetExt("watchlist", wl))
222                                         {
223                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
224                                                 {
225                                                         watchentries::iterator x = whos_watching_me->find(i->first);
226                                                         if (x != whos_watching_me->end())
227                                                         {
228                                                                 /* People are watching this user, am i one of them? */
229                                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
230                                                                 if (n != x->second.end())
231                                                                         /* I'm no longer watching you... */
232                                                                         x->second.erase(n);
233
234                                                                 if (!x->second.size())
235                                                                         whos_watching_me->erase(user->nick);
236                                                         }
237                                                 }
238
239                                                 delete wl;
240                                                 user->Shrink("watchlist");
241                                         }
242                                 }
243                                 else if (!strcasecmp(nick,"L"))
244                                 {
245                                         watchlist* wl;
246                                         if (user->GetExt("watchlist", wl))
247                                         {
248                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
249                                                 {
250                                                         if (!q->second.empty())
251                                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
252                                                         else
253                                                                 user->WriteServ("605 %s %s * * 0 :is offline", user->nick, q->first.c_str());
254                                                 }
255                                         }
256                                         user->WriteServ("607 %s :End of WATCH list",user->nick);
257                                 }
258                                 else if (!strcasecmp(nick,"S"))
259                                 {
260                                         watchlist* wl;
261                                         int you_have = 0;
262                                         int youre_on = 0;
263                                         std::string list;
264
265                                         if (user->GetExt("watchlist", wl))
266                                         {
267                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
268                                                         list.append(q->first.c_str()).append(" ");
269                                                 you_have = wl->size();
270                                         }
271
272                                         watchentries::iterator x = whos_watching_me->find(user->nick);
273                                         if (x != whos_watching_me->end())
274                                                 youre_on = x->second.size();
275
276                                         user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
277                                         user->WriteServ("606 %s :%s",user->nick, list.c_str());
278                                         user->WriteServ("607 %s :End of WATCH S",user->nick);
279                                 }
280                                 else if (nick[0] == '-')
281                                 {
282                                         nick++;
283                                         remove_watch(user, nick);
284                                 }
285                                 else if (nick[0] == '+')
286                                 {
287                                         nick++;
288                                         add_watch(user, nick);
289                                 }
290                         }
291                 }
292                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
293                 return CMD_FAILURE;
294         }
295 };
296
297 class Modulewatch : public Module
298 {
299         cmd_watch* mycommand;
300         unsigned int maxwatch;
301  public:
302
303         Modulewatch(InspIRCd* Me)
304                 : Module::Module(Me), maxwatch(32)
305         {
306                 OnRehash(NULL, "");
307                 whos_watching_me = new watchentries();
308                 mycommand = new cmd_watch(ServerInstance, maxwatch);
309                 ServerInstance->AddCommand(mycommand);
310         }
311
312         virtual void OnRehash(userrec* user, const std::string &parameter)
313         {
314                 ConfigReader Conf(ServerInstance);
315                 maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
316                 if (!maxwatch)
317                         maxwatch = 32;
318         }
319
320         void Implements(char* List)
321         {
322                 List[I_OnRehash] = List[I_OnGarbageCollect] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
323         }
324
325         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
326         {
327                 watchentries::iterator x = whos_watching_me->find(user->nick);
328                 if (x != whos_watching_me->end())
329                 {
330                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
331                         {
332                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
333                                         (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
334
335                                 watchlist* wl;
336                                 if ((*n)->GetExt("watchlist", wl))
337                                         /* We were on somebody's notify list, set ourselves offline */
338                                         (*wl)[user->nick] = "";
339                         }
340                 }
341
342                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
343                 watchlist* wl;
344                 if (user->GetExt("watchlist", wl))
345                 {
346                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
347                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
348                         {
349                                 watchentries::iterator x = whos_watching_me->find(i->first);
350                                 if (x != whos_watching_me->end())
351                                 {
352                                                 /* People are watching this user, am i one of them? */
353                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
354                                                 if (n != x->second.end())
355                                                         /* I'm no longer watching you... */
356                                                         x->second.erase(n);
357         
358                                                 if (!x->second.size())
359                                                         whos_watching_me->erase(user->nick);
360                                 }
361                         }
362
363                         /* User's quitting, we're done with this. */
364                         delete wl;
365                 }
366         }
367
368         virtual void OnGarbageCollect()
369         {
370                 watchentries* old_watch = whos_watching_me;
371                 whos_watching_me = new watchentries();
372
373                 for (watchentries::const_iterator n = old_watch->begin(); n != old_watch->end(); n++)
374                         whos_watching_me->insert(*n);
375
376                 delete old_watch;
377         }
378
379         virtual void OnCleanup(int target_type, void* item)
380         {
381                 if (target_type == TYPE_USER)
382                 {
383                         watchlist* wl;
384                         userrec* user = (userrec*)item;
385
386                         if (user->GetExt("watchlist", wl))
387                         {
388                                 user->Shrink("watchlist");
389                                 delete wl;
390                         }
391                 }
392         }
393
394         virtual void OnPostConnect(userrec* user)
395         {
396                 watchentries::iterator x = whos_watching_me->find(user->nick);
397                 if (x != whos_watching_me->end())
398                 {
399                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
400                         {
401                                 if (!user->Visibility || user->Visibility->VisibleTo(user))
402                                         (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
403
404                                 watchlist* wl;
405                                 if ((*n)->GetExt("watchlist", wl))
406                                         /* We were on somebody's notify list, set ourselves online */
407                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
408                         }
409                 }
410         }
411
412         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
413         {
414                 watchentries::iterator new_online = whos_watching_me->find(user->nick);
415                 watchentries::iterator new_offline = whos_watching_me->find(assign(oldnick));
416
417                 if (new_online != whos_watching_me->end())
418                 {
419                         for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
420                         {
421                                 watchlist* wl;
422                                 if ((*n)->GetExt("watchlist", wl))
423                                 {
424                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
425                                         if (!user->Visibility || user->Visibility->VisibleTo(user))
426                                                 (*n)->WriteServ("600 %s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str());
427                                 }
428                         }
429                 }
430
431                 if (new_offline != whos_watching_me->end())
432                 {
433                         for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
434                         {
435                                 watchlist* wl;
436                                 if ((*n)->GetExt("watchlist", wl))
437                                 {
438                                         if (!user->Visibility || user->Visibility->VisibleTo(user))
439                                                 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, user->age);
440                                         (*wl)[user->nick] = "";
441                                 }
442                         }
443                 }
444         }       
445
446         virtual void On005Numeric(std::string &output)
447         {
448                 // we don't really have a limit...
449                 output = output + " WATCH=" + ConvToStr(maxwatch);
450         }
451         
452         virtual ~Modulewatch()
453         {
454                 delete whos_watching_me;
455         }
456         
457         virtual Version GetVersion()
458         {
459                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
460         }
461 };
462
463
464 class ModulewatchFactory : public ModuleFactory
465 {
466  public:
467         ModulewatchFactory()
468         {
469         }
470         
471         ~ModulewatchFactory()
472         {
473         }
474         
475         virtual Module * CreateModule(InspIRCd* Me)
476         {
477                 return new Modulewatch(Me);
478         }
479         
480 };
481
482
483 extern "C" void * init_module( void )
484 {
485         return new ModulewatchFactory;
486 }
487