added bot_NeonServ.c file
[NeonServV5.git] / modcmd.c
1
2 #include "modcmd.h"
3 #include "IRCEvents.h"
4 #include "ClientSocket.h"
5 #include "UserNode.h"
6 #include "ChanNode.h"
7 #include "ChanUser.h"
8
9 struct trigger_callback {
10     int botid;
11     trigger_callback_t *func;
12     
13     struct trigger_callback *next;
14 };
15
16 static struct cmd_binding **cmd_binds;
17 static struct cmd_function *cmd_functions = NULL;
18 static struct trigger_callback *trigger_callbacks = NULL;
19
20 static int get_binds_index(char first_char) {
21     if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
22         return tolower(first_char - 'a');
23     }
24     return 26;
25 }
26
27 struct ClientSocket* get_prefered_bot(int botid) {
28     struct ClientSocket *client;
29     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
30         if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
31             return client;
32     }
33     return NULL;
34 }
35
36 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
37     struct trigger_cache *trigger;
38     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
39         if(trigger->botid == botid)
40             return trigger->trigger;
41     }
42     struct trigger_callback *cb;
43     for(cb = trigger_callbacks; cb; cb = cb->next) {
44         if(cb->botid == botid)
45             break;
46     }
47     char triggerStr[TRIGGERLEN];
48     if(cb)
49         cb->func(chan, triggerStr);
50     else
51         strcpy(triggerStr, "+");
52     trigger = malloc(sizeof(*trigger));
53     if (!trigger) {
54         perror("malloc() failed");
55         return 0;
56     }
57     trigger->botid = botid;
58     trigger->trigger = strdup(triggerStr);
59     trigger->next = chan->trigger;
60     chan->trigger = trigger;
61     return trigger->trigger;
62 }
63
64 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
65     if(message[0] == '#') {
66         char *chanName = message;
67         message = strstr(message, " ");
68         if(!message) return;
69         *message = '\0';
70         message++;
71         struct ChanNode *chan2 = getChanByName(chanName);
72         if(chan2)
73             chan = chan2;
74     }
75     int bind_index = get_binds_index(message[0]);
76     char *args = strstr(message, " ");
77     if(args) {
78         *args = '\0';
79         args++;
80     }
81     struct cmd_binding *cbind;
82     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
83         if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
84             struct cmd_function *cmdfunc = cbind->func;
85             //parse the arguments...
86             char *arga[MAXNUMPARAMS];
87             char **argv;
88             int argc = 0;
89             while(*args) {
90                 //skip leading spaces
91                 while (*args == ' ')
92                     *args++ = 0;
93                 arga[argc++] = args;
94                 if (argc >= MAXNUMPARAMS)
95                     break;
96                 while (*args != ' ' && *args)
97                     args++;
98             }
99             argv = arga;
100             if(argc != 0 && argv[0][0] == '#') {
101                 struct ChanNode *chan2 = getChanByName(argv[0]);
102                 if(chan2) {
103                     argv += 1;
104                     argc -= 1;
105                     chan = chan2;
106                 }
107             }
108             cmdfunc->func(client, user, chan, argv, argc);
109             return;
110         }
111     }
112 }
113
114 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
115     fd_set fds;
116     char *trigger;
117     struct ClientSocket *client;
118     FD_ZERO(&fds);
119     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
120         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
121             FD_SET(client->botid, &fds);
122             trigger = get_channel_trigger(client->botid, chan);
123             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
124                 handle_command(client, user, chan, message + strlen(trigger));
125             }
126         }
127     }
128     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
129         if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
130             FD_SET(client->botid, &fds);
131             trigger = get_channel_trigger(client->botid, chan);
132             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
133                 handle_command(client, user, chan, message + strlen(trigger));
134             }
135         }
136     }
137 }
138
139 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
140     struct ClientSocket *client;
141     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
142         if(client->user == target) {
143             handle_command(client, user, NULL, message);
144         }
145     }
146 }
147
148 int register_command(int botid, char *name, cmd_bind_t *func) {
149     struct cmd_function *cmdfunc;
150     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
151         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
152             return 0;
153     }
154     cmdfunc = malloc(sizeof(*cmdfunc));
155     if (!cmdfunc) {
156         perror("malloc() failed");
157         return 0;
158     }
159     cmdfunc->botid = botid;
160     cmdfunc->name = strdup(name);
161     cmdfunc->func = func;
162     cmdfunc->next = cmd_functions;
163     cmd_functions = cmdfunc;
164     return 1;
165 }
166
167 int set_trigger_callback(int botid, trigger_callback_t *func) {
168     static struct trigger_callback *cb = NULL;
169     for(cb = trigger_callbacks; cb; cb = cb->next) {
170         if(cb->botid == botid)
171             break;
172     }
173     if(!cb) {
174         cb = malloc(sizeof(*cb));
175         if (!cb) {
176             perror("malloc() failed");
177             return 0;
178         }
179         cb->botid = botid;
180         cb->next = trigger_callbacks;
181         trigger_callbacks = cb;
182     }
183     cb->func = func;
184     return 1;
185 }
186
187 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
188     struct trigger_cache *trigger;
189     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
190         if(trigger->botid == botid) {
191             free(trigger->trigger);
192             trigger->trigger = strdup(new_trigger);
193             return 1;
194         }
195     }
196     return 0;
197 }
198
199 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
200     int bind_index = get_binds_index(cmd[0]);
201     struct cmd_binding *cbind;
202     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
203         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
204             return 0;
205     }
206     cbind = malloc(sizeof(*cbind));
207     if (!cbind) {
208         perror("malloc() failed");
209         return 0;
210     }
211     cbind->botid = botid;
212     cbind->cmd = strdup(cmd);
213     cbind->func = func;
214     cbind->next = cmd_binds[bind_index];
215     cmd_binds[bind_index] = cbind;
216     return 1;
217 }
218
219 int bind_cmd_to_command(int botid, char *cmd, char *func) {
220     struct cmd_function *cmdfunc;
221     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
222         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
223             break;
224     }
225     if(!cmdfunc) return 0;
226     int bind_index = get_binds_index(cmd[0]);
227     struct cmd_binding *cbind;
228     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
229         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
230             return 0;
231     }
232     cbind = malloc(sizeof(*cbind));
233     if (!cbind) {
234         perror("malloc() failed");
235         return 0;
236     }
237     cbind->botid = botid;
238     cbind->cmd = strdup(cmd);
239     cbind->func = cmdfunc;
240     cbind->next = cmd_binds[bind_index];
241     cmd_binds[bind_index] = cbind;
242     return 1;
243 }
244
245 int unbind_cmd(int botid, char *cmd) {
246     int bind_index = get_binds_index(cmd[0]);
247     struct cmd_binding *cbind, *last = NULL;
248     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
249         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
250             if(last)
251                 last->next = cbind->next;
252             else
253                 cmd_binds[bind_index] = cbind->next;
254             free(cbind->cmd);
255             free(cbind);
256             return 1;
257         } else
258             last = cbind;
259     }
260     return 0;
261 }
262
263 void init_modcmd() {
264     cmd_binds = calloc(27, sizeof(*cmd_binds));
265     bind_chanmsg(got_chanmsg);
266     bind_privmsg(got_privmsg);
267 }
268
269 void free_modcmd() {
270     int i;
271     for(i = 0; i < 27; i++) {
272         struct cmd_binding *cbind, *next;
273         for(cbind = cmd_binds[i]; cbind; cbind = next) {
274             next = cbind->next;
275             free(cbind->cmd);
276             free(cbind);
277         }
278     }
279     free(cmd_binds);
280     struct cmd_function *cmdfunct, *next;
281     for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
282         next = cmdfunct->next;
283         free(cmdfunct->name);
284         free(cmdfunct);
285     }
286     struct trigger_callback *cb, *next_cb;
287     for(cb = trigger_callbacks; cb; cb = next_cb) {
288         next_cb = cb->next;
289         free(next_cb);
290     }
291     cmd_functions = NULL;
292     trigger_callbacks = NULL;
293 }
294