4 #include "ClientSocket.h"
8 #include "WHOHandler.h"
11 struct trigger_callback {
13 trigger_callback_t *func;
15 struct trigger_callback *next;
18 struct command_check_user_cache {
19 struct ClientSocket *client;
20 struct UserNode *user;
21 struct ChanNode *chan;
25 struct cmd_binding *cbind;
28 static struct cmd_binding **cmd_binds;
29 static struct cmd_function *cmd_functions = NULL;
30 static struct trigger_callback *trigger_callbacks = NULL;
32 static const struct default_language_entry msgtab[] = {
37 static int get_binds_index(char first_char) {
38 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
39 return tolower(first_char - 'a');
44 struct ClientSocket* get_prefered_bot(int botid) {
45 struct ClientSocket *client;
46 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
47 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
53 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
54 struct trigger_cache *trigger;
55 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
56 if(trigger->botid == botid)
57 return trigger->trigger;
59 struct trigger_callback *cb;
60 for(cb = trigger_callbacks; cb; cb = cb->next) {
61 if(cb->botid == botid)
64 char triggerStr[TRIGGERLEN];
66 cb->func(chan, triggerStr);
68 strcpy(triggerStr, "+");
69 trigger = malloc(sizeof(*trigger));
71 perror("malloc() failed");
74 trigger->botid = botid;
75 trigger->trigger = strdup(triggerStr);
76 trigger->next = chan->trigger;
77 chan->trigger = trigger;
78 return trigger->trigger;
81 static USERAUTH_CALLBACK(command_checked_auth) {
82 struct command_check_user_cache *cache = data;
84 if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
89 cache->cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
95 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
96 if(message[0] == '#') {
97 char *chanName = message;
98 message = strstr(message, " ");
102 struct ChanNode *chan2 = getChanByName(chanName);
106 message = strdup(message);
107 int bind_index = get_binds_index(message[0]);
108 char *args = strstr(message, " ");
113 struct cmd_binding *cbind;
114 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
115 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
116 //parse the arguments...
117 char *arga[MAXNUMPARAMS];
122 //skip leading spaces
126 if (argc >= MAXNUMPARAMS)
128 while (*args != ' ' && *args)
133 if(argc != 0 && argv[0][0] == '#') {
134 struct ChanNode *chan2 = getChanByName(argv[0]);
141 if(cbind->parameters) {
142 //userdefined parameters...
143 char *uarga[MAXNUMPARAMS];
144 char params[strlen(cbind->parameters)+1];
145 strcpy(params, cbind->parameters);
146 int uargpos = 0, argi, allargs = 0;
148 char *prev_ppos = params;
149 while((ppos = strstr(ppos, " "))) {
151 if(prev_ppos[0] == '%') {
153 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
155 prev_ppos[strlen(prev_ppos)-1] = '\0';
158 argi = atoi(prev_ppos);
160 if(argi <= argc) continue;
161 uarga[uargpos++] = argv[argi-1];
163 for(;argi < argc; argi++)
164 uarga[uargpos++] = argv[argi-1];
166 } else if(!strcmp(prev_ppos, "c"))
167 uarga[uargpos++] = (chan ? chan->name : NULL);
168 else if(!strcmp(prev_ppos, "n"))
169 uarga[uargpos++] = user->nick;
171 uarga[uargpos++] = prev_ppos;
179 if(argc < cbind->func->paramcount) {
183 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
187 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
189 struct command_check_user_cache *data = malloc(sizeof(*data));
190 char **temp_argv = malloc(argc*sizeof(*temp_argv));
191 if (!data || !temp_argv) {
192 perror("malloc() failed");
195 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
196 data->argv = temp_argv;
198 data->client = client;
201 data->message = message;
203 get_userauth(user, command_checked_auth, data);
206 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
210 cbind->func->func(client, user, chan, argv, argc);
217 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
220 struct ClientSocket *client;
222 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
223 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
224 FD_SET(client->botid, &fds);
225 trigger = get_channel_trigger(client->botid, chan);
226 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
227 handle_command(client, user, chan, message + strlen(trigger));
231 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
232 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
233 FD_SET(client->botid, &fds);
234 trigger = get_channel_trigger(client->botid, chan);
235 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
236 handle_command(client, user, chan, message + strlen(trigger));
242 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
243 struct ClientSocket *client;
244 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
245 if(client->user == target) {
246 handle_command(client, user, NULL, message);
251 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
252 struct cmd_function *cmdfunc;
253 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
254 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
257 cmdfunc = malloc(sizeof(*cmdfunc));
259 perror("malloc() failed");
262 cmdfunc->botid = botid;
263 cmdfunc->name = strdup(name);
264 cmdfunc->func = func;
266 cmdfunc->paramcount = paramcount;
267 cmdfunc->next = cmd_functions;
268 cmd_functions = cmdfunc;
272 int set_trigger_callback(int botid, trigger_callback_t *func) {
273 static struct trigger_callback *cb = NULL;
274 for(cb = trigger_callbacks; cb; cb = cb->next) {
275 if(cb->botid == botid)
279 cb = malloc(sizeof(*cb));
281 perror("malloc() failed");
285 cb->next = trigger_callbacks;
286 trigger_callbacks = cb;
292 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
293 struct trigger_cache *trigger;
294 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
295 if(trigger->botid == botid) {
296 free(trigger->trigger);
297 trigger->trigger = strdup(new_trigger);
304 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
305 int bind_index = get_binds_index(cmd[0]);
306 struct cmd_binding *cbind;
307 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
308 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
311 cbind = malloc(sizeof(*cbind));
313 perror("malloc() failed");
316 cbind->botid = botid;
317 cbind->cmd = strdup(cmd);
319 cbind->parameters = NULL;
321 cbind->next = cmd_binds[bind_index];
322 cmd_binds[bind_index] = cbind;
326 int bind_cmd_to_command(int botid, char *cmd, char *func) {
327 struct cmd_function *cmdfunc;
328 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
329 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
332 if(!cmdfunc) return 0;
333 int bind_index = get_binds_index(cmd[0]);
334 struct cmd_binding *cbind;
335 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
336 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
339 cbind = malloc(sizeof(*cbind));
341 perror("malloc() failed");
344 cbind->botid = botid;
345 cbind->cmd = strdup(cmd);
346 cbind->func = cmdfunc;
347 cbind->next = cmd_binds[bind_index];
348 cbind->parameters = NULL;
350 cmd_binds[bind_index] = cbind;
354 int unbind_cmd(int botid, char *cmd) {
355 int bind_index = get_binds_index(cmd[0]);
356 struct cmd_binding *cbind, *last = NULL;
357 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
358 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
360 last->next = cbind->next;
362 cmd_binds[bind_index] = cbind->next;
364 if(cbind->parameters)
365 free(cbind->parameters);
375 cmd_binds = calloc(27, sizeof(*cmd_binds));
376 bind_chanmsg(got_chanmsg);
377 bind_privmsg(got_privmsg);
378 register_default_language_table(msgtab);
383 for(i = 0; i < 27; i++) {
384 struct cmd_binding *cbind, *next;
385 for(cbind = cmd_binds[i]; cbind; cbind = next) {
388 if(cbind->parameters)
389 free(cbind->parameters);
394 struct cmd_function *cmdfunct, *next;
395 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
396 next = cmdfunct->next;
397 free(cmdfunct->name);
400 struct trigger_callback *cb, *next_cb;
401 for(cb = trigger_callbacks; cb; cb = next_cb) {
405 cmd_functions = NULL;
406 trigger_callbacks = NULL;