5 #include "ClientSocket.h"
9 #include "WHOHandler.h"
12 struct trigger_callback {
14 trigger_callback_t *func;
16 struct trigger_callback *next;
19 struct command_check_user_cache {
20 struct ClientSocket *client, *textclient;
21 struct UserNode *user;
22 struct ChanNode *chan;
26 struct cmd_binding *cbind;
29 static struct cmd_binding **cmd_binds;
30 static struct cmd_function *cmd_functions = NULL;
31 static struct trigger_callback *trigger_callbacks = NULL;
32 static struct ClientSocket *tmp_text_client;
34 static const struct default_language_entry msgtab[] = {
35 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
36 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists."},
37 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
38 {"MODCMD_PRIVILEGED", "§b%s§b is a privileged command."},
44 static int get_binds_index(char first_char) {
45 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
46 return tolower(first_char - 'a');
51 struct ClientSocket* get_prefered_bot(int botid) {
52 struct ClientSocket *client;
53 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
54 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
60 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
61 struct trigger_cache *trigger;
62 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
63 if(trigger->botid == botid)
64 return trigger->trigger;
66 struct trigger_callback *cb;
67 for(cb = trigger_callbacks; cb; cb = cb->next) {
68 if(cb->botid == botid)
71 char triggerStr[TRIGGERLEN];
73 cb->func(chan, triggerStr);
75 strcpy(triggerStr, "+");
76 trigger = malloc(sizeof(*trigger));
78 perror("malloc() failed");
81 trigger->botid = botid;
82 trigger->trigger = strdup(triggerStr);
83 trigger->next = chan->trigger;
84 chan->trigger = trigger;
85 return trigger->trigger;
88 static USERAUTH_CALLBACK(command_checked_auth) {
89 struct command_check_user_cache *cache = data;
91 tmp_text_client = cache->textclient;
92 if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
93 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
96 else if((cache->cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
97 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cache->cbind->cmd);
101 cache->cbind->func->func(cache->client, user, cache->chan, cache->argv, cache->argc);
103 free(cache->message);
107 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
108 if(message[0] == '#') {
109 char *chanName = message;
110 message = strstr(message, " ");
114 struct ChanNode *chan2 = getChanByName(chanName);
118 message = strdup(message);
119 int bind_index = get_binds_index(message[0]);
120 char *args = strstr(message, " ");
125 struct cmd_binding *cbind;
126 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
127 if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
129 tmp_text_client = get_prefered_bot(client->botid);
130 //parse the arguments...
131 char *arga[MAXNUMPARAMS];
136 //skip leading spaces
140 if (argc >= MAXNUMPARAMS)
142 while (*args != ' ' && *args)
147 if(argc != 0 && argv[0][0] == '#') {
148 struct ChanNode *chan2 = getChanByName(argv[0]);
155 if(cbind->parameters) {
156 //userdefined parameters...
157 char *uarga[MAXNUMPARAMS];
158 char params[strlen(cbind->parameters)+1];
159 strcpy(params, cbind->parameters);
160 int uargpos = 0, argi, allargs = 0;
162 char *prev_ppos = params;
163 while((ppos = strstr(ppos, " "))) {
165 if(prev_ppos[0] == '%') {
167 if(prev_ppos[strlen(prev_ppos)-1] == '-') {
169 prev_ppos[strlen(prev_ppos)-1] = '\0';
172 argi = atoi(prev_ppos);
174 if(argi <= argc) continue;
175 uarga[uargpos++] = argv[argi-1];
177 for(;argi < argc; argi++)
178 uarga[uargpos++] = argv[argi-1];
180 } else if(!strcmp(prev_ppos, "c"))
181 uarga[uargpos++] = (chan ? chan->name : NULL);
182 else if(!strcmp(prev_ppos, "n"))
183 uarga[uargpos++] = user->nick;
185 uarga[uargpos++] = prev_ppos;
193 if(argc < cbind->func->paramcount) {
194 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
197 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
198 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
201 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
203 struct command_check_user_cache *data = malloc(sizeof(*data));
204 char **temp_argv = malloc(argc*sizeof(*temp_argv));
205 if (!data || !temp_argv) {
206 perror("malloc() failed");
209 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
210 data->argv = temp_argv;
212 data->client = client;
215 data->message = message;
217 data->textclient = tmp_text_client;
218 get_userauth(user, command_checked_auth, data);
221 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
222 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
225 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
226 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
229 cbind->func->func(client, user, chan, argv, argc);
236 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
239 struct ClientSocket *client;
241 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
242 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
243 FD_SET(client->botid, &fds);
244 trigger = get_channel_trigger(client->botid, chan);
245 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
246 handle_command(client, user, chan, message + strlen(trigger));
250 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
251 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
252 FD_SET(client->botid, &fds);
253 trigger = get_channel_trigger(client->botid, chan);
254 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
255 handle_command(client, user, chan, message + strlen(trigger));
261 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
262 struct ClientSocket *client;
263 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
264 if(client->user == target) {
265 handle_command(client, user, NULL, message);
270 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags) {
271 struct cmd_function *cmdfunc;
272 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
273 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
276 cmdfunc = malloc(sizeof(*cmdfunc));
278 perror("malloc() failed");
281 cmdfunc->botid = botid;
282 cmdfunc->name = strdup(name);
283 cmdfunc->func = func;
285 cmdfunc->paramcount = paramcount;
286 cmdfunc->next = cmd_functions;
287 cmd_functions = cmdfunc;
291 int set_trigger_callback(int botid, trigger_callback_t *func) {
292 static struct trigger_callback *cb = NULL;
293 for(cb = trigger_callbacks; cb; cb = cb->next) {
294 if(cb->botid == botid)
298 cb = malloc(sizeof(*cb));
300 perror("malloc() failed");
304 cb->next = trigger_callbacks;
305 trigger_callbacks = cb;
311 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
312 struct trigger_cache *trigger;
313 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
314 if(trigger->botid == botid) {
315 free(trigger->trigger);
316 trigger->trigger = strdup(new_trigger);
323 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
324 int bind_index = get_binds_index(cmd[0]);
325 struct cmd_binding *cbind;
326 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
327 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
330 cbind = malloc(sizeof(*cbind));
332 perror("malloc() failed");
335 cbind->botid = botid;
336 cbind->cmd = strdup(cmd);
338 cbind->parameters = NULL;
341 cbind->next = cmd_binds[bind_index];
342 cmd_binds[bind_index] = cbind;
346 int bind_cmd_to_command(int botid, char *cmd, char *func) {
347 struct cmd_function *cmdfunc;
348 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
349 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
352 if(!cmdfunc) return 0;
353 int bind_index = get_binds_index(cmd[0]);
354 struct cmd_binding *cbind;
355 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
356 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
359 cbind = malloc(sizeof(*cbind));
361 perror("malloc() failed");
364 cbind->botid = botid;
365 cbind->cmd = strdup(cmd);
366 cbind->func = cmdfunc;
367 cbind->next = cmd_binds[bind_index];
368 cbind->parameters = NULL;
371 cmd_binds[bind_index] = cbind;
375 int unbind_cmd(int botid, char *cmd) {
376 int bind_index = get_binds_index(cmd[0]);
377 struct cmd_binding *cbind, *last = NULL;
378 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
379 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
381 last->next = cbind->next;
383 cmd_binds[bind_index] = cbind->next;
385 if(cbind->parameters)
386 free(cbind->parameters);
395 struct ClientSocket *getTextBot() {
396 return tmp_text_client;
400 cmd_binds = calloc(27, sizeof(*cmd_binds));
401 bind_chanmsg(got_chanmsg);
402 bind_privmsg(got_privmsg);
403 register_default_language_table(msgtab);
408 for(i = 0; i < 27; i++) {
409 struct cmd_binding *cbind, *next;
410 for(cbind = cmd_binds[i]; cbind; cbind = next) {
413 if(cbind->parameters)
414 free(cbind->parameters);
419 struct cmd_function *cmdfunct, *next;
420 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
421 next = cmdfunct->next;
422 free(cmdfunct->name);
425 struct trigger_callback *cb, *next_cb;
426 for(cb = trigger_callbacks; cb; cb = next_cb) {
430 cmd_functions = NULL;
431 trigger_callbacks = NULL;
434 void bind_set_parameters(int botid, char *cmd, char *parameters) {
438 void bind_set_gaccess(int botid, char *cmd, int access) {