rewrote HandleInfoHandler.c (WHOHandler style)
authorpk910 <philipp@zoelle1.de>
Wed, 2 Nov 2011 01:49:00 +0000 (02:49 +0100)
committerpk910 <philipp@zoelle1.de>
Wed, 2 Nov 2011 01:50:47 +0000 (02:50 +0100)
src/ClientSocket.c
src/ClientSocket.h
src/HandleInfoHandler.c
src/HandleInfoHandler.h

index 5e790dac4e3b2de07d61dc3f15057f6d6fe7ea5e..b76e6f62c421c9af81e9cb6be9a629bf34ac425c 100644 (file)
@@ -20,6 +20,7 @@
 #include "UserNode.h"
 #include "IRCQueue.h"
 #include "WHOHandler.h"
+#include "HandleInfoHandler.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -64,6 +65,8 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
     client->queue = NULL;
     client->whoqueue_first = NULL;
     client->whoqueue_last = NULL;
+    client->handleinfo_first = NULL;
+    client->handleinfo_last = NULL;
     client->next = sockets->data;
     sockets->data = client;
     return client;
@@ -194,6 +197,8 @@ int close_socket(struct ClientSocket *client) {
         queue_destroy(client);
     if(client->whoqueue_first)
         clear_whoqueue(client);
+    if(client->handleinfo_first)
+        clear_handleinfoqueue(client);
     free(client->host);
     free(client->pass);
     free(client);
index 527d17e2abb10fef8efd8400fc1ae251db006f45..f0334508c502779638cc478145b46ab6030a3af9 100644 (file)
@@ -48,6 +48,9 @@ struct ClientSocket {
     
     struct WHOQueueEntry *whoqueue_first;
     struct WHOQueueEntry *whoqueue_last;
+    
+    struct HandleInfoQueueEntry *handleinfo_first;
+    struct HandleInfoQueueEntry *handleinfo_last;
        
        int botid : 16;
     int clientid : 16;
index 269d5fb9489c205a728cfd631db38d61a27f35f4..8a135cdb16c8453b2d87d0f09b2c1c8fb409b587 100644 (file)
 
 #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)
@@ -41,47 +41,65 @@ static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket
         return NULL;
     }
     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;
     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;
+    struct HandleInfoQueueEntry *entry = client->handleinfo_first;
     if(freeEntry) {
-        if(entry == first_entry)
-            first_entry = entry->next;
-        if(entry == last_entry) {
-            struct HandleInfoQueueEntry *last = NULL;
-            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;
         }
     }
     return entry;
 }
 
+void clear_handleinfoqueue(struct ClientSocket *client) {
+    if(!client->handleinfo_first) return;
+    struct HandleInfoQueueEntry *entry, *next;
+    for(entry = client->handleinfo_first; entry; entry = next) {
+        next = entry->next;
+        free(entry);
+    }
+    client->handleinfo_last = NULL;
+}
+
 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);
 }
 
@@ -116,8 +134,14 @@ static void recv_notice(struct UserNode *user, struct UserNode *target, char *me
     if(do_match) {
         struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(bot, 1);
         if(entry) {
-            authlookup_callback_t *callback = entry->callback;
-            callback(auth, exists, entry->data);
+            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);
         }
     }
@@ -128,11 +152,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;
+    
 }
index 7971ddfe53f9c997b2df4896c8c699e4a90f9dfc..1f70642eefb536ee7912ed3330276a222767bf3e 100644 (file)
@@ -25,6 +25,7 @@ struct UserNode;
 #define AUTHLOOKUP_CALLBACK(NAME) void NAME(UNUSED_ARG(char *auth), UNUSED_ARG(int exists), UNUSED_ARG(void *data))
 typedef AUTHLOOKUP_CALLBACK(authlookup_callback_t);
 
+void clear_handleinfoqueue(struct ClientSocket *client);
 void lookup_authname(char *auth, authlookup_callback_t callback, void *data);
 void init_handleinfohandler();
 void free_handleinfohandler();