summaryrefslogtreecommitdiff
path: root/login.cpp
blob: ac16d59845bc665cb2d9a5f95197a1d9ceafd11a (plain) (blame)
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
#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);
}