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
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2018 Attila Molnar <attilamolnar@hush.com>
*
* 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"
#include "modules/cap.h"
#include "modules/ircv3_batch.h"
class BatchMessage : public ClientProtocol::Message
{
public:
BatchMessage(const IRCv3::Batch::Batch& batch, bool start)
: ClientProtocol::Message("BATCH", ServerInstance->Config->GetServerName())
{
char c = (start ? '+' : '-');
PushParam(std::string(1, c) + batch.GetRefTagStr());
if ((start) && (!batch.GetType().empty()))
PushParamRef(batch.GetType());
}
};
/** Extra structure allocated only for running batches, containing objects only relevant for
* that specific run of the batch.
*/
struct IRCv3::Batch::BatchInfo
{
/** List of users that have received the batch start message
*/
std::vector<LocalUser*> users;
BatchMessage startmsg;
ClientProtocol::Event startevent;
BatchMessage endmsg;
ClientProtocol::Event endevent;
BatchInfo(ClientProtocol::EventProvider& protoevprov, IRCv3::Batch::Batch& b)
: startmsg(b, true)
, startevent(protoevprov, startmsg)
, endmsg(b, false)
, endevent(protoevprov, endmsg)
{
}
};
class IRCv3::Batch::ManagerImpl : public Manager
{
typedef std::vector<Batch*> BatchList;
Cap::Capability cap;
ClientProtocol::EventProvider protoevprov;
LocalIntExt batchbits;
BatchList active_batches;
bool unloading;
bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
{
if (!cap.get(user))
return false;
Batch& batch = *static_cast<Batch*>(tagdata.provdata);
// Check if this is the first message the user is getting that is part of the batch
const intptr_t bits = batchbits.get(user);
if (!(bits & batch.GetBit()))
{
// Send the start batch command ("BATCH +reftag TYPE"), remember the user so we can send them a
// "BATCH -reftag" message later when the batch ends and set the flag we just checked so this is
// only done once per user per batch.
batchbits.set(user, (bits | batch.GetBit()));
batch.batchinfo->users.push_back(user);
user->Send(batch.batchinfo->startevent);
}
return true;
}
unsigned int NextFreeId() const
{
if (active_batches.empty())
return 0;
return active_batches.back()->GetId()+1;
}
public:
ManagerImpl(Module* mod)
: Manager(mod)
, cap(mod, "batch")
, protoevprov(mod, "BATCH")
, batchbits("batchbits", ExtensionItem::EXT_USER, mod)
, unloading(false)
{
}
void Init()
{
// Set batchbits to 0 for all users in case we were reloaded and the previous, now meaningless,
// batchbits are set on users
const UserManager::LocalList& users = ServerInstance->Users.GetLocalUsers();
for (UserManager::LocalList::const_iterator i = users.begin(); i != users.end(); ++i)
{
LocalUser* const user = *i;
batchbits.set(user, 0);
}
}
void Shutdown()
{
unloading = true;
while (!active_batches.empty())
ManagerImpl::End(*active_batches.back());
}
void RemoveFromAll(LocalUser* user)
{
const intptr_t bits = batchbits.get(user);
// User is quitting, remove them from all lists
for (BatchList::iterator i = active_batches.begin(); i != active_batches.end(); ++i)
{
Batch& batch = **i;
// Check the bit first to avoid list scan in case they're not on the list
if ((bits & batch.GetBit()) != 0)
stdalgo::vector::swaperase(batch.batchinfo->users, user);
}
}
void Start(Batch& batch) CXX11_OVERRIDE
{
if (unloading)
return;
if (batch.IsRunning())
return; // Already started, don't start again
const size_t id = NextFreeId();
if (id >= MAX_BATCHES)
return;
batch.Setup(id);
// Set the manager field which Batch::IsRunning() checks and is also used by AddToBatch()
// to set the message tag
batch.manager = this;
batch.batchinfo = new IRCv3::Batch::BatchInfo(protoevprov, batch);
batch.batchstartmsg = &batch.batchinfo->startmsg;
batch.batchendmsg = &batch.batchinfo->endmsg;
active_batches.push_back(&batch);
}
void End(Batch& batch) CXX11_OVERRIDE
{
if (!batch.IsRunning())
return;
// Mark batch as stopped
batch.manager = NULL;
BatchInfo& batchinfo = *batch.batchinfo;
// Send end batch message to all users who got the batch start message and unset bit so it can be reused
for (std::vector<LocalUser*>::const_iterator i = batchinfo.users.begin(); i != batchinfo.users.end(); ++i)
{
LocalUser* const user = *i;
user->Send(batchinfo.endevent);
batchbits.set(user, batchbits.get(user) & ~batch.GetBit());
}
// erase() not swaperase because the reftag generation logic depends on the order of the elements
stdalgo::erase(active_batches, &batch);
delete batch.batchinfo;
batch.batchinfo = NULL;
}
};
class ModuleIRCv3Batch : public Module
{
IRCv3::Batch::ManagerImpl manager;
public:
ModuleIRCv3Batch()
: manager(this)
{
}
void init() CXX11_OVERRIDE
{
manager.Init();
}
void OnUnloadModule(Module* mod) CXX11_OVERRIDE
{
if (mod == this)
manager.Shutdown();
}
void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
{
// Remove the user from all internal lists
manager.RemoveFromAll(user);
}
Version GetVersion() CXX11_OVERRIDE
{
return Version("Provides the IRCv3 batch client capability.", VF_VENDOR);
}
};
MODULE_INIT(ModuleIRCv3Batch)
|