Merge branch 'development'
[NeonServV5.git] / src / WHOHandler.c
index 2c46b25dce873a86a31cf51ba02a4ed72aeb5b71..5db3db6a3d58be68ad50a402308033f61d004c6c 100644 (file)
@@ -1,5 +1,5 @@
-/* WHOHandler.c - NeonServ v5.2
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* WHOHandler.c - NeonServ v5.6
+ * 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
@@ -21,6 +21,9 @@
 #include "ChanUser.h"
 #include "ModeNode.h"
 #include "ClientSocket.h"
+#include "IPNode.h"
+#include "modules.h"
+#include "log.h"
 
 #define WHOQUEUETYPE_ISONQUEUE 0x01
 #define WHOQUEUETYPE_USERLIST  0x02
 #define WHOQUEUETYPE_CHECKTYPE 0x07
 #define WHOQUEUETYPE_FOUND     0x08
 
-#define MAXCALLBACKS 3
+#define MAXCALLBACKS 10
 
 struct WHOQueueEntry {
     char type;
-    struct ClientSocket *client;
+    int whoid;
     struct ChanNode *chan;
     struct UserNode *user;
     struct WHOQueueEntry *next;
     void *callback[MAXCALLBACKS];
+    int module_id[MAXCALLBACKS];
     void *data[MAXCALLBACKS];
 };
 
-static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
+static int checkWHOID(struct ClientSocket *client, int whoid) {
+    struct WHOQueueEntry *entry;
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->whoid == whoid)
+            return 1;
+    }
+    return 0;
+}
 
 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
+    SYNCHRONIZE(whohandler_sync);
+    int whoid = 0;
+    do {
+        whoid++;
+    } while(checkWHOID(client, whoid) && whoid < 1000);
+    if(whoid == 1000) {
+        DESYNCHRONIZE(whohandler_sync);
+        return NULL;
+    }
     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->client = client;
-    if(last_entry) {
-        last_entry->next = entry;
-        last_entry = entry;
+    entry->whoid = whoid;
+    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(whohandler_sync);
     return entry;
 }
 
-static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
-    if(!first_entry) return NULL;
+static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int whoid, int freeEntry) {
+    if(!client->whoqueue_first) return NULL;
+    SYNCHRONIZE(whohandler_sync);
     struct WHOQueueEntry *entry;
-    for(entry = first_entry; entry; entry = entry->next) {
-        if(entry->client == client)
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->whoid == whoid)
             break;
     }
-    if(entry == NULL) return NULL;
+    if(!entry) {
+        DESYNCHRONIZE(whohandler_sync);
+        return NULL;
+    }
     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;
         }
     }
+    DESYNCHRONIZE(whohandler_sync);
     return entry;
 }
 
-void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
+void clear_whoqueue(struct ClientSocket *client) {
+    if(!client->whoqueue_first) return;
+    SYNCHRONIZE(whohandler_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(whohandler_sync);
+}
+
+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))
@@ -105,18 +140,19 @@ 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;
         entry->data[0] = data;
         for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
-        putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
+        putsock(bot, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
     } else
         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))
@@ -140,47 +176,56 @@ 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;
         entry->data[0] = data;
         for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
-        putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
+        putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
     } else
         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;
-    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->module_id[i] = module_id;
+                        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) {
+    if(!is_valid_nick(user->nick)) {
+        callback(bot, user->nick, NULL, data);
+        return;
+    }
+    if((user->flags & (USERFLAG_ISAUTHED | 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;
+    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;
@@ -188,18 +233,28 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat
     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);
+    putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->whoid, entry->whoid);
 }
 
+static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
+    _recv_whohandler_354(client, argv, argc);
+}
+
+static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
     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;
-    if(type & WHOQUEUETYPE_USERLIST) {
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
+    if(entry == NULL) return;
+    #ifdef HAVE_THREADS
+    unsigned int tid = (unsigned int) pthread_self_tid();
+    while(!clientsocket_parseorder_top(tid)) {
+        usleep(1000); //1ms
+    }
+    #endif
+    if(entry->type & WHOQUEUETYPE_USERLIST) {
         if(argc < 7) return;
         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
         struct ChanNode *chan = entry->chan;
@@ -207,11 +262,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;
@@ -226,34 +284,36 @@ 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 || (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;
-            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((entry->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);
@@ -264,21 +324,33 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
         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]);
         }
     }
 }
 
+static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
+    _recv_whohandler_315(client, argv, argc);
+}
+
+static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
     if(argc < 2) return;
     char *typestr = strstr(argv[1], ",");
     if(!typestr) return;
     typestr++;
     int type = atoi(typestr);
-    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
-    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
-    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
-    if(type & WHOQUEUETYPE_USERLIST) {
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
+    if(entry == NULL) return;
+    #ifdef HAVE_THREADS
+    unsigned int tid = (unsigned int) pthread_self_tid();
+    while(!clientsocket_parseorder_top(tid)) {
+        usleep(1000); //1ms
+    }
+    #endif
+    getNextWHOQueueEntry(client, type, 1);
+    if(entry->type & WHOQUEUETYPE_USERLIST) {
         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
         userlist_callback_t *callback;
@@ -286,38 +358,38 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int
         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
             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) {
+    } else if(entry->type & WHOQUEUETYPE_USERAUTH) {
         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
             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]);
+                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;
+        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;
+    
 }