]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.h
9ce7f95012c602a2bb6e51cba14d9661dd186172
[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 CapEvent : public Event
25 {
26  public:
27         enum CapEventType
28         {
29                 CAPEVENT_REQ,
30                 CAPEVENT_LS,
31                 CAPEVENT_LIST,
32                 CAPEVENT_CLEAR
33         };
34
35         CapEventType type;
36         std::vector<std::string> wanted;
37         std::vector<std::string> ack;
38         User* user;
39         CapEvent(Module* sender, User* u, CapEventType capevtype) : Event(sender, "cap_request"), type(capevtype), user(u) {}
40 };
41
42 class GenericCap
43 {
44  public:
45         LocalIntExt ext;
46         const std::string cap;
47         GenericCap(Module* parent, const std::string &Cap) : ext("cap_" + Cap, parent), cap(Cap)
48         {
49                 ServerInstance->Extensions.Register(&ext);
50         }
51
52         void HandleEvent(Event& ev)
53         {
54                 if (ev.id != "cap_request")
55                         return;
56
57                 CapEvent *data = static_cast<CapEvent*>(&ev);
58                 if (data->type == CapEvent::CAPEVENT_REQ)
59                 {
60                         std::vector<std::string>::iterator it;
61                         if ((it = std::find(data->wanted.begin(), data->wanted.end(), cap)) != data->wanted.end())
62                         {
63                                 // we can handle this, so ACK it, and remove it from the wanted list
64                                 data->ack.push_back(*it);
65                                 data->wanted.erase(it);
66                                 ext.set(data->user, 1);
67                         }
68                 }
69                 else if (data->type == CapEvent::CAPEVENT_LS)
70                 {
71                         data->wanted.push_back(cap);
72                 }
73                 else if (data->type == CapEvent::CAPEVENT_LIST)
74                 {
75                         if (ext.get(data->user))
76                                 data->wanted.push_back(cap);
77                 }
78                 else if (data->type == CapEvent::CAPEVENT_CLEAR)
79                 {
80                         data->ack.push_back("-" + cap);
81                         ext.set(data->user, 0);
82                 }
83         }
84 };
85
86 #endif