-/* modcmd.c - NeonServ v5.3
- * Copyright (C) 2011 Philipp Kreil (pk910)
+/* modcmd.c - NeonServ v5.5
+ * Copyright (C) 2011-2012 Philipp Kreil (pk910)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
struct trigger_callback {
int botid;
+ int module_id;
trigger_callback_t *func;
struct trigger_callback *next;
static struct cmd_function *cmd_functions = NULL;
static struct trigger_callback *trigger_callbacks = NULL;
static struct cmd_bot_alias *bot_aliases = NULL;
-static struct ClientSocket *tmp_text_client;
static int total_triggered = 0;
int statistics_commands = 0;
return trigger->trigger;
}
-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);
+static void handle_command_async(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc);
static USERAUTH_CALLBACK(command_checked_auth) {
struct command_check_user_cache *cache = data;
- tmp_text_client = cache->textclient;
- handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
+ if(user)
+ handle_command_async(cache->client, cache->textclient, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
free(cache->message);
if(cache->args_buffer)
free(cache->args_buffer);
//just empty
}
+static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, int bind_index, char **args_ptr) {
+ //links subcommands
+ char command[MAXLEN];
+ struct cmd_binding *parent_bind = cbind;
+ char *args = *args_ptr;
+ if(args) {
+ char *subcmd = args;
+ args = strstr(args, " ");
+ if(args) {
+ *args = '\0';
+ args++;
+ }
+ sprintf(command, "%s %s", parent_bind->cmd, subcmd);
+ for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+ if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
+ break;
+ }
+ *args_ptr = args;
+ if(cbind && cbind->func->func == modcmd_linker) {
+ return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
+ }
+ return cbind;
+ } else {
+ //list all sub commands
+ int commandlen = sprintf(command, "%s ", parent_bind->cmd);
+ char subcommands[MAXLEN];
+ int subcompos = 0;
+ for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+ if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
+ if(strstr(cbind->cmd + commandlen, " ")) continue; //ignore if there is another space in the command (sub-sub-command :D)
+ subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
+ }
+ }
+ reply(textclient, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "\1dnone\1d"));
+ return NULL;
+ }
+}
+
static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
struct ChanNode *sent_chan = chan;
if(message[0] == '#') {
if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
found_cmd = 1;
//get a text bot
- tmp_text_client = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
+ struct ClientSocket *textclient = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
if(cbind->func->func == modcmd_linker) {
- //links subcommands
- char command[MAXLEN];
- struct cmd_binding *parent_bind = cbind;
- if(args) {
- char *subcmd = args;
- args = strstr(args, " ");
- if(args) {
- *args = '\0';
- args++;
- }
- sprintf(command, "%s %s", parent_bind->cmd, subcmd);
- for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
- if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
- break;
- }
- if(!cbind)
- break;
- } else {
- //list all sub commands
- int commandlen = sprintf(command, "%s ", parent_bind->cmd);
- char subcommands[MAXLEN];
- int subcompos = 0;
- for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
- if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
- subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
- }
- }
- reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "\1dnone\1d"));
- break;
- }
+ cbind = modcmd_linker_command(client, textclient, user, cbind, bind_index, &args);
+ if(cbind == NULL) break;
}
if(statistics_enabled)
statistics_commands++;
}
}
argv = arga;
- if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
- struct ChanNode *chan2 = getChanByName(argv[0]);
- if(chan2) {
- argv += 1;
- argc -= 1;
- chan = chan2;
- }
- }
if(cbind->paramcount) {
//userdefined parameters...
args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
argv = uargs;
argc = uargc;
}
+ if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
+ struct ChanNode *chan2 = getChanByName(argv[0]);
+ if(chan2) {
+ argv += 1;
+ argc -= 1;
+ chan = chan2;
+ }
+ }
if(argc < cbind->func->paramcount) {
- reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
+ reply(textclient, user, "MODCMD_LESS_PARAM_COUNT");
break;
}
if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) {
- reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
+ reply(textclient, user, "MODCMD_CHAN_REQUIRED");
break;
}
if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
data->message = message;
data->args_buffer = args_buffer;
data->cbind = cbind;
- data->textclient = tmp_text_client;
- get_userauth(user, command_checked_auth, data);
+ data->textclient = textclient;
+ get_userauth(user, 0, command_checked_auth, data);
return;
} else
- handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
+ handle_command_async(client, textclient, user, chan, sent_chan, cbind, argv, argc);
break;
}
}
- if(!found_cmd && !sent_chan)
+ if(!found_cmd && !sent_chan && !(client->flags & SOCKET_FLAG_SILENT))
reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
free(message);
if(args_buffer)
free(args_buffer);
}
-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) {
+static void handle_command_async(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc) {
MYSQL_RES *res;
MYSQL_ROW row;
int uaccess;
char requested_uaccess = 0;
int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG));
if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
- reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
+ reply(textclient, user, "MODCMD_AUTH_REQUIRED");
return;
}
- if(chan && sent_chan != chan && !isUserOnChan(user, chan)) {
+ if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) {
char user_in_chan = 0;
if((user->flags & USERFLAG_ISAUTHED)) {
//maybe there's another user authed to user->auth on the channel...
requested_uaccess = 1;
uaccess = getChannelAccess(user, chan);
if(!uaccess) {
- reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name);
- return;
+ if(isGodMode(user)) {
+ eventflags |= CMDFLAG_OPLOG;
+ } else {
+ reply(textclient, user, "MODCMD_CROSSCHAN", chan->name);
+ return;
+ }
}
}
}
if ((row = mysql_fetch_row(res)) != NULL) {
int saccess = getChannelAccess(user, sent_chan);
if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
- reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
+ reply(textclient, user, "MODCMD_PUBCMD", sent_chan->name);
return;
}
}
}
if(user_global_access < global_access) {
if(!user_global_access)
- reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
+ reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
else
- reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
+ reply(textclient, user, "MODCMD_ACCESS_DENIED");
return;
}
}
str_b = cbind->channel_access;
access_list[0] = '\0';
if(str_b) {
+ struct ChanUser *chanuser = getChanUser(user, chan);
str_c = strdup(str_b);
str_b = str_c;
while((str_a = str_b)) {
*str_b = '\0';
str_b++;
}
+ if(*str_a == '@' || *str_a == '+') {
+ //privs can override this access requirement
+ int priv = 0;
+ if(*str_a == '@') priv = CHANUSERFLAG_OPPED;
+ else if(*str_a == '%') priv = CHANUSERFLAG_HALFOPPED;
+ else if(*str_a == '+') priv = CHANUSERFLAG_VOICED;
+ if(chanuser && (chanuser->flags & priv)) continue;
+ str_a++;
+ }
if(*str_a == '#') {
str_a++;
access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
eventflags |= CMDFLAG_OPLOG;
} else if(uaccess < minaccess) {
//ACCESS DENIED
- reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
+ reply(textclient, user, "MODCMD_ACCESS_DENIED");
return;
}
if(!row[1] && !defaults) {
eventflags |= CMDFLAG_OPLOG;
} else {
//PUBCMD
- reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
+ reply(textclient, user, "MODCMD_PUBCMD", chan->name);
return;
}
}
if(isGodMode(user)) {
eventflags |= CMDFLAG_OPLOG;
} else {
- reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
+ reply(textclient, user, "MODCMD_ACCESS_DENIED");
return;
}
}
chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
}
if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
- reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
+ reply(textclient, user, "MODCMD_CHAN_REQUIRED");
return;
}
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);
res = mysql_use();
if ((row = mysql_fetch_row(res)) == NULL) {
- reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
+ reply(textclient, user, "MODCMD_CHAN_REQUIRED");
return;
} else if(!strcmp(row[1], "1")) {
- reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
+ reply(textclient, user, "MODCMD_CHAN_SUSPENDED");
return;
}
}
if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
- reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
+ reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
return;
}
struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
- cbind->func->func(client, user, chan, argv, argc, event);
+ cbind->func->func(client, textclient, user, chan, argv, argc, event);
}
static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
}
}
-int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
+int register_command(int botid, char *name, int module_id, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
struct cmd_function *cmdfunc;
for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
}
cmdfunc->botid = botid;
cmdfunc->name = strdup(name);
+ cmdfunc->module_id = module_id;
cmdfunc->func = func;
cmdfunc->flags = flags;
cmdfunc->paramcount = paramcount;
return 1;
}
-int set_trigger_callback(int botid, trigger_callback_t *func) {
+int set_trigger_callback(int botid, int module_id, trigger_callback_t *func) {
static struct trigger_callback *cb = NULL;
for(cb = trigger_callbacks; cb; cb = cb->next) {
if(cb->botid == botid)
cb->next = trigger_callbacks;
trigger_callbacks = cb;
}
+ cb->module_id = module_id;
cb->func = func;
return 1;
}
return 0;
}
-int unbind_botwise_allcmd(int clientid) {
+int unbind_botwise_allcmd(int botid, int clientid) {
int i;
for(i = 0; i < 27; i++) {
struct cmd_binding *cbind, *next, *last = NULL;
for(cbind = cmd_binds[i]; cbind; cbind = next) {
next = cbind->next;
- if(clientid == cbind->clientid) {
+ if((botid == 0 && clientid == cbind->clientid) || (botid && botid == cbind->botid)) {
if(last)
last->next = cbind->next;
else
return cmdfunc;
}
-struct ClientSocket *getTextBot() {
- return tmp_text_client;
-}
-
void init_modcmd() {
cmd_binds = calloc(27, sizeof(*cmd_binds));
- bind_chanmsg(got_chanmsg);
- bind_privmsg(got_privmsg);
+ bind_chanmsg(got_chanmsg, 0);
+ bind_privmsg(got_privmsg, 0);
register_default_language_table(msgtab);
- register_command(0, "linker", modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
+ register_command(0, "linker", 0, modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
}
void free_modcmd() {
} while(bind_index < 27);
return NULL;
}
+
+void unregister_module_commands(int module_id) {
+ struct cmd_function *cmdfunct, *nextfunct, *prevfunct = NULL;
+ int i;
+ for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = nextfunct) {
+ nextfunct = cmdfunct->next;
+ if(cmdfunct->module_id == module_id) {
+ for(i = 0; i < 27; i++) {
+ struct cmd_binding *cbind, *next, *last = NULL;
+ for(cbind = cmd_binds[i]; cbind; cbind = next) {
+ next = cbind->next;
+ if(cmdfunct == cbind->func) {
+ if(last)
+ last->next = cbind->next;
+ else
+ cmd_binds[i] = cbind->next;
+ free(cbind->cmd);
+ if(cbind->paramcount) {
+ int j;
+ for(j = 0; j < cbind->paramcount; j++)
+ free(cbind->parameters[j]);
+ }
+ if(cbind->channel_access)
+ free(cbind->channel_access);
+ free(cbind);
+ } else
+ last = cbind;
+ }
+ }
+ if(prevfunct)
+ prevfunct->next = nextfunct;
+ else
+ cmd_functions = nextfunct;
+ free(cmdfunct->name);
+ free(cmdfunct);
+ } else
+ prevfunct = cmdfunct;
+ }
+ struct trigger_callback *cb, *prevcb = NULL, *nextcb;
+ for(cb = trigger_callbacks; cb; cb = nextcb) {
+ nextcb = cb->next;
+ if(cb->module_id == module_id) {
+ if(prevcb)
+ prevcb->next = nextcb;
+ else
+ trigger_callbacks = nextcb;
+ free(cb);
+ } else
+ prevcb = cb;
+ }
+}