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.

217 lines
7.3 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/pubsub_task.h"
  28. #include <map>
  29. #include <string>
  30. #include "talk/base/common.h"
  31. #include "talk/xmpp/constants.h"
  32. #include "talk/xmpp/xmppengine.h"
  33. namespace buzz {
  34. PubsubTask::PubsubTask(XmppTaskParentInterface* parent,
  35. const buzz::Jid& pubsub_node_jid)
  36. : buzz::XmppTask(parent, buzz::XmppEngine::HL_SENDER),
  37. pubsub_node_jid_(pubsub_node_jid) {
  38. }
  39. PubsubTask::~PubsubTask() {
  40. }
  41. // Checks for pubsub publish events as well as responses to get IQs.
  42. bool PubsubTask::HandleStanza(const buzz::XmlElement* stanza) {
  43. const buzz::QName& stanza_name(stanza->Name());
  44. if (stanza_name == buzz::QN_MESSAGE) {
  45. if (MatchStanzaFrom(stanza, pubsub_node_jid_)) {
  46. const buzz::XmlElement* pubsub_event_item =
  47. stanza->FirstNamed(QN_PUBSUB_EVENT);
  48. if (pubsub_event_item != NULL) {
  49. QueueStanza(pubsub_event_item);
  50. return true;
  51. }
  52. }
  53. } else if (stanza_name == buzz::QN_IQ) {
  54. if (MatchResponseIq(stanza, pubsub_node_jid_, task_id())) {
  55. const buzz::XmlElement* pubsub_item = stanza->FirstNamed(QN_PUBSUB);
  56. if (pubsub_item != NULL) {
  57. QueueStanza(pubsub_item);
  58. return true;
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. int PubsubTask::ProcessResponse() {
  65. const buzz::XmlElement* stanza = NextStanza();
  66. if (stanza == NULL) {
  67. return STATE_BLOCKED;
  68. }
  69. if (stanza->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) {
  70. OnPubsubError(stanza->FirstNamed(buzz::QN_ERROR));
  71. return STATE_RESPONSE;
  72. }
  73. const buzz::QName& stanza_name(stanza->Name());
  74. if (stanza_name == QN_PUBSUB_EVENT) {
  75. HandlePubsubEventMessage(stanza);
  76. } else if (stanza_name == QN_PUBSUB) {
  77. HandlePubsubIqGetResponse(stanza);
  78. }
  79. return STATE_RESPONSE;
  80. }
  81. // Registers a function pointer to be called when the value of the pubsub
  82. // node changes.
  83. // Note that this does not actually change the XMPP pubsub
  84. // subscription. All publish events are always received by everyone in the
  85. // MUC. This function just controls whether the handle function will get
  86. // called when the event is received.
  87. bool PubsubTask::SubscribeToNode(const std::string& pubsub_node,
  88. NodeHandler handler) {
  89. subscribed_nodes_[pubsub_node] = handler;
  90. talk_base::scoped_ptr<buzz::XmlElement> get_iq_request(
  91. MakeIq(buzz::STR_GET, pubsub_node_jid_, task_id()));
  92. if (!get_iq_request) {
  93. return false;
  94. }
  95. buzz::XmlElement* pubsub_element = new buzz::XmlElement(QN_PUBSUB, true);
  96. buzz::XmlElement* items_element = new buzz::XmlElement(QN_PUBSUB_ITEMS, true);
  97. items_element->AddAttr(buzz::QN_NODE, pubsub_node);
  98. pubsub_element->AddElement(items_element);
  99. get_iq_request->AddElement(pubsub_element);
  100. if (SendStanza(get_iq_request.get()) != buzz::XMPP_RETURN_OK) {
  101. return false;
  102. }
  103. return true;
  104. }
  105. void PubsubTask::UnsubscribeFromNode(const std::string& pubsub_node) {
  106. subscribed_nodes_.erase(pubsub_node);
  107. }
  108. void PubsubTask::OnPubsubError(const buzz::XmlElement* error_stanza) {
  109. }
  110. // Checks for a pubsub event message like the following:
  111. //
  112. // <message from="muvc-private-chat-some-id@groupchat.google.com"
  113. // to="john@site.com/gcomm582B14C9">
  114. // <event xmlns:"http://jabber.org/protocol/pubsub#event">
  115. // <items node="node-name">
  116. // <item id="some-id">
  117. // <payload/>
  118. // </item>
  119. // </items>
  120. // </event>
  121. // </message>
  122. //
  123. // It also checks for retraction event messages like the following:
  124. //
  125. // <message from="muvc-private-chat-some-id@groupchat.google.com"
  126. // to="john@site.com/gcomm582B14C9">
  127. // <event xmlns:"http://jabber.org/protocol/pubsub#event">
  128. // <items node="node-name">
  129. // <retract id="some-id"/>
  130. // </items>
  131. // </event>
  132. // </message>
  133. void PubsubTask::HandlePubsubEventMessage(
  134. const buzz::XmlElement* pubsub_event) {
  135. ASSERT(pubsub_event->Name() == QN_PUBSUB_EVENT);
  136. for (const buzz::XmlChild* child = pubsub_event->FirstChild();
  137. child != NULL;
  138. child = child->NextChild()) {
  139. const buzz::XmlElement* child_element = child->AsElement();
  140. const buzz::QName& child_name(child_element->Name());
  141. if (child_name == QN_PUBSUB_EVENT_ITEMS) {
  142. HandlePubsubItems(child_element);
  143. }
  144. }
  145. }
  146. // Checks for a response to an pubsub IQ get like the following:
  147. //
  148. // <iq from="muvc-private-chat-some-id@groupchat.google.com"
  149. // to="john@site.com/gcomm582B14C9"
  150. // type="result">
  151. // <pubsub xmlns:"http://jabber.org/protocol/pubsub">
  152. // <items node="node-name">
  153. // <item id="some-id">
  154. // <payload/>
  155. // </item>
  156. // </items>
  157. // </event>
  158. // </message>
  159. void PubsubTask::HandlePubsubIqGetResponse(
  160. const buzz::XmlElement* pubsub_iq_response) {
  161. ASSERT(pubsub_iq_response->Name() == QN_PUBSUB);
  162. for (const buzz::XmlChild* child = pubsub_iq_response->FirstChild();
  163. child != NULL;
  164. child = child->NextChild()) {
  165. const buzz::XmlElement* child_element = child->AsElement();
  166. const buzz::QName& child_name(child_element->Name());
  167. if (child_name == QN_PUBSUB_ITEMS) {
  168. HandlePubsubItems(child_element);
  169. }
  170. }
  171. }
  172. // Calls registered handlers in response to pubsub event or response to
  173. // IQ pubsub get.
  174. // 'items' is the child of a pubsub#event:event node or pubsub:pubsub node.
  175. void PubsubTask::HandlePubsubItems(const buzz::XmlElement* items) {
  176. ASSERT(items->HasAttr(QN_NODE));
  177. const std::string& node_name(items->Attr(QN_NODE));
  178. NodeSubscriptions::iterator iter = subscribed_nodes_.find(node_name);
  179. if (iter != subscribed_nodes_.end()) {
  180. NodeHandler handler = iter->second;
  181. const buzz::XmlElement* item = items->FirstElement();
  182. while (item != NULL) {
  183. const buzz::QName& item_name(item->Name());
  184. if (item_name != QN_PUBSUB_EVENT_ITEM &&
  185. item_name != QN_PUBSUB_EVENT_RETRACT &&
  186. item_name != QN_PUBSUB_ITEM) {
  187. continue;
  188. }
  189. (this->*handler)(item);
  190. item = item->NextElement();
  191. }
  192. return;
  193. }
  194. }
  195. }