]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_watch.cpp
Holy christ that was a LOT OF SPACES. TABS, USE THEM, LOVE THEM, APPRECIATE THEM...
[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 "helperfuncs.h"
26 #include "hashcomp.h"
27
28 /* $ModDesc: Provides support for the /watch command */
29
30 static Server *Srv;
31
32 class watchentry
33 {
34  public:
35         userrec* watcher;
36         std::string target;
37 };
38
39 typedef std::vector<watchentry> watchlist;
40 watchlist watches;
41
42 class cmd_watch : public command_t
43 {
44  public:
45         cmd_watch() : command_t("WATCH",0,0)
46         {
47                 this->source = "m_watch.so";
48         }
49
50         void Handle (char **parameters, int pcnt, userrec *user)
51         {
52                 if (!pcnt)
53                 {
54                         for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
55                         {
56                                 if (q->watcher == user)
57                                 {
58                                         userrec* targ = Srv->FindNick(q->target);
59                                         if (targ)
60                                         {
61                                                 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
62                                         }
63                                 }
64                         }
65                         WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
66                 }
67                 else if (pcnt > 0)
68                 {
69                         for (int x = 0; x < pcnt; x++)
70                         {
71                                 char *nick = parameters[x];
72                                 if (!strcasecmp(nick,"C"))
73                                 {
74                                         // watch clear
75                                         bool done = false;
76                                         while (!done)
77                                         {
78                                                 done = true;
79                                                 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
80                                                 {
81                                                         if (q->watcher == user)
82                                                         {
83                                                                 done = false;
84                                                                 watches.erase(q);
85                                                                 break;
86                                                         }
87                                                 }
88                                         }
89                                 }
90                                 else if (!strcasecmp(nick,"L"))
91                                 {
92                                         for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
93                                         {
94                                                 if (q->watcher == user)
95                                                 {
96                                                         userrec* targ = Srv->FindNick(q->target);
97                                                         if (targ)
98                                                         {
99                                                                 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
100                                                         }
101                                                 }
102                                         }
103                                         WriteServ(user->fd,"607 %s :End of WATCH list",user->nick);
104                                 }
105                                 else if (!strcasecmp(nick,"S"))
106                                 {
107                                         std::string list = "";
108                                         for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
109                                         {
110                                                 if (q->watcher == user)
111                                                 {
112                                                         list = list + " " + q->target;
113                                                 }
114                                         }
115                                         char* l = (char*)list.c_str();
116                                         if (*l == ' ')
117                                                 l++;
118                                         WriteServ(user->fd,"606 %s :%s",user->nick,l);
119                                         WriteServ(user->fd,"607 %s :End of WATCH S",user->nick);
120                                 }
121                                 else if (nick[0] == '-')
122                                 {
123                                         // removing an item from the list
124                                         nick++;
125                                         irc::string n1 = nick;
126                                         for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
127                                         {
128                                                 if (q->watcher == user)
129                                                 {
130                                                         irc::string n2 = q->target.c_str();
131                                                         userrec* a = Srv->FindNick(q->target);
132                                                         if (a)
133                                                         {
134                                                                 WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
135                                                         }
136                                                         else
137                                                         {
138                                                                  WriteServ(user->fd,"602 %s %s * * 0 :stopped watching",user->nick,q->target.c_str());
139                                                         }
140                                                         if (n1 == n2)
141                                                         {
142                                                                 watches.erase(q);
143                                                                 break;
144                                                         }
145                                                 }
146                                         }
147                                 }
148                                 else if (nick[0] == '+')
149                                 {
150                                         nick++;
151                                         irc::string n1 = nick;
152                                         bool exists = false;
153                                         for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
154                                         {
155                                                 if (q->watcher == user)
156                                                 {
157                                                         irc::string n2 = q->target.c_str();
158                                                         if (n1 == n2)
159                                                         {
160                                                                 // already on watch list
161                                                                 exists = true;
162                                                         }
163                                                 }
164                                         }
165                                         if (!exists)
166                                         {
167                                                 watchentry w;
168                                                 w.watcher = user;
169                                                 w.target = nick;
170                                                 watches.push_back(w);
171                                                 log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
172                                         }
173                                         userrec* a = Srv->FindNick(nick);
174                                         if (a)
175                                         {
176                                                 WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
177                                         }
178                                         else
179                                         {
180                                                 WriteServ(user->fd,"605 %s %s * * 0 :is offline",user->nick,nick);
181                                         }
182                                 }
183                         }
184                 }
185                 return;
186         }
187 };
188
189 class Modulewatch : public Module
190 {
191         cmd_watch* mycommand;
192  public:
193
194         Modulewatch(Server* Me)
195                 : Module::Module(Me)
196         {
197                 Srv = Me;
198                 mycommand = new cmd_watch();
199                 Srv->AddCommand(mycommand);
200         }
201
202         void Implements(char* List)
203         {
204                 List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
205         }
206
207         virtual void OnUserQuit(userrec* user, const std::string &reason)
208         {
209                 log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
210                 irc::string n2 = user->nick;
211                 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
212                 {
213                         irc::string n1 = q->target.c_str();
214                         if (n1 == n2)
215                         {
216                                 log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,q->watcher->nick);
217                                 WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
218                         }
219                 }
220                 bool done = false;
221                 while (!done)
222                 {
223                         done = true;
224                         for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
225                         {
226                                 if (q->watcher == user)
227                                 {
228                                         done = false;
229                                         watches.erase(q);
230                                         break;
231                                 }
232                         }
233                 }
234         }
235
236         virtual void OnGlobalConnect(userrec* user)
237         {
238                 irc::string n2 = user->nick;
239                 log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
240                 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
241                 {
242                         irc::string n1 = q->target.c_str();
243                         if (n1 == n2)
244                         {
245                                 log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,q->watcher->nick);
246                                 WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age);
247                         }
248                 }
249         }
250
251         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
252         {
253                 irc::string n2 = oldnick.c_str();
254                 irc::string n3 = user->nick;
255                 log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
256                 for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
257                 {
258                         irc::string n1 = q->target.c_str();
259                         // changed from a nick on the watchlist to one that isnt
260                         if (n1 == n2)
261                         {
262                                 log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),q->watcher->nick);
263                                 WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
264                         }
265                         else if (n1 == n3)
266                         {
267                                 // changed from a nick not on notify to one that is
268                                 log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,q->watcher->nick);
269                                 WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age);
270                         }
271                 }
272         }       
273
274         virtual void On005Numeric(std::string &output)
275         {
276                 // we don't really have a limit...
277                 output = output + " WATCH=999";
278         }
279         
280         virtual ~Modulewatch()
281         {
282         }
283         
284         virtual Version GetVersion()
285         {
286                 return Version(1,0,0,1,VF_VENDOR);
287         }
288 };
289
290
291 class ModulewatchFactory : public ModuleFactory
292 {
293  public:
294         ModulewatchFactory()
295         {
296         }
297         
298         ~ModulewatchFactory()
299         {
300         }
301         
302         virtual Module * CreateModule(Server* Me)
303         {
304                 return new Modulewatch(Me);
305         }
306         
307 };
308
309
310 extern "C" void * init_module( void )
311 {
312         return new ModulewatchFactory;
313 }
314