2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* $ModDesc: Provides the CAP negotiation mechanism seen in ratbox-derived ircds */
28 :alfred.staticbox.net CAP * LS :multi-prefix sasl
30 :alfred.staticbox.net CAP * ACK :multi-prefix
32 :alfred.staticbox.net CAP * ACK :-multi-prefix
34 :alfred.staticbox.net CAP * ACK :multi-prefix
36 :alfred.staticbox.net CAP * LIST :multi-prefix
42 class CommandCAP : public Command
46 CommandCAP (Module* mod) : Command(mod, "CAP", 1),
47 reghold("CAP_REGHOLD", mod)
49 works_before_reg = true;
52 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
54 irc::string subcommand = parameters[0].c_str();
56 if (subcommand == "REQ")
58 if (parameters.size() < 2)
61 CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ);
63 // tokenize the input into a nice list of requested caps
65 irc::spacesepstream cap_stream(parameters[1]);
67 while (cap_stream.GetToken(cap_))
69 // Whilst the handling of extraneous spaces is not currently defined in the CAP specification
70 // every single other implementation ignores extraneous spaces. Lets copy them for
71 // compatibility purposes.
74 Data.wanted.push_back(cap_);
80 if (Data.wanted.empty())
82 user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), parameters[1].c_str());
86 // HACK: reset all of the caps which were enabled on this user because a cap request is atomic.
87 for (std::vector<std::pair<GenericCap*, int> >::iterator iter = Data.changed.begin(); iter != Data.changed.end(); ++iter)
88 iter->first->ext.set(user, iter->second);
90 user->WriteServ("CAP %s NAK :%s", user->nick.c_str(), parameters[1].c_str());
92 else if (subcommand == "END")
96 else if ((subcommand == "LS") || (subcommand == "LIST"))
98 CapEvent Data(creator, user, subcommand == "LS" ? CapEvent::CAPEVENT_LS : CapEvent::CAPEVENT_LIST);
100 reghold.set(user, 1);
104 if (Data.wanted.size() > 0)
105 Result = irc::stringjoiner(" ", Data.wanted, 0, Data.wanted.size() - 1).GetJoined();
107 user->WriteServ("CAP %s %s :%s", user->nick.c_str(), subcommand.c_str(), Result.c_str());
109 else if (subcommand == "CLEAR")
111 CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR);
113 reghold.set(user, 1);
117 if (!Data.ack.empty())
118 Result = irc::stringjoiner(" ", Data.ack, 0, Data.ack.size() - 1).GetJoined();
119 user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), Result.c_str());
123 user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s %s :Invalid CAP subcommand", user->nick.c_str(), subcommand.c_str());
131 class ModuleCAP : public Module
142 ServerInstance->Modules->AddService(cmd);
143 ServerInstance->Modules->AddService(cmd.reghold);
145 Implementation eventlist[] = { I_OnCheckReady };
146 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
149 ModResult OnCheckReady(LocalUser* user)
151 /* Users in CAP state get held until CAP END */
152 if (cmd.reghold.get(user))
155 return MOD_RES_PASSTHRU;
164 return Version("Client CAP extension support", VF_VENDOR);
168 MODULE_INIT(ModuleCAP)