]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.cpp
2b4055e3cf0805dd11b1f68550a6789df75a2594
[user/henk/code/inspircd.git] / src / modules / m_cap.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/cap.h"
22
23 namespace Cap
24 {
25         class ManagerImpl;
26 }
27
28 class Cap::ManagerImpl : public Cap::Manager
29 {
30         typedef insp::flat_map<std::string, Capability*, irc::insensitive_swo> CapMap;
31
32         ExtItem capext;
33         CapMap caps;
34
35         static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding)
36         {
37                 if ((usercaps & cap->GetMask()) == adding)
38                         return true;
39
40                 return cap->OnRequest(user, adding);
41         }
42
43         Capability::Bit AllocateBit() const
44         {
45                 Capability::Bit used = 0;
46                 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
47                 {
48                         Capability* cap = i->second;
49                         used |= cap->GetMask();
50                 }
51
52                 for (unsigned int i = 0; i < MAX_CAPS; i++)
53                 {
54                         Capability::Bit bit = (1 << i);
55                         if (!(used & bit))
56                                 return bit;
57                 }
58                 throw ModuleException("Too many caps");
59         }
60
61  public:
62         ManagerImpl(Module* mod)
63                 : Cap::Manager(mod)
64                 , capext("caps", ExtensionItem::EXT_USER, mod)
65         {
66         }
67
68         ~ManagerImpl()
69         {
70                 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
71                 {
72                         Capability* cap = i->second;
73                         cap->Unregister();
74                 }
75         }
76
77         void AddCap(Cap::Capability* cap) CXX11_OVERRIDE
78         {
79                 // No-op if the cap is already registered.
80                 // This allows modules to call SetActive() on a cap without checking if it's active first.
81                 if (cap->IsRegistered())
82                         return;
83
84                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str());
85                 cap->bit = AllocateBit();
86                 cap->extitem = &capext;
87                 caps.insert(std::make_pair(cap->GetName(), cap));
88         }
89
90         void DelCap(Cap::Capability* cap) CXX11_OVERRIDE
91         {
92                 // No-op if the cap is not registered, see AddCap() above
93                 if (!cap->IsRegistered())
94                         return;
95
96                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str());
97
98                 // Turn off the cap for all users
99                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
100                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
101                 {
102                         LocalUser* user = *i;
103                         cap->set(user, false);
104                 }
105
106                 cap->Unregister();
107                 caps.erase(cap->GetName());
108         }
109
110         Capability* Find(const std::string& capname) const CXX11_OVERRIDE
111         {
112                 CapMap::const_iterator it = caps.find(capname);
113                 if (it != caps.end())
114                         return it->second;
115                 return NULL;
116         }
117
118         bool HandleReq(LocalUser* user, const std::string& reqlist)
119         {
120                 Ext usercaps = capext.get(user);
121                 irc::spacesepstream ss(reqlist);
122                 for (std::string capname; ss.GetToken(capname); )
123                 {
124                         bool remove = (capname[0] == '-');
125                         if (remove)
126                                 capname.erase(capname.begin());
127
128                         Capability* cap = ManagerImpl::Find(capname);
129                         if ((!cap) || (!CanRequest(user, usercaps, cap, !remove)))
130                                 return false;
131
132                         if (remove)
133                                 usercaps = cap->DelFromMask(usercaps);
134                         else
135                                 usercaps = cap->AddToMask(usercaps);
136                 }
137
138                 capext.set(user, usercaps);
139                 return true;
140         }
141
142         void HandleList(std::string& out, LocalUser* user, bool show_all, bool minus_prefix = false) const
143         {
144                 Ext show_caps = (show_all ? ~0 : capext.get(user));
145
146                 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
147                 {
148                         Capability* cap = i->second;
149                         if (!(show_caps & cap->GetMask()))
150                                 continue;
151
152                         if ((show_all) && (!cap->OnList(user)))
153                                 continue;
154
155                         if (minus_prefix)
156                                 out.push_back('-');
157                         out.append(cap->GetName()).push_back(' ');
158                 }
159         }
160
161         void HandleClear(LocalUser* user, std::string& result)
162         {
163                 HandleList(result, user, false, true);
164                 capext.unset(user);
165         }
166 };
167
168 class CommandCap : public SplitCommand
169 {
170         Cap::ManagerImpl manager;
171
172         static void DisplayResult(LocalUser* user, std::string& result)
173         {
174                 if (result.size() > 5)
175                         result.erase(result.end()-1);
176                 user->WriteCommand("CAP", result);
177         }
178
179  public:
180         LocalIntExt holdext;
181
182         CommandCap(Module* mod)
183                 : SplitCommand(mod, "CAP", 1)
184                 , manager(mod)
185                 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
186         {
187                 works_before_reg = true;
188         }
189
190         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
191         {
192                 if (user->registered != REG_ALL)
193                         holdext.set(user, 1);
194
195                 std::string subcommand(parameters[0].length(), ' ');
196                 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
197
198                 if (subcommand == "REQ")
199                 {
200                         if (parameters.size() < 2)
201                                 return CMD_FAILURE;
202
203                         std::string result = (manager.HandleReq(user, parameters[1]) ? "ACK :" : "NAK :");
204                         result.append(parameters[1]);
205                         user->WriteCommand("CAP", result);
206                 }
207                 else if (subcommand == "END")
208                 {
209                         holdext.unset(user);
210                 }
211                 else if ((subcommand == "LS") || (subcommand == "LIST"))
212                 {
213                         const bool is_ls = (subcommand.length() == 2);
214
215                         std::string result = subcommand + " :";
216                         manager.HandleList(result, user, is_ls);
217                         DisplayResult(user, result);
218                 }
219                 else if (subcommand == "CLEAR")
220                 {
221                         std::string result = "ACK :";
222                         manager.HandleClear(user, result);
223                         DisplayResult(user, result);
224                 }
225                 else
226                 {
227                         user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
228                         return CMD_FAILURE;
229                 }
230
231                 return CMD_SUCCESS;
232         }
233 };
234
235 class ModuleCap : public Module
236 {
237         CommandCap cmd;
238
239  public:
240         ModuleCap()
241                 : cmd(this)
242         {
243         }
244
245         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
246         {
247                 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
248         }
249
250         Version GetVersion() CXX11_OVERRIDE
251         {
252                 return Version("Provides support for CAP capability negotiation", VF_VENDOR);
253         }
254 };
255
256 MODULE_INIT(ModuleCap)