--- /dev/null
+
+#include "IRCEvents.h"
+#include "UserNode.h"
+#include "ChanNode.h"
+#include "ChanUser.h"
+#include "ClientSocket.h"
+#include "mysqlConn.h"
+
+struct binding {
+ void *func;
+ struct binding *next;
+};
+
+static void **binds;
+#define BIND_TYPE_JOIN 0
+#define BIND_TYPE_NICK 1
+#define BIND_TYPE_PART 2
+#define BIND_TYPE_QUIT 3
+#define BIND_TYPE_KICK 4
+#define BIND_TYPE_TOPIC 5
+#define BIND_TYPE_MODE 6
+#define BIND_TYPE_CHANMSG 7
+#define BIND_TYPE_PRIVMSG 8
+#define BIND_TYPE_CHANNOTICE 9
+#define BIND_TYPE_PRIVNOTICE 10
+#define BIND_TYPE_CHANCTCP 11
+#define BIND_TYPE_PRIVCTCP 12
+#define BIND_TYPE_INVITE 13
+#define BIND_TYPE_RAW 14
+#define BIND_TYPE_BOT_READY 15
+
+#define TOTAL_BIND_TYPES 16
+
+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) {
+ if(cbind->func == func)
+ return 1;
+ }
+ return 0;
+}
+
+#define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
+int bind_##NAME(FUNCTYPE *func) { \
+ if(!is_bound(TYPE, func)) { \
+ struct binding *cbind = malloc(sizeof(*cbind)); \
+ if (!cbind) { \
+ perror("malloc() failed"); \
+ return 0; \
+ } \
+ cbind->func = func; \
+ cbind->next = binds[TYPE]; \
+ binds[TYPE] = cbind; \
+ return 1; \
+ } \
+ return 0; \
+}
+
+#define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
+void unbind_##NAME(FUNCTYPE *func) { \
+ struct binding *cbind, *last = NULL, *next; \
+ for(cbind = binds[TYPE]; cbind; cbind = next) { \
+ next = cbind->next; \
+ if(cbind->func == func) { \
+ if(last) \
+ last->next = cbind->next; \
+ else \
+ binds[TYPE] = cbind->next; \
+ free(cbind); \
+ } else \
+ last = cbind; \
+ } \
+}
+
+#define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
+int event_##NAME PDECLARATION { \
+ struct binding *cbind; \
+ pre_event(TYPE); \
+ for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
+ FUNCTYPE *func = cbind->func; \
+ func PLIST; \
+ } \
+ post_event(TYPE); \
+ return 1; \
+}
+
+void pre_event(UNUSED_ARG(int type)) {
+
+}
+
+void post_event(UNUSED_ARG(int type)) {
+ mysql_free();
+}
+
+//EVENTS
+
+FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
+FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
+FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser))
+
+FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
+FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
+FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick))
+
+FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
+FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
+FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason))
+
+FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT)
+FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT)
+FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason))
+
+FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
+FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
+FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
+
+FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
+FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
+FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
+
+FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
+FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
+FUNC_EVENT(mode, mode_func_t, BIND_TYPE_MODE, (struct UserNode *user, struct ChanNode *chan, char *modes, char **args, int argc), (user, chan, modes, args, argc))
+
+FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
+FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
+FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
+
+FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
+FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
+FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
+
+FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
+FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
+FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
+
+FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
+FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
+FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
+
+FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
+FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
+FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
+
+FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
+FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
+FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
+
+FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
+FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
+FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct ClientSocket *client, struct UserNode *user, char *channel), (client, user, channel))
+
+FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
+FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
+FUNC_EVENT(raw, raw_func_t, BIND_TYPE_RAW, (struct ClientSocket *client, char *from, char *cmd, char **argv, int argc), (client, from, cmd, argv, argc))
+
+FUNC_BIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
+FUNC_UNBIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
+FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocket *client), (client))