added .gitignore
[NeonServV5.git] / IRCEvents.c
index 8ea279a7983c483e6e9e375186dc4cbf2a3115cd..d85dff9b9027c49736d02c66d6fe1d2ec3a41dae 100644 (file)
@@ -4,11 +4,12 @@
 #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
@@ -26,13 +27,26 @@ static void **binds;
 #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     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) {
@@ -43,22 +57,23 @@ static int is_bound(unsigned char type, void *func) {
 }
 
 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
-int bind_NAME(FUNCTYPE *func) { \
+int bind_##NAME(FUNCTYPE *func) { \
     if(!is_bound(TYPE, func)) { \
-        struct binding *bind = malloc(sizeof(*bind)); \
-        if (!bind) { \
+        struct binding *cbind = malloc(sizeof(*cbind)); \
+        if (!cbind) { \
             perror("malloc() failed"); \
             return 0; \
         } \
-        bind->func = func; \
-        bind->next = binds[TYPE]; \
-        binds[TYPE] = bind; \
+        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) { \
+void unbind_##NAME(FUNCTYPE *func) { \
     struct binding *cbind, *last = NULL, *next; \
     for(cbind = binds[TYPE]; cbind; cbind = next) { \
         next = cbind->next; \
@@ -66,7 +81,7 @@ void unbind_NAME(FUNCTYPE *func) { \
             if(last) \
                 last->next = cbind->next; \
             else \
-                binds[TYPE] = cbind->next \
+                binds[TYPE] = cbind->next; \
             free(cbind); \
         } else \
             last = cbind; \
@@ -74,24 +89,34 @@ void unbind_NAME(FUNCTYPE *func) { \
 }
 
 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
-int event_NAME(PDECLARATION) { \
+int event_##NAME PDECLARATION { \
     struct binding *cbind; \
-    for(cbind = binds[TYPE]; cbind; cbind = next) { \
+    pre_event(TYPE); \
+    for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
         FUNCTYPE *func = cbind->func; \
-        func(PLIST); \
+        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_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, newnick))
+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)
@@ -145,3 +170,6 @@ 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))