]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/invite.h
Convert InspIRCd::SetSignals to a static function.
[user/henk/code/inspircd.git] / src / coremods / core_channel / invite.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 #include "modules/invite.h"
23
24 namespace Invite
25 {
26         template<typename T>
27         struct Store
28         {
29                 typedef insp::intrusive_list<Invite, T> List;
30
31                 /** List of pending Invites
32                  */
33                 List invites;
34         };
35
36         template<typename T, ExtensionItem::ExtensibleType ExtType>
37         class ExtItem;
38
39         class APIImpl;
40 }
41
42 extern void RemoveInvite(Invite::Invite* inv, bool remove_user, bool remove_chan);
43 extern void UnserializeInvite(LocalUser* user, const std::string& value);
44
45 template<typename T, ExtensionItem::ExtensibleType ExtType>
46 class Invite::ExtItem : public ExtensionItem
47 {
48  private:
49         static std::string ToString(void* item, bool human)
50         {
51                 std::string ret;
52                 Store<T>* store = static_cast<Store<T>*>(item);
53                 for (typename insp::intrusive_list<Invite, T>::iterator i = store->invites.begin(); i != store->invites.end(); ++i)
54                 {
55                         Invite* inv = *i;
56                         inv->Serialize(human, (ExtType == ExtensionItem::EXT_USER), ret);
57                 }
58                 if (!ret.empty())
59                         ret.erase(ret.length()-1);
60                 return ret;
61         }
62
63  public:
64         ExtItem(Module* owner, const char* extname)
65                 : ExtensionItem(extname, ExtType, owner)
66         {
67         }
68
69         Store<T>* get(Extensible* ext, bool create = false)
70         {
71                 Store<T>* store = static_cast<Store<T>*>(get_raw(ext));
72                 if ((create) && (!store))
73                 {
74                         store = new Store<T>;
75                         set_raw(ext, store);
76                 }
77                 return store;
78         }
79
80         void unset(Extensible* ext)
81         {
82                 void* store = unset_raw(ext);
83                 if (store)
84                         free(ext, store);
85         }
86
87         void free(Extensible* container, void* item) CXX11_OVERRIDE
88         {
89                 Store<T>* store = static_cast<Store<T>*>(item);
90                 for (typename Store<T>::List::iterator i = store->invites.begin(); i != store->invites.end(); )
91                 {
92                         Invite* inv = *i;
93                         // Destructing the Invite invalidates the iterator, so move it now
94                         ++i;
95                         RemoveInvite(inv, (ExtType != ExtensionItem::EXT_USER), (ExtType == ExtensionItem::EXT_USER));
96                 }
97
98                 delete store;
99         }
100
101         std::string ToHuman(const Extensible* container, void* item) const CXX11_OVERRIDE
102         {
103                 return ToString(item, true);
104         }
105
106         std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE
107         {
108                 return ToString(item, false);
109         }
110
111         void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
112         {
113                 if (ExtType != ExtensionItem::EXT_CHANNEL)
114                         UnserializeInvite(static_cast<LocalUser*>(container), value);
115         }
116 };
117
118 class Invite::APIImpl : public APIBase
119 {
120         ExtItem<LocalUser, ExtensionItem::EXT_USER> userext;
121         ExtItem<Channel, ExtensionItem::EXT_CHANNEL> chanext;
122
123  public:
124         APIImpl(Module* owner);
125
126         void Create(LocalUser* user, Channel* chan, time_t timeout) CXX11_OVERRIDE;
127         Invite* Find(LocalUser* user, Channel* chan) CXX11_OVERRIDE;
128         bool Remove(LocalUser* user, Channel* chan) CXX11_OVERRIDE;
129         const List* GetList(LocalUser* user) CXX11_OVERRIDE;
130
131         void RemoveAll(LocalUser* user) { userext.unset(user); }
132         void RemoveAll(Channel* chan) { chanext.unset(chan); }
133         void Destruct(Invite* inv, bool remove_chan = true, bool remove_user = true);
134         void Unserialize(LocalUser* user, const std::string& value);
135 };