summaryrefslogtreecommitdiff
path: root/src/modules/m_watch.cpp
blob: 8e6b2c50ca5d003ad4420142da70694fdfcebffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

using namespace std;

#include <stdio.h>
#include <string>
#include <vector>
#include "users.h"
#include "channels.h"
#include "modules.h"
#include "helperfuncs.h"
#include "hashcomp.h"
#include "inspircd.h"

/* $ModDesc: Provides support for the /watch command */

static Server *Srv;
extern InspIRCd* ServerInstance;

class watchentry : public classbase
{
 public:
	userrec* watcher;
	std::string target;
};

typedef std::vector<watchentry*> watchlist;
watchlist watches;

class cmd_watch : public command_t
{
 public:
	cmd_watch() : command_t("WATCH",0,0)
	{
		this->source = "m_watch.so";
		syntax = "[C|L|S]|[+|-<nick>]";
	}

	void Handle (const char** parameters, int pcnt, userrec *user)
	{
		if (!pcnt)
		{
			for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
			{
				watchentry* a = (watchentry*)(*q);
				if (a->watcher == user)
				{
					userrec* targ = ServerInstance->FindNick(a->target);
					if (targ)
					{
						user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
					}
				}
			}
			user->WriteServ("607 %s :End of WATCH list",user->nick);
		}
		else if (pcnt > 0)
		{
			for (int x = 0; x < pcnt; x++)
			{
				const char *nick = parameters[x];
				if (!strcasecmp(nick,"C"))
				{
					// watch clear
					bool done = false;
					while (!done)
					{
						done = true;
						for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
						{
							watchentry* a = (watchentry*)(*q);
							if (a->watcher == user)
							{
								done = false;
								watches.erase(q);
								delete a;
								break;
							}
						}
					}
				}
				else if (!strcasecmp(nick,"L"))
				{
					for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
					{
						watchentry* a = (watchentry*)(*q);
						if (a->watcher == user)
						{
							userrec* targ = ServerInstance->FindNick(a->target);
							if (targ)
							{
								user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age);
							}
						}
					}
					user->WriteServ("607 %s :End of WATCH list",user->nick);
				}
				else if (!strcasecmp(nick,"S"))
				{
					std::string list = "";
					for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
					{
						watchentry* a = (watchentry*)(*q);
						if (a->watcher == user)
						{
							list.append(" ").append(a->target);
						}
					}
					char* l = (char*)list.c_str();
					if (*l == ' ')
						l++;
					user->WriteServ("606 %s :%s",user->nick,l);
					user->WriteServ("607 %s :End of WATCH S",user->nick);
				}
				else if (nick[0] == '-')
				{
					// removing an item from the list
					nick++;
					irc::string n1 = nick;
					for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
					{
						watchentry* b = (watchentry*)(*q);
						if (b->watcher == user)
						{
							irc::string n2 = b->target.c_str();
							userrec* a = ServerInstance->FindNick(b->target);
							if (a)
							{
								user->WriteServ("602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age);
							}
							else
							{
								 user->WriteServ("602 %s %s * * 0 :stopped watching",user->nick,b->target.c_str());
							}
							if (n1 == n2)
							{
								watches.erase(q);
								delete b;
								break;
							}
						}
					}
				}
				else if (nick[0] == '+')
				{
					nick++;
					irc::string n1 = nick;
					bool exists = false;
					for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
					{
						watchentry* a = (watchentry*)(*q);
						if (a->watcher == user)
						{
							irc::string n2 = a->target.c_str();
							if (n1 == n2)
							{
								// already on watch list
								exists = true;
							}
						}
					}
					if (!exists)
					{
						watchentry* w = new watchentry();
						w->watcher = user;
						w->target = nick;
						watches.push_back(w);
						log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick);
					}
		       			userrec* a = ServerInstance->FindNick(nick);
		       			if (a)
		       			{
		       				user->WriteServ("604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age);
					}
					else
					{
						user->WriteServ("605 %s %s * * 0 :is offline",user->nick,nick);
					}
				}
			}
		}
		return;
	}
};

class Modulewatch : public Module
{
	cmd_watch* mycommand;
 public:

	Modulewatch(Server* Me)
		: Module::Module(Me)
	{
		Srv = Me;
		mycommand = new cmd_watch();
		Srv->AddCommand(mycommand);
	}

	void Implements(char* List)
	{
		List[I_OnUserQuit] = List[I_OnGlobalConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
	}

	virtual void OnUserQuit(userrec* user, const std::string &reason)
	{
		log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
		irc::string n2 = user->nick;
		for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
		{
			watchentry* a = (watchentry*)(*q);
			irc::string n1 = a->target.c_str();
			if (n1 == n2)
			{
				log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,a->watcher->nick);
				a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,user->nick,user->ident,user->dhost,time(NULL));
			}
		}
		bool done = false;
		while (!done)
		{
			done = true;
			for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
			{
				watchentry* a = (watchentry*)(*q);
				if (a->watcher == user)
				{
					done = false;
					watches.erase(q);
					delete a;
					break;
				}
			}
		}
	}

	virtual void OnGlobalConnect(userrec* user)
	{
		irc::string n2 = user->nick;
		log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
		for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
		{
			watchentry* a = (watchentry*)(*q);
			irc::string n1 = a->target.c_str();
			if (n1 == n2)
			{
				log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,a->watcher->nick);
				a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
			}
		}
	}

	virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
	{
		irc::string n2 = oldnick.c_str();
		irc::string n3 = user->nick;
		log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
		for (watchlist::iterator q = watches.begin(); q != watches.end(); q++)
		{
			watchentry* a = (watchentry*)(*q);
			irc::string n1 = a->target.c_str();
			// changed from a nick on the watchlist to one that isnt
			if (n1 == n2)
			{
				log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),a->watcher->nick);
				a->watcher->WriteServ("601 %s %s %s %s %lu :went offline",a->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL));
			}
			else if (n1 == n3)
			{
				// changed from a nick not on notify to one that is
				log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,a->watcher->nick);
				a->watcher->WriteServ("600 %s %s %s %s %lu :arrived online",a->watcher->nick,user->nick,user->ident,user->dhost,user->age);
			}
		}
	}	

	virtual void On005Numeric(std::string &output)
	{
		// we don't really have a limit...
		output = output + " WATCH=999";
	}
	
	virtual ~Modulewatch()
	{
	}
	
	virtual Version GetVersion()
	{
		return Version(1,0,0,1,VF_VENDOR);
	}
};


class ModulewatchFactory : public ModuleFactory
{
 public:
	ModulewatchFactory()
	{
	}
	
	~ModulewatchFactory()
	{
	}
	
	virtual Module * CreateModule(Server* Me)
	{
		return new Modulewatch(Me);
	}
	
};


extern "C" void * init_module( void )
{
	return new ModulewatchFactory;
}