ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/chanserv/template.C
Revision: 1.8
Committed: Sun Sep 16 18:54:43 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.7: +10 -5 lines
Log Message:
#defines to enum

File Contents

# User Rev Content
1 pippijn 1.8 /**
2     * template.C: This file contains code for the ChanServ TEMPLATE functions.
3     *
4     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5     * Rights to this code are as documented in COPYING.
6     *
7     *
8     * Portions of this file were derived from sources bearing the following license:
9 pippijn 1.5 * Copyright © 2005-2006 Jilles Tjoelker, et al.
10 pippijn 1.2 * Rights to this code are as documented in doc/pod/license.pod.
11 pippijn 1.1 *
12 pippijn 1.8 * $Id: template.C,v 1.7 2007-09-05 11:23:13 pippijn Exp $
13 pippijn 1.1 */
14    
15     #include "atheme.h"
16 pippijn 1.5 #include <ermyth/module.h>
17 pippijn 1.1 #include <account/myuser.h>
18     #include <account/mychan.h>
19     #include <account/chanacs.h>
20     #include "template.h"
21    
22 pippijn 1.8 static char const rcsid[] = "$Id: template.C,v 1.7 2007-09-05 11:23:13 pippijn Exp $";
23 pippijn 1.1
24 pippijn 1.5 REGISTER_MODULE ("chanserv/template", false, "The Ermyth Team <http://ermyth.schmorp.de>");
25 pippijn 1.1
26     static void list_generic_flags (sourceinfo_t *si);
27    
28     static void cs_cmd_template (sourceinfo_t *si, int parc, char *parv[]);
29    
30 pippijn 1.5 command_t const cs_flags = { "TEMPLATE", N_("Manipulates predefined sets of flags."), AC_NONE, 3, cs_cmd_template };
31 pippijn 1.1
32 pippijn 1.5 E cmdvec cs_cmdtree;
33     E helpvec cs_helptree;
34 pippijn 1.1
35 pippijn 1.5 bool
36     _modinit (module *m)
37 pippijn 1.1 {
38     cs_cmdtree << cs_flags;
39 pippijn 1.4 help_addentry (cs_helptree, "TEMPLATE", "help/chanserv/template", NULL);
40 pippijn 1.5
41     return true;
42 pippijn 1.1 }
43    
44     void
45     _moddeinit ()
46     {
47     cs_cmdtree >> cs_flags;
48     help_delentry (cs_helptree, "TEMPLATE");
49     }
50    
51     static void
52     list_generic_flags (sourceinfo_t *si)
53     {
54     command_success_nodata (si, "%-20s %s", _("Name"), _("Flags"));
55     command_success_nodata (si, "%-20s %s", "--------------------", "-----");
56     command_success_nodata (si, "%-20s %s", "SOP", bitmask_to_flags (chansvs.ca_sop, chanacs_flags));
57     command_success_nodata (si, "%-20s %s", "AOP", bitmask_to_flags (chansvs.ca_aop, chanacs_flags));
58     if (chansvs.ca_hop != chansvs.ca_vop)
59     command_success_nodata (si, "%-20s %s", "HOP", bitmask_to_flags (chansvs.ca_hop, chanacs_flags));
60     command_success_nodata (si, "%-20s %s", "VOP", bitmask_to_flags (chansvs.ca_vop, chanacs_flags));
61     command_success_nodata (si, "%-20s %s", "--------------------", "-----");
62     command_success_nodata (si, _("End of network wide template list."));
63     }
64    
65     /* TEMPLATE [channel] [template] [flags] */
66     static void
67     cs_cmd_template (sourceinfo_t *si, int parc, char *parv[])
68     {
69 pippijn 1.7 metadata *md;
70 pippijn 1.1 int operoverride = 0, changechanacs = 0, l;
71     char *channel = parv[0];
72     char *target = parv[1];
73 pippijn 1.6 mychan_t *mc = mychan_t::find (channel);
74 pippijn 1.1 unsigned int oldflags, newflags = 0, addflags, removeflags, restrictflags;
75     char *p, *q, *r;
76     char ss[40], newstr[400];
77     bool found, denied;
78    
79     if (!channel)
80     {
81     list_generic_flags (si);
82     logcommand (si, CMDLOG_GET, "TEMPLATE");
83     return;
84     }
85    
86 pippijn 1.6 mc = mychan_t::find (channel);
87 pippijn 1.1 if (!mc)
88     {
89 pippijn 1.6 command_fail (si, fault::nosuch_target, _("\2%s\2 is not registered."), channel);
90 pippijn 1.1 return;
91     }
92    
93     if (!target)
94     {
95     if (!chanacs_source_has_flag (mc, si, CA_ACLVIEW))
96 pippijn 1.5 {
97     if (has_priv (si, PRIV_CHAN_AUSPEX))
98     operoverride = 1;
99     else
100     {
101 pippijn 1.6 command_fail (si, fault::noprivs, _("You are not authorized to perform this operation."));
102 pippijn 1.5 return;
103     }
104     }
105 pippijn 1.1
106     if (mc->find_metadata ("private:close:closer") && !has_priv (si, PRIV_CHAN_AUSPEX))
107 pippijn 1.5 {
108 pippijn 1.6 command_fail (si, fault::noprivs, _("\2%s\2 is closed."), channel);
109 pippijn 1.5 return;
110     }
111 pippijn 1.1
112     md = mc->find_metadata ("private:templates");
113    
114     if (md != NULL)
115 pippijn 1.5 {
116     command_success_nodata (si, "%-20s %s", _("Name"), _("Flags"));
117     command_success_nodata (si, "%-20s %s", "--------------------", "-----");
118    
119     p = md->value;
120     while (p != NULL)
121     {
122     while (*p == ' ')
123     p++;
124     q = strchr (p, '=');
125     if (q == NULL)
126     break;
127     r = strchr (q, ' ');
128     command_success_nodata (si, "%-20.*s %.*s", (q - p), p, r != NULL ? (r - q - 1) : (int) strlen (q + 1), q + 1);
129     p = r;
130     }
131    
132     command_success_nodata (si, "%-20s %s", "--------------------", "-----");
133     command_success_nodata (si, _("End of \2%s\2 TEMPLATE listing."), mc->name);
134     }
135 pippijn 1.1 else
136 pippijn 1.5 command_success_nodata (si, _("No templates set on channel \2%s\2."), mc->name);
137 pippijn 1.1 if (operoverride)
138 pippijn 1.5 logcommand (si, CMDLOG_ADMIN, "%s TEMPLATE (oper override)", mc->name);
139 pippijn 1.1 else
140 pippijn 1.5 logcommand (si, CMDLOG_GET, "%s TEMPLATE", mc->name);
141 pippijn 1.1 }
142     else
143     {
144     char *flagstr = parv[2];
145    
146     if (!si->smu)
147 pippijn 1.5 {
148 pippijn 1.6 command_fail (si, fault::noprivs, _("You are not logged in."));
149 pippijn 1.5 return;
150     }
151 pippijn 1.1
152     /* probably no need to special-case founder here -- jilles */
153 pippijn 1.5 /* founder may always set flags -- jilles */
154     restrictflags = chanacs_source_flags (mc, si);
155     if (restrictflags & CA_FOUNDER)
156     restrictflags = ca_all;
157 pippijn 1.1 else
158 pippijn 1.5 {
159     if (!(restrictflags & CA_FLAGS))
160     {
161 pippijn 1.6 command_fail (si, fault::noprivs, _("You are not authorized to execute this command."));
162 pippijn 1.5 return;
163     }
164     restrictflags = allow_flags (restrictflags);
165     }
166 pippijn 1.1
167     if (mc->find_metadata ("private:close:closer"))
168 pippijn 1.5 {
169 pippijn 1.6 command_fail (si, fault::noprivs, _("\2%s\2 is closed."), channel);
170 pippijn 1.5 return;
171     }
172 pippijn 1.1
173     if (!target || !flagstr)
174 pippijn 1.5 {
175 pippijn 1.6 command_fail (si, fault::needmoreparams, _("Usage: TEMPLATE %s [target flags]"), channel);
176 pippijn 1.5 return;
177     }
178 pippijn 1.1
179     if (*target == '+' || *target == '-' || *target == '=')
180 pippijn 1.5 {
181 pippijn 1.6 command_fail (si, fault::badparams, _("Invalid template name \2%s\2."), target);
182 pippijn 1.5 return;
183     }
184 pippijn 1.1 l = strlen (target);
185    
186     if (*flagstr == '!' && (flagstr[1] == '+' || flagstr[1] == '-' || flagstr[1] == '='))
187 pippijn 1.5 {
188     changechanacs = 1;
189     flagstr++;
190     }
191 pippijn 1.1
192     if (*flagstr == '+' || *flagstr == '-' || *flagstr == '=')
193 pippijn 1.5 {
194     flags_make_bitmasks (flagstr, chanacs_flags, &addflags, &removeflags);
195     if (addflags == 0 && removeflags == 0)
196     {
197 pippijn 1.6 command_fail (si, fault::badparams, _("No valid flags given, use /%s%s HELP FLAGS for a list"), ircd->uses_rcommand ? "" : "msg ", chansvs.disp);
198 pippijn 1.5 return;
199     }
200     }
201 pippijn 1.1 else
202 pippijn 1.5 {
203     /* allow copying templates as well */
204     addflags = get_template_flags (mc, flagstr);
205     if (addflags == 0)
206     {
207 pippijn 1.6 command_fail (si, fault::nosuch_key, _("Invalid template name given, use /%s%s TEMPLATE %s for a list"), ircd->uses_rcommand ? "" : "msg ", chansvs.disp, mc->name);
208 pippijn 1.5 return;
209     }
210     removeflags = ca_all & ~addflags;
211     }
212    
213     /* if adding +F, also add +f */
214     if (addflags & CA_FOUNDER)
215     addflags |= CA_FLAGS, removeflags &= ~CA_FLAGS;
216     /* if removing +f, also remove +F */
217     else if (removeflags & CA_FLAGS)
218     removeflags |= CA_FOUNDER, addflags &= ~CA_FOUNDER;
219 pippijn 1.1
220     found = denied = false;
221     oldflags = 0;
222    
223     md = mc->find_metadata ("private:templates");
224     if (md != NULL)
225 pippijn 1.5 {
226     p = md->value;
227     strlcpy (newstr, p, sizeof newstr);
228     while (p != NULL)
229     {
230     while (*p == ' ')
231     p++;
232     q = strchr (p, '=');
233     if (q == NULL)
234     break;
235     r = strchr (q, ' ');
236     if (r != NULL && r < q)
237     break;
238     strlcpy (ss, q, sizeof ss);
239     if (r != NULL && r - q < (int) (sizeof ss - 1))
240     {
241     ss[r - q] = '\0';
242     }
243     if (q - p == l && !strncasecmp (target, p, q - p))
244     {
245     found = true;
246     oldflags = flags_to_bitmask (ss, chanacs_flags, 0);
247     addflags &= ~oldflags;
248     removeflags &= oldflags & ~addflags;
249     /* no change? */
250     if ((addflags | removeflags) == 0)
251     break;
252     /* attempting to add bad flag? */
253     /* attempting to remove bad flag? */
254     /* attempting to manipulate something with more privs? */
255     if (~restrictflags & addflags || ~restrictflags & removeflags || ~restrictflags & oldflags)
256     {
257     denied = true;
258     break;
259     }
260     newflags = (oldflags | addflags) & ~removeflags;
261     if (newflags == 0)
262     {
263     if (p == md->value)
264     /* removing first entry,
265     * zap the space after it */
266     strlcpy (newstr, r != NULL ? r + 1 : "", sizeof newstr);
267     else
268     {
269     /* otherwise, zap the space before it */
270     p--;
271     strlcpy (newstr + (p - md->value), r != NULL ? r : "", sizeof newstr - (p - md->value));
272     }
273     }
274     else
275     snprintf (newstr + (p - md->value), sizeof newstr - (p - md->value), "%s=%s%s", target, bitmask_to_flags (newflags, chanacs_flags), r != NULL ? r : "");
276     break;
277     }
278     p = r;
279     }
280     }
281 pippijn 1.1 if (!found)
282 pippijn 1.5 {
283     if (l == 3 && (!strcasecmp (target, "SOP") || !strcasecmp (target, "AOP") || !strcasecmp (target, "HOP") || !strcasecmp (target, "VOP")))
284     {
285     oldflags = get_template_flags (NULL, target);
286     addflags &= ~oldflags;
287     removeflags &= oldflags & ~addflags;
288     newflags = (oldflags | addflags) & ~removeflags;
289     if (newflags == 0)
290     removeflags = 0;
291     if ((addflags | removeflags) != 0)
292     command_success_nodata (si, _("Redefining built-in template \2%s\2."), target);
293     }
294     else
295     {
296     removeflags = 0;
297     newflags = addflags;
298     }
299     if ((addflags | removeflags) == 0)
300     ;
301     else if (~restrictflags & addflags || ~restrictflags & removeflags || ~restrictflags & oldflags)
302     denied = true;
303     else if (md != NULL)
304     snprintf (newstr + strlen (newstr), sizeof newstr - strlen (newstr), " %s=%s", target, bitmask_to_flags (newflags, chanacs_flags));
305     else
306     snprintf (newstr, sizeof newstr, "%s=%s", target, bitmask_to_flags (newflags, chanacs_flags));
307     }
308 pippijn 1.1 if ((addflags | removeflags) == 0)
309 pippijn 1.5 {
310     if (oldflags != 0)
311 pippijn 1.6 command_fail (si, fault::nochange, _("Template \2%s\2 on \2%s\2 unchanged."), target, channel);
312 pippijn 1.5 else
313 pippijn 1.6 command_fail (si, fault::nosuch_key, _("No such template \2%s\2 on \2%s\2."), target, channel);
314 pippijn 1.5 return;
315     }
316 pippijn 1.1 if (denied)
317 pippijn 1.5 {
318 pippijn 1.6 command_fail (si, fault::noprivs, _("You are not allowed to set \2%s\2 on template \2%s\2 in \2%s\2."), bitmask_to_flags2 (addflags, removeflags, chanacs_flags), target, mc->name);
319 pippijn 1.5 return;
320     }
321 pippijn 1.1 if (strlen (newstr) >= 300)
322 pippijn 1.5 {
323 pippijn 1.6 command_fail (si, fault::toomany, _("Sorry, too many templates on \2%s\2."), channel);
324 pippijn 1.5 return;
325     }
326 pippijn 1.1 if (newstr[0] == '\0')
327 pippijn 1.5 mc->del_metadata ("private:templates");
328 pippijn 1.1 else
329 pippijn 1.5 mc->add_metadata ("private:templates", newstr);
330 pippijn 1.1 if (oldflags == 0)
331 pippijn 1.5 command_success_nodata (si, _("Added template \2%s\2 with flags \2%s\2 in \2%s\2."), target, bitmask_to_flags (newflags, chanacs_flags), channel);
332 pippijn 1.1 else if (newflags == 0)
333 pippijn 1.5 command_success_nodata (si, _("Removed template \2%s\2 from \2%s\2."), target, channel);
334 pippijn 1.1 else
335 pippijn 1.5 command_success_nodata (si, _("Changed template \2%s\2 to \2%s\2 in \2%s\2."), target, bitmask_to_flags (newflags, chanacs_flags), channel);
336 pippijn 1.1
337     flagstr = bitmask_to_flags2 (addflags, removeflags, chanacs_flags);
338     if (changechanacs)
339 pippijn 1.5 {
340     node_t *n, *tn;
341     chanacs_t *ca;
342     int changes = 0, founderskipped = 0;
343     char flagstr2[128];
344    
345     LIST_FOREACH_SAFE (n, tn, mc->chanacs.head)
346     {
347     ca = static_cast<chanacs_t *> (n->data);
348     if (ca->level != oldflags)
349     continue;
350     if ((addflags | removeflags) & CA_FOUNDER)
351     {
352     founderskipped++;
353     continue;
354     }
355     changes++;
356     chanacs_modify_simple (ca, newflags, ~newflags);
357     chanacs_close (ca);
358     }
359     logcommand (si, CMDLOG_SET, "%s TEMPLATE %s !%s (%d changes)", mc->name, target, flagstr, changes);
360     strlcpy (flagstr2, flagstr, sizeof flagstr2);
361     if (changes > 0)
362     verbose (mc, "\2%s\2 set \2%s\2 on %d access entries with flags \2%s\2.", get_source_name (si), flagstr2, changes, bitmask_to_flags (oldflags, chanacs_flags));
363     command_success_nodata (si, _("%d access entries updated accordingly."), changes);
364     if (founderskipped)
365     command_success_nodata (si, _("Not updating %d access entries involving founder status. Please do it manually."), founderskipped);
366     }
367 pippijn 1.1 else
368 pippijn 1.5 logcommand (si, CMDLOG_SET, "%s TEMPLATE %s %s", mc->name, target, flagstr);
369 pippijn 1.1 /*verbose(mc, "Flags \2%s\2 were set on template \2%s\2 in \2%s\2.", flagstr, target, channel); */
370     }
371     }