1 /*
2 * Copyright (C) 2008-2021 The QXmpp developers
3 *
4 * Author:
5 * Manjeet Dahiya
6 *
7 * Source:
8 * https://github.com/qxmpp-project/qxmpp
9 *
10 * This file is a part of QXmpp library.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 */
23
24 #include "QXmppConfiguration.h"
25
26 #include "QXmppUtils.h"
27
28 #include <QNetworkProxy>
29 #include <QSslSocket>
30
31 class QXmppConfigurationPrivate : public QSharedData
32 {
33 public:
34 QXmppConfigurationPrivate();
35
36 QString host;
37 int port;
38 QString user;
39 QString password;
40 QString domain;
41 QString resource;
42
43 // Facebook
44 QString facebookAccessToken;
45 QString facebookAppId;
46
47 // Google
48 QString googleAccessToken;
49
50 // Windows Live
51 QString windowsLiveAccessToken;
52
53 // default is false
54 bool autoAcceptSubscriptions;
55 // default is true
56 bool sendIntialPresence;
57 // default is true
58 bool sendRosterRequest;
59 // interval in seconds, if zero won't ping
60 int keepAliveInterval;
61 // interval in seconds, if zero won't timeout
62 int keepAliveTimeout;
63 // will keep reconnecting if disconnected, default is true
64 bool autoReconnectionEnabled;
65 // which authentication systems to use (if any)
66 bool useSASLAuthentication;
67 bool useNonSASLAuthentication;
68 // default is false
69 bool ignoreSslErrors;
70
71 QXmppConfiguration::StreamSecurityMode streamSecurityMode;
72 QXmppConfiguration::NonSASLAuthMechanism nonSASLAuthMechanism;
73 QString saslAuthMechanism;
74
75 QNetworkProxy networkProxy;
76
77 QList<QSslCertificate> caCertificates;
78 };
79
QXmppConfigurationPrivate()80 QXmppConfigurationPrivate::QXmppConfigurationPrivate()
81 : port(5222), resource("QXmpp"), autoAcceptSubscriptions(false), sendIntialPresence(true), sendRosterRequest(true), keepAliveInterval(60), keepAliveTimeout(20), autoReconnectionEnabled(true), useSASLAuthentication(true), useNonSASLAuthentication(true), ignoreSslErrors(false), streamSecurityMode(QXmppConfiguration::TLSEnabled), nonSASLAuthMechanism(QXmppConfiguration::NonSASLDigest)
82 {
83 }
84
85 /// Creates a QXmppConfiguration object.
86
QXmppConfiguration()87 QXmppConfiguration::QXmppConfiguration()
88 : d(new QXmppConfigurationPrivate)
89 {
90 }
91
92 /// Creates a copy of \a other.
93
QXmppConfiguration(const QXmppConfiguration & other)94 QXmppConfiguration::QXmppConfiguration(const QXmppConfiguration& other)
95 : d(other.d)
96 {
97 }
98
99 /// Destructor, destroys the QXmppConfiguration object.
100 ///
101
~QXmppConfiguration()102 QXmppConfiguration::~QXmppConfiguration()
103 {
104 }
105
106 /// Assigns \a other to this QXmppConfiguration.
107
operator =(const QXmppConfiguration & other)108 QXmppConfiguration& QXmppConfiguration::operator=(const QXmppConfiguration& other)
109 {
110 d = other.d;
111 return *this;
112 }
113
114 /// Sets the host name.
115 ///
116 /// \param host host name of the XMPP server where connection has to be made
117 /// (e.g. "jabber.org" and "talk.google.com"). It can also be an IP address in
118 /// the form of a string (e.g. "192.168.1.25").
119 ///
120
setHost(const QString & host)121 void QXmppConfiguration::setHost(const QString& host)
122 {
123 d->host = host;
124 }
125
126 /// Sets the domain name.
127 ///
128 /// \param domain Domain name e.g. "gmail.com" and "jabber.org".
129 /// \note host name and domain name can be different for google
130 /// domain name is gmail.com and host name is talk.google.com
131 ///
132
setDomain(const QString & domain)133 void QXmppConfiguration::setDomain(const QString& domain)
134 {
135 d->domain = domain;
136 }
137
138 /// Sets the port number.
139 ///
140 /// \param port Port number at which the XMPP server is listening. The default
141 /// value is 5222.
142 ///
143
setPort(int port)144 void QXmppConfiguration::setPort(int port)
145 {
146 d->port = port;
147 }
148
149 /// Sets the username.
150 ///
151 /// \param user Username of the account at the specified XMPP server. It should
152 /// be the name without the domain name. E.g. "qxmpp.test1" and not
153 /// "qxmpp.test1@gmail.com"
154 ///
155
setUser(const QString & user)156 void QXmppConfiguration::setUser(const QString& user)
157 {
158 d->user = user;
159 }
160
161 /// Sets the password.
162 ///
163 /// \param password Password for the specified username
164 ///
165
setPassword(const QString & password)166 void QXmppConfiguration::setPassword(const QString& password)
167 {
168 d->password = password;
169 }
170
171 /// Sets the resource identifier.
172 ///
173 /// Multiple resources (e.g., devices or locations) may connect simultaneously
174 /// to a server on behalf of each authorized client, with each resource
175 /// differentiated by the resource identifier of an XMPP address
176 /// (e.g. node\@domain/home vs. node\@domain/work)
177 ///
178 /// The default value is "QXmpp".
179 ///
180 /// \param resource Resource identifier of the client in connection.
181
setResource(const QString & resource)182 void QXmppConfiguration::setResource(const QString& resource)
183 {
184 d->resource = resource;
185 }
186
187 /// Sets the JID. If a full JID (i.e. one with a resource) is given, calling
188 /// this method will update the username, domain and resource. Otherwise, only
189 /// the username and the domain will be updated.
190 ///
191 /// \param jid
192
setJid(const QString & jid)193 void QXmppConfiguration::setJid(const QString& jid)
194 {
195 d->user = QXmppUtils::jidToUser(jid);
196 d->domain = QXmppUtils::jidToDomain(jid);
197 const QString resource = QXmppUtils::jidToResource(jid);
198 if (!resource.isEmpty())
199 d->resource = resource;
200 }
201
202 /// Returns the host name.
203 ///
204 /// \return host name
205 ///
206
host() const207 QString QXmppConfiguration::host() const
208 {
209 return d->host;
210 }
211
212 /// Returns the domain name.
213 ///
214 /// \return domain name
215 ///
216
domain() const217 QString QXmppConfiguration::domain() const
218 {
219 return d->domain;
220 }
221
222 /// Returns the port number.
223 ///
224 /// \return port number
225 ///
226
port() const227 int QXmppConfiguration::port() const
228 {
229 return d->port;
230 }
231
232 /// Returns the username.
233 ///
234 /// \return username
235 ///
236
user() const237 QString QXmppConfiguration::user() const
238 {
239 return d->user;
240 }
241
242 /// Returns the password.
243 ///
244 /// \return password
245 ///
246
password() const247 QString QXmppConfiguration::password() const
248 {
249 return d->password;
250 }
251
252 /// Returns the resource identifier.
253 ///
254 /// \return resource identifier
255 ///
256
resource() const257 QString QXmppConfiguration::resource() const
258 {
259 return d->resource;
260 }
261
262 /// Returns the jabber id (jid).
263 ///
264 /// \return jabber id (jid)
265 /// (e.g. "qxmpp.test1@gmail.com/resource" or qxmpptest@jabber.org/QXmpp156)
266 ///
267
jid() const268 QString QXmppConfiguration::jid() const
269 {
270 if (d->user.isEmpty())
271 return d->domain;
272 else
273 return jidBare() + "/" + d->resource;
274 }
275
276 /// Returns the bare jabber id (jid), without the resource identifier.
277 ///
278 /// \return bare jabber id (jid)
279 /// (e.g. "qxmpp.test1@gmail.com" or qxmpptest@jabber.org)
280 ///
281
jidBare() const282 QString QXmppConfiguration::jidBare() const
283 {
284 if (d->user.isEmpty())
285 return d->domain;
286 else
287 return d->user + "@" + d->domain;
288 }
289
290 /// Returns the access token used for X-FACEBOOK-PLATFORM authentication.
291
facebookAccessToken() const292 QString QXmppConfiguration::facebookAccessToken() const
293 {
294 return d->facebookAccessToken;
295 }
296
297 /// Sets the access token used for X-FACEBOOK-PLATFORM authentication.
298 ///
299 /// This token is returned by Facebook at the end of the OAuth authentication
300 /// process.
301 ///
302 /// \param accessToken
303
setFacebookAccessToken(const QString & accessToken)304 void QXmppConfiguration::setFacebookAccessToken(const QString& accessToken)
305 {
306 d->facebookAccessToken = accessToken;
307 }
308
309 /// Returns the application ID used for X-FACEBOOK-PLATFORM authentication.
310
facebookAppId() const311 QString QXmppConfiguration::facebookAppId() const
312 {
313 return d->facebookAppId;
314 }
315
316 /// Sets the application ID used for X-FACEBOOK-PLATFORM authentication.
317 ///
318 /// \param appId
319
setFacebookAppId(const QString & appId)320 void QXmppConfiguration::setFacebookAppId(const QString& appId)
321 {
322 d->facebookAppId = appId;
323 }
324
325 /// Returns the access token used for X-OAUTH2 authentication.
326
googleAccessToken() const327 QString QXmppConfiguration::googleAccessToken() const
328 {
329 return d->googleAccessToken;
330 }
331
332 /// Sets the access token used for X-OAUTH2 authentication.
333 ///
334 /// This token is returned by Google at the end of the OAuth authentication
335 /// process.
336 ///
337 /// \param accessToken
338
setGoogleAccessToken(const QString & accessToken)339 void QXmppConfiguration::setGoogleAccessToken(const QString& accessToken)
340 {
341 d->googleAccessToken = accessToken;
342 }
343
344 /// Returns the access token used for X-MESSENGER-OAUTH2 authentication.
345
windowsLiveAccessToken() const346 QString QXmppConfiguration::windowsLiveAccessToken() const
347 {
348 return d->windowsLiveAccessToken;
349 }
350
351 /// Sets the access token used for X-MESSENGER-OAUTH2 authentication.
352 ///
353 /// This token is returned by Windows Live at the end of the OAuth authentication
354 /// process.
355 ///
356 /// \param accessToken
357
setWindowsLiveAccessToken(const QString & accessToken)358 void QXmppConfiguration::setWindowsLiveAccessToken(const QString& accessToken)
359 {
360 d->windowsLiveAccessToken = accessToken;
361 }
362
363 /// Returns the auto-accept-subscriptions-request configuration.
364 ///
365 /// \return boolean value
366 /// true means that auto-accept-subscriptions-request is enabled else disabled for false
367 ///
368
autoAcceptSubscriptions() const369 bool QXmppConfiguration::autoAcceptSubscriptions() const
370 {
371 return d->autoAcceptSubscriptions;
372 }
373
374 /// Sets the auto-accept-subscriptions-request configuration.
375 ///
376 /// \param value boolean value
377 /// true means that auto-accept-subscriptions-request is enabled else disabled for false
378 ///
379
setAutoAcceptSubscriptions(bool value)380 void QXmppConfiguration::setAutoAcceptSubscriptions(bool value)
381 {
382 d->autoAcceptSubscriptions = value;
383 }
384
385 /// Returns the auto-reconnect-on-disconnection-on-error configuration.
386 ///
387 /// \return boolean value
388 /// true means that auto-reconnect is enabled else disabled for false
389 ///
390
autoReconnectionEnabled() const391 bool QXmppConfiguration::autoReconnectionEnabled() const
392 {
393 return d->autoReconnectionEnabled;
394 }
395
396 /// Sets the auto-reconnect-on-disconnection-on-error configuration.
397 ///
398 /// \param value boolean value
399 /// true means that auto-reconnect is enabled else disabled for false
400 ///
401
setAutoReconnectionEnabled(bool value)402 void QXmppConfiguration::setAutoReconnectionEnabled(bool value)
403 {
404 d->autoReconnectionEnabled = value;
405 }
406
407 /// Returns whether SSL errors (such as certificate validation errors)
408 /// are to be ignored when connecting to the XMPP server.
409
ignoreSslErrors() const410 bool QXmppConfiguration::ignoreSslErrors() const
411 {
412 return d->ignoreSslErrors;
413 }
414
415 /// Specifies whether SSL errors (such as certificate validation errors)
416 /// are to be ignored when connecting to an XMPP server.
417
setIgnoreSslErrors(bool value)418 void QXmppConfiguration::setIgnoreSslErrors(bool value)
419 {
420 d->ignoreSslErrors = value;
421 }
422
423 /// Returns whether to make use of SASL authentication.
424
useSASLAuthentication() const425 bool QXmppConfiguration::useSASLAuthentication() const
426 {
427 return d->useSASLAuthentication;
428 }
429
430 /// Sets whether to make use of SASL authentication.
431
setUseSASLAuthentication(bool useSASL)432 void QXmppConfiguration::setUseSASLAuthentication(bool useSASL)
433 {
434 d->useSASLAuthentication = useSASL;
435 }
436
437 /// Returns whether to make use of non-SASL authentication.
438
useNonSASLAuthentication() const439 bool QXmppConfiguration::useNonSASLAuthentication() const
440 {
441 return d->useNonSASLAuthentication;
442 }
443
444 /// Sets whether to make use of non-SASL authentication.
445
setUseNonSASLAuthentication(bool useNonSASL)446 void QXmppConfiguration::setUseNonSASLAuthentication(bool useNonSASL)
447 {
448 d->useNonSASLAuthentication = useNonSASL;
449 }
450
451 /// Returns the specified security mode for the stream. The default value is
452 /// QXmppConfiguration::TLSEnabled.
453 /// \return StreamSecurityMode
454
streamSecurityMode() const455 QXmppConfiguration::StreamSecurityMode QXmppConfiguration::streamSecurityMode() const
456 {
457 return d->streamSecurityMode;
458 }
459
460 /// Specifies the specified security mode for the stream. The default value is
461 /// QXmppConfiguration::TLSEnabled.
462 /// \param mode StreamSecurityMode
463
setStreamSecurityMode(QXmppConfiguration::StreamSecurityMode mode)464 void QXmppConfiguration::setStreamSecurityMode(
465 QXmppConfiguration::StreamSecurityMode mode)
466 {
467 d->streamSecurityMode = mode;
468 }
469
470 /// Returns the Non-SASL authentication mechanism configuration.
471 ///
472 /// \return QXmppConfiguration::NonSASLAuthMechanism
473 ///
474
nonSASLAuthMechanism() const475 QXmppConfiguration::NonSASLAuthMechanism QXmppConfiguration::nonSASLAuthMechanism() const
476 {
477 return d->nonSASLAuthMechanism;
478 }
479
480 /// Hints the library the Non-SASL authentication mechanism to be used for authentication.
481 ///
482 /// \param mech QXmppConfiguration::NonSASLAuthMechanism
483 ///
484
setNonSASLAuthMechanism(QXmppConfiguration::NonSASLAuthMechanism mech)485 void QXmppConfiguration::setNonSASLAuthMechanism(
486 QXmppConfiguration::NonSASLAuthMechanism mech)
487 {
488 d->nonSASLAuthMechanism = mech;
489 }
490
491 /// Returns the preferred SASL authentication mechanism.
492
saslAuthMechanism() const493 QString QXmppConfiguration::saslAuthMechanism() const
494 {
495 return d->saslAuthMechanism;
496 }
497
498 /// Sets the preferred SASL authentication \a mechanism.
499 ///
500 /// Valid values: "SCRAM-SHA-256", "SCRAM-SHA-1", "DIGEST-MD5", "PLAIN", "ANONYMOUS",
501 // "X-FACEBOOK-PLATFORM", "X-MESSENGER-OAUTH2", "X-OAUTH2"
502
setSaslAuthMechanism(const QString & mechanism)503 void QXmppConfiguration::setSaslAuthMechanism(const QString& mechanism)
504 {
505 d->saslAuthMechanism = mechanism;
506 }
507
508 /// Specifies the network proxy used for the connection made by QXmppClient.
509 /// The default value is QNetworkProxy::DefaultProxy that is the proxy is
510 /// determined based on the application proxy set using
511 /// QNetworkProxy::setApplicationProxy().
512 /// \param proxy QNetworkProxy
513
setNetworkProxy(const QNetworkProxy & proxy)514 void QXmppConfiguration::setNetworkProxy(const QNetworkProxy& proxy)
515 {
516 d->networkProxy = proxy;
517 }
518
519 /// Returns the specified network proxy.
520 /// The default value is QNetworkProxy::DefaultProxy that is the proxy is
521 /// determined based on the application proxy set using
522 /// QNetworkProxy::setApplicationProxy().
523 /// \return QNetworkProxy
524
networkProxy() const525 QNetworkProxy QXmppConfiguration::networkProxy() const
526 {
527 return d->networkProxy;
528 }
529
530 /// Specifies the interval in seconds at which keep alive (ping) packets
531 /// will be sent to the server.
532 ///
533 /// If set to zero, no keep alive packets will be sent.
534 ///
535 /// The default value is 60 seconds.
536
setKeepAliveInterval(int secs)537 void QXmppConfiguration::setKeepAliveInterval(int secs)
538 {
539 d->keepAliveInterval = secs;
540 }
541
542 /// Returns the keep alive interval in seconds.
543 ///
544 /// The default value is 60 seconds.
545
keepAliveInterval() const546 int QXmppConfiguration::keepAliveInterval() const
547 {
548 return d->keepAliveInterval;
549 }
550
551 /// Specifies the maximum time in seconds to wait for a keep alive response
552 /// from the server before considering we are disconnected.
553 ///
554 /// If set to zero or a value larger than the keep alive interval,
555 /// no timeout will occur.
556 ///
557 /// The default value is 20 seconds.
558
setKeepAliveTimeout(int secs)559 void QXmppConfiguration::setKeepAliveTimeout(int secs)
560 {
561 d->keepAliveTimeout = secs;
562 }
563
564 /// Returns the keep alive timeout in seconds.
565 ///
566 /// The default value is 20 seconds.
567
keepAliveTimeout() const568 int QXmppConfiguration::keepAliveTimeout() const
569 {
570 return d->keepAliveTimeout;
571 }
572
573 /// Specifies a list of trusted CA certificates.
574
setCaCertificates(const QList<QSslCertificate> & caCertificates)575 void QXmppConfiguration::setCaCertificates(const QList<QSslCertificate>& caCertificates)
576 {
577 d->caCertificates = caCertificates;
578 }
579
580 /// Returns the a list of trusted CA certificates.
581
caCertificates() const582 QList<QSslCertificate> QXmppConfiguration::caCertificates() const
583 {
584 return d->caCertificates;
585 }
586