summaryrefslogtreecommitdiff
path: root/modules/commands/hs_request.cpp
blob: 634dec06c55cd742b69a055de408eeb6115f5933 (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
/* hs_request.c - Add request and activate functionality to HostServ,
 *
 *
 * (C) 2003-2012 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"
#include "memoserv.h"

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

void req_send_memos(CommandSource &source, const Anope::string &vIdent, const Anope::string &vHost);

struct HostRequest : ExtensibleItem, Serializable
{
	Anope::string nick;
	Anope::string ident;
	Anope::string host;
	time_t time;

	Anope::string serialize_name() const
	{
		return "HostRequest";
	}

	serialized_data serialize()
	{
		serialized_data data;

		data["nick"] << this->nick;
		data["ident"] << this->ident;
		data["host"] << this->host;
		data["time"].setType(Serialize::DT_INT) << this->time;

		return data;
	}

	static void unserialize(serialized_data &data)
	{
		NickAlias *na = findnick(data["nick"].astr());
		if (na == NULL)
			return;

		HostRequest *req = new HostRequest;
		req->nick = na->nick;
		data["ident"] >> req->ident;
		data["host"] >> req->host;
		data["time"] >> req->time;

		na->Extend("hs_request", req);
	}
};

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(Module *creator) : Command(creator, "hostserv/request", 1, 1)
	{
		this->SetDesc(_("Request a vHost for your nick"));
		this->SetSyntax(_("vhost"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		User *u = source.u;

		NickAlias *na = findnick(u->nick);
		if (na == NULL)
			na = findnick(u->Account()->display);
		if (!na)
			return;

		Anope::string rawhostmask = params[0];
		
		Anope::string user, host;
		size_t a = rawhostmask.find('@');

		if (a == Anope::string::npos)
			host = rawhostmask;
		else
		{
			user = rawhostmask.substr(0, a);
			host = rawhostmask.substr(a + 1);
		}

		if (host.empty())
		{
			this->OnSyntaxError(source, "");
			return;
		}

		if (!user.empty())
		{
			if (user.length() > Config->UserLen)
			{
				source.Reply(HOST_SET_IDENTTOOLONG, Config->UserLen);
				return;
			}
			else if (!ircd->vident)
			{
				source.Reply(HOST_NO_VIDENT);
				return;
			}
			for (Anope::string::iterator s = user.begin(), s_end = user.end(); s != s_end; ++s)
				if (!isvalidchar(*s))
				{
					source.Reply(HOST_SET_IDENT_ERROR);
					return;
				}
		}

		if (host.length() > Config->HostLen)
		{
			source.Reply(HOST_SET_TOOLONG, Config->HostLen);
			return;
		}

		if (!IsValidHost(host))
		{
			source.Reply(HOST_SET_ERROR);
			return;
		}

		if (HSRequestMemoOper && Config->MSSendDelay > 0 && u && u->lastmemosend + Config->MSSendDelay > Anope::CurTime)
		{
			source.Reply(_("Please wait %d seconds before requesting a new vHost"), Config->MSSendDelay);
			u->lastmemosend = Anope::CurTime;
			return;
		}


		HostRequest *req = new HostRequest;
		req->nick = u->nick;
		req->ident = user;
		req->host = host;
		req->time = Anope::CurTime;
		na->Extend("hs_request", req);

		source.Reply(_("Your vHost has been requested"));
		req_send_memos(source, user, host);
		Log(LOG_COMMAND, u, this, NULL) << "to request new vhost " << (!user.empty() ? user + "@" : "") << host;

		return;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_("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;
	}
};

class CommandHSActivate : public Command
{
 public:
	CommandHSActivate(Module *creator) : Command(creator, "hostserv/activate", 1, 1)
	{
		this->SetDesc(_("Approve the requested vHost of a user"));
		this->SetSyntax(_("\037nick\037"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		User *u = source.u;

		const Anope::string &nick = params[0];

		NickAlias *na = findnick(nick);
		HostRequest *req = na ? na->GetExt<HostRequest *>("hs_request") : NULL;
		if (req)
		{
			na->hostinfo.SetVhost(req->ident, req->host, u->nick, req->time);
			FOREACH_MOD(I_OnSetVhost, OnSetVhost(na));

			if (HSRequestMemoUser && memoserv)
				memoserv->Send(Config->HostServ, na->nick, _("[auto memo] Your requested vHost has been approved."), true);

			source.Reply(_("vHost for %s has been activated"), na->nick.c_str());
			Log(LOG_COMMAND, u, this, NULL) << "for " << na->nick << " for vhost " << (!req->ident.empty() ? req->ident + "@" : "") << req->host;
			na->Shrink("hs_request");
		}
		else
			source.Reply(_("No request for nick %s found."), nick.c_str());
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_("Activate the requested vHost for the given nick."));
		if (HSRequestMemoUser)
			source.Reply(_("A memo informing the user will also be sent."));

		return true;
	}
};

class CommandHSReject : public Command
{
 public:
	CommandHSReject(Module *creator) : Command(creator, "hostserv/reject", 1, 2)
	{
		this->SetDesc(_("Reject the requested vHost of a user"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		User *u = source.u;

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

		NickAlias *na = findnick(nick);
		HostRequest *req = na ? na->GetExt<HostRequest *>("hs_request") : NULL;
		if (req)
		{
			na->Shrink("hs_request");

			if (HSRequestMemoUser && memoserv)
			{
				Anope::string message;
				if (!reason.empty())
					message = Anope::printf(_("[auto memo] Your requested vHost has been rejected. Reason: %s"), reason.c_str());
				else
					message = _("[auto memo] Your requested vHost has been rejected.");

				memoserv->Send(Config->HostServ, nick, message, true);
			}

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

		return;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_("Reject the requested vHost for the given nick."));
		if (HSRequestMemoUser)
			source.Reply(_("A memo informing the user will also be sent."));

		return true;
	}
};

class CommandHSWaiting : public Command
{
	void DoList(CommandSource &source)
	{
		int counter = 1;
		int from = 0, to = 0;
		unsigned display_counter = 0;
		ListFormatter list;

		list.addColumn("Number").addColumn("Nick").addColumn("Vhost").addColumn("Created");

		for (nickalias_map::const_iterator it = NickAliasList.begin(), it_end = NickAliasList.end(); it != it_end; ++it)
		{
			NickAlias *na = it->second;
			HostRequest *hr = na->GetExt<HostRequest *>("hs_request");
			if (!hr)
				continue;

			if (((counter >= from && counter <= to) || (!from && !to)) && display_counter < Config->NSListMax)
			{
				++display_counter;

				ListFormatter::ListEntry entry;
				entry["Number"] = stringify(counter);
				entry["Nick"] = it->first;
				if (!hr->ident.empty())
					entry["Vhost"] = hr->ident + "@" + hr->host;
				else
					entry["Vhost"] = hr->host;
				entry["Created"] = do_strftime(hr->time);
				list.addEntry(entry);
			}
			++counter;
		}
		source.Reply(_("Displayed all records (Count: \002%d\002)"), display_counter);

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

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

 public:
	CommandHSWaiting(Module *creator) : Command(creator, "hostserv/waiting", 0, 0)
	{
		this->SetDesc(_("Retrieves the vhost requests"));
		this->SetSyntax("");
	}

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

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_("This command retrieves the vhost requests"));

		return true;
	}
};

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

 public:
	HSRequest(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
		request_type("HostRequest", HostRequest::unserialize), commandhsrequest(this), commandhsactive(this), commandhsreject(this), commandhswaiting(this)
	{
		this->SetAuthor("Anope");

		Implementation i[] = { I_OnReload };
		ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));

		this->OnReload();
	}

	~HSRequest()
	{
		for (nickalias_map::const_iterator it = NickAliasList.begin(), it_end = NickAliasList.end(); it != it_end; ++it)
			it->second->Shrink("hs_request");
	}

	void OnReload()
	{
		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(CommandSource &source, 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 && memoserv)
		for (unsigned i = 0; i < Config->Opers.size(); ++i)
		{
			Oper *o = Config->Opers[i];
			
			NickAlias *na = findnick(o->name);
			if (!na)
				continue;

			Anope::string message = Anope::printf(_("[auto memo] vHost \002%s\002 has been requested by %s."), host.c_str(), source.u->GetMask().c_str());

			memoserv->Send(Config->HostServ, na->nick, message, true);
		}
}

MODULE_INIT(HSRequest)