added .gitignore
[NeonServV5.git] / modcmd.c
index 180c9689ccf4bf523ae26b7f69d114d78626c0b6..6db797b257733afaa03b4061cd45054f7c7b06f4 100644 (file)
--- a/modcmd.c
+++ b/modcmd.c
@@ -11,7 +11,7 @@ struct trigger_callback {
     trigger_callback_t *func;
     
     struct trigger_callback *next;
-}
+};
 
 static struct cmd_binding **cmd_binds;
 static struct cmd_function *cmd_functions = NULL;
@@ -24,10 +24,10 @@ static int get_binds_index(char first_char) {
     return 26;
 }
 
-struct* ClientSocket get_prefered_bot(int botid) {
-    struct ClientSocket *client, *source = NULL;
+struct ClientSocket* get_prefered_bot(int botid) {
+    struct ClientSocket *client;
     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
-        if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERED))
+        if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
             return client;
     }
     return NULL;
@@ -62,17 +62,53 @@ static char* get_channel_trigger(int botid, struct ChanNode *chan) {
 }
 
 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
-    if(!strcmp(message, "users")) {
-        struct ChanUser *chanuser;
-        putsock(client, "PRIVMSG %s :[BOT JOIN] Users on this Channel:", chan->name);
-        for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
-            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);
-        }
+    if(message[0] == '#') {
+        char *chanName = message;
+        message = strstr(message, " ");
+        if(!message) return;
+        *message = '\0';
+        message++;
+        struct ChanNode *chan2 = getChanByName(chanName);
+        if(chan2)
+            chan = chan2;
+    }
+    int bind_index = get_binds_index(message[0]);
+    char *args = strstr(message, " ");
+    if(args) {
+        *args = '\0';
+        args++;
     }
-    if(!strcmp(message, "modes")) {
-        char modeBuf[MAXLEN];
-        getModeString(chan, modeBuf);
-        putsock(client, "PRIVMSG %s :Modes: %s", chan->name, modeBuf);
+    struct cmd_binding *cbind;
+    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+        if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
+            //parse the arguments...
+            char *arga[MAXNUMPARAMS];
+            char **argv;
+            int argc = 0;
+            if(args) {
+                while(*args) {
+                    //skip leading spaces
+                    while (*args == ' ')
+                        *args++ = 0;
+                    arga[argc++] = args;
+                    if (argc >= MAXNUMPARAMS)
+                        break;
+                    while (*args != ' ' && *args)
+                        args++;
+                }
+            }
+            argv = arga;
+            if(argc != 0 && argv[0][0] == '#') {
+                struct ChanNode *chan2 = getChanByName(argv[0]);
+                if(chan2) {
+                    argv += 1;
+                    argc -= 1;
+                    chan = chan2;
+                }
+            }
+            cbind->func->func(client, user, chan, argv, argc);
+            return;
+        }
     }
 }
 
@@ -82,7 +118,7 @@ static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *mess
     struct ClientSocket *client;
     FD_ZERO(&fds);
     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
-        if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERED) && !FD_ISSET(client->botid, &fds)) {
+        if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
             FD_SET(client->botid, &fds);
             trigger = get_channel_trigger(client->botid, chan);
             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
@@ -163,21 +199,21 @@ int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
 
 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
     int bind_index = get_binds_index(cmd[0]);
-    struct cmd_binding *bind;
-    for(bind = cmd_binds[bind_index]; bind; bind = bind->next) {
-        if(bind->botid == botid && strcmp(bind->cmd, cmd) == 0)
+    struct cmd_binding *cbind;
+    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+        if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
             return 0;
     }
-    bind = malloc(sizeof(*bind));
-    if (!bind) {
+    cbind = malloc(sizeof(*cbind));
+    if (!cbind) {
         perror("malloc() failed");
         return 0;
     }
-    bind->botid = botid;
-    bind->cmd = strdup(cmd);
-    bind->func = func;
-    bind->next = cmd_binds[bind_index];
-    cmd_binds[bind_index] = bind;
+    cbind->botid = botid;
+    cbind->cmd = strdup(cmd);
+    cbind->func = func;
+    cbind->next = cmd_binds[bind_index];
+    cmd_binds[bind_index] = cbind;
     return 1;
 }
 
@@ -189,38 +225,38 @@ int bind_cmd_to_command(int botid, char *cmd, char *func) {
     }
     if(!cmdfunc) return 0;
     int bind_index = get_binds_index(cmd[0]);
-    struct cmd_binding *bind;
-    for(bind = cmd_binds[bind_index]; bind; bind = bind->next) {
-        if(bind->botid == botid && strcmp(bind->cmd, cmd) == 0)
+    struct cmd_binding *cbind;
+    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+        if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
             return 0;
     }
-    bind = malloc(sizeof(*bind));
-    if (!bind) {
+    cbind = malloc(sizeof(*cbind));
+    if (!cbind) {
         perror("malloc() failed");
         return 0;
     }
-    bind->botid = botid;
-    bind->cmd = strdup(cmd);
-    bind->func = cmdfunc;
-    bind->next = cmd_binds[bind_index];
-    cmd_binds[bind_index] = bind;
+    cbind->botid = botid;
+    cbind->cmd = strdup(cmd);
+    cbind->func = cmdfunc;
+    cbind->next = cmd_binds[bind_index];
+    cmd_binds[bind_index] = cbind;
     return 1;
 }
 
 int unbind_cmd(int botid, char *cmd) {
     int bind_index = get_binds_index(cmd[0]);
-    struct cmd_binding *bind, *last = NULL;
-    for(bind = cmd_binds[bind_index]; bind; bind = bind->next) {
-        if(bind->botid == botid && strcmp(bind->cmd, cmd) == 0) {
+    struct cmd_binding *cbind, *last = NULL;
+    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+        if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
             if(last)
-                last->next = bind->next;
+                last->next = cbind->next;
             else
-                cmd_binds[bind_index] = bind->next;
-            free(bind->cmd);
-            free(bind);
+                cmd_binds[bind_index] = cbind->next;
+            free(cbind->cmd);
+            free(cbind);
             return 1;
         } else
-            last = bind;
+            last = cbind;
     }
     return 0;
 }
@@ -231,3 +267,29 @@ void init_modcmd() {
     bind_privmsg(got_privmsg);
 }
 
+void free_modcmd() {
+    int i;
+    for(i = 0; i < 27; i++) {
+        struct cmd_binding *cbind, *next;
+        for(cbind = cmd_binds[i]; cbind; cbind = next) {
+            next = cbind->next;
+            free(cbind->cmd);
+            free(cbind);
+        }
+    }
+    free(cmd_binds);
+    struct cmd_function *cmdfunct, *next;
+    for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
+        next = cmdfunct->next;
+        free(cmdfunct->name);
+        free(cmdfunct);
+    }
+    struct trigger_callback *cb, *next_cb;
+    for(cb = trigger_callbacks; cb; cb = next_cb) {
+        next_cb = cb->next;
+        free(next_cb);
+    }
+    cmd_functions = NULL;
+    trigger_callbacks = NULL;
+}
+