QXmppConfiguration.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Manjeet Dahiya
  6. *
  7. * Source:
  8. * http://code.google.com/p/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. #include <QNetworkProxy>
  24. #include <QSslSocket>
  25. #include "QXmppConfiguration.h"
  26. #include "QXmppUtils.h"
  27. class QXmppConfigurationPrivate : public QSharedData
  28. {
  29. public:
  30. QXmppConfigurationPrivate();
  31. QString host;
  32. int port;
  33. QString user;
  34. QString password;
  35. QString domain;
  36. QString resource;
  37. // Facebook
  38. QString facebookAccessToken;
  39. QString facebookAppId;
  40. // Google
  41. QString googleAccessToken;
  42. // Windows Live
  43. QString windowsLiveAccessToken;
  44. // default is false
  45. bool autoAcceptSubscriptions;
  46. // default is true
  47. bool sendIntialPresence;
  48. // default is true
  49. bool sendRosterRequest;
  50. // interval in seconds, if zero won't ping
  51. int keepAliveInterval;
  52. // interval in seconds, if zero won't timeout
  53. int keepAliveTimeout;
  54. // will keep reconnecting if disconnected, default is true
  55. bool autoReconnectionEnabled;
  56. // which authentication systems to use (if any)
  57. bool useSASLAuthentication;
  58. bool useNonSASLAuthentication;
  59. // default is true
  60. bool ignoreSslErrors;
  61. QXmppConfiguration::StreamSecurityMode streamSecurityMode;
  62. QXmppConfiguration::NonSASLAuthMechanism nonSASLAuthMechanism;
  63. QString saslAuthMechanism;
  64. QNetworkProxy networkProxy;
  65. QList<QSslCertificate> caCertificates;
  66. };
  67. QXmppConfigurationPrivate::QXmppConfigurationPrivate()
  68. : port(5222)
  69. , resource("QXmpp")
  70. , autoAcceptSubscriptions(false)
  71. , sendIntialPresence(true)
  72. , sendRosterRequest(true)
  73. , keepAliveInterval(60)
  74. , keepAliveTimeout(20)
  75. , autoReconnectionEnabled(true)
  76. , useSASLAuthentication(true)
  77. , useNonSASLAuthentication(true)
  78. , ignoreSslErrors(true)
  79. , streamSecurityMode(QXmppConfiguration::TLSEnabled)
  80. , nonSASLAuthMechanism(QXmppConfiguration::NonSASLDigest)
  81. , saslAuthMechanism("DIGEST-MD5")
  82. {
  83. }
  84. /// Creates a QXmppConfiguration object.
  85. QXmppConfiguration::QXmppConfiguration()
  86. : d(new QXmppConfigurationPrivate)
  87. {
  88. }
  89. /// Creates a copy of \a other.
  90. QXmppConfiguration::QXmppConfiguration(const QXmppConfiguration &other)
  91. : d(other.d)
  92. {
  93. }
  94. /// Destructor, destroys the QXmppConfiguration object.
  95. ///
  96. QXmppConfiguration::~QXmppConfiguration()
  97. {
  98. }
  99. /// Assigns \a other to this QXmppConfiguration.
  100. QXmppConfiguration& QXmppConfiguration::operator=(const QXmppConfiguration &other)
  101. {
  102. d = other.d;
  103. return *this;
  104. }
  105. /// Sets the host name.
  106. ///
  107. /// \param host host name of the XMPP server where connection has to be made
  108. /// (e.g. "jabber.org" and "talk.google.com"). It can also be an IP address in
  109. /// the form of a string (e.g. "192.168.1.25").
  110. ///
  111. void QXmppConfiguration::setHost(const QString& host)
  112. {
  113. d->host = host;
  114. }
  115. /// Sets the domain name.
  116. ///
  117. /// \param domain Domain name e.g. "gmail.com" and "jabber.org".
  118. /// \note host name and domain name can be different for google
  119. /// domain name is gmail.com and host name is talk.google.com
  120. ///
  121. void QXmppConfiguration::setDomain(const QString& domain)
  122. {
  123. d->domain = domain;
  124. }
  125. /// Sets the port number.
  126. ///
  127. /// \param port Port number at which the XMPP server is listening. The default
  128. /// value is 5222.
  129. ///
  130. void QXmppConfiguration::setPort(int port)
  131. {
  132. d->port = port;
  133. }
  134. /// Sets the username.
  135. ///
  136. /// \param user Username of the account at the specified XMPP server. It should
  137. /// be the name without the domain name. E.g. "qxmpp.test1" and not
  138. /// "qxmpp.test1@gmail.com"
  139. ///
  140. void QXmppConfiguration::setUser(const QString& user)
  141. {
  142. d->user = user;
  143. }
  144. /// Sets the password.
  145. ///
  146. /// \param password Password for the specified username
  147. ///
  148. void QXmppConfiguration::setPassword(const QString& password)
  149. {
  150. d->password = password;
  151. }
  152. /// Sets the resource identifier.
  153. ///
  154. /// Multiple resources (e.g., devices or locations) may connect simultaneously
  155. /// to a server on behalf of each authorized client, with each resource
  156. /// differentiated by the resource identifier of an XMPP address
  157. /// (e.g. node\@domain/home vs. node\@domain/work)
  158. ///
  159. /// The default value is "QXmpp".
  160. ///
  161. /// \param resource Resource identifier of the client in connection.
  162. void QXmppConfiguration::setResource(const QString& resource)
  163. {
  164. d->resource = resource;
  165. }
  166. /// Sets the JID. If a full JID (i.e. one with a resource) is given, calling
  167. /// this method will update the username, domain and resource. Otherwise, only
  168. /// the username and the domain will be updated.
  169. ///
  170. /// \param jid
  171. void QXmppConfiguration::setJid(const QString& jid)
  172. {
  173. d->user = QXmppUtils::jidToUser(jid);
  174. d->domain = QXmppUtils::jidToDomain(jid);
  175. const QString resource = QXmppUtils::jidToResource(jid);
  176. if (!resource.isEmpty())
  177. d->resource = resource;
  178. }
  179. /// Returns the host name.
  180. ///
  181. /// \return host name
  182. ///
  183. QString QXmppConfiguration::host() const
  184. {
  185. return d->host;
  186. }
  187. /// Returns the domain name.
  188. ///
  189. /// \return domain name
  190. ///
  191. QString QXmppConfiguration::domain() const
  192. {
  193. return d->domain;
  194. }
  195. /// Returns the port number.
  196. ///
  197. /// \return port number
  198. ///
  199. int QXmppConfiguration::port() const
  200. {
  201. return d->port;
  202. }
  203. /// Returns the username.
  204. ///
  205. /// \return username
  206. ///
  207. QString QXmppConfiguration::user() const
  208. {
  209. return d->user;
  210. }
  211. /// Returns the password.
  212. ///
  213. /// \return password
  214. ///
  215. QString QXmppConfiguration::password() const
  216. {
  217. return d->password;
  218. }
  219. /// Returns the resource identifier.
  220. ///
  221. /// \return resource identifier
  222. ///
  223. QString QXmppConfiguration::resource() const
  224. {
  225. return d->resource;
  226. }
  227. /// Returns the jabber id (jid).
  228. ///
  229. /// \return jabber id (jid)
  230. /// (e.g. "qxmpp.test1@gmail.com/resource" or qxmpptest@jabber.org/QXmpp156)
  231. ///
  232. QString QXmppConfiguration::jid() const
  233. {
  234. if (d->user.isEmpty())
  235. return d->domain;
  236. else
  237. return jidBare() + "/" + d->resource;
  238. }
  239. /// Returns the bare jabber id (jid), without the resource identifier.
  240. ///
  241. /// \return bare jabber id (jid)
  242. /// (e.g. "qxmpp.test1@gmail.com" or qxmpptest@jabber.org)
  243. ///
  244. QString QXmppConfiguration::jidBare() const
  245. {
  246. if (d->user.isEmpty())
  247. return d->domain;
  248. else
  249. return d->user+"@"+d->domain;
  250. }
  251. /// Returns the access token used for X-FACEBOOK-PLATFORM authentication.
  252. QString QXmppConfiguration::facebookAccessToken() const
  253. {
  254. return d->facebookAccessToken;
  255. }
  256. /// Sets the access token used for X-FACEBOOK-PLATFORM authentication.
  257. ///
  258. /// This token is returned by Facebook at the end of the OAuth authentication
  259. /// process.
  260. ///
  261. /// \param accessToken
  262. void QXmppConfiguration::setFacebookAccessToken(const QString& accessToken)
  263. {
  264. d->facebookAccessToken = accessToken;
  265. }
  266. /// Returns the application ID used for X-FACEBOOK-PLATFORM authentication.
  267. QString QXmppConfiguration::facebookAppId() const
  268. {
  269. return d->facebookAppId;
  270. }
  271. /// Sets the application ID used for X-FACEBOOK-PLATFORM authentication.
  272. ///
  273. /// \param appId
  274. void QXmppConfiguration::setFacebookAppId(const QString& appId)
  275. {
  276. d->facebookAppId = appId;
  277. }
  278. /// Returns the access token used for X-OAUTH2 authentication.
  279. QString QXmppConfiguration::googleAccessToken() const
  280. {
  281. return d->googleAccessToken;
  282. }
  283. /// Sets the access token used for X-OAUTH2 authentication.
  284. ///
  285. /// This token is returned by Google at the end of the OAuth authentication
  286. /// process.
  287. ///
  288. /// \param accessToken
  289. void QXmppConfiguration::setGoogleAccessToken(const QString& accessToken)
  290. {
  291. d->googleAccessToken = accessToken;
  292. }
  293. /// Returns the access token used for X-MESSENGER-OAUTH2 authentication.
  294. QString QXmppConfiguration::windowsLiveAccessToken() const
  295. {
  296. return d->windowsLiveAccessToken;
  297. }
  298. /// Sets the access token used for X-MESSENGER-OAUTH2 authentication.
  299. ///
  300. /// This token is returned by Windows Live at the end of the OAuth authentication
  301. /// process.
  302. ///
  303. /// \param accessToken
  304. void QXmppConfiguration::setWindowsLiveAccessToken(const QString& accessToken)
  305. {
  306. d->windowsLiveAccessToken = accessToken;
  307. }
  308. /// Returns the auto-accept-subscriptions-request configuration.
  309. ///
  310. /// \return boolean value
  311. /// true means that auto-accept-subscriptions-request is enabled else disabled for false
  312. ///
  313. bool QXmppConfiguration::autoAcceptSubscriptions() const
  314. {
  315. return d->autoAcceptSubscriptions;
  316. }
  317. /// Sets the auto-accept-subscriptions-request configuration.
  318. ///
  319. /// \param value boolean value
  320. /// true means that auto-accept-subscriptions-request is enabled else disabled for false
  321. ///
  322. void QXmppConfiguration::setAutoAcceptSubscriptions(bool value)
  323. {
  324. d->autoAcceptSubscriptions = value;
  325. }
  326. /// Returns the auto-reconnect-on-disconnection-on-error configuration.
  327. ///
  328. /// \return boolean value
  329. /// true means that auto-reconnect is enabled else disabled for false
  330. ///
  331. bool QXmppConfiguration::autoReconnectionEnabled() const
  332. {
  333. return d->autoReconnectionEnabled;
  334. }
  335. /// Sets the auto-reconnect-on-disconnection-on-error configuration.
  336. ///
  337. /// \param value boolean value
  338. /// true means that auto-reconnect is enabled else disabled for false
  339. ///
  340. void QXmppConfiguration::setAutoReconnectionEnabled(bool value)
  341. {
  342. d->autoReconnectionEnabled = value;
  343. }
  344. /// Returns whether SSL errors (such as certificate validation errors)
  345. /// are to be ignored when connecting to the XMPP server.
  346. bool QXmppConfiguration::ignoreSslErrors() const
  347. {
  348. return d->ignoreSslErrors;
  349. }
  350. /// Specifies whether SSL errors (such as certificate validation errors)
  351. /// are to be ignored when connecting to an XMPP server.
  352. void QXmppConfiguration::setIgnoreSslErrors(bool value)
  353. {
  354. d->ignoreSslErrors = value;
  355. }
  356. /// Returns whether to make use of SASL authentication.
  357. bool QXmppConfiguration::useSASLAuthentication() const
  358. {
  359. return d->useSASLAuthentication;
  360. }
  361. /// Sets whether to make use of SASL authentication.
  362. void QXmppConfiguration::setUseSASLAuthentication(bool useSASL)
  363. {
  364. d->useSASLAuthentication = useSASL;
  365. }
  366. /// Returns whether to make use of non-SASL authentication.
  367. bool QXmppConfiguration::useNonSASLAuthentication() const
  368. {
  369. return d->useNonSASLAuthentication;
  370. }
  371. /// Sets whether to make use of non-SASL authentication.
  372. void QXmppConfiguration::setUseNonSASLAuthentication(bool useNonSASL)
  373. {
  374. d->useNonSASLAuthentication = useNonSASL;
  375. }
  376. /// Returns the specified security mode for the stream. The default value is
  377. /// QXmppConfiguration::TLSEnabled.
  378. /// \return StreamSecurityMode
  379. QXmppConfiguration::StreamSecurityMode QXmppConfiguration::streamSecurityMode() const
  380. {
  381. return d->streamSecurityMode;
  382. }
  383. /// Specifies the specified security mode for the stream. The default value is
  384. /// QXmppConfiguration::TLSEnabled.
  385. /// \param mode StreamSecurityMode
  386. void QXmppConfiguration::setStreamSecurityMode(
  387. QXmppConfiguration::StreamSecurityMode mode)
  388. {
  389. d->streamSecurityMode = mode;
  390. }
  391. /// Returns the Non-SASL authentication mechanism configuration.
  392. ///
  393. /// \return QXmppConfiguration::NonSASLAuthMechanism
  394. ///
  395. QXmppConfiguration::NonSASLAuthMechanism QXmppConfiguration::nonSASLAuthMechanism() const
  396. {
  397. return d->nonSASLAuthMechanism;
  398. }
  399. /// Hints the library the Non-SASL authentication mechanism to be used for authentication.
  400. ///
  401. /// \param mech QXmppConfiguration::NonSASLAuthMechanism
  402. ///
  403. void QXmppConfiguration::setNonSASLAuthMechanism(
  404. QXmppConfiguration::NonSASLAuthMechanism mech)
  405. {
  406. d->nonSASLAuthMechanism = mech;
  407. }
  408. /// Returns the preferred SASL authentication mechanism.
  409. ///
  410. /// Default value: "DIGEST-MD5"
  411. QString QXmppConfiguration::saslAuthMechanism() const
  412. {
  413. return d->saslAuthMechanism;
  414. }
  415. /// Sets the preferred SASL authentication \a mechanism.
  416. ///
  417. /// Valid values: "PLAIN", "DIGEST-MD5", "ANONYMOUS", "X-FACEBOOK-PLATFORM"
  418. void QXmppConfiguration::setSaslAuthMechanism(const QString &mechanism)
  419. {
  420. d->saslAuthMechanism = mechanism;
  421. }
  422. /// Specifies the network proxy used for the connection made by QXmppClient.
  423. /// The default value is QNetworkProxy::DefaultProxy that is the proxy is
  424. /// determined based on the application proxy set using
  425. /// QNetworkProxy::setApplicationProxy().
  426. /// \param proxy QNetworkProxy
  427. void QXmppConfiguration::setNetworkProxy(const QNetworkProxy& proxy)
  428. {
  429. d->networkProxy = proxy;
  430. }
  431. /// Returns the specified network proxy.
  432. /// The default value is QNetworkProxy::DefaultProxy that is the proxy is
  433. /// determined based on the application proxy set using
  434. /// QNetworkProxy::setApplicationProxy().
  435. /// \return QNetworkProxy
  436. QNetworkProxy QXmppConfiguration::networkProxy() const
  437. {
  438. return d->networkProxy;
  439. }
  440. /// Specifies the interval in seconds at which keep alive (ping) packets
  441. /// will be sent to the server.
  442. ///
  443. /// If set to zero, no keep alive packets will be sent.
  444. ///
  445. /// The default value is 60 seconds.
  446. void QXmppConfiguration::setKeepAliveInterval(int secs)
  447. {
  448. d->keepAliveInterval = secs;
  449. }
  450. /// Returns the keep alive interval in seconds.
  451. ///
  452. /// The default value is 60 seconds.
  453. int QXmppConfiguration::keepAliveInterval() const
  454. {
  455. return d->keepAliveInterval;
  456. }
  457. /// Specifies the maximum time in seconds to wait for a keep alive response
  458. /// from the server before considering we are disconnected.
  459. ///
  460. /// If set to zero or a value larger than the keep alive interval,
  461. /// no timeout will occur.
  462. ///
  463. /// The default value is 20 seconds.
  464. void QXmppConfiguration::setKeepAliveTimeout(int secs)
  465. {
  466. d->keepAliveTimeout = secs;
  467. }
  468. /// Returns the keep alive timeout in seconds.
  469. ///
  470. /// The default value is 20 seconds.
  471. int QXmppConfiguration::keepAliveTimeout() const
  472. {
  473. return d->keepAliveTimeout;
  474. }
  475. /// Specifies a list of trusted CA certificates.
  476. void QXmppConfiguration::setCaCertificates(const QList<QSslCertificate> &caCertificates)
  477. {
  478. d->caCertificates = caCertificates;
  479. }
  480. /// Returns the a list of trusted CA certificates.
  481. QList<QSslCertificate> QXmppConfiguration::caCertificates() const
  482. {
  483. return d->caCertificates;
  484. }