You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/xmpp/xmpptask.h"
  28. #include "talk/xmpp/xmppclient.h"
  29. #include "talk/xmpp/xmppengine.h"
  30. #include "talk/xmpp/constants.h"
  31. namespace buzz {
  32. XmppClientInterface::XmppClientInterface() {
  33. }
  34. XmppClientInterface::~XmppClientInterface() {
  35. }
  36. XmppTask::XmppTask(XmppTaskParentInterface* parent,
  37. XmppEngine::HandlerLevel level)
  38. : XmppTaskBase(parent), stopped_(false) {
  39. #ifdef _DEBUG
  40. debug_force_timeout_ = false;
  41. #endif
  42. id_ = GetClient()->NextId();
  43. GetClient()->AddXmppTask(this, level);
  44. GetClient()->SignalDisconnected.connect(this, &XmppTask::OnDisconnect);
  45. }
  46. XmppTask::~XmppTask() {
  47. StopImpl();
  48. }
  49. void XmppTask::StopImpl() {
  50. while (NextStanza() != NULL) {}
  51. if (!stopped_) {
  52. GetClient()->RemoveXmppTask(this);
  53. GetClient()->SignalDisconnected.disconnect(this);
  54. stopped_ = true;
  55. }
  56. }
  57. XmppReturnStatus XmppTask::SendStanza(const XmlElement* stanza) {
  58. if (stopped_)
  59. return XMPP_RETURN_BADSTATE;
  60. return GetClient()->SendStanza(stanza);
  61. }
  62. XmppReturnStatus XmppTask::SendStanzaError(const XmlElement* element_original,
  63. XmppStanzaError code,
  64. const std::string& text) {
  65. if (stopped_)
  66. return XMPP_RETURN_BADSTATE;
  67. return GetClient()->SendStanzaError(element_original, code, text);
  68. }
  69. void XmppTask::Stop() {
  70. StopImpl();
  71. Task::Stop();
  72. }
  73. void XmppTask::OnDisconnect() {
  74. Error();
  75. }
  76. void XmppTask::QueueStanza(const XmlElement* stanza) {
  77. #ifdef _DEBUG
  78. if (debug_force_timeout_)
  79. return;
  80. #endif
  81. stanza_queue_.push_back(new XmlElement(*stanza));
  82. Wake();
  83. }
  84. const XmlElement* XmppTask::NextStanza() {
  85. XmlElement* result = NULL;
  86. if (!stanza_queue_.empty()) {
  87. result = stanza_queue_.front();
  88. stanza_queue_.pop_front();
  89. }
  90. next_stanza_.reset(result);
  91. return result;
  92. }
  93. XmlElement* XmppTask::MakeIq(const std::string& type,
  94. const buzz::Jid& to,
  95. const std::string& id) {
  96. XmlElement* result = new XmlElement(QN_IQ);
  97. if (!type.empty())
  98. result->AddAttr(QN_TYPE, type);
  99. if (!to.IsEmpty())
  100. result->AddAttr(QN_TO, to.Str());
  101. if (!id.empty())
  102. result->AddAttr(QN_ID, id);
  103. return result;
  104. }
  105. XmlElement* XmppTask::MakeIqResult(const XmlElement * query) {
  106. XmlElement* result = new XmlElement(QN_IQ);
  107. result->AddAttr(QN_TYPE, STR_RESULT);
  108. if (query->HasAttr(QN_FROM)) {
  109. result->AddAttr(QN_TO, query->Attr(QN_FROM));
  110. }
  111. result->AddAttr(QN_ID, query->Attr(QN_ID));
  112. return result;
  113. }
  114. bool XmppTask::MatchResponseIq(const XmlElement* stanza,
  115. const Jid& to,
  116. const std::string& id) {
  117. if (stanza->Name() != QN_IQ)
  118. return false;
  119. if (stanza->Attr(QN_ID) != id)
  120. return false;
  121. return MatchStanzaFrom(stanza, to);
  122. }
  123. bool XmppTask::MatchStanzaFrom(const XmlElement* stanza,
  124. const Jid& to) {
  125. Jid from(stanza->Attr(QN_FROM));
  126. if (from == to)
  127. return true;
  128. // We address the server as "", check if we are doing so here.
  129. if (!to.IsEmpty())
  130. return false;
  131. // It is legal for the server to identify itself with "domain" or
  132. // "myself@domain"
  133. Jid me = GetClient()->jid();
  134. return (from == Jid(me.domain())) || (from == me.BareJid());
  135. }
  136. bool XmppTask::MatchRequestIq(const XmlElement* stanza,
  137. const std::string& type,
  138. const QName& qn) {
  139. if (stanza->Name() != QN_IQ)
  140. return false;
  141. if (stanza->Attr(QN_TYPE) != type)
  142. return false;
  143. if (stanza->FirstNamed(qn) == NULL)
  144. return false;
  145. return true;
  146. }
  147. }