ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/chanserv/template.C
Revision: 1.4
Committed: Sun Jul 22 18:38:43 2007 UTC (19 years, 4 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +3 -3 lines
Log Message:
-fixed help
- temporarily work around corruption in jsonrpc

File Contents

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