4 #include "ClientSocket.h"
9 struct trigger_callback {
11 trigger_callback_t *func;
13 struct trigger_callback *next;
16 struct command_check_user_cache {
17 struct ClientSocket *client;
18 struct UserNode *user;
19 struct ChanNode *chan;
23 struct cmd_binding *cbind;
26 static struct cmd_binding **cmd_binds;
27 static struct cmd_function *cmd_functions = NULL;
28 static struct trigger_callback *trigger_callbacks = NULL;
30 static int get_binds_index(char first_char) {
31 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
32 return tolower(first_char - 'a');
37 struct ClientSocket* get_prefered_bot(int botid) {
38 struct ClientSocket *client;
39 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
40 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
46 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
47 struct trigger_cache *trigger;
48 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
49 if(trigger->botid == botid)
50 return trigger->trigger;
52 struct trigger_callback *cb;
53 for(cb = trigger_callbacks; cb; cb = cb->next) {
54 if(cb->botid == botid)
57 char triggerStr[TRIGGERLEN];
59 cb->func(chan, triggerStr);
61 strcpy(triggerStr, "+");
62 trigger = malloc(sizeof(*trigger));
64 perror("malloc() failed");
67 trigger->botid = botid;
68 trigger->trigger = strdup(triggerStr);
69 trigger->next = chan->trigger;
70 chan->trigger = trigger;
71 return trigger->trigger;
74 static USERAUTH_CALLBACK(command_checked_auth) {
75 struct command_check_user_cache *cache = data;
77 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
82 cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
88 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
89 if(message[0] == '#') {
90 char *chanName = message;
91 message = strstr(message, " ");
95 struct ChanNode *chan2 = getChanByName(chanName);
99 message = strdup(message);
100 int bind_index = get_binds_index(message[0]);
101 char *args = strstr(message, " ");
106 struct cmd_binding *cbind;
107 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
108 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
109 //parse the arguments...
110 char *arga[MAXNUMPARAMS];
115 //skip leading spaces
119 if (argc >= MAXNUMPARAMS)
121 while (*args != ' ' && *args)
126 if(argc != 0 && argv[0][0] == '#') {
127 struct ChanNode *chan2 = getChanByName(argv[0]);
134 if(cbind->func->parameters) {
135 //userdefined parameters...
136 char *uarga[MAXNUMPARAMS];
137 char params[strlen(cbind->func->parameters)+1];
138 strcpy(params, cbind->func->parameters);
139 int uargpos = 0, argi, allargs = 0;
140 char *ppos = params, *prev_ppos = params;
141 while(ppos = strstr(ppos, " ")) {
143 if(prev_ppos[0] == '%') {
145 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
147 prev_ppos[strlen(prev_ppos)-1] = '\0';
150 if((argi = atoi(prev_ppos)) > 0) {
151 if(argi <= argc) continue;
152 uarga[uargpos++] = argv[argi-1]
154 for(;argi < argc; argi++)
155 uarga[uargpos++] = argv[argi-1]
157 } else if(!strcmp(prev_ppos, "c"))
158 uarga[uargpos++] = (chan ? chan->name : NULL);
159 else if(!strcmp(prev_ppos, "n"))
160 uarga[uargpos++] = user->nick;
162 uarga[uargpos++] = prev_ppos;
171 if(argc < cbind->func->paramcount) {
175 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
179 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
181 struct command_check_user_cache *data = malloc(sizeof(*data));
182 char **temp_argv = malloc(argc*sizeof(*temp_argv));
183 if (!data || !temp_argv) {
184 perror("malloc() failed");
187 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
188 data->argv = temp_argv;
190 data->client = client;
193 data->message = message;
195 get_userauth(user, command_checked_auth, data)
198 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
202 cbind->func->func(client, user, chan, argv, argc);
209 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
212 struct ClientSocket *client;
214 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
215 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
216 FD_SET(client->botid, &fds);
217 trigger = get_channel_trigger(client->botid, chan);
218 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
219 handle_command(client, user, chan, message + strlen(trigger));
223 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
224 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
225 FD_SET(client->botid, &fds);
226 trigger = get_channel_trigger(client->botid, chan);
227 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
228 handle_command(client, user, chan, message + strlen(trigger));
234 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
235 struct ClientSocket *client;
236 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
237 if(client->user == target) {
238 handle_command(client, user, NULL, message);
243 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
244 struct cmd_function *cmdfunc;
245 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
246 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
249 cmdfunc = malloc(sizeof(*cmdfunc));
251 perror("malloc() failed");
254 cmdfunc->botid = botid;
255 cmdfunc->name = strdup(name);
256 cmdfunc->func = func;
258 cmdfunc->paramcount = paramcount;
259 cmdfunc->next = cmd_functions;
260 cmd_functions = cmdfunc;
264 int set_trigger_callback(int botid, trigger_callback_t *func) {
265 static struct trigger_callback *cb = NULL;
266 for(cb = trigger_callbacks; cb; cb = cb->next) {
267 if(cb->botid == botid)
271 cb = malloc(sizeof(*cb));
273 perror("malloc() failed");
277 cb->next = trigger_callbacks;
278 trigger_callbacks = cb;
284 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
285 struct trigger_cache *trigger;
286 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
287 if(trigger->botid == botid) {
288 free(trigger->trigger);
289 trigger->trigger = strdup(new_trigger);
296 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
297 int bind_index = get_binds_index(cmd[0]);
298 struct cmd_binding *cbind;
299 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
300 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
303 cbind = malloc(sizeof(*cbind));
305 perror("malloc() failed");
308 cbind->botid = botid;
309 cbind->cmd = strdup(cmd);
311 cbind->parameters = NULL;
313 cbind->next = cmd_binds[bind_index];
314 cmd_binds[bind_index] = cbind;
318 int bind_cmd_to_command(int botid, char *cmd, char *func) {
319 struct cmd_function *cmdfunc;
320 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
321 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
324 if(!cmdfunc) return 0;
325 int bind_index = get_binds_index(cmd[0]);
326 struct cmd_binding *cbind;
327 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
328 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
331 cbind = malloc(sizeof(*cbind));
333 perror("malloc() failed");
336 cbind->botid = botid;
337 cbind->cmd = strdup(cmd);
338 cbind->func = cmdfunc;
339 cbind->next = cmd_binds[bind_index];
340 cbind->parameters = NULL;
342 cmd_binds[bind_index] = cbind;
346 int unbind_cmd(int botid, char *cmd) {
347 int bind_index = get_binds_index(cmd[0]);
348 struct cmd_binding *cbind, *last = NULL;
349 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
350 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
352 last->next = cbind->next;
354 cmd_binds[bind_index] = cbind->next;
356 if(cbind->parameters)
357 free(cbind->parameters);
367 cmd_binds = calloc(27, sizeof(*cmd_binds));
368 bind_chanmsg(got_chanmsg);
369 bind_privmsg(got_privmsg);
374 for(i = 0; i < 27; i++) {
375 struct cmd_binding *cbind, *next;
376 for(cbind = cmd_binds[i]; cbind; cbind = next) {
379 if(cbind->parameters)
380 free(cbind->parameters);
385 struct cmd_function *cmdfunct, *next;
386 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
387 next = cmdfunct->next;
388 free(cmdfunct->name);
391 struct trigger_callback *cb, *next_cb;
392 for(cb = trigger_callbacks; cb; cb = next_cb) {
396 cmd_functions = NULL;
397 trigger_callbacks = NULL;