summaryrefslogtreecommitdiff
path: root/modules/commands/os_forbid.cpp
blob: a60b362dbbb4573fd8e5efde7f75af51468e902c (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
/* OperServ core functions
 *
 * (C) 2003-2012 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_forbid.h"

class MyForbidService : public ForbidService
{
	std::vector<ForbidData *> forbidData;

 public:
	MyForbidService(Module *m) : ForbidService(m) { }

	void AddForbid(ForbidData *d)
	{
		this->forbidData.push_back(d);
	}

	void RemoveForbid(ForbidData *d)
	{
		std::vector<ForbidData *>::iterator it = std::find(this->forbidData.begin(), this->forbidData.end(), d);
		if (it != this->forbidData.end())
			this->forbidData.erase(it);
		delete d;
	}

	ForbidData *FindForbid(const Anope::string &mask, ForbidType ftype)
	{
		const std::vector<ForbidData *> &forbids = this->GetForbids();
		for (unsigned i = forbids.size(); i > 0; --i)
		{
			ForbidData *d = this->forbidData[i - 1];

			if ((ftype == FT_NONE || ftype == d->type) && Anope::Match(mask, d->mask))
				return d;
		}
		return NULL;
	}

	const std::vector<ForbidData *> &GetForbids()
	{
		for (unsigned i = this->forbidData.size(); i > 0; --i)
		{
			ForbidData *d = this->forbidData[i - 1];

			if (d->expires && Anope::CurTime >= d->expires)
			{
				Anope::string ftype = "none";
				if (d->type == FT_NICK)
					ftype = "nick";
				else if (d->type == FT_CHAN)
					ftype = "chan";
				else if (d->type == FT_EMAIL)
					ftype = "email";

				Log(LOG_NORMAL, Config->OperServ + "/forbid") << "Expiring forbid for " << d->mask << " type " << ftype;
				this->forbidData.erase(this->forbidData.begin() + i - 1);
				delete d;
			}
		}

		return this->forbidData;
	}
};

class CommandOSForbid : public Command
{
	service_reference<ForbidService> fs;
 public:
	CommandOSForbid(Module *creator) : Command(creator, "operserv/forbid", 1, 5), fs("ForbidService", "forbid")
	{
		this->SetDesc(_("Forbid usage of nicknames, channels, and emails"));
		this->SetSyntax(_("ADD {NICK|CHAN|EMAIL} [+\037expiry\037] \037entry\037\002 [\037reason\037]"));
		this->SetSyntax(_("DEL {NICK|CHAN|EMAIL} \037entry\037"));
		this->SetSyntax(_("LIST (NICK|CHAN|EMAIL)"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		if (!this->fs)
			return;

		const Anope::string &command = params[0];
		const Anope::string &subcommand = params.size() > 1 ? params[1] : "";

		ForbidType ftype = FT_NONE;
		if (subcommand.equals_ci("NICK"))
			ftype = FT_NICK;
		else if (subcommand.equals_ci("CHAN"))
			ftype = FT_CHAN;
		else if (subcommand.equals_ci("EMAIL"))
			ftype = FT_EMAIL;

		if (command.equals_ci("ADD") && params.size() > 3 && ftype != FT_NONE)
		{
			const Anope::string &expiry = params[2][0] == '+' ? params[2] : "";
			const Anope::string &entry = !expiry.empty() ? params[3] : params[2];
			Anope::string reason;
			if (expiry.empty())
				reason = params[3] + " ";
			if (params.size() > 4)
				reason += params[4];
			reason.trim();

			time_t expiryt = 0;

			if (Config->ForceForbidReason && reason.empty())
			{
				this->OnSyntaxError(source, "");
				return;
			}

			if (!expiry.empty())
			{
				expiryt = dotime(expiry);
				if (expiryt)
					expiryt += Anope::CurTime;
			}

			ForbidData *d = this->fs->FindForbid(entry, ftype);
			bool created = false;
			if (d == NULL)
			{
				d = new ForbidData();
				created = true;
			}

			d->mask = entry;
			d->creator = source.u->nick;
			d->reason = reason;
			d->created = Anope::CurTime;
			d->expires = expiryt;
			d->type = ftype;
			if (created)
				this->fs->AddForbid(d);

			Log(LOG_ADMIN, source.u, this) << "to add a forbid on " << entry << " of type " << subcommand;
			source.Reply(_("Added a%s forbid on %s to expire on %s"), ftype == FT_CHAN ? "n" : "", entry.c_str(), d->expires ? do_strftime(d->expires).c_str() : "never");
		}
		else if (command.equals_ci("DEL") && params.size() > 2 && ftype != FT_NONE)
		{
			const Anope::string &entry = params[2];

			ForbidData *d = this->fs->FindForbid(entry, ftype);
			if (d != NULL)
			{
				Log(LOG_ADMIN, source.u, this) << "to remove forbid " << d->mask << " of type " << subcommand;
				source.Reply(_("%s deleted from the %s forbid list."), d->mask.c_str(), subcommand.c_str());
				this->fs->RemoveForbid(d);
			}
			else
				source.Reply(_("Forbid on %s was not found."), entry.c_str());
		}
		else if (command.equals_ci("LIST"))
		{
			const std::vector<ForbidData *> &forbids = this->fs->GetForbids();
			if (forbids.empty())
				source.Reply(_("Forbid list is empty."));
			else
			{
				ListFormatter list;
				list.addColumn("Mask").addColumn("Type").addColumn("Reason");

				for (unsigned i = 0; i < forbids.size(); ++i)
				{
					ForbidData *d = forbids[i];

					Anope::string stype;
					if (d->type == FT_NICK)
						stype = "NICK";
					else if (d->type == FT_CHAN)
						stype = "CHAN";
					else if (d->type == FT_EMAIL)
						stype = "EMAIL";
					else
						continue;

					ListFormatter::ListEntry entry;
					entry["Mask"] = d->mask;
					entry["Type"] = stype;
					entry["Creator"] = d->creator;
					entry["Expires"] = d->expires ? do_strftime(d->expires).c_str() : "never";
					entry["Reason"] = d->reason;
					list.addEntry(entry);
				}

				source.Reply(_("Forbid list:"));

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

				for (unsigned i = 0; i < replies.size(); ++i)
					source.Reply(replies[i]);

				source.Reply(_("End of forbid list."));
			}
		}
		else
			this->OnSyntaxError(source, command);

		return;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_("Forbid allows you to forbid usage of certain nicknames, channels,\n"
				"and email addresses. Wildcards are accepted for all entries."));
		return true;
	}
};

class OSForbid : public Module
{
	SerializeType forbiddata_type;
	MyForbidService forbidService;
	CommandOSForbid commandosforbid;

 public:
	OSForbid(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
		forbiddata_type("ForbidData", ForbidData::unserialize), forbidService(this), commandosforbid(this)
	{
		this->SetAuthor("Anope");

		Implementation i[] = { I_OnUserConnect, I_OnUserNickChange, I_OnJoinChannel, I_OnPreCommand };
		ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
	}

	void OnUserConnect(dynamic_reference<User> &u, bool &exempt)
	{
		if (!u || exempt)
			return;

		this->OnUserNickChange(u, "");
	}

	void OnUserNickChange(User *u, const Anope::string &)
	{
		if (u->HasMode(UMODE_OPER))
			return;

		ForbidData *d = this->forbidService.FindForbid(u->nick, FT_NICK);
		if (d != NULL)
		{
			BotInfo *bi = findbot(Config->OperServ);
			if (bi)
			{
				if (d->reason.empty())
					u->SendMessage(bi, _("This nickname has been forbidden."));
				else
					u->SendMessage(bi, _("This nickname has been forbidden: %s"), d->reason.c_str());
			}
			u->Collide(NULL);
		}
	}

	void OnJoinChannel(User *u, Channel *c)
	{
		if (u->HasMode(UMODE_OPER))
			return;

		BotInfo *bi = findbot(Config->OperServ);
		ForbidData *d = this->forbidService.FindForbid(c->name, FT_CHAN);
		if (bi != NULL && d != NULL)
		{			
			if (!c->HasFlag(CH_INHABIT))
			{
				/* Join ChanServ and set a timer for this channel to part ChanServ later */
				new ChanServTimer(c);

				/* Set +si to prevent rejoin */
				c->SetMode(NULL, CMODE_NOEXTERNAL);
				c->SetMode(NULL, CMODE_TOPIC);
				c->SetMode(NULL, CMODE_SECRET);
				c->SetMode(NULL, CMODE_INVITE);
			}

			if (d->reason.empty())
				c->Kick(bi, u, _("This channel has been forbidden."));
			else
				c->Kick(bi, u, _("This channel has been forbidden: %s"), d->reason.c_str());
		}
	}

	EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> &params)
	{
		if (source.u->HasMode(UMODE_OPER))
			return EVENT_CONTINUE;
		else if (command->name == "nickserv/register" && params.size() > 1)
		{
			ForbidData *d = this->forbidService.FindForbid(params[1], FT_EMAIL);
			if (d != NULL)
			{
				source.Reply("Your email address is not allowed, choose a different one.");
				return EVENT_STOP;
			}
		}
		else if (command->name == "nickserv/set/email" && params.size() > 0)
		{
			ForbidData *d = this->forbidService.FindForbid(params[0], FT_EMAIL);
			if (d != NULL)
			{
				source.Reply("Your email address is not allowed, choose a different one.");
				return EVENT_STOP;
			}
		}

		return EVENT_CONTINUE;
	}
};

MODULE_INIT(OSForbid)