X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FWHOHandler.c;h=5db3db6a3d58be68ad50a402308033f61d004c6c;hp=63232f7c7998d0be5451118cecb9cedfb662172b;hb=f6bdc9d0fc8db22c265918f3325e11177fd001b9;hpb=eba9bbb5fb73ad2bd0c420e03199a828b6eabc5f diff --git a/src/WHOHandler.c b/src/WHOHandler.c index 63232f7..5db3db6 100644 --- a/src/WHOHandler.c +++ b/src/WHOHandler.c @@ -1,4 +1,4 @@ -/* WHOHandler.c - NeonServ v5.3 +/* WHOHandler.c - NeonServ v5.6 * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -22,6 +22,8 @@ #include "ModeNode.h" #include "ClientSocket.h" #include "IPNode.h" +#include "modules.h" +#include "log.h" #define WHOQUEUETYPE_ISONQUEUE 0x01 #define WHOQUEUETYPE_USERLIST 0x02 @@ -29,7 +31,7 @@ #define WHOQUEUETYPE_CHECKTYPE 0x07 #define WHOQUEUETYPE_FOUND 0x08 -#define MAXCALLBACKS 3 +#define MAXCALLBACKS 10 struct WHOQueueEntry { char type; @@ -38,12 +40,8 @@ struct WHOQueueEntry { struct UserNode *user; struct WHOQueueEntry *next; void *callback[MAXCALLBACKS]; + int module_id[MAXCALLBACKS]; void *data[MAXCALLBACKS]; - #ifdef HAVE_THREADS - unsigned long lock_tid; - pthread_mutex_t lock_mutex; - int lock_count; - #endif }; static int checkWHOID(struct ClientSocket *client, int whoid) { @@ -68,17 +66,12 @@ static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) { struct WHOQueueEntry *entry = malloc(sizeof(*entry)); if (!entry) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); DESYNCHRONIZE(whohandler_sync); return NULL; } entry->next = NULL; entry->whoid = whoid; - #ifdef HAVE_THREADS - entry->lock_tid = 0; - THREAD_MUTEX_INIT(entry->lock_mutex); - entry->lock_count = 0; - #endif if(client->whoqueue_last) { client->whoqueue_last->next = entry; } else { @@ -106,15 +99,6 @@ static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, i if(entry == client->whoqueue_last) { client->whoqueue_last = NULL; } - #ifdef HAVE_THREADS - entry->lock_tid = -1; - if(entry->lock_count) { - int i; - for(i = 0; i < entry->lock_count; i++) { - DESYNCHRONIZE(entry->lock_mutex); //unlock ALL - } - } - #endif } DESYNCHRONIZE(whohandler_sync); return entry; @@ -126,15 +110,6 @@ void clear_whoqueue(struct ClientSocket *client) { struct WHOQueueEntry *entry, *next; for(entry = client->whoqueue_first; entry; entry = next) { next = entry->next; - #ifdef HAVE_THREADS - entry->lock_tid = -1; - if(entry->lock_count) { - int i; - for(i = 0; i < entry->lock_count; i++) { - DESYNCHRONIZE(entry->lock_mutex); //unlock ALL - } - } - #endif free(entry); } client->whoqueue_last = NULL; @@ -142,22 +117,7 @@ void clear_whoqueue(struct ClientSocket *client) { DESYNCHRONIZE(whohandler_sync); } -#if HAVE_THREADS -void whohandler_end_of_recv(struct ClientSocket *client) { - SYNCHRONIZE(whohandler_sync); - unsigned long tid = syscall(SYS_gettid); - struct WHOQueueEntry *entry; - for(entry = client->whoqueue_first; entry; entry = entry->next) { - if(entry->lock_tid == tid) { - entry->lock_tid = 0; - DESYNCHRONIZE(entry->lock_mutex); - } - } - DESYNCHRONIZE(whohandler_sync); -} -#endif - -void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) { +void get_userlist(struct ChanNode *chan, int module_id, userlist_callback_t callback, void *data) { struct ClientSocket *bot; for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) { if(isUserOnChan(bot->user, chan)) @@ -180,6 +140,7 @@ void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *dat entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST; entry->chan = chan; entry->callback[0] = callback; + entry->module_id[0] = module_id; int i; for(i = 1; i < MAXCALLBACKS; i++) entry->callback[i] = NULL; @@ -191,7 +152,7 @@ void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *dat callback(bot, chan, data); } -void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data, int force) { +void _get_userlist_with_invisible(struct ChanNode *chan, int module_id, userlist_callback_t callback, void *data, int force) { struct ClientSocket *bot; for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) { if(isUserOnChan(bot->user, chan)) @@ -215,6 +176,7 @@ void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t cal entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST; entry->chan = chan; entry->callback[0] = callback; + entry->module_id[0] = module_id; int i; for(i = 1; i < MAXCALLBACKS; i++) entry->callback[i] = NULL; @@ -226,7 +188,7 @@ void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t cal callback(bot, chan, data); } -void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) { +void get_userauth(struct UserNode *user, int module_id, 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; @@ -237,6 +199,7 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat for(i = 1; i < MAXCALLBACKS; i++) { if(!entry->callback[i]) { entry->callback[i] = callback; + entry->module_id[i] = module_id; entry->data[i] = data; return; } @@ -253,7 +216,7 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat callback(bot, user->nick, NULL, data); return; } - if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) { + if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) { callback(bot, user->nick, user, data); return; } @@ -262,6 +225,7 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat entry->user = user; user->flags |= USERFLAG_IS_ON_WHO_QUEUE; entry->callback[0] = callback; + entry->module_id[0] = module_id; int i; for(i = 1; i < MAXCALLBACKS; i++) entry->callback[i] = NULL; @@ -285,14 +249,9 @@ static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsig struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0); if(entry == NULL) return; #ifdef HAVE_THREADS - unsigned long tid = syscall(SYS_gettid); - if(entry->lock_tid != tid) { - entry->lock_count++; - SYNCHRONIZE(entry->lock_mutex); - if(entry->lock_tid == -1) { - return; //entry has been destroyed - } - entry->lock_tid = tid; + unsigned int tid = (unsigned int) pthread_self_tid(); + while(!clientsocket_parseorder_top(tid)) { + usleep(1000); //1ms } #endif if(entry->type & WHOQUEUETYPE_USERLIST) { @@ -327,8 +286,8 @@ static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsig struct UserNode *user = getUserByNick(argv[5]); struct ChanUser *chanuser; - if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || !isBot(user))) { - user = createTempUser(argv[5]); + if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || (user && !isBot(user)))) { + user = createTempUser(argv[5]); //always add a temponary user to prevent cache problems when the user joins right now (while it's stored in our cache as being invisible) user->flags |= USERFLAG_ISTMPUSER; chan->flags |= CHANFLAG_HAVE_INVISIBLES; chanuser = addInvisibleChanUser(chan, user); @@ -365,7 +324,8 @@ static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsig for(i = 0; i < MAXCALLBACKS; i++) { userauth_callback_t *callback = entry->callback[i]; if(!callback) break; - callback(client, entry->user->nick, entry->user, entry->data[i]); + if(!entry->module_id[i] || module_loaded(entry->module_id[i])) + callback(client, entry->user->nick, entry->user, entry->data[i]); } } } @@ -384,14 +344,9 @@ static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsig struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0); if(entry == NULL) return; #ifdef HAVE_THREADS - unsigned long tid = syscall(SYS_gettid); - if(entry->lock_tid != tid) { - entry->lock_count++; - SYNCHRONIZE(entry->lock_mutex); - if(entry->lock_tid == -1) { - return; //entry has been destroyed - } - entry->lock_tid = tid; + unsigned int tid = (unsigned int) pthread_self_tid(); + while(!clientsocket_parseorder_top(tid)) { + usleep(1000); //1ms } #endif getNextWHOQueueEntry(client, type, 1); @@ -403,7 +358,8 @@ static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsig for(i = 0; i < MAXCALLBACKS; i++) { callback = entry->callback[i]; if(!callback) break; - callback(client, entry->chan, entry->data[i]); + if(!entry->module_id[i] || module_loaded(entry->module_id[i])) + callback(client, entry->chan, entry->data[i]); } if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) { //remove all invisible users again @@ -422,7 +378,8 @@ static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsig for(i = 0; i < MAXCALLBACKS; i++) { callback = entry->callback[i]; if(!callback) break; - callback(client, entry->user->nick, NULL, entry->data[i]); + if(!entry->module_id[i] || module_loaded(entry->module_id[i])) + callback(client, entry->user->nick, NULL, entry->data[i]); } } entry->user->flags &= ~USERFLAG_IS_ON_WHO_QUEUE;