diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-02-04 11:34:38 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-02-04 15:24:48 +0100 |
| commit | 23cc4be5bc02328fe73ccbd67493dd71ef2e2b8f (patch) | |
| tree | b9e1a7a416f004d96671c0abf597524946b9dda3 /login.cpp | |
| parent | 3b9973552ec613b37fb511f3a0ae24dacb1e4366 (diff) | |
| download | yachat6-23cc4be5bc02328fe73ccbd67493dd71ef2e2b8f.tar.gz | |
Add accounts and login pages
Diffstat (limited to 'login.cpp')
| -rw-r--r-- | login.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/login.cpp b/login.cpp new file mode 100644 index 0000000..ac16d59 --- /dev/null +++ b/login.cpp @@ -0,0 +1,124 @@ +#include "login.h" +#include <QString> +#include "QXmppStanza.h" + +QString Login::error_to_str(const QXmppStanza::Error::Condition c) +{ + switch (c) + { + case QXmppStanza::Error::BadRequest: + return tr("BadRequest"); + case QXmppStanza::Error::Conflict: + return tr("Conflict"); + case QXmppStanza::Error::FeatureNotImplemented: + return tr("FeatureNotImplemented"); + case QXmppStanza::Error::Forbidden: + return tr("Forbidden"); + case QXmppStanza::Error::Gone: + return tr("Gone"); + case QXmppStanza::Error::InternalServerError: + return tr("internal server error"); + case QXmppStanza::Error::ItemNotFound: + return tr("item not found"); + case QXmppStanza::Error::JidMalformed: + return tr("JID Malformed"); + case QXmppStanza::Error::NotAcceptable: + return tr("not acceptable"); + case QXmppStanza::Error::NotAllowed: + return tr("not allowed"); + case QXmppStanza::Error::NotAuthorized: + return tr("not authorized"); + case QXmppStanza::Error::RecipientUnavailable: + return tr("recipient unavailable"); + case QXmppStanza::Error::Redirect: + return tr("Redirect"); + case QXmppStanza::Error::RegistrationRequired: + return tr("RegistrationRequired"); + case QXmppStanza::Error::RemoteServerNotFound: + return tr("RemoteServerNotFound"); + case QXmppStanza::Error::RemoteServerTimeout: + return tr("RemoteServerTimeout"); + case QXmppStanza::Error::ResourceConstraint: + return tr("ResourceConstraint"); + case QXmppStanza::Error::ServiceUnavailable: + return tr("ServiceUnavailable"); + case QXmppStanza::Error::SubscriptionRequired: + return tr("SubscriptionRequired"); + case QXmppStanza::Error::UndefinedCondition: + return tr("UndefinedCondition"); + case QXmppStanza::Error::UnexpectedRequest: + return tr("UnexpectedRequest"); + case QXmppStanza::Error::PolicyViolation: + return tr("PolicyViolation"); + + default: + break; + } + + return tr("UnknownError"); +} + +Login::Login(QObject *parent) : + QObject(parent) +{ +} + +void Login::start(QString jid, QString pwd) +{ + QString domain; + + if (!jid_is_valid(jid, domain)) + emit error(tr("Invalid username")); + else if (pwd.isEmpty()) + emit error(tr("Invalid password")); + else + setup(jid, pwd, domain); +} + +bool Login::jid_is_valid(const QString &jid, QString &domain) +{ + if (jid.isEmpty() || jid.count('@') != 1) + return false; + else if ((domain = jid.mid(jid.indexOf('@') + 1)).isEmpty()) + return false; + + return true; +} + +void Login::setup(const QString &jid, const QString pwd, const QString &domain) +{ + QXmppConfiguration cfg; + + cfg.setStreamSecurityMode(QXmppConfiguration::TLSRequired); + cfg.setJid(jid); + cfg.setPassword(pwd); + cfg.setAutoReconnectionEnabled(false); + + const auto client = new Client(jid); + + connect(client, &Client::stateChanged, + [this, d = domain] (const Client::State state) + { + if (state == Client::ConnectingState) + { + emit error(tr("Connecting to ") + d + "..."); + } + }); + + connect(client, &Client::connected, this, + [this, client] + { + emit authSuccess(client); + emit close(); + }); + + connect(client, &Client::disconnected, this, + [this, client] + { + emit error(tr("Disconnected from server: ") + + error_to_str(client->xmppStreamError())); + delete client; + }); + + client->connectToServer(cfg); +} |
