ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/chanserv/template.C
Revision: 1.7
Committed: Wed Sep 5 11:23:13 2007 UTC (19 years, 3 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.6: +3 -3 lines
Log Message:
removed GPLed code and put license back to BSD

File Contents

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