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