]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/invite.h
Fix an inverted condition in cmd_modules.
[user/henk/code/inspircd.git] / src / coremods / core_channel / invite.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
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 "modules/invite.h"
24
25 namespace Invite
26 {
27         template<typename T>
28         struct Store
29         {
30                 typedef insp::intrusive_list<Invite, T> List;
31
32                 /** List of pending Invites
33                  */
34                 List invites;
35         };
36
37         template<typename T, ExtensionItem::ExtensibleType ExtType>
38         class ExtItem;
39
40         class APIImpl;
41 }
42
43 extern void RemoveInvite(Invite::Invite* inv, bool remove_user, bool remove_chan);
44 extern void UnserializeInvite(LocalUser* user, const std::string& value);
45
46 template<typename T, ExtensionItem::ExtensibleType ExtType>
47 class Invite::ExtItem : public ExtensionItem
48 {
49  private:
50         static std::string ToString(void* item, bool human)
51         {
52                 std::string ret;
53                 Store<T>* store = static_cast<Store<T>*>(item);
54                 for (typename insp::intrusive_list<Invite, T>::iterator i = store->invites.begin(); i != store->invites.end(); ++i)
55                 {
56                         Invite* inv = *i;
57                         inv->Serialize(human, (ExtType == ExtensionItem::EXT_USER), ret);
58                 }
59                 if (!ret.empty())
60                         ret.erase(ret.length()-1);
61                 return ret;
62         }
63
64  public:
65         ExtItem(Module* owner, const char* extname)
66                 : ExtensionItem(extname, ExtType, owner)
67         {
68         }
69
70         Store<T>* get(Extensible* ext, bool create = false)
71         {
72                 Store<T>* store = static_cast<Store<T>*>(get_raw(ext));
73                 if ((create) && (!store))
74                 {
75                         store = new Store<T>;
76                         set_raw(ext, store);
77                 }
78                 return store;
79         }
80
81         void unset(Extensible* ext)
82         {
83                 void* store = unset_raw(ext);
84                 if (store)
85                         free(ext, store);
86         }
87
88         void free(Extensible* container, void* item) CXX11_OVERRIDE
89         {
90                 Store<T>* store = static_cast<Store<T>*>(item);
91                 for (typename Store<T>::List::iterator i = store->invites.begin(); i != store->invites.end(); )
92                 {
93                         Invite* inv = *i;
94                         // Destructing the Invite invalidates the iterator, so move it now
95                         ++i;
96                         RemoveInvite(inv, (ExtType != ExtensionItem::EXT_USER), (ExtType == ExtensionItem::EXT_USER));
97                 }
98
99                 delete store;
100         }
101
102         std::string ToHuman(const Extensible* container, void* item) const CXX11_OVERRIDE
103         {
104                 return ToString(item, true);
105         }
106
107         std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE
108         {
109                 return ToString(item, false);
110         }
111
112         void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
113         {
114                 if (ExtType != ExtensionItem::EXT_CHANNEL)
115                         UnserializeInvite(static_cast<LocalUser*>(container), value);
116         }
117 };
118
119 class Invite::APIImpl : public APIBase
120 {
121         ExtItem<LocalUser, ExtensionItem::EXT_USER> userext;
122         ExtItem<Channel, ExtensionItem::EXT_CHANNEL> chanext;
123
124  public:
125         APIImpl(Module* owner);
126
127         void Create(LocalUser* user, Channel* chan, time_t timeout) CXX11_OVERRIDE;
128         Invite* Find(LocalUser* user, Channel* chan) CXX11_OVERRIDE;
129         bool Remove(LocalUser* user, Channel* chan) CXX11_OVERRIDE;
130         const List* GetList(LocalUser* user) CXX11_OVERRIDE;
131
132         void RemoveAll(LocalUser* user) { userext.unset(user); }
133         void RemoveAll(Channel* chan) { chanext.unset(chan); }
134         void Destruct(Invite* inv, bool remove_chan = true, bool remove_user = true);
135         void Unserialize(LocalUser* user, const std::string& value);
136 };