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