Skip to content

Commit

Permalink
Merge branch 'development' into feat/sentry-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kebap committed Apr 10, 2024
2 parents 1cef8df + 0ac53a3 commit 74b60cc
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 33 deletions.
47 changes: 26 additions & 21 deletions src/CMakeLists.txt
Expand Up @@ -67,8 +67,8 @@ set(mudlet_SRCS
dlgConnectionProfiles.cpp
dlgIRC.cpp
dlgKeysMainArea.cpp
dlgMapper.cpp
dlgMapLabel.cpp
dlgMapper.cpp
dlgModuleManager.cpp
dlgNotepad.cpp
dlgPackageExporter.cpp
Expand All @@ -87,10 +87,10 @@ set(mudlet_SRCS
dlgVarsMainArea.cpp
EAction.cpp
exitstreewidget.cpp
FontManager.cpp
FileOpenHandler.cpp
FontManager.cpp
GifTracker.cpp
TrailingWhitespaceMarker.cpp
GMCPAuthenticator.cpp
Host.cpp
HostManager.cpp
ircmessageformatter.cpp
Expand All @@ -100,8 +100,9 @@ set(mudlet_SRCS
mapInfoContributorManager.cpp
mudlet.cpp
MudletInstanceCoordinator.cpp
ShortcutsManager.cpp
MxpTag.cpp
ScriptUnit.cpp
ShortcutsManager.cpp
T2DMap.cpp
TAccessibleTextEdit.cpp
TAction.cpp
Expand All @@ -121,7 +122,6 @@ set(mudlet_SRCS
TimerUnit.cpp
TKey.cpp
TLabel.cpp
TScrollBox.cpp
TLinkStore.cpp

TLuaInterpreter.cpp
Expand All @@ -142,26 +142,30 @@ set(mudlet_SRCS
TMxpBRTagHandler.cpp
TMxpColorTagHandler.cpp
TMxpCustomElementTagHandler.cpp
TMxpElementDefinitionHandler.cpp
TMxpElementRegistry.cpp
TMxpEntityTagHandler.cpp
TMxpFontTagHandler.cpp
TMxpFormattingTagsHandler.cpp
TMxpLinkTagHandler.cpp
TMxpMudlet.cpp
TMxpMusicTagHandler.cpp
TMxpNodeBuilder.cpp
TMxpMudlet.cpp
TMxpProcessor.cpp
TMxpSendTagHandler.cpp
TMxpSoundTagHandler.cpp
TMxpSupportTagHandler.cpp
MxpTag.cpp
TMxpTagHandler.cpp
TMxpTagParser.cpp
TMxpTagProcessor.cpp
TMxpVarTagHandler.cpp
TMxpVersionTagHandler.cpp
TrailingWhitespaceMarker.cpp
TriggerUnit.cpp
TRoom.cpp
TRoomDB.cpp
TScript.cpp
TScrollBox.cpp
TSplitter.cpp
TSplitterHandle.cpp
TStringUtils.cpp
Expand Down Expand Up @@ -218,9 +222,9 @@ set(mudlet_UIS

set(mudlet_HDRS
ActionUnit.h
Announcer.h
AliasUnit.h
AltFocusMenuBarDisable.h
Announcer.h
ctelnet.h
DarkTheme.h
discord.h
Expand Down Expand Up @@ -255,7 +259,7 @@ set(mudlet_HDRS
FileOpenHandler.h
FontManager.h
GifTracker.h
TrailingWhitespaceMarker.h
GMCPAuthenticator.h
Host.h
HostManager.h
ircmessageformatter.h
Expand All @@ -264,10 +268,11 @@ set(mudlet_HDRS
mapInfoContributorManager.h
mudlet.h
MudletInstanceCoordinator.h
MxpTag.h
post_guard.h
pre_guard.h
ShortcutsManager.h
ScriptUnit.h
ShortcutsManager.h
T2DMap.h
TAccessibleConsole.h
TAccessibleTextEdit.h
Expand All @@ -286,12 +291,11 @@ set(mudlet_HDRS
TEntityResolver.h
testdbg.h
TEvent.h
TForkedProcess.h
TFlipButton.h
TForkedProcess.h
TimerUnit.h
TKey.h
TLabel.h
TScrollBox.h
TLinkStore.h
TLuaInterpreter.h
TMainConsole.h
Expand All @@ -302,32 +306,33 @@ set(mudlet_HDRS
TMxpBRTagHandler.h
TMxpClient.h
TMxpColorTagHandler.h
TMxpContext.h
TMxpCustomElementTagHandler.h
TMxpEntityTagHandler.h
TMxpFontTagHandler.h
TMxpLinkTagHandler.h
TMxpMusicTagHandler.h
TMxpSoundTagHandler.h
TMxpElementDefinitionHandler.h
TMxpElementRegistry.h
TMxpContext.h
TMxpEntityTagHandler.h
TMxpFontTagHandler.h
TMxpFormattingTagsHandler.h
TMxpLinkTagHandler.h
TMxpMudlet.h
TMxpMusicTagHandler.h
TMxpNodeBuilder.h
TMxpProcessor.h
TMxpSendTagHandler.h
MxpTag.h
TMxpSoundTagHandler.h
TMxpSupportTagHandler.cpp
TMxpTagHandler.h
TMxpTagParser.h
TMxpTagProcessor.h
TMxpSupportTagHandler.cpp
TMxpVarTagHandler.h
TMxpVersionTagHandler.h
TrailingWhitespaceMarker.h
Tree.h
TriggerUnit.h
TRoom.h
TRoomDB.h
TScript.h
TScrollBox.h
TSplitter.h
TSplitterHandle.h
TStringUtils.h
Expand All @@ -339,8 +344,8 @@ set(mudlet_HDRS
TTreeWidget.h
TTrigger.h
TVar.h
VarUnit.h
utils.h
VarUnit.h
widechar_width.h
XMLexport.h
XMLimport.h)
Expand Down
129 changes: 129 additions & 0 deletions src/GMCPAuthenticator.cpp
@@ -0,0 +1,129 @@
/***************************************************************************
* 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 {
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
}
54 changes: 54 additions & 0 deletions src/GMCPAuthenticator.h
@@ -0,0 +1,54 @@
#ifndef MUDLET_AUTHENTICATOR_H
#define MUDLET_AUTHENTICATOR_H

/***************************************************************************
* 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 "Host.h"
#include "utils.h"

#include "pre_guard.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QVariantMap>
#include "post_guard.h"


class GMCPAuthenticator
{
Q_DECLARE_TR_FUNCTIONS(GMCPAuthenticator)

public:

explicit GMCPAuthenticator(Host* pHost);
~GMCPAuthenticator() = default;

void saveSupportsSet(const QString& data);
void sendCredentials();
void handleAuthResult(const QString& data);
void handleAuthGMCP(const QString& packageMessage, const QString& data);

private:
Host* mpHost;
QStringList mSupportedAuthTypes;
};

#endif // MUDLET_AUTHENTICATOR_H

0 comments on commit 74b60cc

Please sign in to comment.