Debian Codegraph @

modules/chanserv/set_mlock.c

raw
1 /*
2 * Copyright (c) 2003-2004 E. Will et al.
3 * Copyright (c) 2006-2010 Atheme Development Group
4 * Rights to this code are documented in doc/LICENSE.
5 *
6 * This file contains routines to handle the CService SET MLOCK command.
7 *
8 */
9
10 #include "atheme.h"
11
12 DECLARE_MODULE_V1
13 (
14 "chanserv/set_mlock", false, _modinit, _moddeinit,
15 PACKAGE_STRING,
16 VENDOR_STRING
17 );
18
19 static void cs_cmd_set_mlock(sourceinfo_t *si, int parc, char *parv[]);
20
21 command_t cs_set_mlock = { "MLOCK", N_("Sets channel mode lock."), AC_NONE, 2, cs_cmd_set_mlock, { .path = "cservice/set_mlock" } };
22
23 mowgli_patricia_t **cs_set_cmdtree;
24
25 void _modinit(module_t *m)
26 {
27 MODULE_TRY_REQUEST_SYMBOL(m, cs_set_cmdtree, "chanserv/set_core", "cs_set_cmdtree");
28
29 command_add(&cs_set_mlock, *cs_set_cmdtree);
30 }
31
32 void _moddeinit(module_unload_intent_t intent)
33 {
34 command_delete(&cs_set_mlock, *cs_set_cmdtree);
35 }
36
37 static void cs_cmd_set_mlock(sourceinfo_t *si, int parc, char *parv[])
38 {
39 mychan_t *mc;
40 char modebuf[32], *end, c;
41 int dir = MTYPE_NUL;
42 int newlock_on = 0, newlock_off = 0, newlock_limit = 0, flag = 0;
43 unsigned int mask, changed;
44 bool mask_ext;
45 char newlock_key[KEYLEN];
46 char newlock_ext[ignore_mode_list_size][512];
47 bool newlock_ext_off[ignore_mode_list_size];
48 char newext[512];
49 char ext_plus[ignore_mode_list_size + 1];
50 char ext_minus[ignore_mode_list_size + 1];
51 size_t i;
52 char *letters = strtok(parv[1], " ");
53 char *arg;
54 metadata_t *md;
55
56 if (!(mc = mychan_find(parv[0])))
57 {
58 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), parv[0]);
59 return;
60 }
61
62 if (!chanacs_source_has_flag(mc, si, CA_SET))
63 {
64 if (ircd->oper_only_modes == 0 ||
65 !has_priv(si, PRIV_CHAN_CMODES) ||
66 !has_priv(si, PRIV_CHAN_ADMIN))
67 {
68 command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
69 return;
70 }
71 mask = ~ircd->oper_only_modes;
72 mask_ext = true;
73 }
74 else
75 {
76 mask = has_priv(si, PRIV_CHAN_CMODES) ? 0 : ircd->oper_only_modes;
77 mask_ext = false;
78
79 }
80
81 for (i = 0; i < ignore_mode_list_size; i++)
82 {
83 newlock_ext[i][0] = '\0';
84 newlock_ext_off[i] = false;
85 }
86 newlock_key[0] = '\0';
87
88 while (letters && *letters)
89 {
90 if (*letters != '+' && *letters != '-' && dir == MTYPE_NUL)
91 {
92 letters++;
93 continue;
94 }
95
96 switch ((c = *letters++))
97 {
98 case '+':
99 dir = MTYPE_ADD;
100 break;
101
102 case '-':
103 dir = MTYPE_DEL;
104 break;
105
106 case 'k':
107 if (dir == MTYPE_ADD)
108 {
109 arg = strtok(NULL, " ");
110 if (!arg)
111 {
112 command_fail(si, fault_badparams, _("You need to specify a value for mode +%c."), 'k');
113 return;
114 }
115 else if (strlen(arg) >= KEYLEN)
116 {
117 command_fail(si, fault_badparams, _("MLOCK key is too long (%d > %d)."), (int)strlen(arg), KEYLEN - 1);
118 return;
119 }
120 else if (strchr(arg, ',') || arg[0] == ':')
121 {
122 command_fail(si, fault_badparams, _("MLOCK key contains invalid characters."));
123 return;
124 }
125
126 mowgli_strlcpy(newlock_key, arg, sizeof newlock_key);
127 newlock_off &= ~CMODE_KEY;
128 }
129 else
130 {
131 newlock_key[0] = '\0';
132 newlock_off |= CMODE_KEY;
133 }
134
135 break;
136
137 case 'l':
138 if (dir == MTYPE_ADD)
139 {
140 arg = strtok(NULL, " ");
141 if(!arg)
142 {
143 command_fail(si, fault_badparams, _("You need to specify a value for mode +%c."), 'l');
144 return;
145 }
146
147 if (atol(arg) <= 0)
148 {
149 command_fail(si, fault_badparams, _("You must specify a positive integer for limit."));
150 return;
151 }
152
153 newlock_limit = atol(arg);
154 newlock_off &= ~CMODE_LIMIT;
155 }
156 else
157 {
158 newlock_limit = 0;
159 newlock_off |= CMODE_LIMIT;
160 }
161
162 break;
163
164 default:
165 flag = mode_to_flag(c);
166
167 if (flag)
168 {
169 if (dir == MTYPE_ADD)
170 newlock_on |= flag, newlock_off &= ~flag;
171 else
172 newlock_off |= flag, newlock_on &= ~flag;
173 break;
174 }
175
176 for (i = 0; ignore_mode_list[i].mode != '\0'; i++)
177 {
178 if (c == ignore_mode_list[i].mode)
179 {
180 if (dir == MTYPE_ADD)
181 {
182 arg = strtok(NULL, " ");
183 if(!arg)
184 {
185 command_fail(si, fault_badparams, _("You need to specify a value for mode +%c."), c);
186 return;
187 }
188 if (strlen(arg) > 350)
189 {
190 command_fail(si, fault_badparams, _("Invalid value \2%s\2 for mode +%c."), arg, c);
191 return;
192 }
193 if ((mc->chan == NULL || mc->chan->extmodes[i] == NULL || strcmp(mc->chan->extmodes[i], arg)) && !ignore_mode_list[i].check(arg, mc->chan, mc, si->su, si->smu))
194 {
195 command_fail(si, fault_badparams, _("Invalid value \2%s\2 for mode +%c."), arg, c);
196 return;
197 }
198 mowgli_strlcpy(newlock_ext[i], arg, sizeof newlock_ext[i]);
199 newlock_ext_off[i] = false;
200 }
201 else
202 {
203 newlock_ext[i][0] = '\0';
204 newlock_ext_off[i] = true;
205 }
206 }
207 }
208 }
209 }
210
211 /* note: the following does not treat +lk and extmodes correctly */
212 changed = ((newlock_on ^ mc->mlock_on) | (newlock_off ^ mc->mlock_off));
213 changed &= ~mask;
214 /* if they're only allowed to alter oper only modes, require
215 * them to actually change such modes -- jilles */
216 if (!changed && mask_ext)
217 {
218 command_fail(si, fault_noprivs, _("You may only alter \2+%s\2 modes."), flags_to_string(~mask));
219 return;
220 }
221
222 /* save it to mychan */
223 /* leave the modes in mask unchanged -- jilles */
224 mc->mlock_on = (newlock_on & ~mask) | (mc->mlock_on & mask);
225 mc->mlock_off = (newlock_off & ~mask) | (mc->mlock_off & mask);
226 if (!(mask & CMODE_LIMIT))
227 mc->mlock_limit = newlock_limit;
228 if (!(mask & CMODE_KEY))
229 {
230 free(mc->mlock_key);
231 mc->mlock_key = *newlock_key != '\0' ? sstrdup(newlock_key) : NULL;
232 }
233
234 ext_plus[0] = '\0';
235 ext_minus[0] = '\0';
236 if (mask_ext)
237 {
238 md = metadata_find(mc, "private:mlockext");
239 if (md != NULL)
240 {
241 arg = md->value;
242 while (*arg != '\0')
243 {
244 modebuf[0] = *arg;
245 modebuf[1] = '\0';
246 mowgli_strlcat(arg[1] == ' ' || arg[1] == '\0' ? ext_minus : ext_plus, modebuf, ignore_mode_list_size + 1);
247 arg++;
248 while (*arg != ' ' && *arg != '\0')
249 arg++;
250 while (*arg == ' ')
251 arg++;
252 }
253 }
254 }
255 else
256 {
257 newext[0] = '\0';
258 for (i = 0; i < ignore_mode_list_size; i++)
259 {
260 if (newlock_ext[i][0] != '\0' || newlock_ext_off[i])
261 {
262 if (*newext != '\0')
263 {
264 modebuf[0] = ' ';
265 modebuf[1] = '\0';
266 mowgli_strlcat(newext, modebuf, sizeof newext);
267 }
268 modebuf[0] = ignore_mode_list[i].mode;
269 modebuf[1] = '\0';
270 mowgli_strlcat(newext, modebuf, sizeof newext);
271 mowgli_strlcat(newlock_ext_off[i] ? ext_minus : ext_plus,
272 modebuf, ignore_mode_list_size + 1);
273 if (!newlock_ext_off[i])
274 mowgli_strlcat(newext, newlock_ext[i], sizeof newext);
275 }
276 }
277 if (newext[0] != '\0')
278 metadata_add(mc, "private:mlockext", newext);
279 else
280 metadata_delete(mc, "private:mlockext");
281 }
282
283 end = modebuf;
284 *end = 0;
285
286 if (mc->mlock_on || mc->mlock_key || mc->mlock_limit || *ext_plus)
287 end += snprintf(end, sizeof(modebuf) - (end - modebuf), "+%s%s%s%s", flags_to_string(mc->mlock_on), mc->mlock_key ? "k" : "", mc->mlock_limit ? "l" : "", ext_plus);
288
289 if (mc->mlock_off || *ext_minus)
290 end += snprintf(end, sizeof(modebuf) - (end - modebuf), "-%s%s%s%s", flags_to_string(mc->mlock_off), mc->mlock_off & CMODE_KEY ? "k" : "", mc->mlock_off & CMODE_LIMIT ? "l" : "", ext_minus);
291
292 if (*modebuf)
293 {
294 command_success_nodata(si, _("The MLOCK for \2%s\2 has been set to \2%s\2."), mc->name, modebuf);
295 logcommand(si, CMDLOG_SET, "SET:MLOCK: \2%s\2 to \2%s\2", mc->name, modebuf);
296 verbose(mc, _("\2%s\2 set the mode lock to \2%s\2"), get_source_name(si), modebuf);
297 }
298 else
299 {
300 command_success_nodata(si, _("The MLOCK for \2%s\2 has been removed."), mc->name);
301 logcommand(si, CMDLOG_SET, "SET:MLOCK:NONE: \2%s\2", mc->name);
302 }
303 if (changed & ircd->oper_only_modes)
304 logcommand(si, CMDLOG_SET, _("SET:MLOCK: \2%s\2 to \2%s\2 by \2%s\2"), mc->name, *modebuf != '\0' ? modebuf : "+", get_oper_name(si));
305
306 check_modes(mc, true);
307 if (mc->chan != NULL)
308 mlock_sts(mc->chan);
309
310 return;
311 }
312
313 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
314 * vim:ts=8
315 * vim:sw=8
316 * vim:noexpandtab
317 */