Merge branch 'development'
[NeonServV5.git] / src / IRCEvents.c
index a8239a9c420012c91cdfcd4a537c49c1bc285e9b..2eea34d8ecc1067bddf693e2a721b6a748abeee7 100644 (file)
@@ -1,5 +1,5 @@
-/* IRCEvents.c - NeonServ v5.2
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* IRCEvents.c - NeonServ v5.6
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "ChanUser.h"
 #include "ClientSocket.h"
 #include "mysqlConn.h"
+#include "log.h"
 
 struct binding {
     void *func;
+    int module_id;
     struct binding *next;
 };
 
-static void **binds;
+static void **binds = NULL;
 #define BIND_TYPE_JOIN       0
 #define BIND_TYPE_NICK       1
 #define BIND_TYPE_PART       2
@@ -45,10 +47,16 @@ static void **binds;
 #define BIND_TYPE_RAW        14
 #define BIND_TYPE_BOT_READY  15
 #define BIND_TYPE_REGISTERED 16
+#define BIND_TYPE_FREEUSER   17
+#define BIND_TYPE_FREECHAN   18
+#define BIND_TYPE_RELOAD     19
+#define BIND_TYPE_FREECLIENT 20
 
-#define TOTAL_BIND_TYPES     17
+#define TOTAL_BIND_TYPES     21
 
 void init_bind() {
+    if(binds)
+        return;
     binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
 }
 
@@ -62,6 +70,26 @@ void free_bind() {
         }
     }
     free(binds);
+    binds = NULL;
+}
+
+void unregister_module_events(int module_id) {
+    struct binding *cbind, *next, *prev;
+    int i;
+    for(i = 0; i < TOTAL_BIND_TYPES; i++) {
+        prev = NULL;
+        for(cbind = binds[i]; cbind; cbind = next) {
+            next = cbind->next;
+            if(cbind->module_id == module_id) {
+                if(prev)
+                    prev->next = next;
+                else
+                    binds[i] = next;
+                free(cbind);
+            } else
+                prev = cbind;
+        }
+    }
 }
 
 static int is_bound(unsigned char type, void *func) {
@@ -74,14 +102,15 @@ 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, int module_id) { \
     if(!is_bound(TYPE, func)) { \
         struct binding *cbind = malloc(sizeof(*cbind)); \
         if (!cbind) { \
-            perror("malloc() failed"); \
+            printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); \
             return 0; \
         } \
         cbind->func = func; \
+        cbind->module_id = module_id; \
         cbind->next = binds[TYPE]; \
         binds[TYPE] = cbind; \
         return 1; \
@@ -122,7 +151,7 @@ void pre_event(UNUSED_ARG(int type)) {
 }
 
 void post_event(UNUSED_ARG(int type)) {
-    mysql_free();
+
 }
 
 //EVENTS
@@ -137,11 +166,7 @@ FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_
 
 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_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, int quit, char *reason), (chanuser, quit, reason))
 
 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
@@ -193,14 +218,20 @@ FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocke
 
 FUNC_BIND(registered, registered_func_t, BIND_TYPE_REGISTERED)
 FUNC_UNBIND(registered, registered_func_t, BIND_TYPE_REGISTERED)
-int event_registered(struct UserNode *old_user, struct UserNode *new_user) {
-    struct binding *cbind;
-    int ret = 0;
-    pre_event(BIND_TYPE_REGISTERED);
-    for(cbind = binds[BIND_TYPE_REGISTERED]; cbind; cbind = cbind->next) {
-        registered_func_t *func = cbind->func;
-        ret |= func(old_user, new_user);
-    }
-    post_event(BIND_TYPE_REGISTERED);
-    return ret;
-}
+FUNC_EVENT(registered, registered_func_t, BIND_TYPE_REGISTERED, (struct UserNode *user, char *new_mask), (user, new_mask))
+
+FUNC_BIND(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER)
+FUNC_UNBIND(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER)
+FUNC_EVENT(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER, (struct UserNode *user), (user))
+
+FUNC_BIND(freechan, freechan_func_t, BIND_TYPE_FREECHAN)
+FUNC_UNBIND(freechan, freechan_func_t, BIND_TYPE_FREECHAN)
+FUNC_EVENT(freechan, freechan_func_t, BIND_TYPE_FREECHAN, (struct ChanNode *chan), (chan))
+
+FUNC_BIND(reload, reload_func_t, BIND_TYPE_RELOAD)
+FUNC_UNBIND(reload, reload_func_t, BIND_TYPE_RELOAD)
+FUNC_EVENT(reload, reload_func_t, BIND_TYPE_RELOAD, (int initialization), (initialization))
+
+FUNC_BIND(freeclient, freeclient_func_t, BIND_TYPE_FREECLIENT)
+FUNC_UNBIND(freeclient, freeclient_func_t, BIND_TYPE_FREECLIENT)
+FUNC_EVENT(freeclient, freeclient_func_t, BIND_TYPE_FREECLIENT, (struct ClientSocket *client), (client))