]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
58ceaf64527812104f62f40256f2f6b8e1ddb7b4
[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                                                 }
219                                         }
220                                         user->WriteServ("607 %s :End of WATCH list",user->nick);
221                                 }
222                                 else if (!strcasecmp(nick,"S"))
223                                 {
224                                         watchlist* wl;
225                                         int you_have = 0;
226                                         int youre_on = 0;
227                                         std::string list;
228
229                                         if (user->GetExt("watchlist", wl))
230                                         {
231                                                 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
232                                                         if (!q->second.empty())
233                                                                 list.append(q->first.c_str()).append(" ");
234                                                 you_have = wl->size();
235                                         }
236
237                                         watchentries::iterator x = whos_watching_me.find(user->nick);
238                                         if (x != whos_watching_me.end())
239                                                 youre_on = x->second.size();
240
241                                         user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
242                                         user->WriteServ("606 %s :%s",user->nick, list.c_str());
243                                         user->WriteServ("607 %s :End of WATCH S",user->nick);
244                                 }
245                                 else if (nick[0] == '-')
246                                 {
247                                         nick++;
248                                         remove_watch(user, nick);
249                                 }
250                                 else if (nick[0] == '+')
251                                 {
252                                         nick++;
253                                         add_watch(user, nick);
254                                 }
255                         }
256                 }
257                 /* So that spanningtree doesnt pass the WATCH commands to the network! */
258                 return CMD_FAILURE;
259         }
260 };
261
262 class Modulewatch : public Module
263 {
264         cmd_watch* mycommand;
265         unsigned int maxwatch;
266  public:
267
268         Modulewatch(InspIRCd* Me)
269                 : Module::Module(Me), maxwatch(32)
270         {
271                 mycommand = new cmd_watch(ServerInstance, maxwatch);
272                 ServerInstance->AddCommand(mycommand);
273         }
274
275         void Implements(char* List)
276         {
277                 List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
278         }
279
280         virtual void OnUserQuit(userrec* user, const std::string &reason)
281         {
282                 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
283                 watchentries::iterator x = whos_watching_me.find(user->nick);
284                 if (x != whos_watching_me.end())
285                 {
286                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
287                         {
288                                 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
289                                 watchlist* wl;
290                                 if ((*n)->GetExt("watchlist", wl))
291                                         /* We were on somebody's notify list, set ourselves offline */
292                                         (*wl)[user->nick] = "";
293                         }
294                 }
295
296                 /* Now im quitting, if i have a notify list, im no longer watching anyone */
297                 watchlist* wl;
298                 if (user->GetExt("watchlist", wl))
299                 {
300                         /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
301                         for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
302                         {
303                                 watchentries::iterator x = whos_watching_me.find(i->first);
304                                 if (x != whos_watching_me.end())
305                                 {
306                                                 /* People are watching this user, am i one of them? */
307                                                 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
308                                                 if (n != x->second.end())
309                                                         /* I'm no longer watching you... */
310                                                         x->second.erase(n);
311         
312                                                 if (!x->second.size())
313                                                         whos_watching_me.erase(user->nick);
314                                 }
315                         }
316                 }
317         }
318
319         virtual void OnPostConnect(userrec* user)
320         {
321                 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
322                 watchentries::iterator x = whos_watching_me.find(user->nick);
323                 if (x != whos_watching_me.end())
324                 {
325                         for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
326                         {
327                                 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
328                                 watchlist* wl;
329                                 if ((*n)->GetExt("watchlist", wl))
330                                         /* We were on somebody's notify list, set ourselves online */
331                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
332                         }
333                 }
334         }
335
336         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
337         {
338                 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
339
340                 watchentries::iterator new_online = whos_watching_me.find(user->nick);
341                 watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick));
342
343                 if (new_online != whos_watching_me.end())
344                 {
345                         for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
346                         {
347                                 watchlist* wl;
348                                 if ((*n)->GetExt("watchlist", wl))
349                                 {
350                                         (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
351                                         (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, (*wl)[user->nick].c_str());
352                                 }
353                         }
354                 }
355
356                 if (new_offline != whos_watching_me.end())
357                 {
358                         for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
359                         {
360                                 watchlist* wl;
361                                 if ((*n)->GetExt("watchlist", wl))
362                                 {
363                                         (*n)->WriteServ("601 %s %s %s :went offline", (*n)->nick, (*wl)[user->nick].c_str());
364                                         (*wl)[user->nick] = "";
365                                 }
366                         }
367                 }
368         }       
369
370         virtual void On005Numeric(std::string &output)
371         {
372                 // we don't really have a limit...
373                 output = output + " WATCH=32";
374         }
375         
376         virtual ~Modulewatch()
377         {
378         }
379         
380         virtual Version GetVersion()
381         {
382                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
383         }
384 };
385
386
387 class ModulewatchFactory : public ModuleFactory
388 {
389  public:
390         ModulewatchFactory()
391         {
392         }
393         
394         ~ModulewatchFactory()
395         {
396         }
397         
398         virtual Module * CreateModule(InspIRCd* Me)
399         {
400                 return new Modulewatch(Me);
401         }
402         
403 };
404
405
406 extern "C" void * init_module( void )
407 {
408         return new ModulewatchFactory;
409 }
410