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