From 3114e0ba66f59644658b554b022cd19877ac3ff2 Mon Sep 17 00:00:00 2001 From: pk910 Date: Wed, 26 Oct 2011 16:08:39 +0200 Subject: [PATCH] completely rebuild WHOHander ticket management --- src/ClientSocket.c | 5 +++ src/ClientSocket.h | 3 ++ src/WHOHandler.c | 76 ++++++++++++++++++++-------------------------- src/WHOHandler.h | 1 + 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/ClientSocket.c b/src/ClientSocket.c index 2c77b08..5e790da 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -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); diff --git a/src/ClientSocket.h b/src/ClientSocket.h index 45cf06d..527d17e 100644 --- a/src/ClientSocket.h +++ b/src/ClientSocket.h @@ -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; diff --git a/src/WHOHandler.c b/src/WHOHandler.c index 2c46b25..ffb1e47 100644 --- a/src/WHOHandler.c +++ b/src/WHOHandler.c @@ -32,16 +32,12 @@ 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; + } diff --git a/src/WHOHandler.h b/src/WHOHandler.h index ee27779..04d1018 100644 --- a/src/WHOHandler.h +++ b/src/WHOHandler.h @@ -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); -- 2.20.1