added experimental multi thread support
[NeonServV5.git] / src / WHOHandler.c
index ab1b0c0251d862d9b3c3cb8b0eb951952b4d1d6d..a8a55d91c693956cebd57329babecb6ec73fa829 100644 (file)
@@ -1,5 +1,5 @@
-/* WHOHandler.c - NeonServ v5.2
- * 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
@@ -21,6 +21,7 @@
 #include "ChanUser.h"
 #include "ModeNode.h"
 #include "ClientSocket.h"
+#include "IPNode.h"
 
 #define WHOQUEUETYPE_ISONQUEUE 0x01
 #define WHOQUEUETYPE_USERLIST  0x02
@@ -45,6 +46,7 @@ static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
         perror("malloc() failed");
         return NULL;
     }
+    SYNCHRONIZE(cache_sync);
     entry->next = NULL;
     if(client->whoqueue_last) {
         client->whoqueue_last->next = entry;
@@ -52,11 +54,13 @@ static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
         client->whoqueue_first = entry;
     }
     client->whoqueue_last = entry;
+    DESYNCHRONIZE(cache_sync);
     return entry;
 }
 
 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
     if(!client->whoqueue_first) return NULL;
+    SYNCHRONIZE(cache_sync);
     struct WHOQueueEntry *entry = client->whoqueue_first;
     if(freeEntry) {
         client->whoqueue_first = entry->next;
@@ -64,17 +68,21 @@ static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, i
             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) {
@@ -106,7 +114,7 @@ void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *dat
         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->type, entry->type);
     } else
         callback(bot, chan, data);
 }
@@ -141,7 +149,7 @@ void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t cal
         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->type, entry->type);
     } else
         callback(bot, chan, data);
 }
@@ -169,6 +177,10 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat
     bot = whobot;
     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
     //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;
@@ -176,6 +188,7 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat
     entry = addWHOQueueEntry(bot);
     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
     entry->user = user;
+    user->flags |= USERFLAG_IS_ON_WHO_QUEUE;
     entry->callback[0] = callback;
     int i;
     for(i = 1; i < MAXCALLBACKS; i++)
@@ -203,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;
@@ -222,17 +238,17 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
             }
         }
         
-        struct UserNode *user = getUserByNick(argv[4]);
+        struct UserNode *user = getUserByNick(argv[5]);
         struct ChanUser *chanuser;
         if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || !isBot(user))) {
-            user = createTempUser(argv[4]);
+            user = createTempUser(argv[5]);
             user->flags |= USERFLAG_ISTMPUSER;
             chan->flags |= CHANFLAG_HAVE_INVISIBLES;
             chanuser = addInvisibleChanUser(chan, user);
             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
         } else {
             if(user == NULL) {
-                user = addUser(argv[4]);
+                user = addUser(argv[5]);
             }
             if(!(chanuser = getChanUser(user, chan))) {
                 chanuser = addChanUser(chan, user);
@@ -243,13 +259,15 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
         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);
@@ -304,6 +322,10 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int
                 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);
 }