added free functions to free everything (maybe a restart function later?)
authorpk910 <philipp@zoelle1.de>
Fri, 12 Aug 2011 23:47:45 +0000 (01:47 +0200)
committerpk910 <philipp@zoelle1.de>
Fri, 12 Aug 2011 23:47:45 +0000 (01:47 +0200)
18 files changed:
ChanNode.c
ChanNode.h
ClientSocket.c
ClientSocket.h
IRCEvents.c
IRCEvents.h
IRCParser.c
IRCParser.h
Makefile
UserNode.c
UserNode.h
WHOHandler.c
WHOHandler.h
bots.c [new file with mode: 0644]
bots.h [new file with mode: 0644]
main.c
modcmd.c
modcmd.h

index b8ff9f8e223ad59675a29adbaa0580678e87af05..47e06635179c723ebd9a4f137f1e7924b0061f67 100644 (file)
@@ -77,6 +77,24 @@ void init_ChanNode() {
     }
 }
 
+void free_ChanNode() {
+    //kamikaze free all channels and chanusers
+    int i;
+    struct ChanNode *chan, *next;
+    struct ChanUser *chanuser, *next_chanuser;
+    for(i = 0; i < 47; i++) {
+        for(chan = chanList[i]; chan; chan = next) {
+            next = chan->next;
+            for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
+                next_chanuser = getChannelUsers(chan, chanuser);
+                free(chanuser);
+            }
+            freeChanNode(chan);
+        }
+    }
+    free(chanList);
+}
+
 int is_valid_chan(const char *name) {
     unsigned int ii;
     if (*name !='#')
index ea949fdf4b4c39e8308d68a09c07f6b75ab2781e..b62beff35fc8e1fa165bd05b4d6dbb5f4b59eee6 100644 (file)
@@ -23,6 +23,7 @@ struct ChanNode {
 };
 
 void init_ChanNode();
+void free_ChanNode();
 int is_valid_chan(const char *name);
 struct ChanNode* getChanByName(const char *name);
 struct ChanNode* addChannel(const char *chan);
index fb23f37cac709523cc35d5b887f21c10d9ccf530..25d697f8e80a6b1b7ad52ab4840076121c3aaa96 100644 (file)
@@ -12,7 +12,7 @@ struct socket_list {
 static struct socket_list *sockets = NULL;
 static char buffer[BUF_SIZ];
 
-static void init() {
+static void init_sockets() {
     sockets = malloc(sizeof(*sockets));
     if (!sockets)
     {
@@ -24,7 +24,7 @@ static void init() {
 }
 
 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
-    if(sockets == NULL) init();
+    if(sockets == NULL) init_sockets();
     struct ClientSocket *client = malloc(sizeof(*client));
     if (!client)
     {
@@ -201,3 +201,18 @@ struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
     }
     return NULL;
 }
+
+void free_sockets() {
+    if(!sockets) return;
+    struct ClientSocket *client, *next;
+    for (client = sockets->data; client; client = next) {
+        next = client->next;
+        if((client->flags & SOCKET_FLAG_CONNECTED))
+            close(client->sock);
+        free(client->host);
+        free(client->pass);
+        free(client);
+    }
+    free(sockets);
+    sockets = NULL;
+}
index de08e8c605e504c973b908326b10effcff995471..5b145fb4336a340bd67aa6fb85d942ab4adfc869 100644 (file)
@@ -35,5 +35,6 @@ int write_socket(struct ClientSocket *client, char* msg, int len);
 void socket_loop(int timeout_seconds);
 void putsock(struct ClientSocket *client, const char *text, ...) PRINTF_LIKE(2, 3);
 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot);
+void free_sockets();
 
 #endif
\ No newline at end of file
index c0b7eacea9fb9c080a8d7331a99d5a178d130209..51e0d326563a2db02cc68415976b7e5f1a474ea2 100644 (file)
@@ -33,6 +33,18 @@ void init_bind() {
     binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
 }
 
+void free_bind() {
+    struct binding *cbind, *next;
+    int i;
+    for(i = 0; i < TOTAL_BIND_TYPES; i++) {
+        for(cbind = binds[i]; cbind; cbind = next) {
+            next = cbind->next;
+            free(cbind);
+        }
+    }
+    free(binds);
+}
+
 static int is_bound(unsigned char type, void *func) {
     struct binding *cbind;
     for(cbind = binds[type]; cbind; cbind = cbind->next) {
index 424f3c39b5149d217e4fbefbe6d16b121dbce19b..cb017740e830b61c7cb8f7abf72cbb41cd8ab69e 100644 (file)
@@ -9,6 +9,7 @@ struct ChanUser;
 struct ClientSocket;
 
 void init_bind();
+void free_bind();
 
 typedef void join_func_t(struct ChanUser *chanuser);
 int bind_join(join_func_t *func);
index b13f5eb80a16afcde32daea5f62e98dc81da4702..13b76e4e2125cb88434703de8a01f9b7faa5116f 100644 (file)
@@ -338,7 +338,7 @@ static IRC_CMD(raw_mode) {
     return 1;
 }
 
-void parser_init() {
+void init_parser() {
     //all the raws we receive...
     register_irc_function("001", raw_001);
     register_irc_function("324", raw_324);
@@ -356,3 +356,12 @@ void parser_init() {
     register_irc_function("PING", raw_ping);
     register_irc_function("PRIVMSG", raw_privmsg);
 }
+
+void free_parser() {
+    struct irc_cmd *cmd, *next;
+    for(cmd = irc_commands; cmd; cmd = next) {
+        next = cmd->next;
+        free(cmd);
+    }
+}
+
index c4d2801e02a741149d368b012ff77f47e6cf96f1..1425f57dc86678b8b9bb0852a3b8ac7e34771088 100644 (file)
@@ -17,6 +17,7 @@ struct irc_cmd {
 
 int parse_lines(struct ClientSocket *client, char *lines, int len);
 void bot_disconnect(struct ClientSocket *client);
-void parser_init();
+void init_parser();
+void free_parser();
 
 #endif
\ No newline at end of file
index 8f1464f761ec5f1394b8df650f7f47a2eae49719..41831f93602e329b295d4e0bfd196bc82cdec4c5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ all:
        gcc -g -O2 -I. -c ChanUser.c -o ChanUser.o ${CFLAGS}
        gcc -g -O2 -I. -c WHOHandler.c -o WHOHandler.o ${CFLAGS}
        gcc -g -O2 -I. -c modcmd.c -o modcmd.o ${CFLAGS}
+    gcc -g -O2 -I. -c bots.c -o bots.o ${CFLAGS}
 
 install:
        gcc -g -O0 -I. -o neonserv *.o ${CFLAGS}
index 465fd2e04bf6d2c43a221cebe6ab25c9d58ffbed..3b18cbd0e1c55d7ef254790b2576ce3a4257d166 100644 (file)
@@ -7,6 +7,20 @@ void init_UserNode() {
     userList = calloc(VALID_NICK_CHARS_FIRST_LEN+1, sizeof(*userList));
 }
 
+void free_UserNode() {
+    //kamikaze free all users
+    //chanusers will be destroyed in free_ChanNode()
+    int i;
+    struct UserNode *user, *next;
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+        for(user = userList[i]; user; user = next) {
+            next = user->next;
+            free(user);
+        }
+    }
+    free(userList);
+}
+
 int is_valid_nick(const char *nick) {
     unsigned int i;
     //first char must be one of: a-zA-Z{|}~[\]^_`
index 49ae8e16edbc331012e5bbdfd7f0380bf5d9e668..c96f9a128d7bdd77715ef98bd8e43864784086d4 100644 (file)
@@ -25,6 +25,7 @@ struct UserNode {
 };
 
 void init_UserNode();
+void free_UserNode();
 int is_valid_nick(const char *nick);
 struct UserNode* getUserByNick(const char *nick);
 struct UserNode* getUserByMask(const char *mask);
index 09f49546ca1354d5d26e51da409907f8abcacd60..b49933ef15d14b3190fcfdbb0f736c0a6c706f85 100644 (file)
@@ -132,3 +132,13 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int
     }
     free(entry);
 }
+
+void free_whoqueue() {
+    struct WHOQueueEntry *entry, *next;
+    for(entry = first_entry; entry; entry = next) {
+        next = entry->next
+        free(entry);
+    }
+    first_entry = NULL;
+    last_entry = NULL;
+}
index 25e134e21219dc79e7b0ad612132e3863ae8913c..aa04f9c6d8588d5e15b822523ebdfefd0240948c 100644 (file)
@@ -12,5 +12,6 @@ typedef USERLIST_CALLBACK(userlist_callback_t);
 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data);
+void free_whoqueue();
 
 #endif
\ No newline at end of file
diff --git a/bots.c b/bots.c
new file mode 100644 (file)
index 0000000..8a95cd3
--- /dev/null
+++ b/bots.c
@@ -0,0 +1,10 @@
+
+#include "bots.h"
+
+void init_bots() {
+
+}
+
+void free_bots() {
+
+}
diff --git a/bots.h b/bots.h
new file mode 100644 (file)
index 0000000..38371e4
--- /dev/null
+++ b/bots.h
@@ -0,0 +1,9 @@
+#ifndef _bots_h
+#define _bots_h
+
+#include "main.h"
+
+void init_bots();
+void free_bots();
+
+#endif
\ No newline at end of file
diff --git a/main.c b/main.c
index 2a9a4d9bc5aad3d893c0cacbfb0cb918307d088a..58609a3ce906bcd315c21b9c785c2a8cf514d021 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,6 +6,8 @@
 #include "IRCEvents.h"
 #include "IRCParser.h"
 #include "modcmd.h"
+#include "WHOHandler.h"
+#include "bots.h"
 
 void just_test_it() {
     struct UserNode *user;
@@ -27,13 +29,25 @@ void just_test_it() {
     connect_socket(client);
 }
 
+void cleanup() {
+    free_sockets();
+    free_parser();
+    free_UserNode();
+    free_ChanNode();
+    free_bind();
+    free_modcmd();
+    free_whoqueue();
+    free_bots();
+}
+
 int main(void)
 {
-    parser_init();
+    init_parser();
     init_UserNode();
     init_ChanNode();
     init_bind();
        init_modcmd();
+    init_bots();
     just_test_it();
     
     time_t socket_wait;
index 5efe968e38a7ec832f40101f0e2adaf8137620b1..ead1efd8029735ddbad468e3f65d5b8e3e2efaa6 100644 (file)
--- a/modcmd.c
+++ b/modcmd.c
@@ -62,6 +62,52 @@ 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(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++;
+    }
+    struct cmd_binding *cbind;
+    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+        if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
+            struct cmd_function *cmdfunc = cbind->func;
+            //parse the arguments...
+            char *argv[MAXNUMPARAMS];
+            int argc = 0;
+            while(*args) {
+                //skip leading spaces
+                while (*args == ' ')
+                    *args++ = 0;
+                argv[argc++] = args;
+                if (argc >= MAXNUMPARAMS)
+                    break;
+                while (*args != ' ' && *args)
+                    args++;
+            }
+            if(argc != 0 && args[0][0] == '#') {
+                struct ChanNode *chan2 = getChanByName(args[0]);
+                if(chan2) {
+                    argv += 1;
+                    argc -= 1;
+                    chan = chan2;
+                }
+            }
+            cmdfunc->func(client, user, chan, argv, argc);
+            return;
+        }
+    }
+    
     if(!strcmp(message, "users")) {
         struct ChanUser *chanuser;
         putsock(client, "PRIVMSG %s :[BOT JOIN] Users on this Channel:", chan->name);
@@ -231,3 +277,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;
+}
+
index 8dc866be1a03ac2e57bca0a3c8d7040d34a57be4..c880b5848fdef6285e3d83020c6295814acf0971 100644 (file)
--- a/modcmd.h
+++ b/modcmd.h
@@ -33,6 +33,7 @@ struct trigger_cache {
 };
 
 void init_modcmd();
+void free_modcmd();
 struct ClientSocket* get_prefered_bot(int botid);
 int register_command(int botid, char *name, cmd_bind_t *func);
 int set_trigger_callback(int botid, trigger_callback_t *func);