forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMCPAuthenticator.cpp
130 lines (112 loc) · 4.63 KB
/
GMCPAuthenticator.cpp
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
/***************************************************************************
* Copyright (C) 2024 by Vadim Peretokin - vperetokin@gmail.com *
* *
* This program 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "GMCPAuthenticator.h"
#include "Host.h"
#include "ctelnet.h"
#include <QDebug>
GMCPAuthenticator::GMCPAuthenticator(Host* pHost)
: mpHost(pHost)
{}
void GMCPAuthenticator::saveSupportsSet(const QString& data)
{
auto jsonDoc = QJsonDocument::fromJson(data.toUtf8());
auto jsonObj = jsonDoc.object();
if (jsonObj.contains("type")) {
QJsonArray typesArray = jsonObj["type"].toArray();
for (const auto& type : typesArray) {
mSupportedAuthTypes.append(type.toString());
}
}
#if defined(DEBUG_GMCP_AUTHENTICATION)
qDebug() << "Supported auth types:" << mSupportedAuthTypes;
#endif
}
void GMCPAuthenticator::sendCredentials()
{
auto character = mpHost->getLogin();
auto password = mpHost->getPass();
QJsonObject credentials;
if (!character.isEmpty() && !password.isEmpty()) {
credentials["account"] = character;
credentials["password"] = password;
}
QJsonDocument doc(credentials);
QString gmcpMessage = doc.toJson(QJsonDocument::Compact);
std::string output;
output += TN_IAC;
output += TN_SB;
output += OPT_GMCP;
output += "Char.Login.Credentials ";
output += mpHost->mTelnet.encodeAndCookBytes(gmcpMessage.toStdString());
output += TN_IAC;
output += TN_SE;
// Send credentials to server
mpHost->mTelnet.socketOutRaw(output);
#if defined(DEBUG_GMCP_AUTHENTICATION)
qDebug() << "Sent GMCP credentials";
#endif
}
void GMCPAuthenticator::handleAuthResult(const QString& data)
{
auto doc = QJsonDocument::fromJson(data.toUtf8());
auto obj = doc.object();
// some game drivers can parse JSON for true or false, but may not be able to write booleans back
auto result = obj[qsl("success")];
bool success = (result.isBool() && result.toBool()) || (result.isString() && result.toString() == "true");
auto message = obj[qsl("message")].toString();
if (success) {
#if defined(DEBUG_GMCP_AUTHENTICATION)
qDebug() << "GMCP login successful";
#endif
} else {
#if defined(DEBUG_GMCP_AUTHENTICATION)
qDebug() << "GMCP login failed:" << message;
#endif
mpHost->mTelnet.setDontReconnect(true);
if (message.isEmpty()) {
mpHost->postMessage(tr("[ WARN ] - Could not log in to the game, is the login information correct?"));
} else {
//: %1 shows the reason for failure, could be authentication, etc.
mpHost->postMessage(tr("[ WARN ] - Could not log in to the game: %1").arg(message));
}
}
}
// controller for GMCP authentication
void GMCPAuthenticator::handleAuthGMCP(const QString& packageMessage, const QString& data)
{
if (packageMessage == qsl("Char.Login.Default")) {
saveSupportsSet(data);
if (mSupportedAuthTypes.contains(qsl("password-credentials"))) {
mpHost->mTelnet.cancelLoginTimers();
sendCredentials();
} else {
#if defined(DEBUG_GMCP_AUTHENTICATION)
qDebug() << "Server does not support credentials authentication and we don't support any other";
#endif
}
return;
}
if (packageMessage == qsl("Char.Login.Result")) {
handleAuthResult(data);
return;
}
#if defined(DEBUG_GMCP_AUTHENTICATION)
qDebug() << "Unknown GMCP auth package:" << packageMessage;
#endif
}