]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_cap.cpp
Fix CAP REQ to be atomic like the standard dictates.
[user/henk/code/inspircd.git] / src / modules / m_cap.cpp
index 0a36a9b6af666c72d294262052d6d968212fb7b6..37478243faff19eb27cc3eb9264c9ed7859fa329 100644 (file)
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ * 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 free but copyrighted software; see
- *            the file COPYING for details.
+ * 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 "m_cap.h"
 
-/* $ModDesc: Provides a pointless /dalinfo command, demo module */
+/* $ModDesc: Provides the CAP negotiation mechanism seen in ratbox-derived ircds */
 
 /*
- *          std::string type;
- *                   std::vector<std::string> parameters;
- *                            User* user;
- *                                     Module* creator;
- */
+CAP LS
+:alfred.staticbox.net CAP * LS :multi-prefix sasl
+CAP REQ :multi-prefix
+:alfred.staticbox.net CAP * ACK :multi-prefix
+CAP CLEAR
+:alfred.staticbox.net CAP * ACK :-multi-prefix
+CAP REQ :multi-prefix
+:alfred.staticbox.net CAP * ACK :multi-prefix
+CAP LIST
+:alfred.staticbox.net CAP * LIST :multi-prefix
+CAP END
+*/
 
 /** Handle /CAP
  */
 class CommandCAP : public Command
 {
-       Module* Creator;
  public:
-       /* Command 'dalinfo', takes no parameters and needs no special modes */
-       CommandCAP (InspIRCd* Instance, Module* mod) : Command(Instance,"CAP", 0, 1, true), Creator(mod)
+       LocalIntExt reghold;
+       CommandCAP (Module* mod) : Command(mod, "CAP", 1),
+               reghold("CAP_REGHOLD", mod)
        {
-               this->source = "m_cap.so";
+               works_before_reg = true;
        }
 
-       CmdResult Handle (const char* const* parameters, int pcnt, User *user)
+       CmdResult Handle (const std::vector<std::string> &parameters, User *user)
        {
-               irc::string subcommand = parameters[0];
+               irc::string subcommand = parameters[0].c_str();
+
                if (subcommand == "REQ")
                {
-                       CapData Data;
-                       Data.type = parameters[1];
-                       Data.user = user;
-                       Data.creator = this->Creator;
-                       Data.parameter = (pcnt > 1 ? parameters[1] : "");
-
-                       user->Extend("CAP_REGHOLD");
-                       Event event((char*) &Data, (Module*)this->Creator, "cap_req");
-                       event.Send(this->ServerInstance);
+                       if (parameters.size() < 2)
+                               return CMD_FAILURE;
+
+                       CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ);
+
+                       // tokenize the input into a nice list of requested caps
+                       std::string cap_;
+                       irc::spacesepstream cap_stream(parameters[1]);
+
+                       while (cap_stream.GetToken(cap_))
+                       {
+                               Data.wanted.push_back(cap_);
+                       }
+
+                       reghold.set(user, 1);
+                       Data.Send();
+
+                       if (Data.wanted.empty())
+                       {
+                               user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), parameters[1].c_str());
+                               return CMD_SUCCESS;
+                       }
+
+                       // HACK: reset all of the caps which were enabled on this user because a cap request is atomic.
+                       for (std::vector<std::pair<GenericCap*, int> >::iterator iter = Data.changed.begin(); iter != Data.changed.end(); ++iter)
+                               iter->first->ext.set(user, iter->second);
+
+                       user->WriteServ("CAP %s NAK :%s", user->nick.c_str(), parameters[1].c_str());
                }
                else if (subcommand == "END")
                {
-                       user->Shrink("CAP_REGHOLD");
+                       reghold.set(user, 0);
                }
-               else if (subcommand == "LS")
+               else if ((subcommand == "LS") || (subcommand == "LIST"))
                {
-                       CapData Data;
-                       user->Extend("CAP_REGHOLD");
-                       Data.type = "LS";
-                       Data.user = user;
-                       Data.creator = this->Creator;
-                       Data.parameter.clear();
-
-                       Event event((char*) &Data, (Module*)this->Creator, "cap_ls");
-                       event.Send(this->ServerInstance);
+                       CapEvent Data(creator, user, subcommand == "LS" ? CapEvent::CAPEVENT_LS : CapEvent::CAPEVENT_LIST);
+
+                       reghold.set(user, 1);
+                       Data.Send();
+
+                       std::string Result;
+                       if (Data.wanted.size() > 0)
+                               Result = irc::stringjoiner(" ", Data.wanted, 0, Data.wanted.size() - 1).GetJoined();
+
+                       user->WriteServ("CAP %s %s :%s", user->nick.c_str(), subcommand.c_str(), Result.c_str());
+               }
+               else if (subcommand == "CLEAR")
+               {
+                       CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR);
+
+                       reghold.set(user, 1);
+                       Data.Send();
+
+                       std::string Result;
+                       if (!Data.ack.empty())
+                               Result = irc::stringjoiner(" ", Data.ack, 0, Data.ack.size() - 1).GetJoined();
+                       user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), Result.c_str());
                }
-               return CMD_FAILURE;
+               else
+               {
+                       user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s %s :Invalid CAP subcommand", user->nick.c_str(), subcommand.c_str());
+                       return CMD_FAILURE;
+               }
+
+               return CMD_SUCCESS;
        }
 };
 
 class ModuleCAP : public Module
 {
-       CommandCAP* newcommand;
+       CommandCAP cmd;
  public:
-       ModuleCAP(InspIRCd* Me)
-               : Module(Me)
+       ModuleCAP()
+               : cmd(this)
+       {
+       }
+
+       void init()
        {
-               // Create a new command
-               newcommand = new CommandCAP(ServerInstance, this);
-               ServerInstance->AddCommand(newcommand);
+               ServerInstance->Modules->AddService(cmd);
+               ServerInstance->Modules->AddService(cmd.reghold);
 
-               Implementation eventlist[] = { I_OnCheckReady, I_OnCleanup, I_OnUserDisconnect, I_OnRequest };
-               ServerInstance->Modules->Attach(eventlist, this, 5);
+               Implementation eventlist[] = { I_OnCheckReady };
+               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
        }
 
-       virtual bool OnCheckReady(User* user)
+       ModResult OnCheckReady(LocalUser* user)
        {
                /* Users in CAP state get held until CAP END */
-               if (user->GetExt("CAP_REGHOLD"))
-                       return true;
+               if (cmd.reghold.get(user))
+                       return MOD_RES_DENY;
 
-               return false;
+               return MOD_RES_PASSTHRU;
        }
 
-       virtual ~ModuleCAP()
+       ~ModuleCAP()
        {
        }
 
-       virtual Version GetVersion()
+       Version GetVersion()
        {
-               return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
+               return Version("Client CAP extension support", VF_VENDOR);
        }
 };