5 #include "ClientSocket.h"
9 #include "WHOHandler.h"
11 #include "mysqlConn.h"
14 struct trigger_callback {
16 trigger_callback_t *func;
18 struct trigger_callback *next;
21 struct command_check_user_cache {
22 struct ClientSocket *client, *textclient;
23 struct UserNode *user;
24 struct ChanNode *chan;
28 struct cmd_binding *cbind;
31 static struct cmd_binding **cmd_binds;
32 static struct cmd_function *cmd_functions = NULL;
33 static struct trigger_callback *trigger_callbacks = NULL;
34 static struct ClientSocket *tmp_text_client;
36 static const struct default_language_entry msgtab[] = {
37 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
38 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists."},
39 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
40 {"MODCMD_PRIVILEGED", "§b%s§b is a privileged command."},
46 static int get_binds_index(char first_char) {
47 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
48 return tolower(first_char - 'a');
53 struct ClientSocket* get_prefered_bot(int botid) {
54 struct ClientSocket *client;
55 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
56 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
62 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
63 struct trigger_cache *trigger;
64 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
65 if(trigger->botid == botid)
66 return trigger->trigger;
68 struct trigger_callback *cb;
69 for(cb = trigger_callbacks; cb; cb = cb->next) {
70 if(cb->botid == botid)
73 char triggerStr[TRIGGERLEN];
75 cb->func(chan, triggerStr);
77 strcpy(triggerStr, "+");
78 trigger = malloc(sizeof(*trigger));
80 perror("malloc() failed");
83 trigger->botid = botid;
84 trigger->trigger = strdup(triggerStr);
85 trigger->next = chan->trigger;
86 chan->trigger = trigger;
87 return trigger->trigger;
90 static USERAUTH_CALLBACK(command_checked_auth) {
91 struct command_check_user_cache *cache = data;
93 tmp_text_client = cache->textclient;
94 if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
95 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
98 else if((cache->cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
99 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cache->cbind->cmd);
103 cache->cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
105 free(cache->message);
109 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
110 if(message[0] == '#') {
111 char *chanName = message;
112 message = strstr(message, " ");
116 struct ChanNode *chan2 = getChanByName(chanName);
120 message = strdup(message);
121 int bind_index = get_binds_index(message[0]);
122 char *args = strstr(message, " ");
127 struct cmd_binding *cbind;
128 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
129 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
131 tmp_text_client = get_prefered_bot(client->botid);
132 //parse the arguments...
133 char *arga[MAXNUMPARAMS];
138 //skip leading spaces
142 if (argc >= MAXNUMPARAMS)
144 while (*args != ' ' && *args)
149 if(argc != 0 && argv[0][0] == '#') {
150 struct ChanNode *chan2 = getChanByName(argv[0]);
157 if(cbind->parameters) {
158 //userdefined parameters...
159 char *uarga[MAXNUMPARAMS];
160 char params[strlen(cbind->parameters)+1];
161 strcpy(params, cbind->parameters);
162 int uargpos = 0, argi, allargs = 0;
164 char *prev_ppos = params;
165 while((ppos = strstr(ppos, " "))) {
167 if(prev_ppos[0] == '%') {
169 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
171 prev_ppos[strlen(prev_ppos)-1] = '\0';
174 argi = atoi(prev_ppos);
176 if(argi <= argc) continue;
177 uarga[uargpos++] = argv[argi-1];
179 for(;argi < argc; argi++)
180 uarga[uargpos++] = argv[argi-1];
182 } else if(!strcmp(prev_ppos, "c"))
183 uarga[uargpos++] = (chan ? chan->name : NULL);
184 else if(!strcmp(prev_ppos, "n"))
185 uarga[uargpos++] = user->nick;
187 uarga[uargpos++] = prev_ppos;
195 if(argc < cbind->func->paramcount) {
196 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
199 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
200 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
203 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
204 loadChannelSettings(chan);
205 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
206 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
212 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);
214 if ((row = mysql_fetch_row(res)) == NULL) {
215 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
219 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
221 struct command_check_user_cache *data = malloc(sizeof(*data));
222 char **temp_argv = malloc(argc*sizeof(*temp_argv));
223 if (!data || !temp_argv) {
224 perror("malloc() failed");
227 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
228 data->argv = temp_argv;
230 data->client = client;
233 data->message = message;
235 data->textclient = tmp_text_client;
236 get_userauth(user, command_checked_auth, data);
239 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
240 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
243 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
244 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
247 cbind->func->func(client, user, chan, argv, argc);
254 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
257 struct ClientSocket *client;
259 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
260 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
261 FD_SET(client->botid, &fds);
262 trigger = get_channel_trigger(client->botid, chan);
263 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
264 handle_command(client, user, chan, message + strlen(trigger));
268 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
269 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
270 FD_SET(client->botid, &fds);
271 trigger = get_channel_trigger(client->botid, chan);
272 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
273 handle_command(client, user, chan, message + strlen(trigger));
279 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
280 struct ClientSocket *client;
281 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
282 if(client->user == target) {
283 handle_command(client, user, NULL, message);
288 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
289 struct cmd_function *cmdfunc;
290 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
291 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
294 cmdfunc = malloc(sizeof(*cmdfunc));
296 perror("malloc() failed");
299 cmdfunc->botid = botid;
300 cmdfunc->name = strdup(name);
301 cmdfunc->func = func;
302 cmdfunc->flags = flags;
303 cmdfunc->paramcount = paramcount;
304 cmdfunc->next = cmd_functions;
305 cmd_functions = cmdfunc;
309 int set_trigger_callback(int botid, trigger_callback_t *func) {
310 static struct trigger_callback *cb = NULL;
311 for(cb = trigger_callbacks; cb; cb = cb->next) {
312 if(cb->botid == botid)
316 cb = malloc(sizeof(*cb));
318 perror("malloc() failed");
322 cb->next = trigger_callbacks;
323 trigger_callbacks = cb;
329 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
330 struct trigger_cache *trigger;
331 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
332 if(trigger->botid == botid) {
333 free(trigger->trigger);
334 trigger->trigger = strdup(new_trigger);
341 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
342 int bind_index = get_binds_index(cmd[0]);
343 struct cmd_binding *cbind;
344 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
345 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
348 cbind = malloc(sizeof(*cbind));
350 perror("malloc() failed");
353 cbind->botid = botid;
354 cbind->cmd = strdup(cmd);
356 cbind->parameters = NULL;
359 cbind->next = cmd_binds[bind_index];
360 cmd_binds[bind_index] = cbind;
364 int bind_cmd_to_command(int botid, char *cmd, char *func) {
365 struct cmd_function *cmdfunc;
366 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
367 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
370 if(!cmdfunc) return 0;
371 int bind_index = get_binds_index(cmd[0]);
372 struct cmd_binding *cbind;
373 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
374 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
377 cbind = malloc(sizeof(*cbind));
379 perror("malloc() failed");
382 cbind->botid = botid;
383 cbind->cmd = strdup(cmd);
384 cbind->func = cmdfunc;
385 cbind->next = cmd_binds[bind_index];
386 cbind->parameters = NULL;
389 cmd_binds[bind_index] = cbind;
393 int unbind_cmd(int botid, char *cmd) {
394 int bind_index = get_binds_index(cmd[0]);
395 struct cmd_binding *cbind, *last = NULL;
396 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
397 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
399 last->next = cbind->next;
401 cmd_binds[bind_index] = cbind->next;
403 if(cbind->parameters)
404 free(cbind->parameters);
413 struct ClientSocket *getTextBot() {
414 return tmp_text_client;
418 cmd_binds = calloc(27, sizeof(*cmd_binds));
419 bind_chanmsg(got_chanmsg);
420 bind_privmsg(got_privmsg);
421 register_default_language_table(msgtab);
426 for(i = 0; i < 27; i++) {
427 struct cmd_binding *cbind, *next;
428 for(cbind = cmd_binds[i]; cbind; cbind = next) {
431 if(cbind->parameters)
432 free(cbind->parameters);
437 struct cmd_function *cmdfunct, *next;
438 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
439 next = cmdfunct->next;
440 free(cmdfunct->name);
443 struct trigger_callback *cb, *next_cb;
444 for(cb = trigger_callbacks; cb; cb = next_cb) {
448 cmd_functions = NULL;
449 trigger_callbacks = NULL;
452 void bind_set_parameters(int botid, char *cmd, char *parameters) {
456 void bind_set_gaccess(int botid, char *cmd, int gaccess) {