summaryrefslogtreecommitdiff
path: root/modules/extra/hs_request.cpp
blob: 9ae097d27c43a12c81715804d208c0bbd5c25c9f (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* hs_request.c - Add request and activate functionality to HostServ,
 *
 *
 * (C) 2003-2010 Anope Team
 * Contact us at team@anope.org
 *
 * Based on the original module by Rob <rob@anope.org>
 * Included in the Anope module pack since Anope 1.7.11
 * Anope Coder: GeniusDex <geniusdex@anope.org>
 *
 * Please read COPYING and README for further details.
 *
 * Send bug reports to the Anope Coder instead of the module
 * author, because any changes since the inclusion into anope
 * are not supported by the original author.
 */

#include "module.h"

static bool HSRequestMemoUser = false;
static bool HSRequestMemoOper = false;

void my_add_host_request(const Anope::string &nick, const Anope::string &vIdent, const Anope::string &vhost, const Anope::string &creator, time_t tmp_time);
void req_send_memos(User *u, const Anope::string &vIdent, const Anope::string &vHost);

struct HostRequest
{
	Anope::string ident;
	Anope::string host;
	time_t time;
};

typedef std::map<Anope::string, HostRequest *, std::less<ci::string> > RequestMap;
RequestMap Requests;

static Module *me;

class CommandHSRequest : public Command
{
	bool isvalidchar(char c)
	{
		if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '-')
			return true;
		return false;
	}

 public:
	CommandHSRequest() : Command("REQUEST", 1, 1)
	{
	}

	CommandReturn Execute(User *u, const std::vector<Anope::string> &params)
	{
		Anope::string nick = u->nick;
		Anope::string rawhostmask = params[0];
		Anope::string hostmask;
		NickAlias *na;

		Anope::string vIdent = myStrGetToken(rawhostmask, '@', 0); /* Get the first substring, @ as delimiter */
		if (!vIdent.empty())
		{
			rawhostmask = myStrGetTokenRemainder(rawhostmask, '@', 1); /* get the remaining string */
			if (rawhostmask.empty())
			{
				me->SendMessage(HostServ, u, _("Syntax: \002REQUEST \037vhost\037\002"));
				return MOD_CONT;
			}
			if (vIdent.length() > Config->UserLen)
			{
				u->SendMessage(HostServ, HOST_SET_IDENTTOOLONG, Config->UserLen);
				return MOD_CONT;
			}
			else
				for (Anope::string::iterator s = vIdent.begin(), s_end = vIdent.end(); s != s_end; ++s)
					if (!isvalidchar(*s))
					{
						u->SendMessage(HostServ, HOST_SET_IDENT_ERROR);
						return MOD_CONT;
					}
			if (!ircd->vident)
			{
				u->SendMessage(HostServ, HOST_NO_VIDENT);
				return MOD_CONT;
			}
		}
		if (rawhostmask.length() < Config->HostLen)
			hostmask = rawhostmask;
		else
		{
			u->SendMessage(HostServ, HOST_SET_TOOLONG, Config->HostLen);
			return MOD_CONT;
		}

		if (!isValidHost(hostmask, 3))
		{
			u->SendMessage(HostServ, HOST_SET_ERROR);
			return MOD_CONT;
		}

		if ((na = findnick(nick)))
		{
			if (HSRequestMemoOper && Config->MSSendDelay > 0 && u && u->lastmemosend + Config->MSSendDelay > Anope::CurTime)
			{
				me->SendMessage(HostServ, u, _("Please wait %d seconds before requesting a new vHost"), Config->MSSendDelay);
				u->lastmemosend = Anope::CurTime;
				return MOD_CONT;
			}
			my_add_host_request(nick, vIdent, hostmask, u->nick, Anope::CurTime);

			me->SendMessage(HostServ, u, _("Your vHost has been requested"));
			req_send_memos(u, vIdent, hostmask);
			Log(LOG_COMMAND, u, this, NULL) << "to request new vhost " << (!vIdent.empty() ? vIdent + "@" : "") << hostmask;
		}
		else
			u->SendMessage(HostServ, HOST_NOREG, nick.c_str());

		return MOD_CONT;
	}

	bool OnHelp(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002REQUEST \037vhost\037\002"));
		me->SendMessage(HostServ, u, " ");
		me->SendMessage(HostServ, u, _("Request the given vHost to be actived for your nick by the\n"
			"network administrators. Please be patient while your request\n"
			"is being considered."));
		return true;
	}

	void OnSyntaxError(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002REQUEST \037vhost\037\002"));
	}

	void OnServHelp(User *u)
	{
		me->SendMessage(HostServ, u, _("    REQUEST     Request a vHost for your nick"));
	}
};

class CommandHSActivate : public Command
{
 public:
	CommandHSActivate() : Command("ACTIVATE", 1, 1, "hostserv/set")
	{
	}

	CommandReturn Execute(User *u, const std::vector<Anope::string> &params)
	{
		Anope::string nick = params[0];
		NickAlias *na;

		if ((na = findnick(nick)))
		{
			RequestMap::iterator it = Requests.find(na->nick);
			if (it != Requests.end())
			{
				na->hostinfo.SetVhost(it->second->ident, it->second->host, u->nick, it->second->time);

				if (HSRequestMemoUser)
					memo_send(u, na->nick, _("[auto memo] Your requested vHost has been approved."), 2);

				me->SendMessage(HostServ, u, _("vHost for %s has been activated"), na->nick.c_str());
				Log(LOG_COMMAND, u, this, NULL) << "for " << na->nick << " for vhost " << (!it->second->ident.empty() ? it->second->ident + "@" : "") << it->second->host;
				delete it->second;
				Requests.erase(it);
			}
			else
				me->SendMessage(HostServ, u, _("No request for nick %s found."), nick.c_str());
		}
		else
			u->SendMessage(HostServ, NICK_X_NOT_REGISTERED, nick.c_str());

		return MOD_CONT;
	}

	bool OnHelp(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002ACTIVATE \037nick\037\002"));
		me->SendMessage(HostServ, u, " ");
		me->SendMessage(HostServ, u, _("Activate the requested vHost for the given nick."));
		if (HSRequestMemoUser)
			me->SendMessage(HostServ, u, _("A memo informing the user will also be sent."));

		return true;
	}

	void OnSyntaxError(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002ACTIVATE \037nick\037\002"));
	}

	void OnServHelp(User *u)
	{
		me->SendMessage(HostServ, u, _("    ACTIVATE    Approve the requested vHost of a user\n"
			"    REJECT      Reject the requested vHost of a user\n"
			"    WAITING     Convenience command for LIST +req"));
	}
};

class CommandHSReject : public Command
{
 public:
	CommandHSReject() : Command("REJECT", 1, 2, "hostserv/set")
	{
	}

	CommandReturn Execute(User *u, const std::vector<Anope::string> &params)
	{
		Anope::string nick = params[0];
		Anope::string reason = params.size() > 1 ? params[1] : "";

		RequestMap::iterator it = Requests.find(nick);
		if (it != Requests.end())
		{
			delete it->second;
			Requests.erase(it);

			if (HSRequestMemoUser)
			{
				char message[BUFSIZE];
				if (!reason.empty())
					snprintf(message, sizeof(message), _("[auto memo] Your requested vHost has been rejected. Reason: %s"), reason.c_str());
				else
					snprintf(message, sizeof(message), _("[auto memo] Your requested vHost has been rejected."));

				memo_send(u, nick, message, 2);
			}

			me->SendMessage(HostServ, u, _("vHost for %s has been rejected"), nick.c_str());
			Log(LOG_COMMAND, u, this, NULL) << "to reject vhost for " << nick << " (" << (!reason.empty() ? reason : "") << ")";
		}
		else
			me->SendMessage(HostServ, u, _("No request for nick %s found."), nick.c_str());

		return MOD_CONT;
	}

	bool OnHelp(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002REJECT \037nick\037\002"));
		me->SendMessage(HostServ, u, " ");
		me->SendMessage(HostServ, u, _("Reject the requested vHost for the given nick."));
		if (HSRequestMemoUser)
			me->SendMessage(HostServ, u, _("A memo informing the user will also be sent."));

		return true;
	}

	void OnSyntaxError(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002REJECT \037nick\037\002"));
	}
};

class HSListBase : public Command
{
 protected:
	CommandReturn DoList(User *u)
	{
		int counter = 1;
		int from = 0, to = 0;
		unsigned display_counter = 0;

		for (RequestMap::iterator it = Requests.begin(), it_end = Requests.end(); it != it_end; ++it)
		{
			HostRequest *hr = it->second;
			if (((counter >= from && counter <= to) || (!from && !to)) && display_counter < Config->NSListMax)
			{
				++display_counter;
				if (!hr->ident.empty())
					u->SendMessage(HostServ, HOST_IDENT_ENTRY, counter, it->first.c_str(), hr->ident.c_str(), hr->host.c_str(), it->first.c_str(), do_strftime(hr->time).c_str());
				else
					u->SendMessage(HostServ, HOST_ENTRY, counter, it->first.c_str(), hr->host.c_str(), it->first.c_str(), do_strftime(hr->time).c_str());
			}
			++counter;
		}
		u->SendMessage(HostServ, HOST_LIST_FOOTER, display_counter);

		return MOD_CONT;
	}
 public:
	HSListBase(const Anope::string &cmd, int min, int max) : Command(cmd, min, max, "hostserv/set")
	{
	}

	void OnSyntaxError(User *u, const Anope::string &subcommand)
	{
		// no-op
	}
};

class CommandHSWaiting : public HSListBase
{
 public:
	CommandHSWaiting() : HSListBase("WAITING", 0, 0)
	{
	}

	CommandReturn Execute(User *u, const std::vector<Anope::string> &params)
	{
		return this->DoList(u);
	}

	bool OnHelp(User *u, const Anope::string &subcommand)
	{
		me->SendMessage(HostServ, u, _("Syntax: \002WAITING\002"));
		me->SendMessage(HostServ, u, " ");
		me->SendMessage(HostServ, u, _("This command is provided for convenience. It is essentially\n"
			"the same as performing a LIST +req ."));

		return true;
	}
};

class HSRequest : public Module
{
	CommandHSRequest commandhsrequest;
	CommandHSActivate commandhsactive;
	CommandHSReject commandhsreject;
	CommandHSWaiting commandhswaiting;

 public:
	HSRequest(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
	{
		me = this;

		this->AddCommand(HostServ, &commandhsrequest);
		this->AddCommand(HostServ, &commandhsactive);
		this->AddCommand(HostServ, &commandhsreject);
		this->AddCommand(HostServ, &commandhswaiting);

		this->SetAuthor("Anope");
		this->SetType(SUPPORTED);

		this->OnReload(false);

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

	~HSRequest()
	{
		/* Clean up all open host requests */
		while (!Requests.empty())
		{
			delete Requests.begin()->second;
			Requests.erase(Requests.begin());
		}
	}

	EventReturn OnPreCommand(User *u, BotInfo *service, const Anope::string &command, const std::vector<Anope::string> &params)
	{
		if (!Config->s_HostServ.empty() && service == findbot(Config->s_HostServ))
		{
			if (command.equals_ci("LIST"))
			{
				Anope::string key = params.size() ? params[0] : "";

				if (!key.empty() && key.equals_ci("+req"))
				{
					std::vector<Anope::string> emptyParams;
					Command *c = FindCommand(HostServ, "WAITING");
					c->Execute(u, emptyParams);
					return EVENT_STOP;
				}
			}
		}
		else if (service == findbot(Config->s_NickServ))
		{
			if (command.equals_ci("DROP"))
			{
				NickAlias *na = findnick(u->nick);

				if (na)
				{
					RequestMap::iterator it = Requests.find(na->nick);

					if (it != Requests.end())
					{
						delete it->second;
						Requests.erase(it);
					}
				}
			}
		}

		return EVENT_CONTINUE;
	}

	EventReturn OnDatabaseRead(const std::vector<Anope::string> &params)
	{
		if (params[0].equals_ci("HS_REQUEST") && params.size() >= 5)
		{
			Anope::string vident = params[2].equals_ci("(null)") ? "" : params[2];
			my_add_host_request(params[1], vident, params[3], params[1], params[4].is_pos_number_only() ? convertTo<time_t>(params[4]) : 0);

			return EVENT_STOP;
		}

		return EVENT_CONTINUE;
	}

	void OnDatabaseWrite(void (*Write)(const Anope::string &))
	{
		for (RequestMap::iterator it = Requests.begin(), it_end = Requests.end(); it != it_end; ++it)
		{
			HostRequest *hr = it->second;
			std::stringstream buf;
			buf << "HS_REQUEST " << it->first << " " << (hr->ident.empty() ? "(null)" : hr->ident) << " " << hr->host << " " << hr->time;
			Write(buf.str());
		}
	}

	void OnReload(bool)
	{
		ConfigReader config;
		HSRequestMemoUser = config.ReadFlag("hs_request", "memouser", "no", 0);
		HSRequestMemoOper = config.ReadFlag("hs_request", "memooper", "no", 0);

		Log(LOG_DEBUG) << "[hs_request] Set config vars: MemoUser=" << HSRequestMemoUser << " MemoOper=" <<  HSRequestMemoOper;
	}
};

void req_send_memos(User *u, const Anope::string &vIdent, const Anope::string &vHost)
{
	Anope::string host;
	std::list<std::pair<Anope::string, Anope::string> >::iterator it, it_end;

	if (!vIdent.empty())
		host = vIdent + "@" + vHost;
	else
		host = vHost;

	if (HSRequestMemoOper == 1)
		for (it = Config->Opers.begin(), it_end = Config->Opers.end(); it != it_end; ++it)
		{
			Anope::string nick = it->first;
			char message[BUFSIZE];
			snprintf(message, sizeof(message), _("[auto memo] vHost \002%s\002 has been requested."), host.c_str());
			memo_send(u, nick, message, 2);
		}
}

void my_add_host_request(const Anope::string &nick, const Anope::string &vIdent, const Anope::string &vhost, const Anope::string &creator, time_t tmp_time)
{
	HostRequest *hr = new HostRequest;
	hr->ident = vIdent;
	hr->host = vhost;
	hr->time = tmp_time;
	RequestMap::iterator it = Requests.find(nick);
	if (it != Requests.end())
	{
		delete it->second;
		Requests.erase(it);
	}
	Requests.insert(std::make_pair(nick, hr));
}

void my_load_config()
{
	ConfigReader config;
	HSRequestMemoUser = config.ReadFlag("hs_request", "memouser", "no", 0);
	HSRequestMemoOper = config.ReadFlag("hs_request", "memooper", "no", 0);

	Log(LOG_DEBUG) << "[hs_request] Set config vars: MemoUser=" << HSRequestMemoUser << " MemoOper=" <<  HSRequestMemoOper;
}

MODULE_INIT(HSRequest)