From f3fba85a013cd814b49715965cb654978faa5946 Mon Sep 17 00:00:00 2001 From: pk910 Date: Sat, 13 Aug 2011 01:47:45 +0200 Subject: [PATCH] added free functions to free everything (maybe a restart function later?) --- ChanNode.c | 18 +++++++++++++ ChanNode.h | 1 + ClientSocket.c | 19 +++++++++++-- ClientSocket.h | 1 + IRCEvents.c | 12 +++++++++ IRCEvents.h | 1 + IRCParser.c | 11 +++++++- IRCParser.h | 3 ++- Makefile | 1 + UserNode.c | 14 ++++++++++ UserNode.h | 1 + WHOHandler.c | 10 +++++++ WHOHandler.h | 1 + bots.c | 10 +++++++ bots.h | 9 +++++++ main.c | 16 ++++++++++- modcmd.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ modcmd.h | 1 + 18 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 bots.c create mode 100644 bots.h diff --git a/ChanNode.c b/ChanNode.c index b8ff9f8..47e0663 100644 --- a/ChanNode.c +++ b/ChanNode.c @@ -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 !='#') diff --git a/ChanNode.h b/ChanNode.h index ea949fd..b62beff 100644 --- a/ChanNode.h +++ b/ChanNode.h @@ -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); diff --git a/ClientSocket.c b/ClientSocket.c index fb23f37..25d697f 100644 --- a/ClientSocket.c +++ b/ClientSocket.c @@ -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; +} diff --git a/ClientSocket.h b/ClientSocket.h index de08e8c..5b145fb 100644 --- a/ClientSocket.h +++ b/ClientSocket.h @@ -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 diff --git a/IRCEvents.c b/IRCEvents.c index c0b7eac..51e0d32 100644 --- a/IRCEvents.c +++ b/IRCEvents.c @@ -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) { diff --git a/IRCEvents.h b/IRCEvents.h index 424f3c3..cb01774 100644 --- a/IRCEvents.h +++ b/IRCEvents.h @@ -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); diff --git a/IRCParser.c b/IRCParser.c index b13f5eb..13b76e4 100644 --- a/IRCParser.c +++ b/IRCParser.c @@ -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); + } +} + diff --git a/IRCParser.h b/IRCParser.h index c4d2801..1425f57 100644 --- a/IRCParser.h +++ b/IRCParser.h @@ -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 diff --git a/Makefile b/Makefile index 8f1464f..41831f9 100644 --- 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} diff --git a/UserNode.c b/UserNode.c index 465fd2e..3b18cbd 100644 --- a/UserNode.c +++ b/UserNode.c @@ -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{|}~[\]^_` diff --git a/UserNode.h b/UserNode.h index 49ae8e1..c96f9a1 100644 --- a/UserNode.h +++ b/UserNode.h @@ -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); diff --git a/WHOHandler.c b/WHOHandler.c index 09f4954..b49933e 100644 --- a/WHOHandler.c +++ b/WHOHandler.c @@ -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; +} diff --git a/WHOHandler.h b/WHOHandler.h index 25e134e..aa04f9c 100644 --- a/WHOHandler.h +++ b/WHOHandler.h @@ -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 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 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 2a9a4d9..58609a3 100644 --- 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; diff --git a/modcmd.c b/modcmd.c index 5efe968..ead1efd 100644 --- 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; +} + diff --git a/modcmd.h b/modcmd.h index 8dc866b..c880b58 100644 --- 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); -- 2.20.1