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
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2020 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"
#include "modules/cap.h"
#include "modules/ircv3_batch.h"
class LabeledResponseTag : public ClientProtocol::MessageTagProvider
{
private:
const Cap::Capability& cap;
public:
LocalUser* labeluser;
std::string label;
const std::string labeltag;
LabeledResponseTag(Module* mod, const Cap::Capability& capref)
: ClientProtocol::MessageTagProvider(mod)
, cap(capref)
, labeluser(NULL)
, labeltag("label")
{
}
ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
{
if (!irc::equals(tagname, labeltag))
return MOD_RES_PASSTHRU;
// If the tag is empty or too long then we can't accept it.
if (tagvalue.empty() || tagvalue.size() > 64)
return MOD_RES_DENY;
// If the user is local then we check whether they have the labeled-response
// cap enabled. If not then we reject the label tag originating from them.
LocalUser* lu = IS_LOCAL(user);
if (lu && !cap.get(lu))
return MOD_RES_DENY;
// Remote users have their label tag checked by their local server.
return MOD_RES_ALLOW;
}
bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
{
// Messages only have a label when being sent to a user that sent one.
return user == labeluser && tagdata.value == label;
}
};
class ModuleIRCv3LabeledResponse : public Module
{
private:
Cap::Capability cap;
LabeledResponseTag tag;
IRCv3::Batch::API batchmanager;
IRCv3::Batch::Batch batch;
IRCv3::Batch::CapReference batchcap;
ClientProtocol::EventProvider ackmsgprov;
ClientProtocol::EventProvider labelmsgprov;
insp::aligned_storage<ClientProtocol::Message> firstmsg;
size_t msgcount;
void FlushFirstMsg(LocalUser* user)
{
// This isn't a side effect but we treat it like one to avoid the logic in OnUserWrite.
firstmsg->SetSideEffect(true);
user->Send(labelmsgprov, *firstmsg);
firstmsg->~Message();
}
public:
ModuleIRCv3LabeledResponse()
: cap(this, "labeled-response")
, tag(this, cap)
, batchmanager(this)
, batch("labeled-response")
, batchcap(this)
, ackmsgprov(this, "ACK")
, labelmsgprov(this, "labeled")
, msgcount(0)
{
}
ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
{
// We only care about the initial unvalidated OnPreCommand call.
if (validated || tag.labeluser)
return MOD_RES_PASSTHRU;
// We only care about registered users with the labeled-response and batch caps.
if (user->registered != REG_ALL || !cap.get(user) || !batchcap.get(user))
return MOD_RES_PASSTHRU;
const ClientProtocol::TagMap& tagmap = parameters.GetTags();
const ClientProtocol::TagMap::const_iterator labeltag = tagmap.find(tag.labeltag);
if (labeltag == tagmap.end())
return MOD_RES_PASSTHRU;
tag.label = labeltag->second.value;
tag.labeluser = user;
return MOD_RES_PASSTHRU;
}
void OnPostCommand(Command* command, const CommandBase::Params& parameters, LocalUser* user, CmdResult result, bool loop) CXX11_OVERRIDE
{
// Do nothing if this isn't the last OnPostCommand() run for the command.
//
// If a parameter for the command was originally a list and the command handler chose to be executed
// for each element on the list with synthesized parameters (CommandHandler::LoopCall) then this hook
// too will run for each element on the list plus once after the whole list has been processed.
// loop will only be false for the last run.
if (!loop)
OnCommandBlocked(command->name, parameters, user);
}
void OnCommandBlocked(const std::string& command, const CommandBase::Params& parameters, LocalUser* user) CXX11_OVERRIDE
{
// If no label was sent we don't have to do anything.
if (!tag.labeluser)
return;
switch (msgcount)
{
case 0:
{
// There was no response so we send an ACK instead.
ClientProtocol::Message ackmsg("ACK", ServerInstance->FakeClient);
ackmsg.AddTag(tag.labeltag, &tag, tag.label);
ackmsg.SetSideEffect(true);
tag.labeluser->Send(ackmsgprov, ackmsg);
break;
}
case 1:
{
// There was one response which was cached; send it now.
firstmsg->AddTag(tag.labeltag, &tag, tag.label);
FlushFirstMsg(user);
break;
}
default:
{
// There was two or more responses; send an end-of-batch.
if (batchmanager)
{
// Set end start as side effect so we'll ignore it otherwise it'd end up added into the batch.
batch.GetBatchEndMessage().SetSideEffect(true);
batchmanager->End(batch);
}
break;
}
}
tag.labeluser = NULL;
msgcount = 0;
}
ModResult OnUserWrite(LocalUser* user, ClientProtocol::Message& msg) CXX11_OVERRIDE
{
// The label user is writing a message to another user.
if (user != tag.labeluser)
return MOD_RES_PASSTHRU;
// The message is a side effect (e.g. a self-PRIVMSG).
if (msg.IsSideEffect())
return MOD_RES_PASSTHRU;
switch (++msgcount)
{
case 1:
{
// First reply message. We can' send it yet because we don't know if there will be more.
new(firstmsg) ClientProtocol::Message(msg);
firstmsg->CopyAll();
return MOD_RES_DENY;
}
case 2:
{
// Second reply message. This and all subsequent messages need to go into a batch.
if (batchmanager)
{
batchmanager->Start(batch);
// Set batch start as side effect so we'll ignore it otherwise it'd end up added into the batch.
ClientProtocol::Message& batchstartmsg = batch.GetBatchStartMessage();
batchstartmsg.SetSideEffect(true);
batchstartmsg.AddTag(tag.labeltag, &tag, tag.label);
batch.AddToBatch(*firstmsg);
batch.AddToBatch(msg);
}
// Flush first message which triggers the batch start message
FlushFirstMsg(user);
return MOD_RES_PASSTHRU;
}
default:
{
// Third or later message. Put it in the batch and send directly.
if (batchmanager)
batch.AddToBatch(msg);
return MOD_RES_PASSTHRU;
}
}
}
void Prioritize() CXX11_OVERRIDE
{
Module* alias = ServerInstance->Modules->Find("m_alias.so");
ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, alias);
}
Version GetVersion() CXX11_OVERRIDE
{
return Version("Provides support for the IRCv3 Labeled Response specification.", VF_VENDOR);
}
};
MODULE_INIT(ModuleIRCv3LabeledResponse)
|