]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_knock.cpp
Merge pull request #1157 from SaberUK/insp20+fix-cron-restart
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
index a6d0812feaa1c0c70680a060aebd3c43ea832297..8d2aa4543065be26e7b410ef41007c6d664cd37d 100644 (file)
-// Globops and +g support module by C.J.Edwards
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2004-2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
+ *
+ * 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)
+/* $ModDesc: Provides support for /KNOCK and channel mode +K */
+
+/** Handles the /KNOCK command
+ */
+class CommandKnock : public Command
 {
-       std::string line = "*** GLOBOPS - From " + std::string(user->nick) + ": ";
-       for (int i = 0; i < pcnt; i++)
+ public:
+       bool sendnotice;
+       bool sendnumeric;
+       CommandKnock(Module* Creator) : Command(Creator,"KNOCK", 2, 2)
        {
-               line = line + std::string(parameters[i]) + " ";
+               syntax = "<channel> <reason>";
+               Penalty = 5;
+               TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
        }
-       Srv->SendToModeMask("og",WM_AND,line);
-}
-
 
-class ModuleGlobops : public Module
-{
- public:
-       ModuleGlobops()
+       CmdResult Handle (const std::vector<std::string> &parameters, User *user)
        {
-               Srv = new Server;
-               
-               if (!Srv->AddExtendedMode('g',MT_CLIENT,true,0,0))
+               Channel* c = ServerInstance->FindChan(parameters[0]);
+               if (!c)
                {
-                       Srv->Log(DEFAULT,"*** m_globops: ERROR, failed to allocate user mode +g!");
-                       printf("Could not claim usermode +g for this module!");
-                       exit(0);
+                       user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
+                       return CMD_FAILURE;
                }
-               Srv->AddCommand("GLOBOPS",handle_globops,'o',1);
-       }
-       
-       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)
-       {
-               // 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;
+
+               if (c->HasUser(user))
+               {
+                       user->WriteNumeric(480, "%s :Can't KNOCK on %s, you are already on that channel.", user->nick.c_str(), c->name.c_str());
+                       return CMD_FAILURE;
                }
-               else
+
+               if (c->IsModeSet('K'))
                {
-                       // this mode isn't ours, we have to bail and return 0 to not handle it.
-                       return 0;
+                       user->WriteNumeric(480, "%s :Can't KNOCK on %s, +K is set.",user->nick.c_str(), c->name.c_str());
+                       return CMD_FAILURE;
                }
+
+               if (!c->IsModeSet('i'))
+               {
+                       user->WriteNumeric(480, "%s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick.c_str(), c->name.c_str());
+                       return CMD_FAILURE;
+               }
+
+               if (sendnotice)
+                       c->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name.c_str(), user->nick.c_str(), c->name.c_str(), parameters[1].c_str());
+
+               if (sendnumeric)
+                       c->WriteChannelWithServ(ServerInstance->Config->ServerName, "710 %s %s %s :is KNOCKing: %s", c->name.c_str(), c->name.c_str(), user->GetFullHost().c_str(), parameters[1].c_str());
+
+               user->WriteServ("NOTICE %s :KNOCKing on %s", user->nick.c_str(), c->name.c_str());
+               return CMD_SUCCESS;
        }
 
-       virtual void OnOper(userrec* user)
+       RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
        {
-               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.
+/** Handles channel mode +K
+ */
+class Knock : public SimpleChannelModeHandler
+{
+ public:
+       Knock(Module* Creator) : SimpleChannelModeHandler(Creator, "noknock", 'K') { }
+};
 
-class ModuleGlobopsFactory : public ModuleFactory
+class ModuleKnock : public Module
 {
+       CommandKnock cmd;
+       Knock kn;
  public:
-       ModuleGlobopsFactory()
+       ModuleKnock() : cmd(this), kn(this)
        {
        }
-       
-       ~ModuleGlobopsFactory()
+
+       void init()
        {
+               ServerInstance->Modules->AddService(kn);
+               ServerInstance->Modules->AddService(cmd);
+
+               ServerInstance->Modules->Attach(I_OnRehash, this);
+               OnRehash(NULL);
        }
-       
-       virtual Module * CreateModule()
+
+       void OnRehash(User* user)
        {
-               return new ModuleGlobops;
-       }
-       
-};
+               std::string knocknotify = ServerInstance->Config->ConfValue("knock")->getString("notify");
+               irc::string notify(knocknotify.c_str());
 
+               if (notify == "numeric")
+               {
+                       cmd.sendnotice = false;
+                       cmd.sendnumeric = true;
+               }
+               else if (notify == "both")
+               {
+                       cmd.sendnotice = true;
+                       cmd.sendnumeric = true;
+               }
+               else
+               {
+                       cmd.sendnotice = true;
+                       cmd.sendnumeric = false;
+               }
+       }
 
-extern "C" void * init_module( void )
-{
-       return new ModuleGlobopsFactory;
-}
+       virtual Version GetVersion()
+       {
+               return Version("Provides support for /KNOCK and channel mode +K", VF_OPTCOMMON | VF_VENDOR);
+       }
+};
 
+MODULE_INIT(ModuleKnock)