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