X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FWHOHandler.c;h=a8a55d91c693956cebd57329babecb6ec73fa829;hb=55831bf424312a6908ca07a904f288fba0919a9a;hp=02f5e6251cded9b19d1674c839d0897a10895b0a;hpb=2d9db1adb1946aba00b203f40eff7d5db8163f01;p=NeonServV5.git diff --git a/src/WHOHandler.c b/src/WHOHandler.c index 02f5e62..a8a55d9 100644 --- a/src/WHOHandler.c +++ b/src/WHOHandler.c @@ -1,5 +1,5 @@ -/* WHOHandler.c - NeonServ v5.1 - * Copyright (C) 2011 Philipp Kreil (pk910) +/* WHOHandler.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 @@ -19,7 +19,9 @@ #include "ChanNode.h" #include "UserNode.h" #include "ChanUser.h" +#include "ModeNode.h" #include "ClientSocket.h" +#include "IPNode.h" #define WHOQUEUETYPE_ISONQUEUE 0x01 #define WHOQUEUETYPE_USERLIST 0x02 @@ -27,18 +29,16 @@ #define WHOQUEUETYPE_CHECKTYPE 0x07 #define WHOQUEUETYPE_FOUND 0x08 +#define MAXCALLBACKS 3 + struct WHOQueueEntry { char type; - struct ClientSocket *client; struct ChanNode *chan; struct UserNode *user; struct WHOQueueEntry *next; - void *callback; - void *data; + 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) @@ -46,39 +46,45 @@ static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) { perror("malloc() failed"); return NULL; } + SYNCHRONIZE(cache_sync); 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; + DESYNCHRONIZE(cache_sync); 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; + SYNCHRONIZE(cache_sync); + struct WHOQueueEntry *entry = client->whoqueue_first; if(freeEntry) { - if(entry == first_entry) - first_entry = entry->next; - if(entry == last_entry) { - struct WHOQueueEntry *last; - 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; } } + DESYNCHRONIZE(cache_sync); return entry; } +void clear_whoqueue(struct ClientSocket *client) { + if(!client->whoqueue_first) return; + SYNCHRONIZE(cache_sync); + struct WHOQueueEntry *entry, *next; + for(entry = client->whoqueue_first; entry; entry = next) { + next = entry->next; + free(entry); + } + client->whoqueue_last = NULL; + client->whoqueue_first = NULL; + DESYNCHRONIZE(cache_sync); +} + 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)) { @@ -86,43 +92,110 @@ void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *dat break; } if(bot == NULL) return; - struct WHOQueueEntry* entry = addWHOQueueEntry(bot); - entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST; - entry->chan = chan; - entry->callback = callback; - entry->data = data; - //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id - putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type); + //check if we really need to who the channel + int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST)); + if(!do_who) { + struct ChanUser *chanuser; + for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) { + if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) { + do_who = 1; + break; + } + } + } + if(do_who) { + struct WHOQueueEntry* entry = addWHOQueueEntry(bot); + entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST; + entry->chan = chan; + entry->callback[0] = callback; + int i; + 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, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->type, entry->type); + } else + callback(bot, chan, data); } -void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data) { +void _get_userlist_with_invisible(struct ChanNode *chan, 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)) break; } if(bot == NULL) return; - struct WHOQueueEntry* entry = addWHOQueueEntry(bot); - entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST; - entry->chan = chan; - entry->callback = callback; - entry->data = data; - //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id - putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type); + //check if we really need to who the channel + //invisible users can only be present if chanmode +D or +d is set! + int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST)) || (isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D')); + if(!do_who && force) { + struct ChanUser *chanuser; + for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) { + if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) { + do_who = 1; + break; + } + } + } + if(do_who) { + struct WHOQueueEntry* entry = addWHOQueueEntry(bot); + entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST; + entry->chan = chan; + entry->callback[0] = callback; + int i; + 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, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->type, entry->type); + } else + callback(bot, chan, data); } void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) { - struct ClientSocket *bot; + //check if we have already an active WHO for this user + struct ClientSocket *bot, *whobot = NULL; + struct WHOQueueEntry *entry; 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; + } + } + } + } if(bot->flags & SOCKET_FLAG_PREFERRED) - break; + whobot = bot; } + bot = whobot; if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL); - struct WHOQueueEntry* entry = addWHOQueueEntry(bot); + //check if we really need to who the user + if(!is_valid_nick(user->nick)) { + 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) { + callback(bot, user->nick, user, data); + return; + } + entry = addWHOQueueEntry(bot); entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH; entry->user = user; - entry->callback = callback; - entry->data = data; + user->flags |= USERFLAG_IS_ON_WHO_QUEUE; + entry->callback[0] = callback; + int i; + for(i = 1; i < MAXCALLBACKS; i++) + entry->callback[i] = NULL; + entry->data[0] = data; + for(i = 1; i < MAXCALLBACKS; i++) + entry->data[i] = NULL; //WHO ".$user->getNick().",".$id." %tuhna,".$id putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type); } @@ -131,6 +204,7 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int int i; if(argc < 2) return; int type = atoi(argv[1]); + if(!type) return; if(!(type & WHOQUEUETYPE_ISONQUEUE)) return; struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0); if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return; @@ -142,11 +216,14 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int //parse flags int userflags = 0; int chanuserflags = 0; - for(i = 0; i < strlen(argv[5]); i++) { - switch (argv[5][i]) { + for(i = 0; i < strlen(argv[6]); i++) { + switch (argv[6][i]) { case '@': chanuserflags |= CHANUSERFLAG_OPPED; break; + case '%': + chanuserflags |= CHANUSERFLAG_HALFOPPED; + break; case '+': chanuserflags |= CHANUSERFLAG_VOICED; break; @@ -161,47 +238,56 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int } } - struct UserNode *user; - if(chanuserflags & CHANUSERFLAG_INVISIBLE) { - user = createTempUser(argv[4]); + struct UserNode *user = getUserByNick(argv[5]); + struct ChanUser *chanuser; + if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || !isBot(user))) { + user = createTempUser(argv[5]); user->flags |= USERFLAG_ISTMPUSER; chan->flags |= CHANFLAG_HAVE_INVISIBLES; - struct ChanUser *chanuser = addInvisibleChanUser(chan, user); + chanuser = addInvisibleChanUser(chan, user); chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags; } else { - user = getUserByNick(argv[4]); if(user == NULL) { - user = addUser(argv[4]); + user = addUser(argv[5]); } - if(!isUserOnChan(user, chan)) { - struct ChanUser *chanuser = addChanUser(chan, user); - chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags; + if(!(chanuser = getChanUser(user, chan))) { + chanuser = addChanUser(chan, user); } + chanuser->flags = (chanuser->flags & ~(CHANUSERFLAG_OPPED_OR_VOICED | CHANUSERFLAG_INVISIBLE)) | chanuserflags; } user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags; + user->last_who = time(0); if(!*user->ident) strcpy(user->ident, argv[2]); + if(!user->ip) + user->ip = createIPNode(argv[3]); if(!*user->host) - strcpy(user->host, argv[3]); - if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) { - strcpy(user->auth, argv[6]); + strcpy(user->host, argv[4]); + if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[7], "0")) { + strcpy(user->auth, argv[7]); user->flags |= USERFLAG_ISAUTHED; } - } else if(type & WHOQUEUETYPE_USERAUTH) { + } else if((type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) { //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910 entry->type |= WHOQUEUETYPE_FOUND; + entry->user->last_who = time(0); if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) { strcpy(entry->user->auth, argv[5]); entry->user->flags |= USERFLAG_ISAUTHED; } - userauth_callback_t *callback = entry->callback; - callback(client, entry->user->nick, entry->user, entry->data); + 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]); + } } } void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) { if(argc < 2) return; - char *typestr = strstr(argv[1], ",") + 1; + char *typestr = strstr(argv[1], ","); + if(!typestr) return; + typestr++; int type = atoi(typestr); if(!(type & WHOQUEUETYPE_ISONQUEUE)) return; struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1); @@ -209,33 +295,41 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int if(type & WHOQUEUETYPE_USERLIST) { //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list. entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST; - userlist_callback_t *callback = entry->callback; - callback(client, entry->chan, entry->data); + userlist_callback_t *callback; + int i; + for(i = 0; i < MAXCALLBACKS; i++) { + callback = entry->callback[i]; + if(!callback) break; + callback(client, entry->chan, entry->data[i]); + } if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) { //remove all invisible users again struct ChanUser *chanuser, *next; for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) { next = getChannelUsers(entry->chan, chanuser); - if(chanuser->flags & CHANUSERFLAG_INVISIBLE) { + if((chanuser->flags & CHANUSERFLAG_INVISIBLE) && !isBot(chanuser->user)) { delChanUser(chanuser, 1); } } } } else if(type & WHOQUEUETYPE_USERAUTH) { if(!(entry->type & WHOQUEUETYPE_FOUND)) { - userauth_callback_t *callback = entry->callback; - callback(client, entry->user->nick, NULL, entry->data); + userauth_callback_t *callback; + int i; + for(i = 0; i < MAXCALLBACKS; i++) { + callback = entry->callback[i]; + if(!callback) break; + callback(client, entry->user->nick, NULL, entry->data[i]); + } + } + entry->user->flags &= ~USERFLAG_IS_ON_WHO_QUEUE; + if(entry->user->flags & USERFLAG_FREE_AFTER_WHO) { + delUser(entry->user, 1); } } free(entry); } 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; + }