prevent excessive WHO usage by checking if a WHO is really necessary
[NeonServV5.git] / src / WHOHandler.c
index a296298933395d51e11843fb376b478663067484..25d2467f8acef2f17e03e369a395597fc2ffeff0 100644 (file)
@@ -19,6 +19,7 @@
 #include "ChanNode.h"
 #include "UserNode.h"
 #include "ChanUser.h"
+#include "ModeNode.h"
 #include "ClientSocket.h"
 
 #define WHOQUEUETYPE_ISONQUEUE 0x01
@@ -86,13 +87,26 @@ 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 = callback;
+        entry->data = data;
+        putsock(bot, "WHO %s,%d %%tuhnaf,%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) {
@@ -102,13 +116,27 @@ void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t call
             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) {
+        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 = callback;
+        entry->data = data;
+        putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
+    } else
+        callback(bot, chan, data);
 }
 
 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
@@ -118,6 +146,11 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat
             break;
     }
     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) {
+        callback(bot, user->nick, user, data);
+        return;
+    }
     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
     entry->user = user;
@@ -180,6 +213,7 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
             }
         }
         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
+        user->last_who = time(0);
         if(!*user->ident)
             strcpy(user->ident, argv[2]);
         if(!*user->host)
@@ -191,6 +225,7 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
     } else if(type & WHOQUEUETYPE_USERAUTH) {
         //: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;