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