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
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2026 Sadie Powell <sadie@witchery.services>
*
* 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/>.
*/
#include "inspircd.h"
static insp::intrusive_list<dynamic_reference_base>* dynrefs = nullptr;
dynamic_reference_base::dynamic_reference_base(const WeakModulePtr& mod, const std::string& stype, const std::string& sname, bool strict)
: service_name(sname)
, service_type(stype)
, strict_ref(strict)
, creator(mod)
{
if (!dynrefs)
dynrefs = new insp::intrusive_list<dynamic_reference_base>;
dynrefs->push_front(this);
// Resolve unless there is no ModuleManager (part of class InspIRCd)
if (ServerInstance)
resolve();
}
dynamic_reference_base::dynamic_reference_base(const dynamic_reference_base& other)
: dynamic_reference_base(other.creator, other.service_type, other.service_name, other.strict_ref)
{
// Intentionally left blank.
}
dynamic_reference_base::~dynamic_reference_base()
{
dynrefs->erase(this);
if (dynrefs->empty())
insp::delete_zero(dynrefs);
}
void dynamic_reference_base::reset_all(const std::string& stype)
{
if (!dynrefs)
return;
for (auto* dynref : *dynrefs)
{
if (stype.empty() || dynref->service_type == stype)
dynref->resolve();
}
}
void dynamic_reference_base::resolve()
{
auto* oldvalue = this->value;
this->value = nullptr;
// Because find() may return any element with a matching key in case count(key) > 1 use lower_bound()
// to ensure a dynref with the same name as another one resolves to the same object
auto i = ServerInstance->Modules.Services.lower_bound(std::make_pair(this->service_type, this->service_name));
if (i == ServerInstance->Modules.Services.end())
return; // No service found.
const auto& [stype, sname] = i->first;
if (!insp::casemapped_equals(this->service_type, stype) || !insp::casemapped_equals(this->service_name, sname))
return; // No service found.
if (this->strict_ref && sname.empty())
return; // Can't use generic services for strict references.
this->value = i->second;
if (oldvalue != value && hook)
hook->OnCapture();
}
void dynamic_reference_base::ClearProviderName()
{
this->service_name.clear();
value = nullptr;
}
void dynamic_reference_base::SetProvider(const std::string& stype, const std::string& sname)
{
this->service_type = stype;
this->service_name = sname;
resolve();
}
void dynamic_reference_base::SetProviderName(const std::string& sname)
{
SetProvider(GetProviderType(), sname);
}
Service::Provider::Provider(const WeakModulePtr& mod, const std::string& stype, const std::string& sname)
: service_creator(mod)
, service_name(sname)
, service_type(stype)
{
if ((ServerInstance) && (ServerInstance->Modules.NewServices))
ServerInstance->Modules.NewServices->push_back(this);
}
void Service::Provider::DisableAutoRegister()
{
if ((ServerInstance) && (ServerInstance->Modules.NewServices))
std::erase(*ServerInstance->Modules.NewServices, this);
}
std::string Service::Provider::GetSource() const
{
if (insp::empty_ptr(this->service_creator))
return "the core";
const auto mod = this->service_creator.lock();
if (!mod)
return "an unknown module";
return FMT::format("the {} module", ModuleManager::ShrinkModName(mod->ModuleFile));
}
void Service::Provider::RegisterService()
{
// Intentionally left blank.
}
void Service::Provider::UnregisterService()
{
// Intentionally left blank.
}
Service::SimpleProvider::SimpleProvider(const WeakModulePtr& mod, const std::string& stype, const std::string& sname)
: Service::Provider(mod, stype, sname)
{
}
void Service::SimpleProvider::RegisterService()
{
if (!this->service_name.empty())
ServerInstance->Modules.AddReferent(this->service_type, this->service_name, this);
ServerInstance->Modules.AddReferent(this->service_type, "", this);
}
void Service::SimpleProvider::UnregisterService()
{
ServerInstance->Modules.DelReferent(this);
}
|