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.

218 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, 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 "rostertask.h"
  28. #include "talk/xmpp/constants.h"
  29. #include "talk/base/stream.h"
  30. #undef WIN32
  31. #ifdef WIN32
  32. #include "talk/app/win32/offlineroster.h"
  33. #endif
  34. namespace buzz {
  35. class RosterTask::RosterGetTask : public XmppTask {
  36. public:
  37. RosterGetTask(Task * parent) : XmppTask(parent, XmppEngine::HL_SINGLE),
  38. done_(false) {}
  39. virtual int ProcessStart();
  40. virtual int ProcessResponse();
  41. protected:
  42. virtual bool HandleStanza(const XmlElement * stanza);
  43. bool done_;
  44. };
  45. //==============================================================================
  46. // RosterTask
  47. //==============================================================================
  48. void RosterTask::RefreshRosterNow() {
  49. RosterGetTask* get_task = new RosterGetTask(this);
  50. ResumeTimeout();
  51. get_task->Start();
  52. }
  53. void RosterTask::TranslateItems(const XmlElement * rosterQueryResult) {
  54. #if defined(FEATURE_ENABLE_PSTN)
  55. #ifdef WIN32
  56. // We build up a list of contacts which have had information persisted offline.
  57. // we'll remove items from this list if we get a buzz::SUBSCRIBE_REMOVE
  58. // subscription. After updating all the items from the server, we'll then
  59. // update (and merge) any roster items left in our map of offline items
  60. XmlElement *el_local = OfflineRoster::RetrieveOfflineRoster(GetClient()->jid());
  61. std::map<buzz::Jid, RosterItem> jid_to_item;
  62. if (el_local) {
  63. for (XmlElement *el_item = el_local->FirstNamed(QN_ROSTER_ITEM);
  64. el_item != NULL;
  65. el_item = el_item->NextNamed(QN_ROSTER_ITEM)) {
  66. RosterItem roster_item;
  67. roster_item.FromXml(el_item);
  68. jid_to_item[roster_item.jid()] = roster_item;
  69. }
  70. }
  71. #endif // WIN32
  72. #endif // FEATURE_ENABLE_PSTN
  73. const XmlElement * xml_item;
  74. for (xml_item = rosterQueryResult->FirstNamed(QN_ROSTER_ITEM);
  75. xml_item != NULL; xml_item = xml_item->NextNamed(QN_ROSTER_ITEM)) {
  76. RosterItem roster_item;
  77. roster_item.FromXml(xml_item);
  78. if (roster_item.subscription() == buzz::SUBSCRIBE_REMOVE) {
  79. SignalRosterItemRemoved(roster_item);
  80. #if defined(FEATURE_ENABLE_PSTN)
  81. #ifdef WIN32
  82. std::map<buzz::Jid, RosterItem>::iterator it =
  83. jid_to_item.find(roster_item.jid());
  84. if (it != jid_to_item.end())
  85. jid_to_item.erase(it);
  86. #endif
  87. #endif
  88. } else {
  89. SignalRosterItemUpdated(roster_item, false);
  90. }
  91. }
  92. #if defined(FEATURE_ENABLE_PSTN)
  93. #ifdef WIN32
  94. for (std::map<buzz::Jid, RosterItem>::iterator it = jid_to_item.begin();
  95. it != jid_to_item.end(); ++it) {
  96. SignalRosterItemUpdated(it->second, true);
  97. }
  98. #endif
  99. #endif
  100. }
  101. int RosterTask::ProcessStart() {
  102. const XmlElement * stanza = NextStanza();
  103. if (stanza == NULL)
  104. return STATE_BLOCKED;
  105. if (stanza->Name() == QN_IQ) {
  106. SuspendTimeout();
  107. bool result = (stanza->Attr(QN_TYPE) == STR_RESULT);
  108. if (result)
  109. SignalRosterRefreshStarted();
  110. TranslateItems(stanza->FirstNamed(QN_ROSTER_QUERY));
  111. if (result)
  112. SignalRosterRefreshFinished();
  113. } else if (stanza->Name() == QN_PRESENCE) {
  114. Jid jid(stanza->Attr(QN_FROM));
  115. std::string type = stanza->Attr(QN_TYPE);
  116. if (type == "subscribe")
  117. SignalSubscribe(jid);
  118. else if (type == "unsubscribe")
  119. SignalUnsubscribe(jid);
  120. else if (type == "subscribed")
  121. SignalSubscribed(jid);
  122. else if (type == "unsubscribed")
  123. SignalUnsubscribed(jid);
  124. }
  125. return STATE_START;
  126. }
  127. bool RosterTask::HandleStanza(const XmlElement * stanza) {
  128. if (!MatchRequestIq(stanza, STR_SET, QN_ROSTER_QUERY)) {
  129. // Not a roster IQ. Look for a presence instead
  130. if (stanza->Name() != QN_PRESENCE)
  131. return false;
  132. if (!stanza->HasAttr(QN_TYPE))
  133. return false;
  134. std::string type = stanza->Attr(QN_TYPE);
  135. if (type == "subscribe" || type == "unsubscribe" ||
  136. type == "subscribed" || type == "unsubscribed") {
  137. QueueStanza(stanza);
  138. return true;
  139. }
  140. return false;
  141. }
  142. // only respect roster push from the server
  143. Jid from(stanza->Attr(QN_FROM));
  144. if (from != JID_EMPTY &&
  145. !from.BareEquals(GetClient()->jid()) &&
  146. from != Jid(GetClient()->jid().domain()))
  147. return false;
  148. XmlElement * result = MakeIqResult(stanza);
  149. result->AddElement(new XmlElement(QN_ROSTER_QUERY, true));
  150. SendStanza(result);
  151. QueueStanza(stanza);
  152. return true;
  153. }
  154. //==============================================================================
  155. // RosterTask::RosterGetTask
  156. //==============================================================================
  157. int RosterTask::RosterGetTask::ProcessStart() {
  158. talk_base::scoped_ptr<XmlElement> get(MakeIq(STR_GET, JID_EMPTY, task_id()));
  159. get->AddElement(new XmlElement(QN_ROSTER_QUERY, true));
  160. get->AddAttr(QN_XMLNS_GR, NS_GR, 1);
  161. get->AddAttr(QN_GR_EXT, "2", 1);
  162. get->AddAttr(QN_GR_INCLUDE, "all", 1);
  163. if (SendStanza(get.get()) != XMPP_RETURN_OK) {
  164. return STATE_ERROR;
  165. }
  166. return STATE_RESPONSE;
  167. }
  168. int RosterTask::RosterGetTask::ProcessResponse() {
  169. if (done_)
  170. return STATE_DONE;
  171. return STATE_BLOCKED;
  172. }
  173. bool RosterTask::RosterGetTask::HandleStanza(const XmlElement * stanza) {
  174. if (!MatchResponseIq(stanza, JID_EMPTY, task_id()))
  175. return false;
  176. if (stanza->Attr(QN_TYPE) != STR_RESULT)
  177. return false;
  178. // Queue the stanza with the parent so these don't get handled out of order
  179. RosterTask* parent = static_cast<RosterTask*>(GetParent());
  180. parent->QueueStanza(stanza);
  181. // Wake ourselves so we can go into the done state
  182. done_ = true;
  183. Wake();
  184. return true;
  185. }
  186. }