forked from MUME/MMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTelnet.cpp
More file actions
289 lines (249 loc) · 9.13 KB
/
Copy pathUserTelnet.cpp
File metadata and controls
289 lines (249 loc) · 9.13 KB
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
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2019 The MMapper Authors
// Author: Nils Schimmelmann <nschimme@gmail.com> (Jahara)
#include "UserTelnet.h"
#include "../global/AnsiTextUtils.h"
#include "../global/CharUtils.h"
#include "../global/Charset.h"
#include "../global/Consts.h"
#include "../global/LineUtils.h"
#include "../global/TextUtils.h"
#include "../global/emojis.h"
#include <ostream>
#include <sstream>
// REVISIT: Does this belong somewhere else?
// REVISIT: Should this also normalize ANSI?
static void normalizeForUser(std::ostream &os, const bool goAhead, const std::string_view sv)
{
// REVISIT: perform ANSI normalization in this function, too?
foreachLine(sv, [&os, goAhead](std::string_view line) {
if (line.empty()) {
return;
}
// const bool hasAnsi = containsAnsi(line);
const bool hasNewline = line.back() == char_consts::C_NEWLINE;
trim_newline_inplace(line);
if (!line.empty()) {
using char_consts::C_CARRIAGE_RETURN;
foreachCharMulti2(line, C_CARRIAGE_RETURN, [&os, goAhead](std::string_view txt) {
// so it's only allowed to contain carriage returns if goAhead is true?
// why not just remove all of the extra carriage returns?
if (!txt.empty() && (txt.front() != C_CARRIAGE_RETURN || goAhead)) {
os << txt;
}
});
}
if (hasNewline) {
// REVISIT: add an Ansi reset if the string doesn't contain one?
// if (hasAnsi) {}
os << string_consts::SV_CRLF;
}
});
}
NODISCARD static QString normalizeForUser(const CharacterEncodingEnum userEncoding,
const QString &s,
const bool goAhead)
{
const auto out = std::invoke([goAhead, &userEncoding, &s]() -> std::string {
std::ostringstream oss;
normalizeForUser(oss,
goAhead,
mmqt::toStdStringUtf8((getConfig().parser.decodeEmoji
&& userEncoding == CharacterEncodingEnum::UTF8
&& s.contains(char_consts::C_COLON))
? mmqt::decodeEmojiShortCodes(s)
: s));
return std::move(oss).str();
});
return mmqt::toQStringUtf8(out);
}
NODISCARD static RawBytes decodeFromUser(const CharacterEncodingEnum userEncoding,
const RawBytes &raw)
{
if (userEncoding == CharacterEncodingEnum::UTF8) {
return raw;
}
std::ostringstream oss;
charset::conversion::convert(oss,
mmqt::toStdStringViewRaw(raw.getQByteArray()),
userEncoding,
CharacterEncodingEnum::UTF8);
return RawBytes{mmqt::toQByteArrayUtf8(oss.str())};
}
UserTelnetOutputs::~UserTelnetOutputs() = default;
UserTelnet::UserTelnet(UserTelnetOutputs &outputs)
: AbstractTelnet(TextCodecStrategyEnum::AUTO_SELECT_CODEC, TelnetTermTypeBytes{"unknown"})
, m_outputs{outputs}
{}
void UserTelnet::onConnected()
{
reset();
resetGmcpModules();
// Negotiate options
requestTelnetOption(TN_DO, OPT_TERMINAL_TYPE);
requestTelnetOption(TN_DO, OPT_NAWS);
requestTelnetOption(TN_DO, OPT_CHARSET);
// Most clients expect the server (i.e. MMapper) to send IAC WILL GMCP
requestTelnetOption(TN_WILL, OPT_GMCP);
// Request permission to replace IAC GA with IAC EOR
requestTelnetOption(TN_WILL, OPT_EOR);
}
void UserTelnet::onAnalyzeUserStream(const TelnetIacBytes &data)
{
onReadInternal(data);
}
void UserTelnet::onSendToUser(const QString &s, const bool goAhead)
{
auto outdata = normalizeForUser(getEncoding(), s, goAhead);
submitOverTelnet(outdata, goAhead);
}
void UserTelnet::onGmcpToUser(const GmcpMessage &msg)
{
if (!getOptions().myOptionState[OPT_GMCP]) {
return;
}
const auto name = msg.getName().getStdStringUtf8();
const std::size_t found = name.find_last_of(char_consts::C_PERIOD);
try {
const GmcpModule mod{name.substr(0, found)};
if (m_gmcp.modules.find(mod) != m_gmcp.modules.end()) {
sendGmcpMessage(msg);
}
} catch (const std::exception &e) {
qWarning() << "Message" << msg.toRawBytes() << "error because:" << e.what();
}
}
void UserTelnet::onSendMSSPToUser(const TelnetMsspBytes &data)
{
if (!getOptions().myOptionState[OPT_MSSP]) {
return;
}
sendMudServerStatus(data);
}
void UserTelnet::virt_sendToMapper(const RawBytes &data, const bool goAhead)
{
m_outputs.onAnalyzeUserStream(decodeFromUser(getEncoding(), data), goAhead);
}
void UserTelnet::onRelayEchoMode(const bool isDisabled)
{
sendTelnetOption(isDisabled ? TN_WONT : TN_WILL, OPT_ECHO);
// REVISIT: This is he only non-const use of the options variable;
// it could be refactored so the base class does the writes.
m_options.myOptionState[OPT_ECHO] = !isDisabled;
m_options.announcedState[OPT_ECHO] = true;
}
void UserTelnet::virt_receiveGmcpMessage(const GmcpMessage &msg)
{
// Eat Core.Hello since MMapper sends its own to MUME
if (msg.isCoreHello()) {
return;
}
// Eat External.Discord.Get as MUME does not support it and would return a MUME.Client.Error
if (msg.isExternalDiscordGet()) {
return;
}
const bool requiresRewrite = msg.getJson()
&& (msg.isCoreSupportsAdd() || msg.isCoreSupportsSet()
|| msg.isCoreSupportsRemove())
&& msg.getJsonDocument().has_value()
&& msg.getJsonDocument()->getArray().has_value();
if (!requiresRewrite) {
m_outputs.onRelayGmcpFromUserToMud(msg);
return;
}
// Eat Core.Supports.[Add|Set|Remove] and proxy a MMapper filtered subset
// Handle the various messages
if (msg.isCoreSupportsSet()) {
resetGmcpModules();
}
if (const auto optArray = msg.getJsonDocument()->getArray()) {
for (const auto &e : optArray.value()) {
if (auto optString = e.getString()) {
const auto &module = optString.value();
try {
const GmcpModule mod{mmqt::toStdStringUtf8(module)};
receiveGmcpModule(mod, !msg.isCoreSupportsRemove());
} catch (const std::exception &e) {
qWarning() << "Module" << module
<< (msg.isCoreSupportsRemove() ? "remove" : "add")
<< "error because:" << e.what();
}
}
}
}
// Filter MMapper-internal GMCP modules to proxy on to MUME
std::ostringstream oss;
oss << "[ ";
bool comma = false;
for (const GmcpModule &mod : m_gmcp.modules) {
// REVISIT: Are some MMapper supported modules not supposed to be filtered?
if (mod.isSupported()) {
continue;
}
if (comma) {
oss << ", ";
}
oss << char_consts::C_DQUOTE << mod.toStdString() << char_consts::C_DQUOTE;
comma = true;
}
oss << " ]";
if (!comma) {
if (getDebug()) {
qDebug() << "All modules were supported or nothing was requested";
}
return;
}
const GmcpMessage filteredMsg(GmcpMessageTypeEnum::CORE_SUPPORTS_SET,
GmcpJson{std::move(oss).str()});
m_outputs.onRelayGmcpFromUserToMud(filteredMsg);
}
void UserTelnet::virt_receiveTerminalType(const TelnetTermTypeBytes &data)
{
if (getDebug()) {
qDebug() << "Received Terminal Type" << data;
}
m_outputs.onRelayTermTypeFromUserToMud(data);
}
void UserTelnet::virt_receiveWindowSize(const int x, const int y)
{
m_outputs.onRelayNawsFromUserToMud(x, y);
}
void UserTelnet::virt_sendRawData(const TelnetIacBytes &data)
{
m_outputs.onSendToSocket(data);
}
bool UserTelnet::virt_isGmcpModuleEnabled(const GmcpModuleTypeEnum &name) const
{
if (!getOptions().myOptionState[OPT_GMCP]) {
return false;
}
return m_gmcp.supported[name] != DEFAULT_GMCP_MODULE_VERSION;
}
void UserTelnet::receiveGmcpModule(const GmcpModule &mod, const bool enabled)
{
if (enabled) {
if (!mod.hasVersion()) {
throw std::runtime_error("missing version");
}
m_gmcp.modules.insert(mod);
if (mod.isSupported()) {
m_gmcp.supported[mod.getType()] = mod.getVersion();
}
} else {
m_gmcp.modules.erase(mod);
if (mod.isSupported()) {
m_gmcp.supported[mod.getType()] = DEFAULT_GMCP_MODULE_VERSION;
}
}
}
void UserTelnet::resetGmcpModules()
{
if (getDebug()) {
qDebug() << "Clearing GMCP modules";
}
#define X_CASE(UPPER_CASE, CamelCase, normalized, friendly) \
m_gmcp.supported[GmcpModuleTypeEnum::UPPER_CASE] = DEFAULT_GMCP_MODULE_VERSION;
XFOREACH_GMCP_MODULE_TYPE(X_CASE)
#undef X_CASE
m_gmcp.modules.clear();
}