]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
d502168cfa5f1ffbfa7f1022a5366e4a96ec1db6
[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                 /* This might seem confusing, but we return CMD_FAILURE
90                  * to indicate that this message shouldnt be routed across
91                  * the network to other linked servers.
92                  */
93                 return CMD_FAILURE;
94         }
95
96         CmdResult add_watch(userrec* user, const char* nick)
97         {
98                 if (!ServerInstance->IsNick(nick))
99                 {
100                         user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
101                         return CMD_FAILURE;
102                 }
103
104                 watchlist* wl;
105                 if (!user->GetExt("watchlist", wl))
106                 {
107                         ServerInstance->Log(DEBUG,"Allocate new watchlist");
108                         wl = new watchlist();
109                         user->Extend("watchlist", wl);
110                 }
111
112                 if (wl->size() == MAX_WATCH)
113                 {
114                         user->WriteServ("512 %s %s :Too many WATCH entries", user->nick, nick);
115                         return CMD_FAILURE;
116                 }
117
118                 watchlist::iterator n = wl->find(nick);
119                 if (n == wl->end())
120                 {
121                         ServerInstance->Log(DEBUG,"*** Add to WATCH: '%s'", nick);
122                         /* Don't already have the user on my watch list, proceed */
123                         watchentries::iterator x = whos_watching_me.find(nick);
124                         if (x != whos_watching_me.end())
125                         {
126                                 /* People are watching this user, add myself */
127                                 x->second.push_back(user);
128                         }
129                         else
130                         {
131                                 std::deque<userrec*> newlist;
132                                 newlist.push_back(user);
133                                 whos_watching_me[nick] = newlist;
134                         }
135
136                         userrec* target = ServerInstance->FindNick(nick);
137                         if (target)
138                         {
139                                 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
140                                 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
141                         }
142                         else
143                         {
144                                 (*wl)[nick] = "";
145                                 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
146                         }
147                 }
148                 else
149                 {
150                         ServerInstance->Log(DEBUG,"*** WATCH entry '%s' already exists!", nick);
151                 }
152
153                 return CMD_FAILURE;
154         }
155
156         cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
157         {
158                 this->source = "m_watch.so";
159                 syntax = "[C|L|S]|[+|-<nick>]";
160         }
161
162         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
163         {
164                 if (!pcnt)
165                 {
166                         watchlist* wl;
167                         if (user->GetExt("watchlist", wl))
168                         {
169                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
170                                 {
171                                         if (!q->second.empty())
172                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
173                                 }
174                         }
175                         user->WriteServ("607 %s :End of WATCH list",user->nick);
176                 }
177                 else if (pcnt > 0)
178                 {
179                         for (int x = 0; x < pcnt; x++)
180                         {
181                                 const char *nick = parameters[x];
182                                 ServerInstance->Log(DEBUG,"WATCH iterate item '%s'", nick);
183                                 if (!strcasecmp(nick,"C"))
184                                 {
185                                         // watch clear
186                                         watchlist* wl;
187                                         if (user->GetExt("watchlist", wl))
188                                         {
189                                                 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
190                                                 {
191                                                         watchentries::iterator x = whos_watching_me.find(i->first);
192                                                         if (x != whos_watching_me.end())
193                                                         {
194                                                                 /* People are watching this user, am i one of them? */
195                                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
196                                                                 if (n != x->second.end())
197                                                                         /* I'm no longer watching you... */
198                                                                         x->second.erase(n);
199
200                                                                 if (!x->second.size())
201                                                                         whos_watching_me.erase(user->nick);
202                                                         }
203                                                 }
204
205                                                 delete wl;
206                                                 user->Shrink("watchlist");
207                                         }
208                                 }
209                                 else if (!strcasecmp(nick,"L"))
210                                 {
211                                         watchlist* wl;
212                                         if (user->GetExt("watchlist", wl))
213                                         {
214                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
215                                                 {
216                                                         if (!q->second.empty())
217                                                                 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
218                                                         else
219                                                                 user->WriteServ("605 %s %s * * 0 :is offline", user->nick, q->first.c_str());
220                                                 }
221                                         }
222                                         user->WriteServ("607 %s :End of WATCH list",user->nick);
223                                 }
224                                 else if (!strcasecmp(nick,"S"))
225                                 {
226                                         watchlist* wl;
227                                         int you_have = 0;
228                                         int youre_on = 0;
229                                         std::string list;
230
231                                         if (user->GetExt("watchlist", wl))
232                                         {
233                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
234                                                         list.append(q->first.c_str()).append(" ");
235                                                 you_have = wl->size();
236                                         }
237
238                                         watchentries::iterator x = whos_watching_me.find(user->nick);
239                                         if (x != whos_watching_me.end())
240                                                 youre_on = x->second.size();
241
242                                         user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
243                                         user->WriteServ("606 %s :%s",user->nick, list.c_str());
244                                         user->WriteServ("607 %s :End of WATCH S",user->nick);
245                                 }
246                                 else if (nick[0] == '-')
247                                 {
248                                         nick++;
249                                         remove_watch(user, nick);
250                                 }
251                                 else if (nick[0] == '+')
252                                 {
253                                         nick++;
254                                         add_watch(user, nick);
255                                 }
256                         }
257                 }
258                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
259                 return CMD_FAILURE;
260         }
261 };
262
263 class Modulewatch : public Module
264 {
265         cmd_watch* mycommand;
266         unsigned int maxwatch;
267  public:
268
269         Modulewatch(InspIRCd* Me)
270                 : Module::Module(Me), maxwatch(32)
271         {
272                 mycommand = new cmd_watch(ServerInstance, maxwatch);
273                 ServerInstance->AddCommand(mycommand);
274         }
275
276         void Implements(char* List)
277         {
278                 List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
279         }
280
281         virtual void OnUserQuit(userrec* user, const std::string &reason)
282         {
283                 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
284                 watchentries::iterator x = whos_watching_me.find(user->nick);
285                 if (x != whos_watching_me.end())
286                 {
287                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
288                         {
289                                 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
290                                 watchlist* wl;
291                                 if ((*n)->GetExt("watchlist", wl))
292                                         /* We were on somebody's notify list, set ourselves offline */
293                                         (*wl)[user->nick] = "";
294                         }
295                 }
296
297                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
298                 watchlist* wl;
299                 if (user->GetExt("watchlist", wl))
300                 {
301                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
302                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
303                         {
304                                 watchentries::iterator x = whos_watching_me.find(i->first);
305                                 if (x != whos_watching_me.end())
306                                 {
307                                                 /* People are watching this user, am i one of them? */
308                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
309                                                 if (n != x->second.end())
310                                                         /* I'm no longer watching you... */
311                                                         x->second.erase(n);
312         
313                                                 if (!x->second.size())
314                                                         whos_watching_me.erase(user->nick);
315                                 }
316                         }
317                 }
318         }
319
320         virtual void OnPostConnect(userrec* user)
321         {
322                 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
323                 watchentries::iterator x = whos_watching_me.find(user->nick);
324                 if (x != whos_watching_me.end())
325                 {
326                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
327                         {
328                                 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
329                                 watchlist* wl;
330                                 if ((*n)->GetExt("watchlist", wl))
331                                         /* We were on somebody's notify list, set ourselves online */
332                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
333                         }
334                 }
335         }
336
337         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
338         {
339                 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
340
341                 watchentries::iterator new_online = whos_watching_me.find(user->nick);
342                 watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick));
343
344                 if (new_online != whos_watching_me.end())
345                 {
346                         for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
347                         {
348                                 watchlist* wl;
349                                 if ((*n)->GetExt("watchlist", wl))
350                                 {
351                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
352                                         (*n)->WriteServ("600 %s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str());
353                                 }
354                         }
355                 }
356
357                 if (new_offline != whos_watching_me.end())
358                 {
359                         for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
360                         {
361                                 watchlist* wl;
362                                 if ((*n)->GetExt("watchlist", wl))
363                                 {
364                                         (*n)->WriteServ("601 %s %s %s %s %s %lu :went offline", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
365                                         (*wl)[user->nick] = "";
366                                 }
367                         }
368                 }
369         }       
370
371         virtual void On005Numeric(std::string &output)
372         {
373                 // we don't really have a limit...
374                 output = output + " WATCH=32";
375         }
376         
377         virtual ~Modulewatch()
378         {
379         }
380         
381         virtual Version GetVersion()
382         {
383                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
384         }
385 };
386
387
388 class ModulewatchFactory : public ModuleFactory
389 {
390  public:
391         ModulewatchFactory()
392         {
393         }
394         
395         ~ModulewatchFactory()
396         {
397         }
398         
399         virtual Module * CreateModule(InspIRCd* Me)
400         {
401                 return new Modulewatch(Me);
402         }
403         
404 };
405
406
407 extern "C" void * init_module( void )
408 {
409         return new ModulewatchFactory;
410 }
411