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