added .gitignore
[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             //parse the arguments...
85             char *arga[MAXNUMPARAMS];
86             char **argv;
87             int argc = 0;
88             if(args) {
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             }
100             argv = arga;
101             if(argc != 0 && argv[0][0] == '#') {
102                 struct ChanNode *chan2 = getChanByName(argv[0]);
103                 if(chan2) {
104                     argv += 1;
105                     argc -= 1;
106                     chan = chan2;
107                 }
108             }
109             cbind->func->func(client, user, chan, argv, argc);
110             return;
111         }
112     }
113 }
114
115 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
116     fd_set fds;
117     char *trigger;
118     struct ClientSocket *client;
119     FD_ZERO(&fds);
120     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
121         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
122             FD_SET(client->botid, &fds);
123             trigger = get_channel_trigger(client->botid, chan);
124             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
125                 handle_command(client, user, chan, message + strlen(trigger));
126             }
127         }
128     }
129     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
130         if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
131             FD_SET(client->botid, &fds);
132             trigger = get_channel_trigger(client->botid, chan);
133             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
134                 handle_command(client, user, chan, message + strlen(trigger));
135             }
136         }
137     }
138 }
139
140 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
141     struct ClientSocket *client;
142     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
143         if(client->user == target) {
144             handle_command(client, user, NULL, message);
145         }
146     }
147 }
148
149 int register_command(int botid, char *name, cmd_bind_t *func) {
150     struct cmd_function *cmdfunc;
151     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
152         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
153             return 0;
154     }
155     cmdfunc = malloc(sizeof(*cmdfunc));
156     if (!cmdfunc) {
157         perror("malloc() failed");
158         return 0;
159     }
160     cmdfunc->botid = botid;
161     cmdfunc->name = strdup(name);
162     cmdfunc->func = func;
163     cmdfunc->next = cmd_functions;
164     cmd_functions = cmdfunc;
165     return 1;
166 }
167
168 int set_trigger_callback(int botid, trigger_callback_t *func) {
169     static struct trigger_callback *cb = NULL;
170     for(cb = trigger_callbacks; cb; cb = cb->next) {
171         if(cb->botid == botid)
172             break;
173     }
174     if(!cb) {
175         cb = malloc(sizeof(*cb));
176         if (!cb) {
177             perror("malloc() failed");
178             return 0;
179         }
180         cb->botid = botid;
181         cb->next = trigger_callbacks;
182         trigger_callbacks = cb;
183     }
184     cb->func = func;
185     return 1;
186 }
187
188 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
189     struct trigger_cache *trigger;
190     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
191         if(trigger->botid == botid) {
192             free(trigger->trigger);
193             trigger->trigger = strdup(new_trigger);
194             return 1;
195         }
196     }
197     return 0;
198 }
199
200 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
201     int bind_index = get_binds_index(cmd[0]);
202     struct cmd_binding *cbind;
203     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
204         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
205             return 0;
206     }
207     cbind = malloc(sizeof(*cbind));
208     if (!cbind) {
209         perror("malloc() failed");
210         return 0;
211     }
212     cbind->botid = botid;
213     cbind->cmd = strdup(cmd);
214     cbind->func = func;
215     cbind->next = cmd_binds[bind_index];
216     cmd_binds[bind_index] = cbind;
217     return 1;
218 }
219
220 int bind_cmd_to_command(int botid, char *cmd, char *func) {
221     struct cmd_function *cmdfunc;
222     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
223         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
224             break;
225     }
226     if(!cmdfunc) return 0;
227     int bind_index = get_binds_index(cmd[0]);
228     struct cmd_binding *cbind;
229     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
230         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
231             return 0;
232     }
233     cbind = malloc(sizeof(*cbind));
234     if (!cbind) {
235         perror("malloc() failed");
236         return 0;
237     }
238     cbind->botid = botid;
239     cbind->cmd = strdup(cmd);
240     cbind->func = cmdfunc;
241     cbind->next = cmd_binds[bind_index];
242     cmd_binds[bind_index] = cbind;
243     return 1;
244 }
245
246 int unbind_cmd(int botid, char *cmd) {
247     int bind_index = get_binds_index(cmd[0]);
248     struct cmd_binding *cbind, *last = NULL;
249     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
250         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
251             if(last)
252                 last->next = cbind->next;
253             else
254                 cmd_binds[bind_index] = cbind->next;
255             free(cbind->cmd);
256             free(cbind);
257             return 1;
258         } else
259             last = cbind;
260     }
261     return 0;
262 }
263
264 void init_modcmd() {
265     cmd_binds = calloc(27, sizeof(*cmd_binds));
266     bind_chanmsg(got_chanmsg);
267     bind_privmsg(got_privmsg);
268 }
269
270 void free_modcmd() {
271     int i;
272     for(i = 0; i < 27; i++) {
273         struct cmd_binding *cbind, *next;
274         for(cbind = cmd_binds[i]; cbind; cbind = next) {
275             next = cbind->next;
276             free(cbind->cmd);
277             free(cbind);
278         }
279     }
280     free(cmd_binds);
281     struct cmd_function *cmdfunct, *next;
282     for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
283         next = cmdfunct->next;
284         free(cmdfunct->name);
285         free(cmdfunct);
286     }
287     struct trigger_callback *cb, *next_cb;
288     for(cb = trigger_callbacks; cb; cb = next_cb) {
289         next_cb = cb->next;
290         free(next_cb);
291     }
292     cmd_functions = NULL;
293     trigger_callbacks = NULL;
294 }
295