]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - 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
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "m_cap.h"
23
24 /* $ModDesc: Provides the CAP negotiation mechanism seen in ratbox-derived ircds */
25
26 /*
27 CAP LS
28 :alfred.staticbox.net CAP * LS :multi-prefix sasl
29 CAP REQ :multi-prefix
30 :alfred.staticbox.net CAP * ACK :multi-prefix
31 CAP CLEAR
32 :alfred.staticbox.net CAP * ACK :-multi-prefix
33 CAP REQ :multi-prefix
34 :alfred.staticbox.net CAP * ACK :multi-prefix
35 CAP LIST
36 :alfred.staticbox.net CAP * LIST :multi-prefix
37 CAP END
38 */
39
40 /** Handle /CAP
41  */
42 class CommandCAP : public Command
43 {
44  public:
45         LocalIntExt reghold;
46         CommandCAP (Module* mod) : Command(mod, "CAP", 1),
47                 reghold("CAP_REGHOLD", mod)
48         {
49                 works_before_reg = true;
50         }
51
52         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
53         {
54                 irc::string subcommand = parameters[0].c_str();
55
56                 if (subcommand == "REQ")
57                 {
58                         if (parameters.size() < 2)
59                                 return CMD_FAILURE;
60
61                         CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ);
62
63                         // tokenize the input into a nice list of requested caps
64                         std::string cap_;
65                         irc::spacesepstream cap_stream(parameters[1]);
66
67                         while (cap_stream.GetToken(cap_))
68                         {
69                                 Data.wanted.push_back(cap_);
70                         }
71
72                         reghold.set(user, 1);
73                         Data.Send();
74
75                         if (Data.wanted.empty())
76                         {
77                                 user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), parameters[1].c_str());
78                                 return CMD_SUCCESS;
79                         }
80
81                         // HACK: reset all of the caps which were enabled on this user because a cap request is atomic.
82                         for (std::vector<std::pair<GenericCap*, int> >::iterator iter = Data.changed.begin(); iter != Data.changed.end(); ++iter)
83                                 iter->first->ext.set(user, iter->second);
84
85                         user->WriteServ("CAP %s NAK :%s", user->nick.c_str(), parameters[1].c_str());
86                 }
87                 else if (subcommand == "END")
88                 {
89                         reghold.set(user, 0);
90                 }
91                 else if ((subcommand == "LS") || (subcommand == "LIST"))
92                 {
93                         CapEvent Data(creator, user, subcommand == "LS" ? CapEvent::CAPEVENT_LS : CapEvent::CAPEVENT_LIST);
94
95                         reghold.set(user, 1);
96                         Data.Send();
97
98                         std::string Result;
99                         if (Data.wanted.size() > 0)
100                                 Result = irc::stringjoiner(" ", Data.wanted, 0, Data.wanted.size() - 1).GetJoined();
101
102                         user->WriteServ("CAP %s %s :%s", user->nick.c_str(), subcommand.c_str(), Result.c_str());
103                 }
104                 else if (subcommand == "CLEAR")
105                 {
106                         CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR);
107
108                         reghold.set(user, 1);
109                         Data.Send();
110
111                         std::string Result;
112                         if (!Data.ack.empty())
113                                 Result = irc::stringjoiner(" ", Data.ack, 0, Data.ack.size() - 1).GetJoined();
114                         user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), Result.c_str());
115                 }
116                 else
117                 {
118                         user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s %s :Invalid CAP subcommand", user->nick.c_str(), subcommand.c_str());
119                         return CMD_FAILURE;
120                 }
121
122                 return CMD_SUCCESS;
123         }
124 };
125
126 class ModuleCAP : public Module
127 {
128         CommandCAP cmd;
129  public:
130         ModuleCAP()
131                 : cmd(this)
132         {
133         }
134
135         void init()
136         {
137                 ServerInstance->Modules->AddService(cmd);
138                 ServerInstance->Modules->AddService(cmd.reghold);
139
140                 Implementation eventlist[] = { I_OnCheckReady };
141                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
142         }
143
144         ModResult OnCheckReady(LocalUser* user)
145         {
146                 /* Users in CAP state get held until CAP END */
147                 if (cmd.reghold.get(user))
148                         return MOD_RES_DENY;
149
150                 return MOD_RES_PASSTHRU;
151         }
152
153         ~ModuleCAP()
154         {
155         }
156
157         Version GetVersion()
158         {
159                 return Version("Client CAP extension support", VF_VENDOR);
160         }
161 };
162
163 MODULE_INIT(ModuleCAP)
164