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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// SPDX-FileCopyrightText: 2020 Melvin Keskin <melvo@olomono.de>
// SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppRegistrationManager.h"
#include "QXmppClient.h"
#include "QXmppConstants_p.h"
#include "QXmppDiscoveryManager.h"
#include "QXmppRegisterIq.h"
#include "QXmppStreamFeatures.h"
#include "QXmppUtils.h"
#include <QDomElement>
class QXmppRegistrationManagerPrivate
{
public:
QXmppRegistrationManagerPrivate();
// whether to block login and request the registration form on connect
bool registerOnConnectEnabled;
// whether the server supports registration (after login)
bool supportedByServer;
// caching
QString changePasswordIqId;
QString newPassword;
QString deleteAccountIqId;
QString registrationIqId;
QXmppRegisterIq registrationFormToSend;
};
QXmppRegistrationManagerPrivate::QXmppRegistrationManagerPrivate()
: registerOnConnectEnabled(false),
supportedByServer(false)
{
}
///
/// Default constructor.
///
QXmppRegistrationManager::QXmppRegistrationManager()
: d(std::make_unique<QXmppRegistrationManagerPrivate>())
{
}
QXmppRegistrationManager::~QXmppRegistrationManager() = default;
///
/// This adds the \c jabber:iq:register namespace to the features.
///
QStringList QXmppRegistrationManager::discoveryFeatures() const
{
return QStringList {
ns_register
};
}
///
/// Changes the password of the user's account.
///
/// \note Be sure to only call this when any previous requests have finished.
///
/// \param newPassword The new password to be set. This must not be empty.
///
void QXmppRegistrationManager::changePassword(const QString &newPassword)
{
auto iq = QXmppRegisterIq::createChangePasswordRequest(client()->configuration().user(), newPassword);
d->changePasswordIqId = iq.id();
d->newPassword = newPassword;
client()->sendPacket(iq);
}
///
/// Cancels an existing registration on the server.
///
/// \sa accountDeleted()
/// \sa accountDeletionFailed()
///
void QXmppRegistrationManager::deleteAccount()
{
auto iq = QXmppRegisterIq::createUnregistrationRequest();
d->deleteAccountIqId = iq.id();
client()->sendPacket(iq);
}
bool QXmppRegistrationManager::supportedByServer() const
{
return d->supportedByServer;
}
///
/// Requests the registration form for registering.
///
/// \param service The service which the registration form should be requested
/// from. If left empty, this will default to the local server.
///
void QXmppRegistrationManager::requestRegistrationForm(const QString &service)
{
QXmppRegisterIq iq;
iq.setType(QXmppIq::Get);
iq.setTo(service);
client()->sendPacket(iq);
}
///
/// Sets a registration form to be sent on the next connect with the server.
/// \param iq The completed registration form.
///
void QXmppRegistrationManager::setRegistrationFormToSend(const QXmppRegisterIq &iq)
{
d->registrationFormToSend = iq;
}
///
/// Sets a registration form to be sent on the next connect with the server.
/// \param dataForm The completed data form for registration.
///
void QXmppRegistrationManager::setRegistrationFormToSend(const QXmppDataForm &dataForm)
{
d->registrationFormToSend = QXmppRegisterIq();
d->registrationFormToSend.setForm(dataForm);
}
///
/// Sends a completed registration form that was previously set using
/// setRegistrationFormToSend().
///
/// You usually only need to set the form and the manager will automatically
/// send it when connected. More details can be found in the documentation of
/// the QXmppRegistrationManager.
///
void QXmppRegistrationManager::sendCachedRegistrationForm()
{
if (auto form = d->registrationFormToSend.form(); !form.isNull()) {
form.setType(QXmppDataForm::Submit);
d->registrationFormToSend.setForm(form);
}
d->registrationFormToSend.setType(QXmppIq::Set);
client()->sendPacket(d->registrationFormToSend);
d->registrationIqId = d->registrationFormToSend.id();
// clear cache
d->registrationFormToSend = QXmppRegisterIq();
}
///
/// Returns whether to only request the registration form and not to connect
/// with username/password.
///
bool QXmppRegistrationManager::registerOnConnectEnabled() const
{
return d->registerOnConnectEnabled;
}
///
/// Sets whether to only request the registration form and not to connect with
/// username/password.
///
/// \param enabled true to register, false to connect normally.
///
void QXmppRegistrationManager::setRegisterOnConnectEnabled(bool enabled)
{
d->registerOnConnectEnabled = enabled;
}
/// \cond
bool QXmppRegistrationManager::handleStanza(const QDomElement &stanza)
{
if (d->registerOnConnectEnabled && QXmppStreamFeatures::isStreamFeatures(stanza)) {
QXmppStreamFeatures features;
features.parse(stanza);
if (features.registerMode() == QXmppStreamFeatures::Disabled) {
warning(QStringLiteral("Could not request the registration form, because the server does not advertise the register stream feature."));
client()->disconnectFromServer();
Q_EMIT registrationFailed({ QXmppStanza::Error::Cancel,
QXmppStanza::Error::FeatureNotImplemented,
QStringLiteral("The server does not advertise the register stream feature.") });
return true;
}
if (!d->registrationFormToSend.form().isNull() || !d->registrationFormToSend.username().isNull()) {
info(QStringLiteral("Sending completed form."));
sendCachedRegistrationForm();
return true;
}
info(QStringLiteral("Requesting registration form from server."));
requestRegistrationForm();
return true;
}
if (stanza.tagName() == "iq") {
const QString &id = stanza.attribute(QStringLiteral("id"));
if (!id.isEmpty() && id == d->registrationIqId) {
QXmppIq iq;
iq.parse(stanza);
switch (iq.type()) {
case QXmppIq::Result:
info(QStringLiteral("Successfully registered with the service."));
Q_EMIT registrationSucceeded();
break;
case QXmppIq::Error:
warning(QStringLiteral("Registering with the service failed: ").append(iq.error().text()));
Q_EMIT registrationFailed(iq.error());
break;
default:
break; // should never occur
}
d->registrationIqId.clear();
return true;
} else if (!id.isEmpty() && id == d->changePasswordIqId) {
QXmppIq iq;
iq.parse(stanza);
switch (iq.type()) {
case QXmppIq::Result:
info(QStringLiteral("Changed password successfully."));
client()->configuration().setPassword(d->newPassword);
Q_EMIT passwordChanged(d->newPassword);
break;
case QXmppIq::Error:
warning(QStringLiteral("Failed to change password: ").append(iq.error().text()));
Q_EMIT passwordChangeFailed(iq.error());
break;
default:
break; // should never occur
}
d->changePasswordIqId.clear();
d->newPassword.clear();
return true;
} else if (!id.isEmpty() && id == d->deleteAccountIqId) {
QXmppIq iq;
iq.parse(stanza);
switch (iq.type()) {
case QXmppIq::Result:
info(QStringLiteral("Account deleted successfully."));
Q_EMIT accountDeleted();
client()->disconnectFromServer();
break;
case QXmppIq::Error:
warning(QStringLiteral("Failed to delete account: ").append(iq.error().text()));
Q_EMIT accountDeletionFailed(iq.error());
break;
default:
break; // should never occur
}
d->deleteAccountIqId.clear();
return true;
} else if (QXmppRegisterIq::isRegisterIq(stanza)) {
QXmppRegisterIq iq;
iq.parse(stanza);
Q_EMIT registrationFormReceived(iq);
}
}
return false;
}
/// \endcond
void QXmppRegistrationManager::setClient(QXmppClient *client)
{
QXmppClientExtension::setClient(client);
// get service discovery manager
auto *disco = client->findExtension<QXmppDiscoveryManager>();
if (disco) {
connect(disco, &QXmppDiscoveryManager::infoReceived, this, &QXmppRegistrationManager::handleDiscoInfo);
}
connect(client, &QXmppClient::disconnected, this, [this]() {
setSupportedByServer(false);
});
}
void QXmppRegistrationManager::handleDiscoInfo(const QXmppDiscoveryIq &iq)
{
// check features of own server
if (iq.from().isEmpty() || iq.from() == client()->configuration().domain()) {
if (iq.features().contains(ns_register)) {
setSupportedByServer(true);
}
}
}
void QXmppRegistrationManager::setSupportedByServer(bool registrationSupported)
{
if (d->supportedByServer != registrationSupported) {
d->supportedByServer = registrationSupported;
Q_EMIT supportedByServerChanged();
}
}
|