]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_kicknorejoin.cpp
Add a config option for exempting classes from connflood.
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
index 97d88786ff13640247a44e99b5d7abcf8df8450d..cc899282fccd7447a5fb36ff6b5dbfc9821cf3aa 100644 (file)
@@ -1 +1,176 @@
-/*       +------------------------------------+\r *       | Inspire Internet Relay Chat Daemon |\r *       +------------------------------------+\r *\r *  InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r *            the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "inspircd.h"\r#include <sstream>\r#include "users.h"\r#include "channels.h"\r#include "modules.h"\r\r/* $ModDesc: Provides channel mode +J (delay rejoin after kick) */\r\rinline int strtoint(const std::string &str)\r{\r std::istringstream ss(str);\r    int result;\r    ss >> result;\r  return result;\r}\r\rtypedef std::map<userrec*, time_t> delaylist;\r\r/** Handles channel mode +J\r */\rclass KickRejoin : public ModeHandler\r{\r public:\r      KickRejoin(InspIRCd* Instance) : ModeHandler(Instance, 'J', 1, 0, false, MODETYPE_CHANNEL, false) { }\r\r ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)\r       {\r              if (channel->IsModeSet('J'))\r                   return std::make_pair(true, channel->GetModeParameter('J'));\r           else\r                   return std::make_pair(false, parameter);\r       } \r\r    bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)\r        {\r              /* When TS is equal, the alphabetically later one wins */\r              return (their_param < our_param);\r      }\r      \r       ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)\r {\r              if (!adding)\r           {\r                      // Taking the mode off, we need to clean up.\r                   delaylist* dl;\r                 \r                       if (channel->GetExt("norejoinusers", dl))\r                      {\r                              DELETE(dl);\r                            channel->Shrink("norejoinusers");\r                      }\r                      \r                       if (!channel->IsModeSet('J'))\r                  {\r                              return MODEACTION_DENY;\r                        }\r                      else\r                   {\r                              channel->SetMode('J', false);\r                          return MODEACTION_ALLOW;\r                       }\r              }\r              else if (atoi(parameter.c_str()) > 0)\r          {\r                      if (!channel->IsModeSet('J'))\r                  {\r                              parameter = ConvToStr(atoi(parameter.c_str()));\r                                channel->SetModeParam('J', parameter.c_str(), adding);\r                         channel->SetMode('J', adding);\r                         return MODEACTION_ALLOW;\r                       }\r                      else\r                   {\r                              std::string cur_param = channel->GetModeParameter('J');\r                                if (cur_param == parameter)\r                            {\r                                      // mode params match, don't change mode\r                                        return MODEACTION_DENY;\r                                }\r                              else\r                           {\r                                      // new mode param, replace old with new\r                                        parameter = ConvToStr(atoi(parameter.c_str()));\r                                        cur_param = ConvToStr(atoi(cur_param.c_str()));\r                                        if (parameter != "0")\r                                  {\r                                              channel->SetModeParam('J', cur_param.c_str(), false);\r                                          channel->SetModeParam('J', parameter.c_str(), adding);\r                                         return MODEACTION_ALLOW;\r                                       }\r                                      else\r                                   {\r                                              /* Fix to jamie's fix, dont allow +J 0 on the new value! */\r                                            return MODEACTION_DENY;\r                                        }\r                              }\r                      }\r              }\r              else\r           {\r                      return MODEACTION_DENY;\r                }\r      }\r};\r\rclass ModuleKickNoRejoin : public Module\r{\r       \r       KickRejoin* kr;\r        \rpublic:\r \r     ModuleKickNoRejoin(InspIRCd* Me)\r               : Module(Me)\r   {\r              \r               kr = new KickRejoin(ServerInstance);\r           if (!ServerInstance->AddMode(kr, 'J'))\r                 throw ModuleException("Could not add new modes!");\r     }\r\r     virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)\r {\r              if (chan)\r              {\r                      delaylist* dl;\r                 if (chan->GetExt("norejoinusers", dl))\r                 {\r                              std::vector<userrec*> itemstoremove;\r                   \r                               for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++)\r                                {\r                                      if (iter->second > time(NULL))\r                                 {\r                                              if (iter->first == user)                                        \r                                               {\r                                                      user->WriteServ( "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);\r                                                     return 1;\r                                              }\r                                      }\r                                      else\r                                   {\r                                              // Expired record, remove.\r                                             itemstoremove.push_back(iter->first);\r                                  }\r                              }\r                              \r                               for (unsigned int i = 0; i < itemstoremove.size(); i++)\r                                        dl->erase(itemstoremove[i]);\r                                                                                                                                   \r                               if (!dl->size())\r                               {\r                                      // Now it's empty..\r                                    DELETE(dl);\r                                    chan->Shrink("norejoinusers");\r                         }\r                      }\r              }\r              return 0;\r      }\r              \r       virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)\r        {\r              if (chan->IsModeSet('J') && (source != user))\r          {\r                      delaylist* dl;\r                 if (!chan->GetExt("norejoinusers", dl))\r                        {\r                              dl = new delaylist;\r                            chan->Extend("norejoinusers", dl);\r                     }\r                      (*dl)[user] = time(NULL) + strtoint(chan->GetModeParameter('J'));\r              }\r      }\r      \r       virtual void OnChannelDelete(chanrec* chan)\r    {\r              delaylist* dl;\r                 \r               if (chan->GetExt("norejoinusers", dl))\r         {\r                      DELETE(dl);\r                    chan->Shrink("norejoinusers");\r         }\r      }\r      \r       virtual void OnCleanup(int target_type, void* item)\r    {\r              if(target_type == TYPE_CHANNEL)\r                        OnChannelDelete((chanrec*)item);\r       }\r\r     virtual void Implements(char* List)\r    {\r              List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1;\r  }\r\r     virtual ~ModuleKickNoRejoin()\r  {\r              ServerInstance->Modes->DelMode(kr);\r            DELETE(kr);\r    }\r      \r       virtual Version GetVersion()\r   {\r              return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);\r        }\r};\r\r\rMODULE_INIT(ModuleKickNoRejoin)\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2018 A_D <A-UNDERSCORE-D@users.noreply.github.com>
+ *   Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
+ *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
+ *
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "inspircd.h"
+#include "modules/invite.h"
+
+enum
+{
+       // From RFC 2182.
+       ERR_UNAVAILRESOURCE = 437
+};
+
+
+class KickRejoinData
+{
+       struct KickedUser
+       {
+               std::string uuid;
+               time_t expire;
+
+               KickedUser(User* user, unsigned int Delay)
+                       : uuid(user->uuid)
+                       , expire(ServerInstance->Time() + Delay)
+               {
+               }
+       };
+
+       typedef std::vector<KickedUser> KickedList;
+
+       mutable KickedList kicked;
+
+ public:
+       const unsigned int delay;
+
+       KickRejoinData(unsigned int Delay) : delay(Delay) { }
+
+       bool canjoin(LocalUser* user) const
+       {
+               for (KickedList::iterator i = kicked.begin(); i != kicked.end(); )
+               {
+                       KickedUser& rec = *i;
+                       if (rec.expire > ServerInstance->Time())
+                       {
+                               if (rec.uuid == user->uuid)
+                                       return false;
+                               ++i;
+                       }
+                       else
+                       {
+                               // Expired record, remove.
+                               stdalgo::vector::swaperase(kicked, i);
+                               if (kicked.empty())
+                                       break;
+                       }
+               }
+               return true;
+       }
+
+       void add(User* user)
+       {
+               // One user can be in the list multiple times if the user gets kicked, force joins
+               // (skipping OnUserPreJoin) and gets kicked again, but that's okay because canjoin()
+               // works correctly in this case as well
+               kicked.push_back(KickedUser(user, delay));
+       }
+};
+
+/** Handles channel mode +J
+ */
+class KickRejoin : public ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >
+{
+       const unsigned int max;
+ public:
+       KickRejoin(Module* Creator)
+               : ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >(Creator, "kicknorejoin", 'J')
+               , max(60)
+       {
+               syntax = "<seconds>";
+       }
+
+       ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
+       {
+               unsigned int v = ConvToNum<unsigned int>(parameter);
+               if (v <= 0)
+               {
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
+                       return MODEACTION_DENY;
+               }
+
+               if (IS_LOCAL(source) && v > max)
+                       v = max;
+
+               ext.set(channel, new KickRejoinData(v));
+               return MODEACTION_ALLOW;
+       }
+
+       void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out)
+       {
+               out.append(ConvToStr(krd->delay));
+       }
+
+       std::string GetModuleSettings() const
+       {
+               return ConvToStr(max);
+       }
+};
+
+class ModuleKickNoRejoin : public Module
+{
+       KickRejoin kr;
+       Invite::API invapi;
+
+public:
+       ModuleKickNoRejoin()
+               : kr(this)
+               , invapi(this)
+       {
+       }
+
+       ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
+       {
+               if (chan)
+               {
+                       const KickRejoinData* data = kr.ext.get(chan);
+                       if ((data) && !invapi->IsInvited(user, chan) && (!data->canjoin(user)))
+                       {
+                               user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J is set)", data->delay));
+                               return MOD_RES_DENY;
+                       }
+               }
+               return MOD_RES_PASSTHRU;
+       }
+
+       void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
+       {
+               if ((!IS_LOCAL(memb->user)) || (source == memb->user))
+                       return;
+
+               KickRejoinData* data = kr.ext.get(memb->chan);
+               if (data)
+               {
+                       data->add(memb->user);
+               }
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Adds channel mode J (kicknorejoin) which prevents users from rejoining after being kicked from a channel.", VF_VENDOR | VF_COMMON, kr.GetModuleSettings());
+       }
+};
+
+MODULE_INIT(ModuleKickNoRejoin)