summaryrefslogtreecommitdiff
path: root/modules/core/os_ignore.cpp
blob: 91a3fff79d9253b7ba4e7de4ebfa99000baecb87 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* OperServ core functions
 *
 * (C) 2003-2010 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

/*************************************************************************/

#include "module.h"
#include "os_ignore.h"

class OSIgnoreService : public IgnoreService
{
 public:
	OSIgnoreService(Module *o, const Anope::string &n) : IgnoreService(o, n) { }

	void AddIgnore(const Anope::string &mask, const Anope::string &creator, const Anope::string &reason, time_t delta = Anope::CurTime)
	{
		/* If it s an existing user, we ignore the hostmask. */
		Anope::string realmask = mask;
		size_t user, host;

		User *u = finduser(mask);
		if (u)
			realmask = "*!*@" + u->host;
		/* Determine whether we get a nick or a mask. */
		else if ((host = mask.find('@')) != Anope::string::npos)
		{
			/* Check whether we have a nick too.. */
			if ((user = mask.find('!')) != Anope::string::npos)
			{
				/* this should never happen */
				if (user > host)
					return;
			}
			else
				/* We have user@host. Add nick wildcard. */
				realmask = "*!" + mask;
		}
		/* We only got a nick.. */
		else
			realmask = mask + "!*@*";

		/* Check if we already got an identical entry. */
		IgnoreData *ign = this->Find(realmask);
		if (ign != NULL)
		{
			if (!delta)
				ign->time = 0;
			else
				ign->time = Anope::CurTime + delta;
		}
		/* Create new entry.. */
		else
		{
			IgnoreData newign;
			newign.mask = realmask;
			newign.creator = creator;
			newign.reason = reason;
			newign.time = delta ? Anope::CurTime + delta : 0;
			this->ignores.push_back(newign);
		}
	}

	bool DelIgnore(const Anope::string &mask)
	{
		for (std::list<IgnoreData>::iterator it = this->ignores.begin(), it_end = this->ignores.end(); it != it_end; ++it)
		{
			IgnoreData &idn = *it;
			if (idn.mask.equals_ci(mask))
			{
				this->ignores.erase(it);
				return true;
			}
		}

		return false;
	}

	IgnoreData *Find(const Anope::string &mask)
	{
		User *u = finduser(mask);
		std::list<IgnoreData>::iterator ign = this->ignores.begin(), ign_end = this->ignores.end();
		
		if (u)
		{
			for (; ign != ign_end; ++ign)
			{
				Entry ignore_mask(ign->mask);
				if (ignore_mask.Matches(u))
					break;
			}
		}
		else
		{
			size_t user, host;
			Anope::string tmp;
			/* We didn't get a user.. generate a valid mask. */
			if ((host = mask.find('@')) != Anope::string::npos)
			{
				if ((user = mask.find('!')) != Anope::string::npos)
				{
					/* this should never happen */
					if (user > host)
						return NULL;
					tmp = mask;
				}
				else
					/* We have user@host. Add nick wildcard. */
				tmp = "*!" + mask;
			}
			/* We only got a nick.. */
			else
				tmp = mask + "!*@*";

			for (; ign != ign_end; ++ign)
				if (Anope::Match(tmp, ign->mask))
					break;
		}

		/* Check whether the entry has timed out */
		if (ign != ign_end)// && (*ign)->time && (*ign)->time <= Anope::CurTime)
		{
			IgnoreData &id = *ign;

			if (id.time && id.time <= Anope::CurTime)
			{
				Log(LOG_DEBUG) << "Expiring ignore entry " << id.mask;
				this->ignores.erase(ign);
			}
			else
				return &id;
		}

		return NULL;
	}
};

class CommandOSIgnore : public Command
{
 private:
	CommandReturn DoAdd(CommandSource &source, const std::vector<Anope::string> &params)
	{
		service_reference<IgnoreService> ignore_service("operserv/ignore");
		if (!ignore_service)
			return MOD_CONT;

		const Anope::string &time = params.size() > 1 ? params[1] : "";
		const Anope::string &nick = params.size() > 2 ? params[2] : "";
		const Anope::string &reason = params.size() > 3 ? params[3] : "";

		if (time.empty() || nick.empty())
		{
			this->OnSyntaxError(source, "ADD");
			return MOD_CONT;
		}
		else
		{
			time_t t = dotime(time);

			if (t <= -1)
			{
				source.Reply(OPER_IGNORE_VALID_TIME);
				return MOD_CONT;
			}

			ignore_service->AddIgnore(nick, source.u->nick, reason, t);
			if (!t)
				source.Reply(OPER_IGNORE_PERM_DONE, nick.c_str());
			else
				source.Reply(OPER_IGNORE_TIME_DONE, nick.c_str(), time.c_str());
		}

		return MOD_CONT;
	}

	CommandReturn DoList(CommandSource &source)
	{
		service_reference<IgnoreService> ignore_service("operserv/ignore");
		if (!ignore_service)
			return MOD_CONT;

		const std::list<IgnoreData> &ignores = ignore_service->GetIgnores();
		if (ignores.empty())
			source.Reply(OPER_IGNORE_LIST_EMPTY);
		else
		{
			source.Reply(OPER_IGNORE_LIST);
			for (std::list<IgnoreData>::const_iterator ign = ignores.begin(), ign_end = ignores.end(); ign != ign_end; ++ign)
			{
				const IgnoreData &ignore = *ign;

				source.Reply("  %-11s %-11s  %-11s %s", ignore.mask.c_str(), ignore.creator.c_str(), ignore.reason.c_str(), do_strftime(ignore.time).c_str());
			}
		}

		return MOD_CONT;
	}

	CommandReturn DoDel(CommandSource &source, const std::vector<Anope::string> &params)
	{
		service_reference<IgnoreService> ignore_service("operserv/ignore");
		if (!ignore_service)
			return MOD_CONT;

		const Anope::string nick = params.size() > 1 ? params[1] : "";
		if (nick.empty())
			this->OnSyntaxError(source, "DEL");
		else if (ignore_service->DelIgnore(nick))
			source.Reply(OPER_IGNORE_DEL_DONE, nick.c_str());
		else
			source.Reply(OPER_IGNORE_LIST_NOMATCH, nick.c_str());

		return MOD_CONT;
	}

	CommandReturn DoClear(CommandSource &source)
	{
		service_reference<IgnoreService> ignore_service("operserv/ignore");
		if (!ignore_service)
			return MOD_CONT;

		ignore_service->ClearIgnores();
		source.Reply(OPER_IGNORE_LIST_CLEARED);

		return MOD_CONT;
	}

 public:
	CommandOSIgnore() : Command("IGNORE", 1, 4, "operserv/ignore")
	{
	}

	CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		const Anope::string &cmd = params[0];

		if (cmd.equals_ci("ADD"))
			return this->DoAdd(source, params);
		else if (cmd.equals_ci("LIST"))
			return this->DoList(source);
		else if (cmd.equals_ci("DEL"))
			return this->DoDel(source, params);
		else if (cmd.equals_ci("CLEAR"))
			return this->DoClear(source);
		else
			this->OnSyntaxError(source, "");

		return MOD_CONT;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		source.Reply(OPER_HELP_IGNORE);
		return true;
	}

	void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
	{
		SyntaxError(source, "IGNORE", OPER_IGNORE_SYNTAX);
	}

	void OnServHelp(CommandSource &source)
	{
		source.Reply(OPER_HELP_CMD_IGNORE);
	}
};

class OSIgnore : public Module
{
	OSIgnoreService osignoreservice;
	CommandOSIgnore commandosignore;

 public:
	OSIgnore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator), osignoreservice(this, "operserv/ignore")
	{
		this->SetAuthor("Anope");
		this->SetType(CORE);

		this->AddCommand(OperServ, &commandosignore);

		Implementation i[] = { I_OnDatabaseRead, I_OnDatabaseWrite, I_OnPreCommandRun, I_OnBotPrivmsg };
		ModuleManager::Attach(i, this, 4);

		ModuleManager::RegisterService(&this->osignoreservice);
	}

	EventReturn OnDatabaseRead(const std::vector<Anope::string> &params)
	{
		if (params.size() >= 4 && params[0].equals_ci("OS") && params[1].equals_ci("IGNORE"))
		{
			service_reference<IgnoreService> ignore_service("operserv/ignore");
			if (ignore_service)
			{
				const Anope::string &mask = params[2];
				time_t time = params[3].is_pos_number_only() ? convertTo<time_t>(params[3]) : 0;
				const Anope::string &creator = params.size() > 4 ? params[4] : "";
				const Anope::string &reason = params.size() > 5 ? params[5] : "";
				ignore_service->AddIgnore(mask, creator, reason, time - Anope::CurTime);

				return EVENT_STOP;
			}
		}

		return EVENT_CONTINUE;
	}

	void OnDatabaseWrite(void (*Write)(const Anope::string &))
	{
		service_reference<IgnoreService> ignore_service("operserv/ignore");
		if (ignore_service)
		{
			for (std::list<IgnoreData>::iterator ign = ignore_service->GetIgnores().begin(), ign_end = ignore_service->GetIgnores().end(); ign != ign_end; )
			{
				if (ign->time && ign->time <= Anope::CurTime)
				{
					Log(LOG_DEBUG) << "[os_ignore] Expiring ignore entry " << ign->mask;
					ign = ignore_service->GetIgnores().erase(ign);
				}
				else
				{
					std::stringstream buf;
					buf << "OS IGNORE " << ign->mask << " " << ign->time << " " << ign->creator << " :" << ign->reason;
					Write(buf.str());
					++ign;
				}
			}
		}
	}

	EventReturn OnPreCommandRun(User *&u, BotInfo *&bi, Anope::string &command, Anope::string &message, ChannelInfo *&ci)
	{
		return this->OnBotPrivmsg(u, bi, message);
	}

	EventReturn OnBotPrivmsg(User *u, BotInfo *bi, const Anope::string &message)
	{
		service_reference<IgnoreService> ignore_service("operserv/ignore");
		if (ignore_service)
		{
			if (ignore_service->Find(u->nick))
				return EVENT_STOP;
		}

		return EVENT_CONTINUE;
	}
};

MODULE_INIT(OSIgnore)