]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_knock.cpp
Only assign NewServices once the duplicate check is done.
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
index a6d0812feaa1c0c70680a060aebd3c43ea832297..f1cca891ac1f93af205d13f2a40ef279ba48d72e 100644 (file)
-// Globops and +g support module by C.J.Edwards
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
+ *   Copyright (C) 2013, 2018, 2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2013, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
+ *   Copyright (C) 2008 Craig Edwards <brain@inspircd.org>
+ *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@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 <stdio.h>
-#include <string>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
 
-/* $ModDesc: Provides support for unreal-style GLOBOPS and umode +g */
+#include "inspircd.h"
 
-Server *Srv;
-        
-void handle_globops(char **parameters, int pcnt, userrec *user)
+enum
 {
-       std::string line = "*** GLOBOPS - From " + std::string(user->nick) + ": ";
-       for (int i = 0; i < pcnt; i++)
-       {
-               line = line + std::string(parameters[i]) + " ";
-       }
-       Srv->SendToModeMask("og",WM_AND,line);
-}
+       // From UnrealIRCd.
+       ERR_CANNOTKNOCK = 480,
 
+       // From ircd-ratbox.
+       RPL_KNOCK = 710,
+       RPL_KNOCKDLVR = 711,
+       ERR_CHANOPEN = 713,
+       ERR_KNOCKONCHAN = 714
+};
 
-class ModuleGlobops : public Module
+/** Handles the /KNOCK command
+ */
+class CommandKnock : public Command
 {
+       SimpleChannelModeHandler& noknockmode;
+       ChanModeReference inviteonlymode;
+
  public:
-       ModuleGlobops()
+       bool sendnotice;
+       bool sendnumeric;
+       CommandKnock(Module* Creator, SimpleChannelModeHandler& Noknockmode)
+               : Command(Creator,"KNOCK", 2, 2)
+               , noknockmode(Noknockmode)
+               , inviteonlymode(Creator, "inviteonly")
        {
-               Srv = new Server;
-               
-               if (!Srv->AddExtendedMode('g',MT_CLIENT,true,0,0))
-               {
-                       Srv->Log(DEFAULT,"*** m_globops: ERROR, failed to allocate user mode +g!");
-                       printf("Could not claim usermode +g for this module!");
-                       exit(0);
-               }
-               Srv->AddCommand("GLOBOPS",handle_globops,'o',1);
+               syntax = "<channel> :<reason>";
+               Penalty = 5;
        }
-       
-       virtual ~ModuleGlobops()
-       {
-               delete Srv;
-       }
-       
-       virtual Version GetVersion()
-       {
-               return Version(1,0,0,1);
-       }
-       
-       virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
+
+       CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
        {
-               // check if this is our mode character...
-               if ((modechar == 'g') && (type == MT_CLIENT))
-               {
-                       // we dont actually do anything with the mode in this module -
-                       // just tell the core its been claimed and is ok to give users.                         
-                       return 1;
+               Channel* c = ServerInstance->FindChan(parameters[0]);
+               if (!c)
+               {
+                       user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
+                       return CMD_FAILURE;
                }
-               else
+
+               if (c->HasUser(user))
+               {
+                       user->WriteNumeric(ERR_KNOCKONCHAN, c->name, InspIRCd::Format("Can't KNOCK on %s, you are already on that channel.", c->name.c_str()));
+                       return CMD_FAILURE;
+               }
+
+               if (c->IsModeSet(noknockmode))
+               {
+                       user->WriteNumeric(ERR_CANNOTKNOCK, InspIRCd::Format("Can't KNOCK on %s, +K is set.", c->name.c_str()));
+                       return CMD_FAILURE;
+               }
+
+               if (!c->IsModeSet(inviteonlymode))
+               {
+                       user->WriteNumeric(ERR_CHANOPEN, c->name, InspIRCd::Format("Can't KNOCK on %s, channel is not invite only so knocking is pointless!", c->name.c_str()));
+                       return CMD_FAILURE;
+               }
+
+               if (sendnotice)
+               {
+                       c->WriteNotice(InspIRCd::Format("User %s is KNOCKing on %s (%s)", user->nick.c_str(), c->name.c_str(), parameters[1].c_str()));
+                       user->WriteNotice("KNOCKing on " + c->name);
+               }
+
+               if (sendnumeric)
                {
-                       // this mode isn't ours, we have to bail and return 0 to not handle it.
-                       return 0;
+                       Numeric::Numeric numeric(RPL_KNOCK);
+                       numeric.push(c->name).push(user->GetFullHost()).push("is KNOCKing: " + parameters[1]);
+
+                       ClientProtocol::Messages::Numeric numericmsg(numeric, c->name);
+                       c->Write(ServerInstance->GetRFCEvents().numeric, numericmsg);
+
+                       user->WriteNumeric(RPL_KNOCKDLVR, c->name, "KNOCKing on channel");
                }
+
+               return CMD_SUCCESS;
        }
 
-       virtual void OnOper(userrec* user)
+       RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
        {
-               char* modes[2];                 // only two parameters
-               modes[0] = user->nick;          // first parameter is the nick
-               modes[1] = "+g";                // second parameter is the mode
-               Srv->SendMode(modes,2,user);    // send these, forming the command "MODE <nick> +g"
+               return ROUTE_OPT_BCAST;
        }
-
 };
 
-// stuff down here is the module-factory stuff. For basic modules you can ignore this.
-
-class ModuleGlobopsFactory : public ModuleFactory
+class ModuleKnock : public Module
 {
+       SimpleChannelModeHandler kn;
+       CommandKnock cmd;
+
  public:
-       ModuleGlobopsFactory()
+       ModuleKnock()
+               : kn(this, "noknock", 'K')
+               , cmd(this, kn)
        {
        }
-       
-       ~ModuleGlobopsFactory()
+
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
+               std::string knocknotify = ServerInstance->Config->ConfValue("knock")->getString("notify");
+               if (stdalgo::string::equalsci(knocknotify, "numeric"))
+               {
+                       cmd.sendnotice = false;
+                       cmd.sendnumeric = true;
+               }
+               else if (stdalgo::string::equalsci(knocknotify, "both"))
+               {
+                       cmd.sendnotice = true;
+                       cmd.sendnumeric = true;
+               }
+               else
+               {
+                       cmd.sendnotice = true;
+                       cmd.sendnumeric = false;
+               }
        }
-       
-       virtual Module * CreateModule()
+
+       Version GetVersion() CXX11_OVERRIDE
        {
-               return new ModuleGlobops;
+               return Version("Adds the /KNOCK command which allows users to request access to an invite-only channel and channel mode K (noknock) which allows channels to disable usage of this command.", VF_OPTCOMMON | VF_VENDOR);
        }
-       
 };
 
-
-extern "C" void * init_module( void )
-{
-       return new ModuleGlobopsFactory;
-}
-
+MODULE_INIT(ModuleKnock)