5 #include "ClientSocket.h"
9 #include "WHOHandler.h"
11 #include "mysqlConn.h"
13 #include "EventLogger.h"
15 struct trigger_callback {
17 trigger_callback_t *func;
19 struct trigger_callback *next;
22 struct command_check_user_cache {
23 struct ClientSocket *client, *textclient;
24 struct UserNode *user;
25 struct ChanNode *chan, *sent_chan;
29 struct cmd_binding *cbind;
32 static struct cmd_binding **cmd_binds;
33 static struct cmd_function *cmd_functions = NULL;
34 static struct trigger_callback *trigger_callbacks = NULL;
35 static struct ClientSocket *tmp_text_client;
37 static const struct default_language_entry msgtab[] = {
38 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
39 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists and the bot is on."},
40 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
41 {"MODCMD_CHAN_SUSPENDED", "This channel is currently suspended."},
42 {"MODCMD_PRIVILEGED", "$b%s$b is a privileged command."}, /* {ARGS: "god"} */
43 {"MODCMD_PUBCMD", "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
44 {"MODCMD_ACCESS_DENIED", "Access denied."},
48 static int get_binds_index(char first_char) {
49 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
50 return tolower(first_char) - 'a';
55 struct ClientSocket* get_prefered_bot(int botid) {
56 struct ClientSocket *client;
57 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
58 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
64 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
65 struct trigger_cache *trigger;
66 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
67 if(trigger->botid == botid)
68 return trigger->trigger;
70 struct trigger_callback *cb;
71 for(cb = trigger_callbacks; cb; cb = cb->next) {
72 if(cb->botid == botid)
75 char triggerStr[TRIGGERLEN];
77 cb->func(chan, triggerStr);
79 strcpy(triggerStr, "+");
80 trigger = malloc(sizeof(*trigger));
82 perror("malloc() failed");
85 trigger->botid = botid;
86 trigger->trigger = strdup(triggerStr);
87 trigger->next = chan->trigger;
88 chan->trigger = trigger;
89 return trigger->trigger;
92 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);
94 static USERAUTH_CALLBACK(command_checked_auth) {
95 struct command_check_user_cache *cache = data;
96 tmp_text_client = cache->textclient;
97 handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
102 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
103 struct ChanNode *sent_chan = chan;
104 if(message[0] == '#') {
105 char *chanName = message;
106 message = strstr(message, " ");
110 struct ChanNode *chan2 = getChanByName(chanName);
114 message = strdup(message);
115 int bind_index = get_binds_index(message[0]);
116 char *args = strstr(message, " ");
121 struct cmd_binding *cbind;
122 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
123 if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
125 tmp_text_client = get_prefered_bot(client->botid);
126 //parse the arguments...
127 char *arga[MAXNUMPARAMS];
132 //skip leading spaces
136 if (argc >= MAXNUMPARAMS)
138 while (*args != ' ' && *args)
143 if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
144 struct ChanNode *chan2 = getChanByName(argv[0]);
151 if(cbind->parameters) {
152 //userdefined parameters...
153 char *uarga[MAXNUMPARAMS];
154 char params[strlen(cbind->parameters)+1];
155 strcpy(params, cbind->parameters);
156 int uargpos = 0, argi, allargs = 0;
158 char *prev_ppos = params;
159 while((ppos = strstr(ppos, " "))) {
161 if(prev_ppos[0] == '%') {
163 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
165 prev_ppos[strlen(prev_ppos)-1] = '\0';
168 argi = atoi(prev_ppos);
170 if(argi <= argc) continue;
171 uarga[uargpos++] = argv[argi-1];
173 for(;argi < argc; argi++)
174 uarga[uargpos++] = argv[argi-1];
176 } else if(!strcmp(prev_ppos, "c"))
177 uarga[uargpos++] = (chan ? chan->name : NULL);
178 else if(!strcmp(prev_ppos, "n"))
179 uarga[uargpos++] = user->nick;
181 uarga[uargpos++] = prev_ppos;
189 if(argc < cbind->func->paramcount) {
190 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
193 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
194 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
197 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
199 struct command_check_user_cache *data = malloc(sizeof(*data));
200 char **temp_argv = malloc(argc*sizeof(*temp_argv));
201 if (!data || !temp_argv) {
202 perror("malloc() failed");
205 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
206 data->argv = temp_argv;
208 data->client = client;
211 data->sent_chan = sent_chan;
212 data->message = message;
214 data->textclient = tmp_text_client;
215 get_userauth(user, command_checked_auth, data);
218 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
225 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) {
229 int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
230 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
231 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
234 if(sent_chan && sent_chan != chan) {
235 //check pubcmd of this channel
236 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
238 if ((row = mysql_fetch_row(res)) != NULL) {
239 uaccess = getChannelAccess(user, sent_chan, 1);
240 if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
241 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
246 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
247 if(global_access > 0) {
248 int user_global_access = 0;
249 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
251 if ((row = mysql_fetch_row(res)) != NULL) {
252 user_global_access = atoi(row[0]);
254 if(user_global_access < global_access) {
255 if(!user_global_access)
256 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
258 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
262 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
263 MYSQL_ROW defaults = NULL;
264 char access_list[256];
266 int access_count = 0;
268 char *str_a, *str_b = cbind->func->channel_access, *str_c;
269 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
270 str_b = cbind->channel_access;
271 access_list[0] = '\0';
273 str_c = strdup(str_b);
275 while((str_a = str_b)) {
276 str_b = strstr(str_a, ",");
283 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
286 if(atoi(str_a) > minaccess)
287 minaccess = atoi(str_a);
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, 0);
300 if(uaccess < minaccess && isGodMode(user)) {
301 eventflags |= CMDFLAG_OPLOG;
302 } else if(uaccess < minaccess) {
304 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
307 if(!row[1] && !defaults) {
308 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
309 defaults = mysql_fetch_row(mysql_use());
311 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
312 if(isGodMode(user)) {
313 eventflags |= CMDFLAG_OPLOG;
316 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
321 for(i = 0; i < access_count; i++) {
322 if(!row[2+i] && !defaults) {
323 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
324 defaults = mysql_fetch_row(mysql_use());
326 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
327 if(isGodMode(user)) {
328 eventflags |= CMDFLAG_OPLOG;
330 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
337 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
339 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
340 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
343 printf_mysql_query("SELECT `botid`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, client->botid);
345 if ((row = mysql_fetch_row(res)) == NULL) {
346 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
348 } else if(!strcmp(row[1], "1")) {
349 reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
353 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
354 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
357 struct Event *event = createEvent(client, user, chan, cbind->func->name, argv, argc, eventflags);
358 cbind->func->func(client, user, chan, argv, argc, event);
361 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
364 struct ClientSocket *client;
366 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
367 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
368 FD_SET(client->botid, &fds);
369 trigger = get_channel_trigger(client->botid, chan);
370 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
371 handle_command(client, user, chan, message + strlen(trigger));
375 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
376 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
377 FD_SET(client->botid, &fds);
378 trigger = get_channel_trigger(client->botid, chan);
379 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
380 handle_command(client, user, chan, message + strlen(trigger));
386 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
387 struct ClientSocket *client;
388 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
389 if(client->user == target) {
390 handle_command(client, user, NULL, message);
395 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags, char *channel_access, int global_access) {
396 struct cmd_function *cmdfunc;
397 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
398 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
401 cmdfunc = malloc(sizeof(*cmdfunc));
403 perror("malloc() failed");
406 cmdfunc->botid = botid;
407 cmdfunc->name = strdup(name);
408 cmdfunc->func = func;
409 cmdfunc->flags = flags;
410 cmdfunc->paramcount = paramcount;
411 cmdfunc->channel_access = channel_access;
412 cmdfunc->global_access = global_access;
413 cmdfunc->next = cmd_functions;
414 cmd_functions = cmdfunc;
418 int set_trigger_callback(int botid, trigger_callback_t *func) {
419 static struct trigger_callback *cb = NULL;
420 for(cb = trigger_callbacks; cb; cb = cb->next) {
421 if(cb->botid == botid)
425 cb = malloc(sizeof(*cb));
427 perror("malloc() failed");
431 cb->next = trigger_callbacks;
432 trigger_callbacks = cb;
438 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
439 struct trigger_cache *trigger;
440 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
441 if(trigger->botid == botid) {
442 free(trigger->trigger);
443 trigger->trigger = strdup(new_trigger);
450 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
451 int bind_index = get_binds_index(cmd[0]);
452 struct cmd_binding *cbind;
453 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
454 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
457 cbind = malloc(sizeof(*cbind));
459 perror("malloc() failed");
462 cbind->botid = botid;
463 cbind->cmd = strdup(cmd);
465 cbind->parameters = NULL;
466 cbind->global_access = 0;
467 cbind->channel_access = NULL;
469 cbind->next = cmd_binds[bind_index];
470 cmd_binds[bind_index] = cbind;
474 int bind_cmd_to_command(int botid, char *cmd, char *func) {
475 struct cmd_function *cmdfunc;
476 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
477 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
480 if(!cmdfunc) return 0;
481 int bind_index = get_binds_index(cmd[0]);
482 struct cmd_binding *cbind;
483 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
484 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
487 cbind = malloc(sizeof(*cbind));
489 perror("malloc() failed");
492 cbind->botid = botid;
493 cbind->cmd = strdup(cmd);
494 cbind->func = cmdfunc;
495 cbind->next = cmd_binds[bind_index];
496 cbind->parameters = NULL;
497 cbind->global_access = 0;
498 cbind->channel_access = NULL;
500 cmd_binds[bind_index] = cbind;
504 int unbind_cmd(int botid, char *cmd) {
505 int bind_index = get_binds_index(cmd[0]);
506 struct cmd_binding *cbind, *last = NULL;
507 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
508 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
510 last->next = cbind->next;
512 cmd_binds[bind_index] = cbind->next;
514 if(cbind->parameters)
515 free(cbind->parameters);
524 struct ClientSocket *getTextBot() {
525 return tmp_text_client;
529 cmd_binds = calloc(27, sizeof(*cmd_binds));
530 bind_chanmsg(got_chanmsg);
531 bind_privmsg(got_privmsg);
532 register_default_language_table(msgtab);
537 for(i = 0; i < 27; i++) {
538 struct cmd_binding *cbind, *next;
539 for(cbind = cmd_binds[i]; cbind; cbind = next) {
542 if(cbind->parameters)
543 free(cbind->parameters);
544 if(cbind->channel_access)
545 free(cbind->channel_access);
550 struct cmd_function *cmdfunct, *next;
551 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
552 next = cmdfunct->next;
553 free(cmdfunct->name);
556 struct trigger_callback *cb, *next_cb;
557 for(cb = trigger_callbacks; cb; cb = next_cb) {
561 cmd_functions = NULL;
562 trigger_callbacks = NULL;
565 void bind_set_parameters(int botid, char *cmd, char *parameters) {
566 int bind_index = get_binds_index(cmd[0]);
567 struct cmd_binding *cbind;
568 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
569 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
570 if(cbind->parameters)
571 free(cbind->parameters);
572 cbind->parameters = strdup(parameters);
578 void bind_set_global_access(int botid, char *cmd, int gaccess) {
579 int bind_index = get_binds_index(cmd[0]);
580 struct cmd_binding *cbind;
581 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
582 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
584 cbind->global_access = gaccess;
585 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
587 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
594 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
595 int bind_index = get_binds_index(cmd[0]);
596 struct cmd_binding *cbind;
597 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
598 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
599 if(cbind->channel_access)
600 free(cbind->channel_access);
602 cbind->channel_access = strdup(chanaccess);
603 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
605 cbind->channel_access = NULL;
606 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;