]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.h
Fix CAP REQ to be atomic like the standard dictates.
[user/henk/code/inspircd.git] / src / modules / m_cap.h
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 #ifndef M_CAP_H
22 #define M_CAP_H
23
24 class GenericCap;
25
26 class CapEvent : public Event
27 {
28  public:
29         enum CapEventType
30         {
31                 CAPEVENT_REQ,
32                 CAPEVENT_LS,
33                 CAPEVENT_LIST,
34                 CAPEVENT_CLEAR
35         };
36
37         CapEventType type;
38         std::vector<std::string> wanted;
39         std::vector<std::string> ack;
40         std::vector<std::pair<GenericCap*, int> > changed; // HACK: clean this up before 2.2
41         User* user;
42         CapEvent(Module* sender, User* u, CapEventType capevtype) : Event(sender, "cap_request"), type(capevtype), user(u) {}
43 };
44
45 class GenericCap
46 {
47  public:
48         LocalIntExt ext;
49         const std::string cap;
50         GenericCap(Module* parent, const std::string &Cap) : ext("cap_" + Cap, parent), cap(Cap)
51         {
52                 ServerInstance->Modules->AddService(ext);
53         }
54
55         void HandleEvent(Event& ev)
56         {
57                 if (ev.id != "cap_request")
58                         return;
59
60                 CapEvent *data = static_cast<CapEvent*>(&ev);
61                 if (data->type == CapEvent::CAPEVENT_REQ)
62                 {
63                         for (std::vector<std::string>::iterator it = data->wanted.begin(); it != data->wanted.end(); ++it)
64                         {
65                                 if (it->empty())
66                                         continue;
67                                 bool enablecap = ((*it)[0] != '-');
68                                 if (((enablecap) && (*it == cap)) || (*it == "-" + cap))
69                                 {
70                                         // we can handle this, so ACK it, and remove it from the wanted list
71                                         data->ack.push_back(*it);
72                                         data->wanted.erase(it);
73                                         data->changed.push_back(std::make_pair(this, ext.set(data->user, enablecap ? 1 : 0)));
74                                         break;
75                                 }
76                         }
77                 }
78                 else if (data->type == CapEvent::CAPEVENT_LS)
79                 {
80                         data->wanted.push_back(cap);
81                 }
82                 else if (data->type == CapEvent::CAPEVENT_LIST)
83                 {
84                         if (ext.get(data->user))
85                                 data->wanted.push_back(cap);
86                 }
87                 else if (data->type == CapEvent::CAPEVENT_CLEAR)
88                 {
89                         data->ack.push_back("-" + cap);
90                         ext.set(data->user, 0);
91                 }
92         }
93 };
94
95 #endif