xmpp.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief XMPP Interface
  20. * \author Joshua Colp <jcolp@digium.com>
  21. * IKSEMEL http://iksemel.jabberstudio.org
  22. */
  23. #ifndef _ASTERISK_XMPP_H
  24. #define _ASTERISK_XMPP_H
  25. #ifdef HAVE_OPENSSL
  26. #include <openssl/ssl.h>
  27. #include <openssl/err.h>
  28. #define TRY_SECURE 2
  29. #define SECURE 4
  30. #endif /* HAVE_OPENSSL */
  31. /* file is read by blocks with this size */
  32. #define NET_IO_BUF_SIZE 16384
  33. /* Return value for timeout connection expiration */
  34. #define IKS_NET_EXPIRED 12
  35. #include <iksemel.h>
  36. #include "asterisk/utils.h"
  37. #include "asterisk/astobj2.h"
  38. #include "asterisk/linkedlists.h"
  39. #include "asterisk/stringfields.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/stasis.h"
  42. /*
  43. * As per RFC 3920 - section 3.1, the maximum length for a full Jabber ID
  44. * is 3071 bytes.
  45. * The ABNF syntax for jid :
  46. * jid = [node "@" ] domain [ "/" resource ]
  47. * Each allowable portion of a JID (node identifier, domain identifier,
  48. * and resource identifier) MUST NOT be more than 1023 bytes in length,
  49. * resulting in a maximum total size (including the '@' and '/' separators)
  50. * of 3071 bytes.
  51. */
  52. #define XMPP_MAX_JIDLEN 3071
  53. /*! \brief Maximum size of a resource JID */
  54. #define XMPP_MAX_RESJIDLEN 1023
  55. /*! \brief Maximum size of an attribute */
  56. #define XMPP_MAX_ATTRLEN 256
  57. /*! \brief Client connection states */
  58. enum xmpp_state {
  59. XMPP_STATE_DISCONNECTING, /*!< Client is disconnecting */
  60. XMPP_STATE_DISCONNECTED, /*!< Client is disconnected */
  61. XMPP_STATE_CONNECTING, /*!< Client is connecting */
  62. XMPP_STATE_REQUEST_TLS, /*!< Client should request TLS */
  63. XMPP_STATE_REQUESTED_TLS, /*!< Client has requested TLS */
  64. XMPP_STATE_AUTHENTICATE, /*!< Client needs to authenticate */
  65. XMPP_STATE_AUTHENTICATING, /*!< Client is authenticating */
  66. XMPP_STATE_ROSTER, /*!< Client is currently getting the roster */
  67. XMPP_STATE_CONNECTED, /*!< Client is fully connected */
  68. };
  69. /*! \brief Resource capabilities */
  70. struct ast_xmpp_capabilities {
  71. char node[200]; /*!< Node string from the capabilities stanza in presence notification */
  72. char version[50]; /*!< Version string from the capabilities stanza in presence notification */
  73. unsigned int jingle:1; /*!< Set if the resource supports Jingle */
  74. unsigned int google:1; /*!< Set if the resource supports Google Talk */
  75. };
  76. /*! \brief XMPP Resource */
  77. struct ast_xmpp_resource {
  78. char resource[XMPP_MAX_RESJIDLEN]; /*!< JID of the resource */
  79. int status; /*!< Current status of the resource */
  80. char *description; /*!< Description of the resource */
  81. int priority; /*!< Priority, used for deciding what resource to use */
  82. struct ast_xmpp_capabilities caps; /*!< Capabilities of the resource */
  83. };
  84. /*! \brief XMPP Message */
  85. struct ast_xmpp_message {
  86. char *from; /*!< Who the message is from */
  87. char *message; /*!< Message contents */
  88. char id[25]; /*!< Identifier for the message */
  89. struct timeval arrived; /*!< When the message arrived */
  90. AST_LIST_ENTRY(ast_xmpp_message) list; /*!< Linked list information */
  91. };
  92. struct ast_endpoint;
  93. /*! \brief XMPP Buddy */
  94. struct ast_xmpp_buddy {
  95. char id[XMPP_MAX_JIDLEN]; /*!< JID of the buddy */
  96. struct ao2_container *resources; /*!< Resources for the buddy */
  97. unsigned int subscribe:1; /*!< Need to subscribe to get their status */
  98. };
  99. /*! \brief XMPP Client Connection */
  100. struct ast_xmpp_client {
  101. AST_DECLARE_STRING_FIELDS(
  102. /*! Name of the client configuration */
  103. AST_STRING_FIELD(name);
  104. );
  105. /*! Message ID */
  106. char mid[6];
  107. iksid *jid;
  108. iksparser *parser;
  109. iksfilter *filter;
  110. ikstack *stack;
  111. #ifdef HAVE_OPENSSL
  112. SSL_CTX *ssl_context;
  113. SSL *ssl_session;
  114. const SSL_METHOD *ssl_method;
  115. unsigned int stream_flags;
  116. #endif /* HAVE_OPENSSL */
  117. enum xmpp_state state;
  118. struct ao2_container *buddies;
  119. AST_LIST_HEAD(, ast_xmpp_message) messages;
  120. pthread_t thread;
  121. int timeout;
  122. /*! Reconnect this client */
  123. unsigned int reconnect:1;
  124. /*! If distributing event information the MWI subscription */
  125. struct stasis_subscription *mwi_sub;
  126. /*! If distributing event information the device state subscription */
  127. struct stasis_subscription *device_state_sub;
  128. /*! The endpoint associated with this client */
  129. struct ast_endpoint *endpoint;
  130. };
  131. /*!
  132. * \brief Find an XMPP client connection using a given name
  133. *
  134. * \param name Name of the client connection
  135. *
  136. * \retval non-NULL on success
  137. * \retval NULL on failure
  138. *
  139. * \note This will return the client connection with the reference count incremented by one.
  140. */
  141. struct ast_xmpp_client *ast_xmpp_client_find(const char *name);
  142. /*!
  143. * \brief Disconnect an XMPP client connection
  144. *
  145. * \param client Pointer to the client
  146. *
  147. * \retval 0 on success
  148. * \retval -1 on failure
  149. */
  150. int ast_xmpp_client_disconnect(struct ast_xmpp_client *client);
  151. /*!
  152. * \brief Release XMPP client connection reference
  153. *
  154. * \param client Pointer to the client
  155. */
  156. void ast_xmpp_client_unref(struct ast_xmpp_client *client);
  157. /*!
  158. * \brief Lock an XMPP client connection
  159. *
  160. * \param client Pointer to the client
  161. */
  162. void ast_xmpp_client_lock(struct ast_xmpp_client *client);
  163. /*!
  164. * \brief Unlock an XMPP client connection
  165. *
  166. * \param client Pointer to the client
  167. */
  168. void ast_xmpp_client_unlock(struct ast_xmpp_client *client);
  169. /*!
  170. * \brief Send an XML stanza out using an established XMPP client connection
  171. *
  172. * \param client Pointer to the client
  173. * \param stanza Pointer to the Iksemel stanza
  174. *
  175. * \retval 0 on success
  176. * \retval -1 on failure
  177. */
  178. int ast_xmpp_client_send(struct ast_xmpp_client *client, iks *stanza);
  179. /*!
  180. * \brief Send a message to a given user using an established XMPP client connection
  181. *
  182. * \param client Pointer to the client
  183. * \param user User the message should be sent to
  184. * \param message The message to send
  185. *
  186. * \retval 0 on success
  187. * \retval -1 on failure
  188. */
  189. int ast_xmpp_client_send_message(struct ast_xmpp_client *client, const char *user, const char *message);
  190. /*!
  191. * \brief Invite a user to an XMPP multi-user chatroom
  192. *
  193. * \param client Pointer to the client
  194. * \param user JID of the user
  195. * \param room Name of the chatroom
  196. * \param message Message to send with the invitation
  197. *
  198. * \retval 0 on success
  199. * \retval -1 on failure
  200. */
  201. int ast_xmpp_chatroom_invite(struct ast_xmpp_client *client, const char *user, const char *room, const char *message);
  202. /*!
  203. * \brief Join an XMPP multi-user chatroom
  204. *
  205. * \param client Pointer to the client
  206. * \param room Name of the chatroom
  207. * \param nickname Nickname to use
  208. *
  209. * \retval 0 on success
  210. * \retval -1 on failure
  211. */
  212. int ast_xmpp_chatroom_join(struct ast_xmpp_client *client, const char *room, const char *nickname);
  213. /*!
  214. * \brief Send a message to an XMPP multi-user chatroom
  215. *
  216. * \param client Pointer to the client
  217. * \param nickname Nickname to use
  218. * \param address Address of the room
  219. * \param message Message itself
  220. *
  221. * \retval 0 on success
  222. * \retval -1 on failure
  223. */
  224. int ast_xmpp_chatroom_send(struct ast_xmpp_client *client, const char *nickname, const char *address, const char *message);
  225. /*!
  226. * \brief Leave an XMPP multi-user chatroom
  227. *
  228. * \param client Pointer to the client
  229. * \param room Name of the chatroom
  230. * \param nickname Nickname being used
  231. *
  232. * \retval 0 on success
  233. * \retval -1 on failure
  234. */
  235. int ast_xmpp_chatroom_leave(struct ast_xmpp_client *client, const char *room, const char *nickname);
  236. /*!
  237. * \brief Helper function which increments the message identifier
  238. *
  239. * \param mid Pointer to a string containing the message identifier
  240. */
  241. void ast_xmpp_increment_mid(char *mid);
  242. #endif