]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.cpp
Merge branch 'master+dns'
[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         Events::ModuleEventProvider capevprov;
43
44  public:
45         LocalIntExt reghold;
46         CommandCAP (Module* mod) : Command(mod, "CAP", 1),
47                 capevprov(mod, "event/cap"),
48                 reghold("CAP_REGHOLD", ExtensionItem::EXT_USER, mod)
49         {
50                 works_before_reg = true;
51         }
52
53         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
54         {
55                 std::string subcommand(parameters[0].length(), ' ');
56                 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
57
58                 if (subcommand == "REQ")
59                 {
60                         if (parameters.size() < 2)
61                                 return CMD_FAILURE;
62
63                         CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ);
64
65                         // tokenize the input into a nice list of requested caps
66                         std::string cap_;
67                         irc::spacesepstream cap_stream(parameters[1]);
68
69                         while (cap_stream.GetToken(cap_))
70                         {
71                                 std::transform(cap_.begin(), cap_.end(), cap_.begin(), ::tolower);
72                                 Data.wanted.push_back(cap_);
73                         }
74
75                         reghold.set(user, 1);
76                         FOREACH_MOD_CUSTOM(capevprov, GenericCap, OnCapEvent, (Data));
77
78                         if (Data.ack.size() > 0)
79                         {
80                                 std::string AckResult = irc::stringjoiner(Data.ack);
81                                 user->WriteCommand("CAP", "ACK :" + AckResult);
82                         }
83
84                         if (Data.wanted.size() > 0)
85                         {
86                                 std::string NakResult = irc::stringjoiner(Data.wanted);
87                                 user->WriteCommand("CAP", "NAK :" + NakResult);
88                         }
89                 }
90                 else if (subcommand == "END")
91                 {
92                         reghold.set(user, 0);
93                 }
94                 else if ((subcommand == "LS") || (subcommand == "LIST"))
95                 {
96                         CapEvent Data(creator, user, subcommand == "LS" ? CapEvent::CAPEVENT_LS : CapEvent::CAPEVENT_LIST);
97
98                         reghold.set(user, 1);
99                         FOREACH_MOD_CUSTOM(capevprov, GenericCap, OnCapEvent, (Data));
100
101                         std::string Result = irc::stringjoiner(Data.wanted);
102                         user->WriteCommand("CAP", subcommand + " :" + Result);
103                 }
104                 else if (subcommand == "CLEAR")
105                 {
106                         CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR);
107
108                         reghold.set(user, 1);
109                         FOREACH_MOD_CUSTOM(capevprov, GenericCap, OnCapEvent, (Data));
110
111                         std::string Result = irc::stringjoiner(Data.ack);
112                         user->WriteCommand("CAP", "ACK :" + Result);
113                 }
114                 else
115                 {
116                         user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
117                         return CMD_FAILURE;
118                 }
119
120                 return CMD_SUCCESS;
121         }
122 };
123
124 class ModuleCAP : public Module
125 {
126         CommandCAP cmd;
127  public:
128         ModuleCAP()
129                 : cmd(this)
130         {
131         }
132
133         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
134         {
135                 /* Users in CAP state get held until CAP END */
136                 if (cmd.reghold.get(user))
137                         return MOD_RES_DENY;
138
139                 return MOD_RES_PASSTHRU;
140         }
141
142         Version GetVersion() CXX11_OVERRIDE
143         {
144                 return Version("Client CAP extension support", VF_VENDOR);
145         }
146 };
147
148 MODULE_INIT(ModuleCAP)