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