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.

302 lines
11 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. #ifndef _rostermoduleimpl_h_
  28. #define _rostermoduleimpl_h_
  29. #include "talk/xmpp/moduleimpl.h"
  30. #include "talk/xmpp/rostermodule.h"
  31. namespace buzz {
  32. //! Presence Information
  33. //! This class stores both presence information for outgoing presence and is
  34. //! returned by methods in XmppRosterModule to represent received incoming
  35. //! presence information. When this class is writeable (non-const) then each
  36. //! update to any property will set the inner xml. Setting the raw_xml will
  37. //! rederive all of the other properties.
  38. class XmppPresenceImpl : public XmppPresence {
  39. public:
  40. virtual ~XmppPresenceImpl() {}
  41. //! The from Jid of for the presence information.
  42. //! Typically this will be a full Jid with resource specified. For outgoing
  43. //! presence this should remain JID_NULL and will be scrubbed from the
  44. //! stanza when being sent.
  45. virtual const Jid jid() const;
  46. //! Is the contact available?
  47. virtual XmppPresenceAvailable available() const;
  48. //! Sets if the user is available or not
  49. virtual XmppReturnStatus set_available(XmppPresenceAvailable available);
  50. //! The show value of the presence info
  51. virtual XmppPresenceShow presence_show() const;
  52. //! Set the presence show value
  53. virtual XmppReturnStatus set_presence_show(XmppPresenceShow show);
  54. //! The Priority of the presence info
  55. virtual int priority() const;
  56. //! Set the priority of the presence
  57. virtual XmppReturnStatus set_priority(int priority);
  58. //! The plain text status of the presence info.
  59. //! If there are multiple status because of language, this will either be a
  60. //! status that is not tagged for language or the first available
  61. virtual const std::string status() const;
  62. //! Sets the status for the presence info.
  63. //! If there is more than one status present already then this will remove
  64. //! them all and replace it with one status element we no specified language
  65. virtual XmppReturnStatus set_status(const std::string& status);
  66. //! The connection status
  67. virtual XmppPresenceConnectionStatus connection_status() const;
  68. //! The focus obfuscated GAIA id
  69. virtual const std::string google_user_id() const;
  70. //! The nickname in the presence
  71. virtual const std::string nickname() const;
  72. //! The raw xml of the presence update
  73. virtual const XmlElement* raw_xml() const;
  74. //! Sets the raw presence stanza for the presence update
  75. //! This will cause all other data items in this structure to be rederived
  76. virtual XmppReturnStatus set_raw_xml(const XmlElement * xml);
  77. private:
  78. XmppPresenceImpl();
  79. friend class XmppPresence;
  80. friend class XmppRosterModuleImpl;
  81. void CreateRawXmlSkeleton();
  82. // Store everything in the XML element. If this becomes a perf issue we can
  83. // cache the data.
  84. talk_base::scoped_ptr<XmlElement> raw_xml_;
  85. };
  86. //! A contact as given by the server
  87. class XmppRosterContactImpl : public XmppRosterContact {
  88. public:
  89. virtual ~XmppRosterContactImpl() {}
  90. //! The jid for the contact.
  91. //! Typically this will be a bare Jid.
  92. virtual const Jid jid() const;
  93. //! Sets the jid for the roster contact update
  94. virtual XmppReturnStatus set_jid(const Jid& jid);
  95. //! The name (nickname) stored for this contact
  96. virtual const std::string name() const;
  97. //! Sets the name
  98. virtual XmppReturnStatus set_name(const std::string& name);
  99. //! The Presence subscription state stored on the server for this contact
  100. //! This is never settable and will be ignored when generating a roster
  101. //! add/update request
  102. virtual XmppSubscriptionState subscription_state() const;
  103. //! The number of Groups applied to this contact
  104. virtual size_t GetGroupCount() const;
  105. //! Gets a Group applied to the contact based on index.
  106. virtual const std::string GetGroup(size_t index) const;
  107. //! Adds a group to this contact.
  108. //! This will return a no error if the group is already present.
  109. virtual XmppReturnStatus AddGroup(const std::string& group);
  110. //! Removes a group from the contact.
  111. //! This will return no error if the group isn't there
  112. virtual XmppReturnStatus RemoveGroup(const std::string& group);
  113. //! The raw xml for this roster contact
  114. virtual const XmlElement* raw_xml() const;
  115. //! Sets the raw presence stanza for the presence update
  116. //! This will cause all other data items in this structure to be rederived
  117. virtual XmppReturnStatus set_raw_xml(const XmlElement * xml);
  118. private:
  119. XmppRosterContactImpl();
  120. void CreateRawXmlSkeleton();
  121. void SetXmlFromWire(const XmlElement * xml);
  122. void ResetGroupCache();
  123. bool FindGroup(const std::string& group,
  124. XmlElement** element,
  125. XmlChild** child_before);
  126. friend class XmppRosterContact;
  127. friend class XmppRosterModuleImpl;
  128. int group_count_;
  129. int group_index_returned_;
  130. XmlElement * group_returned_;
  131. talk_base::scoped_ptr<XmlElement> raw_xml_;
  132. };
  133. //! An XmppModule for handle roster and presence functionality
  134. class XmppRosterModuleImpl : public XmppModuleImpl,
  135. public XmppRosterModule, public XmppIqHandler {
  136. public:
  137. virtual ~XmppRosterModuleImpl();
  138. IMPLEMENT_XMPPMODULE
  139. //! Sets the roster handler (callbacks) for the module
  140. virtual XmppReturnStatus set_roster_handler(XmppRosterHandler * handler);
  141. //! Gets the roster handler for the module
  142. virtual XmppRosterHandler* roster_handler();
  143. // USER PRESENCE STATE -------------------------------------------------------
  144. //! Gets the aggregate outgoing presence
  145. //! This object is non-const and be edited directly. No update is sent
  146. //! to the server until a Broadcast is sent
  147. virtual XmppPresence* outgoing_presence();
  148. //! Broadcasts that the user is available.
  149. //! Nothing with respect to presence is sent until this is called.
  150. virtual XmppReturnStatus BroadcastPresence();
  151. //! Sends a directed presence to a Jid
  152. //! Note that the client doesn't store where directed presence notifications
  153. //! have been sent. The server can keep the appropriate state
  154. virtual XmppReturnStatus SendDirectedPresence(const XmppPresence* presence,
  155. const Jid& to_jid);
  156. // INCOMING PRESENCE STATUS --------------------------------------------------
  157. //! Returns the number of incoming presence data recorded
  158. virtual size_t GetIncomingPresenceCount();
  159. //! Returns an incoming presence datum based on index
  160. virtual const XmppPresence* GetIncomingPresence(size_t index);
  161. //! Gets the number of presence data for a bare Jid
  162. //! There may be a datum per resource
  163. virtual size_t GetIncomingPresenceForJidCount(const Jid& jid);
  164. //! Returns a single presence data for a Jid based on index
  165. virtual const XmppPresence* GetIncomingPresenceForJid(const Jid& jid,
  166. size_t index);
  167. // ROSTER MANAGEMENT ---------------------------------------------------------
  168. //! Requests an update of the roster from the server
  169. //! This must be called to initialize the client side cache of the roster
  170. //! After this is sent the server should keep this module apprised of any
  171. //! changes.
  172. virtual XmppReturnStatus RequestRosterUpdate();
  173. //! Returns the number of contacts in the roster
  174. virtual size_t GetRosterContactCount();
  175. //! Returns a contact by index
  176. virtual const XmppRosterContact* GetRosterContact(size_t index);
  177. //! Finds a contact by Jid
  178. virtual const XmppRosterContact* FindRosterContact(const Jid& jid);
  179. //! Send a request to the server to add a contact
  180. //! Note that the contact won't show up in the roster until the server can
  181. //! respond. This happens async when the socket is being serviced
  182. virtual XmppReturnStatus RequestRosterChange(
  183. const XmppRosterContact* contact);
  184. //! Request that the server remove a contact
  185. //! The jabber protocol specifies that the server should also cancel any
  186. //! subscriptions when this is done. Like adding, this contact won't be
  187. //! removed until the server responds.
  188. virtual XmppReturnStatus RequestRosterRemove(const Jid& jid);
  189. // SUBSCRIPTION MANAGEMENT ---------------------------------------------------
  190. //! Request a subscription to presence notifications form a Jid
  191. virtual XmppReturnStatus RequestSubscription(const Jid& jid);
  192. //! Cancel a subscription to presence notifications from a Jid
  193. virtual XmppReturnStatus CancelSubscription(const Jid& jid);
  194. //! Approve a request to deliver presence notifications to a jid
  195. virtual XmppReturnStatus ApproveSubscriber(const Jid& jid);
  196. //! Deny or cancel presence notification deliver to a jid
  197. virtual XmppReturnStatus CancelSubscriber(const Jid& jid);
  198. // XmppIqHandler IMPLEMENTATION ----------------------------------------------
  199. virtual void IqResponse(XmppIqCookie cookie, const XmlElement * stanza);
  200. protected:
  201. // XmppModuleImpl OVERRIDES --------------------------------------------------
  202. virtual bool HandleStanza(const XmlElement *);
  203. // PRIVATE DATA --------------------------------------------------------------
  204. private:
  205. friend class XmppRosterModule;
  206. XmppRosterModuleImpl();
  207. // Helper functions
  208. void DeleteIncomingPresence();
  209. void DeleteContacts();
  210. XmppReturnStatus SendSubscriptionRequest(const Jid& jid,
  211. const std::string& type);
  212. void InternalSubscriptionRequest(const Jid& jid, const XmlElement* stanza,
  213. XmppSubscriptionRequestType request_type);
  214. void InternalIncomingPresence(const Jid& jid, const XmlElement* stanza);
  215. void InternalIncomingPresenceError(const Jid& jid, const XmlElement* stanza);
  216. void InternalRosterItems(const XmlElement* stanza);
  217. // Member data
  218. XmppPresenceImpl outgoing_presence_;
  219. XmppRosterHandler* roster_handler_;
  220. typedef std::vector<XmppPresenceImpl*> PresenceVector;
  221. typedef std::map<Jid, PresenceVector*> JidPresenceVectorMap;
  222. talk_base::scoped_ptr<JidPresenceVectorMap> incoming_presence_map_;
  223. talk_base::scoped_ptr<PresenceVector> incoming_presence_vector_;
  224. typedef std::vector<XmppRosterContactImpl*> ContactVector;
  225. talk_base::scoped_ptr<ContactVector> contacts_;
  226. };
  227. }
  228. #endif