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.

349 lines
13 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 _xmppengine_h_
  28. #define _xmppengine_h_
  29. // also part of the API
  30. #include "talk/xmpp/jid.h"
  31. #include "talk/xmllite/qname.h"
  32. #include "talk/xmllite/xmlelement.h"
  33. namespace buzz {
  34. class XmppEngine;
  35. class SaslHandler;
  36. typedef void * XmppIqCookie;
  37. //! XMPP stanza error codes.
  38. //! Used in XmppEngine.SendStanzaError().
  39. enum XmppStanzaError {
  40. XSE_BAD_REQUEST,
  41. XSE_CONFLICT,
  42. XSE_FEATURE_NOT_IMPLEMENTED,
  43. XSE_FORBIDDEN,
  44. XSE_GONE,
  45. XSE_INTERNAL_SERVER_ERROR,
  46. XSE_ITEM_NOT_FOUND,
  47. XSE_JID_MALFORMED,
  48. XSE_NOT_ACCEPTABLE,
  49. XSE_NOT_ALLOWED,
  50. XSE_PAYMENT_REQUIRED,
  51. XSE_RECIPIENT_UNAVAILABLE,
  52. XSE_REDIRECT,
  53. XSE_REGISTRATION_REQUIRED,
  54. XSE_SERVER_NOT_FOUND,
  55. XSE_SERVER_TIMEOUT,
  56. XSE_RESOURCE_CONSTRAINT,
  57. XSE_SERVICE_UNAVAILABLE,
  58. XSE_SUBSCRIPTION_REQUIRED,
  59. XSE_UNDEFINED_CONDITION,
  60. XSE_UNEXPECTED_REQUEST,
  61. };
  62. // XmppReturnStatus
  63. // This is used by API functions to synchronously return status.
  64. enum XmppReturnStatus {
  65. XMPP_RETURN_OK,
  66. XMPP_RETURN_BADARGUMENT,
  67. XMPP_RETURN_BADSTATE,
  68. XMPP_RETURN_PENDING,
  69. XMPP_RETURN_UNEXPECTED,
  70. XMPP_RETURN_NOTYETIMPLEMENTED,
  71. };
  72. // TlsOptions
  73. // This is used by API to identify TLS setting.
  74. enum TlsOptions {
  75. TLS_DISABLED,
  76. TLS_ENABLED,
  77. TLS_REQUIRED
  78. };
  79. //! Callback for socket output for an XmppEngine connection.
  80. //! Register via XmppEngine.SetOutputHandler. An XmppEngine
  81. //! can call back to this handler while it is processing
  82. //! Connect, SendStanza, SendIq, Disconnect, or HandleInput.
  83. class XmppOutputHandler {
  84. public:
  85. virtual ~XmppOutputHandler() {}
  86. //! Deliver the specified bytes to the XMPP socket.
  87. virtual void WriteOutput(const char * bytes, size_t len) = 0;
  88. //! Initiate TLS encryption on the socket.
  89. //! The implementation must verify that the SSL
  90. //! certificate matches the given domainname.
  91. virtual void StartTls(const std::string & domainname) = 0;
  92. //! Called when engine wants the connecton closed.
  93. virtual void CloseConnection() = 0;
  94. };
  95. //! Callback to deliver engine state change notifications
  96. //! to the object managing the engine.
  97. class XmppSessionHandler {
  98. public:
  99. virtual ~XmppSessionHandler() {}
  100. //! Called when engine changes state. Argument is new state.
  101. virtual void OnStateChange(int state) = 0;
  102. };
  103. //! Callback to deliver stanzas to an Xmpp application module.
  104. //! Register via XmppEngine.SetDefaultSessionHandler or via
  105. //! XmppEngine.AddSessionHAndler.
  106. class XmppStanzaHandler {
  107. public:
  108. virtual ~XmppStanzaHandler() {}
  109. //! Process the given stanza.
  110. //! The handler must return true if it has handled the stanza.
  111. //! A false return value causes the stanza to be passed on to
  112. //! the next registered handler.
  113. virtual bool HandleStanza(const XmlElement * stanza) = 0;
  114. };
  115. //! Callback to deliver iq responses (results and errors).
  116. //! Register while sending an iq via XmppEngine.SendIq.
  117. //! Iq responses are routed to matching XmppIqHandlers in preference
  118. //! to sending to any registered SessionHandlers.
  119. class XmppIqHandler {
  120. public:
  121. virtual ~XmppIqHandler() {}
  122. //! Called to handle the iq response.
  123. //! The response may be either a result or an error, and will have
  124. //! an 'id' that matches the request and a 'from' that matches the
  125. //! 'to' of the request. Called no more than once; once this is
  126. //! called, the handler is automatically unregistered.
  127. virtual void IqResponse(XmppIqCookie cookie, const XmlElement * pelStanza) = 0;
  128. };
  129. //! The XMPP connection engine.
  130. //! This engine implements the client side of the 'core' XMPP protocol.
  131. //! To use it, register an XmppOutputHandler to handle socket output
  132. //! and pass socket input to HandleInput. Then application code can
  133. //! set up the connection with a user, password, and other settings,
  134. //! and then call Connect() to initiate the connection.
  135. //! An application can listen for events and receive stanzas by
  136. //! registering an XmppStanzaHandler via AddStanzaHandler().
  137. class XmppEngine {
  138. public:
  139. static XmppEngine * Create();
  140. virtual ~XmppEngine() {}
  141. //! Error codes. See GetError().
  142. enum Error {
  143. ERROR_NONE = 0, //!< No error
  144. ERROR_XML, //!< Malformed XML or encoding error
  145. ERROR_STREAM, //!< XMPP stream error - see GetStreamError()
  146. ERROR_VERSION, //!< XMPP version error
  147. ERROR_UNAUTHORIZED, //!< User is not authorized (rejected credentials)
  148. ERROR_TLS, //!< TLS could not be negotiated
  149. ERROR_AUTH, //!< Authentication could not be negotiated
  150. ERROR_BIND, //!< Resource or session binding could not be negotiated
  151. ERROR_CONNECTION_CLOSED,//!< Connection closed by output handler.
  152. ERROR_DOCUMENT_CLOSED, //!< Closed by </stream:stream>
  153. ERROR_SOCKET, //!< Socket error
  154. ERROR_NETWORK_TIMEOUT, //!< Some sort of timeout (eg., we never got the roster)
  155. ERROR_MISSING_USERNAME //!< User has a Google Account but no nickname
  156. };
  157. //! States. See GetState().
  158. enum State {
  159. STATE_NONE = 0, //!< Nonexistent state
  160. STATE_START, //!< Initial state.
  161. STATE_OPENING, //!< Exchanging stream headers, authenticating and so on.
  162. STATE_OPEN, //!< Authenticated and bound.
  163. STATE_CLOSED, //!< Session closed, possibly due to error.
  164. };
  165. // SOCKET INPUT AND OUTPUT ------------------------------------------------
  166. //! Registers the handler for socket output
  167. virtual XmppReturnStatus SetOutputHandler(XmppOutputHandler *pxoh) = 0;
  168. //! Provides socket input to the engine
  169. virtual XmppReturnStatus HandleInput(const char * bytes, size_t len) = 0;
  170. //! Advises the engine that the socket has closed
  171. virtual XmppReturnStatus ConnectionClosed(int subcode) = 0;
  172. // SESSION SETUP ---------------------------------------------------------
  173. //! Indicates the (bare) JID for the user to use.
  174. virtual XmppReturnStatus SetUser(const Jid & jid)= 0;
  175. //! Get the login (bare) JID.
  176. virtual const Jid & GetUser() = 0;
  177. //! Provides different methods for credentials for login.
  178. //! Takes ownership of this object; deletes when login is done
  179. virtual XmppReturnStatus SetSaslHandler(SaslHandler * h) = 0;
  180. //! Sets whether TLS will be used within the connection (default true).
  181. virtual XmppReturnStatus SetTls(TlsOptions useTls) = 0;
  182. //! Sets an alternate domain from which we allows TLS certificates.
  183. //! This is for use in the case where a we want to allow a proxy to
  184. //! serve up its own certificate rather than one owned by the underlying
  185. //! domain.
  186. virtual XmppReturnStatus SetTlsServer(const std::string & proxy_hostname,
  187. const std::string & proxy_domain) = 0;
  188. //! Gets whether TLS will be used within the connection.
  189. virtual TlsOptions GetTls() = 0;
  190. //! Sets the request resource name, if any (optional).
  191. //! Note that the resource name may be overridden by the server; after
  192. //! binding, the actual resource name is available as part of FullJid().
  193. virtual XmppReturnStatus SetRequestedResource(const std::string& resource) = 0;
  194. //! Gets the request resource name.
  195. virtual const std::string & GetRequestedResource() = 0;
  196. //! Sets language
  197. virtual void SetLanguage(const std::string & lang) = 0;
  198. // SESSION MANAGEMENT ---------------------------------------------------
  199. //! Set callback for state changes.
  200. virtual XmppReturnStatus SetSessionHandler(XmppSessionHandler* handler) = 0;
  201. //! Initiates the XMPP connection.
  202. //! After supplying connection settings, call this once to initiate,
  203. //! (optionally) encrypt, authenticate, and bind the connection.
  204. virtual XmppReturnStatus Connect() = 0;
  205. //! The current engine state.
  206. virtual State GetState() = 0;
  207. //! Returns true if the connection is encrypted (under TLS)
  208. virtual bool IsEncrypted() = 0;
  209. //! The error code.
  210. //! Consult this after XmppOutputHandler.OnClose().
  211. virtual Error GetError(int *subcode) = 0;
  212. //! The stream:error stanza, when the error is XmppEngine::ERROR_STREAM.
  213. //! Notice the stanza returned is owned by the XmppEngine and
  214. //! is deleted when the engine is destroyed.
  215. virtual const XmlElement * GetStreamError() = 0;
  216. //! Closes down the connection.
  217. //! Sends CloseConnection to output, and disconnects and registered
  218. //! session handlers. After Disconnect completes, it is guaranteed
  219. //! that no further callbacks will be made.
  220. virtual XmppReturnStatus Disconnect() = 0;
  221. // APPLICATION USE -------------------------------------------------------
  222. enum HandlerLevel {
  223. HL_NONE = 0,
  224. HL_PEEK, //!< Sees messages before all other processing; cannot abort
  225. HL_SINGLE, //!< Watches for a single message, e.g., by id and sender
  226. HL_SENDER, //!< Watches for a type of message from a specific sender
  227. HL_TYPE, //!< Watches a type of message, e.g., all groupchat msgs
  228. HL_ALL, //!< Watches all messages - gets last shot
  229. HL_COUNT, //!< Count of handler levels
  230. };
  231. //! Adds a listener for session events.
  232. //! Stanza delivery is chained to session handlers; the first to
  233. //! return 'true' is the last to get each stanza.
  234. virtual XmppReturnStatus AddStanzaHandler(XmppStanzaHandler* handler, HandlerLevel level = HL_PEEK) = 0;
  235. //! Removes a listener for session events.
  236. virtual XmppReturnStatus RemoveStanzaHandler(XmppStanzaHandler* handler) = 0;
  237. //! Sends a stanza to the server.
  238. virtual XmppReturnStatus SendStanza(const XmlElement * pelStanza) = 0;
  239. //! Sends raw text to the server
  240. virtual XmppReturnStatus SendRaw(const std::string & text) = 0;
  241. //! Sends an iq to the server, and registers a callback for the result.
  242. //! Returns the cookie passed to the result handler.
  243. virtual XmppReturnStatus SendIq(const XmlElement* pelStanza,
  244. XmppIqHandler* iq_handler,
  245. XmppIqCookie* cookie) = 0;
  246. //! Unregisters an iq callback handler given its cookie.
  247. //! No callback will come to this handler after it's unregistered.
  248. virtual XmppReturnStatus RemoveIqHandler(XmppIqCookie cookie,
  249. XmppIqHandler** iq_handler) = 0;
  250. //! Forms and sends an error in response to the given stanza.
  251. //! Swaps to and from, sets type to "error", and adds error information
  252. //! based on the passed code. Text is optional and may be STR_EMPTY.
  253. virtual XmppReturnStatus SendStanzaError(const XmlElement * pelOriginal,
  254. XmppStanzaError code,
  255. const std::string & text) = 0;
  256. //! The fullly bound JID.
  257. //! This JID is only valid after binding has succeeded. If the value
  258. //! is JID_NULL, the binding has not succeeded.
  259. virtual const Jid & FullJid() = 0;
  260. //! The next unused iq id for this connection.
  261. //! Call this when building iq stanzas, to ensure that each iq
  262. //! gets its own unique id.
  263. virtual std::string NextId() = 0;
  264. };
  265. }
  266. // Move these to a better location
  267. #define XMPP_FAILED(x) \
  268. ( (x) == buzz::XMPP_RETURN_OK ? false : true) \
  269. #define XMPP_SUCCEEDED(x) \
  270. ( (x) == buzz::XMPP_RETURN_OK ? true : false) \
  271. #define IFR(x) \
  272. do { \
  273. xmpp_status = (x); \
  274. if (XMPP_FAILED(xmpp_status)) { \
  275. return xmpp_status; \
  276. } \
  277. } while (false) \
  278. #define IFC(x) \
  279. do { \
  280. xmpp_status = (x); \
  281. if (XMPP_FAILED(xmpp_status)) { \
  282. goto Cleanup; \
  283. } \
  284. } while (false) \
  285. #endif