]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.cpp
Fix xline reasons being truncated in m_xline_db.
[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 "modules/cap.h"
23
24 /*
25 CAP LS
26 :alfred.staticbox.net CAP * LS :multi-prefix sasl
27 CAP REQ :multi-prefix
28 :alfred.staticbox.net CAP * ACK :multi-prefix
29 CAP CLEAR
30 :alfred.staticbox.net CAP * ACK :-multi-prefix
31 CAP REQ :multi-prefix
32 :alfred.staticbox.net CAP * ACK :multi-prefix
33 CAP LIST
34 :alfred.staticbox.net CAP * LIST :multi-prefix
35 CAP END
36 */
37
38 /** Handle /CAP
39  */
40 class CommandCAP : public Command
41 {
42  public:
43         LocalIntExt reghold;
44         CommandCAP (Module* mod) : Command(mod, "CAP", 1),
45                 reghold("CAP_REGHOLD", mod)
46         {
47                 works_before_reg = true;
48         }
49
50         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
51         {
52                 std::string subcommand(parameters[0].length(), ' ');
53                 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
54
55                 if (subcommand == "REQ")
56                 {
57                         if (parameters.size() < 2)
58                                 return CMD_FAILURE;
59
60                         CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ);
61
62                         // tokenize the input into a nice list of requested caps
63                         std::string cap_;
64                         irc::spacesepstream cap_stream(parameters[1]);
65
66                         while (cap_stream.GetToken(cap_))
67                         {
68                                 std::transform(cap_.begin(), cap_.end(), cap_.begin(), ::tolower);
69                                 Data.wanted.push_back(cap_);
70                         }
71
72                         reghold.set(user, 1);
73                         Data.Send();
74
75                         if (Data.ack.size() > 0)
76                         {
77                                 std::string AckResult = irc::stringjoiner(Data.ack);
78                                 user->WriteCommand("CAP", "ACK :" + AckResult);
79                         }
80
81                         if (Data.wanted.size() > 0)
82                         {
83                                 std::string NakResult = irc::stringjoiner(Data.wanted);
84                                 user->WriteCommand("CAP", "NAK :" + NakResult);
85                         }
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 = irc::stringjoiner(Data.wanted);
99                         user->WriteCommand("CAP", subcommand + " :" + Result);
100                 }
101                 else if (subcommand == "CLEAR")
102                 {
103                         CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR);
104
105                         reghold.set(user, 1);
106                         Data.Send();
107
108                         std::string Result = irc::stringjoiner(Data.ack);
109                         user->WriteCommand("CAP", "ACK :" + Result);
110                 }
111                 else
112                 {
113                         user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
114                         return CMD_FAILURE;
115                 }
116
117                 return CMD_SUCCESS;
118         }
119 };
120
121 class ModuleCAP : public Module
122 {
123         CommandCAP cmd;
124  public:
125         ModuleCAP()
126                 : cmd(this)
127         {
128         }
129
130         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
131         {
132                 /* Users in CAP state get held until CAP END */
133                 if (cmd.reghold.get(user))
134                         return MOD_RES_DENY;
135
136                 return MOD_RES_PASSTHRU;
137         }
138
139         Version GetVersion() CXX11_OVERRIDE
140         {
141                 return Version("Client CAP extension support", VF_VENDOR);
142         }
143 };
144
145 MODULE_INIT(ModuleCAP)