QXmppStreamFeatures.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Jeremy Lainé
  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 <QDomElement>
  24. #include "QXmppConstants.h"
  25. #include "QXmppStreamFeatures.h"
  26. QXmppStreamFeatures::QXmppStreamFeatures()
  27. : m_bindMode(Disabled),
  28. m_sessionMode(Disabled),
  29. m_nonSaslAuthMode(Disabled),
  30. m_tlsMode(Disabled)
  31. {
  32. }
  33. QXmppStreamFeatures::Mode QXmppStreamFeatures::bindMode() const
  34. {
  35. return m_bindMode;
  36. }
  37. void QXmppStreamFeatures::setBindMode(QXmppStreamFeatures::Mode mode)
  38. {
  39. m_bindMode = mode;
  40. }
  41. QXmppStreamFeatures::Mode QXmppStreamFeatures::sessionMode() const
  42. {
  43. return m_sessionMode;
  44. }
  45. void QXmppStreamFeatures::setSessionMode(Mode mode)
  46. {
  47. m_sessionMode = mode;
  48. }
  49. QXmppStreamFeatures::Mode QXmppStreamFeatures::nonSaslAuthMode() const
  50. {
  51. return m_nonSaslAuthMode;
  52. }
  53. void QXmppStreamFeatures::setNonSaslAuthMode(QXmppStreamFeatures::Mode mode)
  54. {
  55. m_nonSaslAuthMode = mode;
  56. }
  57. QStringList QXmppStreamFeatures::authMechanisms() const
  58. {
  59. return m_authMechanisms;
  60. }
  61. void QXmppStreamFeatures::setAuthMechanisms(const QStringList &mechanisms)
  62. {
  63. m_authMechanisms = mechanisms;
  64. }
  65. QStringList QXmppStreamFeatures::compressionMethods() const
  66. {
  67. return m_compressionMethods;
  68. }
  69. void QXmppStreamFeatures::setCompressionMethods(const QStringList &methods)
  70. {
  71. m_compressionMethods = methods;
  72. }
  73. QXmppStreamFeatures::Mode QXmppStreamFeatures::tlsMode() const
  74. {
  75. return m_tlsMode;
  76. }
  77. void QXmppStreamFeatures::setTlsMode(QXmppStreamFeatures::Mode mode)
  78. {
  79. m_tlsMode = mode;
  80. }
  81. /// \cond
  82. bool QXmppStreamFeatures::isStreamFeatures(const QDomElement &element)
  83. {
  84. return element.namespaceURI() == ns_stream &&
  85. element.tagName() == "features";
  86. }
  87. static QXmppStreamFeatures::Mode readFeature(const QDomElement &element, const char *tagName, const char *tagNs)
  88. {
  89. QDomElement subElement = element.firstChildElement(tagName);
  90. if (subElement.namespaceURI() == tagNs)
  91. {
  92. if (!subElement.firstChildElement("required").isNull())
  93. return QXmppStreamFeatures::Required;
  94. else
  95. return QXmppStreamFeatures::Enabled;
  96. } else {
  97. return QXmppStreamFeatures::Disabled;
  98. }
  99. }
  100. void QXmppStreamFeatures::parse(const QDomElement &element)
  101. {
  102. m_bindMode = readFeature(element, "bind", ns_bind);
  103. m_sessionMode = readFeature(element, "session", ns_session);
  104. m_nonSaslAuthMode = readFeature(element, "auth", ns_authFeature);
  105. m_tlsMode = readFeature(element, "starttls", ns_tls);
  106. // parse advertised compression methods
  107. QDomElement compression = element.firstChildElement("compression");
  108. if (compression.namespaceURI() == ns_compressFeature)
  109. {
  110. QDomElement subElement = compression.firstChildElement("method");
  111. while(!subElement.isNull())
  112. {
  113. m_compressionMethods << subElement.text();
  114. subElement = subElement.nextSiblingElement("method");
  115. }
  116. }
  117. // parse advertised SASL Authentication mechanisms
  118. QDomElement mechs = element.firstChildElement("mechanisms");
  119. if (mechs.namespaceURI() == ns_sasl)
  120. {
  121. QDomElement subElement = mechs.firstChildElement("mechanism");
  122. while(!subElement.isNull()) {
  123. m_authMechanisms << subElement.text();
  124. subElement = subElement.nextSiblingElement("mechanism");
  125. }
  126. }
  127. }
  128. static void writeFeature(QXmlStreamWriter *writer, const char *tagName, const char *tagNs, QXmppStreamFeatures::Mode mode)
  129. {
  130. if (mode != QXmppStreamFeatures::Disabled)
  131. {
  132. writer->writeStartElement(tagName);
  133. writer->writeAttribute("xmlns", tagNs);
  134. if (mode == QXmppStreamFeatures::Required)
  135. writer->writeEmptyElement("required");
  136. writer->writeEndElement();
  137. }
  138. }
  139. void QXmppStreamFeatures::toXml(QXmlStreamWriter *writer) const
  140. {
  141. writer->writeStartElement("stream:features");
  142. writeFeature(writer, "bind", ns_bind, m_bindMode);
  143. writeFeature(writer, "session", ns_session, m_sessionMode);
  144. writeFeature(writer, "auth", ns_authFeature, m_nonSaslAuthMode);
  145. writeFeature(writer, "starttls", ns_tls, m_tlsMode);
  146. if (!m_compressionMethods.isEmpty())
  147. {
  148. writer->writeStartElement("compression");
  149. writer->writeAttribute("xmlns", ns_compressFeature);
  150. foreach (const QString &method, m_compressionMethods)
  151. writer->writeTextElement("method", method);
  152. writer->writeEndElement();
  153. }
  154. if (!m_authMechanisms.isEmpty())
  155. {
  156. writer->writeStartElement("mechanisms");
  157. writer->writeAttribute("xmlns", ns_sasl);
  158. foreach (const QString &mechanism, m_authMechanisms)
  159. writer->writeTextElement("mechanism", mechanism);
  160. writer->writeEndElement();
  161. }
  162. writer->writeEndElement();
  163. }
  164. /// \endcond