1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2018-2019, 2021 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012, 2014-2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 
26 #include "inspircd.h"
27 #include "base.h"
28 
29 #ifdef INSPIRCD_ENABLE_RTTI
30 #include <typeinfo>
31 #endif
32 
classbase()33 classbase::classbase()
34 {
35 #ifdef INSPIRCD_ENABLE_RTTI
36 	if (ServerInstance)
37 		ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+%s @%p", typeid(*this).name(), (void*)this);
38 #endif
39 }
40 
cull()41 CullResult classbase::cull()
42 {
43 #ifdef INSPIRCD_ENABLE_RTTI
44 	if (ServerInstance)
45 		ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p", typeid(*this).name(), (void*)this);
46 #endif
47 	return CullResult();
48 }
49 
~classbase()50 classbase::~classbase()
51 {
52 #ifdef INSPIRCD_ENABLE_RTTI
53 	if (ServerInstance)
54 		ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::~%s @%p", typeid(*this).name(), (void*)this);
55 #endif
56 }
57 
CullResult()58 CullResult::CullResult()
59 {
60 }
61 
62 // This trick detects heap allocations of refcountbase objects
63 static void* last_heap = NULL;
64 
operator new(size_t size)65 void* refcountbase::operator new(size_t size)
66 {
67 	last_heap = ::operator new(size);
68 	return last_heap;
69 }
70 
operator delete(void * obj)71 void refcountbase::operator delete(void* obj)
72 {
73 	if (last_heap == obj)
74 		last_heap = NULL;
75 	::operator delete(obj);
76 }
77 
refcountbase()78 refcountbase::refcountbase() : refcount(0)
79 {
80 	if (this != last_heap)
81 		throw CoreException("Reference allocate on the stack!");
82 }
83 
~refcountbase()84 refcountbase::~refcountbase()
85 {
86 	if (refcount && ServerInstance)
87 		ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
88 			(void*)this, refcount);
89 }
90 
~usecountbase()91 usecountbase::~usecountbase()
92 {
93 	if (usecount && ServerInstance)
94 		ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "usecountbase::~ @%p with refcount %d",
95 			(void*)this, usecount);
96 }
97 
~ServiceProvider()98 ServiceProvider::~ServiceProvider()
99 {
100 }
101 
RegisterService()102 void ServiceProvider::RegisterService()
103 {
104 }
105 
ExtensionItem(const std::string & Key,ExtensibleType exttype,Module * mod)106 ExtensionItem::ExtensionItem(const std::string& Key, ExtensibleType exttype, Module* mod)
107 	: ServiceProvider(mod, Key, SERVICE_METADATA)
108 	, type(exttype)
109 {
110 }
111 
~ExtensionItem()112 ExtensionItem::~ExtensionItem()
113 {
114 }
115 
get_raw(const Extensible * container) const116 void* ExtensionItem::get_raw(const Extensible* container) const
117 {
118 	Extensible::ExtensibleStore::const_iterator i =
119 		container->extensions.find(const_cast<ExtensionItem*>(this));
120 	if (i == container->extensions.end())
121 		return NULL;
122 	return i->second;
123 }
124 
set_raw(Extensible * container,void * value)125 void* ExtensionItem::set_raw(Extensible* container, void* value)
126 {
127 	std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
128 		container->extensions.insert(std::make_pair(this, value));
129 	if (rv.second)
130 	{
131 		return NULL;
132 	}
133 	else
134 	{
135 		void* old = rv.first->second;
136 		rv.first->second = value;
137 		return old;
138 	}
139 }
140 
unset_raw(Extensible * container)141 void* ExtensionItem::unset_raw(Extensible* container)
142 {
143 	Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
144 	if (i == container->extensions.end())
145 		return NULL;
146 	void* rv = i->second;
147 	container->extensions.erase(i);
148 	return rv;
149 }
150 
RegisterService()151 void ExtensionItem::RegisterService()
152 {
153 	if (!ServerInstance->Extensions.Register(this))
154 		throw ModuleException("Extension already exists: " + name);
155 }
156 
Register(ExtensionItem * item)157 bool ExtensionManager::Register(ExtensionItem* item)
158 {
159 	return types.insert(std::make_pair(item->name, item)).second;
160 }
161 
BeginUnregister(Module * module,std::vector<reference<ExtensionItem>> & list)162 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
163 {
164 	ExtMap::iterator i = types.begin();
165 	while (i != types.end())
166 	{
167 		ExtMap::iterator me = i++;
168 		ExtensionItem* item = me->second;
169 		if (item->creator == module)
170 		{
171 			list.push_back(item);
172 			types.erase(me);
173 		}
174 	}
175 }
176 
GetItem(const std::string & name)177 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
178 {
179 	ExtMap::iterator i = types.find(name);
180 	if (i == types.end())
181 		return NULL;
182 	return i->second;
183 }
184 
doUnhookExtensions(const std::vector<reference<ExtensionItem>> & toRemove)185 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
186 {
187 	for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
188 	{
189 		ExtensionItem* item = *i;
190 		ExtensibleStore::iterator e = extensions.find(item);
191 		if (e != extensions.end())
192 		{
193 			item->free(this, e->second);
194 			extensions.erase(e);
195 		}
196 	}
197 }
198 
Extensible()199 Extensible::Extensible()
200 	: culled(false)
201 {
202 }
203 
cull()204 CullResult Extensible::cull()
205 {
206 	FreeAllExtItems();
207 	culled = true;
208 	return classbase::cull();
209 }
210 
FreeAllExtItems()211 void Extensible::FreeAllExtItems()
212 {
213 	for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
214 	{
215 		i->first->free(this, i->second);
216 	}
217 	extensions.clear();
218 }
219 
~Extensible()220 Extensible::~Extensible()
221 {
222 	if ((!extensions.empty() || !culled) && ServerInstance)
223 		ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
224 }
225 
FromInternal(Extensible * container,const std::string & value)226 void ExtensionItem::FromInternal(Extensible* container, const std::string& value)
227 {
228 	FromNetwork(container, value);
229 }
230 
FromNetwork(Extensible * container,const std::string & value)231 void ExtensionItem::FromNetwork(Extensible* container, const std::string& value)
232 {
233 }
234 
ToHuman(const Extensible * container,void * item) const235 std::string ExtensionItem::ToHuman(const Extensible* container, void* item) const
236 {
237 	// Try to use the network form by default.
238 	std::string ret = ToNetwork(container, item);
239 
240 	// If there's no network form then fall back to the internal form.
241 	if (ret.empty())
242 		ret = ToInternal(container, item);
243 
244 	return ret;
245 }
246 
ToInternal(const Extensible * container,void * item) const247 std::string ExtensionItem::ToInternal(const Extensible* container, void* item) const
248 {
249 	return ToNetwork(container, item);
250 }
251 
ToNetwork(const Extensible * container,void * item) const252 std::string ExtensionItem::ToNetwork(const Extensible* container, void* item) const
253 {
254 	return std::string();
255 }
256 
serialize(SerializeFormat format,const Extensible * container,void * item) const257 std::string ExtensionItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
258 {
259 	// Wrap the deprecated API with the new API.
260 	switch (format)
261 	{
262 		case FORMAT_USER:
263 			return ToHuman(container, item);
264 		case FORMAT_INTERNAL:
265 		case FORMAT_PERSIST:
266 			return ToInternal(container, item);
267 		case FORMAT_NETWORK:
268 			return ToNetwork(container, item);
269 	}
270 	return "";
271 }
272 
273 
unserialize(SerializeFormat format,Extensible * container,const std::string & value)274 void ExtensionItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
275 {
276 	// Wrap the deprecated API with the new API.
277 	switch (format)
278 	{
279 		case FORMAT_USER:
280 			break;
281 		case FORMAT_INTERNAL:
282 		case FORMAT_PERSIST:
283 			FromInternal(container, value);
284 			break;
285 		case FORMAT_NETWORK:
286 			FromNetwork(container, value);
287 			break;
288 	}
289 }
290 
LocalStringExt(const std::string & Key,ExtensibleType exttype,Module * Owner)291 LocalStringExt::LocalStringExt(const std::string& Key, ExtensibleType exttype, Module* Owner)
292 	: SimpleExtItem<std::string>(Key, exttype, Owner)
293 {
294 }
295 
~LocalStringExt()296 LocalStringExt::~LocalStringExt()
297 {
298 }
299 
ToInternal(const Extensible * container,void * item) const300 std::string LocalStringExt::ToInternal(const Extensible* container, void* item) const
301 {
302 	return item ? *static_cast<std::string*>(item) : std::string();
303 }
304 
FromInternal(Extensible * container,const std::string & value)305 void LocalStringExt::FromInternal(Extensible* container, const std::string& value)
306 {
307 	set(container, value);
308 }
309 
LocalIntExt(const std::string & Key,ExtensibleType exttype,Module * mod)310 LocalIntExt::LocalIntExt(const std::string& Key, ExtensibleType exttype, Module* mod)
311 	: ExtensionItem(Key, exttype, mod)
312 {
313 }
314 
~LocalIntExt()315 LocalIntExt::~LocalIntExt()
316 {
317 }
318 
ToInternal(const Extensible * container,void * item) const319 std::string LocalIntExt::ToInternal(const Extensible* container, void* item) const
320 {
321 	return ConvToStr(reinterpret_cast<intptr_t>(item));
322 }
323 
FromInternal(Extensible * container,const std::string & value)324 void LocalIntExt::FromInternal(Extensible* container, const std::string& value)
325 {
326 	set(container, ConvToNum<intptr_t>(value));
327 }
328 
get(const Extensible * container) const329 intptr_t LocalIntExt::get(const Extensible* container) const
330 {
331 	return reinterpret_cast<intptr_t>(get_raw(container));
332 }
333 
set(Extensible * container,intptr_t value)334 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
335 {
336 	if (value)
337 		return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
338 	else
339 		return reinterpret_cast<intptr_t>(unset_raw(container));
340 }
341 
free(Extensible * container,void * item)342 void LocalIntExt::free(Extensible* container, void* item)
343 {
344 }
345 
StringExtItem(const std::string & Key,ExtensibleType exttype,Module * mod)346 StringExtItem::StringExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
347 	: ExtensionItem(Key, exttype, mod)
348 {
349 }
350 
~StringExtItem()351 StringExtItem::~StringExtItem()
352 {
353 }
354 
get(const Extensible * container) const355 std::string* StringExtItem::get(const Extensible* container) const
356 {
357 	return static_cast<std::string*>(get_raw(container));
358 }
359 
ToNetwork(const Extensible * container,void * item) const360 std::string StringExtItem::ToNetwork(const Extensible* container, void* item) const
361 {
362 	return item ? *static_cast<std::string*>(item) : std::string();
363 }
364 
FromNetwork(Extensible * container,const std::string & value)365 void StringExtItem::FromNetwork(Extensible* container, const std::string& value)
366 {
367 	if (value.empty())
368 		unset(container);
369 	else
370 		set(container, value);
371 }
372 
set(Extensible * container,const std::string & value)373 void StringExtItem::set(Extensible* container, const std::string& value)
374 {
375 	void* old = set_raw(container, new std::string(value));
376 	free(container, old);
377 }
378 
unset(Extensible * container)379 void StringExtItem::unset(Extensible* container)
380 {
381 	void* old = unset_raw(container);
382 	free(container, old);
383 }
384 
free(Extensible * container,void * item)385 void StringExtItem::free(Extensible* container, void* item)
386 {
387 	delete static_cast<std::string*>(item);
388 }
389 
ModuleException(const std::string & message,Module * who)390 ModuleException::ModuleException(const std::string &message, Module* who)
391 	: CoreException(message, who ? who->ModuleSourceFile : "A Module")
392 {
393 }
394