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