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.

214 lines
7.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, 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/pubsubtasks.h"
  28. #include <string>
  29. #include <vector>
  30. #include "talk/xmpp/constants.h"
  31. #include "talk/xmpp/receivetask.h"
  32. // An implementation of the tasks for XEP-0060
  33. // (http://xmpp.org/extensions/xep-0060.html).
  34. namespace buzz {
  35. namespace {
  36. bool IsPubSubEventItemsElem(const XmlElement* stanza,
  37. const std::string& expected_node) {
  38. if (stanza->Name() != QN_MESSAGE) {
  39. return false;
  40. }
  41. const XmlElement* event_elem = stanza->FirstNamed(QN_PUBSUB_EVENT);
  42. if (event_elem == NULL) {
  43. return false;
  44. }
  45. const XmlElement* items_elem = event_elem->FirstNamed(QN_PUBSUB_EVENT_ITEMS);
  46. if (items_elem == NULL) {
  47. return false;
  48. }
  49. const std::string& actual_node = items_elem->Attr(QN_NODE);
  50. return (actual_node == expected_node);
  51. }
  52. // Creates <pubsub node="node"><items></pubsub>
  53. XmlElement* CreatePubSubItemsElem(const std::string& node) {
  54. XmlElement* items_elem = new XmlElement(QN_PUBSUB_ITEMS, false);
  55. items_elem->AddAttr(QN_NODE, node);
  56. XmlElement* pubsub_elem = new XmlElement(QN_PUBSUB, false);
  57. pubsub_elem->AddElement(items_elem);
  58. return pubsub_elem;
  59. }
  60. // Creates <pubsub node="node"><publish><item id="itemid">payload</item>...
  61. // Takes ownership of payload.
  62. XmlElement* CreatePubSubPublishItemElem(
  63. const std::string& node,
  64. const std::string& itemid,
  65. const std::vector<XmlElement*>& children) {
  66. XmlElement* pubsub_elem = new XmlElement(QN_PUBSUB, true);
  67. XmlElement* publish_elem = new XmlElement(QN_PUBSUB_PUBLISH, false);
  68. publish_elem->AddAttr(QN_NODE, node);
  69. XmlElement* item_elem = new XmlElement(QN_PUBSUB_ITEM, false);
  70. item_elem->AddAttr(QN_ID, itemid);
  71. for (std::vector<XmlElement*>::const_iterator child = children.begin();
  72. child != children.end(); ++child) {
  73. item_elem->AddElement(*child);
  74. }
  75. publish_elem->AddElement(item_elem);
  76. pubsub_elem->AddElement(publish_elem);
  77. return pubsub_elem;
  78. }
  79. // Creates <pubsub node="node"><publish><item id="itemid">payload</item>...
  80. // Takes ownership of payload.
  81. XmlElement* CreatePubSubRetractItemElem(const std::string& node,
  82. const std::string& itemid) {
  83. XmlElement* pubsub_elem = new XmlElement(QN_PUBSUB, true);
  84. XmlElement* retract_elem = new XmlElement(QN_PUBSUB_RETRACT, false);
  85. retract_elem->AddAttr(QN_NODE, node);
  86. retract_elem->AddAttr(QN_NOTIFY, "true");
  87. XmlElement* item_elem = new XmlElement(QN_PUBSUB_ITEM, false);
  88. item_elem->AddAttr(QN_ID, itemid);
  89. retract_elem->AddElement(item_elem);
  90. pubsub_elem->AddElement(retract_elem);
  91. return pubsub_elem;
  92. }
  93. void ParseItem(const XmlElement* item_elem,
  94. std::vector<PubSubItem>* items) {
  95. PubSubItem item;
  96. item.itemid = item_elem->Attr(QN_ID);
  97. item.elem = item_elem;
  98. items->push_back(item);
  99. }
  100. // Right now, <retract>s are treated the same as items with empty
  101. // payloads. We may want to change it in the future, but right now
  102. // it's sufficient for our needs.
  103. void ParseRetract(const XmlElement* retract_elem,
  104. std::vector<PubSubItem>* items) {
  105. ParseItem(retract_elem, items);
  106. }
  107. void ParseEventItemsElem(const XmlElement* stanza,
  108. std::vector<PubSubItem>* items) {
  109. const XmlElement* event_elem = stanza->FirstNamed(QN_PUBSUB_EVENT);
  110. if (event_elem != NULL) {
  111. const XmlElement* items_elem =
  112. event_elem->FirstNamed(QN_PUBSUB_EVENT_ITEMS);
  113. if (items_elem != NULL) {
  114. for (const XmlElement* item_elem =
  115. items_elem->FirstNamed(QN_PUBSUB_EVENT_ITEM);
  116. item_elem != NULL;
  117. item_elem = item_elem->NextNamed(QN_PUBSUB_EVENT_ITEM)) {
  118. ParseItem(item_elem, items);
  119. }
  120. for (const XmlElement* retract_elem =
  121. items_elem->FirstNamed(QN_PUBSUB_EVENT_RETRACT);
  122. retract_elem != NULL;
  123. retract_elem = retract_elem->NextNamed(QN_PUBSUB_EVENT_RETRACT)) {
  124. ParseRetract(retract_elem, items);
  125. }
  126. }
  127. }
  128. }
  129. void ParsePubSubItemsElem(const XmlElement* stanza,
  130. std::vector<PubSubItem>* items) {
  131. const XmlElement* pubsub_elem = stanza->FirstNamed(QN_PUBSUB);
  132. if (pubsub_elem != NULL) {
  133. const XmlElement* items_elem = pubsub_elem->FirstNamed(QN_PUBSUB_ITEMS);
  134. if (items_elem != NULL) {
  135. for (const XmlElement* item_elem = items_elem->FirstNamed(QN_PUBSUB_ITEM);
  136. item_elem != NULL;
  137. item_elem = item_elem->NextNamed(QN_PUBSUB_ITEM)) {
  138. ParseItem(item_elem, items);
  139. }
  140. }
  141. }
  142. }
  143. } // namespace
  144. PubSubRequestTask::PubSubRequestTask(XmppTaskParentInterface* parent,
  145. const Jid& pubsubjid,
  146. const std::string& node)
  147. : IqTask(parent, STR_GET, pubsubjid, CreatePubSubItemsElem(node)) {
  148. }
  149. void PubSubRequestTask::HandleResult(const XmlElement* stanza) {
  150. std::vector<PubSubItem> items;
  151. ParsePubSubItemsElem(stanza, &items);
  152. SignalResult(this, items);
  153. }
  154. bool PubSubReceiveTask::WantsStanza(const XmlElement* stanza) {
  155. return MatchStanzaFrom(stanza, pubsubjid_) &&
  156. IsPubSubEventItemsElem(stanza, node_);
  157. }
  158. void PubSubReceiveTask::ReceiveStanza(const XmlElement* stanza) {
  159. std::vector<PubSubItem> items;
  160. ParseEventItemsElem(stanza, &items);
  161. SignalUpdate(this, items);
  162. }
  163. PubSubPublishTask::PubSubPublishTask(XmppTaskParentInterface* parent,
  164. const Jid& pubsubjid,
  165. const std::string& node,
  166. const std::string& itemid,
  167. const std::vector<XmlElement*>& children)
  168. : IqTask(parent, STR_SET, pubsubjid,
  169. CreatePubSubPublishItemElem(node, itemid, children)),
  170. itemid_(itemid) {
  171. }
  172. void PubSubPublishTask::HandleResult(const XmlElement* stanza) {
  173. SignalResult(this);
  174. }
  175. PubSubRetractTask::PubSubRetractTask(XmppTaskParentInterface* parent,
  176. const Jid& pubsubjid,
  177. const std::string& node,
  178. const std::string& itemid)
  179. : IqTask(parent, STR_SET, pubsubjid,
  180. CreatePubSubRetractItemElem(node, itemid)),
  181. itemid_(itemid) {
  182. }
  183. void PubSubRetractTask::HandleResult(const XmlElement* stanza) {
  184. SignalResult(this);
  185. }
  186. } // namespace buzz