4 #include "ClientSocket.h"
8 #include "WHOHandler.h"
10 struct trigger_callback {
12 trigger_callback_t *func;
14 struct trigger_callback *next;
17 struct command_check_user_cache {
18 struct ClientSocket *client;
19 struct UserNode *user;
20 struct ChanNode *chan;
24 struct cmd_binding *cbind;
27 static struct cmd_binding **cmd_binds;
28 static struct cmd_function *cmd_functions = NULL;
29 static struct trigger_callback *trigger_callbacks = NULL;
31 static int get_binds_index(char first_char) {
32 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
33 return tolower(first_char - 'a');
38 struct ClientSocket* get_prefered_bot(int botid) {
39 struct ClientSocket *client;
40 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
41 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
47 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
48 struct trigger_cache *trigger;
49 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
50 if(trigger->botid == botid)
51 return trigger->trigger;
53 struct trigger_callback *cb;
54 for(cb = trigger_callbacks; cb; cb = cb->next) {
55 if(cb->botid == botid)
58 char triggerStr[TRIGGERLEN];
60 cb->func(chan, triggerStr);
62 strcpy(triggerStr, "+");
63 trigger = malloc(sizeof(*trigger));
65 perror("malloc() failed");
68 trigger->botid = botid;
69 trigger->trigger = strdup(triggerStr);
70 trigger->next = chan->trigger;
71 chan->trigger = trigger;
72 return trigger->trigger;
75 static USERAUTH_CALLBACK(command_checked_auth) {
76 struct command_check_user_cache *cache = data;
78 if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
83 cache->cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
89 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
90 if(message[0] == '#') {
91 char *chanName = message;
92 message = strstr(message, " ");
96 struct ChanNode *chan2 = getChanByName(chanName);
100 message = strdup(message);
101 int bind_index = get_binds_index(message[0]);
102 char *args = strstr(message, " ");
107 struct cmd_binding *cbind;
108 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
109 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
110 //parse the arguments...
111 char *arga[MAXNUMPARAMS];
116 //skip leading spaces
120 if (argc >= MAXNUMPARAMS)
122 while (*args != ' ' && *args)
127 if(argc != 0 && argv[0][0] == '#') {
128 struct ChanNode *chan2 = getChanByName(argv[0]);
135 if(cbind->parameters) {
136 //userdefined parameters...
137 char *uarga[MAXNUMPARAMS];
138 char params[strlen(cbind->parameters)+1];
139 strcpy(params, cbind->parameters);
140 int uargpos = 0, argi, allargs = 0;
142 char *prev_ppos = params;
143 while((ppos = strstr(ppos, " "))) {
145 if(prev_ppos[0] == '%') {
147 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
149 prev_ppos[strlen(prev_ppos)-1] = '\0';
152 argi = atoi(prev_ppos);
154 if(argi <= argc) continue;
155 uarga[uargpos++] = argv[argi-1];
157 for(;argi < argc; argi++)
158 uarga[uargpos++] = argv[argi-1]
160 } else if(!strcmp(prev_ppos, "c"))
161 uarga[uargpos++] = (chan ? chan->name : NULL);
162 else if(!strcmp(prev_ppos, "n"))
163 uarga[uargpos++] = user->nick;
165 uarga[uargpos++] = prev_ppos;
173 if(argc < cbind->func->paramcount) {
177 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
181 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
183 struct command_check_user_cache *data = malloc(sizeof(*data));
184 char **temp_argv = malloc(argc*sizeof(*temp_argv));
185 if (!data || !temp_argv) {
186 perror("malloc() failed");
189 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
190 data->argv = temp_argv;
192 data->client = client;
195 data->message = message;
197 get_userauth(user, command_checked_auth, data);
200 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
204 cbind->func->func(client, user, chan, argv, argc);
211 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
214 struct ClientSocket *client;
216 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
217 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
218 FD_SET(client->botid, &fds);
219 trigger = get_channel_trigger(client->botid, chan);
220 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
221 handle_command(client, user, chan, message + strlen(trigger));
225 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
226 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
227 FD_SET(client->botid, &fds);
228 trigger = get_channel_trigger(client->botid, chan);
229 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
230 handle_command(client, user, chan, message + strlen(trigger));
236 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
237 struct ClientSocket *client;
238 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
239 if(client->user == target) {
240 handle_command(client, user, NULL, message);
245 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
246 struct cmd_function *cmdfunc;
247 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
248 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
251 cmdfunc = malloc(sizeof(*cmdfunc));
253 perror("malloc() failed");
256 cmdfunc->botid = botid;
257 cmdfunc->name = strdup(name);
258 cmdfunc->func = func;
260 cmdfunc->paramcount = paramcount;
261 cmdfunc->next = cmd_functions;
262 cmd_functions = cmdfunc;
266 int set_trigger_callback(int botid, trigger_callback_t *func) {
267 static struct trigger_callback *cb = NULL;
268 for(cb = trigger_callbacks; cb; cb = cb->next) {
269 if(cb->botid == botid)
273 cb = malloc(sizeof(*cb));
275 perror("malloc() failed");
279 cb->next = trigger_callbacks;
280 trigger_callbacks = cb;
286 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
287 struct trigger_cache *trigger;
288 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
289 if(trigger->botid == botid) {
290 free(trigger->trigger);
291 trigger->trigger = strdup(new_trigger);
298 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
299 int bind_index = get_binds_index(cmd[0]);
300 struct cmd_binding *cbind;
301 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
302 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
305 cbind = malloc(sizeof(*cbind));
307 perror("malloc() failed");
310 cbind->botid = botid;
311 cbind->cmd = strdup(cmd);
313 cbind->parameters = NULL;
315 cbind->next = cmd_binds[bind_index];
316 cmd_binds[bind_index] = cbind;
320 int bind_cmd_to_command(int botid, char *cmd, char *func) {
321 struct cmd_function *cmdfunc;
322 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
323 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
326 if(!cmdfunc) return 0;
327 int bind_index = get_binds_index(cmd[0]);
328 struct cmd_binding *cbind;
329 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
330 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
333 cbind = malloc(sizeof(*cbind));
335 perror("malloc() failed");
338 cbind->botid = botid;
339 cbind->cmd = strdup(cmd);
340 cbind->func = cmdfunc;
341 cbind->next = cmd_binds[bind_index];
342 cbind->parameters = NULL;
344 cmd_binds[bind_index] = cbind;
348 int unbind_cmd(int botid, char *cmd) {
349 int bind_index = get_binds_index(cmd[0]);
350 struct cmd_binding *cbind, *last = NULL;
351 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
352 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
354 last->next = cbind->next;
356 cmd_binds[bind_index] = cbind->next;
358 if(cbind->parameters)
359 free(cbind->parameters);
369 cmd_binds = calloc(27, sizeof(*cmd_binds));
370 bind_chanmsg(got_chanmsg);
371 bind_privmsg(got_privmsg);
376 for(i = 0; i < 27; i++) {
377 struct cmd_binding *cbind, *next;
378 for(cbind = cmd_binds[i]; cbind; cbind = next) {
381 if(cbind->parameters)
382 free(cbind->parameters);
387 struct cmd_function *cmdfunct, *next;
388 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
389 next = cmdfunct->next;
390 free(cmdfunct->name);
393 struct trigger_callback *cb, *next_cb;
394 for(cb = trigger_callbacks; cb; cb = next_cb) {
398 cmd_functions = NULL;
399 trigger_callbacks = NULL;