]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_cban.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_cban.cpp
index 5491f5e3abb6d941392fe86ea4b8c829de1ba8f7..c7960c836d0ca6a5a77d7143440b21b3c7d59c5c 100644 (file)
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *   Copyright (C) 2020 Michael <michaelhazell@hotmail.com>
+ *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
+ *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2013, 2016 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009 John Brooks <special@inspircd.org>
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
+ *   Copyright (C) 2005, 2008 Robin Burchell <robin+git@viroteck.net>
  *
- * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ * 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 <algorithm>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "configreader.h"
+
 #include "inspircd.h"
+#include "xline.h"
+#include "modules/stats.h"
+
+enum
+{
+       // InspIRCd-specific.
+       ERR_BADCHANNEL = 926
+};
 
-/* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
+// Compatibility: Use glob matching?
+// InspIRCd versions 3.7.0 and below use only exact matching
+static bool glob = false;
 
 /** Holds a CBAN item
  */
-class CBan : public classbase
+class CBan : public XLine
 {
+private:
+       std::string matchtext;
+
 public:
-       irc::string chname;
-       std::string set_by;
-       time_t set_on;
-       long length;
-       std::string reason;
+       CBan(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ch)
+               : XLine(s_time, d, src, re, "CBAN")
+               , matchtext(ch)
+       {
+       }
 
-       CBan()
+       // XXX I shouldn't have to define this
+       bool Matches(User* u) CXX11_OVERRIDE
        {
+               return false;
        }
 
-       CBan(irc::string cn, std::string sb, time_t so, long ln, std::string rs) : chname(cn), set_by(sb), set_on(so), length(ln), reason(rs)
+       bool Matches(const std::string& s) CXX11_OVERRIDE
        {
+               if (glob)
+                       return InspIRCd::Match(s, matchtext);
+               else
+                       return irc::equals(matchtext, s);
+       }
+
+       const std::string& Displayable() CXX11_OVERRIDE
+       {
+               return matchtext;
        }
 };
 
-bool CBanComp(const CBan &ban1, const CBan &ban2);
+/** An XLineFactory specialized to generate cban pointers
+ */
+class CBanFactory : public XLineFactory
+{
+ public:
+       CBanFactory() : XLineFactory("CBAN") { }
 
-typedef std::vector<CBan> cbanlist;
+       /** Generate a CBAN
+       */
+       XLine* Generate(time_t set_time, unsigned long duration, const std::string& source, const std::string& reason, const std::string& xline_specific_mask) CXX11_OVERRIDE
+       {
+               return new CBan(set_time, duration, source, reason, xline_specific_mask);
+       }
 
-/* cbans is declared here, as our type is right above. Don't try move it. */
-cbanlist cbans;
+       bool AutoApplyToUserList(XLine* x) CXX11_OVERRIDE
+       {
+               return false; // No, we apply to channels.
+       }
+};
 
 /** Handle /CBAN
  */
-class cmd_cban : public command_t
+class CommandCBan : public Command
 {
  public:
-       cmd_cban(InspIRCd* Me) : command_t(Me, "CBAN", 'o', 1)
+       CommandCBan(Module* Creator) : Command(Creator, "CBAN", 1, 3)
        {
-               this->source = "m_cban.so";
-               this->
-               syntax = "<channel> [<duration> :<reason>]";
+               flags_needed = 'o';
+               this->syntax = "<channelmask> [<duration> [:<reason>]]";
        }
 
-       CmdResult Handle(const char** parameters, int pcnt, userrec *user)
+       CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
        {
                /* syntax: CBAN #channel time :reason goes here */
                /* 'time' is a human-readable timestring, like 2d3h2s. */
 
-               if(pcnt == 1)
+               if (parameters.size() == 1)
                {
-                       /* form: CBAN #channel removes a CBAN */
-                       for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
+                       std::string reason;
+
+                       if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "CBAN", reason, user))
                        {
-                               if (parameters[0] == iter->chname)
-                               {
-                                       unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
-                                       user->WriteServ( "386 %s %s :Removed CBAN with %lu seconds left before expiry (%s)", user->nick, iter->chname.c_str(), remaining, iter->reason.c_str());
-                                       cbans.erase(iter);
-                                       break;
-                               }
+                               ServerInstance->SNO->WriteGlobalSno('x', "%s removed CBan on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
+                       }
+                       else
+                       {
+                               user->WriteNotice("*** CBan " + parameters[0] + " not found on the list.");
+                               return CMD_FAILURE;
                        }
                }
-               else if (pcnt >= 2)
+               else
                {
-                       /* full form to add a CBAN */
-                       if (ServerInstance->IsChannel(parameters[0]))
+                       // Adding - XXX todo make this respect <insane> tag perhaps..
+                       unsigned long duration;
+                       if (!InspIRCd::Duration(parameters[1], duration))
+                       {
+                               user->WriteNotice("*** Invalid duration for CBan.");
+                               return CMD_FAILURE;
+                       }
+                       const char *reason = (parameters.size() > 2) ? parameters[2].c_str() : "No reason supplied";
+                       CBan* r = new CBan(ServerInstance->Time(), duration, user->nick.c_str(), reason, parameters[0].c_str());
+
+                       if (ServerInstance->XLines->AddLine(r, user))
                        {
-                               // parameters[0] = #channel
-                               // parameters[1] = 1h3m2s
-                               // parameters[2] = Tortoise abuser
-                               long length = ServerInstance->Duration(parameters[1]);
-                               std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
-                               
-                               cbans.push_back(CBan(parameters[0], user->nick, ServerInstance->Time(), length, reason));
-                                       
-                               std::sort(cbans.begin(), cbans.end(), CBanComp);
-                               
-                               if(length > 0)
+                               if (!duration)
                                {
-                                       user->WriteServ( "385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
-                                       ServerInstance->WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
+                                       ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent CBan for %s: %s", user->nick.c_str(), parameters[0].c_str(), reason);
                                }
                                else
                                {
-                                       user->WriteServ( "385 %s %s :Added permenant channel ban (%s)", user->nick, parameters[0], reason.c_str());
-                                       ServerInstance->WriteOpers("*** %s added permenant channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
+                                       ServerInstance->SNO->WriteGlobalSno('x', "%s added timed CBan for %s, expires in %s (on %s): %s",
+                                               user->nick.c_str(), parameters[0].c_str(), InspIRCd::DurationString(duration).c_str(),
+                                               InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), reason);
                                }
                        }
                        else
                        {
-                               user->WriteServ( "403 %s %s :Invalid channel name", user->nick, parameters[0]);
+                               delete r;
+                               user->WriteNotice("*** CBan for " + parameters[0] + " already exists");
                                return CMD_FAILURE;
                        }
                }
-
                return CMD_SUCCESS;
        }
-};
 
-bool CBanComp(const CBan &ban1, const CBan &ban2)
-{
-       return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
-}
+       RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
+       {
+               if (IS_LOCAL(user))
+                       return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
+
+               return ROUTE_BROADCAST;
+       }
+};
 
-class ModuleCBan : public Module
+class ModuleCBan : public Module, public Stats::EventListener
 {
-       cmd_cban* mycommand;
-       
+       CommandCBan mycommand;
+       CBanFactory f;
 
  public:
-       ModuleCBan(InspIRCd* Me) : Module::Module(Me)
+       ModuleCBan()
+               : Stats::EventListener(this)
+               , mycommand(this)
        {
-               
-               mycommand = new cmd_cban(Me);
-               ServerInstance->AddCommand(mycommand);
        }
 
-       void Implements(char* List)
+       void init() CXX11_OVERRIDE
        {
-               List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
-       }
-       
-       virtual int OnStats(char symbol, userrec* user, string_list &results)
-       {
-               ExpireBans();
-       
-               if(symbol == 'C')
-               {
-                       for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
-                       {
-                               unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
-                               results.push_back(std::string(ServerInstance->Config->ServerName)+" 210 "+user->nick+" "+iter->chname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
-                       }
-               }
-               
-               return 0;
+               ServerInstance->XLines->RegisterFactory(&f);
        }
 
-       virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
+       ~ModuleCBan()
        {
-               ExpireBans();
-       
-               /* check cbans in here, and apply as necessary. */
-               for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
-               {
-                       if(iter->chname == cname && !user->modes[UM_OPERATOR])
-                       {
-                               // Channel is banned.
-                               user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
-                               ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
-                               return 1;
-                       }
-               }
-               return 0;
-       }
-       
-       virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
-       {
-               for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
-               {
-                       proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
-               }
-       }
-       
-       virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
-       {
-               if((target_type == TYPE_OTHER) && (extname == "cban"))
-               {
-                       cbans.push_back(DecodeCBan(extdata));
-                       std::sort(cbans.begin(), cbans.end(), CBanComp);
-               }
+               ServerInstance->XLines->DelAll("CBAN");
+               ServerInstance->XLines->UnregisterFactory(&f);
        }
 
-       virtual ~ModuleCBan()
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
-       }
-       
-       virtual Version GetVersion()
-       {
-               return Version(1,1,0,1,VF_VENDOR,API_VERSION);
-       }
+               ConfigTag* tag = ServerInstance->Config->ConfValue("cban");
 
-       std::string EncodeCBan(const CBan &ban)
-       {
-               std::ostringstream stream;      
-               stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
-               return stream.str();    
+               // XXX: Consider changing default behavior on the next major version
+               glob = tag->getBool("glob", false);
        }
 
-       CBan DecodeCBan(const std::string &data)
+       ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
        {
-               CBan res;
-               std::istringstream stream(data);
-               stream >> res.chname;
-               stream >> res.set_by;
-               stream >> res.set_on;
-               stream >> res.length;
-               res.reason = stream.str();
-       
-               return res;
+               if (stats.GetSymbol() != 'C')
+                       return MOD_RES_PASSTHRU;
+
+               ServerInstance->XLines->InvokeStats("CBAN", stats);
+               return MOD_RES_DENY;
        }
 
-       void ExpireBans()
+       ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
        {
-               bool go_again = true;
+               XLine *rl = ServerInstance->XLines->MatchesLine("CBAN", cname);
 
-               while (go_again)
+               if (rl)
                {
-                       go_again = false;
-       
-                       for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
-                       {
-                               /* 0 == permanent, don't mess with them! -- w00t */
-                               if (iter->length != 0)
-                               {
-                                       if (iter->set_on + iter->length <= ServerInstance->Time())
-                                       {
-                                               ServerInstance->WriteOpers("*** %li second CBAN on %s (%s) set %u seconds ago expired", iter->length, iter->chname.c_str(), iter->reason.c_str(), ServerInstance->Time() - iter->set_on);
-                                               cbans.erase(iter);
-                                               go_again = true;
-                                       }
-                               }
-       
-                               if (go_again == true)
-                                       break;
-                       }
+                       // Channel is banned.
+                       user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is CBANed: %s", cname.c_str(), rl->reason.c_str()));
+                       ServerInstance->SNO->WriteGlobalSno('a', "%s tried to join %s which is CBANed (%s)",
+                               user->nick.c_str(), cname.c_str(), rl->reason.c_str());
+                       return MOD_RES_DENY;
                }
-       }
-};
 
-class ModuleCBanFactory : public ModuleFactory
-{
- public:
-       ModuleCBanFactory()
-       {
+               return MOD_RES_PASSTHRU;
        }
-       
-       ~ModuleCBanFactory()
-       {
-       }
-       
-       virtual Module * CreateModule(InspIRCd* Me)
+
+       Version GetVersion() CXX11_OVERRIDE
        {
-               return new ModuleCBan(Me);
+               return Version("Adds the /CBAN command which allows server operators to prevent channels matching a glob from being created.", VF_COMMON | VF_VENDOR, glob ? "glob" : "");
        }
-       
 };
 
-
-extern "C" void * init_module( void )
-{
-       return new ModuleCBanFactory;
-}
+MODULE_INIT(ModuleCBan)