5 #include "ClientSocket.h"
9 #include "WHOHandler.h"
11 #include "mysqlConn.h"
13 struct trigger_callback {
15 trigger_callback_t *func;
17 struct trigger_callback *next;
20 struct command_check_user_cache {
21 struct ClientSocket *client, *textclient;
22 struct UserNode *user;
23 struct ChanNode *chan;
27 struct cmd_binding *cbind;
30 static struct cmd_binding **cmd_binds;
31 static struct cmd_function *cmd_functions = NULL;
32 static struct trigger_callback *trigger_callbacks = NULL;
33 static struct ClientSocket *tmp_text_client;
35 static const struct default_language_entry msgtab[] = {
36 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
37 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists."},
38 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
39 {"MODCMD_PRIVILEGED", "§b%s§b is a privileged command."},
45 static int get_binds_index(char first_char) {
46 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
47 return tolower(first_char - 'a');
52 struct ClientSocket* get_prefered_bot(int botid) {
53 struct ClientSocket *client;
54 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
55 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
61 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
62 struct trigger_cache *trigger;
63 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
64 if(trigger->botid == botid)
65 return trigger->trigger;
67 struct trigger_callback *cb;
68 for(cb = trigger_callbacks; cb; cb = cb->next) {
69 if(cb->botid == botid)
72 char triggerStr[TRIGGERLEN];
74 cb->func(chan, triggerStr);
76 strcpy(triggerStr, "+");
77 trigger = malloc(sizeof(*trigger));
79 perror("malloc() failed");
82 trigger->botid = botid;
83 trigger->trigger = strdup(triggerStr);
84 trigger->next = chan->trigger;
85 chan->trigger = trigger;
86 return trigger->trigger;
89 static USERAUTH_CALLBACK(command_checked_auth) {
90 struct command_check_user_cache *cache = data;
92 tmp_text_client = cache->textclient;
93 if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
94 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
97 else if((cache->cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
98 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cache->cbind->cmd);
102 cache->cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
104 free(cache->message);
108 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
109 if(message[0] == '#') {
110 char *chanName = message;
111 message = strstr(message, " ");
115 struct ChanNode *chan2 = getChanByName(chanName);
119 message = strdup(message);
120 int bind_index = get_binds_index(message[0]);
121 char *args = strstr(message, " ");
126 struct cmd_binding *cbind;
127 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
128 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
130 tmp_text_client = get_prefered_bot(client->botid);
131 //parse the arguments...
132 char *arga[MAXNUMPARAMS];
137 //skip leading spaces
141 if (argc >= MAXNUMPARAMS)
143 while (*args != ' ' && *args)
148 if(argc != 0 && argv[0][0] == '#') {
149 struct ChanNode *chan2 = getChanByName(argv[0]);
156 if(cbind->parameters) {
157 //userdefined parameters...
158 char *uarga[MAXNUMPARAMS];
159 char params[strlen(cbind->parameters)+1];
160 strcpy(params, cbind->parameters);
161 int uargpos = 0, argi, allargs = 0;
163 char *prev_ppos = params;
164 while((ppos = strstr(ppos, " "))) {
166 if(prev_ppos[0] == '%') {
168 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
170 prev_ppos[strlen(prev_ppos)-1] = '\0';
173 argi = atoi(prev_ppos);
175 if(argi <= argc) continue;
176 uarga[uargpos++] = argv[argi-1];
178 for(;argi < argc; argi++)
179 uarga[uargpos++] = argv[argi-1];
181 } else if(!strcmp(prev_ppos, "c"))
182 uarga[uargpos++] = (chan ? chan->name : NULL);
183 else if(!strcmp(prev_ppos, "n"))
184 uarga[uargpos++] = user->nick;
186 uarga[uargpos++] = prev_ppos;
194 if(argc < cbind->func->paramcount) {
195 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
198 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
199 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
202 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
203 load_channel_settings(chan);
204 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
205 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
211 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);
213 if ((row = mysql_fetch_row(res)) == NULL) {
214 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
218 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
220 struct command_check_user_cache *data = malloc(sizeof(*data));
221 char **temp_argv = malloc(argc*sizeof(*temp_argv));
222 if (!data || !temp_argv) {
223 perror("malloc() failed");
226 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
227 data->argv = temp_argv;
229 data->client = client;
232 data->message = message;
234 data->textclient = tmp_text_client;
235 get_userauth(user, command_checked_auth, data);
238 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
239 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
242 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
243 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
246 cbind->func->func(client, user, chan, argv, argc);
253 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
256 struct ClientSocket *client;
258 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
259 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
260 FD_SET(client->botid, &fds);
261 trigger = get_channel_trigger(client->botid, chan);
262 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
263 handle_command(client, user, chan, message + strlen(trigger));
267 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
268 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
269 FD_SET(client->botid, &fds);
270 trigger = get_channel_trigger(client->botid, chan);
271 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
272 handle_command(client, user, chan, message + strlen(trigger));
278 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
279 struct ClientSocket *client;
280 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
281 if(client->user == target) {
282 handle_command(client, user, NULL, message);
287 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
288 struct cmd_function *cmdfunc;
289 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
290 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
293 cmdfunc = malloc(sizeof(*cmdfunc));
295 perror("malloc() failed");
298 cmdfunc->botid = botid;
299 cmdfunc->name = strdup(name);
300 cmdfunc->func = func;
302 cmdfunc->paramcount = paramcount;
303 cmdfunc->next = cmd_functions;
304 cmd_functions = cmdfunc;
308 int set_trigger_callback(int botid, trigger_callback_t *func) {
309 static struct trigger_callback *cb = NULL;
310 for(cb = trigger_callbacks; cb; cb = cb->next) {
311 if(cb->botid == botid)
315 cb = malloc(sizeof(*cb));
317 perror("malloc() failed");
321 cb->next = trigger_callbacks;
322 trigger_callbacks = cb;
328 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
329 struct trigger_cache *trigger;
330 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
331 if(trigger->botid == botid) {
332 free(trigger->trigger);
333 trigger->trigger = strdup(new_trigger);
340 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
341 int bind_index = get_binds_index(cmd[0]);
342 struct cmd_binding *cbind;
343 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
344 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
347 cbind = malloc(sizeof(*cbind));
349 perror("malloc() failed");
352 cbind->botid = botid;
353 cbind->cmd = strdup(cmd);
355 cbind->parameters = NULL;
358 cbind->next = cmd_binds[bind_index];
359 cmd_binds[bind_index] = cbind;
363 int bind_cmd_to_command(int botid, char *cmd, char *func) {
364 struct cmd_function *cmdfunc;
365 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
366 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
369 if(!cmdfunc) return 0;
370 int bind_index = get_binds_index(cmd[0]);
371 struct cmd_binding *cbind;
372 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
373 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
376 cbind = malloc(sizeof(*cbind));
378 perror("malloc() failed");
381 cbind->botid = botid;
382 cbind->cmd = strdup(cmd);
383 cbind->func = cmdfunc;
384 cbind->next = cmd_binds[bind_index];
385 cbind->parameters = NULL;
388 cmd_binds[bind_index] = cbind;
392 int unbind_cmd(int botid, char *cmd) {
393 int bind_index = get_binds_index(cmd[0]);
394 struct cmd_binding *cbind, *last = NULL;
395 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
396 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
398 last->next = cbind->next;
400 cmd_binds[bind_index] = cbind->next;
402 if(cbind->parameters)
403 free(cbind->parameters);
412 struct ClientSocket *getTextBot() {
413 return tmp_text_client;
417 cmd_binds = calloc(27, sizeof(*cmd_binds));
418 bind_chanmsg(got_chanmsg);
419 bind_privmsg(got_privmsg);
420 register_default_language_table(msgtab);
425 for(i = 0; i < 27; i++) {
426 struct cmd_binding *cbind, *next;
427 for(cbind = cmd_binds[i]; cbind; cbind = next) {
430 if(cbind->parameters)
431 free(cbind->parameters);
436 struct cmd_function *cmdfunct, *next;
437 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
438 next = cmdfunct->next;
439 free(cmdfunct->name);
442 struct trigger_callback *cb, *next_cb;
443 for(cb = trigger_callbacks; cb; cb = next_cb) {
447 cmd_functions = NULL;
448 trigger_callbacks = NULL;
451 void bind_set_parameters(int botid, char *cmd, char *parameters) {
455 void bind_set_gaccess(int botid, char *cmd, int gaccess) {