]> jfr.im git - irc/evilnet/x3.git/blame - src/modcmd.c
selectable ban types
[irc/evilnet/x3.git] / src / modcmd.c
CommitLineData
d76ed9a9 1/* modcmd.c - Generalized module command support
2 * Copyright 2002-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 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
2f61d1d7 21#include "../ChangeLog"
d76ed9a9 22#include "chanserv.h"
23#include "conf.h"
24#include "modcmd.h"
7637f48f 25#include "opserv.h"
d76ed9a9 26#include "saxdb.h"
7637f48f 27#include "timeq.h"
d76ed9a9 28
29struct pending_template {
30 struct svccmd *cmd;
31 char *base;
32 struct pending_template *next;
33};
34
7637f48f 35extern unsigned long god_timeout;
d76ed9a9 36static struct dict *modules;
37static struct dict *services;
38static struct pending_template *pending_templates;
39static struct module *modcmd_module;
40static struct modcmd *bind_command, *help_command, *version_command;
41static 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" },
4048352e 61 { "MCMSG_HELP_COMMAND_ALIAS", "$uAlias for:$u %s" },
b1bf690d 62 { "MCMSG_HELP_COMMAND_HEADER", "Command help for: $b%s$b" },
ed05669c 63 { "MCMSG_HELP_COMMAND_UNKNOWN", "No help available for that command." },
b1bf690d 64 { "MCMSG_HELP_TOPIC_HEADER", "Help topic: $b%s$b" },
65 { "MCMSG_HELP_DIVIDER", "=---------------------------------------=" },
66 { "MCMSG_HELP_FOOTER", "=------------- End of Help -------------=" },
d76ed9a9 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." },
7637f48f 136 { "MCMSG_GOD_EXPIRED", "Security override expired." },
d76ed9a9 137 { NULL, NULL }
138};
139struct 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
148static 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 },
0d16e639 158 { "helping", MODCMD_REQUIRE_HELPING },
d76ed9a9 159 { "ignore_csuspend", MODCMD_IGNORE_CSUSPEND },
160 { "joinable", MODCMD_REQUIRE_JOINABLE },
161 { "keepbound", MODCMD_KEEP_BOUND },
162 { "loghostmask", MODCMD_LOG_HOSTMASK },
d76ed9a9 163 { "networkhelper", MODCMD_REQUIRE_NETWORK_HELPER },
164 { "never_csuspend", MODCMD_NEVER_CSUSPEND },
0d16e639 165 { "nolog", MODCMD_NO_LOG },
d76ed9a9 166 { "oper", MODCMD_REQUIRE_OPER },
167 { "qualified", MODCMD_REQUIRE_QUALIFIED },
168 { "regchan", MODCMD_REQUIRE_REGCHAN },
169 { "supporthelper", MODCMD_REQUIRE_SUPPORT_HELPER },
d76ed9a9 170 { "toy", MODCMD_TOY },
171 { NULL, 0 }
172};
173
174static int
175flags_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
181static int
182flags_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
187DEFINE_LIST(svccmd_list, struct svccmd*);
188DEFINE_LIST(module_list, struct module*);
189
190static void
191free_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
205static void
206free_service(void *data) {
207 struct service *service = data;
208 dict_delete(service->commands);
209 module_list_clean(&service->modules);
210 free(service);
211}
212
213static void
214free_module_command(void *data) {
215 struct modcmd *modcmd = data;
216 free_service_command(modcmd->defaults);
217 free(modcmd->name);
218 free(modcmd);
219}
220
221static void
222free_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
230struct module *
231module_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
248struct module *
249module_find(const char *name) {
250 return dict_find(modules, name, NULL);
251}
252
253static void
254add_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
2187a4e3 262struct svccmd *
d76ed9a9 263svccmd_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
288static void
289modcmd_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
304static void
305svccmd_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
316static int
317svccmd_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
399struct modcmd *
400modcmd_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 */
445int
446svccmd_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) {
0d16e639 558 if (!oper_has_access(user, bot, cmd->min_opserv_level, !(options & SVCCMD_NOISY)))
559 return 0;
d76ed9a9 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)
0d16e639 580 && (!uData || (uData->access > 500))
d76ed9a9 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
589static int
590svccmd_expand_alias(struct svccmd *cmd, 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 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);
629 return 0;
630 }
631 }
632 return new_argc;
633}
634
635int
636svccmd_invoke_argv(struct userNode *user, struct service *service, struct chanNode *channel, unsigned int argc, char *argv[], unsigned int server_qualified) {
637 extern struct userNode *chanserv;
638 struct svccmd *cmd;
639 unsigned int cmd_arg, perms, flags, options;
640 char channel_name[CHANNELLEN+1];
641
642 /* First check pubcmd for the channel. */
643 if (channel && (channel->channel_info) && (service->bot == chanserv)
644 && !check_user_level(channel, user, lvlPubCmd, 1, 0)) {
645 send_message(user, service->bot, "MCMSG_PUBLIC_DENY", channel->name);
646 return 0;
647 }
648
649 options = (server_qualified ? SVCCMD_QUALIFIED : 0) | SVCCMD_DEBIT | SVCCMD_NOISY;
650 /* Find the command argument. */
651 cmd_arg = IsChannelName(argv[0]) ? 1 : 0;
652 if (argc < cmd_arg+1) {
653 send_message(user, service->bot, "MCMSG_MISSING_COMMAND");
654 return 0;
655 }
656 if (!isalnum(*argv[cmd_arg])) {
657 /* Silently ignore stuff that doesn't begin with a letter or number. */
658 return 0;
659 }
660 cmd = dict_find(service->commands, argv[cmd_arg], NULL);
661 if (!cmd) {
662 if (!channel)
663 send_message(user, service->bot, "MSG_COMMAND_UNKNOWN", argv[cmd_arg]);
664 return 0;
665 }
666 flags = cmd->effective_flags;
667 /* If they put a channel name first, check if the command allows
668 * it. If so, swap it with the command name.
669 */
670 if (cmd_arg == 1) {
671 char *tmp;
672 /* Complain if we're not supposed to accept the channel. */
673 if (!(flags & MODCMD_ACCEPT_CHANNEL)) {
674 send_message(user, service->bot, "MCMSG_NO_CHANNEL_BEFORE");
675 return 0;
676 }
677 if (!(flags & MODCMD_ACCEPT_PCHANNEL)
678 && (argv[0][0] == '+')) {
679 send_message(user, service->bot, "MCMSG_NO_PLUS_CHANNEL");
680 return 0;
681 }
682 tmp = argv[1];
683 argv[1] = argv[0];
684 argv[0] = tmp;
685 }
686
687 /* Try to grab a channel handle before alias expansion.
688 * If the command accepts a channel name, and argv[1] is
689 * one, use it as a channel name, and hide it from logging.
690 */
691 if ((argc > 1)
692 && (flags & MODCMD_ACCEPT_CHANNEL)
693 && IsChannelName(argv[1])
694 && ((argv[1][0] != '+') || (flags & MODCMD_ACCEPT_PCHANNEL))
695 && (channel = dict_find(channels, argv[1], NULL))) {
696 argv[1] = argv[0];
697 argv++, argc--;
698 cmd_arg = 1;
699 }
700
701 /* Expand the alias arguments, if there are any. */
702 if (cmd->alias.used) {
703 char *new_argv[MAXNUMPARAMS];
704 argc = svccmd_expand_alias(cmd, argc, argv, new_argv);
705 if (!argc) {
706 send_message(service->bot, user, "MCMSG_ALIAS_ERROR", cmd->name);
707 return 0;
708 }
709 argv = new_argv;
710
711 /* Try again to grab a handle to the channel after alias
712 * expansion, overwriting any previous channel. This should,
713 * of course, only be done again if an alias was acually
714 * expanded. */
715 if ((argc > 1)
716 && (flags & MODCMD_ACCEPT_CHANNEL)
717 && IsChannelName(argv[1])
718 && ((argv[1][0] != '+') || (flags & MODCMD_ACCEPT_PCHANNEL))
719 && (channel = dict_find(channels, argv[1], NULL))) {
720 argv[1] = argv[0];
721 argv++, argc--;
722 cmd_arg = 1;
723 }
724 }
725
726 /* Figure out what actions we should do for it.. */
727 if (cmd_arg && (flags & MODCMD_TOY)) {
728 /* Do not let user manually specify a channel. */
729 channel = NULL;
730 }
731 if (argc < cmd->command->min_argc) {
732 send_message(user, service->bot, "MSG_MISSING_PARAMS", cmd->name);
567a5f26 733 svccmd_send_help_brief(user, service->bot, cmd);
d76ed9a9 734 return 0;
735 }
736 if (!cmd->command->func) {
737 send_message(user, service->bot, "MCMSG_INTERNAL_COMMAND", cmd->name);
738 return 0;
739 }
740 perms = svccmd_can_invoke(user, service->bot, cmd, channel, options);
741 if (!perms)
742 return 0;
743 cmd->uses++;
744 if (perms & ACTION_NOCHANNEL)
745 channel = NULL;
746
747 if (channel)
748 safestrncpy(channel_name, channel->name, sizeof(channel_name));
749 else
750 channel_name[0] = 0;
751 if (!cmd->command->func(user, channel, argc, argv, cmd))
752 return 0;
753 if (!(flags & MODCMD_NO_LOG)) {
754 enum log_severity slvl;
755 if (perms & ACTION_STAFF)
756 slvl = LOG_STAFF;
757 else if (perms & ACTION_OVERRIDE)
758 slvl = LOG_OVERRIDE;
759 else
760 slvl = LOG_COMMAND;
761 /* Unsplit argv after running the function to get the benefit
762 * of any mangling/hiding done by the commands. */
2187a4e3 763 log_audit(cmd->command->parent->clog, slvl, user, service->bot, channel_name, AUDIT_HOSTMASK, unsplit_string(argv, argc, NULL));
d76ed9a9 764 }
765 return 1;
766}
767
b1bf690d 768/* First line (syntax usually) only help.. used for wrong param counts etc */
fc8798ec 769int
567a5f26 770svccmd_send_help_brief(struct userNode *user, struct userNode *bot, struct svccmd *cmd) {
fc8798ec 771 int r;
fc8798ec 772
180e0971 773 /* If it's an alias, show what it's an alias for. */
774 if (cmd->alias.used) {
775 char alias_text[MAXLEN];
776 unsplit_string((char**)cmd->alias.list, cmd->alias.used, alias_text);
777 send_message(user, bot, "MCMSG_COMMAND_ALIASES", cmd->name, cmd->command->name);
778 }
b1bf690d 779 /* Send the syntax line of help.. */
567a5f26 780 r = send_help_brief(user, bot, cmd->command->parent->helpfile, cmd->name);
180e0971 781 if(!r) {
782 if(cmd->command->name)
783 {
4cf6e271 784 send_message(user, bot, "MCMSG_COMMAND_ALIASES", cmd->name, cmd->command->name);
180e0971 785 r = send_help_brief(user, bot, cmd->command->parent->helpfile, cmd->command->name);
786 }
787 }
fc8798ec 788 return r;
789}
790
6aae9404 791int is_joiner(struct svccmd *cmd)
792{
793 if(cmd->command->name)
794 {
795 if(strcasecmp("joiner", cmd->command->name))
796 return(0);
797 else
798 return(1);
799 }
800 return(0);
801/*
802 if(cmd->alias.used)
803 {
804 char alias_text[MAXLEN];
805 unsplit_string((char**)cmd->alias.list, cmd->alias.used, alias_text);
806 if(strcasecmp("joiner", alias_text))
807 return(0);
808 else
809 return(1);
810 }
811*/
812}
813
d76ed9a9 814int
b1bf690d 815svccmd_send_help(struct userNode *user, struct service *service, const char *topic) {
d76ed9a9 816 struct module *module;
817 struct svccmd *cmd;
b1bf690d 818 char cmdname[MAXLEN];
ed05669c 819 unsigned int nn, ii;
1d957482 820 int helpsent = 0;
4048352e 821
822 /* If there is no topic show the index */
d76ed9a9 823 if (!topic)
824 topic = "<index>";
b1bf690d 825 /* make heading str (uppercase) */
826 for (nn=0; topic[nn]; nn++)
827 cmdname[nn] = toupper(topic[nn]);
828 cmdname[nn] = 0;
829
6aae9404 830 /* If there is a command 'topic', send command help for the command (unless its bound to JOINER) */
831 if ((cmd = dict_find(service->commands, topic, NULL)) && !is_joiner(cmd))
b1bf690d 832 {
833 send_message(user, service->bot, "MCMSG_HELP_COMMAND_HEADER", cmdname);
834 send_message(user, service->bot, "MCMSG_HELP_DIVIDER");
1d957482 835 helpsent = send_help(user, service->bot, cmd->command->parent->helpfile, cmd->name);
b1bf690d 836
837 /* Show if its an alias, or a binding of another command */
838 if (cmd->alias.used)
839 {
840 char alias_text[MAXLEN];
841 unsplit_string((char**)cmd->alias.list, cmd->alias.used, alias_text);
842 send_message(user, service->bot, "MCMSG_HELP_COMMAND_ALIAS", alias_text);
1d957482 843 /* If send_help above didnt work, try again with the referenced command.. */
844 if(!helpsent)
845 helpsent = send_help(user, service->bot, cmd->command->parent->helpfile, alias_text);
b1bf690d 846 }
847 else if(cmd->command->name && strcasecmp(cmd->command->name, cmd->name))
848 {
849 send_message(user, service->bot, "MCMSG_HELP_COMMAND_ALIAS", cmd->command->name);
1d957482 850 /* If send_help above didnt work, try again with the referenced command.. */
851 if(!helpsent)
852 helpsent = send_help(user, service->bot, cmd->command->parent->helpfile, cmd->command->name);
b1bf690d 853 }
1d957482 854 /* If send_help still couldnt find it, tell them sorry */
855 if(!helpsent)
ed05669c 856 send_message(user, service->bot, "MCMSG_HELP_COMMAND_UNKNOWN");
b1bf690d 857 send_message(user, service->bot, "MCMSG_HELP_FOOTER");
858 return true;
859 }
860 else /* look for topic in the help files loaded to this nick/service */
861 {
ed05669c 862 /* Check for non command help in first primary help file, then next and so on */
863 /* Note - we need to think about default bindings. see opserv.helpfiles */
864 for(ii = 0; ii < service->modules.used; ii++)
b1bf690d 865 {
ed05669c 866 module = service->modules.list[ii];
867 if(!module->helpfile)
868 continue;
869 if(dict_find(module->helpfile->db, topic, NULL))
870 {
871 if (module->helpfile && dict_find(module->helpfile->db, topic, NULL))
872 {
873
874 send_message(user, service->bot, "MCMSG_HELP_TOPIC_HEADER", cmdname);
875 send_message(user, service->bot, "MCMSG_HELP_DIVIDER");
876 /* This should never fail but maybe if something is odd? */
877 if(!send_help(user, service->bot, module->helpfile, topic))
878 send_message(user, service->bot, "MSG_TOPIC_UNKNOWN");
879 send_message(user, service->bot, "MCMSG_HELP_FOOTER");
880 return true;
881 }
882 }
b1bf690d 883 }
d76ed9a9 884 }
4048352e 885 /* Otherwise say we cant find it */
d76ed9a9 886 send_message(user, service->bot, "MSG_TOPIC_UNKNOWN");
887 return 0;
888}
889
890static int
891svccmd_invoke(struct userNode *user, struct service *service, struct chanNode *channel, char *text, int server_qualified) {
892 unsigned int argc;
893 char *argv[MAXNUMPARAMS];
894
895 if (!*text)
896 return 0;
897 if (service->privileged) {
898 if (!IsOper(user)) {
899 send_message(user, service->bot, "MSG_SERVICE_PRIVILEGED", service->bot->nick);
900 return 0;
901 }
902 if (!user->handle_info) {
903 send_message(user, service->bot, "MSG_AUTHENTICATE");
904 return 0;
905 }
906 if (HANDLE_FLAGGED(user->handle_info, OPER_SUSPENDED)) {
907 send_message(user, service->bot, "MSG_OPER_SUSPENDED");
908 return 0;
909 }
910 }
911 argc = split_line(text, false, ArrayLength(argv), argv);
912 return argc ? svccmd_invoke_argv(user, service, channel, argc, argv, server_qualified) : 0;
913}
914
915void
916modcmd_privmsg(struct userNode *user, struct userNode *bot, char *text, int server_qualified) {
917 struct service *service;
918
919 if (!(service = dict_find(services, bot->nick, NULL))) {
920 log_module(MAIN_LOG, LOG_ERROR, "modcmd_privmsg got privmsg for unhandled service %s, unregistering.", bot->nick);
921 reg_privmsg_func(bot, NULL);
922 return;
923 }
924
925 if (text[0] == '\x01') {
926 char *term, response[MAXLEN];
927
928 text++; /* Skip leading ^A. */
929 /* Chop off final ^A. */
930 term = strchr(text, '\x01');
931 if (!term)
932 return;
933 *term = '\0';
934 /* Parse out leading text. */
935 term = strchr(text, ' ');
936 if (term) {
937 *term++ = '\0';
938 if (!*term)
939 term = NULL;
940 }
941 /* No dict lookup since these are so few. */
942 if (!irccasecmp(text, "CLIENTINFO")) {
943 /* Use \001 instead of \x01 because apparently \x01C is
944 * interpreted as ASCII FS (\034, decimal 28, hex 1C).
945 */
946 irc_notice_user(bot, user, "\001CLIENTINFO CLIENTINFO PING TIME USERINFO VERSION\x01");
947 } else if (!irccasecmp(text, "PING")) {
948 if (term) {
ec1a68c8 949 snprintf(response, sizeof(response), "\x01PING %s\x01", term);
d76ed9a9 950 irc_notice_user(bot, user, response);
951 } else {
ec1a68c8 952 irc_notice_user(bot,user, "\x01PING\x01");
d76ed9a9 953 }
954 } else if (!irccasecmp(text, "TIME")) {
955 struct tm tm;
956 localtime_r(&now, &tm);
957 strftime(response, sizeof(response), "\x01TIME %a %b %d %H:%M:%S %Y\x01", &tm);
958 irc_notice_user(bot, user, response);
959 } else if (!irccasecmp(text, "USERINFO")) {
960 snprintf(response, sizeof(response), "\x01USERINFO %s\x01", bot->info);
961 irc_notice_user(bot, user, response);
962 } else if (!irccasecmp(text, "VERSION")) {
963 /* This function provides copyright management information
ceafd592 964 * to end users of X3. You should not alter, disable or
d76ed9a9 965 * remove this command or its accessibility to normal IRC
966 * users, except to add copyright information pertaining
ceafd592 967 * to changes you make to X3.
d76ed9a9 968 */
ceafd592 969 snprintf(response, sizeof(response), "\x01VERSION %s\x01", PACKAGE_STRING);
d76ed9a9 970 irc_notice_user(bot, user, response);
23475fc6 971 } else if (!irccasecmp(text, "GENDER")) {
972 snprintf(response, sizeof(response), "\x01GENDER ummm im still deciding\x01");
973 irc_notice_user(bot, user, response);
d76ed9a9 974 }
975 return;
976 }
977
978 if (service->msg_hook && service->msg_hook(user, bot, text, server_qualified))
979 return;
980 svccmd_invoke(user, service, NULL, text, server_qualified);
981}
982
983void
984modcmd_chanmsg(struct userNode *user, struct chanNode *chan, char *text, struct userNode *bot) {
985 struct service *service;
986 if (!(service = dict_find(services, bot->nick, NULL))) return;
987 svccmd_invoke(user, service, chan, text, 0);
988}
989
990struct service *
991service_register(struct userNode *bot) {
992 struct service *service;
993 if ((service = dict_find(services, bot->nick, NULL)))
994 return service;
995 service = calloc(1, sizeof(*service));
996 module_list_init(&service->modules);
997 service->commands = dict_new();
998 service->bot = bot;
999 dict_set_free_data(service->commands, free_service_command);
1000 dict_insert(services, service->bot->nick, service);
1001 reg_privmsg_func(bot, modcmd_privmsg);
1002 return service;
1003}
1004
1005struct service *
1006service_find(const char *name) {
1007 return dict_find(services, name, NULL);
1008}
1009
1010static void
1011svccmd_insert(struct service *service, char *name, struct svccmd *svccmd, struct modcmd *modcmd) {
1012 unsigned int ii;
1013 svccmd->parent = service;
1014 svccmd->name = name;
1015 svccmd->command = modcmd;
1016 svccmd->command->bind_count++;
1017 dict_insert(service->commands, svccmd->name, svccmd);
1018 for (ii=0; ii<service->modules.used; ++ii) {
1019 if (service->modules.list[ii] == svccmd->command->parent) break;
1020 }
1021 if (ii == service->modules.used) {
1022 module_list_append(&service->modules, svccmd->command->parent);
1023 }
1024}
1025
1026struct svccmd *
1027service_bind_modcmd(struct service *service, struct modcmd *cmd, const char *name) {
1028 struct svccmd *svccmd;
1029 if ((svccmd = dict_find(service->commands, name, NULL))) {
1030 if (svccmd->command == cmd) return svccmd;
1031 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);
1032 return NULL;
1033 }
1034 svccmd = calloc(1, sizeof(*svccmd));
1035 svccmd_insert(service, strdup(name), svccmd, cmd);
1036 svccmd_copy_rules(svccmd, cmd->defaults);
1037 return svccmd;
1038}
1039
1040static unsigned int
1041service_bind_module(struct service *service, struct module *module) {
1042 dict_iterator_t it;
1043 struct modcmd *modcmd;
1044 unsigned int count;
1045
1046 count = 0;
1047 for (it = dict_first(module->commands); it; it = iter_next(it)) {
1048 modcmd = iter_data(it);
1049 if (!((modcmd->flags | modcmd->defaults->flags) & MODCMD_NO_DEFAULT_BIND))
1050 if (service_bind_modcmd(service, modcmd, iter_key(it)))
1051 count++;
1052 }
1053 return count;
1054}
1055
1056/* This MUST return argc if the alias expansion code knows how to deal
1057 * with every argument in argv; otherwise, it MUST return the index of
1058 * an argument that the expansion code does not know how to deal with.
1059 */
1060static unsigned int
1061check_alias_args(char *argv[], unsigned int argc) {
1062 unsigned int arg;
1063
1064 for (arg=0; arg<argc; ++arg) {
1065 if (argv[arg][0] != '$') {
1066 continue;
1067 } else if (argv[arg][1] == '$') {
1068 continue;
1069 } else if (isdigit(argv[arg][1])) {
1070 char *end_num;
1071
1072 strtoul(argv[arg]+1, &end_num, 10);
1073 switch (end_num[0]) {
1074 case 0:
1075 continue;
1076 case '-':
1077 if (end_num[1] == 0)
1078 continue;
1079 else if (isdigit(end_num[1]))
1080 continue;
1081 /* else fall through to default case */
1082 default:
1083 return arg;
1084 }
1085 } else
1086 return arg;
1087 }
1088 return arg;
1089}
1090
1091static unsigned int
1092collapse_cmdname(char *argv[], unsigned int argc, char *dest) {
1093 unsigned int ii, pos, arg;
1094 if (!argc) {
1095 dest[0] = 0;
1096 return 0;
1097 }
1098 for (ii=pos=0, arg=0; argv[arg][ii]; ) {
1099 if (argv[arg][ii] == '\\') {
1100 if (argv[arg][ii+1]) {
1101 /* escaping a real character just puts it in literally */
1102 dest[pos++] = argv[arg][++ii];
1103 } else if ((arg+1) == argc) {
1104 /* we ran to the end of the argument list; abort */
1105 break;
1106 } else {
1107 /* escape at end of a word is a space */
1108 dest[pos++] = ' ';
1109 ii = 0;
1110 arg++;
1111 }
1112 } else {
1113 /* normal characters don't need escapes */
1114 dest[pos++] = argv[arg][ii++];
1115 }
1116 }
1117 dest[pos] = 0;
1118 return arg + 1;
1119}
1120
1121static MODCMD_FUNC(cmd_bind) {
1122 struct service *service;
1123 struct svccmd *template, *newcmd;
1124 char *svcname, *dot;
1125 char newname[MAXLEN], cmdname[MAXLEN];
1126 unsigned int arg, diff;
1127
1128 assert(argc > 3);
1129 svcname = argv[1];
1130 arg = collapse_cmdname(argv+2, argc-2, newname) + 2;
1131 if (!arg) {
1132 reply("MSG_MISSING_PARAMS", cmd->name);
1133 return 0;
1134 }
1135 diff = collapse_cmdname(argv+arg, argc-arg, cmdname);
1136 if (!diff) {
1137 reply("MSG_MISSING_PARAMS", cmd->name);
1138 return 0;
1139 }
1140 arg += diff;
1141
1142 if (!(service = service_find(svcname))) {
1143 reply("MCMSG_UNKNOWN_SERVICE", svcname);
1144 return 0;
1145 }
1146
1147 if ((newcmd = dict_find(service->commands, newname, NULL))) {
1148 reply("MCMSG_ALREADY_BOUND", service->bot->nick, newname);
1149 return 0;
1150 }
1151
1152 if ((dot = strchr(cmdname, '.')) && (dot[1] == '*') && (dot[2] == 0)) {
1153 unsigned int count;
1154 struct module *module;
1155 *dot = 0;
1156 module = module_find((cmdname[0] == '*') ? cmdname+1 : cmdname);
1157 if (!module) {
1158 reply("MSG_MODULE_UNKNOWN", cmdname);
1159 return 0;
1160 }
1161 count = service_bind_module(service, module);
1162 reply("MCMSG_MODULE_BOUND", count, module->name, service->bot->nick);
1163 return count != 0;
1164 }
1165 newcmd = calloc(1, sizeof(*newcmd));
1166 newcmd->name = strdup(newname);
1167 newcmd->parent = service;
1168 if (!(template = svccmd_resolve_name(newcmd, cmdname))) {
1169 reply("MCMSG_UNKNOWN_COMMAND_2", cmdname, service->bot->nick);
1170 free(newcmd->name);
1171 free(newcmd);
1172 return 0;
1173 }
1174 if (template->alias.used) {
1175 reply("MCMSG_CANNOT_DOUBLE_ALIAS");
1176 free(newcmd->name);
1177 free(newcmd);
1178 return 0;
1179 }
1180
1181 if (argc > arg) {
1182 /* a more complicated alias; fix it up */
1183 unsigned int nn;
1184
1185 arg -= diff;
1186 nn = check_alias_args(argv+arg, argc-arg);
1187 if (nn+arg < argc) {
1188 reply("MCMSG_BAD_ALIAS_ARGUMENT", argv[nn+arg]);
1189 free(newcmd->name);
1190 free(newcmd);
1191 return 0;
1192 }
1193 newcmd->alias.used = newcmd->alias.size = argc-arg;
1194 newcmd->alias.list = calloc(newcmd->alias.size, sizeof(newcmd->alias.list[0]));
1195 for (nn=0; nn<newcmd->alias.used; ++nn)
1196 newcmd->alias.list[nn] = strdup(argv[nn+arg]);
1197 }
1198
1199 svccmd_insert(service, newcmd->name, newcmd, template->command);
1200 svccmd_copy_rules(newcmd, template);
1201 reply("MCMSG_COMMAND_BOUND", newcmd->name, newcmd->parent->bot->nick);
1202 return 1;
1203}
1204
1205static int
1206service_recheck_bindings(struct service *service, struct module *module) {
1207 dict_iterator_t it;
1208 struct svccmd *cmd;
1209
1210 for (it = dict_first(service->commands); it; it = iter_next(it)) {
1211 cmd = iter_data(it);
1212 if (cmd->command->parent == module) return 0;
1213 }
1214 /* No more bindings, remove it from our list. */
1215 module_list_remove(&service->modules, module);
1216 return 1;
1217}
1218
1219static svccmd_unbind_func_t *suf_list;
1220unsigned int suf_size, suf_used;
1221
1222void
1223reg_svccmd_unbind_func(svccmd_unbind_func_t handler) {
1224 if (suf_used == suf_size) {
1225 if (suf_size) {
1226 suf_size <<= 1;
1227 suf_list = realloc(suf_list, suf_size*sizeof(svccmd_unbind_func_t));
1228 } else {
1229 suf_size = 8;
1230 suf_list = malloc(suf_size*sizeof(svccmd_unbind_func_t));
1231 }
1232 }
1233 suf_list[suf_used++] = handler;
1234}
1235
1236static MODCMD_FUNC(cmd_unbind) {
1237 struct service *service;
1238 struct userNode *bot;
1239 struct svccmd *bound;
1240 struct module *module;
1241 const char *svcname;
1242 unsigned int arg, ii;
1243 char cmdname[MAXLEN];
1244
1245 assert(argc > 2);
1246 svcname = argv[1];
1247 arg = collapse_cmdname(argv+2, argc-2, cmdname) + 2;
1248 if (!arg) {
1249 reply("MSG_MISSING_PARAMS", cmd->name);
1250 return 0;
1251 }
1252 if (!(service = service_find(svcname))) {
1253 reply("MCMSG_UNKNOWN_SERVICE", svcname);
1254 return 0;
1255 }
1256 if (!(bound = dict_find(service->commands, cmdname, NULL))) {
1257 reply("MCMSG_NO_COMMAND_BOUND", service->bot->nick, cmdname);
1258 return 0;
1259 }
1260 if ((bound->command->flags & MODCMD_KEEP_BOUND) && (bound->command->bind_count == 1)) {
1261 reply("MCMSG_UNBIND_PROHIBITED", bound->command->name);
1262 return 0;
1263 }
1264
1265 for (ii=0; ii<suf_used; ii++)
1266 suf_list[ii](bound);
1267 /* If this command binding is removing itself, we must take care
1268 * not to dereference it after the dict_remove.
1269 */
1270 bot = cmd->parent->bot;
1271 module = cmd->command->parent;
1272 dict_remove(service->commands, bound->name);
1273 send_message(user, bot, "MCMSG_COMMAND_UNBOUND", cmdname, service->bot->nick);
1274 if (service_recheck_bindings(service, module))
1275 send_message(user, bot, "MCMSG_HELPFILE_UNBOUND", module->name, module->name);
1276 return 1;
1277}
1278
1279static MODCMD_FUNC(cmd_readhelp) {
1280 const char *modname;
1281 struct module *module;
1282 struct helpfile *old_helpfile;
1283 struct timeval start, stop;
1284
1285 assert(argc > 1);
1286 modname = argv[1];
1287 if (!(module = module_find(modname))) {
1288 reply("MSG_MODULE_UNKNOWN", modname);
1289 return 0;
1290 }
1291 if (!module->helpfile_name) {
1292 reply("MCMSG_NO_HELPFILE", module->name);
1293 return 0;
1294 }
1295 old_helpfile = module->helpfile;
1296 gettimeofday(&start, NULL);
1297 module->helpfile = open_helpfile(module->helpfile_name, module->expand_help);
1298 if (!module->helpfile) {
1299 module->helpfile = old_helpfile;
1300 reply("MCMSG_HELPFILE_ERROR", module->helpfile_name);
1301 return 0;
1302 }
1303 if (old_helpfile) close_helpfile(old_helpfile);
1304 gettimeofday(&stop, NULL);
1305 stop.tv_sec -= start.tv_sec;
1306 stop.tv_usec -= start.tv_usec;
1307 if (stop.tv_usec < 0) {
6aae9404 1308 stop.tv_sec -= 1;
1309 stop.tv_usec += 1000000;
d76ed9a9 1310 }
1311 reply("MCMSG_HELPFILE_READ", module->name, stop.tv_sec, stop.tv_usec/1000);
1312 return 1;
1313}
1314
1315static MODCMD_FUNC(cmd_help) {
1316 const char *topic;
1317
1318 topic = (argc < 2) ? NULL : unsplit_string(argv+1, argc-1, NULL);
b1bf690d 1319 return svccmd_send_help(user, cmd->parent, topic);
d76ed9a9 1320}
1321
1322static MODCMD_FUNC(cmd_timecmd) {
1323 struct timeval start, stop;
1324 char cmd_text[MAXLEN];
1325
1326 unsplit_string(argv+1, argc-1, cmd_text);
1327 gettimeofday(&start, NULL);
1328 svccmd_invoke(user, cmd->parent, channel, cmd_text, 0);
1329 gettimeofday(&stop, NULL);
1330 stop.tv_sec -= start.tv_sec;
1331 stop.tv_usec -= start.tv_usec;
1332 if (stop.tv_usec < 0) {
6aae9404 1333 stop.tv_sec -= 1;
1334 stop.tv_usec += 1000000;
d76ed9a9 1335 }
1336 reply("MCMSG_COMMAND_TIME", cmd_text, stop.tv_sec, stop.tv_usec);
1337 return 1;
1338}
1339
1340static MODCMD_FUNC(cmd_command) {
1341 struct svccmd *svccmd;
1342 const char *cmd_name, *fmt_str;
1343 unsigned int flags, shown_flags, nn, pos;
1344 char buf[MAXLEN];
1345
1346 assert(argc >= 2);
1347 cmd_name = unsplit_string(argv+1, argc-1, NULL);
1348 if (!(svccmd = svccmd_resolve_name(cmd, cmd_name))) {
1349 reply("MCMSG_UNKNOWN_COMMAND_2", cmd_name, cmd->parent->bot->nick);
1350 return 0;
1351 }
1352 pos = snprintf(buf, sizeof(buf), "%s.%s", svccmd->command->parent->name, svccmd->command->name);
1353 if (svccmd->alias.used) {
1354 buf[pos++] = ' ';
1355 unsplit_string((char**)svccmd->alias.list+1, svccmd->alias.used-1, buf+pos);
1356 reply("MCMSG_COMMAND_ALIASES", svccmd->name, buf);
1357 } else {
1358 reply("MCMSG_COMMAND_BINDING", svccmd->name, buf);
1359 }
1360 flags = svccmd->effective_flags;
1361 if ((svccmd->parent && svccmd->parent->privileged && !IsOper(user))
1362 || ((flags & MODCMD_REQUIRE_STAFF)
1363 && !IsOper(user) && !IsNetworkHelper(user) && !IsSupportHelper(user))) {
1364 reply("MCMSG_INSPECTION_REFUSED", svccmd->name);
1365 return 0;
1366 }
1367 if (flags & MODCMD_DISABLED) {
1368 reply("MSG_COMMAND_DISABLED", svccmd->name);
1369 return 1;
1370 }
1371 shown_flags = 0;
1372 if (svccmd->min_opserv_level > 0) {
1373 reply("MCMSG_NEED_OPSERV_LEVEL", svccmd->min_opserv_level);
1374 shown_flags |= MODCMD_REQUIRE_OPER | MODCMD_REQUIRE_AUTHED;
1375 }
1376 if (svccmd->min_channel_access > 0) {
1377 reply("MCMSG_NEED_CHANSERV_LEVEL", svccmd->min_channel_access);
1378 shown_flags |= MODCMD_REQUIRE_CHANUSER | MODCMD_REQUIRE_REGCHAN | MODCMD_REQUIRE_CHANNEL | MODCMD_REQUIRE_AUTHED;
1379 }
1380 if (svccmd->req_account_flags) {
1381 for (nn=pos=0; nn<32; nn++) {
1382 if (!(svccmd->req_account_flags & (1 << nn))) continue;
1383 buf[pos++] = HANDLE_FLAGS[nn];
1384 }
1385 buf[pos] = 0;
1386 reply("MCMSG_NEED_ACCOUNT_FLAGS", buf);
1387 shown_flags |= MODCMD_REQUIRE_AUTHED;
1388 }
1389 if (!flags && !shown_flags) {
1390 reply("MCMSG_NEED_NOTHING", svccmd->name);
1391 return 1;
1392 }
1393 if (flags & ~shown_flags & MODCMD_REQUIRE_HELPING) {
1394 reply("MCMSG_MUST_BE_HELPING");
1395 shown_flags |= MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_STAFF;
1396 }
1397 if (flags & ~shown_flags & MODCMD_REQUIRE_STAFF) {
1398 switch (flags & MODCMD_REQUIRE_STAFF) {
1399 default: case MODCMD_REQUIRE_STAFF:
1400 fmt_str = "MCMSG_NEED_STAFF_ACCESS";
1401 break;
1402 case MODCMD_REQUIRE_OPER:
1403 fmt_str = "MCMSG_NEED_STAFF_OPER";
1404 break;
1405 case MODCMD_REQUIRE_NETWORK_HELPER:
1406 fmt_str = "MCMSG_NEED_STAFF_NETHELPER";
1407 break;
1408 case MODCMD_REQUIRE_OPER|MODCMD_REQUIRE_NETWORK_HELPER:
1409 fmt_str = "MCMSG_NEED_STAFF_NETHELPER_OR_OPER";
1410 break;
1411 case MODCMD_REQUIRE_SUPPORT_HELPER:
1412 fmt_str = "MCMSG_NEED_STAFF_SHELPER";
1413 break;
1414 case MODCMD_REQUIRE_OPER|MODCMD_REQUIRE_SUPPORT_HELPER:
1415 fmt_str = "MCMSG_NEED_STAFF_SHELPER_OR_OPER";
1416 break;
1417 case MODCMD_REQUIRE_SUPPORT_HELPER|MODCMD_REQUIRE_NETWORK_HELPER:
1418 fmt_str = "MCMSG_NEED_STAFF_HELPER";
1419 break;
1420 }
1421 reply(fmt_str);
1422 shown_flags |= MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_STAFF;
1423 }
1424 if (flags & ~shown_flags & MODCMD_REQUIRE_JOINABLE) {
1425 reply("MCMSG_NEED_JOINABLE");
1426 shown_flags |= MODCMD_REQUIRE_CHANUSER;
1427 }
1428 if (flags & ~shown_flags & MODCMD_REQUIRE_CHANUSER) {
1429 if (flags & ~shown_flags & MODCMD_IGNORE_CSUSPEND)
1430 reply("MCMSG_NEED_CHANUSER_CSUSPENDABLE");
1431 else
1432 reply("MCMSG_NEED_CHANUSER");
1433 shown_flags |= MODCMD_IGNORE_CSUSPEND | MODCMD_REQUIRE_REGCHAN | MODCMD_REQUIRE_CHANNEL | MODCMD_REQUIRE_AUTHED;
1434 }
1435 if (flags & ~shown_flags & MODCMD_REQUIRE_REGCHAN) {
1436 reply("MCMSG_NEED_REGCHAN");
1437 shown_flags |= MODCMD_REQUIRE_CHANNEL;
1438 }
1439 if (flags & ~shown_flags & MODCMD_REQUIRE_CHANNEL)
1440 reply("MCMSG_NEED_CHANNEL");
1441 if (flags & ~shown_flags & MODCMD_REQUIRE_AUTHED)
1442 reply("MCMSG_NEED_AUTHED");
1443 if (flags & ~shown_flags & MODCMD_TOY)
1444 reply("MCMSG_IS_TOY", svccmd->name);
1445 if (flags & ~shown_flags & MODCMD_REQUIRE_QUALIFIED) {
1446 const char *botnick = svccmd->parent ? svccmd->parent->bot->nick : "SomeBot";
1447 reply("MCMSG_MUST_QUALIFY", botnick, svccmd->name, botnick);
1448 }
1449 reply("MCMSG_END_REQUIREMENTS", svccmd->name);
1450 return 1;
1451}
1452
1453static void
1454modcmd_describe_command(struct userNode *user, struct svccmd *cmd, struct svccmd *target) {
1455 char buf1[MAXLEN], buf2[MAXLEN];
1456 unsigned int ii, len, buf1_used, buf2_used;
1457
1458 if (target->alias.used) {
1459 unsplit_string((char**)target->alias.list, target->alias.used, buf1);
1460 reply("MCMSG_COMMAND_ALIASES", target->name, buf1);
1461 } else {
1462 snprintf(buf1, sizeof(buf1), "%s.%s", target->command->parent->name, target->command->name);
1463 reply("MCMSG_COMMAND_BINDING", target->name, buf1);
1464 }
1465 for (ii = buf1_used = buf2_used = 0; flags[ii].name; ++ii) {
1466 if (target->flags & flags[ii].flag) {
1467 if (buf1_used)
1468 buf1[buf1_used++] = ',';
1469 len = strlen(flags[ii].name);
1470 memcpy(buf1 + buf1_used, flags[ii].name, len);
1471 buf1_used += len;
1472 } else if (target->effective_flags & flags[ii].flag) {
1473 if (buf2_used)
1474 buf2[buf2_used++] = ',';
1475 len = strlen(flags[ii].name);
1476 memcpy(buf2 + buf2_used, flags[ii].name, len);
1477 buf2_used += len;
1478 }
1479 }
1480 if (buf1_used)
1481 buf1[buf1_used] = '\0';
1482 else
1483 strcpy(buf1, user_find_message(user, "MSG_NONE"));
1484 if (buf2_used)
1485 buf2[buf2_used] = '\0';
1486 else
1487 strcpy(buf2, user_find_message(user, "MSG_NONE"));
1488 reply("MCMSG_COMMAND_FLAGS", buf1, buf2);
1489 for (ii = buf1_used = buf2_used = 0; handle_flags[ii]; ++ii) {
1490 if (target->req_account_flags & (1 << ii))
1491 buf1[buf1_used++] = handle_flags[ii];
1492 else if (target->deny_account_flags & (1 << ii))
1493 buf2[buf2_used++] = handle_flags[ii];
1494 }
1495 buf1[buf1_used] = buf2[buf2_used] = '\0';
1496 reply("MCMSG_COMMAND_ACCOUNT_FLAGS", buf1, buf2);
1497 reply("MCMSG_COMMAND_ACCESS_LEVEL", target->min_channel_access, target->min_opserv_level);
1498 reply("MCMSG_COMMAND_USES", target->name, target->uses);
1499}
1500
1501static MODCMD_FUNC(cmd_modcmd) {
1502 struct svccmd *svccmd;
1503 unsigned int arg, changed;
1504 char cmdname[MAXLEN];
1505
1506 assert(argc >= 2);
1507 arg = collapse_cmdname(argv+1, argc-1, cmdname) + 1;
1508 if (!arg) {
1509 reply("MSG_MISSING_PARAMS", cmd->name);
1510 return 0;
1511 }
1512 if (!(svccmd = svccmd_resolve_name(cmd, cmdname))) {
1513 reply("MCMSG_UNKNOWN_COMMAND_2", cmdname, cmd->parent->bot->nick);
1514 return 0;
1515 }
1516 for (changed = 0; arg+1 < argc; arg += 2) {
1517 if (svccmd_configure(svccmd, user, cmd->parent->bot, argv[arg], argv[arg+1])) {
1518 reply("MCMSG_COMMAND_MODIFIED", argv[arg], svccmd->name);
1519 changed = 1;
1520 }
1521 }
1522 if (changed)
1523 modcmd_set_effective_flags(svccmd);
1524 modcmd_describe_command(user, cmd, svccmd);
1525 return changed;
1526}
1527
7637f48f 1528static void
1529timeout_god(void *data)
1530{
1531 extern struct userNode *chanserv;
1532 struct userNode *user = data;
1533
1534 if(!user || !IsHelping(user)) return;
1535
1536 HANDLE_CLEAR_FLAG(user->handle_info, HELPING);
1537 send_message(user, chanserv, "MCMSG_GOD_EXPIRED");
1538}
1539
d76ed9a9 1540static MODCMD_FUNC(cmd_god) {
1541 int helping;
1542
1543 if (argc > 1) {
1544 if (enabled_string(argv[1])) {
1545 if (HANDLE_FLAGGED(user->handle_info, HELPING)) {
1546 reply("MCMSG_ALREADY_HELPING");
1547 return 0;
1548 }
6aae9404 1549 helping = 1;
d76ed9a9 1550 } else if (disabled_string(argv[1])) {
1551 if (!HANDLE_FLAGGED(user->handle_info, HELPING)) {
1552 reply("MCMSG_ALREADY_NOT_HELPING");
1553 return 0;
1554 }
6aae9404 1555 helping = 0;
d76ed9a9 1556 } else {
1557 reply("MSG_INVALID_BINARY", argv[1]);
1558 return 0;
6aae9404 1559 }
d76ed9a9 1560 } else {
1561 helping = !IsHelping(user);
1562 }
1563
1564 if (helping) {
1565 HANDLE_SET_FLAG(user->handle_info, HELPING);
7637f48f 1566 timeq_add(now + god_timeout, timeout_god, user);
d76ed9a9 1567 reply("MCMSG_NOW_HELPING");
1568 } else {
1569 HANDLE_CLEAR_FLAG(user->handle_info, HELPING);
7637f48f 1570 timeq_del(0, timeout_god, user, TIMEQ_IGNORE_WHEN);
d76ed9a9 1571 reply("MCMSG_NOW_NOT_HELPING");
1572 }
1573
1574 return 1;
1575}
1576
1577static MODCMD_FUNC(cmd_joiner) {
1117fc5a 1578 char cmdname[MAXLEN];
d76ed9a9 1579
1580 if (argc < 2) {
1581 int len = sprintf(cmdname, "%s ", cmd->name);
1582 dict_iterator_t it;
1583 struct string_buffer sbuf;
1584
1585 string_buffer_init(&sbuf);
1586 for (it = dict_first(cmd->parent->commands); it; it = iter_next(it)) {
1587 if (!ircncasecmp(iter_key(it), cmdname, len)) {
1588 if (sbuf.used) string_buffer_append_string(&sbuf, ", ");
1589 string_buffer_append_string(&sbuf, iter_key(it));
1590 }
1591 }
1592 if (!sbuf.used) string_buffer_append(&sbuf, 0);
1593 reply("MCMSG_JOINER_CHOICES", cmd->name, sbuf.list);
1594 string_buffer_clean(&sbuf);
1595 return 1;
1596 }
1597 sprintf(cmdname, "%s %s", cmd->name, argv[1]);
1598 argv[1] = cmdname;
1599 svccmd_invoke_argv(user, cmd->parent, channel, argc-1, argv+1, 0);
1600 return 0; /* never try to log this; the recursive one logs it */
1601}
1602
1603static MODCMD_FUNC(cmd_stats_modules) {
1604 struct helpfile_table tbl;
1605 dict_iterator_t it;
1606 unsigned int ii;
1607 struct module *mod;
1608 struct modcmd *modcmd;
1609
1610 if (argc < 2) {
1611 tbl.length = dict_size(modules) + 1;
1612 tbl.width = 3;
1613 tbl.flags = TABLE_PAD_LEFT;
1614 tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0]));
1615 tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
1616 tbl.contents[0][0] = "Module";
1617 tbl.contents[0][1] = "Commands";
1618 tbl.contents[0][2] = "Helpfile";
1619 for (ii=1, it=dict_first(modules); it; it=iter_next(it), ii++) {
1620 mod = iter_data(it);
1621 tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
1622 tbl.contents[ii][0] = mod->name;
1623 tbl.contents[ii][1] = strtab(dict_size(mod->commands));
1624 tbl.contents[ii][2] = mod->helpfile_name ? mod->helpfile_name : "(none)";
1625 }
1626 } else if (!(mod = dict_find(modules, argv[1], NULL))) {
1627 reply("MCMSG_UNKNOWN_MODULE", argv[1]);
1628 return 0;
1629 } else {
1630 reply("MCMSG_MODULE_INFO", mod->name);
1631 tbl.length = dict_size(mod->commands) + 1;
1632 tbl.width = 3;
1633 tbl.flags = TABLE_PAD_LEFT;
1634 tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0]));
1635 tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
1636 tbl.contents[0][0] = "Command";
1637 tbl.contents[0][1] = "Min. Args";
1638 tbl.contents[0][2] = "Bind Count";
1639 for (ii=1, it=dict_first(mod->commands); it; it=iter_next(it), ii++) {
1640 modcmd = iter_data(it);
1641 tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
1642 tbl.contents[ii][0] = modcmd->name;
1643 tbl.contents[ii][1] = strtab(modcmd->min_argc);
1644 tbl.contents[ii][2] = strtab(modcmd->bind_count);
1645 }
1646 }
1647 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
1648 return 0;
1649}
1650
1651static MODCMD_FUNC(cmd_stats_services) {
1652 struct helpfile_table tbl;
1653 dict_iterator_t it;
1654 unsigned int ii;
1655 struct service *service;
1656 struct svccmd *svccmd;
1657 char *extra;
1658
1659 if (argc < 2) {
1660 tbl.length = dict_size(services) + 1;
1661 tbl.width = 4;
1662 tbl.flags = TABLE_PAD_LEFT;
1663 tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0]));
1664 tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
1665 tbl.contents[0][0] = "Service";
1666 tbl.contents[0][1] = "Commands";
1667 tbl.contents[0][2] = "Priv'd?";
1668 tbl.contents[0][3] = "Trigger";
1669 extra = calloc(2, tbl.length);
1670 for (ii=1, it=dict_first(services); it; it=iter_next(it), ii++) {
1671 service = iter_data(it);
1672 tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
1673 tbl.contents[ii][0] = service->bot->nick;
1674 tbl.contents[ii][1] = strtab(dict_size(service->commands));
1675 tbl.contents[ii][2] = service->privileged ? "yes" : "no";
1676 extra[ii*2] = service->trigger;
1677 tbl.contents[ii][3] = extra+ii*2;
1678 }
1679 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
1680 free(extra);
1681 return 0;
1682 } else if (!(service = dict_find(services, argv[1], NULL))) {
1683 reply("MCMSG_UNKNOWN_SERVICE", argv[1]);
1684 return 0;
1685 } else {
1686 tbl.length = dict_size(service->commands) + 1;
1687 tbl.width = 5;
1688 tbl.flags = TABLE_PAD_LEFT | TABLE_NO_FREE;
1689 tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0]));
1690 tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
1691 tbl.contents[0][0] = "Command";
1692 tbl.contents[0][1] = "Module";
1693 tbl.contents[0][2] = "ModCmd";
1694 tbl.contents[0][3] = "Alias?";
1695 tbl.contents[0][4] = strdup("Uses");
1696 for (ii=1, it=dict_first(service->commands); it; it=iter_next(it), ii++) {
1697 svccmd = iter_data(it);
1698 tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
1699 tbl.contents[ii][0] = svccmd->name;
1700 tbl.contents[ii][1] = svccmd->command->parent->name;
1701 tbl.contents[ii][2] = svccmd->command->name;
1702 tbl.contents[ii][3] = svccmd->alias.used ? "yes" : "no";
1703 tbl.contents[ii][4] = extra = malloc(12);
1704 sprintf(extra, "%u", svccmd->uses);
1705 }
1706 reply("MCMSG_SERVICE_INFO", service->bot->nick);
1707 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
1708 for (ii=0; ii<tbl.length; ii++) {
1709 free((char*)tbl.contents[ii][4]);
1710 free(tbl.contents[ii]);
1711 }
1712 free(tbl.contents);
1713 return 0;
1714 }
1715}
1716
1717static MODCMD_FUNC(cmd_showcommands) {
1718 struct svccmd_list commands;
1719 struct helpfile_table tbl;
1720 struct svccmd *svccmd;
1721 dict_iterator_t it;
1722 unsigned int ii, ignore_flags = 0;
1723 unsigned int max_opserv_level = 1000;
1724 unsigned short max_chanserv_level = 500;
1725 char show_opserv_level = 0, show_channel_access = 0;
1726
1727 /* Check to see what the max access they want to see is. */
1728 for (ii=1; ii<argc; ++ii) {
1729 if (isdigit(argv[ii][0]))
1730 max_opserv_level = atoi(argv[ii]);
1731 else
1732 max_chanserv_level = user_level_from_name(argv[ii], UL_OWNER);
1733 }
1734
1735 /* Find the matching commands. */
1736 svccmd_list_init(&commands);
1737 if (cmd->parent->privileged)
1738 ignore_flags = MODCMD_REQUIRE_OPER;
1739 for (it = dict_first(cmd->parent->commands); it; it = iter_next(it)) {
1740 svccmd = iter_data(it);
1741 if (strchr(svccmd->name, ' '))
1742 continue;
1743 if (!svccmd_can_invoke(user, svccmd->parent->bot, svccmd, channel, SVCCMD_QUALIFIED))
1744 continue;
1745 if (svccmd->min_opserv_level > max_opserv_level)
1746 continue;
1747 if (svccmd->min_channel_access > max_chanserv_level)
1748 continue;
1749 if (svccmd->min_opserv_level > 0)
1750 show_opserv_level = 1;
1751 if (svccmd->min_channel_access > 0)
1752 show_channel_access = 1;
1753 if (svccmd->effective_flags
1754 & (MODCMD_REQUIRE_STAFF|MODCMD_REQUIRE_HELPING)
1755 & ~ignore_flags) {
1756 show_channel_access = 1;
1757 }
1758 svccmd_list_append(&commands, svccmd);
1759 }
1760
1761 /* Build the table. */
1762 tbl.length = commands.used + 1;
1763 tbl.width = 1 + show_opserv_level + show_channel_access;
1764 tbl.flags = TABLE_REPEAT_ROWS;
1765 tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0]));
1766 tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
1767 tbl.contents[0][ii = 0] = "Command";
1768 if (show_opserv_level)
1769 tbl.contents[0][++ii] = "OpServ Level";
1770 if (show_channel_access)
1771 tbl.contents[0][++ii] = "ChanServ Access";
1772 for (ii=0; ii<commands.used; ++ii) {
1773 svccmd = commands.list[ii];
1774 tbl.contents[ii+1] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
1775 tbl.contents[ii+1][0] = svccmd->name;
1776 if (show_opserv_level)
1777 tbl.contents[ii+1][1] = strtab(svccmd->min_opserv_level);
1778 if (show_channel_access) {
1779 const char *access;
1780 int flags = svccmd->effective_flags;
1781 if (flags & MODCMD_REQUIRE_HELPING)
1782 access = "helping";
1783 else if (flags & MODCMD_REQUIRE_STAFF) {
0d16e639 1784 if (flags & MODCMD_REQUIRE_OPER)
1785 access = "oper";
1786 else if (flags & MODCMD_REQUIRE_NETWORK_HELPER)
1787 access = "net.helper";
1788 else
1789 access = "staff";
d76ed9a9 1790 } else
1791 access = strtab(svccmd->min_channel_access);
1792 tbl.contents[ii+1][1+show_opserv_level] = access;
1793 }
1794 }
1795 svccmd_list_clean(&commands);
1796 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
1797 return 0;
1798}
1799
1800static MODCMD_FUNC(cmd_helpfiles) {
1801 struct service *service;
1802 unsigned int ii;
1803
1804 if (!(service = dict_find(services, argv[1], NULL))) {
1805 reply("MCMSG_UNKNOWN_SERVICE", argv[1]);
1806 return 0;
1807 }
1808
1809 if (argc < 3) {
1810 for (ii=0; ii<service->modules.used; ++ii)
1811 reply("MCMSG_HELPFILE_SEQUENCE", ii+1, service->modules.list[ii]->name);
1812 return 0;
1813 }
1814
1815 service->modules.used = 0;
1816 for (ii=0; ii<argc-2; ii++) {
1817 struct module *module = dict_find(modules, argv[ii+2], NULL);
1818 if (!module) {
1819 reply("MCMSG_UNKNOWN_MODULE", argv[ii+2]);
1820 continue;
1821 }
1822 module_list_append(&service->modules, module);
1823 }
1824 reply("MCMSG_HELPFILE_SEQUENCE_SET", service->bot->nick);
1825 return 1;
1826}
1827
1828static MODCMD_FUNC(cmd_service_add) {
1829 const char *nick, *hostname, *desc;
1830 struct userNode *bot;
1831
1832 nick = argv[1];
1833 if (!is_valid_nick(nick)) {
1834 reply("MCMSG_BAD_SERVICE_NICK", nick);
1835 return 0;
1836 }
1837 hostname = argv[2];
1838 desc = unsplit_string(argv+3, argc-3, NULL);
1839 bot = GetUserH(nick);
1840 if (bot && IsService(bot)) {
1841 reply("MCMSG_ALREADY_SERVICE", bot->nick);
1842 return 0;
1843 }
a32da4c7 1844 bot = AddService(nick, NULL, desc, hostname);
d76ed9a9 1845 service_register(bot);
1846 reply("MCMSG_NEW_SERVICE", bot->nick);
1847 return 1;
1848}
1849
1850static MODCMD_FUNC(cmd_service_rename) {
1851 struct service *service;
1852
1853 if (!(service = service_find(argv[1]))) {
1854 reply("MCMSG_UNKNOWN_SERVICE", argv[1]);
1855 return 0;
1856 }
1857 NickChange(service->bot, argv[2], 0);
1858 reply("MCMSG_SERVICE_RENAMED", service->bot->nick);
1859 return 1;
1860}
1861
1862static MODCMD_FUNC(cmd_service_trigger) {
1863 struct userNode *bogon;
1864 struct service *service;
1865
1866 if (!(service = service_find(argv[1]))) {
1867 reply("MCMSG_UNKNOWN_SERVICE", argv[1]);
1868 return 0;
1869 }
1870 if (argc < 3) {
1871 if (service->trigger)
1872 reply("MCMSG_CURRENT_TRIGGER", service->bot->nick, service->trigger);
1873 else
1874 reply("MCMSG_NO_TRIGGER", service->bot->nick);
1875 return 1;
1876 }
1877 if (service->trigger)
1878 reg_chanmsg_func(service->trigger, NULL, NULL);
1879 if (!irccasecmp(argv[2], "none") || !irccasecmp(argv[2], "remove")) {
1880 service->trigger = 0;
1881 reply("MCMSG_REMOVED_TRIGGER", service->bot->nick);
1882 } else if ((bogon = get_chanmsg_bot(argv[2][0]))) {
1883 reply("MCMSG_DUPLICATE_TRIGGER", bogon->nick, argv[2][0]);
1884 return 1;
1885 } else {
1886 service->trigger = argv[2][0];
1887 reg_chanmsg_func(service->trigger, service->bot, modcmd_chanmsg);
1888 reply("MCMSG_NEW_TRIGGER", service->bot->nick, service->trigger);
1889 }
1890 return 1;
1891}
1892
1893static MODCMD_FUNC(cmd_service_privileged) {
1894 struct service *service;
1895 const char *newval;
1896
1897 if (!(service = service_find(argv[1]))) {
1898 reply("MCMSG_UNKNOWN_SERVICE", argv[1]);
1899 return 0;
1900 }
1901 if (argc >= 3)
1902 service->privileged = true_string(argv[2]) || enabled_string(argv[2]);
1903 newval = user_find_message(user, service->privileged ? "MSG_ON" : "MSG_OFF");
1904 reply("MCMSG_SERVICE_PRIVILEGED", service->bot->nick, newval);
1905 return 1;
1906}
1907
1908static MODCMD_FUNC(cmd_service_remove) {
1909 char *name, *reason;
1910 struct service *service;
1911
1912 name = argv[1];
1913 if (argc > 2)
1914 reason = unsplit_string(argv+2, argc-2, NULL);
1915 else
1916 reason = "Removing bot";
1917 if (!(service = service_find(name))) {
1918 reply("MCMSG_UNKNOWN_SERVICE", name);
1919 return 0;
1920 }
1921 DelUser(service->bot, NULL, 1, reason);
1922 reply("MCMSG_SERVICE_REMOVED", name);
1923 dict_remove(services, name);
1924 return 1;
1925}
1926
1927static MODCMD_FUNC(cmd_dump_messages) {
1928 const char *fname = "strings.db";
1929 struct saxdb_context *ctx;
1930 dict_iterator_t it;
1931 FILE *pf;
1932 int res;
1933
1934 if (!(pf = fopen(fname, "w"))) {
1935 reply("MCMSG_FILE_NOT_OPENED", fname);
1936 return 0;
1937 }
1938 if (!(ctx = saxdb_open_context(pf))) {
1939 reply("MSG_INTERNAL_FAILURE");
1940 return 0;
1941 }
1942 if ((res = setjmp(ctx->jbuf)) != 0) {
1943 ctx->complex.used = 0; /* to avoid false assert()s in close */
1944 saxdb_close_context(ctx);
1945 fclose(pf);
1946 reply("MCMSG_MESSAGE_DUMP_FAILED", strerror(res));
1947 return 0;
1948 } else {
1949 for (it = dict_first(lang_C->messages); it; it = iter_next(it))
1950 saxdb_write_string(ctx, iter_key(it), iter_data(it));
1951 saxdb_close_context(ctx);
1952 fclose(pf);
1953 reply("MCMSG_MESSAGES_DUMPED", fname);
1954 return 1;
1955 }
1956}
1957
1958static MODCMD_FUNC(cmd_version) {
1959 /* This function provides copyright management information to end
ceafd592 1960 * users of X3. You should not alter, disable or remove this
d76ed9a9 1961 * command or its accessibility to normal IRC users, except to add
ceafd592 1962 * copyright information pertaining to changes you make to X3.
d76ed9a9 1963 */
63665495 1964
1965 char *svn = NULL, *svnver = NULL, *svndate = NULL, *v = 0, ver[MAXLEN];
1966 int vc = 0;
1967
1968 strcpy(ver, CVS_VERSION);
1969 for (svn = x3_strtok(&v, ver, " "); svn;
1970 svn = x3_strtok(&v, 0, " ")) {
1971 if (vc == 2)
1972 svnver = svn;
1973 else if (vc == 3)
1974 svndate = svn;
1975 vc++;
1976 }
1977
2f61d1d7 1978 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);
63665495 1979 send_message_type(4, user, cmd->parent->bot, "$b$b");
1980 send_message_type(4, user, cmd->parent->bot, "Copyright 2000-2006 srvx Development Team.");
1981 send_message_type(4, user, cmd->parent->bot, "Copyright 2004-2006 X3 Development Team.");
1982 send_message_type(4, user, cmd->parent->bot, "$b$b");
2f61d1d7 1983 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.");
ceafd592 1984 send_message_type(4, user, cmd->parent->bot, "The X3 Development Team includes Alex Schumann, Reed Loden, Neil Spierling.");
63665495 1985 send_message_type(4, user, cmd->parent->bot, "The X3 Development Team can be reached at http://sourceforge.net/projects/evilnet or in #evilnet on irc.afternet.org.");
1986 send_message_type(4, user, cmd->parent->bot, "$b$b");
7637f48f 1987 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.");
63665495 1988 send_message_type(4, user, cmd->parent->bot, "This program is free software; see COPYING in the distribution.");
1989
d76ed9a9 1990 return 1;
1991}
1992
c8273589 1993static void create_default_binds(int rebind);
1994
1995static MODCMD_FUNC(cmd_rebindall) {
1996 send_message_type(4, user, cmd->parent->bot, "$bRe-binding all default commands to respective services..$b");
1997 create_default_binds(1);
1998 return 1;
1999}
2000
d76ed9a9 2001
2002void
2003modcmd_nick_change(struct userNode *user, const char *old_nick) {
2004 struct service *svc;
2005 if (!(svc = dict_find(services, old_nick, NULL)))
2006 return;
2007 dict_remove2(services, old_nick, 1);
2008 dict_insert(services, user->nick, svc);
2009}
2010
2011void
2012modcmd_cleanup(void) {
2013 dict_delete(services);
2014 dict_delete(modules);
2015 if (suf_list)
2016 free(suf_list);
2017}
2018
2019static void
2020modcmd_saxdb_write_command(struct saxdb_context *ctx, struct svccmd *cmd) {
2021 char buf[MAXLEN];
2022 unsigned int nn, len, pos;
2023 struct svccmd *template = cmd->command->defaults;
2024
2025 saxdb_start_record(ctx, cmd->name, 0);
2026 sprintf(buf, "%s.%s", cmd->command->parent->name, cmd->command->name);
2027 saxdb_write_string(ctx, "command", buf);
2028 if (cmd->alias.used)
2029 saxdb_write_string_list(ctx, "aliased", &cmd->alias);
2030 if (cmd->min_opserv_level != template->min_opserv_level)
2031 saxdb_write_int(ctx, "oper_access", cmd->min_opserv_level);
2032 if (cmd->min_channel_access != template->min_channel_access)
2033 saxdb_write_int(ctx, "channel_access", cmd->min_channel_access);
2034 if (cmd->flags != template->flags) {
2035 if (cmd->flags) {
2036 for (nn=pos=0; flags[nn].name; ++nn) {
2037 if (cmd->flags & flags[nn].flag) {
2038 buf[pos++] = '+';
2039 len = strlen(flags[nn].name);
2040 memcpy(buf+pos, flags[nn].name, len);
2041 pos += len;
2042 buf[pos++] = ',';
2043 }
2044 }
2045 } else {
2046 pos = 1;
2047 }
2048 buf[--pos] = 0;
2049 saxdb_write_string(ctx, "flags", buf);
2050 }
2051 if ((cmd->req_account_flags != template->req_account_flags)
2052 || (cmd->deny_account_flags != template->req_account_flags)) {
2053 pos = 0;
2054 if (cmd->req_account_flags) {
2055 buf[pos++] = '+';
2056 for (nn=0; nn<32; nn++)
2057 if (cmd->req_account_flags & (1 << nn))
2058 buf[pos++] = handle_flags[nn];
2059 }
2060 if (cmd->deny_account_flags) {
2061 buf[pos++] = '-';
2062 for (nn=0; nn<32; nn++)
2063 if (cmd->deny_account_flags & (1 << nn))
2064 buf[pos++] = handle_flags[nn];
2065 }
2066 buf[pos] = 0;
2067 saxdb_write_string(ctx, "account_flags", buf);
2068 }
2069 saxdb_end_record(ctx);
2070}
2071
2072static int
2073modcmd_saxdb_write(struct saxdb_context *ctx) {
2074 struct string_list slist;
2075 dict_iterator_t it, it2;
2076 struct service *service;
2077 unsigned int ii;
2078
2079 saxdb_start_record(ctx, "bots", 1);
2080 for (it = dict_first(services); it; it = iter_next(it)) {
2081 char buff[16];
2082 service = iter_data(it);
2083 saxdb_start_record(ctx, service->bot->nick, 1);
2084 if (service->trigger) {
2085 buff[0] = service->trigger;
2086 buff[1] = '\0';
2087 saxdb_write_string(ctx, "trigger", buff);
2088 }
2089 saxdb_write_string(ctx, "description", service->bot->info);
2090 saxdb_write_string(ctx, "hostname", service->bot->hostname);
2091 if (service->privileged)
2092 saxdb_write_string(ctx, "privileged", "1");
2093 saxdb_end_record(ctx);
2094 }
2095 saxdb_end_record(ctx);
2096
2097 saxdb_start_record(ctx, "services", 1);
2098 for (it = dict_first(services); it; it = iter_next(it)) {
2099 service = iter_data(it);
2100 saxdb_start_record(ctx, service->bot->nick, 1);
2101 for (it2 = dict_first(service->commands); it2; it2 = iter_next(it2))
2102 modcmd_saxdb_write_command(ctx, iter_data(it2));
2103 saxdb_end_record(ctx);
2104 }
2105 saxdb_end_record(ctx);
2106
2107 saxdb_start_record(ctx, "helpfiles", 1);
2108 slist.size = 0;
2109 for (it = dict_first(services); it; it = iter_next(it)) {
2110 service = iter_data(it);
2111 slist.used = 0;
2112 for (ii = 0; ii < service->modules.used; ++ii)
2113 string_list_append(&slist, service->modules.list[ii]->name);
2114 saxdb_write_string_list(ctx, iter_key(it), &slist);
2115 }
2116 if (slist.list)
2117 free(slist.list);
2118 saxdb_end_record(ctx);
2119
2120 return 0;
2121}
2122
2123static int
2124append_entry(const char *key, UNUSED_ARG(void *data), void *extra) {
2125 struct helpfile_expansion *exp = extra;
2126 int row = exp->value.table.length++;
2127 exp->value.table.contents[row] = calloc(1, sizeof(char*));
2128 exp->value.table.contents[row][0] = key;
2129 return 0;
2130}
2131
2132static struct helpfile_expansion
2133modcmd_expand(const char *variable) {
2134 struct helpfile_expansion exp;
2135 extern struct userNode *message_source;
2136 struct service *service;
2137
2138 service = dict_find(services, message_source->nick, NULL);
2139 if (!irccasecmp(variable, "index")) {
2140 exp.type = HF_TABLE;
2141 exp.value.table.length = 1;
2142 exp.value.table.width = 1;
2143 exp.value.table.flags = TABLE_REPEAT_ROWS;
2144 exp.value.table.contents = calloc(dict_size(service->commands)+1, sizeof(char**));
2145 exp.value.table.contents[0] = calloc(1, sizeof(char*));
2146 exp.value.table.contents[0][0] = "Commands:";
2147 dict_foreach(service->commands, append_entry, &exp);
2148 return exp;
2149 } else if (!irccasecmp(variable, "languages")) {
2150 struct string_buffer sbuf;
2151 dict_iterator_t it;
2152 sbuf.used = sbuf.size = 0;
2153 sbuf.list = NULL;
2154 for (it = dict_first(languages); it; it = iter_next(it)) {
2155 string_buffer_append_string(&sbuf, iter_key(it));
2156 string_buffer_append(&sbuf, ' ');
2157 }
2158 sbuf.list[--sbuf.used] = 0;
2159 exp.type = HF_STRING;
2160 exp.value.str = sbuf.list;
2161 return exp;
2162 }
2163 exp.type = HF_STRING;
2164 exp.value.str = NULL;
2165 return exp;
2166}
2167
2168static void
2169modcmd_load_bots(struct dict *db, int default_nick) {
2170 dict_iterator_t it;
2171
2172 for (it = dict_first(db); it; it = iter_next(it)) {
2173 struct record_data *rd;
2174 struct service *svc;
2175 const char *nick, *desc, *hostname;
2176
2177 rd = iter_data(it);
2178 if (rd->type != RECDB_OBJECT) {
2179 log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'bots/%s' in modcmd db (expected object).", iter_key(it));
2180 continue;
2181 }
2182 nick = database_get_data(rd->d.object, "nick", RECDB_QSTRING);
2183 if (!nick) {
2184 if (default_nick)
2185 nick = iter_key(it);
2186 else
2187 continue;
2188 }
2189 svc = service_find(nick);
2190 desc = database_get_data(rd->d.object, "description", RECDB_QSTRING);
2191 hostname = database_get_data(rd->d.object, "hostname", RECDB_QSTRING);
2192 if (desc) {
2193 if (!svc)
a32da4c7 2194 svc = service_register(AddService(nick, NULL, desc, hostname));
d76ed9a9 2195 else if (hostname)
2196 strcpy(svc->bot->hostname, hostname);
2197 desc = database_get_data(rd->d.object, "trigger", RECDB_QSTRING);
2198 if (desc)
2199 svc->trigger = desc[0];
2200 desc = database_get_data(rd->d.object, "privileged", RECDB_QSTRING);
2201 if (desc && (true_string(desc) || enabled_string(desc)))
2202 svc->privileged = 1;
2203 }
2204 }
2205}
2206
2207static void
2208modcmd_conf_read(void) {
2209 modcmd_load_bots(conf_get_data("services", RECDB_OBJECT), 0);
2210}
2211
2212void
2213modcmd_init(void) {
2214 qsort(flags, ArrayLength(flags)-1, sizeof(flags[0]), flags_qsort);
2215 modules = dict_new();
2216 dict_set_free_data(modules, free_module);
2217 services = dict_new();
2218 dict_set_free_data(services, free_service);
2219 reg_nick_change_func(modcmd_nick_change);
2220 reg_exit_func(modcmd_cleanup);
2221 conf_register_reload(modcmd_conf_read);
2222
2223 modcmd_module = module_register("modcmd", MAIN_LOG, "modcmd.help", modcmd_expand);
2224 bind_command = modcmd_register(modcmd_module, "bind", cmd_bind, 4, MODCMD_KEEP_BOUND, "oper_level", "800", NULL);
2225 help_command = modcmd_register(modcmd_module, "help", cmd_help, 1, 0, "flags", "+nolog", NULL);
2226 modcmd_register(modcmd_module, "command", cmd_command, 2, 0, "flags", "+nolog", NULL);
2227 modcmd_register(modcmd_module, "modcmd", cmd_modcmd, 2, MODCMD_KEEP_BOUND, "template", "bind", NULL);
2228 modcmd_register(modcmd_module, "god", cmd_god, 0, MODCMD_REQUIRE_AUTHED, "flags", "+oper,+networkhelper", NULL);
2229 modcmd_register(modcmd_module, "readhelp", cmd_readhelp, 2, 0, "oper_level", "650", NULL);
2230 modcmd_register(modcmd_module, "timecmd", cmd_timecmd, 2, 0, "oper_level", "1", NULL);
2231 modcmd_register(modcmd_module, "unbind", cmd_unbind, 3, 0, "template", "bind", NULL);
2232 modcmd_register(modcmd_module, "joiner", cmd_joiner, 1, 0, NULL);
2233 modcmd_register(modcmd_module, "stats modules", cmd_stats_modules, 1, 0, "flags", "+oper", NULL);
2234 modcmd_register(modcmd_module, "stats services", cmd_stats_services, 1, 0, "flags", "+oper", NULL);
2235 modcmd_register(modcmd_module, "showcommands", cmd_showcommands, 1, 0, "flags", "+acceptchan", NULL);
2236 modcmd_register(modcmd_module, "helpfiles", cmd_helpfiles, 2, 0, "template", "bind", NULL);
2237 modcmd_register(modcmd_module, "service add", cmd_service_add, 4, 0, "flags", "+oper", NULL);
2238 modcmd_register(modcmd_module, "service rename", cmd_service_rename, 3, 0, "flags", "+oper", NULL);
2239 modcmd_register(modcmd_module, "service trigger", cmd_service_trigger, 2, 0, "flags", "+oper", NULL);
2240 modcmd_register(modcmd_module, "service privileged", cmd_service_privileged, 2, 0, "flags", "+oper", NULL);
2241 modcmd_register(modcmd_module, "service remove", cmd_service_remove, 2, 0, "flags", "+oper", NULL);
2242 modcmd_register(modcmd_module, "dumpmessages", cmd_dump_messages, 1, 0, "oper_level", "1000", NULL);
c8273589 2243 modcmd_register(modcmd_module, "rebindall", cmd_rebindall, 0, MODCMD_KEEP_BOUND, "oper_level", "800", NULL);
d76ed9a9 2244 version_command = modcmd_register(modcmd_module, "version", cmd_version, 1, 0, NULL);
2245 message_register_table(msgtab);
2246}
2247
2248static void
2249modcmd_db_load_command(struct service *service, const char *cmdname, struct dict *obj) {
2250 struct svccmd *svccmd;
2251 struct module *module;
2252 struct modcmd *modcmd;
2253 struct string_list *slist;
2254 const char *str, *sep;
2255 char buf[MAXLEN];
2256
2257 str = database_get_data(obj, "command", RECDB_QSTRING);
2258 if (!str) {
2259 log_module(MAIN_LOG, LOG_ERROR, "Missing command for service %s command %s in modcmd.db", service->bot->nick, cmdname);
2260 return;
2261 }
2262 sep = strchr(str, '.');
2263 if (!sep) {
2264 log_module(MAIN_LOG, LOG_ERROR, "Invalid command %s for service %s command %s in modcmd.db", str, service->bot->nick, cmdname);
2265 return;
2266 }
2267 memcpy(buf, str, sep-str);
2268 buf[sep-str] = 0;
2269 if (!(module = module_find(buf))) {
2270 log_module(MAIN_LOG, LOG_ERROR, "Unknown module %s for service %s command %s in modcmd.db", buf, service->bot->nick, cmdname);
2271 return;
2272 }
2273 if (!(modcmd = dict_find(module->commands, sep+1, NULL))) {
2274 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);
2275 return;
2276 }
2277 /* Now that we know we have a command to use, fill in the basics. */
2278 svccmd = calloc(1, sizeof(*svccmd));
2279 svccmd_insert(service, strdup(cmdname), svccmd, modcmd);
2280 if ((str = database_get_data(obj, "template", RECDB_QSTRING))) {
2281 add_pending_template(svccmd, str);
2282 } else {
2283 svccmd_copy_rules(svccmd, modcmd->defaults);
2284 }
2285 if ((str = database_get_data(obj, "account_flags", RECDB_QSTRING))) {
2286 svccmd->req_account_flags = svccmd->deny_account_flags = 0;
2287 svccmd_configure(svccmd, NULL, service->bot, "account_flags", str);
2288 }
2289 if ((str = database_get_data(obj, "flags", RECDB_QSTRING))) {
2290 svccmd->flags = 0;
2291 svccmd_configure(svccmd, NULL, service->bot, "flags", str);
2292 }
2293 if ((str = database_get_data(obj, "oper_access", RECDB_QSTRING))
2294 || (str = database_get_data(obj, "opserv_level", RECDB_QSTRING))) {
2295 svccmd_configure(svccmd, NULL, service->bot, "oper_access", str);
2296 }
2297 if ((str = database_get_data(obj, "channel_access", RECDB_QSTRING))
2298 || (str = database_get_data(obj, "chanserv_level", RECDB_QSTRING))) {
2299 svccmd_configure(svccmd, NULL, service->bot, "channel_access", str);
2300 }
2301 if ((slist = database_get_data(obj, "aliased", RECDB_STRING_LIST))) {
2302 unsigned int nn;
2303 svccmd->alias.used = svccmd->alias.size = slist->used;
2304 svccmd->alias.list = calloc(svccmd->alias.size, sizeof(svccmd->alias.list[0]));
2305 for (nn=0; nn<slist->used; ++nn)
2306 svccmd->alias.list[nn] = strdup(slist->list[nn]);
2307 }
2308 modcmd_set_effective_flags(svccmd);
2309}
2310
2311static struct svccmd *
2312service_make_alias(struct service *service, const char *alias, ...) {
2313 char *arg, *argv[MAXNUMPARAMS];
2314 unsigned int nn, argc;
2315 struct svccmd *svccmd, *template;
2316 va_list args;
2317
2318 va_start(args, alias);
2319 argc = 0;
2320 while (1) {
2321 arg = va_arg(args, char*);
2322 if (!arg)
2323 break;
2324 argv[argc++] = arg;
2325 }
2326 va_end(args);
2327 svccmd = calloc(1, sizeof(*svccmd));
2328 if (!(template = svccmd_resolve_name(svccmd, argv[0]))) {
2329 log_module(MAIN_LOG, LOG_ERROR, "Invalid base command %s for alias %s in service %s", argv[0], alias, service->bot->nick);
2330 free(svccmd->name);
2331 free(svccmd);
2332 return NULL;
2333 }
2334 if (argc > 1) {
2335 svccmd->alias.used = svccmd->alias.size = argc;
2336 svccmd->alias.list = calloc(svccmd->alias.size, sizeof(svccmd->alias.list[0]));
2337 for (nn=0; nn<argc; nn++)
2338 svccmd->alias.list[nn] = strdup(argv[nn]);
2339 }
2340 svccmd_insert(service, strdup(alias), svccmd, template->command);
2341 svccmd_copy_rules(svccmd, template);
2342 return svccmd;
2343}
2344
2345static int saxdb_present;
2346
2347static int
2348modcmd_saxdb_read(struct dict *db) {
2349 struct dict *db2;
2350 dict_iterator_t it, it2;
2351 struct record_data *rd, *rd2;
2352 struct service *service;
2353
2354 modcmd_load_bots(database_get_data(db, "bots", RECDB_OBJECT), 1);
2355 db2 = database_get_data(db, "services", RECDB_OBJECT);
2356 if (!db2) {
2357 log_module(MAIN_LOG, LOG_ERROR, "Missing section 'services' in modcmd db.");
2358 return 1;
2359 }
2360 for (it = dict_first(db2); it; it = iter_next(it)) {
2361 rd = iter_data(it);
2362 if (rd->type != RECDB_OBJECT) {
2363 log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'services/%s' in modcmd db (expected object).", iter_key(it));
2364 continue;
2365 }
2366 if (!(service = service_find(iter_key(it)))) {
2367 log_module(MAIN_LOG, LOG_ERROR, "Unknown service '%s' listed in modcmd db.", iter_key(it));
2368 continue;
2369 }
2370 for (it2 = dict_first(rd->d.object); it2; it2 = iter_next(it2)) {
2371 rd2 = iter_data(it2);
2372 if (rd2->type != RECDB_OBJECT) {
2373 log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'services/%s/%s' in modcmd db (expected object).", iter_key(it), iter_key(it2));
2374 continue;
2375 }
2376 modcmd_db_load_command(service, iter_key(it2), rd2->d.object);
2377 }
2378 }
2379 db2 = database_get_data(db, "helpfiles", RECDB_OBJECT);
2380 for (it = dict_first(db2); it; it = iter_next(it)) {
2381 struct module *module;
2382 struct string_list *slist;
2383 unsigned int ii;
2384
2385 rd = iter_data(it);
2386 if (rd->type != RECDB_STRING_LIST) {
2387 log_module(MAIN_LOG, LOG_ERROR, "Bad type for 'helpfiles/%s' in modcmd db (expected string list).", iter_key(it));
2388 continue;
2389 }
2390 slist = rd->d.slist;
2391 if (!(service = service_find(iter_key(it)))) {
2392 /* We probably whined about the service being missing above. */
2393 continue;
2394 }
2395 service->modules.used = 0;
2396 for (ii=0; ii<slist->used; ++ii) {
2397 if (!(module = dict_find(modules, slist->list[ii], NULL))) {
2398 log_module(MAIN_LOG, LOG_ERROR, "Unknown module '%s' listed in modcmd 'helpfiles/%s'.", slist->list[ii], iter_key(it));
2399 continue;
2400 }
2401 module_list_append(&service->modules, module);
2402 }
2403 }
2404 saxdb_present = 1;
2405 return 0;
2406}
2407
2408static void
c8273589 2409create_default_binds(int rebind) {
d76ed9a9 2410 /* Which services should import which modules by default? */
2411 struct {
2412 const char *svcname;
2413 /* C is lame and requires a fixed size for this array.
2414 * Be sure you NULL-terminate each array and increment the
2415 * size here if you add more default modules to any
2416 * service. */
2417 const char *modnames[8];
2418 } def_binds[] = {
2419 { "ChanServ", { "ChanServ", NULL } },
2420 { "Global", { "Global", NULL } },
2421 { "NickServ", { "NickServ", NULL } },
2422 { "OpServ", { "OpServ", "modcmd", "sendmail", "saxdb", "proxycheck", NULL } },
2423 { NULL, { NULL } }
2424 };
2425 unsigned int ii, jj;
2426 char buf[128], *nick;
2427 struct service *service;
2428 struct module *module;
2429
2430 for (ii = 0; def_binds[ii].svcname; ++ii) {
2431 sprintf(buf, "services/%s/nick", def_binds[ii].svcname);
2432 if (!(nick = conf_get_data(buf, RECDB_QSTRING)))
2433 continue;
2434 if (!(service = service_find(nick)))
2435 continue;
c8273589 2436 if (dict_size(service->commands) > 0 && !rebind)
d76ed9a9 2437 continue;
2438
2439 /* Bind the default modules for this service to it */
2440 for (jj = 0; def_binds[ii].modnames[jj]; ++jj) {
2441 if (!(module = module_find(def_binds[ii].modnames[jj])))
2442 continue;
2443 service_bind_module(service, module);
2444 }
2445
2446 /* Bind the help and version commands to this service */
2447 service_bind_modcmd(service, help_command, help_command->name);
2448 service_bind_modcmd(service, version_command, version_command->name);
2449
2450 /* Now some silly hax.. (aliases that most people want) */
2451 if (!irccasecmp(def_binds[ii].svcname, "ChanServ")) {
2452 service_make_alias(service, "addowner", "*chanserv.adduser", "$1", "owner", NULL);
2453 service_make_alias(service, "addcoowner", "*chanserv.adduser", "$1", "coowner", NULL);
c8273589 2454 service_make_alias(service, "addco", "*chanserv.adduser", "$1", "coowner", NULL);
4048352e 2455 service_make_alias(service, "addmanager", "*chanserv.adduser", "$1", "manager", NULL);
d76ed9a9 2456 service_make_alias(service, "addop", "*chanserv.adduser", "$1", "op", NULL);
55342ce8 2457 service_make_alias(service, "addhop", "*chanserv.adduser", "$1", "halfop", NULL);
d76ed9a9 2458 service_make_alias(service, "addpeon", "*chanserv.adduser", "$1", "peon", NULL);
2459 service_make_alias(service, "delowner", "*chanserv.deluser", "owner", "$1", NULL);
2460 service_make_alias(service, "delcoowner", "*chanserv.deluser", "coowner", "$1", NULL);
4048352e 2461 service_make_alias(service, "delmanager", "*chanserv.deluser", "manager", "$1", NULL);
d76ed9a9 2462 service_make_alias(service, "delop", "*chanserv.deluser", "op", "$1", NULL);
2463 service_make_alias(service, "delpeon", "*chanserv.deluser", "peon", "$1", NULL);
b1bf690d 2464 service_make_alias(service, "llist", "*chanserv.lamers", "$1", NULL);
d76ed9a9 2465 service_make_alias(service, "command", "*modcmd.command", NULL);
2466 service_make_alias(service, "god", "*modcmd.god", NULL);
2467 } else if (!irccasecmp(def_binds[ii].svcname, "OpServ")) {
2468 struct svccmd *svccmd;
2469 svccmd = service_make_alias(service, "stats", "*modcmd.joiner", NULL);
2470 svccmd->min_opserv_level = 101;
2471 svccmd = service_make_alias(service, "service", "*modcmd.joiner", NULL);
2472 svccmd->min_opserv_level = 900;
2473 }
2474 }
2475}
2476
2477static void
2478import_aliases_db() {
2479 struct dict *db;
2480 dict_iterator_t it, it2;
2481 struct record_data *rd, *rd2;
2482 struct service *service;
2483 struct module *module;
2484
2485 if (!(db = parse_database("aliases.db")))
2486 return;
2487 for (it = dict_first(db); it; it = iter_next(it)) {
2488 service = service_find(iter_key(it));
2489 if (!service)
2490 continue;
2491 module = module_find(service->bot->nick);
2492 rd = iter_data(it);
2493 if (rd->type != RECDB_OBJECT)
2494 continue;
2495 for (it2 = dict_first(rd->d.object); it2; it2 = iter_next(it2)) {
2496 struct modcmd *command;
2497 rd2 = iter_data(it2);
2498 if (rd2->type != RECDB_QSTRING)
2499 continue;
2500 command = dict_find(module->commands, rd2->d.qstring, NULL);
2501 if (!command)
2502 continue;
2503 service_bind_modcmd(service, command, iter_key(it2));
2504 }
2505 }
2506}
2507
2508void
2509modcmd_finalize(void) {
2510 dict_iterator_t it;
2511
2512 /* Check databases. */
2513 saxdb_register("modcmd", modcmd_saxdb_read, modcmd_saxdb_write);
c8273589 2514 create_default_binds(0);
d76ed9a9 2515 if (!saxdb_present)
2516 import_aliases_db();
2517
2518 /* Register services for their triggers. */
2519 for (it = dict_first(services); it; it = iter_next(it)) {
2520 struct service *svc = iter_data(it);
2521 if (svc->trigger)
2522 reg_chanmsg_func(svc->trigger, svc->bot, modcmd_chanmsg);
2523 }
2524
2525 /* Resolve command rule-templates. */
2526 while (pending_templates) {
2527 struct pending_template *ptempl = pending_templates;
2528 struct svccmd *svccmd;
2529
2530 pending_templates = ptempl->next;
2531 /* Only overwrite the current template if we have a valid template. */
2532 if (!strcmp(ptempl->base, "*")) {
2533 /* Do nothing. */
2534 } else if ((svccmd = svccmd_resolve_name(ptempl->cmd, ptempl->base))) {
2535 svccmd_copy_rules(ptempl->cmd, svccmd);
2536 } else {
2537 assert(ptempl->cmd->parent);
2538 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);
2539 }
2540 free(ptempl->base);
2541 free(ptempl);
2542 }
2543}