5 #include "ClientSocket.h"
9 #include "WHOHandler.h"
11 #include "mysqlConn.h"
14 struct trigger_callback {
16 trigger_callback_t *func;
18 struct trigger_callback *next;
21 struct command_check_user_cache {
22 struct ClientSocket *client, *textclient;
23 struct UserNode *user;
24 struct ChanNode *chan, *sent_chan;
28 struct cmd_binding *cbind;
31 static struct cmd_binding **cmd_binds;
32 static struct cmd_function *cmd_functions = NULL;
33 static struct trigger_callback *trigger_callbacks = NULL;
34 static struct ClientSocket *tmp_text_client;
36 static const struct default_language_entry msgtab[] = {
37 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
38 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists."},
39 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
40 {"MODCMD_PRIVILEGED", "\002%s\002 is a privileged command."},
41 {"MODCMD_PUBCMD", "Public commands in \002%s\002 are restricted."},
42 {"MODCMD_ACCESS_DENIED", "Access denied."},
46 static int get_binds_index(char first_char) {
47 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
48 return tolower(first_char - 'a');
53 struct ClientSocket* get_prefered_bot(int botid) {
54 struct ClientSocket *client;
55 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
56 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
62 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
63 struct trigger_cache *trigger;
64 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
65 if(trigger->botid == botid)
66 return trigger->trigger;
68 struct trigger_callback *cb;
69 for(cb = trigger_callbacks; cb; cb = cb->next) {
70 if(cb->botid == botid)
73 char triggerStr[TRIGGERLEN];
75 cb->func(chan, triggerStr);
77 strcpy(triggerStr, "+");
78 trigger = malloc(sizeof(*trigger));
80 perror("malloc() failed");
83 trigger->botid = botid;
84 trigger->trigger = strdup(triggerStr);
85 trigger->next = chan->trigger;
86 chan->trigger = trigger;
87 return trigger->trigger;
90 static void handle_command_async(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc);
92 static USERAUTH_CALLBACK(command_checked_auth) {
93 struct command_check_user_cache *cache = data;
94 tmp_text_client = cache->textclient;
95 handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
100 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
101 struct ChanNode *sent_chan = chan;
102 if(message[0] == '#') {
103 char *chanName = message;
104 message = strstr(message, " ");
108 struct ChanNode *chan2 = getChanByName(chanName);
112 message = strdup(message);
113 int bind_index = get_binds_index(message[0]);
114 char *args = strstr(message, " ");
119 struct cmd_binding *cbind;
120 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
121 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
123 tmp_text_client = get_prefered_bot(client->botid);
124 //parse the arguments...
125 char *arga[MAXNUMPARAMS];
130 //skip leading spaces
134 if (argc >= MAXNUMPARAMS)
136 while (*args != ' ' && *args)
141 if(argc != 0 && argv[0][0] == '#') {
142 struct ChanNode *chan2 = getChanByName(argv[0]);
149 if(cbind->parameters) {
150 //userdefined parameters...
151 char *uarga[MAXNUMPARAMS];
152 char params[strlen(cbind->parameters)+1];
153 strcpy(params, cbind->parameters);
154 int uargpos = 0, argi, allargs = 0;
156 char *prev_ppos = params;
157 while((ppos = strstr(ppos, " "))) {
159 if(prev_ppos[0] == '%') {
161 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
163 prev_ppos[strlen(prev_ppos)-1] = '\0';
166 argi = atoi(prev_ppos);
168 if(argi <= argc) continue;
169 uarga[uargpos++] = argv[argi-1];
171 for(;argi < argc; argi++)
172 uarga[uargpos++] = argv[argi-1];
174 } else if(!strcmp(prev_ppos, "c"))
175 uarga[uargpos++] = (chan ? chan->name : NULL);
176 else if(!strcmp(prev_ppos, "n"))
177 uarga[uargpos++] = user->nick;
179 uarga[uargpos++] = prev_ppos;
187 if(argc < cbind->func->paramcount) {
188 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
191 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
192 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
195 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
197 struct command_check_user_cache *data = malloc(sizeof(*data));
198 char **temp_argv = malloc(argc*sizeof(*temp_argv));
199 if (!data || !temp_argv) {
200 perror("malloc() failed");
203 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
204 data->argv = temp_argv;
206 data->client = client;
209 data->sent_chan = sent_chan;
210 data->message = message;
212 data->textclient = tmp_text_client;
213 get_userauth(user, command_checked_auth, data);
216 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
223 static void handle_command_async(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc) {
227 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
228 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
231 if(sent_chan && sent_chan != chan) {
232 //check pubcmd of this channel
233 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
235 if ((row = mysql_fetch_row(res)) != NULL) {
236 uaccess = getChannelAccess(user, sent_chan, 1);
237 if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
238 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
243 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
244 if(global_access > 0) {
245 int user_global_access = 0;
246 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
248 if ((row = mysql_fetch_row(res)) != NULL) {
249 user_global_access = atoi(row[0]);
251 if(user_global_access < global_access) {
252 if(!user_global_access)
253 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
255 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
259 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
261 MYSQL_ROW defaults = NULL;
262 char access_list[256];
264 int access_count = 0;
266 char *str_a, *str_b = cbind->func->channel_access;
267 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
268 str_b = cbind->channel_access;
270 while((str_a = strstr(str_b, ","))) {
274 access_pos += sprintf(access_list+access_pos, ", `%s`", str_b);
277 if(atoi(str_b) > minaccess)
278 minaccess = atoi(str_b);
285 access_pos += sprintf(access_list+access_pos, ", `%s`", str_b);
287 } else if(atoi(str_b) > minaccess)
288 minaccess = atoi(str_b);
291 access_list[0] = '\0';
292 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
293 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
295 if ((row = mysql_fetch_row(res)) != NULL) {
296 chan->flags |= CHANFLAG_CHAN_REGISTERED;
297 chan->channel_id = atoi(row[0]);
298 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
299 uaccess = getChannelAccess(user, chan, 1);
300 if(uaccess < minaccess) {
302 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
305 if(!row[1] && !defaults) {
306 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
307 defaults = mysql_fetch_row(mysql_use());
309 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
311 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
315 for(i = 0; i < access_count; i++) {
316 if(!row[2+i] && !defaults) {
317 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
318 defaults = mysql_fetch_row(mysql_use());
320 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
321 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
327 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
329 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
330 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
333 printf_mysql_query("SELECT `botid` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, client->botid);
335 if ((row = mysql_fetch_row(res)) == NULL) {
336 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
340 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
341 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
344 cbind->func->func(client, user, chan, argv, argc);
347 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
350 struct ClientSocket *client;
352 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
353 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
354 FD_SET(client->botid, &fds);
355 trigger = get_channel_trigger(client->botid, chan);
356 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
357 handle_command(client, user, chan, message + strlen(trigger));
361 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
362 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
363 FD_SET(client->botid, &fds);
364 trigger = get_channel_trigger(client->botid, chan);
365 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
366 handle_command(client, user, chan, message + strlen(trigger));
372 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
373 struct ClientSocket *client;
374 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
375 if(client->user == target) {
376 handle_command(client, user, NULL, message);
381 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags, char *channel_access, int global_access) {
382 struct cmd_function *cmdfunc;
383 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
384 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
387 cmdfunc = malloc(sizeof(*cmdfunc));
389 perror("malloc() failed");
392 cmdfunc->botid = botid;
393 cmdfunc->name = strdup(name);
394 cmdfunc->func = func;
395 cmdfunc->flags = flags;
396 cmdfunc->paramcount = paramcount;
397 cmdfunc->channel_access = channel_access;
398 cmdfunc->global_access = global_access;
399 cmdfunc->next = cmd_functions;
400 cmd_functions = cmdfunc;
404 int set_trigger_callback(int botid, trigger_callback_t *func) {
405 static struct trigger_callback *cb = NULL;
406 for(cb = trigger_callbacks; cb; cb = cb->next) {
407 if(cb->botid == botid)
411 cb = malloc(sizeof(*cb));
413 perror("malloc() failed");
417 cb->next = trigger_callbacks;
418 trigger_callbacks = cb;
424 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
425 struct trigger_cache *trigger;
426 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
427 if(trigger->botid == botid) {
428 free(trigger->trigger);
429 trigger->trigger = strdup(new_trigger);
436 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
437 int bind_index = get_binds_index(cmd[0]);
438 struct cmd_binding *cbind;
439 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
440 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
443 cbind = malloc(sizeof(*cbind));
445 perror("malloc() failed");
448 cbind->botid = botid;
449 cbind->cmd = strdup(cmd);
451 cbind->parameters = NULL;
452 cbind->global_access = 0;
453 cbind->channel_access = NULL;
455 cbind->next = cmd_binds[bind_index];
456 cmd_binds[bind_index] = cbind;
460 int bind_cmd_to_command(int botid, char *cmd, char *func) {
461 struct cmd_function *cmdfunc;
462 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
463 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
466 if(!cmdfunc) return 0;
467 int bind_index = get_binds_index(cmd[0]);
468 struct cmd_binding *cbind;
469 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
470 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
473 cbind = malloc(sizeof(*cbind));
475 perror("malloc() failed");
478 cbind->botid = botid;
479 cbind->cmd = strdup(cmd);
480 cbind->func = cmdfunc;
481 cbind->next = cmd_binds[bind_index];
482 cbind->parameters = NULL;
483 cbind->global_access = 0;
484 cbind->channel_access = NULL;
486 cmd_binds[bind_index] = cbind;
490 int unbind_cmd(int botid, char *cmd) {
491 int bind_index = get_binds_index(cmd[0]);
492 struct cmd_binding *cbind, *last = NULL;
493 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
494 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
496 last->next = cbind->next;
498 cmd_binds[bind_index] = cbind->next;
500 if(cbind->parameters)
501 free(cbind->parameters);
510 struct ClientSocket *getTextBot() {
511 return tmp_text_client;
515 cmd_binds = calloc(27, sizeof(*cmd_binds));
516 bind_chanmsg(got_chanmsg);
517 bind_privmsg(got_privmsg);
518 register_default_language_table(msgtab);
523 for(i = 0; i < 27; i++) {
524 struct cmd_binding *cbind, *next;
525 for(cbind = cmd_binds[i]; cbind; cbind = next) {
528 if(cbind->parameters)
529 free(cbind->parameters);
530 if(cbind->channel_access)
531 free(cbind->channel_access);
536 struct cmd_function *cmdfunct, *next;
537 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
538 next = cmdfunct->next;
539 free(cmdfunct->name);
542 struct trigger_callback *cb, *next_cb;
543 for(cb = trigger_callbacks; cb; cb = next_cb) {
547 cmd_functions = NULL;
548 trigger_callbacks = NULL;
551 void bind_set_parameters(int botid, char *cmd, char *parameters) {
552 int bind_index = get_binds_index(cmd[0]);
553 struct cmd_binding *cbind;
554 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
555 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
556 if(cbind->parameters)
557 free(cbind->parameters);
558 cbind->parameters = strdup(parameters);
564 void bind_set_global_access(int botid, char *cmd, int gaccess) {
565 int bind_index = get_binds_index(cmd[0]);
566 struct cmd_binding *cbind;
567 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
568 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
570 cbind->global_access = gaccess;
571 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
573 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
580 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
581 int bind_index = get_binds_index(cmd[0]);
582 struct cmd_binding *cbind;
583 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
584 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
585 if(cbind->channel_access)
586 free(cbind->channel_access);
588 cbind->channel_access = strdup(chanaccess);
589 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
591 cbind->channel_access = NULL;
592 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;