]>
Commit | Line | Data |
---|---|---|
1 | /* modcmd.c - Generalized module command support | |
2 | * Copyright 2002-2004 srvx Development Team | |
3 | * | |
4 | * This file is part of x3. | |
5 | * | |
6 | * x3 is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #include "../ChangeLog" | |
22 | #include "chanserv.h" | |
23 | #include "conf.h" | |
24 | #include "modcmd.h" | |
25 | #include "opserv.h" | |
26 | #include "saxdb.h" | |
27 | #include "timeq.h" | |
28 | ||
29 | struct pending_template { | |
30 | struct svccmd *cmd; | |
31 | char *base; | |
32 | struct pending_template *next; | |
33 | }; | |
34 | ||
35 | extern unsigned long god_timeout; | |
36 | static struct dict *modules; | |
37 | static struct dict *services; | |
38 | static struct pending_template *pending_templates; | |
39 | static struct module *modcmd_module; | |
40 | static struct modcmd *bind_command, *help_command, *version_command; | |
41 | static const struct message_entry msgtab[] = { | |
42 | { "MCMSG_BARE_FLAG", "Flag %.*s must be preceded by a + or -." }, | |
43 | { "MCMSG_UNKNOWN_FLAG", "Unknown module flag %.*s." }, | |
44 | { "MCMSG_BAD_OPSERV_LEVEL", "Invalid $O access level %s." }, | |
45 | { "MCMSG_BAD_CHANSERV_LEVEL", "Invalid $C access level %s." }, | |
46 | { "MCMSG_LEVEL_TOO_LOW", "You cannot set the access requirements for %s (your level is too low.)" }, | |
47 | { "MCMSG_LEVEL_TOO_HIGH", "You cannot set the access requirements to %s (that is too high)." }, | |
48 | { "MCMSG_BAD_OPTION", "Unknown option %s." }, | |
49 | { "MCMSG_MUST_QUALIFY", "You $bMUST$b \"/msg %s@$s %s\" (not just /msg %s)." }, | |
50 | { "MCMSG_ACCOUNT_SUSPENDED", "Your account has been suspended." }, | |
51 | { "MCMSG_CHAN_NOT_REGISTERED", "%s has not been registered with $C." }, | |
52 | { "MCMSG_CHAN_SUSPENDED", "$b$C$b access to $b%s$b has been temporarily suspended (%s)." }, | |
53 | { "MCMSG_NO_CHANNEL_ACCESS", "You lack access to %s." }, | |
54 | { "MCMSG_LOW_CHANNEL_ACCESS", "You lack sufficient access in %s to use this command." }, | |
55 | { "MCMSG_REQUIRES_JOINABLE", "You must be in %s (or on its userlist) to use this command." }, | |
56 | { "MCMSG_MUST_BE_HELPING", "You must have security override (helping mode) on to use this command." }, | |
57 | { "MCMSG_MISSING_COMMAND", "You must specify a command as well as a channel." }, | |
58 | { "MCMSG_NO_CHANNEL_BEFORE", "You may not give a channel name before this command." }, | |
59 | { "MCMSG_NO_PLUS_CHANNEL", "You may not use a +channel with this command." }, | |
60 | { "MCMSG_COMMAND_ALIASES", "%s is an alias for: %s" }, | |
61 | { "MCMSG_HELP_COMMAND_ALIAS", "$uAlias for:$u %s" }, | |
62 | { "MCMSG_HELP_COMMAND_HEADER", "Command help for: $b%s$b" }, | |
63 | { "MCMSG_HELP_COMMAND_UNKNOWN", "No help available for that command." }, | |
64 | { "MCMSG_HELP_TOPIC_HEADER", "Help topic: $b%s$b" }, | |
65 | { "MCMSG_HELP_DIVIDER", "=---------------------------------------=" }, | |
66 | { "MCMSG_HELP_FOOTER", "=------------- End of Help -------------=" }, | |
67 | { "MCMSG_COMMAND_BINDING", "%s is a binding of: %s" }, | |
68 | { "MCMSG_ALIAS_ERROR", "Error in alias expansion for %s; check the error log for details." }, | |
69 | { "MCMSG_INTERNAL_COMMAND", "$b%s$b is an internal command and cannot be called directly; please check command bindings." }, | |
70 | { "MCMSG_UNKNOWN_MODULE", "Unknown module %s." }, | |
71 | { "MCMSG_UNKNOWN_SERVICE", "Unknown service %s." }, | |
72 | { "MCMSG_ALREADY_BOUND", "%s already has a command bound as %s." }, | |
73 | { "MCMSG_UNKNOWN_COMMAND_2", "Unknown command name %s (relative to service %s)." }, | |
74 | { "MCMSG_COMMAND_MODIFIED", "Option $b%s$b for $b%s$b has been set." }, | |
75 | { "MCMSG_INSPECTION_REFUSED", "You do not have access to inspect command %s." }, | |
76 | { "MCMSG_CANNOT_DOUBLE_ALIAS", "You cannot bind to a complex (argument-carrying) bind." }, | |
77 | { "MCMSG_BAD_ALIAS_ARGUMENT", "Invalid alias argument $b%s$b." }, | |
78 | { "MCMSG_COMMAND_BOUND", "New command %s bound to %s." }, | |
79 | { "MCMSG_MODULE_BOUND", "Bound %d commands from %s to %s." }, | |
80 | { "MCMSG_NO_COMMAND_BOUND", "%s has nothing bound as command %s." }, | |
81 | { "MCMSG_UNBIND_PROHIBITED", "It wouldn't be very much fun to unbind the last %s command, now would it?" }, | |
82 | { "MCMSG_COMMAND_UNBOUND", "Unbound command %s from %s." }, | |
83 | { "MCMSG_HELPFILE_UNBOUND", "Since that was the last command from module %s on the service, the helpfile for %s was removed." }, | |
84 | { "MCMSG_NO_HELPFILE", "Module %s does not have a help file." }, | |
85 | { "MCMSG_HELPFILE_ERROR", "Syntax error reading %s; help contents not changed." }, | |
86 | { "MCMSG_HELPFILE_READ", "Read %s help database in "FMT_TIME_T".%03lu seconds." }, | |
87 | { "MCMSG_COMMAND_TIME", "Command $b%s$b finished in "FMT_TIME_T".%06lu seconds." }, | |
88 | { "MCMSG_NEED_OPSERV_LEVEL", "You must have $O access of at least $b%u$b." }, | |
89 | { "MCMSG_NEED_CHANSERV_LEVEL", "You must have $C access of at least $b%u$b in the channel." }, | |
90 | { "MCMSG_NEED_ACCOUNT_FLAGS", "You must have account flags $b%s$b." }, | |
91 | { "MCMSG_NEED_NOTHING", "Anyone may use the $b%s$b command." }, | |
92 | { "MCMSG_NEED_STAFF_ACCESS", "You must be network staff." }, | |
93 | { "MCMSG_NEED_STAFF_OPER", "You must be an IRC operator." }, | |
94 | { "MCMSG_NEED_STAFF_NETHELPER", "You must be a network helper." }, | |
95 | { "MCMSG_NEED_STAFF_NETHELPER_OR_OPER", "You must be a network helper or IRC operator." }, | |
96 | { "MCMSG_NEED_STAFF_SHELPER", "You must be a support helper." }, | |
97 | { "MCMSG_NEED_STAFF_SHELPER_OR_OPER", "You must be a support helper or IRC operator." }, | |
98 | { "MCMSG_NEED_STAFF_HELPER", "You must be a network or support helper." }, | |
99 | { "MCMSG_NEED_JOINABLE", "The channel must be open or you must be in the channel or on its userlist." }, | |
100 | { "MCMSG_NEED_CHANUSER_CSUSPENDABLE", "You must be on the channel's userlist, and the channel can be suspended." }, | |
101 | { "MCMSG_NEED_CHANUSER", "You must be on the channel's userlist." }, | |
102 | { "MCMSG_NEED_REGCHAN", "You must specify a channel registered with $C." }, | |
103 | { "MCMSG_NEED_CHANNEL", "You must specify a channel that exists." }, | |
104 | { "MCMSG_NEED_AUTHED", "You must be authenticated with $N." }, | |
105 | { "MCMSG_IS_TOY", "$b%s$b is a toy command." }, | |
106 | { "MCMSG_END_REQUIREMENTS", "End of requirements for $b%s$b." }, | |
107 | { "MCMSG_ALREADY_HELPING", "You already have security override enabled." }, | |
108 | { "MCMSG_ALREADY_NOT_HELPING", "You already have security override disabled." }, | |
109 | { "MCMSG_NOW_HELPING", "Security override has been enabled." }, | |
110 | { "MCMSG_NOW_NOT_HELPING", "Security override has been disabled." }, | |
111 | { "MCMSG_JOINER_CHOICES", "Subcommands of %s: %s" }, | |
112 | { "MCMSG_MODULE_INFO", "Commands exported by module $b%s$b:" }, | |
113 | { "MCMSG_SERVICE_INFO", "Commands bound to service $b%s$b:" }, | |
114 | { "MCMSG_TOYS_DISABLED", "Toys are disabled in %s." }, | |
115 | { "MCMSG_PUBLIC_DENY", "Public commands in $b%s$b are restricted." }, | |
116 | { "MCMSG_HELPFILE_SEQUENCE", "Help priority %d: %s" }, | |
117 | { "MCMSG_HELPFILE_SEQUENCE_SET", "Set helpfile priority sequence for %s." }, | |
118 | { "MCMSG_BAD_SERVICE_NICK", "$b%s$b is an invalid nickname." }, | |
119 | { "MCMSG_ALREADY_SERVICE", "$b%s$b is already a service." }, | |
120 | { "MCMSG_NEW_SERVICE", "Added new service bot $b%s$b." }, | |
121 | { "MCMSG_SERVICE_RENAMED", "Service renamed to $b%s$b." }, | |
122 | { "MCMSG_NO_TRIGGER", "$b%s$b does not have an in-channel trigger." }, | |
123 | { "MCMSG_REMOVED_TRIGGER", "Removed trigger from $b%s$b." }, | |
124 | { "MCMSG_DUPLICATE_TRIGGER", "$b%s$b already uses trigger $b%c$b." }, | |
125 | { "MCMSG_CURRENT_TRIGGER", "Trigger for $b%s$b is $b%c$b." }, | |
126 | { "MCMSG_NEW_TRIGGER", "Changed trigger for $b%s$b to $b%c$b." }, | |
127 | { "MCMSG_SERVICE_PRIVILEGED", "Service $b%s$b privileged: $b%s$b." }, | |
128 | { "MCMSG_SERVICE_REMOVED", "Service $b%s$b has been deleted." }, | |
129 | { "MCMSG_FILE_NOT_OPENED", "Unable to open file $b%s$b for writing." }, | |
130 | { "MCMSG_MESSAGES_DUMPED", "Messages written to $b%s$b." }, | |
131 | { "MCMSG_MESSAGE_DUMP_FAILED", "Message dump failed: %s." }, | |
132 | { "MCMSG_COMMAND_FLAGS", "Command flags are %s (inferred: %s)." }, | |
133 | { "MCMSG_COMMAND_ACCOUNT_FLAGS", "Requires account flags +%s, prohibits account flags +%s." }, | |
134 | { "MCMSG_COMMAND_ACCESS_LEVEL", "Requires channel access %d and $O access %d." }, | |
135 | { "MCMSG_COMMAND_USES", "%s has been used %d times." }, | |
136 | { "MCMSG_GOD_EXPIRED", "Security override expired." }, | |
137 | { NULL, NULL } | |
138 | }; | |
139 | struct userData *_GetChannelUser(struct chanData *channel, struct handle_info *handle, int override, int allow_suspended); | |
140 | ||
141 | #define ACTION_ALLOW 1 | |
142 | #define ACTION_OVERRIDE 2 | |
143 | #define ACTION_NOCHANNEL 4 | |
144 | #define ACTION_STAFF 8 | |
145 | ||
146 | #define RESOLVE_DEPTH 4 | |
147 | ||
148 | static struct modcmd_flag { | |
149 | const char *name; | |
150 | unsigned int flag; | |
151 | } flags[] = { | |
152 | { "acceptchan", MODCMD_ACCEPT_CHANNEL }, | |
153 | { "acceptpluschan", MODCMD_ACCEPT_PCHANNEL }, | |
154 | { "authed", MODCMD_REQUIRE_AUTHED }, | |
155 | { "channel", MODCMD_REQUIRE_CHANNEL }, | |
156 | { "chanuser", MODCMD_REQUIRE_CHANUSER }, | |
157 | { "disabled", MODCMD_DISABLED }, | |
158 | { "helping", MODCMD_REQUIRE_HELPING }, | |
159 | { "ignore_csuspend", MODCMD_IGNORE_CSUSPEND }, | |
160 | { "joinable", MODCMD_REQUIRE_JOINABLE }, | |
161 | { "keepbound", MODCMD_KEEP_BOUND }, | |
162 | { "loghostmask", MODCMD_LOG_HOSTMASK }, | |
163 | { "networkhelper", MODCMD_REQUIRE_NETWORK_HELPER }, | |
164 | { "never_csuspend", MODCMD_NEVER_CSUSPEND }, | |
165 | { "nolog", MODCMD_NO_LOG }, | |
166 | { "oper", MODCMD_REQUIRE_OPER }, | |
167 | { "qualified", MODCMD_REQUIRE_QUALIFIED }, | |
168 | { "regchan", MODCMD_REQUIRE_REGCHAN }, | |
169 | { "supporthelper", MODCMD_REQUIRE_SUPPORT_HELPER }, | |
170 | { "toy", MODCMD_TOY }, | |
171 | { NULL, 0 } | |
172 | }; | |
173 | ||
174 | static int | |
175 | flags_bsearch(const void *a, const void *b) { | |
176 | const char *key = a; | |
177 | const struct modcmd_flag *flag = b; | |
178 | return ircncasecmp(key, flag->name, strlen(flag->name)); | |
179 | } | |
180 | ||
181 | static int | |
182 | flags_qsort(const void *a, const void *b) { | |
183 | const struct modcmd_flag *fa = a, *fb = b; | |
184 | return irccasecmp(fa->name, fb->name); | |
185 | } | |
186 | ||
187 | DEFINE_LIST(svccmd_list, struct svccmd*); | |
188 | DEFINE_LIST(module_list, struct module*); | |
189 | ||
190 | static void | |
191 | free_service_command(void *data) { | |
192 | struct svccmd *svccmd; | |
193 | unsigned int nn; | |
194 | ||
195 | svccmd = data; | |
196 | if (svccmd->alias.used) { | |
197 | for (nn=0; nn<svccmd->alias.used; ++nn) | |
198 | free(svccmd->alias.list[nn]); | |
199 | free(svccmd->alias.list); | |
200 | } | |
201 | free(svccmd->name); | |
202 | free(svccmd); | |
203 | } | |
204 | ||
205 | static void | |
206 | free_service(void *data) { | |
207 | struct service *service = data; | |
208 | dict_delete(service->commands); | |
209 | module_list_clean(&service->modules); | |
210 | free(service); | |
211 | } | |
212 | ||
213 | static void | |
214 | free_module_command(void *data) { | |
215 | struct modcmd *modcmd = data; | |
216 | free_service_command(modcmd->defaults); | |
217 | free(modcmd->name); | |
218 | free(modcmd); | |
219 | } | |
220 | ||
221 | static void | |
222 | free_module(void *data) { | |
223 | struct module *module = data; | |
224 | dict_delete(module->commands); | |
225 | close_helpfile(module->helpfile); | |
226 | free(module->name); | |
227 | free(module); | |
228 | } | |
229 | ||
230 | struct module * | |
231 | module_register(const char *name, struct log_type *clog, const char *helpfile_name, expand_func_t expand_help) { | |
232 | struct module *newmod; | |
233 | ||
234 | newmod = calloc(1, sizeof(*newmod)); | |
235 | newmod->name = strdup(name); | |
236 | newmod->commands = dict_new(); | |
237 | dict_set_free_data(newmod->commands, free_module_command); | |
238 | newmod->clog = clog; | |
239 | newmod->helpfile_name = helpfile_name; | |
240 | newmod->expand_help = expand_help; | |
241 | if (newmod->helpfile_name) { | |
242 | newmod->helpfile = open_helpfile(newmod->helpfile_name, newmod->expand_help); | |
243 | } | |
244 | dict_insert(modules, newmod->name, newmod); | |
245 | return newmod; | |
246 | } | |
247 | ||
248 | struct module * | |
249 | module_find(const char *name) { | |
250 | return dict_find(modules, name, NULL); | |
251 | } | |
252 | ||
253 | static void | |
254 | add_pending_template(struct svccmd *cmd, const char *target) { | |
255 | struct pending_template *pending = calloc(1, sizeof(*pending)); | |
256 | pending->cmd = cmd; | |
257 | pending->base = strdup(target); | |
258 | pending->next = pending_templates; | |
259 | pending_templates = pending; | |
260 | } | |
261 | ||
262 | struct svccmd * | |
263 | svccmd_resolve_name(struct svccmd *origin, const char *name) { | |
264 | char *sep, svcname[MAXLEN]; | |
265 | ||
266 | if ((sep = strchr(name, '.'))) { | |
267 | memcpy(svcname, name, sep-name); | |
268 | svcname[sep-name] = 0; | |
269 | name = sep + 1; | |
270 | if (svcname[0] == '*') { | |
271 | struct module *module = module_find(svcname+1); | |
272 | struct modcmd *cmd = module ? dict_find(module->commands, name, NULL) : NULL; | |
273 | return cmd ? cmd->defaults : NULL; | |
274 | } else { | |
275 | struct service *service = service_find(svcname); | |
276 | return service ? dict_find(service->commands, name, NULL) : NULL; | |
277 | } | |
278 | } else { | |
279 | if (origin->parent) { | |
280 | return dict_find(origin->parent->commands, name, NULL); | |
281 | } else { | |
282 | struct modcmd *cmd = dict_find(origin->command->parent->commands, name, NULL); | |
283 | return cmd ? cmd->defaults : NULL; | |
284 | } | |
285 | } | |
286 | } | |
287 | ||
288 | static void | |
289 | modcmd_set_effective_flags(struct svccmd *cmd) { | |
290 | int flags = cmd->flags | cmd->command->flags; | |
291 | if (cmd->min_opserv_level > 0) | |
292 | flags |= MODCMD_REQUIRE_OPER; | |
293 | if (cmd->min_channel_access > 0) | |
294 | flags |= MODCMD_REQUIRE_CHANUSER; | |
295 | if (flags & MODCMD_REQUIRE_CHANUSER) | |
296 | flags |= MODCMD_REQUIRE_REGCHAN; | |
297 | if (flags & MODCMD_REQUIRE_REGCHAN) | |
298 | flags |= MODCMD_REQUIRE_CHANNEL; | |
299 | if (flags & (MODCMD_REQUIRE_STAFF|MODCMD_REQUIRE_HELPING)) | |
300 | flags |= MODCMD_REQUIRE_AUTHED; | |
301 | cmd->effective_flags = flags; | |
302 | } | |
303 | ||
304 | static void | |
305 | svccmd_copy_rules(struct svccmd *dest, struct svccmd *src) { | |
306 | dest->flags |= src->flags; | |
307 | dest->req_account_flags |= src->req_account_flags; | |
308 | dest->deny_account_flags |= src->deny_account_flags; | |
309 | if (src->min_opserv_level > dest->min_opserv_level) | |
310 | dest->min_opserv_level = src->min_opserv_level; | |
311 | if (src->min_channel_access > dest->min_channel_access) | |
312 | dest->min_channel_access = src->min_channel_access; | |
313 | modcmd_set_effective_flags(dest); | |
314 | } | |
315 | ||
316 | static int | |
317 | svccmd_configure(struct svccmd *cmd, struct userNode *user, struct userNode *bot, const char *param, const char *value) { | |
318 | if (!irccasecmp(param, "flags")) { | |
319 | unsigned int set_flags, rem_flags; | |
320 | struct modcmd_flag *flag; | |
321 | int opt, end; | |
322 | ||
323 | for (set_flags = rem_flags = 0; 1; value += end) { | |
324 | end = strcspn(value, ","); | |
325 | if (*value == '+') | |
326 | opt = 1; | |
327 | else if (*value == '-') | |
328 | opt = 0; | |
329 | else { | |
330 | if (user) | |
331 | send_message(user, bot, "MCMSG_BARE_FLAG", end, value); | |
332 | else | |
333 | log_module(MAIN_LOG, LOG_ERROR, "Flag %.*s must be preceded by a + or - (for command %s).", end, value, cmd->name); | |
334 | return 0; | |
335 | } | |
336 | value++; | |
337 | flag = bsearch(value, flags, ArrayLength(flags)-1, sizeof(flags[0]), flags_bsearch); | |
338 | if (!flag) { | |
339 | if (user) | |
340 | send_message(user, bot, "MCMSG_UNKNOWN_FLAG", end, value); | |
341 | else | |
342 | log_module(MAIN_LOG, LOG_ERROR, "Unknown module flag %.*s (for command %s).", end, value, cmd->name); | |
343 | return 0; | |
344 | } | |
345 | if (opt) | |
346 | set_flags |= flag->flag, rem_flags &= ~flag->flag; | |
347 | else | |
348 | rem_flags |= flag->flag, set_flags &= ~flag->flag; | |
349 | if (!value[end-1]) | |
350 | break; | |
351 | } | |
352 | cmd->flags = (cmd->flags | set_flags) & ~rem_flags; | |
353 | return 1; | |
354 | } else if (!irccasecmp(param, "channel_level") || !irccasecmp(param, "channel_access") || !irccasecmp(param, "access")) { | |
355 | unsigned short ul; | |
356 | if (!irccasecmp(value, "none") || !irccasecmp(value, "0")) { | |
357 | cmd->min_channel_access = 0; | |
358 | return 1; | |
359 | } else if ((ul = user_level_from_name(value, UL_OWNER)) > 0) { | |
360 | cmd->min_channel_access = ul; | |
361 | return 1; | |
362 | } else if (user) { | |
363 | send_message(user, bot, "MCMSG_BAD_CHANSERV_LEVEL", value); | |
364 | return 0; | |
365 | } else { | |
366 | log_module(MAIN_LOG, LOG_ERROR, "Invalid ChanServ access level %s (for command %s).", value, cmd->name); | |
367 | return 0; | |
368 | } | |
369 | } else if (!irccasecmp(param, "oper_level") || !irccasecmp(param, "oper_access") || !irccasecmp(param, "level")) { | |
370 | unsigned int newval = atoi(value); | |
371 | if (!isdigit(value[0]) || (newval > 1000)) { | |
372 | if (user) | |
373 | send_message(user, bot, "MCMSG_BAD_OPSERV_LEVEL", value); | |
374 | else | |
375 | log_module(MAIN_LOG, LOG_ERROR, "Invalid OpServ access level %s (for command %s).", value, cmd->name); | |
376 | return 0; | |
377 | } | |
378 | if (user && (!user->handle_info || (cmd->min_opserv_level > user->handle_info->opserv_level))) { | |
379 | send_message(user, bot, "MCMSG_LEVEL_TOO_LOW", cmd->name); | |
380 | return 0; | |
381 | } | |
382 | if (user && (!user->handle_info || (newval > user->handle_info->opserv_level))) { | |
383 | send_message(user, bot, "MCMSG_LEVEL_TOO_HIGH", value); | |
384 | return 0; | |
385 | } | |
386 | cmd->min_opserv_level = newval; | |
387 | return 1; | |
388 | } else if (!irccasecmp(param, "account_flags")) { | |
389 | return nickserv_modify_handle_flags(user, bot, value, &cmd->req_account_flags, &cmd->deny_account_flags); | |
390 | } else { | |
391 | if (user) | |
392 | send_message(user, bot, "MCMSG_BAD_OPTION", param); | |
393 | else | |
394 | log_module(MAIN_LOG, LOG_ERROR, "Unknown option %s (for command %s).", param, cmd->name); | |
395 | return 0; | |
396 | } | |
397 | } | |
398 | ||
399 | struct modcmd * | |
400 | modcmd_register(struct module *module, const char *name, modcmd_func_t func, unsigned int min_argc, unsigned int flags, ...) { | |
401 | struct modcmd *newcmd; | |
402 | va_list args; | |
403 | const char *param, *value; | |
404 | ||
405 | newcmd = calloc(1, sizeof(*newcmd)); | |
406 | newcmd->name = strdup(name); | |
407 | newcmd->parent = module; | |
408 | newcmd->func = func; | |
409 | newcmd->min_argc = min_argc; | |
410 | newcmd->flags = flags; | |
411 | newcmd->defaults = calloc(1, sizeof(*newcmd->defaults)); | |
412 | newcmd->defaults->name = strdup(newcmd->name); | |
413 | newcmd->defaults->command = newcmd; | |
414 | dict_insert(module->commands, newcmd->name, newcmd); | |
415 | if (newcmd->flags & (MODCMD_REQUIRE_REGCHAN|MODCMD_REQUIRE_CHANNEL|MODCMD_REQUIRE_CHANUSER|MODCMD_REQUIRE_JOINABLE)) { | |
416 | newcmd->defaults->flags |= MODCMD_ACCEPT_CHANNEL; | |
417 | } | |
418 | if (newcmd->flags & MODCMD_REQUIRE_STAFF) { | |
419 | newcmd->defaults->flags |= MODCMD_REQUIRE_AUTHED; | |
420 | } | |
421 | va_start(args, flags); | |
422 | while ((param = va_arg(args, const char*))) { | |
423 | value = va_arg(args, const char*); | |
424 | if (!irccasecmp(param, "template")) { | |
425 | struct svccmd *svccmd = svccmd_resolve_name(newcmd->defaults, value); | |
426 | if (svccmd) { | |
427 | svccmd_copy_rules(newcmd->defaults, svccmd); | |
428 | } else { | |
429 | log_module(MAIN_LOG, LOG_ERROR, "Unable to resolve template name %s for %s.%s.", value, newcmd->parent->name, newcmd->name); | |
430 | } | |
431 | add_pending_template(newcmd->defaults, value); | |
432 | } else { | |
433 | svccmd_configure(newcmd->defaults, NULL, NULL, param, value); | |
434 | } | |
435 | } | |
436 | modcmd_set_effective_flags(newcmd->defaults); | |
437 | va_end(args); | |
438 | return newcmd; | |
439 | } | |
440 | ||
441 | /* This is kind of a lame hack, but it is actually simpler than having | |
442 | * the permission check vary based on the command itself, or having a | |
443 | * more generic rule system. | |
444 | */ | |
445 | int | |
446 | svccmd_can_invoke(struct userNode *user, struct userNode *bot, struct svccmd *cmd, struct chanNode *channel, int options) { | |
447 | unsigned int uData_checked = 0; | |
448 | struct userData *uData = NULL; | |
449 | int rflags = 0, flags = cmd->effective_flags; | |
450 | ||
451 | if (flags & MODCMD_DISABLED) { | |
452 | if (options & SVCCMD_NOISY) | |
453 | send_message(user, bot, "MSG_COMMAND_DISABLED", cmd->name); | |
454 | return 0; | |
455 | } | |
456 | if ((flags & MODCMD_REQUIRE_QUALIFIED) && !(options & SVCCMD_QUALIFIED)) { | |
457 | if (options & SVCCMD_NOISY) | |
458 | send_message(user, bot, "MCMSG_MUST_QUALIFY", bot->nick, cmd->name, bot->nick); | |
459 | return 0; | |
460 | } | |
461 | if (flags & MODCMD_REQUIRE_AUTHED) { | |
462 | if (!user->handle_info) { | |
463 | if (options & SVCCMD_NOISY) | |
464 | send_message(user, bot, "MSG_AUTHENTICATE"); | |
465 | return 0; | |
466 | } | |
467 | if (HANDLE_FLAGGED(user->handle_info, SUSPENDED)) { | |
468 | if (options & SVCCMD_NOISY) | |
469 | send_message(user, bot, "MCMSG_ACCOUNT_SUSPENDED"); | |
470 | return 0; | |
471 | } | |
472 | } | |
473 | if (channel || (options & SVCCMD_NOISY)) { | |
474 | if ((flags & MODCMD_REQUIRE_CHANNEL) && !channel) { | |
475 | if (options & SVCCMD_NOISY) | |
476 | send_message(user, bot, "MSG_INVALID_CHANNEL"); | |
477 | return 0; | |
478 | } | |
479 | if (flags & MODCMD_REQUIRE_REGCHAN) { | |
480 | if (!channel->channel_info) { | |
481 | if (options & SVCCMD_NOISY) | |
482 | send_message(user, bot, "MCMSG_CHAN_NOT_REGISTERED", channel->name); | |
483 | return 0; | |
484 | } else if (IsSuspended(channel->channel_info) && !(flags & MODCMD_IGNORE_CSUSPEND)) { | |
485 | /* allow security-override users to always ignore channel suspensions, but flag it as a staff command */ | |
486 | if (!user->handle_info | |
487 | || !HANDLE_FLAGGED(user->handle_info, HELPING) | |
488 | || (flags & MODCMD_NEVER_CSUSPEND)) { | |
489 | if (options & SVCCMD_NOISY) | |
490 | send_message(user, bot, "MCMSG_CHAN_SUSPENDED", channel->name, channel->channel_info->suspended->reason); | |
491 | return 0; | |
492 | } | |
493 | rflags |= ACTION_STAFF; | |
494 | } | |
495 | } | |
496 | if (flags & MODCMD_REQUIRE_CHANUSER) { | |
497 | if (!uData_checked) | |
498 | uData = _GetChannelUser(channel->channel_info, user->handle_info, 1, 0), uData_checked = 1; | |
499 | if (!uData) { | |
500 | if (options & SVCCMD_NOISY) | |
501 | send_message(user, bot, "MCMSG_NO_CHANNEL_ACCESS", channel->name); | |
502 | return 0; | |
503 | } else if (uData->access < cmd->min_channel_access) { | |
504 | if (options & SVCCMD_NOISY) | |
505 | send_message(user, bot, "MCMSG_LOW_CHANNEL_ACCESS", channel->name); | |
506 | return 0; | |
507 | } | |
508 | } | |
509 | if ((flags & MODCMD_REQUIRE_JOINABLE) && channel) { | |
510 | if (!uData_checked) | |
511 | uData = _GetChannelUser(channel->channel_info, user->handle_info, 1, 0), uData_checked = 1; | |
512 | if ((channel->modes & (MODE_INVITEONLY|MODE_KEY|MODE_SECRET)) | |
513 | && !uData | |
514 | && !IsService(user) | |
515 | && !GetUserMode(channel, user)) { | |
516 | if (options & SVCCMD_NOISY) | |
517 | send_message(user, bot, "MCMSG_REQUIRES_JOINABLE", channel->name); | |
518 | return 0; | |
519 | } | |
520 | } | |
521 | if ((flags & MODCMD_TOY) && channel) { | |
522 | if (!channel->channel_info) | |
523 | rflags |= ACTION_NOCHANNEL; | |
524 | else switch (channel->channel_info->chOpts[chToys]) { | |
525 | case 'd': | |
526 | if (options & SVCCMD_NOISY) | |
527 | send_message(user, bot, "MCMSG_TOYS_DISABLED", channel->name); | |
528 | return 0; | |
529 | case 'n': | |
530 | rflags |= ACTION_NOCHANNEL; | |
531 | break; | |
532 | case 'p': | |
533 | break; | |
534 | } | |
535 | } | |
536 | } | |
537 | if (flags & MODCMD_REQUIRE_STAFF) { | |
538 | if (((flags & MODCMD_REQUIRE_OPER) && IsOper(user)) | |
539 | || ((flags & MODCMD_REQUIRE_NETWORK_HELPER) && IsNetworkHelper(user)) | |
540 | || ((flags & MODCMD_REQUIRE_SUPPORT_HELPER) && IsSupportHelper(user))) { | |
541 | /* allow it */ | |
542 | rflags |= ACTION_STAFF; | |
543 | } else { | |
544 | if (options & SVCCMD_NOISY) | |
545 | send_message(user, bot, "MSG_COMMAND_PRIVILEGED", cmd->name); | |
546 | return 0; | |
547 | } | |
548 | } | |
549 | if (flags & MODCMD_REQUIRE_HELPING) { | |
550 | if (!HANDLE_FLAGGED(user->handle_info, HELPING)) { | |
551 | if (options & SVCCMD_NOISY) | |
552 | send_message(user, bot, "MCMSG_MUST_BE_HELPING"); | |
553 | return 0; | |
554 | } | |
555 | rflags |= ACTION_STAFF; | |
556 | } | |
557 | if (cmd->min_opserv_level > 0) { | |
558 | if (!oper_has_access(user, bot, cmd->min_opserv_level, !(options & SVCCMD_NOISY))) | |
559 | return 0; | |
560 | rflags |= ACTION_STAFF; | |
561 | } | |
562 | if (cmd->req_account_flags || cmd->deny_account_flags) { | |
563 | if (!user->handle_info) { | |
564 | if (options & SVCCMD_NOISY) | |
565 | send_message(user, bot, "MSG_AUTHENTICATE"); | |
566 | return 0; | |
567 | } | |
568 | /* Do we want separate or different messages here? */ | |
569 | if ((cmd->req_account_flags & ~user->handle_info->flags) | |
570 | || (cmd->deny_account_flags & user->handle_info->flags)) { | |
571 | if (options & SVCCMD_NOISY) | |
572 | send_message(user, bot, "MSG_COMMAND_PRIVILEGED", cmd->name); | |
573 | return 0; | |
574 | } | |
575 | } | |
576 | ||
577 | /* If it's an override, return a special value. */ | |
578 | if ((flags & MODCMD_REQUIRE_CHANUSER) | |
579 | && (options & SVCCMD_NOISY) | |
580 | && (!uData || (uData->access > 500)) | |
581 | && (!(uData = _GetChannelUser(channel->channel_info, user->handle_info, 0, 0)) | |
582 | || uData->access < cmd->min_channel_access) | |
583 | && !(flags & (MODCMD_REQUIRE_STAFF|MODCMD_REQUIRE_HELPING))) { | |
584 | rflags |= ACTION_OVERRIDE; | |
585 | } | |
586 | return rflags | ACTION_ALLOW; | |
587 | } | |
588 | ||
589 | static int | |
590 | svccmd_expand_alias(struct svccmd *cmd, struct userNode *user, unsigned int old_argc, char *old_argv[], char *new_argv[]) { | |
591 | unsigned int ii, new_argc; | |
592 | char *arg; | |
593 | ||
594 | for (ii=new_argc=0; ii<cmd->alias.used; ++ii) { | |
595 | arg = cmd->alias.list[ii]; | |
596 | if (arg[0] != '$') { | |
597 | new_argv[new_argc++] = arg; | |
598 | continue; | |
599 | } | |
600 | if (arg[1] == '$') { | |
601 | new_argv[new_argc++] = arg + 1; | |
602 | } else if (isdigit(arg[1])) { | |
603 | unsigned int lbound, ubound, jj; | |
604 | char *end_num; | |
605 | ||
606 | lbound = strtoul(arg+1, &end_num, 10); | |
607 | switch (end_num[0]) { | |
608 | case 0: ubound = lbound; break; | |
609 | case '-': | |
610 | if (end_num[1] == 0) { | |
611 | ubound = old_argc - 1; | |
612 | break; | |
613 | } else if (isdigit(end_num[1])) { | |
614 | ubound = strtoul(end_num+1, NULL, 10); | |
615 | break; | |
616 | } | |
617 | /* else fall through to default case */ | |
618 | default: | |
619 | log_module(MAIN_LOG, LOG_ERROR, "Alias expansion parse error in %s (near %s; %s.%s arg %d).", arg, end_num, cmd->parent->bot->nick, cmd->name, ii); | |
620 | return 0; | |
621 | } | |
622 | if (ubound >= old_argc) | |
623 | ubound = old_argc - 1; | |
624 | if (lbound < old_argc) | |
625 | for (jj = lbound; jj <= ubound; ) | |
626 | new_argv[new_argc++] = old_argv[jj++]; | |
627 | } else { | |
628 | switch(arg[1]) { | |
629 | case 'a': | |
630 | new_argv[new_argc++] = (user && user->handle_info) ? user->handle_info->handle : "(account)"; | |
631 | break; | |
632 | case 'n': | |
633 | new_argv[new_argc++] = user ? user->nick : "(nick)"; | |
634 | break; | |
635 | case 'm': | |
636 | #ifdef WITH_PROTOCOL_P10 | |
637 | new_argv[new_argc++] = user ? user->numeric : "(numnick)"; | |
638 | #else | |
639 | new_argv[new_argc++] = "(This ircd protocol has no numnicks!)"; | |
640 | #endif | |
641 | break; | |
642 | default: | |
643 | log_module(MAIN_LOG, LOG_ERROR, "Alias expansion: I do not know how to handle %s (%s.%s arg %d).", arg, cmd->parent->bot->nick, cmd->name, ii); | |
644 | return 0; | |
645 | } | |
646 | } | |
647 | } | |
648 | return new_argc; | |
649 | } | |
650 | ||
651 | int | |
652 | svccmd_invoke_argv(struct userNode *user, struct service *service, struct chanNode *channel, unsigned int argc, char *argv[], unsigned int server_qualified) { | |
653 | extern struct userNode *chanserv; | |
654 | struct svccmd *cmd; | |
655 | unsigned int cmd_arg, perms, flags, options; | |
656 | char channel_name[CHANNELLEN+1]; | |
657 | ||
658 | options = (server_qualified ? SVCCMD_QUALIFIED : 0) | SVCCMD_DEBIT | SVCCMD_NOISY; | |
659 | /* Find the command argument. */ | |
660 | cmd_arg = IsChannelName(argv[0]) ? 1 : 0; | |
661 | if (argc < cmd_arg+1) { | |
662 | send_message(user, service->bot, "MCMSG_MISSING_COMMAND"); | |
663 | return 0; | |
664 | } | |
665 | if (!isalnum(*argv[cmd_arg])) { | |
666 | /* Silently ignore stuff that doesn't begin with a letter or number. */ | |
667 | return 0; | |
668 | } | |
669 | cmd = dict_find(service->commands, argv[cmd_arg], NULL); | |
670 | if (!cmd) { | |
671 | if (!channel) | |
672 | send_message(user, service->bot, "MSG_COMMAND_UNKNOWN", argv[cmd_arg]); | |
673 | return 0; | |
674 | } | |
675 | ||
676 | /* Check pubcmd for the channel. */ | |
677 | if (channel && (channel->channel_info) && (service->bot == chanserv) | |
678 | && !check_user_level(channel, user, lvlPubCmd, 1, 0)) { | |
679 | send_message(user, service->bot, "MCMSG_PUBLIC_DENY", channel->name); | |
680 | return 0; | |
681 | } | |
682 | ||
683 | flags = cmd->effective_flags; | |
684 | /* If they put a channel name first, check if the command allows | |
685 | * it. If so, swap it with the command name. | |
686 | */ | |
687 | if (cmd_arg == 1) { | |
688 | char *tmp; | |
689 | /* Complain if we're not supposed to accept the channel. */ | |
690 | if (!(flags & MODCMD_ACCEPT_CHANNEL)) { | |
691 | send_message(user, service->bot, "MCMSG_NO_CHANNEL_BEFORE"); | |
692 | return 0; | |
693 | } | |
694 | if (!(flags & MODCMD_ACCEPT_PCHANNEL) | |
695 | && (argv[0][0] == '+')) { | |
696 | send_message(user, service->bot, "MCMSG_NO_PLUS_CHANNEL"); | |
697 | return 0; | |
698 | } | |
699 | tmp = argv[1]; | |
700 | argv[1] = argv[0]; | |
701 | argv[0] = tmp; | |
702 | } | |
703 | ||
704 | /* Try to grab a channel handle before alias expansion. | |
705 | * If the command accepts a channel name, and argv[1] is | |
706 | * one, use it as a channel name, and hide it from logging. | |
707 | */ | |
708 | if ((argc > 1) | |
709 | && (flags & MODCMD_ACCEPT_CHANNEL) | |
710 | && IsChannelName(argv[1]) | |
711 | && ((argv[1][0] != '+') || (flags & MODCMD_ACCEPT_PCHANNEL)) | |
712 | && (channel = dict_find(channels, argv[1], NULL))) { | |
713 | argv[1] = argv[0]; | |
714 | argv++, argc--; | |
715 | cmd_arg = 1; | |
716 | } | |
717 | ||
718 | /* Expand the alias arguments, if there are any. */ | |
719 | if (cmd->alias.used) { | |
720 | char *new_argv[MAXNUMPARAMS]; | |
721 | argc = svccmd_expand_alias(cmd, user, argc, argv, new_argv); | |
722 | if (!argc) { | |
723 | send_message(user, service->bot, "MCMSG_ALIAS_ERROR", cmd->name); | |
724 | return 0; | |
725 | } | |
726 | argv = new_argv; | |
727 | ||
728 | /* Try again to grab a handle to the channel after alias | |
729 | * expansion, overwriting any previous channel. This should, | |
730 | * of course, only be done again if an alias was acually | |
731 | * expanded. */ | |
732 | if ((argc > 1) | |
733 | && (flags & MODCMD_ACCEPT_CHANNEL) | |
734 | && IsChannelName(argv[1]) | |
735 | && ((argv[1][0] != '+') || (flags & MODCMD_ACCEPT_PCHANNEL)) | |
736 | && (channel = dict_find(channels, argv[1], NULL))) { | |
737 | argv[1] = argv[0]; | |
738 | argv++, argc--; | |
739 | cmd_arg = 1; | |
740 | } | |
741 | } | |
742 | ||
743 | /* Figure out what actions we should do for it.. */ | |
744 | if (cmd_arg && (flags & MODCMD_TOY)) { | |
745 | /* Do not let user manually specify a channel. */ | |
746 | channel = NULL; | |
747 | } | |
748 | if (argc < cmd->command->min_argc) { | |
749 | send_message(user, service->bot, "MSG_MISSING_PARAMS", cmd->name); | |
750 | svccmd_send_help_brief(user, service->bot, cmd); | |
751 | return 0; | |
752 | } | |
753 | if (!cmd->command->func) { | |
754 | send_message(user, service->bot, "MCMSG_INTERNAL_COMMAND", cmd->name); | |
755 | return 0; | |
756 | } | |
757 | perms = svccmd_can_invoke(user, service->bot, cmd, channel, options); | |
758 | if (!perms) | |
759 | return 0; | |
760 | cmd->uses++; | |
761 | if (perms & ACTION_NOCHANNEL) | |
762 | channel = NULL; | |
763 | ||
764 | if (channel) | |
765 | safestrncpy(channel_name, channel->name, sizeof(channel_name)); | |
766 | else | |
767 | channel_name[0] = 0; | |
768 | if (!cmd->command->func(user, channel, argc, argv, cmd)) | |
769 | return 0; | |
770 | if (!(flags & MODCMD_NO_LOG)) { | |
771 | enum log_severity slvl; | |
772 | if (perms & ACTION_STAFF) | |
773 | slvl = LOG_STAFF; | |
774 | else if (perms & ACTION_OVERRIDE) | |
775 | slvl = LOG_OVERRIDE; | |
776 | else | |
777 | slvl = LOG_COMMAND; | |
778 | /* Unsplit argv after running the function to get the benefit | |
779 | * of any mangling/hiding done by the commands. */ | |
780 | log_audit(cmd->command->parent->clog, slvl, user, service->bot, channel_name, AUDIT_HOSTMASK, unsplit_string(argv, argc, NULL)); | |
781 | } | |
782 | return 1; | |
783 | } | |
784 | ||
785 | /* First line (syntax usually) only help.. used for wrong param counts etc */ | |
786 | int | |
787 | svccmd_send_help_brief(struct userNode *user, struct userNode *bot, struct svccmd *cmd) { | |
788 | int r; | |
789 | ||
790 | /* If it's an alias, show what it's an alias for. */ | |
791 | if (cmd->alias.used) { | |
792 | char alias_text[MAXLEN]; | |
793 | unsplit_string((char**)cmd->alias.list, cmd->alias.used, alias_text); | |
794 | send_message(user, bot, "MCMSG_COMMAND_ALIASES", cmd->name, cmd->command->name); | |
795 | } | |
796 | /* Send the syntax line of help.. */ | |
797 | r = send_help_brief(user, bot, cmd->command->parent->helpfile, cmd->name); | |
798 | if(!r) { | |
799 | if(cmd->command->name) | |
800 | { | |
801 | send_message(user, bot, "MCMSG_COMMAND_ALIASES", cmd->name, cmd->command->name); | |
802 | r = send_help_brief(user, bot, cmd->command->parent->helpfile, cmd->command->name); | |
803 | } | |
804 | } | |
805 | return r; | |
806 | } | |
807 | ||
808 | int is_joiner(struct svccmd *cmd) | |
809 | { | |
810 | if(cmd->command->name) | |
811 | { | |
812 | if(strcasecmp("joiner", cmd->command->name)) | |
813 | return(0); | |
814 | else | |
815 | return(1); | |
816 | } | |
817 | return(0); | |
818 | /* | |
819 | if(cmd->alias.used) | |
820 | { | |
821 | char alias_text[MAXLEN]; | |
822 | unsplit_string((char**)cmd->alias.list, cmd->alias.used, alias_text); | |
823 | if(strcasecmp("joiner", alias_text)) | |
824 | return(0); | |
825 | else | |
826 | return(1); | |
827 | } | |
828 | */ | |
829 | } | |
830 | ||
831 | int | |
832 | svccmd_send_help(struct userNode *user, struct service *service, const char *topic) { | |
833 | struct module *module; | |
834 | struct svccmd *cmd; | |
835 | char cmdname[MAXLEN]; | |
836 | unsigned int nn, ii; | |
837 | int helpsent = 0; | |
838 | ||
839 | /* If there is no topic show the index */ | |
840 | if (!topic) | |
841 | topic = "<index>"; | |
842 | /* make heading str (uppercase) */ | |
843 | for (nn=0; topic[nn]; nn++) | |
844 | cmdname[nn] = toupper(topic[nn]); | |
845 | cmdname[nn] = 0; | |
846 | ||
847 | /* If there is a command 'topic', send command help for the command (unless its bound to JOINER) */ | |
848 | if ((cmd = dict_find(service->commands, topic, NULL)) && !is_joiner(cmd)) | |
849 | { | |
850 | send_message(user, service->bot, "MCMSG_HELP_COMMAND_HEADER", cmdname); | |
851 | send_message(user, service->bot, "MCMSG_HELP_DIVIDER"); | |
852 | helpsent = send_help(user, service->bot, cmd->command->parent->helpfile, cmd->name); | |
853 | ||
854 | /* Show if its an alias, or a binding of another command */ | |
855 | if (cmd->alias.used) | |
856 | { | |
857 | char alias_text[MAXLEN]; | |
858 | unsplit_string((char**)cmd->alias.list, cmd->alias.used, alias_text); | |
859 | send_message(user, service->bot, "MCMSG_HELP_COMMAND_ALIAS", alias_text); | |
860 | /* If send_help above didnt work, try again with the referenced command.. */ | |
861 | if(!helpsent) | |
862 | helpsent = send_help(user, service->bot, cmd->command->parent->helpfile, alias_text); | |
863 | } | |
864 | else if(cmd->command->name && strcasecmp(cmd->command->name, cmd->name)) | |
865 | { | |
866 | send_message(user, service->bot, "MCMSG_HELP_COMMAND_ALIAS", cmd->command->name); | |
867 | /* If send_help above didnt work, try again with the referenced command.. */ | |
868 | if(!helpsent) | |
869 | helpsent = send_help(user, service->bot, cmd->command->parent->helpfile, cmd->command->name); | |
870 | } | |
871 | /* If send_help still couldnt find it, tell them sorry */ | |
872 | if(!helpsent) | |
873 | send_message(user, service->bot, "MCMSG_HELP_COMMAND_UNKNOWN"); | |
874 | send_message(user, service->bot, "MCMSG_HELP_FOOTER"); | |
875 | return true; | |
876 | } | |
877 | else /* look for topic in the help files loaded to this nick/service */ | |
878 | { | |
879 | /* Check for non command help in first primary help file, then next and so on */ | |
880 | /* Note - we need to think about default bindings. see opserv.helpfiles */ | |
881 | for(ii = 0; ii < service->modules.used; ii++) | |
882 | { | |
883 | module = service->modules.list[ii]; | |
884 | if(!module->helpfile) | |
885 | continue; | |
886 | if(dict_find(module->helpfile->db, topic, NULL)) | |
887 | { | |
888 | if (module->helpfile && dict_find(module->helpfile->db, topic, NULL)) | |
889 | { | |
890 | ||
891 | send_message(user, service->bot, "MCMSG_HELP_TOPIC_HEADER", cmdname); | |
892 | send_message(user, service->bot, "MCMSG_HELP_DIVIDER"); | |
893 | /* This should never fail but maybe if something is odd? */ | |
894 | if(!send_help(user, service->bot, module->helpfile, topic)) | |
895 | send_message(user, service->bot, "MSG_TOPIC_UNKNOWN"); | |
896 | send_message(user, service->bot, "MCMSG_HELP_FOOTER"); | |
897 | return true; | |
898 | } | |
899 | } | |
900 | } | |
901 | } | |
902 | /* Otherwise say we cant find it */ | |
903 | send_message(user, service->bot, "MSG_TOPIC_UNKNOWN"); | |
904 | return 0; | |
905 | } | |
906 | ||
907 | static int | |
908 | svccmd_invoke(struct userNode *user, struct service *service, struct chanNode *channel, char *text, int server_qualified) { | |
909 | unsigned int argc; | |
910 | char *argv[MAXNUMPARAMS]; | |
911 | ||
912 | if (!*text) | |
913 | return 0; | |
914 | if (service->privileged) { | |
915 | if (!IsOper(user)) { | |
916 | send_message(user, service->bot, "MSG_SERVICE_PRIVILEGED", service->bot->nick); | |
917 | return 0; | |
918 | } | |
919 | if (!user->handle_info) { | |
920 | send_message(user, service->bot, "MSG_AUTHENTICATE"); | |
921 | return 0; | |
922 | } | |
923 | if (HANDLE_FLAGGED(user->handle_info, OPER_SUSPENDED)) { | |
924 | send_message(user, service->bot, "MSG_OPER_SUSPENDED"); | |
925 | return 0; | |
926 | } | |
927 | } | |
928 | argc = split_line(text, false, ArrayLength(argv), argv); | |
929 | return argc ? svccmd_invoke_argv(user, service, channel, argc, argv, server_qualified) : 0; | |
930 | } | |
931 | ||
932 | void | |
933 | modcmd_privmsg(struct userNode *user, struct userNode *bot, char *text, int server_qualified) { | |
934 | struct service *service; | |
935 | ||
936 | if (!(service = dict_find(services, bot->nick, NULL))) { | |
937 | log_module(MAIN_LOG, LOG_ERROR, "modcmd_privmsg got privmsg for unhandled service %s, unregistering.", bot->nick); | |
938 | reg_privmsg_func(bot, NULL); | |
939 | return; | |
940 | } | |
941 | ||
942 | if (text[0] == '\x01') { | |
943 | char *term, response[MAXLEN]; | |
944 | ||
945 | text++; /* Skip leading ^A. */ | |
946 | /* Chop off final ^A. */ | |
947 | term = strchr(text, '\x01'); | |
948 | if (!term) | |
949 | return; | |
950 | *term = '\0'; | |
951 | /* Parse out leading text. */ | |
952 | term = strchr(text, ' '); | |
953 | if (term) { | |
954 | *term++ = '\0'; | |
955 | if (!*term) | |
956 | term = NULL; | |
957 | } | |
958 | /* No dict lookup since these are so few. */ | |
959 | if (!irccasecmp(text, "CLIENTINFO")) { | |
960 | /* Use \001 instead of \x01 because apparently \x01C is | |
961 | * interpreted as ASCII FS (\034, decimal 28, hex 1C). | |
962 | */ | |
963 | irc_notice_user(bot, user, "\001CLIENTINFO CLIENTINFO PING TIME USERINFO VERSION\x01"); | |
964 | } else if (!irccasecmp(text, "PING")) { | |
965 | if (term) { | |
966 | snprintf(response, sizeof(response), "\x01PING %s\x01", term); | |
967 | irc_notice_user(bot, user, response); | |
968 | } else { | |
969 | irc_notice_user(bot,user, "\x01PING\x01"); | |
970 | } | |
971 | } else if (!irccasecmp(text, "TIME")) { | |
972 | struct tm tm; | |
973 | localtime_r(&now, &tm); | |
974 | strftime(response, sizeof(response), "\x01TIME %a %b %d %H:%M:%S %Y\x01", &tm); | |
975 | irc_notice_user(bot, user, response); | |
976 | } else if (!irccasecmp(text, "USERINFO")) { | |
977 | snprintf(response, sizeof(response), "\x01USERINFO %s\x01", bot->info); | |
978 | irc_notice_user(bot, user, response); | |
979 | } else if (!irccasecmp(text, "VERSION")) { | |
980 | /* This function provides copyright management information | |
981 | * to end users of X3. You should not alter, disable or | |
982 | * remove this command or its accessibility to normal IRC | |
983 | * users, except to add copyright information pertaining | |
984 | * to changes you make to X3. | |
985 | */ | |
986 | snprintf(response, sizeof(response), "\x01VERSION %s\x01", PACKAGE_STRING); | |
987 | irc_notice_user(bot, user, response); | |
988 | } else if (!irccasecmp(text, "GENDER")) { | |
989 | snprintf(response, sizeof(response), "\x01GENDER ummm im still deciding\x01"); | |
990 | irc_notice_user(bot, user, response); | |
991 | } | |
992 | return; | |
993 | } | |
994 | ||
995 | if (service->msg_hook && service->msg_hook(user, bot, text, server_qualified)) | |
996 | return; | |
997 | svccmd_invoke(user, service, NULL, text, server_qualified); | |
998 | } | |
999 | ||
1000 | void | |
1001 | modcmd_chanmsg(struct userNode *user, struct chanNode *chan, char *text, struct userNode *bot) { | |
1002 | struct service *service; | |
1003 | if (!(service = dict_find(services, bot->nick, NULL))) return; | |
1004 | svccmd_invoke(user, service, chan, text, 0); | |
1005 | } | |
1006 | ||
1007 | struct service * | |
1008 | service_register(struct userNode *bot) { | |
1009 | struct service *service; | |
1010 | if ((service = dict_find(services, bot->nick, NULL))) | |
1011 | return service; | |
1012 | service = calloc(1, sizeof(*service)); | |
1013 | module_list_init(&service->modules); | |
1014 | service->commands = dict_new(); | |
1015 | service->bot = bot; | |
1016 | dict_set_free_data(service->commands, free_service_command); | |
1017 | dict_insert(services, service->bot->nick, service); | |
1018 | reg_privmsg_func(bot, modcmd_privmsg); | |
1019 | return service; | |
1020 | } | |
1021 | ||
1022 | struct service * | |
1023 | service_find(const char *name) { | |
1024 | return dict_find(services, name, NULL); | |
1025 | } | |
1026 | ||
1027 | static void | |
1028 | svccmd_insert(struct service *service, char *name, struct svccmd *svccmd, struct modcmd *modcmd) { | |
1029 | unsigned int ii; | |
1030 | svccmd->parent = service; | |
1031 | svccmd->name = name; | |
1032 | svccmd->command = modcmd; | |
1033 | svccmd->command->bind_count++; | |
1034 | dict_insert(service->commands, svccmd->name, svccmd); | |
1035 | for (ii=0; ii<service->modules.used; ++ii) { | |
1036 | if (service->modules.list[ii] == svccmd->command->parent) break; | |
1037 | } | |
1038 | if (ii == service->modules.used) { | |
1039 | module_list_append(&service->modules, svccmd->command->parent); | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | struct svccmd * | |
1044 | service_bind_modcmd(struct service *service, struct modcmd *cmd, const char *name) { | |
1045 | struct svccmd *svccmd; | |
1046 | if ((svccmd = dict_find(service->commands, name, NULL))) { | |
1047 | if (svccmd->command == cmd) return svccmd; | |
1048 | log_module(MAIN_LOG, LOG_ERROR, "Tried to bind command %s.%s into service %s as %s, but already bound (as %s.%s).", cmd->parent->name, cmd->name, service->bot->nick, name, svccmd->command->parent->name, svccmd->command->name); | |
1049 | return NULL; | |
1050 | } | |
1051 | svccmd = calloc(1, sizeof(*svccmd)); | |
1052 | svccmd_insert(service, strdup(name), svccmd, cmd); | |
1053 | svccmd_copy_rules(svccmd, cmd->defaults); | |
1054 | return svccmd; | |
1055 | } | |
1056 | ||
1057 | static unsigned int | |
1058 | service_bind_module(struct service *service, struct module *module) { | |
1059 | dict_iterator_t it; | |
1060 | struct modcmd *modcmd; | |
1061 | unsigned int count; | |
1062 | ||
1063 | count = 0; | |
1064 | for (it = dict_first(module->commands); it; it = iter_next(it)) { | |
1065 | modcmd = iter_data(it); | |
1066 | if (!((modcmd->flags | modcmd->defaults->flags) & MODCMD_NO_DEFAULT_BIND)) | |
1067 | if (service_bind_modcmd(service, modcmd, iter_key(it))) | |
1068 | count++; | |
1069 | } | |
1070 | return count; | |
1071 | } | |
1072 | ||
1073 | /* This MUST return argc if the alias expansion code knows how to deal | |
1074 | * with every argument in argv; otherwise, it MUST return the index of | |
1075 | * an argument that the expansion code does not know how to deal with. | |
1076 | */ | |
1077 | static unsigned int | |
1078 | check_alias_args(char *argv[], unsigned int argc) { | |
1079 | unsigned int arg; | |
1080 | ||
1081 | for (arg=0; arg<argc; ++arg) { | |
1082 | if (argv[arg][0] != '$') { | |
1083 | continue; | |
1084 | } else if (argv[arg][1] == '$') { | |
1085 | continue; | |
1086 | } else if (isdigit(argv[arg][1])) { | |
1087 | char *end_num; | |
1088 | ||
1089 | strtoul(argv[arg]+1, &end_num, 10); | |
1090 | switch (end_num[0]) { | |
1091 | case 0: | |
1092 | continue; | |
1093 | case '-': | |
1094 | if (end_num[1] == 0) | |
1095 | continue; | |
1096 | else if (isdigit(end_num[1])) | |
1097 | continue; | |
1098 | /* else fall through to default case */ | |
1099 | default: | |
1100 | return arg; | |
1101 | } | |
1102 | } else { | |
1103 | switch(argv[arg][1]) { | |
1104 | case 'a': | |
1105 | case 'n': | |
1106 | case 'm': | |
1107 | continue; | |
1108 | default: | |
1109 | return arg; | |
1110 | } | |
1111 | } | |
1112 | } | |
1113 | return arg; | |
1114 | } | |
1115 | ||
1116 | static unsigned int | |
1117 | collapse_cmdname(char *argv[], unsigned int argc, char *dest) { | |
1118 | unsigned int ii, pos, arg; | |
1119 | if (!argc) { | |
1120 | dest[0] = 0; | |
1121 | return 0; | |
1122 | } | |
1123 | for (ii=pos=0, arg=0; argv[arg][ii]; ) { | |
1124 | if (argv[arg][ii] == '\\') { | |
1125 | if (argv[arg][ii+1]) { | |
1126 | /* escaping a real character just puts it in literally */ | |
1127 | dest[pos++] = argv[arg][++ii]; | |
1128 | } else if ((arg+1) == argc) { | |
1129 | /* we ran to the end of the argument list; abort */ | |
1130 | break; | |
1131 | } else { | |
1132 | /* escape at end of a word is a space */ | |
1133 | dest[pos++] = ' '; | |
1134 | ii = 0; | |
1135 | arg++; | |
1136 | } | |
1137 | } else { | |
1138 | /* normal characters don't need escapes */ | |
1139 | dest[pos++] = argv[arg][ii++]; | |
1140 | } | |
1141 | } | |
1142 | dest[pos] = 0; | |
1143 | return arg + 1; | |
1144 | } | |
1145 | ||
1146 | static MODCMD_FUNC(cmd_bind) { | |
1147 | struct service *service; | |
1148 | struct svccmd *template, *newcmd; | |
1149 | char *svcname, *dot; | |
1150 | char newname[MAXLEN], cmdname[MAXLEN]; | |
1151 | unsigned int arg, diff; | |
1152 | ||
1153 | assert(argc > 3); | |
1154 | svcname = argv[1]; | |
1155 | arg = collapse_cmdname(argv+2, argc-2, newname) + 2; | |
1156 | if (!arg) { | |
1157 | reply("MSG_MISSING_PARAMS", cmd->name); | |
1158 | return 0; | |
1159 | } | |
1160 | diff = collapse_cmdname(argv+arg, argc-arg, cmdname); | |
1161 | if (!diff) { | |
1162 | reply("MSG_MISSING_PARAMS", cmd->name); | |
1163 | return 0; | |
1164 | } | |
1165 | arg += diff; | |
1166 | ||
1167 | if (!(service = service_find(svcname))) { | |
1168 | reply("MCMSG_UNKNOWN_SERVICE", svcname); | |
1169 | return 0; | |
1170 | } | |
1171 | ||
1172 | if ((newcmd = dict_find(service->commands, newname, NULL))) { | |
1173 | reply("MCMSG_ALREADY_BOUND", service->bot->nick, newname); | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | if ((dot = strchr(cmdname, '.')) && (dot[1] == '*') && (dot[2] == 0)) { | |
1178 | unsigned int count; | |
1179 | struct module *module; | |
1180 | *dot = 0; | |
1181 | module = module_find((cmdname[0] == '*') ? cmdname+1 : cmdname); | |
1182 | if (!module) { | |
1183 | reply("MSG_MODULE_UNKNOWN", cmdname); | |
1184 | return 0; | |
1185 | } | |
1186 | count = service_bind_module(service, module); | |
1187 | reply("MCMSG_MODULE_BOUND", count, module->name, service->bot->nick); | |
1188 | return count != 0; | |
1189 | } | |
1190 | newcmd = calloc(1, sizeof(*newcmd)); | |
1191 | newcmd->name = strdup(newname); | |
1192 | newcmd->parent = service; | |
1193 | if (!(template = svccmd_resolve_name(newcmd, cmdname))) { | |
1194 | reply("MCMSG_UNKNOWN_COMMAND_2", cmdname, service->bot->nick); | |
1195 | free(newcmd->name); | |
1196 | free(newcmd); | |
1197 | return 0; | |
1198 | } | |
1199 | if (template->alias.used) { | |
1200 | reply("MCMSG_CANNOT_DOUBLE_ALIAS"); | |
1201 | free(newcmd->name); | |
1202 | free(newcmd); | |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | if (argc > arg) { | |
1207 | /* a more complicated alias; fix it up */ | |
1208 | unsigned int nn; | |
1209 | ||
1210 | arg -= diff; | |
1211 | nn = check_alias_args(argv+arg, argc-arg); | |
1212 | if (nn+arg < argc) { | |
1213 | reply("MCMSG_BAD_ALIAS_ARGUMENT", argv[nn+arg]); | |
1214 | free(newcmd->name); | |
1215 | free(newcmd); | |
1216 | return 0; | |
1217 | } | |
1218 | newcmd->alias.used = newcmd->alias.size = argc-arg; | |
1219 | newcmd->alias.list = calloc(newcmd->alias.size, sizeof(newcmd->alias.list[0])); | |
1220 | for (nn=0; nn<newcmd->alias.used; ++nn) | |
1221 | newcmd->alias.list[nn] = strdup(argv[nn+arg]); | |
1222 | } | |
1223 | ||
1224 | svccmd_insert(service, newcmd->name, newcmd, template->command); | |
1225 | svccmd_copy_rules(newcmd, template); | |
1226 | reply("MCMSG_COMMAND_BOUND", newcmd->name, newcmd->parent->bot->nick); | |
1227 | return 1; | |
1228 | } | |
1229 | ||
1230 | static int | |
1231 | service_recheck_bindings(struct service *service, struct module *module) { | |
1232 | dict_iterator_t it; | |
1233 | struct svccmd *cmd; | |
1234 | ||
1235 | for (it = dict_first(service->commands); it; it = iter_next(it)) { | |
1236 | cmd = iter_data(it); | |
1237 | if (cmd->command->parent == module) return 0; | |
1238 | } | |
1239 | /* No more bindings, remove it from our list. */ | |
1240 | module_list_remove(&service->modules, module); | |
1241 | return 1; | |
1242 | } | |
1243 | ||
1244 | static svccmd_unbind_func_t *suf_list; | |
1245 | unsigned int suf_size, suf_used; | |
1246 | ||
1247 | void | |
1248 | reg_svccmd_unbind_func(svccmd_unbind_func_t handler) { | |
1249 | if (suf_used == suf_size) { | |
1250 | if (suf_size) { | |
1251 | suf_size <<= 1; | |
1252 | suf_list = realloc(suf_list, suf_size*sizeof(svccmd_unbind_func_t)); | |
1253 | } else { | |
1254 | suf_size = 8; | |
1255 | suf_list = malloc(suf_size*sizeof(svccmd_unbind_func_t)); | |
1256 | } | |
1257 | } | |
1258 | suf_list[suf_used++] = handler; | |
1259 | } | |
1260 | ||
1261 | static MODCMD_FUNC(cmd_unbind) { | |
1262 | struct service *service; | |
1263 | struct userNode *bot; | |
1264 | struct svccmd *bound; | |
1265 | struct module *module; | |
1266 | const char *svcname; | |
1267 | unsigned int arg, ii; | |
1268 | char cmdname[MAXLEN]; | |
1269 | ||
1270 | assert(argc > 2); | |
1271 | svcname = argv[1]; | |
1272 | arg = collapse_cmdname(argv+2, argc-2, cmdname) + 2; | |
1273 | if (!arg) { | |
1274 | reply("MSG_MISSING_PARAMS", cmd->name); | |
1275 | return 0; | |
1276 | } | |
1277 | if (!(service = service_find(svcname))) { | |
1278 | reply("MCMSG_UNKNOWN_SERVICE", svcname); | |
1279 | return 0; | |
1280 | } | |
1281 | if (!(bound = dict_find(service->commands, cmdname, NULL))) { | |
1282 | reply("MCMSG_NO_COMMAND_BOUND", service->bot->nick, cmdname); | |
1283 | return 0; | |
1284 | } | |
1285 | if ((bound->command->flags & MODCMD_KEEP_BOUND) && (bound->command->bind_count == 1)) { | |
1286 | reply("MCMSG_UNBIND_PROHIBITED", bound->command->name); | |
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | for (ii=0; ii<suf_used; ii++) | |
1291 | suf_list[ii](bound); | |
1292 | /* If this command binding is removing itself, we must take care | |
1293 | * not to dereference it after the dict_remove. | |
1294 | */ | |
1295 | bot = cmd->parent->bot; | |
1296 | module = cmd->command->parent; | |
1297 | dict_remove(service->commands, bound->name); | |
1298 | send_message(user, bot, "MCMSG_COMMAND_UNBOUND", cmdname, service->bot->nick); | |
1299 | if (service_recheck_bindings(service, module)) | |
1300 | send_message(user, bot, "MCMSG_HELPFILE_UNBOUND", module->name, module->name); | |
1301 | return 1; | |
1302 | } | |
1303 | ||
1304 | static MODCMD_FUNC(cmd_readhelp) { | |
1305 | const char *modname; | |
1306 | struct module *module; | |
1307 | struct helpfile *old_helpfile; | |
1308 | struct timeval start, stop; | |
1309 | ||
1310 | assert(argc > 1); | |
1311 | modname = argv[1]; | |
1312 | if (!(module = module_find(modname))) { | |
1313 | reply("MSG_MODULE_UNKNOWN", modname); | |
1314 | return 0; | |
1315 | } | |
1316 | if (!module->helpfile_name) { | |
1317 | reply("MCMSG_NO_HELPFILE", module->name); | |
1318 | return 0; | |
1319 | } | |
1320 | old_helpfile = module->helpfile; | |
1321 | gettimeofday(&start, NULL); | |
1322 | module->helpfile = open_helpfile(module->helpfile_name, module->expand_help); | |
1323 | if (!module->helpfile) { | |
1324 | module->helpfile = old_helpfile; | |
1325 | reply("MCMSG_HELPFILE_ERROR", module->helpfile_name); | |
1326 | return 0; | |
1327 | } | |
1328 | if (old_helpfile) close_helpfile(old_helpfile); | |
1329 | gettimeofday(&stop, NULL); | |
1330 | stop.tv_sec -= start.tv_sec; | |
1331 | stop.tv_usec -= start.tv_usec; | |
1332 | if (stop.tv_usec < 0) { | |
1333 | stop.tv_sec -= 1; | |
1334 | stop.tv_usec += 1000000; | |
1335 | } | |
1336 | reply("MCMSG_HELPFILE_READ", module->name, stop.tv_sec, stop.tv_usec/1000); | |
1337 | return 1; | |
1338 | } | |
1339 | ||
1340 | static MODCMD_FUNC(cmd_help) { | |
1341 | const char *topic; | |
1342 | ||
1343 | topic = (argc < 2) ? NULL : unsplit_string(argv+1, argc-1, NULL); | |
1344 | return svccmd_send_help(user, cmd->parent, topic); | |
1345 | } | |
1346 | ||
1347 | static MODCMD_FUNC(cmd_timecmd) { | |
1348 | struct timeval start, stop; | |
1349 | char cmd_text[MAXLEN]; | |
1350 | ||
1351 | unsplit_string(argv+1, argc-1, cmd_text); | |
1352 | gettimeofday(&start, NULL); | |
1353 | svccmd_invoke(user, cmd->parent, channel, cmd_text, 0); | |
1354 | gettimeofday(&stop, NULL); | |
1355 | stop.tv_sec -= start.tv_sec; | |
1356 | stop.tv_usec -= start.tv_usec; | |
1357 | if (stop.tv_usec < 0) { | |
1358 | stop.tv_sec -= 1; | |
1359 | stop.tv_usec += 1000000; | |
1360 | } | |
1361 | reply("MCMSG_COMMAND_TIME", cmd_text, stop.tv_sec, stop.tv_usec); | |
1362 | return 1; | |
1363 | } | |
1364 | ||
1365 | static MODCMD_FUNC(cmd_command) { | |
1366 | struct svccmd *svccmd; | |
1367 | const char *cmd_name, *fmt_str; | |
1368 | unsigned int flags, shown_flags, nn, pos; | |
1369 | char buf[MAXLEN]; | |
1370 | ||
1371 | assert(argc >= 2); | |
1372 | cmd_name = unsplit_string(argv+1, argc-1, NULL); | |
1373 | if (!(svccmd = svccmd_resolve_name(cmd, cmd_name))) { | |
1374 | reply("MCMSG_UNKNOWN_COMMAND_2", cmd_name, cmd->parent->bot->nick); | |
1375 | return 0; | |
1376 | } | |
1377 | pos = snprintf(buf, sizeof(buf), "%s.%s", svccmd->command->parent->name, svccmd->command->name); | |
1378 | if (svccmd->alias.used) { | |
1379 | buf[pos++] = ' '; | |
1380 | unsplit_string((char**)svccmd->alias.list+1, svccmd->alias.used-1, buf+pos); | |
1381 | reply("MCMSG_COMMAND_ALIASES", svccmd->name, buf); | |
1382 | } else { | |
1383 | reply("MCMSG_COMMAND_BINDING", svccmd->name, buf); | |
1384 | } | |
1385 | flags = svccmd->effective_flags; | |
1386 | if ((svccmd->parent && svccmd->parent->privileged && !IsOper(user)) | |
1387 | || ((flags & MODCMD_REQUIRE_STAFF) | |
1388 | && !IsOper(user) && !IsNetworkHelper(user) && !IsSupportHelper(user))) { | |
1389 | reply("MCMSG_INSPECTION_REFUSED", svccmd->name); | |
1390 | return 0; | |
1391 | } | |
1392 | if (flags & MODCMD_DISABLED) { | |
1393 | reply("MSG_COMMAND_DISABLED", svccmd->name); | |
1394 | return 1; | |
1395 | } | |
1396 | shown_flags = 0; | |
1397 | if (svccmd->min_opserv_level > 0) { | |
1398 | reply("MCMSG_NEED_OPSERV_LEVEL", svccmd->min_opserv_level); | |
1399 | shown_flags |= MODCMD_REQUIRE_OPER | MODCMD_REQUIRE_AUTHED; | |
1400 | } | |
1401 | if (svccmd->min_channel_access > 0) { | |
1402 | reply("MCMSG_NEED_CHANSERV_LEVEL", svccmd->min_channel_access); | |
1403 | shown_flags |= MODCMD_REQUIRE_CHANUSER | MODCMD_REQUIRE_REGCHAN | MODCMD_REQUIRE_CHANNEL | MODCMD_REQUIRE_AUTHED; | |
1404 | } | |
1405 | if (svccmd->req_account_flags) { | |
1406 | for (nn=pos=0; nn<32; nn++) { | |
1407 | if (!(svccmd->req_account_flags & (1 << nn))) continue; | |
1408 | buf[pos++] = HANDLE_FLAGS[nn]; | |
1409 | } | |
1410 | buf[pos] = 0; | |
1411 | reply("MCMSG_NEED_ACCOUNT_FLAGS", buf); | |
1412 | shown_flags |= MODCMD_REQUIRE_AUTHED; | |
1413 | } | |
1414 | if (!flags && !shown_flags) { | |
1415 | reply("MCMSG_NEED_NOTHING", svccmd->name); | |
1416 | return 1; | |
1417 | } | |
1418 | if (flags & ~shown_flags & MODCMD_REQUIRE_HELPING) { | |
1419 | reply("MCMSG_MUST_BE_HELPING"); | |
1420 | shown_flags |= MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_STAFF; | |
1421 | } | |
1422 | if (flags & ~shown_flags & MODCMD_REQUIRE_STAFF) { | |
1423 | switch (flags & MODCMD_REQUIRE_STAFF) { | |
1424 | default: case MODCMD_REQUIRE_STAFF: | |
1425 | fmt_str = "MCMSG_NEED_STAFF_ACCESS"; | |
1426 | break; | |
1427 | case MODCMD_REQUIRE_OPER: | |
1428 | fmt_str = "MCMSG_NEED_STAFF_OPER"; | |
1429 | break; | |
1430 | case MODCMD_REQUIRE_NETWORK_HELPER: | |
1431 | fmt_str = "MCMSG_NEED_STAFF_NETHELPER"; | |
1432 | break; | |
1433 | case MODCMD_REQUIRE_OPER|MODCMD_REQUIRE_NETWORK_HELPER: | |
1434 | fmt_str = "MCMSG_NEED_STAFF_NETHELPER_OR_OPER"; | |
1435 | break; | |
1436 | case MODCMD_REQUIRE_SUPPORT_HELPER: | |
1437 | fmt_str = "MCMSG_NEED_STAFF_SHELPER"; | |
1438 | break; | |
1439 | case MODCMD_REQUIRE_OPER|MODCMD_REQUIRE_SUPPORT_HELPER: | |
1440 | fmt_str = "MCMSG_NEED_STAFF_SHELPER_OR_OPER"; | |
1441 | break; | |
1442 | case MODCMD_REQUIRE_SUPPORT_HELPER|MODCMD_REQUIRE_NETWORK_HELPER: | |
1443 | fmt_str = "MCMSG_NEED_STAFF_HELPER"; | |
1444 | break; | |
1445 | } | |
1446 | reply(fmt_str); | |
1447 | shown_flags |= MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_STAFF; | |
1448 | } | |
1449 | if (flags & ~shown_flags & MODCMD_REQUIRE_JOINABLE) { | |
1450 | reply("MCMSG_NEED_JOINABLE"); | |
1451 | shown_flags |= MODCMD_REQUIRE_CHANUSER; | |
1452 | } | |
1453 | if (flags & ~shown_flags & MODCMD_REQUIRE_CHANUSER) { | |
1454 | if (flags & ~shown_flags & MODCMD_IGNORE_CSUSPEND) | |
1455 | reply("MCMSG_NEED_CHANUSER_CSUSPENDABLE"); | |
1456 | else | |
1457 | reply("MCMSG_NEED_CHANUSER"); | |
1458 | shown_flags |= MODCMD_IGNORE_CSUSPEND | MODCMD_REQUIRE_REGCHAN | MODCMD_REQUIRE_CHANNEL | MODCMD_REQUIRE_AUTHED; | |
1459 | } | |
1460 | if (flags & ~shown_flags & MODCMD_REQUIRE_REGCHAN) { | |
1461 | reply("MCMSG_NEED_REGCHAN"); | |
1462 | shown_flags |= MODCMD_REQUIRE_CHANNEL; | |
1463 | } | |
1464 | if (flags & ~shown_flags & MODCMD_REQUIRE_CHANNEL) | |
1465 | reply("MCMSG_NEED_CHANNEL"); | |
1466 | if (flags & ~shown_flags & MODCMD_REQUIRE_AUTHED) | |
1467 | reply("MCMSG_NEED_AUTHED"); | |
1468 | if (flags & ~shown_flags & MODCMD_TOY) | |
1469 | reply("MCMSG_IS_TOY", svccmd->name); | |
1470 | if (flags & ~shown_flags & MODCMD_REQUIRE_QUALIFIED) { | |
1471 | const char *botnick = svccmd->parent ? svccmd->parent->bot->nick : "SomeBot"; | |
1472 | reply("MCMSG_MUST_QUALIFY", botnick, svccmd->name, botnick); | |
1473 | } | |
1474 | reply("MCMSG_END_REQUIREMENTS", svccmd->name); | |
1475 | return 1; | |
1476 | } | |
1477 | ||
1478 | static void | |
1479 | modcmd_describe_command(struct userNode *user, struct svccmd *cmd, struct svccmd *target) { | |
1480 | char buf1[MAXLEN], buf2[MAXLEN]; | |
1481 | unsigned int ii, len, buf1_used, buf2_used; | |
1482 | ||
1483 | if (target->alias.used) { | |
1484 | unsplit_string((char**)target->alias.list, target->alias.used, buf1); | |
1485 | reply("MCMSG_COMMAND_ALIASES", target->name, buf1); | |
1486 | } else { | |
1487 | snprintf(buf1, sizeof(buf1), "%s.%s", target->command->parent->name, target->command->name); | |
1488 | reply("MCMSG_COMMAND_BINDING", target->name, buf1); | |
1489 | } | |
1490 | for (ii = buf1_used = buf2_used = 0; flags[ii].name; ++ii) { | |
1491 | if (target->flags & flags[ii].flag) { | |
1492 | if (buf1_used) | |
1493 | buf1[buf1_used++] = ','; | |
1494 | len = strlen(flags[ii].name); | |
1495 | memcpy(buf1 + buf1_used, flags[ii].name, len); | |
1496 | buf1_used += len; | |
1497 | } else if (target->effective_flags & flags[ii].flag) { | |
1498 | if (buf2_used) | |
1499 | buf2[buf2_used++] = ','; | |
1500 | len = strlen(flags[ii].name); | |
1501 | memcpy(buf2 + buf2_used, flags[ii].name, len); | |
1502 | buf2_used += len; | |
1503 | } | |
1504 | } | |
1505 | if (buf1_used) | |
1506 | buf1[buf1_used] = '\0'; | |
1507 | else | |
1508 | strcpy(buf1, user_find_message(user, "MSG_NONE")); | |
1509 | if (buf2_used) | |
1510 | buf2[buf2_used] = '\0'; | |
1511 | else | |
1512 | strcpy(buf2, user_find_message(user, "MSG_NONE")); | |
1513 | reply("MCMSG_COMMAND_FLAGS", buf1, buf2); | |
1514 | for (ii = buf1_used = buf2_used = 0; handle_flags[ii]; ++ii) { | |
1515 | if (target->req_account_flags & (1 << ii)) | |
1516 | buf1[buf1_used++] = handle_flags[ii]; | |
1517 | else if (target->deny_account_flags & (1 << ii)) | |
1518 | buf2[buf2_used++] = handle_flags[ii]; | |
1519 | } | |
1520 | buf1[buf1_used] = buf2[buf2_used] = '\0'; | |
1521 | reply("MCMSG_COMMAND_ACCOUNT_FLAGS", buf1, buf2); | |
1522 | reply("MCMSG_COMMAND_ACCESS_LEVEL", target->min_channel_access, target->min_opserv_level); | |
1523 | reply("MCMSG_COMMAND_USES", target->name, target->uses); | |
1524 | } | |
1525 | ||
1526 | static MODCMD_FUNC(cmd_modcmd) { | |
1527 | struct svccmd *svccmd; | |
1528 | unsigned int arg, changed; | |
1529 | char cmdname[MAXLEN]; | |
1530 | ||
1531 | assert(argc >= 2); | |
1532 | arg = collapse_cmdname(argv+1, argc-1, cmdname) + 1; | |
1533 | if (!arg) { | |
1534 | reply("MSG_MISSING_PARAMS", cmd->name); | |
1535 | return 0; | |
1536 | } | |
1537 | if (!(svccmd = svccmd_resolve_name(cmd, cmdname))) { | |
1538 | reply("MCMSG_UNKNOWN_COMMAND_2", cmdname, cmd->parent->bot->nick); | |
1539 | return 0; | |
1540 | } | |
1541 | for (changed = 0; arg+1 < argc; arg += 2) { | |
1542 | if (svccmd_configure(svccmd, user, cmd->parent->bot, argv[arg], argv[arg+1])) { | |
1543 | reply("MCMSG_COMMAND_MODIFIED", argv[arg], svccmd->name); | |
1544 | changed = 1; | |
1545 | } | |
1546 | } | |
1547 | if (changed) | |
1548 | modcmd_set_effective_flags(svccmd); | |
1549 | modcmd_describe_command(user, cmd, svccmd); | |
1550 | return changed; | |
1551 | } | |
1552 | ||
1553 | static void | |
1554 | timeout_god(void *data) | |
1555 | { | |
1556 | extern struct userNode *chanserv; | |
1557 | struct userNode *user = data; | |
1558 | ||
1559 | if(!user || !IsHelping(user)) return; | |
1560 | ||
1561 | HANDLE_CLEAR_FLAG(user->handle_info, HELPING); | |
1562 | send_message(user, chanserv, "MCMSG_GOD_EXPIRED"); | |
1563 | } | |
1564 | ||
1565 | static MODCMD_FUNC(cmd_god) { | |
1566 | int helping; | |
1567 | ||
1568 | if (argc > 1) { | |
1569 | if (enabled_string(argv[1])) { | |
1570 | if (HANDLE_FLAGGED(user->handle_info, HELPING)) { | |
1571 | reply("MCMSG_ALREADY_HELPING"); | |
1572 | return 0; | |
1573 | } | |
1574 | helping = 1; | |
1575 | } else if (disabled_string(argv[1])) { | |
1576 | if (!HANDLE_FLAGGED(user->handle_info, HELPING)) { | |
1577 | reply("MCMSG_ALREADY_NOT_HELPING"); | |
1578 | return 0; | |
1579 | } | |
1580 | helping = 0; | |
1581 | } else { | |
1582 | reply("MSG_INVALID_BINARY", argv[1]); | |
1583 | return 0; | |
1584 | } | |
1585 | } else { | |
1586 | helping = !IsHelping(user); | |
1587 | } | |
1588 | ||
1589 | if (helping) { | |
1590 | HANDLE_SET_FLAG(user->handle_info, HELPING); | |
1591 | timeq_add(now + god_timeout, timeout_god, user); | |
1592 | reply("MCMSG_NOW_HELPING"); | |
1593 | } else { | |
1594 | HANDLE_CLEAR_FLAG(user->handle_info, HELPING); | |
1595 | timeq_del(0, timeout_god, user, TIMEQ_IGNORE_WHEN); | |
1596 | reply("MCMSG_NOW_NOT_HELPING"); | |
1597 | } | |
1598 | ||
1599 | return 1; | |
1600 | } | |
1601 | ||
1602 | static MODCMD_FUNC(cmd_joiner) { | |
1603 | char cmdname[MAXLEN]; | |
1604 | ||
1605 | if (argc < 2) { | |
1606 | int len = sprintf(cmdname, "%s ", cmd->name); | |
1607 | dict_iterator_t it; | |
1608 | struct string_buffer sbuf; | |
1609 | ||
1610 | string_buffer_init(&sbuf); | |
1611 | for (it = dict_first(cmd->parent->commands); it; it = iter_next(it)) { | |
1612 | if (!ircncasecmp(iter_key(it), cmdname, len)) { | |
1613 | if (sbuf.used) string_buffer_append_string(&sbuf, ", "); | |
1614 | string_buffer_append_string(&sbuf, iter_key(it)); | |
1615 | } | |
1616 | } | |
1617 | if (!sbuf.used) string_buffer_append(&sbuf, 0); | |
1618 | reply("MCMSG_JOINER_CHOICES", cmd->name, sbuf.list); | |
1619 | string_buffer_clean(&sbuf); | |
1620 | return 1; | |
1621 | } | |
1622 | sprintf(cmdname, "%s %s", cmd->name, argv[1]); | |
1623 | argv[1] = cmdname; | |
1624 | svccmd_invoke_argv(user, cmd->parent, channel, argc-1, argv+1, 0); | |
1625 | return 0; /* never try to log this; the recursive one logs it */ | |
1626 | } | |
1627 | ||
1628 | static MODCMD_FUNC(cmd_stats_modules) { | |
1629 | struct helpfile_table tbl; | |
1630 | dict_iterator_t it; | |
1631 | unsigned int ii; | |
1632 | struct module *mod; | |
1633 | struct modcmd *modcmd; | |
1634 | ||
1635 | if (argc < 2) { | |
1636 | tbl.length = dict_size(modules) + 1; | |
1637 | tbl.width = 3; | |
1638 | tbl.flags = TABLE_PAD_LEFT; | |
1639 | tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0])); | |
1640 | tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0])); | |
1641 | tbl.contents[0][0] = "Module"; | |
1642 | tbl.contents[0][1] = "Commands"; | |
1643 | tbl.contents[0][2] = "Helpfile"; | |
1644 | for (ii=1, it=dict_first(modules); it; it=iter_next(it), ii++) { | |
1645 | mod = iter_data(it); | |
1646 | tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0])); | |
1647 | tbl.contents[ii][0] = mod->name; | |
1648 | tbl.contents[ii][1] = strtab(dict_size(mod->commands)); | |
1649 | tbl.contents[ii][2] = mod->helpfile_name ? mod->helpfile_name : "(none)"; | |
1650 | } | |
1651 | } else if (!(mod = dict_find(modules, argv[1], NULL))) { | |
1652 | reply("MCMSG_UNKNOWN_MODULE", argv[1]); | |
1653 | return 0; | |
1654 | } else { | |
1655 | reply("MCMSG_MODULE_INFO", mod->name); | |
1656 | tbl.length = dict_size(mod->commands) + 1; | |
1657 | tbl.width = 3; | |
1658 | tbl.flags = TABLE_PAD_LEFT; | |
1659 | tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0])); | |
1660 | tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0])); | |
1661 | tbl.contents[0][0] = "Command"; | |
1662 | tbl.contents[0][1] = "Min. Args"; | |
1663 | tbl.contents[0][2] = "Bind Count"; | |
1664 | for (ii=1, it=dict_first(mod->commands); it; it=iter_next(it), ii++) { | |
1665 | modcmd = iter_data(it); | |
1666 | tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0])); | |
1667 | tbl.contents[ii][0] = modcmd->name; | |
1668 | tbl.contents[ii][1] = strtab(modcmd->min_argc); | |
1669 | tbl.contents[ii][2] = strtab(modcmd->bind_count); | |
1670 | } | |
1671 | } | |
1672 | table_send(cmd->parent->bot, user->nick, 0, 0, tbl); | |
1673 | return 0; | |
1674 | } | |
1675 | ||
1676 | static MODCMD_FUNC(cmd_stats_services) { | |
1677 | struct helpfile_table tbl; | |
1678 | dict_iterator_t it; | |
1679 | unsigned int ii; | |
1680 | struct service *service; | |
1681 | struct svccmd *svccmd; | |
1682 | char *extra; | |
1683 | ||
1684 | if (argc < 2) { | |
1685 | tbl.length = dict_size(services) + 1; | |
1686 | tbl.width = 4; | |
1687 | tbl.flags = TABLE_PAD_LEFT; | |
1688 | tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0])); | |
1689 | tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0])); | |
1690 | tbl.contents[0][0] = "Service"; | |
1691 | tbl.contents[0][1] = "Commands"; | |
1692 | tbl.contents[0][2] = "Priv'd?"; | |
1693 | tbl.contents[0][3] = "Trigger"; | |
1694 | extra = calloc(2, tbl.length); | |
1695 | for (ii=1, it=dict_first(services); it; it=iter_next(it), ii++) { | |
1696 | service = iter_data(it); | |
1697 | tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0])); | |
1698 | tbl.contents[ii][0] = service->bot->nick; | |
1699 | tbl.contents[ii][1] = strtab(dict_size(service->commands)); | |
1700 | tbl.contents[ii][2] = service->privileged ? "yes" : "no"; | |
1701 | extra[ii*2] = service->trigger; | |
1702 | tbl.contents[ii][3] = extra+ii*2; | |
1703 | } | |
1704 | table_send(cmd->parent->bot, user->nick, 0, 0, tbl); | |
1705 | free(extra); | |
1706 | return 0; | |
1707 | } else if (!(service = dict_find(services, argv[1], NULL))) { | |
1708 | reply("MCMSG_UNKNOWN_SERVICE", argv[1]); | |
1709 | return 0; | |
1710 | } else { | |
1711 | tbl.length = dict_size(service->commands) + 1; | |
1712 | tbl.width = 5; | |
1713 | tbl.flags = TABLE_PAD_LEFT | TABLE_NO_FREE; | |
1714 | tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0])); | |
1715 | tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0])); | |
1716 | tbl.contents[0][0] = "Command"; | |
1717 | tbl.contents[0][1] = "Module"; | |
1718 | tbl.contents[0][2] = "ModCmd"; | |
1719 | tbl.contents[0][3] = "Alias?"; | |
1720 | tbl.contents[0][4] = strdup("Uses"); | |
1721 | for (ii=1, it=dict_first(service->commands); it; it=iter_next(it), ii++) { | |
1722 | svccmd = iter_data(it); | |
1723 | tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0])); | |
1724 | tbl.contents[ii][0] = svccmd->name; | |
1725 | tbl.contents[ii][1] = svccmd->command->parent->name; | |
1726 | tbl.contents[ii][2] = svccmd->command->name; | |
1727 | tbl.contents[ii][3] = svccmd->alias.used ? "yes" : "no"; | |
1728 | tbl.contents[ii][4] = extra = malloc(12); | |
1729 | sprintf(extra, "%u", svccmd->uses); | |
1730 | } | |
1731 | reply("MCMSG_SERVICE_INFO", service->bot->nick); | |
1732 | table_send(cmd->parent->bot, user->nick, 0, 0, tbl); | |
1733 | for (ii=0; ii<tbl.length; ii++) { | |
1734 | free((char*)tbl.contents[ii][4]); | |
1735 | free(tbl.contents[ii]); | |
1736 | } | |
1737 | free(tbl.contents); | |
1738 | return 0; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | static MODCMD_FUNC(cmd_showcommands) { | |
1743 | struct svccmd_list commands; | |
1744 | struct helpfile_table tbl; | |
1745 | struct svccmd *svccmd; | |
1746 | dict_iterator_t it; | |
1747 | unsigned int ii, ignore_flags = 0; | |
1748 | unsigned int max_opserv_level = 1000; | |
1749 | unsigned short max_chanserv_level = 500; | |
1750 | char show_opserv_level = 0, show_channel_access = 0; | |
1751 | ||
1752 | /* Check to see what the max access they want to see is. */ | |
1753 | for (ii=1; ii<argc; ++ii) { | |
1754 | if (isdigit(argv[ii][0])) | |
1755 | max_opserv_level = atoi(argv[ii]); | |
1756 | else | |
1757 | max_chanserv_level = user_level_from_name(argv[ii], UL_OWNER); | |
1758 | } | |
1759 | ||
1760 | /* Find the matching commands. */ | |
1761 | svccmd_list_init(&commands); | |
1762 | if (cmd->parent->privileged) | |
1763 | ignore_flags = MODCMD_REQUIRE_OPER; | |
1764 | for (it = dict_first(cmd->parent->commands); it; it = iter_next(it)) { | |
1765 | svccmd = iter_data(it); | |
1766 | if (strchr(svccmd->name, ' ')) | |
1767 | continue; | |
1768 | if (!svccmd_can_invoke(user, svccmd->parent->bot, svccmd, channel, SVCCMD_QUALIFIED)) | |
1769 | continue; | |
1770 | if (svccmd->min_opserv_level > max_opserv_level) | |
1771 | continue; | |
1772 | if (svccmd->min_channel_access > max_chanserv_level) | |
1773 | continue; | |
1774 | if (svccmd->min_opserv_level > 0) | |
1775 | show_opserv_level = 1; | |
1776 | if (svccmd->min_channel_access > 0) | |
1777 | show_channel_access = 1; | |
1778 | if (svccmd->effective_flags | |
1779 | & (MODCMD_REQUIRE_STAFF|MODCMD_REQUIRE_HELPING) | |
1780 | & ~ignore_flags) { | |
1781 | show_channel_access = 1; | |
1782 | } | |
1783 | svccmd_list_append(&commands, svccmd); | |
1784 | } | |
1785 | ||
1786 | /* Build the table. */ | |
1787 | tbl.length = commands.used + 1; | |
1788 | tbl.width = 1 + show_opserv_level + show_channel_access; | |
1789 | tbl.flags = TABLE_REPEAT_ROWS; | |
1790 | tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0])); | |
1791 | tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0])); | |
1792 | tbl.contents[0][ii = 0] = "Command"; | |
1793 | if (show_opserv_level) | |
1794 | tbl.contents[0][++ii] = "OpServ Level"; | |
1795 | if (show_channel_access) | |
1796 | tbl.contents[0][++ii] = "ChanServ Access"; | |
1797 | for (ii=0; ii<commands.used; ++ii) { | |
1798 | svccmd = commands.list[ii]; | |
1799 | tbl.contents[ii+1] = calloc(tbl.width, sizeof(tbl.contents[0][0])); | |
1800 | tbl.contents[ii+1][0] = svccmd->name; | |
1801 | if (show_opserv_level) | |
1802 | tbl.contents[ii+1][1] = strtab(svccmd->min_opserv_level); | |
1803 | if (show_channel_access) { | |
1804 | const char *access; | |
1805 | int flags = svccmd->effective_flags; | |
1806 | if (flags & MODCMD_REQUIRE_HELPING) | |
1807 | access = "helping"; | |
1808 | else if (flags & MODCMD_REQUIRE_STAFF) { | |
1809 | if (flags & MODCMD_REQUIRE_OPER) | |
1810 | access = "oper"; | |
1811 | else if (flags & MODCMD_REQUIRE_NETWORK_HELPER) | |
1812 | access = "net.helper"; | |
1813 | else | |
1814 | access = "staff"; | |
1815 | } else | |
1816 | access = strtab(svccmd->min_channel_access); | |
1817 | tbl.contents[ii+1][1+show_opserv_level] = access; | |
1818 | } | |
1819 | } | |
1820 | svccmd_list_clean(&commands); | |
1821 | table_send(cmd->parent->bot, user->nick, 0, 0, tbl); | |
1822 | return 0; | |
1823 | } | |
1824 | ||
1825 | static MODCMD_FUNC(cmd_helpfiles) { | |
1826 | struct service *service; | |
1827 | unsigned int ii; | |
1828 | ||
1829 | if (!(service = dict_find(services, argv[1], NULL))) { | |
1830 | reply("MCMSG_UNKNOWN_SERVICE", argv[1]); | |
1831 | return 0; | |
1832 | } | |
1833 | ||
1834 | if (argc < 3) { | |
1835 | for (ii=0; ii<service->modules.used; ++ii) | |
1836 | reply("MCMSG_HELPFILE_SEQUENCE", ii+1, service->modules.list[ii]->name); | |
1837 | return 0; | |
1838 | } | |
1839 | ||
1840 | service->modules.used = 0; | |
1841 | for (ii=0; ii<argc-2; ii++) { | |
1842 | struct module *module = dict_find(modules, argv[ii+2], NULL); | |
1843 | if (!module) { | |
1844 | reply("MCMSG_UNKNOWN_MODULE", argv[ii+2]); | |
1845 | continue; | |
1846 | } | |
1847 | module_list_append(&service->modules, module); | |
1848 | } | |
1849 | reply("MCMSG_HELPFILE_SEQUENCE_SET", service->bot->nick); | |
1850 | return 1; | |
1851 | } | |
1852 | ||
1853 | static MODCMD_FUNC(cmd_service_add) { | |
1854 | const char *nick, *hostname, *desc; | |
1855 | struct userNode *bot; | |
1856 | ||
1857 | nick = argv[1]; | |
1858 | if (!is_valid_nick(nick)) { | |
1859 | reply("MCMSG_BAD_SERVICE_NICK", nick); | |
1860 | return 0; | |
1861 | } | |
1862 | hostname = argv[2]; | |
1863 | desc = unsplit_string(argv+3, argc-3, NULL); | |
1864 | bot = GetUserH(nick); | |
1865 | if (bot && IsService(bot)) { | |
1866 | reply("MCMSG_ALREADY_SERVICE", bot->nick); | |
1867 | return 0; | |
1868 | } | |
1869 | bot = AddService(nick, NULL, desc, hostname); | |
1870 | service_register(bot); | |
1871 | reply("MCMSG_NEW_SERVICE", bot->nick); | |
1872 | return 1; | |
1873 | } | |
1874 | ||
1875 | static MODCMD_FUNC(cmd_service_rename) { | |
1876 | struct service *service; | |
1877 | ||
1878 | if (!(service = service_find(argv[1]))) { | |
1879 | reply("MCMSG_UNKNOWN_SERVICE", argv[1]); | |
1880 | return 0; | |
1881 | } | |
1882 | NickChange(service->bot, argv[2], 0); | |
1883 | reply("MCMSG_SERVICE_RENAMED", service->bot->nick); | |
1884 | return 1; | |
1885 | } | |
1886 | ||
1887 | static MODCMD_FUNC(cmd_service_trigger) { | |
1888 | struct userNode *bogon; | |
1889 | struct service *service; | |
1890 | ||
1891 | if (!(service = service_find(argv[1]))) { | |
1892 | reply("MCMSG_UNKNOWN_SERVICE", argv[1]); | |
1893 | return 0; | |
1894 | } | |
1895 | if (argc < 3) { | |
1896 | if (service->trigger) | |
1897 | reply("MCMSG_CURRENT_TRIGGER", service->bot->nick, service->trigger); | |
1898 | else | |
1899 | reply("MCMSG_NO_TRIGGER", service->bot->nick); | |
1900 | return 1; | |
1901 | } | |
1902 | if (service->trigger) | |
1903 | reg_chanmsg_func(service->trigger, NULL, NULL); | |
1904 | if (!irccasecmp(argv[2], "none") || !irccasecmp(argv[2], "remove")) { | |
1905 | service->trigger = 0; | |
1906 | reply("MCMSG_REMOVED_TRIGGER", service->bot->nick); | |
1907 | } else if ((bogon = get_chanmsg_bot(argv[2][0]))) { | |
1908 | reply("MCMSG_DUPLICATE_TRIGGER", bogon->nick, argv[2][0]); | |
1909 | return 1; | |
1910 | } else { | |
1911 | service->trigger = argv[2][0]; | |
1912 | reg_chanmsg_func(service->trigger, service->bot, modcmd_chanmsg); | |
1913 | reply("MCMSG_NEW_TRIGGER", service->bot->nick, service->trigger); | |
1914 | } | |
1915 | return 1; | |
1916 | } | |
1917 | ||
1918 | static MODCMD_FUNC(cmd_service_privileged) { | |
1919 | struct service *service; | |
1920 | const char *newval; | |
1921 | ||
1922 | if (!(service = service_find(argv[1]))) { | |
1923 | reply("MCMSG_UNKNOWN_SERVICE", argv[1]); | |
1924 | return 0; | |
1925 | } | |
1926 | if (argc >= 3) | |
1927 | service->privileged = true_string(argv[2]) || enabled_string(argv[2]); | |
1928 | newval = user_find_message(user, service->privileged ? "MSG_ON" : "MSG_OFF"); | |
1929 | reply("MCMSG_SERVICE_PRIVILEGED", service->bot->nick, newval); | |
1930 | return 1; | |
1931 | } | |
1932 | ||
1933 | static MODCMD_FUNC(cmd_service_remove) { | |
1934 | char *name, *reason; | |
1935 | struct service *service; | |
1936 | ||
1937 | name = argv[1]; | |
1938 | if (argc > 2) | |
1939 | reason = unsplit_string(argv+2, argc-2, NULL); | |
1940 | else | |
1941 | reason = "Removing bot"; | |
1942 | if (!(service = service_find(name))) { | |
1943 | reply("MCMSG_UNKNOWN_SERVICE", name); | |
1944 | return 0; | |
1945 | } | |
1946 | DelUser(service->bot, NULL, 1, reason); | |
1947 | reply("MCMSG_SERVICE_REMOVED", name); | |
1948 | dict_remove(services, name); | |
1949 | return 1; | |
1950 | } | |
1951 | ||
1952 | static MODCMD_FUNC(cmd_dump_messages) { | |
1953 | const char *fname = "strings.db"; | |
1954 | struct saxdb_context *ctx; | |
1955 | dict_iterator_t it; | |
1956 | FILE *pf; | |
1957 | int res; | |
1958 | ||
1959 | if (!(pf = fopen(fname, "w"))) { | |
1960 | reply("MCMSG_FILE_NOT_OPENED", fname); | |
1961 | return 0; | |
1962 | } | |
1963 | if (!(ctx = saxdb_open_context(pf))) { | |
1964 | reply("MSG_INTERNAL_FAILURE"); | |
1965 | return 0; | |
1966 | } | |
1967 | if ((res = setjmp(ctx->jbuf)) != 0) { | |
1968 | ctx->complex.used = 0; /* to avoid false assert()s in close */ | |
1969 | saxdb_close_context(ctx); | |
1970 | fclose(pf); | |
1971 | reply("MCMSG_MESSAGE_DUMP_FAILED", strerror(res)); | |
1972 | return 0; | |
1973 | } else { | |
1974 | for (it = dict_first(lang_C->messages); it; it = iter_next(it)) | |
1975 | saxdb_write_string(ctx, iter_key(it), iter_data(it)); | |
1976 | saxdb_close_context(ctx); | |
1977 | fclose(pf); | |
1978 | reply("MCMSG_MESSAGES_DUMPED", fname); | |
1979 | return 1; | |
1980 | } | |
1981 | } | |
1982 | ||
1983 | static MODCMD_FUNC(cmd_version) { | |
1984 | /* This function provides copyright management information to end | |
1985 | * users of X3. You should not alter, disable or remove this | |
1986 | * command or its accessibility to normal IRC users, except to add | |
1987 | * copyright information pertaining to changes you make to X3. | |
1988 | */ | |
1989 | ||
1990 | char *svn = NULL, *svnver = NULL, *svndate = NULL, *v = 0, ver[MAXLEN]; | |
1991 | int vc = 0; | |
1992 | ||
1993 | strcpy(ver, CVS_VERSION); | |
1994 | for (svn = x3_strtok(&v, ver, " "); svn; | |
1995 | svn = x3_strtok(&v, 0, " ")) { | |
1996 | if (vc == 2) | |
1997 | svnver = svn; | |
1998 | else if (vc == 3) | |
1999 | svndate = svn; | |
2000 | vc++; | |
2001 | } | |
2002 | ||
2003 | send_message_type(4, user, cmd->parent->bot, "$b"PACKAGE_STRING"+[%s %s]$b (Based on srvx 1.3.x), Built: "__DATE__", "__TIME__".", svnver, svndate); | |
2004 | send_message_type(4, user, cmd->parent->bot, "$b$b"); | |
2005 | send_message_type(4, user, cmd->parent->bot, "Copyright 2000-2006 srvx Development Team."); | |
2006 | send_message_type(4, user, cmd->parent->bot, "Copyright 2004-2006 X3 Development Team."); | |
2007 | send_message_type(4, user, cmd->parent->bot, "This software is OSI Certified Open Source Software."); | |
2008 | send_message_type(4, user, cmd->parent->bot, "OSI Certified is a certification mark of the Open Source Initiative."); | |
2009 | send_message_type(4, user, cmd->parent->bot, "$b$b"); | |
2010 | send_message_type(4, user, cmd->parent->bot, "The srvx 1.3.x Development Team includes Paul Chang, Adrian Dewhurst, Miles Peterson, Michael Poole and others."); | |
2011 | send_message_type(4, user, cmd->parent->bot, "The X3 Development Team includes Alex Schumann, Reed Loden, Neil Spierling."); | |
2012 | send_message_type(4, user, cmd->parent->bot, "The X3 Development Team can be reached at http://evilnet.sourceforge.net or in #evilnet on irc.afternet.org."); | |
2013 | send_message_type(4, user, cmd->parent->bot, "$b$b"); | |
2014 | send_message_type(4, user, cmd->parent->bot, "Thanks goes to Adrian M (thiefmaster), Joe Hansche (joeatrr), Martijn Smit (wasted), and to any other people who have contributed to X3."); | |
2015 | send_message_type(4, user, cmd->parent->bot, "This program is free software; see COPYING in the distribution."); | |
2016 | ||
2017 | return 1; | |
2018 | } | |
2019 | ||
2020 | static void create_default_binds(int rebind); | |
2021 | ||
2022 | static MODCMD_FUNC(cmd_rebindall) { | |
2023 | send_message_type(4, user, cmd->parent->bot, "$bRe-binding all default commands to respective services..$b"); | |
2024 | create_default_binds(1); | |
2025 | return 1; | |
2026 | } | |
2027 | ||
2028 | ||
2029 | void | |
2030 | modcmd_nick_change(struct userNode *user, const char *old_nick) { | |
2031 | struct service *svc; | |
2032 | if (!(svc = dict_find(services, old_nick, NULL))) | |
2033 | return; | |
2034 | dict_remove2(services, old_nick, 1); | |
2035 | dict_insert(services, user->nick, svc); | |
2036 | } | |
2037 | ||
2038 | void | |
2039 | modcmd_cleanup(void) { | |
2040 | dict_delete(services); | |
2041 | dict_delete(modules); | |
2042 | if (suf_list) | |
2043 | free(suf_list); | |
2044 | } | |
2045 | ||
2046 | static void | |
2047 | modcmd_saxdb_write_command(struct saxdb_context *ctx, struct svccmd *cmd) { | |
2048 | char buf[MAXLEN]; | |
2049 | unsigned int nn, len, pos; | |
2050 | struct svccmd *template = cmd->command->defaults; | |
2051 | ||
2052 | saxdb_start_record(ctx, cmd->name, 0); | |
2053 | sprintf(buf, "%s.%s", cmd->command->parent->name, cmd->command->name); | |
2054 | saxdb_write_string(ctx, "command", buf); | |
2055 | if (cmd->alias.used) | |
2056 | saxdb_write_string_list(ctx, "aliased", &cmd->alias); | |
2057 | if (cmd->min_opserv_level != template->min_opserv_level) | |
2058 | saxdb_write_int(ctx, "oper_access", cmd->min_opserv_level); | |
2059 | if (cmd->min_channel_access != template->min_channel_access) | |
2060 | saxdb_write_int(ctx, "channel_access", cmd->min_channel_access); | |
2061 | if (cmd->flags != template->flags) { | |
2062 | if (cmd->flags) { | |
2063 | for (nn=pos=0; flags[nn].name; ++nn) { | |
2064 | if (cmd->flags & flags[nn].flag) { | |
2065 | buf[pos++] = '+'; | |
2066 | len = strlen(flags[nn].name); | |
2067 | memcpy(buf+pos, flags[nn].name, len); | |
2068 | pos += len; | |
2069 | buf[pos++] = ','; | |
2070 | } | |
2071 | } | |
2072 | } else { | |
2073 | pos = 1; | |
2074 | } | |
2075 | buf[--pos] = 0; | |
2076 | saxdb_write_string(ctx, "flags", buf); | |
2077 | } | |
2078 | if ((cmd->req_account_flags != template->req_account_flags) | |
2079 | || (cmd->deny_account_flags != template->req_account_flags)) { | |
2080 | pos = 0; | |
2081 | if (cmd->req_account_flags) { | |
2082 | buf[pos++] = '+'; | |
2083 | for (nn=0; nn<32; nn++) | |
2084 | if (cmd->req_account_flags & (1 << nn)) | |
2085 | buf[pos++] = handle_flags[nn]; | |
2086 | } | |
2087 | if (cmd->deny_account_flags) { | |
2088 | buf[pos++] = '-'; | |
2089 | for (nn=0; nn<32; nn++) | |
2090 | if (cmd->deny_account_flags & (1 << nn)) | |
2091 | buf[pos++] = handle_flags[nn]; | |
2092 | } | |
2093 | buf[pos] = 0; | |
2094 | saxdb_write_string(ctx, "account_flags", buf); | |
2095 | } | |
2096 | saxdb_end_record(ctx); | |
2097 | } | |
2098 | ||
2099 | static int | |
2100 | modcmd_saxdb_write(struct saxdb_context *ctx) { | |
2101 | struct string_list slist; | |
2102 | dict_iterator_t it, it2; | |
2103 | struct service *service; | |
2104 | unsigned int ii; | |
2105 | ||
2106 | saxdb_start_record(ctx, "bots", 1); | |
2107 | for (it = dict_first(services); it; it = iter_next(it)) { | |
2108 | char buff[16]; | |
2109 | service = iter_data(it); | |
2110 | saxdb_start_record(ctx, service->bot->nick, 1); | |
2111 | if (service->trigger) { | |
2112 | buff[0] = service->trigger; | |
2113 | buff[1] = '\0'; | |
2114 | saxdb_write_string(ctx, "trigger", buff); | |
2115 | } | |
2116 | saxdb_write_string(ctx, "description", service->bot->info); | |
2117 | saxdb_write_string(ctx, "hostname", service->bot->hostname); | |
2118 | if (service->privileged) | |
2119 | saxdb_write_string(ctx, "privileged", "1"); | |
2120 | saxdb_end_record(ctx); | |
2121 | } | |
2122 | saxdb_end_record(ctx); | |
2123 | ||
2124 | saxdb_start_record(ctx, "services", 1); | |
2125 | for (it = dict_first(services); it; it = iter_next(it)) { | |
2126 | service = iter_data(it); | |
2127 | saxdb_start_record(ctx, service->bot->nick, 1); | |
2128 | for (it2 = dict_first(service->commands); it2; it2 = iter_next(it2)) | |
2129 | modcmd_saxdb_write_command(ctx, iter_data(it2)); | |
2130 | saxdb_end_record(ctx); | |
2131 | } | |
2132 | saxdb_end_record(ctx); | |
2133 | ||
2134 | saxdb_start_record(ctx, "helpfiles", 1); | |
2135 | slist.size = 0; | |
2136 | for (it = dict_first(services); it; it = iter_next(it)) { | |
2137 | service = iter_data(it); | |
2138 | slist.used = 0; | |
2139 | for (ii = 0; ii < service->modules.used; ++ii) | |
2140 | string_list_append(&slist, service->modules.list[ii]->name); | |
2141 | saxdb_write_string_list(ctx, iter_key(it), &slist); | |
2142 | } | |
2143 | if (slist.list) | |
2144 | free(slist.list); | |
2145 | saxdb_end_record(ctx); | |
2146 | ||
2147 | return 0; | |
2148 | } | |
2149 | ||
2150 | static int | |
2151 | append_entry(const char *key, UNUSED_ARG(void *data), void *extra) { | |
2152 | struct helpfile_expansion *exp = extra; | |
2153 | int row = exp->value.table.length++; | |
2154 | exp->value.table.contents[row] = calloc(1, sizeof(char*)); | |
2155 | exp->value.table.contents[row][0] = key; | |
2156 | return 0; | |
2157 | } | |
2158 | ||
2159 | static struct helpfile_expansion | |
2160 | modcmd_expand(const char *variable) { | |
2161 | struct helpfile_expansion exp; | |
2162 | extern struct userNode *message_source; | |
2163 | struct service *service; | |
2164 | ||
2165 | service = dict_find(services, message_source->nick, NULL); | |
2166 | if (!irccasecmp(variable, "index")) { | |
2167 | exp.type = HF_TABLE; | |
2168 | exp.value.table.length = 1; | |
2169 | exp.value.table.width = 1; | |
2170 | exp.value.table.flags = TABLE_REPEAT_ROWS | TABLE_NO_HEADERS; | |
2171 | exp.value.table.contents = calloc(dict_size(service->commands)+1, sizeof(char**)); | |
2172 | exp.value.table.contents[0] = calloc(1, sizeof(char*)); | |
2173 | exp.value.table.contents[0][0] = "Commands"; | |
2174 | dict_foreach(service->commands, append_entry, &exp); | |
2175 | return exp; | |
2176 | } else if (!irccasecmp(variable, "languages")) { | |
2177 | struct string_buffer sbuf; | |
2178 | dict_iterator_t it; | |
2179 | sbuf.used = sbuf.size = 0; | |
2180 | sbuf.list = NULL; | |
2181 | for (it = dict_first(languages); it; it = iter_next(it)) { | |
2182 | string_buffer_append_string(&sbuf, iter_key(it)); | |
2183 | string_buffer_append(&sbuf, ' '); | |
2184 | } | |
2185 | sbuf.list[--sbuf.used] = 0; | |
2186 | exp.type = HF_STRING; | |
2187 | exp.value.str = sbuf.list; | |
2188 | return exp; | |
2189 | } | |
2190 | exp.type = HF_STRING; | |
2191 | exp.value.str = NULL; | |
2192 | return exp; | |
2193 | } | |
2194 | ||
2195 | static void | |
2196 | modcmd_load_bots(struct dict *db, int default_nick) { | |
2197 | dict_iterator_t it; | |
2198 | ||
2199 | for (it = dict_first(db); it; it = iter_next(it)) { | |
2200 | struct record_data *rd; | |
2201 | struct service *svc; | |
2202 | const char *nick, *desc, *hostname; | |
2203 | ||
2204 | rd = iter_data(it); | |
2205 | if (rd->type != RECDB_OBJECT) { | |
2206 | log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'bots/%s' in modcmd db (expected object).", iter_key(it)); | |
2207 | continue; | |
2208 | } | |
2209 | nick = database_get_data(rd->d.object, "nick", RECDB_QSTRING); | |
2210 | if (!nick) { | |
2211 | if (default_nick) | |
2212 | nick = iter_key(it); | |
2213 | else | |
2214 | continue; | |
2215 | } | |
2216 | svc = service_find(nick); | |
2217 | desc = database_get_data(rd->d.object, "description", RECDB_QSTRING); | |
2218 | hostname = database_get_data(rd->d.object, "hostname", RECDB_QSTRING); | |
2219 | if (desc) { | |
2220 | if (!svc) | |
2221 | svc = service_register(AddService(nick, NULL, desc, hostname)); | |
2222 | else if (hostname) | |
2223 | strcpy(svc->bot->hostname, hostname); | |
2224 | desc = database_get_data(rd->d.object, "trigger", RECDB_QSTRING); | |
2225 | if (desc) | |
2226 | svc->trigger = desc[0]; | |
2227 | desc = database_get_data(rd->d.object, "privileged", RECDB_QSTRING); | |
2228 | if (desc && (true_string(desc) || enabled_string(desc))) | |
2229 | svc->privileged = 1; | |
2230 | } | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | static void | |
2235 | modcmd_conf_read(void) { | |
2236 | modcmd_load_bots(conf_get_data("services", RECDB_OBJECT), 0); | |
2237 | } | |
2238 | ||
2239 | void | |
2240 | modcmd_init(void) { | |
2241 | qsort(flags, ArrayLength(flags)-1, sizeof(flags[0]), flags_qsort); | |
2242 | modules = dict_new(); | |
2243 | dict_set_free_data(modules, free_module); | |
2244 | services = dict_new(); | |
2245 | dict_set_free_data(services, free_service); | |
2246 | reg_nick_change_func(modcmd_nick_change); | |
2247 | reg_exit_func(modcmd_cleanup); | |
2248 | conf_register_reload(modcmd_conf_read); | |
2249 | ||
2250 | modcmd_module = module_register("modcmd", MAIN_LOG, "modcmd.help", modcmd_expand); | |
2251 | bind_command = modcmd_register(modcmd_module, "bind", cmd_bind, 4, MODCMD_KEEP_BOUND, "oper_level", "800", NULL); | |
2252 | help_command = modcmd_register(modcmd_module, "help", cmd_help, 1, 0, "flags", "+nolog", NULL); | |
2253 | modcmd_register(modcmd_module, "command", cmd_command, 2, 0, "flags", "+nolog", NULL); | |
2254 | modcmd_register(modcmd_module, "modcmd", cmd_modcmd, 2, MODCMD_KEEP_BOUND, "template", "bind", NULL); | |
2255 | modcmd_register(modcmd_module, "god", cmd_god, 0, MODCMD_REQUIRE_AUTHED, "flags", "+oper,+networkhelper", NULL); | |
2256 | modcmd_register(modcmd_module, "readhelp", cmd_readhelp, 2, 0, "oper_level", "650", NULL); | |
2257 | modcmd_register(modcmd_module, "timecmd", cmd_timecmd, 2, 0, "oper_level", "1", NULL); | |
2258 | modcmd_register(modcmd_module, "unbind", cmd_unbind, 3, 0, "template", "bind", NULL); | |
2259 | modcmd_register(modcmd_module, "joiner", cmd_joiner, 1, 0, NULL); | |
2260 | modcmd_register(modcmd_module, "stats modules", cmd_stats_modules, 1, 0, "flags", "+oper", NULL); | |
2261 | modcmd_register(modcmd_module, "stats services", cmd_stats_services, 1, 0, "flags", "+oper", NULL); | |
2262 | modcmd_register(modcmd_module, "showcommands", cmd_showcommands, 1, 0, "flags", "+acceptchan", NULL); | |
2263 | modcmd_register(modcmd_module, "helpfiles", cmd_helpfiles, 2, 0, "template", "bind", NULL); | |
2264 | modcmd_register(modcmd_module, "service add", cmd_service_add, 4, 0, "flags", "+oper", NULL); | |
2265 | modcmd_register(modcmd_module, "service rename", cmd_service_rename, 3, 0, "flags", "+oper", NULL); | |
2266 | modcmd_register(modcmd_module, "service trigger", cmd_service_trigger, 2, 0, "flags", "+oper", NULL); | |
2267 | modcmd_register(modcmd_module, "service privileged", cmd_service_privileged, 2, 0, "flags", "+oper", NULL); | |
2268 | modcmd_register(modcmd_module, "service remove", cmd_service_remove, 2, 0, "flags", "+oper", NULL); | |
2269 | modcmd_register(modcmd_module, "dumpmessages", cmd_dump_messages, 1, 0, "oper_level", "1000", NULL); | |
2270 | modcmd_register(modcmd_module, "rebindall", cmd_rebindall, 0, MODCMD_KEEP_BOUND, "oper_level", "800", NULL); | |
2271 | version_command = modcmd_register(modcmd_module, "version", cmd_version, 1, 0, NULL); | |
2272 | message_register_table(msgtab); | |
2273 | } | |
2274 | ||
2275 | static void | |
2276 | modcmd_db_load_command(struct service *service, const char *cmdname, struct dict *obj) { | |
2277 | struct svccmd *svccmd; | |
2278 | struct module *module; | |
2279 | struct modcmd *modcmd; | |
2280 | struct string_list *slist; | |
2281 | const char *str, *sep; | |
2282 | char buf[MAXLEN]; | |
2283 | ||
2284 | str = database_get_data(obj, "command", RECDB_QSTRING); | |
2285 | if (!str) { | |
2286 | log_module(MAIN_LOG, LOG_ERROR, "Missing command for service %s command %s in modcmd.db", service->bot->nick, cmdname); | |
2287 | return; | |
2288 | } | |
2289 | sep = strchr(str, '.'); | |
2290 | if (!sep) { | |
2291 | log_module(MAIN_LOG, LOG_ERROR, "Invalid command %s for service %s command %s in modcmd.db", str, service->bot->nick, cmdname); | |
2292 | return; | |
2293 | } | |
2294 | memcpy(buf, str, sep-str); | |
2295 | buf[sep-str] = 0; | |
2296 | if (!(module = module_find(buf))) { | |
2297 | log_module(MAIN_LOG, LOG_ERROR, "Unknown module %s for service %s command %s in modcmd.db", buf, service->bot->nick, cmdname); | |
2298 | return; | |
2299 | } | |
2300 | if (!(modcmd = dict_find(module->commands, sep+1, NULL))) { | |
2301 | log_module(MAIN_LOG, LOG_ERROR, "Unknown command %s in module %s for service %s command %s", sep+1, module->name, service->bot->nick, cmdname); | |
2302 | return; | |
2303 | } | |
2304 | /* Now that we know we have a command to use, fill in the basics. */ | |
2305 | svccmd = calloc(1, sizeof(*svccmd)); | |
2306 | svccmd_insert(service, strdup(cmdname), svccmd, modcmd); | |
2307 | if ((str = database_get_data(obj, "template", RECDB_QSTRING))) { | |
2308 | add_pending_template(svccmd, str); | |
2309 | } else { | |
2310 | svccmd_copy_rules(svccmd, modcmd->defaults); | |
2311 | } | |
2312 | if ((str = database_get_data(obj, "account_flags", RECDB_QSTRING))) { | |
2313 | svccmd->req_account_flags = svccmd->deny_account_flags = 0; | |
2314 | svccmd_configure(svccmd, NULL, service->bot, "account_flags", str); | |
2315 | } | |
2316 | if ((str = database_get_data(obj, "flags", RECDB_QSTRING))) { | |
2317 | svccmd->flags = 0; | |
2318 | svccmd_configure(svccmd, NULL, service->bot, "flags", str); | |
2319 | } | |
2320 | if ((str = database_get_data(obj, "oper_access", RECDB_QSTRING)) | |
2321 | || (str = database_get_data(obj, "opserv_level", RECDB_QSTRING))) { | |
2322 | svccmd_configure(svccmd, NULL, service->bot, "oper_access", str); | |
2323 | } | |
2324 | if ((str = database_get_data(obj, "channel_access", RECDB_QSTRING)) | |
2325 | || (str = database_get_data(obj, "chanserv_level", RECDB_QSTRING))) { | |
2326 | svccmd_configure(svccmd, NULL, service->bot, "channel_access", str); | |
2327 | } | |
2328 | if ((slist = database_get_data(obj, "aliased", RECDB_STRING_LIST))) { | |
2329 | unsigned int nn; | |
2330 | svccmd->alias.used = svccmd->alias.size = slist->used; | |
2331 | svccmd->alias.list = calloc(svccmd->alias.size, sizeof(svccmd->alias.list[0])); | |
2332 | for (nn=0; nn<slist->used; ++nn) | |
2333 | svccmd->alias.list[nn] = strdup(slist->list[nn]); | |
2334 | } | |
2335 | modcmd_set_effective_flags(svccmd); | |
2336 | } | |
2337 | ||
2338 | static struct svccmd * | |
2339 | service_make_alias(struct service *service, const char *alias, ...) { | |
2340 | char *arg, *argv[MAXNUMPARAMS]; | |
2341 | unsigned int nn, argc; | |
2342 | struct svccmd *svccmd, *template; | |
2343 | va_list args; | |
2344 | ||
2345 | va_start(args, alias); | |
2346 | argc = 0; | |
2347 | while (1) { | |
2348 | arg = va_arg(args, char*); | |
2349 | if (!arg) | |
2350 | break; | |
2351 | argv[argc++] = arg; | |
2352 | } | |
2353 | va_end(args); | |
2354 | svccmd = calloc(1, sizeof(*svccmd)); | |
2355 | if (!(template = svccmd_resolve_name(svccmd, argv[0]))) { | |
2356 | log_module(MAIN_LOG, LOG_ERROR, "Invalid base command %s for alias %s in service %s", argv[0], alias, service->bot->nick); | |
2357 | free(svccmd->name); | |
2358 | free(svccmd); | |
2359 | return NULL; | |
2360 | } | |
2361 | if (argc > 1) { | |
2362 | svccmd->alias.used = svccmd->alias.size = argc; | |
2363 | svccmd->alias.list = calloc(svccmd->alias.size, sizeof(svccmd->alias.list[0])); | |
2364 | for (nn=0; nn<argc; nn++) | |
2365 | svccmd->alias.list[nn] = strdup(argv[nn]); | |
2366 | } | |
2367 | svccmd_insert(service, strdup(alias), svccmd, template->command); | |
2368 | svccmd_copy_rules(svccmd, template); | |
2369 | return svccmd; | |
2370 | } | |
2371 | ||
2372 | static int saxdb_present; | |
2373 | ||
2374 | static int | |
2375 | modcmd_saxdb_read(struct dict *db) { | |
2376 | struct dict *db2; | |
2377 | dict_iterator_t it, it2; | |
2378 | struct record_data *rd, *rd2; | |
2379 | struct service *service; | |
2380 | ||
2381 | modcmd_load_bots(database_get_data(db, "bots", RECDB_OBJECT), 1); | |
2382 | db2 = database_get_data(db, "services", RECDB_OBJECT); | |
2383 | if (!db2) { | |
2384 | log_module(MAIN_LOG, LOG_ERROR, "Missing section 'services' in modcmd db."); | |
2385 | return 1; | |
2386 | } | |
2387 | for (it = dict_first(db2); it; it = iter_next(it)) { | |
2388 | rd = iter_data(it); | |
2389 | if (rd->type != RECDB_OBJECT) { | |
2390 | log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'services/%s' in modcmd db (expected object).", iter_key(it)); | |
2391 | continue; | |
2392 | } | |
2393 | if (!(service = service_find(iter_key(it)))) { | |
2394 | log_module(MAIN_LOG, LOG_ERROR, "Unknown service '%s' listed in modcmd db.", iter_key(it)); | |
2395 | continue; | |
2396 | } | |
2397 | for (it2 = dict_first(rd->d.object); it2; it2 = iter_next(it2)) { | |
2398 | rd2 = iter_data(it2); | |
2399 | if (rd2->type != RECDB_OBJECT) { | |
2400 | log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'services/%s/%s' in modcmd db (expected object).", iter_key(it), iter_key(it2)); | |
2401 | continue; | |
2402 | } | |
2403 | modcmd_db_load_command(service, iter_key(it2), rd2->d.object); | |
2404 | } | |
2405 | } | |
2406 | db2 = database_get_data(db, "helpfiles", RECDB_OBJECT); | |
2407 | for (it = dict_first(db2); it; it = iter_next(it)) { | |
2408 | struct module *module; | |
2409 | struct string_list *slist; | |
2410 | unsigned int ii; | |
2411 | ||
2412 | rd = iter_data(it); | |
2413 | if (rd->type != RECDB_STRING_LIST) { | |
2414 | log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'helpfiles/%s' in modcmd db (expected string list).", iter_key(it)); | |
2415 | continue; | |
2416 | } | |
2417 | slist = rd->d.slist; | |
2418 | if (!(service = service_find(iter_key(it)))) { | |
2419 | /* We probably whined about the service being missing above. */ | |
2420 | continue; | |
2421 | } | |
2422 | service->modules.used = 0; | |
2423 | for (ii=0; ii<slist->used; ++ii) { | |
2424 | if (!(module = dict_find(modules, slist->list[ii], NULL))) { | |
2425 | log_module(MAIN_LOG, LOG_ERROR, "Unknown module '%s' listed in modcmd 'helpfiles/%s'.", slist->list[ii], iter_key(it)); | |
2426 | continue; | |
2427 | } | |
2428 | module_list_append(&service->modules, module); | |
2429 | } | |
2430 | } | |
2431 | saxdb_present = 1; | |
2432 | return 0; | |
2433 | } | |
2434 | ||
2435 | static void | |
2436 | create_default_binds(int rebind) { | |
2437 | /* Which services should import which modules by default? */ | |
2438 | struct { | |
2439 | const char *svcname; | |
2440 | /* C is lame and requires a fixed size for this array. | |
2441 | * Be sure you NULL-terminate each array and increment the | |
2442 | * size here if you add more default modules to any | |
2443 | * service. */ | |
2444 | const char *modnames[8]; | |
2445 | } def_binds[] = { | |
2446 | { "ChanServ", { "ChanServ", NULL } }, | |
2447 | { "Global", { "Global", NULL } }, | |
2448 | { "NickServ", { "NickServ", NULL } }, | |
2449 | { "OpServ", { "OpServ", "modcmd", "sendmail", "saxdb", "proxycheck", NULL } }, | |
2450 | { "SpamServ", { "SpamServ", NULL } }, | |
2451 | { NULL, { NULL } } | |
2452 | }; | |
2453 | unsigned int ii, jj; | |
2454 | char buf[128], *nick; | |
2455 | struct service *service; | |
2456 | struct module *module; | |
2457 | ||
2458 | for (ii = 0; def_binds[ii].svcname; ++ii) { | |
2459 | sprintf(buf, "services/%s/nick", def_binds[ii].svcname); | |
2460 | if (!(nick = conf_get_data(buf, RECDB_QSTRING))) | |
2461 | continue; | |
2462 | if (!(service = service_find(nick))) | |
2463 | continue; | |
2464 | if (dict_size(service->commands) > 0 && !rebind) | |
2465 | continue; | |
2466 | ||
2467 | /* Bind the default modules for this service to it */ | |
2468 | for (jj = 0; def_binds[ii].modnames[jj]; ++jj) { | |
2469 | if (!(module = module_find(def_binds[ii].modnames[jj]))) | |
2470 | continue; | |
2471 | service_bind_module(service, module); | |
2472 | } | |
2473 | ||
2474 | /* Bind the help and version commands to this service */ | |
2475 | service_bind_modcmd(service, help_command, help_command->name); | |
2476 | service_bind_modcmd(service, version_command, version_command->name); | |
2477 | ||
2478 | /* Now some silly hax.. (aliases that most people want) */ | |
2479 | if (!irccasecmp(def_binds[ii].svcname, "ChanServ")) { | |
2480 | service_make_alias(service, "addowner", "*chanserv.adduser", "$1", "owner", NULL); | |
2481 | service_make_alias(service, "addcoowner", "*chanserv.adduser", "$1", "coowner", NULL); | |
2482 | service_make_alias(service, "addco", "*chanserv.adduser", "$1", "coowner", NULL); | |
2483 | service_make_alias(service, "addmanager", "*chanserv.adduser", "$1", "manager", NULL); | |
2484 | service_make_alias(service, "addop", "*chanserv.adduser", "$1", "op", NULL); | |
2485 | service_make_alias(service, "addhop", "*chanserv.adduser", "$1", "halfop", NULL); | |
2486 | service_make_alias(service, "addpeon", "*chanserv.adduser", "$1", "peon", NULL); | |
2487 | service_make_alias(service, "delowner", "*chanserv.deluser", "owner", "$1", NULL); | |
2488 | service_make_alias(service, "delcoowner", "*chanserv.deluser", "coowner", "$1", NULL); | |
2489 | service_make_alias(service, "delmanager", "*chanserv.deluser", "manager", "$1", NULL); | |
2490 | service_make_alias(service, "delop", "*chanserv.deluser", "op", "$1", NULL); | |
2491 | service_make_alias(service, "delpeon", "*chanserv.deluser", "peon", "$1", NULL); | |
2492 | service_make_alias(service, "llist", "*chanserv.lamers", "$1", NULL); | |
2493 | service_make_alias(service, "command", "*modcmd.command", NULL); | |
2494 | service_make_alias(service, "god", "*modcmd.god", NULL); | |
2495 | } else if (!irccasecmp(def_binds[ii].svcname, "OpServ")) { | |
2496 | struct svccmd *svccmd; | |
2497 | svccmd = service_make_alias(service, "stats", "*modcmd.joiner", NULL); | |
2498 | svccmd->min_opserv_level = 101; | |
2499 | svccmd = service_make_alias(service, "service", "*modcmd.joiner", NULL); | |
2500 | svccmd->min_opserv_level = 900; | |
2501 | svccmd = service_make_alias(service, "routing", "*modcmd.joiner", NULL); | |
2502 | svccmd->min_opserv_level = 100; | |
2503 | } | |
2504 | } | |
2505 | } | |
2506 | ||
2507 | static void | |
2508 | import_aliases_db() { | |
2509 | struct dict *db; | |
2510 | dict_iterator_t it, it2; | |
2511 | struct record_data *rd, *rd2; | |
2512 | struct service *service; | |
2513 | struct module *module; | |
2514 | ||
2515 | if (!(db = parse_database("aliases.db"))) | |
2516 | return; | |
2517 | for (it = dict_first(db); it; it = iter_next(it)) { | |
2518 | service = service_find(iter_key(it)); | |
2519 | if (!service) | |
2520 | continue; | |
2521 | module = module_find(service->bot->nick); | |
2522 | rd = iter_data(it); | |
2523 | if (rd->type != RECDB_OBJECT) | |
2524 | continue; | |
2525 | for (it2 = dict_first(rd->d.object); it2; it2 = iter_next(it2)) { | |
2526 | struct modcmd *command; | |
2527 | rd2 = iter_data(it2); | |
2528 | if (rd2->type != RECDB_QSTRING) | |
2529 | continue; | |
2530 | command = dict_find(module->commands, rd2->d.qstring, NULL); | |
2531 | if (!command) | |
2532 | continue; | |
2533 | service_bind_modcmd(service, command, iter_key(it2)); | |
2534 | } | |
2535 | } | |
2536 | } | |
2537 | ||
2538 | void | |
2539 | modcmd_finalize(void) { | |
2540 | dict_iterator_t it; | |
2541 | ||
2542 | /* Check databases. */ | |
2543 | saxdb_register("modcmd", modcmd_saxdb_read, modcmd_saxdb_write); | |
2544 | create_default_binds(0); | |
2545 | if (!saxdb_present) | |
2546 | import_aliases_db(); | |
2547 | ||
2548 | /* Register services for their triggers. */ | |
2549 | for (it = dict_first(services); it; it = iter_next(it)) { | |
2550 | struct service *svc = iter_data(it); | |
2551 | if (svc->trigger) | |
2552 | reg_chanmsg_func(svc->trigger, svc->bot, modcmd_chanmsg); | |
2553 | } | |
2554 | ||
2555 | /* Resolve command rule-templates. */ | |
2556 | while (pending_templates) { | |
2557 | struct pending_template *ptempl = pending_templates; | |
2558 | struct svccmd *svccmd; | |
2559 | ||
2560 | pending_templates = ptempl->next; | |
2561 | /* Only overwrite the current template if we have a valid template. */ | |
2562 | if (!strcmp(ptempl->base, "*")) { | |
2563 | /* Do nothing. */ | |
2564 | } else if ((svccmd = svccmd_resolve_name(ptempl->cmd, ptempl->base))) { | |
2565 | svccmd_copy_rules(ptempl->cmd, svccmd); | |
2566 | } else { | |
2567 | assert(ptempl->cmd->parent); | |
2568 | log_module(MAIN_LOG, LOG_ERROR, "Unable to resolve template name %s for command %s in service %s.", ptempl->base, ptempl->cmd->name, ptempl->cmd->parent->bot->nick); | |
2569 | } | |
2570 | free(ptempl->base); | |
2571 | free(ptempl); | |
2572 | } | |
2573 | } |