1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2012, 2014-2015 Attila Molnar <attilamolnar@hush.com>
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
* Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/** Types of extensible that an extension can extend. */
enum class ExtensionType
: uint8_t
{
/** The extension extends the User class. */
USER = 0,
/** The extension extends the Channel class. */
CHANNEL = 1,
/** The extension extends the Membership class. */
MEMBERSHIP = 2,
};
/** Base class for types that extend an extensible. */
class CoreExport ExtensionItem
: public ServiceProvider
{
public:
/** The type of extensible that this extension extends. */
const ExtensionType extype:2;
/** Deletes a \p value which is set on \p container.
* @param container The container that this extension is set on.
* @param item The item to delete.
*/
virtual void Delete(Extensible* container, void* item) = 0;
/** Deserialises a value for this extension of the specified container from the internal format.
* @param container A container this extension should be set on.
* @param value A value in the internal format.
*/
virtual void FromInternal(Extensible* container, const std::string& value) noexcept;
/** Deserialises a value for this extension of the specified container from the network format.
* @param container A container this extension should be set on.
* @param value A value in the network format.
*/
virtual void FromNetwork(Extensible* container, const std::string& value) noexcept;
/** @copydoc ServiceProvider::RegisterService */
void RegisterService() override;
/** Serialises a value for this extension of the specified container to the human-readable
* format.
* @param container The container that this extension is set on.
* @param item The value to convert to the human-readable format.
* @return The value specified in \p item in the human-readable format.
*/
virtual std::string ToHuman(const Extensible* container, void* item) const noexcept;
/** Serialises a value for this extension of the specified container to the internal format.
* @param container The container that this extension is set on.
* @param item The value to convert to the internal format.
* @return The value specified in \p item in the internal format.
*/
virtual std::string ToInternal(const Extensible* container, void* item) const noexcept;
/** Serialises a value for this extension of the specified container to the network format.
* @param container The container that this extension is set on.
* @param item The value to convert to the network format.
* @return The value specified in \p item in the network format.
*/
virtual std::string ToNetwork(const Extensible* container, void* item) const noexcept;
protected:
/** Initializes an instance of the ExtensionItem class.
* @param owner The module which created the extension.
* @param key The name of the extension (e.g. foo-bar).
* @param exttype The type of extensible that the extension applies to.
*/
ExtensionItem(Module* owner, const std::string& key, ExtensionType exttype);
/** Retrieves the value for this extension of the specified container from the internal map.
* @param container The container that this extension is set on.
* @return Either the value of this extension or nullptr if it does not exist.
*/
void* GetRaw(const Extensible* container) const;
/** Sets a value for this extension of the specified container in the internal map and
* returns the old value if one was set
* @param container The container that this extension should be set on.
* @param value The new value to set for this extension. Will NOT be copied.
* @return Either the old value or nullptr if one is not set.
*/
void* SetRaw(Extensible* container, void* value);
/** Syncs the value of this extension of the specified container across the network. Does
* nothing if an inheritor does not implement ExtensionItem::ToNetwork.
* @param container The container that this extension is set on.
* @param item The value of this extension.
*/
void Sync(const Extensible* container, void* item);
/** Removes this extension from the specified container and returns it.
* @param container The container that this extension should be removed from.
* @return Either the old value of this extension or nullptr if it was not set.
*/
void* UnsetRaw(Extensible* container);
};
/** Base class for types which can be extended with additional data. */
class CoreExport Extensible
: public Cullable
, public Serializable
{
public:
/** The container which extension values are stored in. */
typedef insp::flat_map<ExtensionItem*, void*> ExtensibleStore;
/** Allows extensions to access the extension store. */
friend class ExtensionItem;
/** The type of extensible that this is. */
const ExtensionType extype:2;
~Extensible() override;
/** @copydoc Cullable::Cull */
Cullable::Result Cull() override;
/** @copydoc Serializable::Deserialize */
bool Deserialize(Data& data) override;
/** Frees all extensions attached to this extensible. */
void FreeAllExtItems();
/** Retrieves the values for extensions which are set on this extensible. */
const ExtensibleStore& GetExtList() const { return extensions; }
/** @copydoc Serializable::Deserialize */
bool Serialize(Serializable::Data& data) override;
/** Unhooks the specifies extensions from this extensible.
* @param items The items to unhook.
*/
void UnhookExtensions(const std::vector<ExtensionItem*>& items);
protected:
Extensible(ExtensionType exttype);
private:
/** The values for extensions which are set on this extensible. */
ExtensibleStore extensions;
/** Whether this extensible has been culled yet. */
bool culled:1;
};
/** Manager for the extension system */
class CoreExport ExtensionManager final
{
public:
/** The container which registered extensions are stored in. */
typedef std::map<std::string, ExtensionItem*, irc::insensitive_swo> ExtMap;
/** Begins unregistering extensions belonging to the specified module.
* @param module The module to unregister extensions for.
* @param list The list to add unregistered extensions to.
*/
void BeginUnregister(Module* module, std::vector<ExtensionItem*>& list);
/** Retrieves registered extensions keyed by their names. */
const ExtMap& GetExts() const { return types; }
/** Retrieves an extension by name.
* @param name The name of the extension to retrieve.
* @return Either the value of this extension or nullptr if it does not exist.
*/
ExtensionItem* GetItem(const std::string& name);
/** Registers an extension with the manager.
* @return Either true if the extension was registered or false if an extension with the same
* name already exists.
*/
bool Register(ExtensionItem* item);
private:
/** Registered extensions keyed by their names. */
ExtMap types;
};
/** An extension which has a simple (usually POD) value. */
template <typename T, typename Del = std::default_delete<T>>
class SimpleExtItem
: public ExtensionItem
{
protected:
/** Whether to sync this extension across the network. */
bool synced;
public:
/** Initializes an instance of the SimpleExtItem<T,Del> class.
* @param owner The module which created the extension.
* @param key The name of the extension (e.g. foo-bar).
* @param exttype The type of extensible that the extension applies to.
* @param sync Whether this extension should be broadcast to other servers.
*/
SimpleExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false)
: ExtensionItem(owner, key, exttype)
, synced(sync)
{
}
/** @copydoc ExtensionItem::Delete */
void Delete(Extensible* container, void* item) override
{
Del del;
del(static_cast<T*>(item));
}
/** Retrieves the value for this extension of the specified container.
* @param container The container that this extension is set on.
* @return Either the value of this extension or nullptr if it is not set.
*/
inline T* Get(const Extensible* container) const
{
return static_cast<T*>(GetRaw(container));
}
/** Sets a value for this extension of the specified container.
* @param container The container that this extension should be set on.
* @param value The new value to set for this extension. Will NOT be copied.
* @param sync If syncable then whether to sync this set to the network.
*/
inline void Set(Extensible* container, T* value, bool sync = true)
{
if (container->extype != this->extype)
return;
T* old = static_cast<T*>(SetRaw(container, value));
Delete(container, old);
if (sync && synced)
Sync(container, value);
}
/** Sets a value for this extension of the specified container.
* @param container The container that this extension should be set on.
* @param value The new value to set for this extension. Will be copied.
* @param sync If syncable then whether to sync this set to the network.
*/
inline void Set(Extensible* container, const T& value, bool sync = true)
{
if (container->extype == this->extype)
Set(container, new T(value), sync);
}
/** Sets a forwarded value for this extension of the specified container.
* @param container The container that this extension should be set on.
* @param args The arguments to forward to the constructor of \p T.
*/
template <typename... Args>
inline void SetFwd(Extensible* container, Args&&... args)
{
// Forwarded arguments are for complex types which are assumed to not
// be synced across the network. You can manually call Sync() if this
// is not the case.
if (container->extype == this->extype)
Set(container, new T(std::forward<Args>(args)...), false);
}
/** Removes this extension from the specified container.
* @param container The container that this extension should be removed from.
* @param sync If syncable then whether to sync this unset to the network.
*/
inline void Unset(Extensible* container, bool sync = true)
{
if (container->extype != this->extype)
return;
Delete(container, UnsetRaw(container));
if (synced && sync)
Sync(container, nullptr);
}
};
/** An extension which has a string value. */
class CoreExport StringExtItem
: public SimpleExtItem<std::string>
{
public:
/** Initializes an instance of the StringExtItem class.
* @param owner The module which created the extension.
* @param key The name of the extension (e.g. foo-bar).
* @param exttype The type of extensible that the extension applies to.
* @param sync Whether this extension should be broadcast to other servers.
*/
StringExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false);
/** @copydoc ExtensionItem::FromInternal */
void FromInternal(Extensible* container, const std::string& value) noexcept override;
/** @copydoc ExtensionItem::FromNetwork */
void FromNetwork(Extensible* container, const std::string& value) noexcept override;
/** @copydoc ExtensionItem::ToInternal */
std::string ToInternal(const Extensible* container, void* item) const noexcept override;
/** @copydoc ExtensionItem::ToNetwork */
std::string ToNetwork(const Extensible* container, void* item) const noexcept override;
};
/** An extension which has an integer value. */
class CoreExport IntExtItem
: public ExtensionItem
{
protected:
/** Whether to sync this extension across the network. */
bool synced;
public:
/** Initializes an instance of the IntExtItem class.
* @param owner The module which created the extension.
* @param key The name of the extension (e.g. foo-bar).
* @param exttype The type of extensible that the extension applies to.
* @param sync Whether this extension should be broadcast to other servers.
*/
IntExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false);
/** @copydoc ExtensionItem::Delete */
void Delete(Extensible* container, void* item) override;
/** Retrieves the value for this extension of the specified container.
* @param container The container that this extension is set on.
* @return Either the value of this extension or 0 if it is not set.
*/
intptr_t Get(const Extensible* container) const;
/** @copydoc ExtensionItem::FromInternal */
void FromInternal(Extensible* container, const std::string& value) noexcept override;
/** @copydoc ExtensionItem::FromNetwork */
void FromNetwork(Extensible* container, const std::string& value) noexcept override;
/** Sets a value for this extension of the specified container.
* @param container The container that this extension should be set on.
* @param value The new value to set for this extension.
* @param sync If syncable then whether to sync this set to the network.
*/
void Set(Extensible* container, intptr_t value, bool sync = true);
/** @copydoc ExtensionItem::ToInternal */
std::string ToInternal(const Extensible* container, void* item) const noexcept override;
/** @copydoc ExtensionItem::ToNetwork */
std::string ToNetwork(const Extensible* container, void* item) const noexcept override;
/** Removes this extension from the specified container.
* @param container The container that this extension should be removed from.
* @param sync If syncable then whether to sync this unset to the network.
*/
void Unset(Extensible* container, bool sync = true);
};
/** An extension which has a boolean value. */
class CoreExport BoolExtItem
: public ExtensionItem
{
protected:
/** Whether to sync this extension across the network. */
bool synced;
public:
/** Initializes an instance of the BoolExtItem class.
* @param owner The module which created the extension.
* @param key The name of the extension (e.g. foo-bar).
* @param exttype The type of extensible that the extension applies to.
* @param sync Whether this extension should be broadcast to other servers.
*/
BoolExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false);
/** @copydoc ExtensionItem::Delete */
void Delete(Extensible* container, void* item) override;
/** Retrieves the value for this extension of the specified container.
* @param container The container that this extension is set on.
* @return Either the value of this extension or false if it is not set.
*/
bool Get(const Extensible* container) const;
/** @copydoc ExtensionItem::FromInternal */
void FromInternal(Extensible* container, const std::string& value) noexcept override;
/** @copydoc ExtensionItem::FromNetwork */
void FromNetwork(Extensible* container, const std::string& value) noexcept override;
/** Sets a value for this extension of the specified container.
* @param container The container that this extension should be set on.
* @param sync If syncable then whether to sync this set to the network.
*/
void Set(Extensible* container, bool sync = true);
/** @copydoc ExtensionItem::ToHuman */
std::string ToHuman(const Extensible* container, void* item) const noexcept override;
/** @copydoc ExtensionItem::ToInternal */
std::string ToInternal(const Extensible* container, void* item) const noexcept override;
/** @copydoc ExtensionItem::ToNetwork */
std::string ToNetwork(const Extensible* container, void* item) const noexcept override;
/** Removes this extension from the specified container.
* @param container The container that this extension should be removed from.
* @param sync If syncable then whether to sync this unset to the network.
*/
void Unset(Extensible* container, bool sync = true);
};
|