summaryrefslogtreecommitdiff
path: root/modules/operserv/os_ignore.cpp
blob: c30cfe8b2e0995647c97da2dc3bceb15a8213bd3 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* OperServ core functions
 *
 * (C) 2003-2025 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 "modules/operserv/ignore.h"

struct IgnoreDataImpl final
	: IgnoreData
	, Serializable
{
	IgnoreDataImpl() : Serializable("IgnoreData") { }
	~IgnoreDataImpl() override;
};

IgnoreDataImpl::~IgnoreDataImpl()
{
	if (ignore_service)
		ignore_service->DelIgnore(this);
}

struct IgnoreDataTypeImpl final
	: public Serialize::Type
{
	IgnoreDataTypeImpl()
		: Serialize::Type("IgnoreData")
	{
	}
	void Serialize(const Serializable *obj, Serialize::Data &data) const override;
	Serializable *Unserialize(Serializable *obj, Serialize::Data &data) const override;
};

void IgnoreDataTypeImpl::Serialize(const Serializable *obj, Serialize::Data &data) const
{
	const auto *ign = static_cast<const IgnoreDataImpl *>(obj);
	data.Store("mask", ign->mask);
	data.Store("creator", ign->creator);
	data.Store("reason", ign->reason);
	data.Store("time", ign->time);
}

Serializable *IgnoreDataTypeImpl::Unserialize(Serializable *obj, Serialize::Data &data) const
{
	if (!ignore_service)
		return NULL;

	IgnoreDataImpl *ign;
	if (obj)
		ign = anope_dynamic_static_cast<IgnoreDataImpl *>(obj);
	else
	{
		ign = new IgnoreDataImpl();
		ignore_service->AddIgnore(ign);
	}

	data["mask"] >> ign->mask;
	data["creator"] >> ign->creator;
	data["reason"] >> ign->reason;
	data["time"] >> ign->time;

	return ign;
}


class OSIgnoreService final
	: public IgnoreService
{
	Serialize::Checker<std::vector<IgnoreData *> > ignores;

public:
	OSIgnoreService(Module *o) : IgnoreService(o), ignores("IgnoreData") { }

	void AddIgnore(IgnoreData *ign) override
	{
		ignores->push_back(ign);
	}

	void DelIgnore(IgnoreData *ign) override
	{
		std::vector<IgnoreData *>::iterator it = std::find(ignores->begin(), ignores->end(), ign);
		if (it != ignores->end())
			ignores->erase(it);
	}

	void ClearIgnores() override
	{
		for (unsigned i = ignores->size(); i > 0; --i)
		{
			IgnoreData *ign = ignores->at(i - 1);
			delete ign;
		}
	}

	IgnoreData *Create() override
	{
		return new IgnoreDataImpl();
	}

	IgnoreData *Find(const Anope::string &mask) override
	{
		User *u = User::Find(mask, true);
		std::vector<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, true))
					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, false, true))
					break;
		}

		/* Check whether the entry has timed out */
		if (ign != ign_end)
		{
			IgnoreData *id = *ign;

			if (id->time && !Anope::NoExpire && id->time <= Anope::CurTime)
			{
				Log(LOG_NORMAL, "expire/ignore", Config->GetClient("OperServ")) << "Expiring ignore entry " << id->mask;
				delete id;
			}
			else
				return id;
		}

		return NULL;
	}

	std::vector<IgnoreData *> &GetIgnores() override
	{
		return *ignores;
	}
};

class CommandOSIgnore final
	: public Command
{
private:
	static Anope::string RealMask(const Anope::string &mask)
	{
		/* If it s an existing user, we ignore the hostmask. */
		User *u = User::Find(mask, true);
		if (u)
			return "*!*@" + u->host;

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

		/* We only got a nick.. */
		return mask + "!*@*";
	}

	void DoAdd(CommandSource &source, const std::vector<Anope::string> &params)
	{
		if (!ignore_service)
			return;

		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;
		}
		else
		{
			time_t t = Anope::DoTime(time);

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

			Anope::string mask = RealMask(nick);
			if (mask.empty())
			{
				source.Reply(BAD_USERHOST_MASK);
				return;
			}

			if (Anope::ReadOnly)
				source.Reply(READ_ONLY_MODE);

			auto *ign = new IgnoreDataImpl();
			ign->mask = mask;
			ign->creator = source.GetNick();
			ign->reason = reason;
			ign->time = t ? Anope::CurTime + t : 0;

			ignore_service->AddIgnore(ign);
			if (!t)
			{
				source.Reply(_("\002%s\002 will now permanently be ignored."), mask.c_str());
				Log(LOG_ADMIN, source, this) << "to add a permanent ignore for " << mask;
			}
			else
			{
				source.Reply(_("\002%s\002 will now be ignored for \002%s\002."), mask.c_str(), Anope::Duration(t, source.GetAccount()).c_str());
				Log(LOG_ADMIN, source, this) << "to add an ignore on " << mask << " for " << Anope::Duration(t);
			}
		}
	}

	static void DoList(CommandSource &source)
	{
		if (!ignore_service)
			return;

		std::vector<IgnoreData *> &ignores = ignore_service->GetIgnores();
		for (unsigned i = ignores.size(); i > 0; --i)
		{
			IgnoreData *id = ignores[i - 1];

			if (id->time && !Anope::NoExpire && id->time <= Anope::CurTime)
			{
				Log(LOG_NORMAL, "expire/ignore", Config->GetClient("OperServ")) << "Expiring ignore entry " << id->mask;
				delete id;
			}
		}

		if (ignores.empty())
			source.Reply(_("Ignore list is empty."));
		else
		{
			ListFormatter list(source.GetAccount());
			list.AddColumn(_("Mask")).AddColumn(_("Creator")).AddColumn(_("Reason")).AddColumn(_("Expires"));

			for (unsigned i = ignores.size(); i > 0; --i)
			{
				const IgnoreData *ignore = ignores[i - 1];

				ListFormatter::ListEntry entry;
				entry["Mask"] = ignore->mask;
				entry["Creator"] = ignore->creator;
				entry["Reason"] = ignore->reason;
				entry["Expires"] = Anope::Expires(ignore->time, source.GetAccount());
				list.AddEntry(entry);
			}

			source.Reply(_("Services ignore list:"));

			std::vector<Anope::string> replies;
			list.Process(replies);

			for (const auto &reply : replies)
				source.Reply(reply);
		}
	}

	void DoDel(CommandSource &source, const std::vector<Anope::string> &params)
	{
		if (!ignore_service)
			return;

		const Anope::string nick = params.size() > 1 ? params[1] : "";
		if (nick.empty())
		{
			this->OnSyntaxError(source, "DEL");
			return;
		}

		Anope::string mask = RealMask(nick);
		if (mask.empty())
		{
			source.Reply(BAD_USERHOST_MASK);
			return;
		}

		IgnoreData *ign = ignore_service->Find(mask);
		if (ign)
		{
			if (Anope::ReadOnly)
				source.Reply(READ_ONLY_MODE);

			Log(LOG_ADMIN, source, this) << "to remove an ignore on " << mask;
			source.Reply(_("\002%s\002 will no longer be ignored."), mask.c_str());
			delete ign;
		}
		else
			source.Reply(_("\002%s\002 not found on ignore list."), mask.c_str());
	}

	void DoClear(CommandSource &source)
	{
		if (!ignore_service)
			return;

		if (Anope::ReadOnly)
			source.Reply(READ_ONLY_MODE);

		ignore_service->ClearIgnores();
		Log(LOG_ADMIN, source, this) << "to CLEAR the list";
		source.Reply(_("Ignore list has been cleared."));

		return;
	}

public:
	CommandOSIgnore(Module *creator) : Command(creator, "operserv/ignore", 1, 4)
	{
		this->SetDesc(_("Modify the services ignore list"));
		this->SetSyntax(_("ADD \037expiry\037 {\037nick\037|\037mask\037} [\037reason\037]"));
		this->SetSyntax(_("DEL {\037nick\037|\037mask\037}"));
		this->SetSyntax("LIST");
		this->SetSyntax("CLEAR");
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		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;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_(
			"Allows Services Operators to make services ignore a nick or mask "
			"for a certain time or until the next restart. The default "
			"time format is seconds. You can specify it by using units. "
			"Valid units are: \037s\037 for seconds, \037m\037 for minutes, "
			"\037h\037 for hours and \037d\037 for days. "
			"Combinations of these units are not permitted. "
			"To make services permanently ignore the user, type 0 as time. "
			"When adding a \037mask\037, it should be in the format nick!user@host, "
			"everything else will be considered a nick. Wildcards are permitted."
			"\n\n"
			"Ignores will not be enforced on IRC Operators."
		));

		const Anope::string &regexengine = Config->GetBlock("options").Get<const Anope::string>("regexengine");
		if (!regexengine.empty())
		{
			source.Reply(" ");
			source.Reply(_(
					"Regex matches are also supported using the %s engine. "
					"Enclose your pattern in // if this is desired."
				),
				regexengine.c_str());
		}

		return true;
	}
};

class OSIgnore final
	: public Module
{
	IgnoreDataTypeImpl ignoredata_type;
	OSIgnoreService osignoreservice;
	CommandOSIgnore commandosignore;

public:
	OSIgnore(const Anope::string &modname, const Anope::string &creator)
		: Module(modname, creator, VENDOR)
		, osignoreservice(this)
		, commandosignore(this)
	{
	}

	void Prioritize() override
	{
		ModuleManager::SetPriority(this, I_OnBotPrivmsg, PRIORITY_FIRST);
	}

	EventReturn OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &message, const Anope::map<Anope::string> &tags) override
	{
		if (!u->HasMode("OPER") && this->osignoreservice.Find(u->nick))
			return EVENT_STOP;

		return EVENT_CONTINUE;
	}
};

MODULE_INIT(OSIgnore)