5 #include "ClientSocket.h"
9 #include "WHOHandler.h"
12 struct trigger_callback {
14 trigger_callback_t *func;
16 struct trigger_callback *next;
19 struct command_check_user_cache {
20 struct ClientSocket *client, *textclient;
21 struct UserNode *user;
22 struct ChanNode *chan;
26 struct cmd_binding *cbind;
29 static struct cmd_binding **cmd_binds;
30 static struct cmd_function *cmd_functions = NULL;
31 static struct trigger_callback *trigger_callbacks = NULL;
32 static struct ClientSocket *tmp_text_client;
34 static const struct default_language_entry msgtab[] = {
35 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
36 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists."},
37 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
38 {"MODCMD_PRIVILEGED", "§b%s§b is a privileged command."},
44 static int get_binds_index(char first_char) {
45 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
46 return tolower(first_char - 'a');
51 struct ClientSocket* get_prefered_bot(int botid) {
52 struct ClientSocket *client;
53 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
54 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
60 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
61 struct trigger_cache *trigger;
62 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
63 if(trigger->botid == botid)
64 return trigger->trigger;
66 struct trigger_callback *cb;
67 for(cb = trigger_callbacks; cb; cb = cb->next) {
68 if(cb->botid == botid)
71 char triggerStr[TRIGGERLEN];
73 cb->func(chan, triggerStr);
75 strcpy(triggerStr, "+");
76 trigger = malloc(sizeof(*trigger));
78 perror("malloc() failed");
81 trigger->botid = botid;
82 trigger->trigger = strdup(triggerStr);
83 trigger->next = chan->trigger;
84 chan->trigger = trigger;
85 return trigger->trigger;
88 static USERAUTH_CALLBACK(command_checked_auth) {
89 struct command_check_user_cache *cache = data;
91 tmp_text_client = cache->textclient;
92 if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
93 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
96 else if((cache->cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
97 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cache->cbind->cmd);
101 cache->cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
103 free(cache->message);
107 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
108 if(message[0] == '#') {
109 char *chanName = message;
110 message = strstr(message, " ");
114 struct ChanNode *chan2 = getChanByName(chanName);
118 message = strdup(message);
119 int bind_index = get_binds_index(message[0]);
120 char *args = strstr(message, " ");
125 struct cmd_binding *cbind;
126 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
127 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
129 tmp_text_client = get_prefered_bot(client->botid);
130 //parse the arguments...
131 char *arga[MAXNUMPARAMS];
136 //skip leading spaces
140 if (argc >= MAXNUMPARAMS)
142 while (*args != ' ' && *args)
147 if(argc != 0 && argv[0][0] == '#') {
148 struct ChanNode *chan2 = getChanByName(argv[0]);
155 if(cbind->parameters) {
156 //userdefined parameters...
157 char *uarga[MAXNUMPARAMS];
158 char params[strlen(cbind->parameters)+1];
159 strcpy(params, cbind->parameters);
160 int uargpos = 0, argi, allargs = 0;
162 char *prev_ppos = params;
163 while((ppos = strstr(ppos, " "))) {
165 if(prev_ppos[0] == '%') {
167 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
169 prev_ppos[strlen(prev_ppos)-1] = '\0';
172 argi = atoi(prev_ppos);
174 if(argi <= argc) continue;
175 uarga[uargpos++] = argv[argi-1];
177 for(;argi < argc; argi++)
178 uarga[uargpos++] = argv[argi-1];
180 } else if(!strcmp(prev_ppos, "c"))
181 uarga[uargpos++] = (chan ? chan->name : NULL);
182 else if(!strcmp(prev_ppos, "n"))
183 uarga[uargpos++] = user->nick;
185 uarga[uargpos++] = prev_ppos;
193 if(argc < cbind->func->paramcount) {
194 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
197 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
198 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
201 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
202 load_channel_settings(chan);
203 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
204 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
210 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);
212 if ((row = mysql_fetch_row(res)) == NULL) {
213 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
217 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
219 struct command_check_user_cache *data = malloc(sizeof(*data));
220 char **temp_argv = malloc(argc*sizeof(*temp_argv));
221 if (!data || !temp_argv) {
222 perror("malloc() failed");
225 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
226 data->argv = temp_argv;
228 data->client = client;
231 data->message = message;
233 data->textclient = tmp_text_client;
234 get_userauth(user, command_checked_auth, data);
237 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
238 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
241 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
242 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
245 cbind->func->func(client, user, chan, argv, argc);
252 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
255 struct ClientSocket *client;
257 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
258 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
259 FD_SET(client->botid, &fds);
260 trigger = get_channel_trigger(client->botid, chan);
261 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
262 handle_command(client, user, chan, message + strlen(trigger));
266 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
267 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
268 FD_SET(client->botid, &fds);
269 trigger = get_channel_trigger(client->botid, chan);
270 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
271 handle_command(client, user, chan, message + strlen(trigger));
277 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
278 struct ClientSocket *client;
279 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
280 if(client->user == target) {
281 handle_command(client, user, NULL, message);
286 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
287 struct cmd_function *cmdfunc;
288 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
289 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
292 cmdfunc = malloc(sizeof(*cmdfunc));
294 perror("malloc() failed");
297 cmdfunc->botid = botid;
298 cmdfunc->name = strdup(name);
299 cmdfunc->func = func;
301 cmdfunc->paramcount = paramcount;
302 cmdfunc->next = cmd_functions;
303 cmd_functions = cmdfunc;
307 int set_trigger_callback(int botid, trigger_callback_t *func) {
308 static struct trigger_callback *cb = NULL;
309 for(cb = trigger_callbacks; cb; cb = cb->next) {
310 if(cb->botid == botid)
314 cb = malloc(sizeof(*cb));
316 perror("malloc() failed");
320 cb->next = trigger_callbacks;
321 trigger_callbacks = cb;
327 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
328 struct trigger_cache *trigger;
329 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
330 if(trigger->botid == botid) {
331 free(trigger->trigger);
332 trigger->trigger = strdup(new_trigger);
339 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
340 int bind_index = get_binds_index(cmd[0]);
341 struct cmd_binding *cbind;
342 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
343 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
346 cbind = malloc(sizeof(*cbind));
348 perror("malloc() failed");
351 cbind->botid = botid;
352 cbind->cmd = strdup(cmd);
354 cbind->parameters = NULL;
357 cbind->next = cmd_binds[bind_index];
358 cmd_binds[bind_index] = cbind;
362 int bind_cmd_to_command(int botid, char *cmd, char *func) {
363 struct cmd_function *cmdfunc;
364 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
365 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
368 if(!cmdfunc) return 0;
369 int bind_index = get_binds_index(cmd[0]);
370 struct cmd_binding *cbind;
371 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
372 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
375 cbind = malloc(sizeof(*cbind));
377 perror("malloc() failed");
380 cbind->botid = botid;
381 cbind->cmd = strdup(cmd);
382 cbind->func = cmdfunc;
383 cbind->next = cmd_binds[bind_index];
384 cbind->parameters = NULL;
387 cmd_binds[bind_index] = cbind;
391 int unbind_cmd(int botid, char *cmd) {
392 int bind_index = get_binds_index(cmd[0]);
393 struct cmd_binding *cbind, *last = NULL;
394 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
395 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
397 last->next = cbind->next;
399 cmd_binds[bind_index] = cbind->next;
401 if(cbind->parameters)
402 free(cbind->parameters);
411 struct ClientSocket *getTextBot() {
412 return tmp_text_client;
416 cmd_binds = calloc(27, sizeof(*cmd_binds));
417 bind_chanmsg(got_chanmsg);
418 bind_privmsg(got_privmsg);
419 register_default_language_table(msgtab);
424 for(i = 0; i < 27; i++) {
425 struct cmd_binding *cbind, *next;
426 for(cbind = cmd_binds[i]; cbind; cbind = next) {
429 if(cbind->parameters)
430 free(cbind->parameters);
435 struct cmd_function *cmdfunct, *next;
436 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
437 next = cmdfunct->next;
438 free(cmdfunct->name);
441 struct trigger_callback *cb, *next_cb;
442 for(cb = trigger_callbacks; cb; cb = next_cb) {
446 cmd_functions = NULL;
447 trigger_callbacks = NULL;
450 void bind_set_parameters(int botid, char *cmd, char *parameters) {
454 void bind_set_gaccess(int botid, char *cmd, int gaccess) {