]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
Untested! New m_watch that should be hundreds of times faster (im not joking either)
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *       E-mail:
7  *<brain@chatspike.net>
8  *        <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *    the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25 #include "hashcomp.h"
26 #include "inspircd.h"
27
28 /* $ModDesc: Provides support for the /watch command */
29
30 /*       nickname             list of users watching the nick               */
31 typedef std::map<irc::string, std::deque<userrec*> >                        watchentries;
32
33 /*       nickname             'ident host signon', or empty if not online   */
34 typedef std::map<irc::string, std::string>                                  watchlist;
35
36 /* Whos watching each nickname */
37 watchentries whos_watching_me;
38
39 /** Handle /WATCH
40  */
41 class cmd_watch : public command_t
42 {
43         unsigned int& MAX_WATCH;
44  public:
45         CmdResult remove_watch(userrec* user, const char* nick)
46         {
47                 // removing an item from the list
48                 if (!ServerInstance->IsNick(nick))
49                 {
50                         user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
51                         return CMD_FAILURE;
52                 }
53
54                 watchlist* wl;
55                 if (user->GetExt("watchlist", wl))
56                 {
57                         /* Yup, is on my list */
58                         watchlist::iterator n = wl->find(nick);
59                         if (n != wl->end())
60                         {
61                                 if (!n->second.empty())
62                                         user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
63                                 else
64                                         user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
65
66                                 wl->erase(n);
67                         }
68
69                         if (!wl->size())
70                         {
71                                 user->Shrink("watchlist");
72                                 delete wl;
73                         }
74
75                         watchentries::iterator x = whos_watching_me.find(nick);
76                         if (x != whos_watching_me.end())
77                         {
78                                 /* People are watching this user, am i one of them? */
79                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
80                                 if (n != x->second.end())
81                                         /* I'm no longer watching you... */
82                                         x->second.erase(n);
83
84                                 if (!x->second.size())
85                                         whos_watching_me.erase(nick);
86                         }
87                 }
88
89                 return CMD_SUCCESS;
90         }
91
92         CmdResult add_watch(userrec* user, const char* nick)
93         {
94                 if (!ServerInstance->IsNick(nick))
95                 {
96                         user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
97                         return CMD_FAILURE;
98                 }
99
100                 watchlist* wl;
101                 if (!user->GetExt("watchlist", wl))
102                 {
103                         wl = new watchlist();
104                         user->Extend("watchlist", wl);
105                 }
106
107                 if (wl->size() == MAX_WATCH)
108                 {
109                         user->WriteServ("942 %s %s :Too many WATCH entries", user->nick, nick);
110                         return CMD_FAILURE;
111                 }
112
113                 watchlist::iterator n = wl->find(nick);
114                 if (n == wl->end())
115                 {
116                         /* Don't already have the user on my watch list, proceed */
117                         watchentries::iterator x = whos_watching_me.find(nick);
118                         if (x != whos_watching_me.end())
119                         {
120                                 /* People are watching this user, add myself */
121                                 x->second.push_back(user);
122                         }
123                         else
124                         {
125                                 std::deque<userrec*> newlist;
126                                 newlist.push_back(user);
127                                 whos_watching_me[nick] = newlist;
128                         }
129
130                         userrec* target = ServerInstance->FindNick(nick);
131                         if (target)
132                         {
133                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
134                                 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
135                         }
136                         else
137                         {
138                                 (*wl)[nick] = "";
139                                 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
140                         }
141                 }
142
143                 return CMD_SUCCESS;
144         }
145
146         cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
147         {
148                 this->source = "m_watch.so";
149                 syntax = "[C|L|S]|[+|-<nick>]";
150         }
151
152         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
153         {
154                 if (!pcnt)
155                 {
156                         watchlist* wl;
157                         if (user->GetExt("watchlist", wl))
158                         {
159                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
160                                 {
161                                         if (!q->second.empty())
162                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
163                                 }
164                         }
165                         user->WriteServ("607 %s :End of WATCH list",user->nick);
166                 }
167                 else if (pcnt > 0)
168                 {
169                         for (int x = 0; x < pcnt; x++)
170                         {
171                                 const char *nick = parameters[x];
172                                 if (!strcasecmp(nick,"C"))
173                                 {
174                                         // watch clear
175                                         watchlist* wl;
176                                         if (user->GetExt("watchlist", wl))
177                                         {
178                                                 delete wl;
179                                                 user->Shrink("watchlist");
180                                         }
181                                 }
182                                 else if (!strcasecmp(nick,"L"))
183                                 {
184                                         watchlist* wl;
185                                         if (user->GetExt("watchlist", wl))
186                                         {
187                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
188                                                 {
189                                                         if (!q->second.empty())
190                                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
191                                                 }
192                                         }
193                                         user->WriteServ("607 %s :End of WATCH list",user->nick);
194                                 }
195                                 else if (!strcasecmp(nick,"S"))
196                                 {
197                                         watchlist* wl;
198                                         int you_have = 0;
199                                         int youre_on = 0;
200                                         std::string list;
201
202                                         if (user->GetExt("watchlist", wl))
203                                         {
204                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
205                                                         if (!q->second.empty())
206                                                                 list.append(q->first.c_str()).append(" ");
207                                                 you_have = wl->size();
208                                         }
209
210                                         watchentries::iterator x = whos_watching_me.find(user->nick);
211                                         if (x != whos_watching_me.end())
212                                                 youre_on = x->second.size();
213
214                                         user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
215                                         if (!list.empty())
216                                                 user->WriteServ("606 %s :%s",user->nick, list.c_str());
217                                         user->WriteServ("607 %s :End of WATCH S",user->nick);
218                                 }
219                                 else if (nick[0] == '-')
220                                 {
221                                         nick++;
222                                         return remove_watch(user, nick);
223                                 }
224                                 else if (nick[0] == '+')
225                                 {
226                                         nick++;
227                                         return add_watch(user, nick);
228                                 }
229                         }
230                 }
231                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
232                 return CMD_FAILURE;
233         }
234 };
235
236 class Modulewatch : public Module
237 {
238         cmd_watch* mycommand;
239         unsigned int maxwatch;
240  public:
241
242         Modulewatch(InspIRCd* Me)
243                 : Module::Module(Me), maxwatch(32)
244         {
245                 mycommand = new cmd_watch(ServerInstance, maxwatch);
246                 ServerInstance->AddCommand(mycommand);
247         }
248
249         void Implements(char* List)
250         {
251                 List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
252         }
253
254         virtual void OnUserQuit(userrec* user, const std::string &reason)
255         {
256                 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
257                 watchentries::iterator x = whos_watching_me.find(user->nick);
258                 if (x != whos_watching_me.end())
259                 {
260                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
261                         {
262                                 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
263                                 watchlist* wl;
264                                 if ((*n)->GetExt("watchlist", wl))
265                                         /* We were on somebody's notify list, set ourselves offline */
266                                         (*wl)[user->nick] = "";
267                         }
268                 }
269
270                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
271                 watchlist* wl;
272                 if (user->GetExt("watchlist", wl))
273                 {
274                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
275                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
276                         {
277                                 watchentries::iterator x = whos_watching_me.find(i->first);
278                                 if (x != whos_watching_me.end())
279                                 {
280                                                 /* People are watching this user, am i one of them? */
281                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
282                                                 if (n != x->second.end())
283                                                         /* I'm no longer watching you... */
284                                                         x->second.erase(n);
285         
286                                                 if (!x->second.size())
287                                                         whos_watching_me.erase(user->nick);
288                                 }
289                         }
290                 }
291         }
292
293         virtual void OnPostConnect(userrec* user)
294         {
295                 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
296                 watchentries::iterator x = whos_watching_me.find(user->nick);
297                 if (x != whos_watching_me.end())
298                 {
299                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
300                         {
301                                 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
302                                 watchlist* wl;
303                                 if ((*n)->GetExt("watchlist", wl))
304                                         /* We were on somebody's notify list, set ourselves online */
305                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
306                         }
307                 }
308         }
309
310         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
311         {
312                 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
313
314                 watchentries::iterator new_online = whos_watching_me.find(user->nick);
315                 watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick));
316
317                 if (new_online != whos_watching_me.end())
318                 {
319                         for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
320                         {
321                                 watchlist* wl;
322                                 if ((*n)->GetExt("watchlist", wl))
323                                 {
324                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
325                                         (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, (*wl)[user->nick].c_str());
326                                 }
327                         }
328                 }
329
330                 if (new_offline != whos_watching_me.end())
331                 {
332                         for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
333                         {
334                                 watchlist* wl;
335                                 if ((*n)->GetExt("watchlist", wl))
336                                 {
337                                         (*n)->WriteServ("601 %s %s %s :went offline", (*n)->nick, (*wl)[user->nick].c_str());
338                                         (*wl)[user->nick] = "";
339                                 }
340                         }
341                 }
342         }       
343
344         virtual void On005Numeric(std::string &output)
345         {
346                 // we don't really have a limit...
347                 output = output + " WATCH=32";
348         }
349         
350         virtual ~Modulewatch()
351         {
352         }
353         
354         virtual Version GetVersion()
355         {
356                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
357         }
358 };
359
360
361 class ModulewatchFactory : public ModuleFactory
362 {
363  public:
364         ModulewatchFactory()
365         {
366         }
367         
368         ~ModulewatchFactory()
369         {
370         }
371         
372         virtual Module * CreateModule(InspIRCd* Me)
373         {
374                 return new Modulewatch(Me);
375         }
376         
377 };
378
379
380 extern "C" void * init_module( void )
381 {
382         return new ModulewatchFactory;
383 }
384