aboutsummaryrefslogtreecommitdiffstats
path: root/modules/groupserv/fflags.c
blob: e9061f93ccacddf040025832ccc9f4df046853bd (about) (plain) (blame)
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
/*
 * Copyright (c) 2005 Atheme Development Group
 * Copyright (c) 2016-2017 ChatLounge IRC Network Development Team
 *     (http://www.chatlounge.net/)
 * Rights to this code are documented in doc/LICENSE.
 *
 * This file contains routines to handle the GroupServ FFLAGS command.
 *
 */

#include "atheme.h"
#include "groupserv.h"
#include "../hostserv/hostserv.h"

DECLARE_MODULE_V1
(
	"groupserv/fflags", false, _modinit, _moddeinit,
	PACKAGE_STRING,
	VENDOR_STRING
);

static unsigned int (*get_hostsvs_req_time)(void) = NULL;
static bool *(*get_hostsvs_limit_first_req)(void) = NULL;

static void gs_cmd_fflags(sourceinfo_t *si, int parc, char *parv[]);

command_t gs_fflags = { "FFLAGS", N_("Forces a flag change on a user in a group."), PRIV_GROUP_ADMIN, 3, gs_cmd_fflags, { .path = "groupserv/fflags" } };

static void gs_cmd_fflags(sourceinfo_t *si, int parc, char *parv[])
{
	mowgli_node_t *n;
	mygroup_t *mg;
	myentity_t *mt;
	groupacs_t *ga;
	unsigned int flags = 0, oldflags = 0, addflags = 0, removeflags = 0;

	if (!parv[0] || !parv[1] || !parv[2])
	{
		command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "FFLAGS");
		command_fail(si, fault_needmoreparams, _("Syntax: FFLAGS <!group> [user] [changes]"));
		return;
	}

	if ((mg = mygroup_find(parv[0])) == NULL)
	{
		command_fail(si, fault_nosuch_target, _("The group \2%s\2 does not exist."), parv[0]);
		return;
	}

	if (si->smu == NULL)
	{
		command_fail(si, fault_noprivs, _("You are not logged in."));
		return;
	}

	if ((mt = myentity_find_ext(parv[1])) == NULL)
	{
		command_fail(si, fault_nosuch_target, _("\2%s\2 is not a registered account."), parv[1]);
		return;
	}

	ga = groupacs_find(mg, mt, 0, false);
	if (ga != NULL)
		flags = ga->flags;

	oldflags = flags;

	if (strchr(parv[2], '+') || strchr(parv[2], '-') || strchr(parv[2], '='))
		flags = gs_flags_parser(parv[2], 1, flags);
	else
	{
		flags = get_group_template_flags(mg, parv[2]);
		if (flags == 0)
		{
			command_fail(si, fault_badparams, _("Invalid template name on the group: %s"), entity(mg)->name);
			return;
		}
	}

	if (!(flags & GA_FOUNDER) && groupacs_find(mg, mt, GA_FOUNDER, false))
	{
		if (mygroup_count_flag(mg, GA_FOUNDER) == 1)
		{
			command_fail(si, fault_noprivs, _("You may not remove the last founder."));
			return;
		}

		if (!groupacs_sourceinfo_has_flag(mg, si, GA_FOUNDER))
		{
			command_fail(si, fault_noprivs, _("You may not remove a founder's +F access."));
			return;
		}
	}

	if (ga != NULL && flags != 0)
	{
		if (ga->flags != flags)
			ga->flags = flags;
		else
		{
			command_fail(si, fault_nochange, _("Group \2%s\2 access for \2%s\2 unchanged."), entity(mg)->name, mt->name);
			return;
		}
	}
	else if (ga != NULL)
	{
		metadata_t *md;

		groupacs_delete(mg, mt);

		addflags = flags;
		removeflags = GA_ALL_ALL & ~addflags;
		addflags &= ~oldflags;
		removeflags &= oldflags & ~addflags;

		command_success_nodata(si, _("\2%s\2 has been removed from \2%s\2."), mt->name, entity(mg)->name);
		wallops("\2%s\2 is removing flags for \2%s\2 on \2%s\2", get_oper_name(si), mt->name, entity(mg)->name);
		logcommand(si, CMDLOG_ADMIN, "FFLAGS:REMOVE: \2%s\2 on \2%s\2", mt->name, entity(mg)->name);

		if (isuser(mt))
		{
			notify_target_acl_change(si, user(mt), mg,
				bitmask_to_gflags2(addflags, removeflags), ga->flags);
			notify_group_acl_change(si, user(mt), mg,
				bitmask_to_gflags2(addflags, removeflags), ga->flags);
		}

		if (isuser(mt) && module_locate_symbol("hostserv/main", "get_hostsvs_req_time") &&
			(get_group_template_vhost_by_flags(mg, oldflags)) != NULL &&
			(md = metadata_find(mt, "private:usercloak")) != NULL)
		{
			char templatevhost[128];

			myuser_t *mu = myuser_find_by_nick(parv[1]);

			mowgli_strlcpy(templatevhost, get_group_template_vhost_by_flags(mg, oldflags), BUFSIZE);

			if (strstr(templatevhost, "$account"))
				replace(templatevhost, BUFSIZE, "$account", entity(mt)->name);

			if (!strcasecmp(md->value, templatevhost))
			{
				hs_sethost_all(mu, NULL, get_source_name(si));
				// Send notice/memo to affected user.
				logcommand(si, CMDLOG_ADMIN, "VHOST:REMOVE: \2%s\2 by virtue of flags change on: \2%s\2", entity(mt)->name, entity(mg)->name);
				do_sethost_all(mu, NULL); // restore user vhost from user host
			}
		}

		return;
	}
	else if (flags != 0)
	{
		if (MOWGLI_LIST_LENGTH(&mg->acs) > gs_config->maxgroupacs && (!(mg->flags & MG_ACSNOLIMIT)))
		{
			command_fail(si, fault_toomany, _("Group %s access list is full."), entity(mg)->name);
			return;
		}
		ga = groupacs_add(mg, mt, flags);
	}
	else
	{
		command_fail(si, fault_nochange, _("Group \2%s\2 access for \2%s\2 unchanged."), entity(mg)->name, mt->name);
		return;
	}

	/* Remove the user's vhost setting, if the user's flags are changed or removed.
	 * However, change the user's vhost setting, *if* the user is being granted flags that
	 * match another TEMPLATEVHOST offer, *and* he previously held a group template vhost offer,
	 * and the vhost cooldown for the user has passed.
	 */

	get_hostsvs_req_time = module_locate_symbol("hostserv/main", "get_hostsvs_req_time");
	get_hostsvs_limit_first_req = module_locate_symbol("hostserv/main", "get_hostsvs_limit_first_req");

	if (get_hostsvs_req_time != NULL && get_hostsvs_limit_first_req != NULL)
	{
		if (isuser(mt) && (get_group_template_vhost_by_flags(mg, oldflags)) != NULL)
		{
			mowgli_node_t *o;
			bool matches = false;
			myuser_t *mu = myuser_find_by_nick(parv[1]);
			bool limit_first_req = get_hostsvs_limit_first_req();
			unsigned int request_time = get_hostsvs_req_time();

			MOWGLI_ITER_FOREACH(n, mu->nicks.head)
			{
				char templatevhost[128];
				metadata_t *md;

				if ((md = metadata_find(mt, "private:usercloak")) == NULL)
					break;

				mowgli_strlcpy(templatevhost, get_group_template_vhost_by_flags(mg, oldflags), BUFSIZE);

				if (strstr(templatevhost, "$account"))
						replace(templatevhost, BUFSIZE, "$account", entity(mt)->name);

				if (!strcasecmp(md->value, templatevhost))
				{
					if (!has_priv(si, PRIV_USER_VHOSTOVERRIDE) && !has_priv(si, PRIV_GROUP_ADMIN) && !has_priv(si, PRIV_ADMIN))
					{
						metadata_t *md_vhosttime = metadata_find(mu, "private:usercloak-timestamp");

						/* 86,400 seconds per day */
						if (limit_first_req && md_vhosttime == NULL && (CURRTIME - mu->registered > (request_time * 86400)))
						{
							hs_sethost_all(mu, NULL, get_source_name(si));
							// Send notice/memo to affected user.
							logcommand(si, CMDLOG_ADMIN, "VHOST:REMOVE: \2%s\2 by virtue of early flags change on: \2%s\2", entity(mt)->name, entity(mg)->name);
							do_sethost_all(mu, NULL); // restore user vhost from user host
							break;
						}

						time_t vhosttime = atoi(md_vhosttime->value);

						if (vhosttime + (request_time * 86400) > CURRTIME)
						{
							hs_sethost_all(mu, NULL, get_source_name(si));
							// Send notice/memo to affected user.
							logcommand(si, CMDLOG_ADMIN, "VHOST:REMOVE: \2%s\2 by virtue of early flags change on: \2%s\2", entity(mt)->name, entity(mg)->name);
							do_sethost_all(mu, NULL); // restore user vhost from user host
							break;
						}
					}

					// Check if the new flags have a vhost offer.

					if (get_group_template_vhost_by_flags(mg, flags))
					{
						char newtemplatevhost[128];
						mowgli_strlcpy(newtemplatevhost, get_group_template_vhost_by_flags(mg, flags), BUFSIZE);

						if (strstr(newtemplatevhost, "$account"))
							replace(newtemplatevhost, BUFSIZE, "$account", entity(mt)->name);

						hs_sethost_all(mu, newtemplatevhost, get_source_name(si));
						do_sethost_all(mu, newtemplatevhost);
						// Send notice/memo to affected user.
						logcommand(si, CMDLOG_ADMIN, "VHOST:CHANGE: from \2%s\2 to \2%s\2 on \2%s\2 by virtue of flags change on: \2%s\2",
							templatevhost,
							newtemplatevhost,
							entity(mt)->name,
							entity(mg)->name);

						matches = true;
						break;
					}
				}

				if (matches)
					break;
			}
		}
	}

	addflags = flags;
	removeflags = GA_ALL_ALL & ~addflags;
	addflags &= ~oldflags;
	removeflags &= oldflags & ~addflags;

	MOWGLI_ITER_FOREACH(n, entity(mg)->chanacs.head)
	{
		chanacs_t *ca = n->data;

		verbose(ca->mychan, "\2%s\2 now has flags \2%s\2 in the group \2%s\2 which communally has \2%s\2 on \2%s\2.",
			mt->name, gflags_tostr(ga_flags, ga->flags), entity(mg)->name,
			bitmask_to_flags(ca->level), ca->mychan->name);

		hook_call_channel_acl_change(&(hook_channel_acl_req_t){ .ca = ca });
	}

	if (get_group_template_name(mg, ga->flags))
	{
		wallops("\2%s\2 is modifying flags (\2%s\2) for \2%s\2 (TEMPLATE: \2%s\2) on \2%s\2", get_oper_name(si), gflags_tostr(ga_flags, ga->flags),
			mt->name, get_group_template_name(mg, ga->flags), entity(mg)->name);
		logcommand(si, CMDLOG_ADMIN, "FFLAGS: \2%s\2 now has flags \2%s\2 (TEMPLATE: \2%s\2) on \2%s\2", mt->name, gflags_tostr(ga_flags,  ga->flags),
			get_group_template_name(mg, ga->flags), entity(mg)->name);
		command_success_nodata(si, _("\2%s\2 now has flags \2%s\2 (TEMPLATE: \2%s\2) on: \2%s\2"),
			mt->name, gflags_tostr(ga_flags, ga->flags),
			get_group_template_name(mg, ga->flags), entity(mg)->name);
	}
	else
	{
		wallops("\2%s\2 is modifying flags (\2%s\2) for \2%s\2 on \2%s\2", get_oper_name(si), gflags_tostr(ga_flags, ga->flags), mt->name, entity(mg)->name);
		logcommand(si, CMDLOG_ADMIN, "FFLAGS: \2%s\2 now has flags \2%s\2 on \2%s\2", mt->name, gflags_tostr(ga_flags,  ga->flags), entity(mg)->name);
		command_success_nodata(si, _("\2%s\2 now has flags \2%s\2 on: \2%s\2"), mt->name, gflags_tostr(ga_flags, ga->flags), entity(mg)->name);
	}

	if (isuser(mt))
	{
		notify_target_acl_change(si, user(mt), mg,
			bitmask_to_gflags2(addflags, removeflags), ga->flags);
		notify_group_acl_change(si, user(mt), mg,
			bitmask_to_gflags2(addflags, removeflags), ga->flags);
	}
}

void _modinit(module_t *m)
{
	use_groupserv_main_symbols(m);

	service_named_bind_command("groupserv", &gs_fflags);
}

void _moddeinit(module_unload_intent_t intent)
{
	service_named_unbind_command("groupserv", &gs_fflags);
}