]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.cpp
Merge pull request #215 from attilamolnar/insp20+modfixes
[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                         CapEvent Data(creator, "cap_req");
59
60                         Data.type = subcommand;
61                         Data.user = user;
62                         Data.creator = this->creator;
63
64                         if (parameters.size() < 2)
65                                 return CMD_FAILURE;
66
67                         // tokenize the input into a nice list of requested caps
68                         std::string param = parameters[1];
69                         std::string cap_;
70                         irc::spacesepstream cap_stream(param);
71
72                         while (cap_stream.GetToken(cap_))
73                         {
74                                 Data.wanted.push_back(cap_);
75                         }
76
77                         reghold.set(user, 1);
78                         Data.Send();
79
80                         if (Data.ack.size() > 0)
81                         {
82                                 std::string AckResult = irc::stringjoiner(" ", Data.ack, 0, Data.ack.size() - 1).GetJoined();
83                                 user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), AckResult.c_str());
84                         }
85
86                         if (Data.wanted.size() > 0)
87                         {
88                                 std::string NakResult = irc::stringjoiner(" ", Data.wanted, 0, Data.wanted.size() - 1).GetJoined();
89                                 user->WriteServ("CAP %s NAK :%s", user->nick.c_str(), NakResult.c_str());
90                         }
91                 }
92                 else if (subcommand == "END")
93                 {
94                         reghold.set(user, 0);
95                 }
96                 else if ((subcommand == "LS") || (subcommand == "LIST"))
97                 {
98                         CapEvent Data(creator, subcommand == "LS" ? "cap_ls" : "cap_list");
99
100                         Data.type = subcommand;
101                         Data.user = user;
102                         Data.creator = this->creator;
103
104                         reghold.set(user, 1);
105                         Data.Send();
106
107                         std::string Result;
108                         if (Data.wanted.size() > 0)
109                                 Result = irc::stringjoiner(" ", Data.wanted, 0, Data.wanted.size() - 1).GetJoined();
110                         else
111                                 Result = "";
112
113                         user->WriteServ("CAP %s %s :%s", user->nick.c_str(), subcommand.c_str(), Result.c_str());
114                 }
115                 else if (subcommand == "CLEAR")
116                 {
117                         CapEvent Data(creator, "cap_clear");
118
119                         Data.type = subcommand;
120                         Data.user = user;
121                         Data.creator = this->creator;
122
123                         reghold.set(user, 1);
124                         Data.Send();
125
126                         std::string Result = irc::stringjoiner(" ", Data.ack, 0, Data.ack.size() - 1).GetJoined();
127                         user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), Result.c_str());
128                 }
129                 else
130                 {
131                         user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s %s :Invalid CAP subcommand", user->nick.c_str(), subcommand.c_str());
132                 }
133
134                 return CMD_FAILURE;
135         }
136 };
137
138 class ModuleCAP : public Module
139 {
140         CommandCAP cmd;
141  public:
142         ModuleCAP()
143                 : cmd(this)
144         {
145                 ServerInstance->AddCommand(&cmd);
146                 ServerInstance->Extensions.Register(&cmd.reghold);
147
148                 Implementation eventlist[] = { I_OnCheckReady };
149                 ServerInstance->Modules->Attach(eventlist, this, 1);
150         }
151
152         ModResult OnCheckReady(LocalUser* user)
153         {
154                 /* Users in CAP state get held until CAP END */
155                 if (cmd.reghold.get(user))
156                         return MOD_RES_DENY;
157
158                 return MOD_RES_PASSTHRU;
159         }
160
161         ~ModuleCAP()
162         {
163         }
164
165         Version GetVersion()
166         {
167                 return Version("Client CAP extension support", VF_VENDOR);
168         }
169 };
170
171 MODULE_INIT(ModuleCAP)
172