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."},
40 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
41 {"MODCMD_PRIVILEGED", "$b%s$b is a privileged command."}, /* {ARGS: "god"} */
42 {"MODCMD_PUBCMD", "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
43 {"MODCMD_ACCESS_DENIED", "Access denied."},
47 static int get_binds_index(char first_char) {
48 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
49 return tolower(first_char) - 'a';
54 struct ClientSocket* get_prefered_bot(int botid) {
55 struct ClientSocket *client;
56 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
57 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
63 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
64 struct trigger_cache *trigger;
65 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
66 if(trigger->botid == botid)
67 return trigger->trigger;
69 struct trigger_callback *cb;
70 for(cb = trigger_callbacks; cb; cb = cb->next) {
71 if(cb->botid == botid)
74 char triggerStr[TRIGGERLEN];
76 cb->func(chan, triggerStr);
78 strcpy(triggerStr, "+");
79 trigger = malloc(sizeof(*trigger));
81 perror("malloc() failed");
84 trigger->botid = botid;
85 trigger->trigger = strdup(triggerStr);
86 trigger->next = chan->trigger;
87 chan->trigger = trigger;
88 return trigger->trigger;
91 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);
93 static USERAUTH_CALLBACK(command_checked_auth) {
94 struct command_check_user_cache *cache = data;
95 tmp_text_client = cache->textclient;
96 handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
101 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
102 struct ChanNode *sent_chan = chan;
103 if(message[0] == '#') {
104 char *chanName = message;
105 message = strstr(message, " ");
109 struct ChanNode *chan2 = getChanByName(chanName);
113 message = strdup(message);
114 int bind_index = get_binds_index(message[0]);
115 char *args = strstr(message, " ");
120 struct cmd_binding *cbind;
121 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
122 if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
124 tmp_text_client = get_prefered_bot(client->botid);
125 //parse the arguments...
126 char *arga[MAXNUMPARAMS];
131 //skip leading spaces
135 if (argc >= MAXNUMPARAMS)
137 while (*args != ' ' && *args)
142 if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
143 struct ChanNode *chan2 = getChanByName(argv[0]);
150 if(cbind->parameters) {
151 //userdefined parameters...
152 char *uarga[MAXNUMPARAMS];
153 char params[strlen(cbind->parameters)+1];
154 strcpy(params, cbind->parameters);
155 int uargpos = 0, argi, allargs = 0;
157 char *prev_ppos = params;
158 while((ppos = strstr(ppos, " "))) {
160 if(prev_ppos[0] == '%') {
162 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
164 prev_ppos[strlen(prev_ppos)-1] = '\0';
167 argi = atoi(prev_ppos);
169 if(argi <= argc) continue;
170 uarga[uargpos++] = argv[argi-1];
172 for(;argi < argc; argi++)
173 uarga[uargpos++] = argv[argi-1];
175 } else if(!strcmp(prev_ppos, "c"))
176 uarga[uargpos++] = (chan ? chan->name : NULL);
177 else if(!strcmp(prev_ppos, "n"))
178 uarga[uargpos++] = user->nick;
180 uarga[uargpos++] = prev_ppos;
188 if(argc < cbind->func->paramcount) {
189 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
192 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
193 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
196 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
198 struct command_check_user_cache *data = malloc(sizeof(*data));
199 char **temp_argv = malloc(argc*sizeof(*temp_argv));
200 if (!data || !temp_argv) {
201 perror("malloc() failed");
204 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
205 data->argv = temp_argv;
207 data->client = client;
210 data->sent_chan = sent_chan;
211 data->message = message;
213 data->textclient = tmp_text_client;
214 get_userauth(user, command_checked_auth, data);
217 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
224 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) {
228 int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
229 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
230 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
233 if(sent_chan && sent_chan != chan) {
234 //check pubcmd of this channel
235 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
237 if ((row = mysql_fetch_row(res)) != NULL) {
238 uaccess = getChannelAccess(user, sent_chan, 1);
239 if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
240 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
245 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
246 if(global_access > 0) {
247 int user_global_access = 0;
248 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
250 if ((row = mysql_fetch_row(res)) != NULL) {
251 user_global_access = atoi(row[0]);
253 if(user_global_access < global_access) {
254 if(!user_global_access)
255 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
257 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
261 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
262 MYSQL_ROW defaults = NULL;
263 char access_list[256];
265 int access_count = 0;
267 char *str_a, *str_b = cbind->func->channel_access, *str_c;
268 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
269 str_b = cbind->channel_access;
270 access_list[0] = '\0';
272 str_c = strdup(str_b);
274 while((str_a = str_b)) {
275 str_b = strstr(str_a, ",");
282 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
285 if(atoi(str_a) > minaccess)
286 minaccess = atoi(str_a);
291 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
292 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
294 if ((row = mysql_fetch_row(res)) != NULL) {
295 chan->flags |= CHANFLAG_CHAN_REGISTERED;
296 chan->channel_id = atoi(row[0]);
297 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
298 uaccess = getChannelAccess(user, chan, 0);
299 if(uaccess < minaccess && isGodMode(user)) {
300 eventflags |= CMDFLAG_OPLOG;
301 } else if(uaccess < minaccess) {
303 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
306 if(!row[1] && !defaults) {
307 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
308 defaults = mysql_fetch_row(mysql_use());
310 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
311 if(isGodMode(user)) {
312 eventflags |= CMDFLAG_OPLOG;
315 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
320 for(i = 0; i < access_count; i++) {
321 if(!row[2+i] && !defaults) {
322 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
323 defaults = mysql_fetch_row(mysql_use());
325 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
326 if(isGodMode(user)) {
327 eventflags |= CMDFLAG_OPLOG;
329 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
336 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
338 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
339 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
342 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);
344 if ((row = mysql_fetch_row(res)) == NULL) {
345 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
349 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
350 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
353 struct Event *event = createEvent(client, user, chan, cbind->func->name, argv, argc, eventflags);
354 cbind->func->func(client, user, chan, argv, argc, event);
357 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
360 struct ClientSocket *client;
362 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
363 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
364 FD_SET(client->botid, &fds);
365 trigger = get_channel_trigger(client->botid, chan);
366 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
367 handle_command(client, user, chan, message + strlen(trigger));
371 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
372 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
373 FD_SET(client->botid, &fds);
374 trigger = get_channel_trigger(client->botid, chan);
375 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
376 handle_command(client, user, chan, message + strlen(trigger));
382 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
383 struct ClientSocket *client;
384 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
385 if(client->user == target) {
386 handle_command(client, user, NULL, message);
391 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags, char *channel_access, int global_access) {
392 struct cmd_function *cmdfunc;
393 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
394 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
397 cmdfunc = malloc(sizeof(*cmdfunc));
399 perror("malloc() failed");
402 cmdfunc->botid = botid;
403 cmdfunc->name = strdup(name);
404 cmdfunc->func = func;
405 cmdfunc->flags = flags;
406 cmdfunc->paramcount = paramcount;
407 cmdfunc->channel_access = channel_access;
408 cmdfunc->global_access = global_access;
409 cmdfunc->next = cmd_functions;
410 cmd_functions = cmdfunc;
414 int set_trigger_callback(int botid, trigger_callback_t *func) {
415 static struct trigger_callback *cb = NULL;
416 for(cb = trigger_callbacks; cb; cb = cb->next) {
417 if(cb->botid == botid)
421 cb = malloc(sizeof(*cb));
423 perror("malloc() failed");
427 cb->next = trigger_callbacks;
428 trigger_callbacks = cb;
434 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
435 struct trigger_cache *trigger;
436 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
437 if(trigger->botid == botid) {
438 free(trigger->trigger);
439 trigger->trigger = strdup(new_trigger);
446 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
447 int bind_index = get_binds_index(cmd[0]);
448 struct cmd_binding *cbind;
449 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
450 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
453 cbind = malloc(sizeof(*cbind));
455 perror("malloc() failed");
458 cbind->botid = botid;
459 cbind->cmd = strdup(cmd);
461 cbind->parameters = NULL;
462 cbind->global_access = 0;
463 cbind->channel_access = NULL;
465 cbind->next = cmd_binds[bind_index];
466 cmd_binds[bind_index] = cbind;
470 int bind_cmd_to_command(int botid, char *cmd, char *func) {
471 struct cmd_function *cmdfunc;
472 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
473 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
476 if(!cmdfunc) return 0;
477 int bind_index = get_binds_index(cmd[0]);
478 struct cmd_binding *cbind;
479 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
480 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
483 cbind = malloc(sizeof(*cbind));
485 perror("malloc() failed");
488 cbind->botid = botid;
489 cbind->cmd = strdup(cmd);
490 cbind->func = cmdfunc;
491 cbind->next = cmd_binds[bind_index];
492 cbind->parameters = NULL;
493 cbind->global_access = 0;
494 cbind->channel_access = NULL;
496 cmd_binds[bind_index] = cbind;
500 int unbind_cmd(int botid, char *cmd) {
501 int bind_index = get_binds_index(cmd[0]);
502 struct cmd_binding *cbind, *last = NULL;
503 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
504 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
506 last->next = cbind->next;
508 cmd_binds[bind_index] = cbind->next;
510 if(cbind->parameters)
511 free(cbind->parameters);
520 struct ClientSocket *getTextBot() {
521 return tmp_text_client;
525 cmd_binds = calloc(27, sizeof(*cmd_binds));
526 bind_chanmsg(got_chanmsg);
527 bind_privmsg(got_privmsg);
528 register_default_language_table(msgtab);
533 for(i = 0; i < 27; i++) {
534 struct cmd_binding *cbind, *next;
535 for(cbind = cmd_binds[i]; cbind; cbind = next) {
538 if(cbind->parameters)
539 free(cbind->parameters);
540 if(cbind->channel_access)
541 free(cbind->channel_access);
546 struct cmd_function *cmdfunct, *next;
547 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
548 next = cmdfunct->next;
549 free(cmdfunct->name);
552 struct trigger_callback *cb, *next_cb;
553 for(cb = trigger_callbacks; cb; cb = next_cb) {
557 cmd_functions = NULL;
558 trigger_callbacks = NULL;
561 void bind_set_parameters(int botid, char *cmd, char *parameters) {
562 int bind_index = get_binds_index(cmd[0]);
563 struct cmd_binding *cbind;
564 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
565 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
566 if(cbind->parameters)
567 free(cbind->parameters);
568 cbind->parameters = strdup(parameters);
574 void bind_set_global_access(int botid, char *cmd, int gaccess) {
575 int bind_index = get_binds_index(cmd[0]);
576 struct cmd_binding *cbind;
577 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
578 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
580 cbind->global_access = gaccess;
581 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
583 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
590 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
591 int bind_index = get_binds_index(cmd[0]);
592 struct cmd_binding *cbind;
593 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
594 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
595 if(cbind->channel_access)
596 free(cbind->channel_access);
598 cbind->channel_access = strdup(chanaccess);
599 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
601 cbind->channel_access = NULL;
602 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;