completely rebuild WHOHander ticket management
authorpk910 <philipp@zoelle1.de>
Wed, 26 Oct 2011 14:08:39 +0000 (16:08 +0200)
committerpk910 <philipp@zoelle1.de>
Thu, 27 Oct 2011 00:23:02 +0000 (02:23 +0200)
src/ClientSocket.c
src/ClientSocket.h
src/WHOHandler.c
src/WHOHandler.h

index 2c77b080207e1eb607bda3eafbc241c8f6e672d0..5e790dac4e3b2de07d61dc3f15057f6d6fe7ea5e 100644 (file)
@@ -19,6 +19,7 @@
 #include "IRCParser.h"
 #include "UserNode.h"
 #include "IRCQueue.h"
+#include "WHOHandler.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -61,6 +62,8 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
        client->botid = 0;
     client->clientid = 0;
     client->queue = NULL;
+    client->whoqueue_first = NULL;
+    client->whoqueue_last = NULL;
     client->next = sockets->data;
     sockets->data = client;
     return client;
@@ -189,6 +192,8 @@ int close_socket(struct ClientSocket *client) {
     }
     if(client->queue)
         queue_destroy(client);
+    if(client->whoqueue_first)
+        clear_whoqueue(client);
     free(client->host);
     free(client->pass);
     free(client);
index 45cf06d9499e69d53cd18addb969bb3f103a39ba..527d17e2abb10fef8efd8400fc1ae251db006f45 100644 (file)
@@ -45,6 +45,9 @@ struct ClientSocket {
     time_t connection_time;
     
     struct BotQueue *queue;
+    
+    struct WHOQueueEntry *whoqueue_first;
+    struct WHOQueueEntry *whoqueue_last;
        
        int botid : 16;
     int clientid : 16;
index 2c46b25dce873a86a31cf51ba02a4ed72aeb5b71..ffb1e47469750d8472f023fe13f8301a2340ebca 100644 (file)
 
 struct WHOQueueEntry {
     char type;
-    struct ClientSocket *client;
     struct ChanNode *chan;
     struct UserNode *user;
     struct WHOQueueEntry *next;
     void *callback[MAXCALLBACKS];
     void *data[MAXCALLBACKS];
 };
-
-static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
-
 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
     if (!entry)
@@ -50,38 +46,37 @@ static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
         return NULL;
     }
     entry->next = NULL;
-    entry->client = client;
-    if(last_entry) {
-        last_entry->next = entry;
-        last_entry = entry;
+    if(client->whoqueue_last) {
+        client->whoqueue_last->next = entry;
     } else {
-        last_entry = entry;
-        first_entry = entry;
+        client->whoqueue_first = entry;
     }
+    client->whoqueue_last = entry;
     return entry;
 }
 
 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
-    if(!first_entry) return NULL;
-    struct WHOQueueEntry *entry;
-    for(entry = first_entry; entry; entry = entry->next) {
-        if(entry->client == client)
-            break;
-    }
-    if(entry == NULL) return NULL;
+    if(!client->whoqueue_first) return NULL;
+    struct WHOQueueEntry *entry = client->whoqueue_first;
     if(freeEntry) {
-        if(entry == first_entry)
-            first_entry = entry->next;
-        if(entry == last_entry) {
-            struct WHOQueueEntry *last = NULL;
-            for(last = first_entry; last; last = last->next)
-                if(last->next == NULL) break;
-            last_entry = last;
+        client->whoqueue_first = entry->next;
+        if(entry == client->whoqueue_last) {
+            client->whoqueue_last = NULL;
         }
     }
     return entry;
 }
 
+void clear_whoqueue(struct ClientSocket *client) {
+    if(!client->whoqueue_first) return;
+    struct WHOQueueEntry *entry, *next;
+    for(entry = client->whoqueue_first; entry; entry = next) {
+        next = entry->next;
+        free(entry);
+    }
+    client->whoqueue_last = NULL;
+}
+
 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
     struct ClientSocket *bot;
     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
@@ -153,24 +148,25 @@ void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t cal
 
 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
     //check if we have already an active WHO for this user
+    struct ClientSocket *bot, *whobot = NULL;
     struct WHOQueueEntry *entry;
-    for(entry = first_entry; entry; entry = entry->next) {
-        if((entry->type & WHOQUEUETYPE_USERAUTH) && entry->user == user) {
-            int i = 0;
-            for(i = 1; i < MAXCALLBACKS; i++) {
-                if(!entry->callback[i]) {
-                    entry->callback[i] = callback;
-                    entry->data[i] = data;
-                    return;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        for(entry = bot->whoqueue_first; entry; entry = entry->next) {
+            if((entry->type & WHOQUEUETYPE_USERAUTH) && entry->user == user) {
+                int i = 0;
+                for(i = 1; i < MAXCALLBACKS; i++) {
+                    if(!entry->callback[i]) {
+                        entry->callback[i] = callback;
+                        entry->data[i] = data;
+                        return;
+                    }
                 }
             }
         }
-    }
-    struct ClientSocket *bot;
-    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
         if(bot->flags & SOCKET_FLAG_PREFERRED)
-            break;
+            whobot = bot;
     }
+    bot = whobot;
     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
     //check if we really need to who the user
     if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) {
@@ -313,11 +309,5 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int
 }
 
 void free_whoqueue() {
-    struct WHOQueueEntry *entry, *next;
-    for(entry = first_entry; entry; entry = next) {
-        next = entry->next;
-        free(entry);
-    }
-    first_entry = NULL;
-    last_entry = NULL;
+    
 }
index ee27779ceff56eac7d31ee5b5957854d76acc0a9..04d1018c2d1253199d0ca6a3a247965d570c16ae 100644 (file)
@@ -29,6 +29,7 @@ typedef USERLIST_CALLBACK(userlist_callback_t);
 #define USERAUTH_CALLBACK(NAME) void NAME(UNUSED_ARG(struct ClientSocket *client), UNUSED_ARG(char *user_nick), UNUSED_ARG(struct UserNode *user), UNUSED_ARG(void *data))
 typedef USERAUTH_CALLBACK(userauth_callback_t);
 
+void clear_whoqueue(struct ClientSocket *client);
 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data);