added experimental multi thread support
[NeonServV5.git] / src / HandleInfoHandler.c
index bda467e8ecfeab6959de34d0bf91d431cea25b73..99d19658b73c33d6b24abb729988eecbc44cc746 100644 (file)
@@ -1,22 +1,39 @@
+/* HandleInfoHandler.c - NeonServ v5.3
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
 
 #include "HandleInfoHandler.h"
 #include "ClientSocket.h"
 #include "UserNode.h"
+#include "ChanNode.h"
 #include "IRCEvents.h"
 #include "tools.h"
 
 #define AUTHSERV_NICK "AuthServ"
 
+#define MAXCALLBACKS 3
+
 struct HandleInfoQueueEntry {
-    struct ClientSocket *client;
-    void *callback;
-    void *data;
+    char *auth;
+    void *callback[MAXCALLBACKS];
+    void *data[MAXCALLBACKS];
     
     struct HandleInfoQueueEntry *next;
 };
 
-static struct HandleInfoQueueEntry *first_entry = NULL, *last_entry = NULL;
-
 static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket *client) {
     struct HandleInfoQueueEntry *entry = malloc(sizeof(*entry));
     if (!entry)
@@ -24,48 +41,73 @@ static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket
         perror("malloc() failed");
         return NULL;
     }
+    SYNCHRONIZE(cache_sync);
     entry->next = NULL;
-    entry->client = client;
-    if(last_entry)
-        last_entry->next = entry;
+    if(client->handleinfo_last)
+        client->handleinfo_last->next = entry;
     else
-        last_entry = entry;
-    if(!first_entry)
-        first_entry = entry;
+        client->handleinfo_first = entry;
+    client->handleinfo_last = entry;
+    DESYNCHRONIZE(cache_sync);
     return entry;
 }
 
 static struct HandleInfoQueueEntry* getNextHandleInfoQueueEntry(struct ClientSocket *client, int freeEntry) {
-    if(!first_entry) return NULL;
-    struct HandleInfoQueueEntry *entry;
-    for(entry = first_entry; entry; entry = entry->next) {
-        if(entry->client == client)
-            break;
-    }
-    if(entry == NULL) return NULL;
+    if(!client->handleinfo_first) return NULL;
+    SYNCHRONIZE(cache_sync);
+    struct HandleInfoQueueEntry *entry = client->handleinfo_first;
     if(freeEntry) {
-        if(entry == first_entry)
-            first_entry = entry->next;
-        if(entry == last_entry) {
-            struct HandleInfoQueueEntry *last;
-            for(last = first_entry; last; last = last->next)
-                if(last->next == NULL) break;
-            last_entry = last;
+        client->handleinfo_first = entry->next;
+        if(entry == client->handleinfo_last) {
+            client->handleinfo_last = NULL;
         }
     }
+    DESYNCHRONIZE(cache_sync);
     return entry;
 }
 
+void clear_handleinfoqueue(struct ClientSocket *client) {
+    if(!client->handleinfo_first) return;
+    SYNCHRONIZE(cache_sync);
+    struct HandleInfoQueueEntry *entry, *next;
+    for(entry = client->handleinfo_first; entry; entry = next) {
+        next = entry->next;
+        free(entry);
+    }
+    client->handleinfo_last = NULL;
+    client->handleinfo_first = NULL;
+    DESYNCHRONIZE(cache_sync);
+}
+
 void lookup_authname(char *auth, authlookup_callback_t callback, void *data) {
     struct ClientSocket *bot;
+    struct HandleInfoQueueEntry* entry;
     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        for(entry = bot->handleinfo_first; entry; entry = entry->next) {
+            if(!stricmp(entry->auth, auth)) {
+                int i = 0;
+                for(i = 1; i < MAXCALLBACKS; i++) {
+                    if(!entry->callback[i]) {
+                        entry->callback[i] = callback;
+                        entry->data[i] = data;
+                        return;
+                    }
+                }
+            }
+        }
         if(bot->flags & SOCKET_FLAG_PREFERRED)
             break;
     }
     if(bot == NULL) return;
-    struct HandleInfoQueueEntry* entry = addHandleInfoQueueEntry(bot);
-    entry->callback = callback;
-    entry->data = data;
+    entry = addHandleInfoQueueEntry(bot);
+    int i;
+    entry->auth = strdup(auth);
+    entry->callback[0] = callback;
+    for(i = 1; i < MAXCALLBACKS; i++)
+        entry->callback[i] = NULL;
+    entry->data[0] = data;
+    for(i = 1; i < MAXCALLBACKS; i++)
+        entry->data[i] = NULL;
     putsock(bot, "PRIVMSG " AUTHSERV_NICK " :ACCOUNTINFO *%s", auth);
 }
 
@@ -99,9 +141,17 @@ static void recv_notice(struct UserNode *user, struct UserNode *target, char *me
     }
     if(do_match) {
         struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(bot, 1);
-        authlookup_callback_t *callback = entry->callback;
-        callback(auth, exists, entry->data);
-        free(entry);
+        if(entry) {
+            authlookup_callback_t *callback;
+            int i;
+            for(i = 0; i < MAXCALLBACKS; i++) {
+                callback = entry->callback[i];
+                if(!callback) break;
+                callback(auth, exists, entry->data[i]);
+            }
+            free(entry->auth);
+            free(entry);
+        }
     }
 }
 
@@ -110,11 +160,5 @@ void init_handleinfohandler() {
 }
 
 void free_handleinfohandler() {
-    struct HandleInfoQueueEntry *entry, *next;
-    for(entry = first_entry; entry; entry = next) {
-        next = entry->next;
-        free(entry);
-    }
-    first_entry = NULL;
-    last_entry = NULL;
+    
 }