]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_safelist.cpp
We were already sending FMODE +nt after each channel creation to keep services happy...
[user/henk/code/inspircd.git] / src / modules / m_safelist.cpp
index 1bdfb97bd50c11bb40c082ea172c7a7b69401044..174b848331df136364217fc97297b2a9cfe79316 100644 (file)
  *
  * ---------------------------------------------------
  */
+
+#include "inspircd.h" 
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
-#include "configreader.h"
-#include "inspircd.h"
 #include "wildcard.h"
 
 /** Holds a users m_safelist state
@@ -27,9 +26,11 @@ class ListData : public classbase
        long list_position;
        bool list_ended;
        const std::string glob;
+       int minusers;
+       int maxusers;
 
        ListData() : list_start(0), list_position(0), list_ended(false) {};
-       ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
+       ListData(long pos, time_t t, const std::string &pattern, int mi, int ma) : list_start(t), list_position(pos), list_ended(false), glob(pattern), minusers(mi), maxusers(ma) {};
 };
 
 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
@@ -38,8 +39,10 @@ class ModuleSafeList : public Module
 {
        time_t ThrottleSecs;
        size_t ServerNameSize;
+       int global_listing;
+       int LimitList;
  public:
-       ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
+       ModuleSafeList(InspIRCd* Me) : Module(Me)
        {
                OnRehash(NULL, "");
        }
@@ -52,7 +55,9 @@ class ModuleSafeList : public Module
        {
                ConfigReader MyConf(ServerInstance);
                ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
+               LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
                ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
+               global_listing = 0;
        }
  
        virtual Version GetVersion()
@@ -88,6 +93,16 @@ class ModuleSafeList : public Module
         */
        int HandleList(const char** parameters, int pcnt, userrec* user)
        {
+               int minusers = 0, maxusers = 0;
+
+               if (global_listing >= LimitList)
+               {
+                       user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick);
+                       user->WriteServ("321 %s Channel :Users Name",user->nick);
+                       user->WriteServ("323 %s :End of channel list.",user->nick);
+                       return 1;
+               }
+
                /* First, let's check if the user is currently /list'ing */
                ListData *ld;
                user->GetExt("safelist_cache", ld);
@@ -99,8 +114,21 @@ class ModuleSafeList : public Module
                }
 
                /* Work around mIRC suckyness. YOU SUCK, KHALED! */
-               if ((pcnt == 1) && (*parameters[0] == '<'))
-                       pcnt = 0;
+               if (pcnt == 1)
+               {
+                       if (*parameters[0] == '<')
+                       {
+                               maxusers = atoi(parameters[0]+1);
+                               ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
+                               pcnt = 0;
+                       }
+                       else if (*parameters[0] == '>')
+                       {
+                               minusers = atoi(parameters[0]+1);
+                               ServerInstance->Log(DEBUG,"Min users: %d", minusers);
+                               pcnt = 0;
+                       }
+               }
 
                time_t* last_list_time;
                user->GetExt("safelist_last", last_list_time);
@@ -117,11 +145,12 @@ class ModuleSafeList : public Module
                        DELETE(last_list_time);
                        user->Shrink("safelist_last");
                }
+
  
                /*
                 * start at channel 0! ;)
                 */
-               ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
+               ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
                user->Extend("safelist_cache", ld);
 
                time_t* llt = new time_t;
@@ -130,6 +159,8 @@ class ModuleSafeList : public Module
 
                user->WriteServ("321 %s Channel :Users Name",user->nick);
 
+               global_listing++;
+
                return 1;
        }
 
@@ -145,10 +176,20 @@ class ModuleSafeList : public Module
                        {
                                chan = ServerInstance->GetChannelIndex(ld->list_position);
                                bool has_user = (chan && chan->HasUser(user));
+                               long users = chan ? chan->GetUserCounter() : 0;
+
+                               bool too_few = (ld->minusers && (users <= ld->minusers));
+                               bool too_many = (ld->maxusers && (users >= ld->maxusers));
+
+                               if (chan && (too_many || too_few))
+                               {
+                                       ld->list_position++;
+                                       continue;
+                               }
+
                                if ((chan) && (chan->modes[CM_PRIVATE]))
                                {
-                                       bool display = match(chan->name, ld->glob.c_str());
-                                       long users = chan->GetUserCounter();
+                                       bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
                                        if ((users) && (display))
                                        {
                                                int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
@@ -158,8 +199,7 @@ class ModuleSafeList : public Module
                                }
                                else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
                                {
-                                       bool display = match(chan->name, ld->glob.c_str());
-                                       long users = chan->GetUserCounter();
+                                       bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
                                        if ((users) && (display))
                                        {
                                                int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
@@ -185,6 +225,7 @@ class ModuleSafeList : public Module
                        {
                                user->Shrink("safelist_cache");
                                DELETE(ld);
+                               global_listing--;
                        }
                }
        }
@@ -200,6 +241,7 @@ class ModuleSafeList : public Module
                        {
                                u->Shrink("safelist_cache");
                                DELETE(ld);
+                               global_listing--;
                        }
                        time_t* last_list_time;
                        u->GetExt("safelist_last", last_list_time);
@@ -216,7 +258,7 @@ class ModuleSafeList : public Module
                output.append(" SAFELIST");
        }
 
-       virtual void OnUserQuit(userrec* user, const std::string &message)
+       virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
        {
                this->OnCleanup(TYPE_USER,user);
        }
@@ -242,7 +284,7 @@ class ModuleSafeListFactory : public ModuleFactory
  
 };
  
-extern "C" void * init_module( void )
+extern "C" DllExport void * init_module( void )
 {
        return new ModuleSafeListFactory;
 }