2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
21 * \brief XMPP client and component module.
23 * \author Joshua Colp <jcolp@digium.com>
25 * Iksemel http://code.google.com/p/iksemel/
27 * A reference module for interfacting Asterisk directly as a client or component with
28 * an XMPP/Jabber compliant server.
30 * This module is based upon the original res_jabber as done by Matt O'Gorman.
35 * \li The resource res_xmpp uses the configuration file \ref xmpp.conf
36 * \addtogroup configuration_file Configuration Files
40 * \page xmpp.conf xmpp.conf
41 * \verbinclude xmpp.conf.sample
45 <depend>iksemel</depend>
46 <use type="external">openssl</use>
47 <support_level>core</support_level>
52 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
57 #include "asterisk/xmpp.h"
58 #include "asterisk/module.h"
59 #include "asterisk/manager.h"
60 #include "asterisk/app.h"
61 #include "asterisk/message.h"
62 #include "asterisk/manager.h"
63 #include "asterisk/event.h"
64 #include "asterisk/cli.h"
65 #include "asterisk/config_options.h"
68 <application name="JabberSend" language="en_US">
70 Sends an XMPP message to a buddy.
73 <parameter name="account" required="true">
74 <para>The local named account to listen on (specified in
77 <parameter name="jid" required="true">
78 <para>Jabber ID of the buddy to send the message to. It can be a
79 bare JID (username@domain) or a full JID (username@domain/resource).</para>
81 <parameter name="message" required="true">
82 <para>The message to send.</para>
86 <para>Sends the content of <replaceable>message</replaceable> as text message
87 from the given <replaceable>account</replaceable> to the buddy identified by
88 <replaceable>jid</replaceable></para>
89 <para>Example: JabberSend(asterisk,bob@domain.com,Hello world) sends "Hello world"
90 to <replaceable>bob@domain.com</replaceable> as an XMPP message from the account
91 <replaceable>asterisk</replaceable>, configured in xmpp.conf.</para>
94 <ref type="function">JABBER_STATUS</ref>
95 <ref type="function">JABBER_RECEIVE</ref>
98 <function name="JABBER_RECEIVE" language="en_US">
103 <parameter name="account" required="true">
104 <para>The local named account to listen on (specified in
107 <parameter name="jid" required="true">
108 <para>Jabber ID of the buddy to receive message from. It can be a
109 bare JID (username@domain) or a full JID (username@domain/resource).</para>
111 <parameter name="timeout">
112 <para>In seconds, defaults to <literal>20</literal>.</para>
116 <para>Receives a text message on the given <replaceable>account</replaceable>
117 from the buddy identified by <replaceable>jid</replaceable> and returns the contents.</para>
118 <para>Example: ${JABBER_RECEIVE(asterisk,bob@domain.com)} returns an XMPP message
119 sent from <replaceable>bob@domain.com</replaceable> (or nothing in case of a time out), to
120 the <replaceable>asterisk</replaceable> XMPP account configured in xmpp.conf.</para>
123 <ref type="function">JABBER_STATUS</ref>
124 <ref type="application">JabberSend</ref>
127 <function name="JABBER_STATUS" language="en_US">
129 Retrieves a buddy's status.
132 <parameter name="account" required="true">
133 <para>The local named account to listen on (specified in
136 <parameter name="jid" required="true">
137 <para>Jabber ID of the buddy to receive message from. It can be a
138 bare JID (username@domain) or a full JID (username@domain/resource).</para>
142 <para>Retrieves the numeric status associated with the buddy identified
143 by <replaceable>jid</replaceable>.
144 If the buddy does not exist in the buddylist, returns 7.</para>
145 <para>Status will be 1-7.</para>
146 <para>1=Online, 2=Chatty, 3=Away, 4=XAway, 5=DND, 6=Offline</para>
147 <para>If not in roster variable will be set to 7.</para>
148 <para>Example: ${JABBER_STATUS(asterisk,bob@domain.com)} returns 1 if
149 <replaceable>bob@domain.com</replaceable> is online. <replaceable>asterisk</replaceable> is
150 the associated XMPP account configured in xmpp.conf.</para>
153 <ref type="function">JABBER_RECEIVE</ref>
154 <ref type="application">JabberSend</ref>
157 <application name="JabberSendGroup" language="en_US">
159 Send a Jabber Message to a specified chat room
162 <parameter name="Jabber" required="true">
163 <para>Client or transport Asterisk uses to connect to Jabber.</para>
165 <parameter name="RoomJID" required="true">
166 <para>XMPP/Jabber JID (Name) of chat room.</para>
168 <parameter name="Message" required="true">
169 <para>Message to be sent to the chat room.</para>
171 <parameter name="Nickname" required="false">
172 <para>The nickname Asterisk uses in the chat room.</para>
176 <para>Allows user to send a message to a chat room via XMPP.</para>
177 <note><para>To be able to send messages to a chat room, a user must have previously joined it. Use the <replaceable>JabberJoin</replaceable> function to do so.</para></note>
180 <application name="JabberJoin" language="en_US">
185 <parameter name="Jabber" required="true">
186 <para>Client or transport Asterisk uses to connect to Jabber.</para>
188 <parameter name="RoomJID" required="true">
189 <para>XMPP/Jabber JID (Name) of chat room.</para>
191 <parameter name="Nickname" required="false">
192 <para>The nickname Asterisk will use in the chat room.</para>
193 <note><para>If a different nickname is supplied to an already joined room, the old nick will be changed to the new one.</para></note>
197 <para>Allows Asterisk to join a chat room.</para>
200 <application name="JabberLeave" language="en_US">
205 <parameter name="Jabber" required="true">
206 <para>Client or transport Asterisk uses to connect to Jabber.</para>
208 <parameter name="RoomJID" required="true">
209 <para>XMPP/Jabber JID (Name) of chat room.</para>
211 <parameter name="Nickname" required="false">
212 <para>The nickname Asterisk uses in the chat room.</para>
216 <para>Allows Asterisk to leave a chat room.</para>
219 <application name="JabberStatus" language="en_US">
221 Retrieve the status of a jabber list member
224 <parameter name="Jabber" required="true">
225 <para>Client or transport Asterisk users to connect to Jabber.</para>
227 <parameter name="JID" required="true">
228 <para>XMPP/Jabber JID (Name) of recipient.</para>
230 <parameter name="Variable" required="true">
231 <para>Variable to store the status of requested user.</para>
235 <para>This application is deprecated. Please use the JABBER_STATUS() function instead.</para>
236 <para>Retrieves the numeric status associated with the specified buddy <replaceable>JID</replaceable>.
237 The return value in the <replaceable>Variable</replaceable>will be one of the following.</para>
249 <para>Extended Away.</para>
252 <para>Do Not Disturb.</para>
255 <para>Offline.</para>
258 <para>Not In Roster.</para>
263 <manager name="JabberSend" language="en_US">
265 Sends a message to a Jabber Client.
268 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
269 <parameter name="Jabber" required="true">
270 <para>Client or transport Asterisk uses to connect to JABBER.</para>
272 <parameter name="JID" required="true">
273 <para>XMPP/Jabber JID (Name) of recipient.</para>
275 <parameter name="Message" required="true">
276 <para>Message to be sent to the buddy.</para>
280 <para>Sends a message to a Jabber Client.</para>
283 <info name="XMPPMessageToInfo" language="en_US" tech="XMPP">
284 <para>Specifying a prefix of <literal>xmpp:</literal> will send the
285 message as an XMPP chat message.</para>
287 <info name="XMPPMessageFromInfo" language="en_US" tech="XMPP">
288 <para>Specifying a prefix of <literal>xmpp:</literal> will specify the
289 account defined in <literal>xmpp.conf</literal> to send the message from.
290 Note that this field is required for XMPP messages.</para>
294 /*! \brief Supported general configuration flags */
296 XMPP_AUTOPRUNE = (1 << 0),
297 XMPP_AUTOREGISTER = (1 << 1),
298 XMPP_AUTOACCEPT = (1 << 2),
299 XMPP_DEBUG = (1 << 3),
300 XMPP_USETLS = (1 << 4),
301 XMPP_USESASL = (1 << 5),
302 XMPP_FORCESSL = (1 << 6),
303 XMPP_KEEPALIVE = (1 << 7),
304 XMPP_COMPONENT = (1 << 8),
305 XMPP_SEND_TO_DIALPLAN = (1 << 9),
306 XMPP_DISTRIBUTE_EVENTS = (1 << 10),
309 /*! \brief Supported pubsub configuration flags */
311 XMPP_XEP0248 = (1 << 0),
312 XMPP_PUBSUB = (1 << 1),
313 XMPP_PUBSUB_AUTOCREATE = (1 << 2),
316 /*! \brief Number of buckets for client connections */
317 #define CLIENT_BUCKETS 53
319 /*! \brief Number of buckets for buddies (per client) */
320 #define BUDDY_BUCKETS 53
322 /*! \brief Number of buckets for resources (per buddy) */
323 #define RESOURCE_BUCKETS 53
325 /*! \brief Namespace for TLS support */
326 #define XMPP_TLS_NS "urn:ietf:params:xml:ns:xmpp-tls"
328 /*! \brief Status for a disappearing buddy */
329 #define STATUS_DISAPPEAR 6
331 /*! \brief Global debug status */
334 /*! \brief XMPP Global Configuration */
335 struct ast_xmpp_global_config {
336 struct ast_flags general; /*!< General configuration options */
337 struct ast_flags pubsub; /*!< Pubsub related configuration options */
340 /*! \brief XMPP Client Configuration */
341 struct ast_xmpp_client_config {
342 AST_DECLARE_STRING_FIELDS(
343 AST_STRING_FIELD(name); /*!< Name of the client connection */
344 AST_STRING_FIELD(user); /*!< Username to use for authentication */
345 AST_STRING_FIELD(password); /*!< Password to use for authentication */
346 AST_STRING_FIELD(server); /*!< Server hostname */
347 AST_STRING_FIELD(statusmsg); /*!< Status message for presence */
348 AST_STRING_FIELD(pubsubnode); /*!< Pubsub node */
349 AST_STRING_FIELD(context); /*!< Context for incoming messages */
351 int port; /*!< Port to use when connecting to server */
352 int message_timeout; /*!< Timeout for messages */
353 int priority; /*!< Resource priority */
354 struct ast_flags flags; /*!< Various options that have been set */
355 enum ikshowtype status; /*!< Presence status */
356 struct ast_xmpp_client *client; /*!< Pointer to the client */
357 struct ao2_container *buddies; /*!< Configured buddies */
361 struct ast_xmpp_global_config *global; /*!< Global configuration options */
362 struct ao2_container *clients; /*!< Configured clients */
365 static AO2_GLOBAL_OBJ_STATIC(globals);
367 static int xmpp_client_request_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
368 static int xmpp_client_requested_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
369 static int xmpp_client_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
370 static int xmpp_client_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
372 static int xmpp_component_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
373 static int xmpp_component_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
375 /*! \brief Defined handlers for XMPP client states */
376 static const struct xmpp_state_handler {
379 int (*handler)(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
380 } xmpp_state_handlers[] = {
381 { XMPP_STATE_REQUEST_TLS, 0, xmpp_client_request_tls, },
382 { XMPP_STATE_REQUESTED_TLS, 0, xmpp_client_requested_tls, },
383 { XMPP_STATE_AUTHENTICATE, 0, xmpp_client_authenticate, },
384 { XMPP_STATE_AUTHENTICATING, 0, xmpp_client_authenticating, },
385 { XMPP_STATE_AUTHENTICATE, 1, xmpp_component_authenticate, },
386 { XMPP_STATE_AUTHENTICATING, 1, xmpp_component_authenticating, },
389 static int xmpp_pak_message(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
390 static int xmpp_pak_presence(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
391 static int xmpp_pak_s10n(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
393 /*! \brief Defined handlers for different PAK types */
394 static const struct xmpp_pak_handler {
396 int (*handler)(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
397 } xmpp_pak_handlers[] = {
398 { IKS_PAK_MESSAGE, xmpp_pak_message, },
399 { IKS_PAK_PRESENCE, xmpp_pak_presence, },
400 { IKS_PAK_S10N, xmpp_pak_s10n, },
403 static const char *app_ajisend = "JabberSend";
404 static const char *app_ajisendgroup = "JabberSendGroup";
405 static const char *app_ajistatus = "JabberStatus";
406 static const char *app_ajijoin = "JabberJoin";
407 static const char *app_ajileave = "JabberLeave";
409 static ast_cond_t message_received_condition;
410 static ast_mutex_t messagelock;
412 static int xmpp_client_config_post_apply(void *obj, void *arg, int flags);
414 /*! \brief Destructor function for configuration */
415 static void ast_xmpp_client_config_destructor(void *obj)
417 struct ast_xmpp_client_config *cfg = obj;
418 ast_string_field_free_memory(cfg);
419 ao2_cleanup(cfg->client);
420 ao2_cleanup(cfg->buddies);
423 /*! \brief Destroy function for XMPP messages */
424 static void xmpp_message_destroy(struct ast_xmpp_message *message)
427 ast_free(message->from);
429 if (message->message) {
430 ast_free(message->message);
436 /*! \brief Destructor callback function for XMPP client */
437 static void xmpp_client_destructor(void *obj)
439 struct ast_xmpp_client *client = obj;
440 struct ast_xmpp_message *message;
442 ast_xmpp_client_disconnect(client);
445 iks_stack_delete(client->stack);
448 ao2_cleanup(client->buddies);
450 while ((message = AST_LIST_REMOVE_HEAD(&client->messages, list))) {
451 xmpp_message_destroy(message);
453 AST_LIST_HEAD_DESTROY(&client->messages);
456 /*! \brief Hashing function for XMPP buddy */
457 static int xmpp_buddy_hash(const void *obj, const int flags)
459 const struct ast_xmpp_buddy *buddy = obj;
460 const char *id = obj;
462 return ast_str_hash(flags & OBJ_KEY ? id : buddy->id);
465 /*! \brief Comparator function for XMPP buddy */
466 static int xmpp_buddy_cmp(void *obj, void *arg, int flags)
468 struct ast_xmpp_buddy *buddy1 = obj, *buddy2 = arg;
469 const char *id = arg;
471 return !strcmp(buddy1->id, flags & OBJ_KEY ? id : buddy2->id) ? CMP_MATCH | CMP_STOP : 0;
474 /*! \brief Allocator function for ast_xmpp_client */
475 static struct ast_xmpp_client *xmpp_client_alloc(const char *name)
477 struct ast_xmpp_client *client;
479 if (!(client = ao2_alloc(sizeof(*client), xmpp_client_destructor))) {
483 AST_LIST_HEAD_INIT(&client->messages);
484 client->thread = AST_PTHREADT_NULL;
486 if (!(client->buddies = ao2_container_alloc(BUDDY_BUCKETS, xmpp_buddy_hash, xmpp_buddy_cmp))) {
487 ast_log(LOG_ERROR, "Could not initialize buddy container for '%s'\n", name);
492 if (ast_string_field_init(client, 512)) {
493 ast_log(LOG_ERROR, "Could not initialize stringfields for '%s'\n", name);
498 if (!(client->stack = iks_stack_new(8192, 8192))) {
499 ast_log(LOG_ERROR, "Could not create an Iksemel stack for '%s'\n", name);
504 ast_string_field_set(client, name, name);
506 client->timeout = 50;
507 client->state = XMPP_STATE_DISCONNECTED;
508 ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
513 /*! \brief Find function for configuration */
514 static void *xmpp_config_find(struct ao2_container *tmp_container, const char *category)
516 return ao2_find(tmp_container, category, OBJ_KEY);
519 /*! \brief Look up existing client or create a new one */
520 static void *xmpp_client_find_or_create(const char *category)
522 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
523 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
525 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, category))) {
526 return xmpp_client_alloc(category);
529 ao2_ref(clientcfg->client, +1);
530 return clientcfg->client;
533 /*! \brief Allocator function for configuration */
534 static void *ast_xmpp_client_config_alloc(const char *cat)
536 struct ast_xmpp_client_config *cfg;
538 if (!(cfg = ao2_alloc(sizeof(*cfg), ast_xmpp_client_config_destructor))) {
542 if (ast_string_field_init(cfg, 512)) {
547 if (!(cfg->client = xmpp_client_find_or_create(cat))) {
552 if (!(cfg->buddies = ao2_container_alloc(BUDDY_BUCKETS, xmpp_buddy_hash, xmpp_buddy_cmp))) {
557 ast_string_field_set(cfg, name, cat);
562 /*! \brief Destructor for XMPP configuration */
563 static void xmpp_config_destructor(void *obj)
565 struct xmpp_config *cfg = obj;
566 ao2_cleanup(cfg->global);
567 ao2_cleanup(cfg->clients);
570 /*! \brief Hashing function for configuration */
571 static int xmpp_config_hash(const void *obj, const int flags)
573 const struct ast_xmpp_client_config *cfg = obj;
574 const char *name = (flags & OBJ_KEY) ? obj : cfg->name;
575 return ast_str_case_hash(name);
578 /*! \brief Comparator function for configuration */
579 static int xmpp_config_cmp(void *obj, void *arg, int flags)
581 struct ast_xmpp_client_config *one = obj, *two = arg;
582 const char *match = (flags & OBJ_KEY) ? arg : two->name;
583 return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
586 /*! \brief Allocator for XMPP configuration */
587 static void *xmpp_config_alloc(void)
589 struct xmpp_config *cfg;
591 if (!(cfg = ao2_alloc(sizeof(*cfg), xmpp_config_destructor))) {
595 if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) {
599 ast_set_flag(&cfg->global->general, XMPP_AUTOREGISTER | XMPP_AUTOACCEPT | XMPP_USETLS | XMPP_USESASL | XMPP_KEEPALIVE);
601 if (!(cfg->clients = ao2_container_alloc(1, xmpp_config_hash, xmpp_config_cmp))) {
611 static int xmpp_config_prelink(void *newitem)
613 struct ast_xmpp_client_config *clientcfg = newitem;
614 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
615 RAII_VAR(struct ast_xmpp_client_config *, oldclientcfg, NULL, ao2_cleanup);
617 if (ast_strlen_zero(clientcfg->user)) {
618 ast_log(LOG_ERROR, "No user specified on client '%s'\n", clientcfg->name);
620 } else if (ast_strlen_zero(clientcfg->password)) {
621 ast_log(LOG_ERROR, "No password specified on client '%s'\n", clientcfg->name);
623 } else if (ast_strlen_zero(clientcfg->server)) {
624 ast_log(LOG_ERROR, "No server specified on client '%s'\n", clientcfg->name);
628 /* If this is a new connection force a reconnect */
629 if (!cfg || !cfg->clients || !(oldclientcfg = xmpp_config_find(cfg->clients, clientcfg->name))) {
630 clientcfg->client->reconnect = 1;
634 /* If any configuration options are changing that would require reconnecting set the bit so we will do so if possible */
635 if (strcmp(clientcfg->user, oldclientcfg->user) ||
636 strcmp(clientcfg->password, oldclientcfg->password) ||
637 strcmp(clientcfg->server, oldclientcfg->server) ||
638 (clientcfg->port != oldclientcfg->port) ||
639 (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) != ast_test_flag(&oldclientcfg->flags, XMPP_COMPONENT)) ||
640 (clientcfg->priority != oldclientcfg->priority)) {
641 clientcfg->client->reconnect = 1;
643 clientcfg->client->reconnect = 0;
649 static void xmpp_config_post_apply(void)
651 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
653 ao2_callback(cfg->clients, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_config_post_apply, NULL);
656 static struct aco_type global_option = {
658 .item_offset = offsetof(struct xmpp_config, global),
659 .category_match = ACO_WHITELIST,
660 .category = "^general$",
663 struct aco_type *global_options[] = ACO_TYPES(&global_option);
665 static struct aco_type client_option = {
667 .category_match = ACO_BLACKLIST,
668 .category = "^(general)$",
669 .item_alloc = ast_xmpp_client_config_alloc,
670 .item_find = xmpp_config_find,
671 .item_prelink = xmpp_config_prelink,
672 .item_offset = offsetof(struct xmpp_config, clients),
675 struct aco_type *client_options[] = ACO_TYPES(&client_option);
677 struct aco_file res_xmpp_conf = {
678 .filename = "xmpp.conf",
679 .alias = "jabber.conf",
680 .types = ACO_TYPES(&global_option, &client_option),
683 CONFIG_INFO_STANDARD(cfg_info, globals, xmpp_config_alloc,
684 .files = ACO_FILES(&res_xmpp_conf),
685 .post_apply_config = xmpp_config_post_apply,
688 /*! \brief Destructor callback function for XMPP resource */
689 static void xmpp_resource_destructor(void *obj)
691 struct ast_xmpp_resource *resource = obj;
693 if (resource->description) {
694 ast_free(resource->description);
698 /*! \brief Hashing function for XMPP resource */
699 static int xmpp_resource_hash(const void *obj, const int flags)
701 const struct ast_xmpp_resource *resource = obj;
703 return flags & OBJ_KEY ? -1 : resource->priority;
706 /*! \brief Comparator function for XMPP resource */
707 static int xmpp_resource_cmp(void *obj, void *arg, int flags)
709 struct ast_xmpp_resource *resource1 = obj, *resource2 = arg;
710 const char *resource = arg;
712 return !strcmp(resource1->resource, flags & OBJ_KEY ? resource : resource2->resource) ? CMP_MATCH | CMP_STOP : 0;
715 /*! \brief Destructor callback function for XMPP buddy */
716 static void xmpp_buddy_destructor(void *obj)
718 struct ast_xmpp_buddy *buddy = obj;
720 if (buddy->resources) {
721 ao2_ref(buddy->resources, -1);
725 /*! \brief Helper function which returns whether an XMPP client connection is secure or not */
726 static int xmpp_is_secure(struct ast_xmpp_client *client)
729 return client->stream_flags & SECURE;
735 struct ast_xmpp_client *ast_xmpp_client_find(const char *name)
737 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
738 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
740 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
744 ao2_ref(clientcfg->client, +1);
745 return clientcfg->client;
748 void ast_xmpp_client_unref(struct ast_xmpp_client *client)
753 void ast_xmpp_client_lock(struct ast_xmpp_client *client)
758 void ast_xmpp_client_unlock(struct ast_xmpp_client *client)
763 /*! \brief Internal function used to send a message to a user or chatroom */
764 static int xmpp_client_send_message(struct ast_xmpp_client *client, int group, const char *nick, const char *address, const char *message)
766 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
767 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
769 char from[XMPP_MAX_JIDLEN];
772 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
773 !(message_packet = iks_make_msg(group ? IKS_TYPE_GROUPCHAT : IKS_TYPE_CHAT, address, message))) {
777 if (!ast_strlen_zero(nick) && ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
778 snprintf(from, sizeof(from), "%s@%s/%s", nick, client->jid->full, nick);
780 snprintf(from, sizeof(from), "%s", client->jid->full);
783 iks_insert_attrib(message_packet, "from", from);
785 res = ast_xmpp_client_send(client, message_packet);
787 iks_delete(message_packet);
792 int ast_xmpp_client_send_message(struct ast_xmpp_client *client, const char *user, const char *message)
794 return xmpp_client_send_message(client, 0, NULL, user, message);
797 int ast_xmpp_chatroom_invite(struct ast_xmpp_client *client, const char *user, const char *room, const char *message)
800 iks *invite, *body = NULL, *namespace = NULL;
802 if (!(invite = iks_new("message")) || !(body = iks_new("body")) || !(namespace = iks_new("x"))) {
807 iks_insert_attrib(invite, "to", user);
808 ast_xmpp_client_lock(client);
809 iks_insert_attrib(invite, "id", client->mid);
810 ast_xmpp_increment_mid(client->mid);
811 ast_xmpp_client_unlock(client);
812 iks_insert_cdata(body, message, 0);
813 iks_insert_node(invite, body);
814 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
815 iks_insert_attrib(namespace, "jid", room);
816 iks_insert_node(invite, namespace);
818 res = ast_xmpp_client_send(client, invite);
821 iks_delete(namespace);
828 static int xmpp_client_set_group_presence(struct ast_xmpp_client *client, const char *room, int level, const char *nick)
830 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
831 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
833 iks *presence = NULL, *x = NULL;
834 char from[XMPP_MAX_JIDLEN], roomid[XMPP_MAX_JIDLEN];
836 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
837 !(presence = iks_make_pres(level, NULL)) || !(x = iks_new("x"))) {
842 if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
843 snprintf(from, sizeof(from), "%s@%s/%s", nick, client->jid->full, nick);
844 snprintf(roomid, sizeof(roomid), "%s/%s", room, nick);
846 snprintf(from, sizeof(from), "%s", client->jid->full);
847 snprintf(roomid, sizeof(roomid), "%s/%s", room, S_OR(nick, client->jid->user));
850 iks_insert_attrib(presence, "to", roomid);
851 iks_insert_attrib(presence, "from", from);
852 iks_insert_attrib(x, "xmlns", "http://jabber.org/protocol/muc");
853 iks_insert_node(presence, x);
855 res = ast_xmpp_client_send(client, presence);
859 iks_delete(presence);
864 int ast_xmpp_chatroom_join(struct ast_xmpp_client *client, const char *room, const char *nickname)
866 return xmpp_client_set_group_presence(client, room, IKS_SHOW_AVAILABLE, nickname);
869 int ast_xmpp_chatroom_send(struct ast_xmpp_client *client, const char *nickname, const char *address, const char *message)
871 return xmpp_client_send_message(client, 1, nickname, address, message);
874 int ast_xmpp_chatroom_leave(struct ast_xmpp_client *client, const char *room, const char *nickname)
876 return xmpp_client_set_group_presence(client, room, IKS_SHOW_UNAVAILABLE, nickname);
879 void ast_xmpp_increment_mid(char *mid)
883 for (i = strlen(mid) - 1; i >= 0; i--) {
894 * \brief Create an IQ packet
895 * \param client the configured XMPP client we use to connect to a XMPP server
896 * \param type the type of IQ packet to create
899 static iks* xmpp_pubsub_iq_create(struct ast_xmpp_client *client, const char *type)
901 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
902 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
905 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
906 !(request = iks_new("iq"))) {
910 if (!ast_strlen_zero(clientcfg->pubsubnode)) {
911 iks_insert_attrib(request, "to", clientcfg->pubsubnode);
914 iks_insert_attrib(request, "from", client->jid->full);
915 iks_insert_attrib(request, "type", type);
916 ast_xmpp_client_lock(client);
917 ast_xmpp_increment_mid(client->mid);
918 iks_insert_attrib(request, "id", client->mid);
919 ast_xmpp_client_unlock(client);
925 * \brief Build the skeleton of a publish
926 * \param client the configured XMPP client we use to connect to a XMPP server
927 * \param node Name of the node that will be published to
931 static iks* xmpp_pubsub_build_publish_skeleton(struct ast_xmpp_client *client, const char *node,
932 const char *event_type)
934 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
935 iks *request, *pubsub, *publish, *item;
937 if (!cfg || !cfg->global || !(request = xmpp_pubsub_iq_create(client, "set"))) {
941 pubsub = iks_insert(request, "pubsub");
942 iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
943 publish = iks_insert(pubsub, "publish");
944 iks_insert_attrib(publish, "node", ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248) ? node : event_type);
945 item = iks_insert(publish, "item");
946 iks_insert_attrib(item, "id", node);
952 static iks* xmpp_pubsub_build_node_config(iks *pubsub, const char *node_type, const char *collection_name)
954 iks *configure, *x, *field_owner, *field_node_type, *field_node_config,
955 *field_deliver_payload, *field_persist_items, *field_access_model,
956 *field_pubsub_collection;
957 configure = iks_insert(pubsub, "configure");
958 x = iks_insert(configure, "x");
959 iks_insert_attrib(x, "xmlns", "jabber:x:data");
960 iks_insert_attrib(x, "type", "submit");
961 field_owner = iks_insert(x, "field");
962 iks_insert_attrib(field_owner, "var", "FORM_TYPE");
963 iks_insert_attrib(field_owner, "type", "hidden");
964 iks_insert_cdata(iks_insert(field_owner, "value"),
965 "http://jabber.org/protocol/pubsub#owner", 39);
967 field_node_type = iks_insert(x, "field");
968 iks_insert_attrib(field_node_type, "var", "pubsub#node_type");
969 iks_insert_cdata(iks_insert(field_node_type, "value"), node_type, strlen(node_type));
971 field_node_config = iks_insert(x, "field");
972 iks_insert_attrib(field_node_config, "var", "FORM_TYPE");
973 iks_insert_attrib(field_node_config, "type", "hidden");
974 iks_insert_cdata(iks_insert(field_node_config, "value"),
975 "http://jabber.org/protocol/pubsub#node_config", 45);
976 field_deliver_payload = iks_insert(x, "field");
977 iks_insert_attrib(field_deliver_payload, "var", "pubsub#deliver_payloads");
978 iks_insert_cdata(iks_insert(field_deliver_payload, "value"), "1", 1);
979 field_persist_items = iks_insert(x, "field");
980 iks_insert_attrib(field_persist_items, "var", "pubsub#persist_items");
981 iks_insert_cdata(iks_insert(field_persist_items, "value"), "1", 1);
982 field_access_model = iks_insert(x, "field");
983 iks_insert_attrib(field_access_model, "var", "pubsub#access_model");
984 iks_insert_cdata(iks_insert(field_access_model, "value"), "whitelist", 9);
985 if (node_type && !strcasecmp(node_type, "leaf")) {
986 field_pubsub_collection = iks_insert(x, "field");
987 iks_insert_attrib(field_pubsub_collection, "var", "pubsub#collection");
988 iks_insert_cdata(iks_insert(field_pubsub_collection, "value"), collection_name,
989 strlen(collection_name));
995 * \brief Add Owner affiliations for pubsub node
996 * \param client the configured XMPP client we use to connect to a XMPP server
997 * \param node the name of the node to which to add affiliations
1000 static void xmpp_pubsub_create_affiliations(struct ast_xmpp_client *client, const char *node)
1002 iks *modify_affiliates = xmpp_pubsub_iq_create(client, "set");
1003 iks *pubsub, *affiliations, *affiliate;
1004 struct ao2_iterator i;
1005 struct ast_xmpp_buddy *buddy;
1007 if (!modify_affiliates) {
1008 ast_log(LOG_ERROR, "Could not create IQ for creating affiliations on client '%s'\n", client->name);
1012 pubsub = iks_insert(modify_affiliates, "pubsub");
1013 iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub#owner");
1014 affiliations = iks_insert(pubsub, "affiliations");
1015 iks_insert_attrib(affiliations, "node", node);
1017 i = ao2_iterator_init(client->buddies, 0);
1018 while ((buddy = ao2_iterator_next(&i))) {
1019 affiliate = iks_insert(affiliations, "affiliation");
1020 iks_insert_attrib(affiliate, "jid", buddy->id);
1021 iks_insert_attrib(affiliate, "affiliation", "owner");
1024 ao2_iterator_destroy(&i);
1026 ast_xmpp_client_send(client, modify_affiliates);
1027 iks_delete(modify_affiliates);
1031 * \brief Create a pubsub node
1032 * \param client the configured XMPP client we use to connect to a XMPP server
1033 * \param node_type the type of node to create
1034 * \param name the name of the node to create
1035 * \param collection_name
1038 static void xmpp_pubsub_create_node(struct ast_xmpp_client *client, const char *node_type, const
1039 char *name, const char *collection_name)
1041 iks *node, *pubsub, *create;
1043 if (!(node = xmpp_pubsub_iq_create(client, "set"))) {
1047 pubsub = iks_insert(node, "pubsub");
1048 iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
1049 create = iks_insert(pubsub, "create");
1050 iks_insert_attrib(create, "node", name);
1051 xmpp_pubsub_build_node_config(pubsub, node_type, collection_name);
1052 ast_xmpp_client_send(client, node);
1053 xmpp_pubsub_create_affiliations(client, name);
1058 * \brief Delete a PubSub node
1059 * \param client the configured XMPP client we use to connect to a XMPP server
1060 * \param node_name the name of the node to delete
1063 static void xmpp_pubsub_delete_node(struct ast_xmpp_client *client, const char *node_name)
1065 iks *request, *pubsub, *delete;
1067 if (!(request = xmpp_pubsub_iq_create(client, "set"))) {
1071 pubsub = iks_insert(request, "pubsub");
1072 iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub#owner");
1073 delete = iks_insert(pubsub, "delete");
1074 iks_insert_attrib(delete, "node", node_name);
1075 ast_xmpp_client_send(client, request);
1079 iks_delete(request);
1083 * \brief Create a PubSub collection node.
1084 * \param client the configured XMPP client we use to connect to a XMPP server
1085 * \param collection_name The name to use for this collection
1088 static void xmpp_pubsub_create_collection(struct ast_xmpp_client *client, const char *collection_name)
1090 xmpp_pubsub_create_node(client, "collection", collection_name, NULL);
1095 * \brief Create a PubSub leaf node.
1096 * \param client the configured XMPP client we use to connect to a XMPP server
1097 * \param collection_name
1098 * \param leaf_name The name to use for this collection
1101 static void xmpp_pubsub_create_leaf(struct ast_xmpp_client *client, const char *collection_name,
1102 const char *leaf_name)
1104 xmpp_pubsub_create_node(client, "leaf", leaf_name, collection_name);
1108 * \brief Publish MWI to a PubSub node
1109 * \param client the configured XMPP client we use to connect to a XMPP server
1110 * \param mailbox The Mailbox
1111 * \param context The Context
1112 * \param oldmsgs Old messages
1113 * \param newmsgs New Messages
1116 static void xmpp_pubsub_publish_mwi(struct ast_xmpp_client *client, const char *mailbox,
1117 const char *context, const char *oldmsgs, const char *newmsgs)
1119 char full_mailbox[AST_MAX_EXTENSION+AST_MAX_CONTEXT], eid_str[20];
1120 iks *mailbox_node, *request;
1122 snprintf(full_mailbox, sizeof(full_mailbox), "%s@%s", mailbox, context);
1124 if (!(request = xmpp_pubsub_build_publish_skeleton(client, full_mailbox, "message_waiting"))) {
1128 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
1129 mailbox_node = iks_insert(request, "mailbox");
1130 iks_insert_attrib(mailbox_node, "xmlns", "http://asterisk.org");
1131 iks_insert_attrib(mailbox_node, "eid", eid_str);
1132 iks_insert_cdata(iks_insert(mailbox_node, "NEWMSGS"), newmsgs, strlen(newmsgs));
1133 iks_insert_cdata(iks_insert(mailbox_node, "OLDMSGS"), oldmsgs, strlen(oldmsgs));
1135 ast_xmpp_client_send(client, iks_root(request));
1137 iks_delete(request);
1141 * \brief Publish device state to a PubSub node
1142 * \param client the configured XMPP client we use to connect to a XMPP server
1143 * \param device the name of the device whose state to publish
1144 * \param device_state the state to publish
1147 static void xmpp_pubsub_publish_device_state(struct ast_xmpp_client *client, const char *device,
1148 const char *device_state)
1150 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1151 iks *request, *state;
1154 if (!cfg || !cfg->global || !(request = xmpp_pubsub_build_publish_skeleton(client, device, "device_state"))) {
1158 if (ast_test_flag(&cfg->global->pubsub, XMPP_PUBSUB_AUTOCREATE)) {
1159 if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
1160 xmpp_pubsub_create_node(client, "leaf", device, "device_state");
1162 xmpp_pubsub_create_node(client, NULL, device, NULL);
1166 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
1167 state = iks_insert(request, "state");
1168 iks_insert_attrib(state, "xmlns", "http://asterisk.org");
1169 iks_insert_attrib(state, "eid", eid_str);
1170 iks_insert_cdata(state, device_state, strlen(device_state));
1171 ast_xmpp_client_send(client, iks_root(request));
1172 iks_delete(request);
1176 * \brief Callback function for MWI events
1178 * \param data void pointer to ast_client structure
1181 static void xmpp_pubsub_mwi_cb(const struct ast_event *ast_event, void *data)
1183 struct ast_xmpp_client *client = data;
1184 const char *mailbox, *context;
1185 char oldmsgs[10], newmsgs[10];
1187 if (ast_eid_cmp(&ast_eid_default, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID))) {
1188 /* If the event didn't originate from this server, don't send it back out. */
1189 ast_debug(1, "Returning here\n");
1193 mailbox = ast_event_get_ie_str(ast_event, AST_EVENT_IE_MAILBOX);
1194 context = ast_event_get_ie_str(ast_event, AST_EVENT_IE_CONTEXT);
1195 snprintf(oldmsgs, sizeof(oldmsgs), "%d",
1196 ast_event_get_ie_uint(ast_event, AST_EVENT_IE_OLDMSGS));
1197 snprintf(newmsgs, sizeof(newmsgs), "%d",
1198 ast_event_get_ie_uint(ast_event, AST_EVENT_IE_NEWMSGS));
1199 xmpp_pubsub_publish_mwi(client, mailbox, context, oldmsgs, newmsgs);
1203 * \brief Callback function for device state events
1205 * \param data void pointer to ast_client structure
1208 static void xmpp_pubsub_devstate_cb(const struct ast_event *ast_event, void *data)
1210 struct ast_xmpp_client *client = data;
1211 const char *device, *device_state;
1213 if (ast_eid_cmp(&ast_eid_default, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID))) {
1214 /* If the event didn't originate from this server, don't send it back out. */
1215 ast_debug(1, "Returning here\n");
1219 device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE);
1220 device_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, AST_EVENT_IE_STATE));
1221 xmpp_pubsub_publish_device_state(client, device, device_state);
1225 * \brief Unsubscribe from a PubSub node
1226 * \param client the configured XMPP client we use to connect to a XMPP server
1227 * \param node the name of the node to which to unsubscribe from
1230 static void xmpp_pubsub_unsubscribe(struct ast_xmpp_client *client, const char *node)
1232 iks *request = xmpp_pubsub_iq_create(client, "set");
1233 iks *pubsub, *unsubscribe;
1236 ast_log(LOG_ERROR, "Could not create IQ when creating pubsub unsubscription on client '%s'\n", client->name);
1240 pubsub = iks_insert(request, "pubsub");
1241 iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
1242 unsubscribe = iks_insert(pubsub, "unsubscribe");
1243 iks_insert_attrib(unsubscribe, "jid", client->jid->partial);
1244 iks_insert_attrib(unsubscribe, "node", node);
1246 ast_xmpp_client_send(client, request);
1247 iks_delete(request);
1251 * \brief Subscribe to a PubSub node
1252 * \param client the configured XMPP client we use to connect to a XMPP server
1253 * \param node the name of the node to which to subscribe
1256 static void xmpp_pubsub_subscribe(struct ast_xmpp_client *client, const char *node)
1258 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1259 iks *request = xmpp_pubsub_iq_create(client, "set");
1260 iks *pubsub, *subscribe;
1262 if (!cfg || !cfg->global || !request) {
1263 ast_log(LOG_ERROR, "Could not create IQ when creating pubsub subscription on client '%s'\n", client->name);
1267 pubsub = iks_insert(request, "pubsub");
1268 iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
1269 subscribe = iks_insert(pubsub, "subscribe");
1270 iks_insert_attrib(subscribe, "jid", client->jid->partial);
1271 iks_insert_attrib(subscribe, "node", node);
1272 if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
1273 iks *options, *x, *sub_options, *sub_type, *sub_depth, *sub_expire;
1274 options = iks_insert(pubsub, "options");
1275 x = iks_insert(options, "x");
1276 iks_insert_attrib(x, "xmlns", "jabber:x:data");
1277 iks_insert_attrib(x, "type", "submit");
1278 sub_options = iks_insert(x, "field");
1279 iks_insert_attrib(sub_options, "var", "FORM_TYPE");
1280 iks_insert_attrib(sub_options, "type", "hidden");
1281 iks_insert_cdata(iks_insert(sub_options, "value"),
1282 "http://jabber.org/protocol/pubsub#subscribe_options", 51);
1283 sub_type = iks_insert(x, "field");
1284 iks_insert_attrib(sub_type, "var", "pubsub#subscription_type");
1285 iks_insert_cdata(iks_insert(sub_type, "value"), "items", 5);
1286 sub_depth = iks_insert(x, "field");
1287 iks_insert_attrib(sub_depth, "var", "pubsub#subscription_depth");
1288 iks_insert_cdata(iks_insert(sub_depth, "value"), "all", 3);
1289 sub_expire = iks_insert(x, "field");
1290 iks_insert_attrib(sub_expire, "var", "pubsub#expire");
1291 iks_insert_cdata(iks_insert(sub_expire, "value"), "presence", 8);
1293 ast_xmpp_client_send(client, request);
1294 iks_delete(request);
1298 * \brief Callback for handling PubSub events
1299 * \param data void pointer to ast_xmpp_client structure
1301 * \return IKS_FILTER_EAT
1303 static int xmpp_pubsub_handle_event(void *data, ikspak *pak)
1305 char *item_id, *device_state, *context;
1306 int oldmsgs, newmsgs;
1307 iks *item, *item_content;
1308 struct ast_eid pubsub_eid;
1309 struct ast_event *event;
1310 item = iks_find(iks_find(iks_find(pak->x, "event"), "items"), "item");
1312 ast_log(LOG_ERROR, "Could not parse incoming PubSub event\n");
1313 return IKS_FILTER_EAT;
1315 item_id = iks_find_attrib(item, "id");
1316 item_content = iks_child(item);
1317 ast_str_to_eid(&pubsub_eid, iks_find_attrib(item_content, "eid"));
1318 if (!ast_eid_cmp(&ast_eid_default, &pubsub_eid)) {
1319 ast_debug(1, "Returning here, eid of incoming event matches ours!\n");
1320 return IKS_FILTER_EAT;
1322 if (!strcasecmp(iks_name(item_content), "state")) {
1323 device_state = iks_find_cdata(item, "state");
1324 if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE,
1325 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, item_id, AST_EVENT_IE_STATE,
1326 AST_EVENT_IE_PLTYPE_UINT, ast_devstate_val(device_state), AST_EVENT_IE_EID,
1327 AST_EVENT_IE_PLTYPE_RAW, &pubsub_eid, sizeof(pubsub_eid),
1328 AST_EVENT_IE_END))) {
1329 return IKS_FILTER_EAT;
1331 } else if (!strcasecmp(iks_name(item_content), "mailbox")) {
1332 context = strsep(&item_id, "@");
1333 sscanf(iks_find_cdata(item_content, "OLDMSGS"), "%10d", &oldmsgs);
1334 sscanf(iks_find_cdata(item_content, "NEWMSGS"), "%10d", &newmsgs);
1335 if (!(event = ast_event_new(AST_EVENT_MWI, AST_EVENT_IE_MAILBOX,
1336 AST_EVENT_IE_PLTYPE_STR, item_id, AST_EVENT_IE_CONTEXT,
1337 AST_EVENT_IE_PLTYPE_STR, context, AST_EVENT_IE_OLDMSGS,
1338 AST_EVENT_IE_PLTYPE_UINT, oldmsgs, AST_EVENT_IE_NEWMSGS,
1339 AST_EVENT_IE_PLTYPE_UINT, newmsgs, AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW,
1340 &pubsub_eid, sizeof(pubsub_eid), AST_EVENT_IE_END))) {
1341 return IKS_FILTER_EAT;
1344 ast_debug(1, "Don't know how to handle PubSub event of type %s\n",
1345 iks_name(item_content));
1346 return IKS_FILTER_EAT;
1348 ast_event_queue_and_cache(event);
1349 return IKS_FILTER_EAT;
1352 static int xmpp_pubsub_handle_error(void *data, ikspak *pak)
1354 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1355 char *node_name, *error;
1357 iks *orig_request, *orig_pubsub = iks_find(pak->x, "pubsub");
1358 struct ast_xmpp_client *client = data;
1360 if (!cfg || !cfg->global) {
1361 ast_log(LOG_ERROR, "No global configuration available\n");
1362 return IKS_FILTER_EAT;
1366 ast_log(LOG_ERROR, "Error isn't a PubSub error, why are we here?\n");
1367 return IKS_FILTER_EAT;
1370 orig_request = iks_child(orig_pubsub);
1371 error = iks_find_attrib(iks_find(pak->x, "error"), "code");
1372 node_name = iks_find_attrib(orig_request, "node");
1374 if (!sscanf(error, "%30d", &error_num)) {
1375 return IKS_FILTER_EAT;
1378 if (error_num > 399 && error_num < 500 && error_num != 404) {
1380 "Error performing operation on PubSub node %s, %s.\n", node_name, error);
1381 return IKS_FILTER_EAT;
1382 } else if (error_num > 499 && error_num < 600) {
1383 ast_log(LOG_ERROR, "PubSub Server error, %s\n", error);
1384 return IKS_FILTER_EAT;
1387 if (!strcasecmp(iks_name(orig_request), "publish")) {
1390 if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
1391 if (iks_find(iks_find(orig_request, "item"), "state")) {
1392 xmpp_pubsub_create_leaf(client, "device_state", node_name);
1393 } else if (iks_find(iks_find(orig_request, "item"), "mailbox")) {
1394 xmpp_pubsub_create_leaf(client, "message_waiting", node_name);
1397 xmpp_pubsub_create_node(client, NULL, node_name, NULL);
1400 if ((request = xmpp_pubsub_iq_create(client, "set"))) {
1401 iks_insert_node(request, orig_pubsub);
1402 ast_xmpp_client_send(client, request);
1403 iks_delete(request);
1405 ast_log(LOG_ERROR, "PubSub publish could not create IQ\n");
1408 return IKS_FILTER_EAT;
1409 } else if (!strcasecmp(iks_name(orig_request), "subscribe")) {
1410 if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
1411 xmpp_pubsub_create_collection(client, node_name);
1413 xmpp_pubsub_create_node(client, NULL, node_name, NULL);
1417 return IKS_FILTER_EAT;
1421 * \brief Initialize collections for event distribution
1422 * \param client the configured XMPP client we use to connect to a XMPP server
1425 static void xmpp_init_event_distribution(struct ast_xmpp_client *client)
1427 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1428 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1430 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
1434 xmpp_pubsub_unsubscribe(client, "device_state");
1435 xmpp_pubsub_unsubscribe(client, "message_waiting");
1437 if (!(client->mwi_sub = ast_event_subscribe(AST_EVENT_MWI, xmpp_pubsub_mwi_cb, "xmpp_pubsub_mwi_subscription",
1438 client, AST_EVENT_IE_END))) {
1442 if (ast_enable_distributed_devstate()) {
1447 if (!(client->device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
1448 xmpp_pubsub_devstate_cb, "xmpp_pubsub_devstate_subscription", client, AST_EVENT_IE_END))) {
1449 ast_event_unsubscribe(client->mwi_sub);
1450 client->mwi_sub = NULL;
1454 ast_event_dump_cache(client->device_state_sub);
1456 xmpp_pubsub_subscribe(client, "device_state");
1457 xmpp_pubsub_subscribe(client, "message_waiting");
1458 iks_filter_add_rule(client->filter, xmpp_pubsub_handle_event, client, IKS_RULE_TYPE,
1459 IKS_PAK_MESSAGE, IKS_RULE_FROM, clientcfg->pubsubnode, IKS_RULE_DONE);
1460 iks_filter_add_rule(client->filter, xmpp_pubsub_handle_error, client, IKS_RULE_TYPE,
1461 IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_ERROR, IKS_RULE_DONE);
1465 /*! \brief Internal astobj2 callback function which returns the first resource, which is the highest priority one */
1466 static int xmpp_resource_immediate(void *obj, void *arg, int flags)
1468 return CMP_MATCH | CMP_STOP;
1473 * \brief Dial plan function status(). puts the status of watched user
1474 * into a channel variable.
1475 * \param chan ast_channel
1480 static int xmpp_status_exec(struct ast_channel *chan, const char *data)
1482 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1483 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1484 struct ast_xmpp_buddy *buddy;
1485 struct ast_xmpp_resource *resource;
1486 char *s = NULL, status[2];
1488 static int deprecation_warning = 0;
1489 AST_DECLARE_APP_ARGS(args,
1490 AST_APP_ARG(sender);
1492 AST_APP_ARG(variable);
1494 AST_DECLARE_APP_ARGS(jid,
1495 AST_APP_ARG(screenname);
1496 AST_APP_ARG(resource);
1499 if (deprecation_warning++ % 10 == 0) {
1500 ast_log(LOG_WARNING, "JabberStatus is deprecated. Please use the JABBER_STATUS dialplan function in the future.\n");
1503 if (ast_strlen_zero(data)) {
1504 ast_log(LOG_ERROR, "Usage: JabberStatus(<sender>,<jid>[/<resource>],<varname>\n");
1507 s = ast_strdupa(data);
1508 AST_STANDARD_APP_ARGS(args, s);
1510 if (args.argc != 3) {
1511 ast_log(LOG_ERROR, "JabberStatus() requires 3 arguments.\n");
1515 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
1516 if (jid.argc < 1 || jid.argc > 2) {
1517 ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
1521 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
1522 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
1526 if (!(buddy = ao2_find(clientcfg->client->buddies, jid.screenname, OBJ_KEY))) {
1527 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
1531 if (ast_strlen_zero(jid.resource) || !(resource = ao2_find(buddy->resources, jid.resource, OBJ_KEY))) {
1532 resource = ao2_callback(buddy->resources, OBJ_NODATA, xmpp_resource_immediate, NULL);
1538 stat = resource->status;
1539 ao2_ref(resource, -1);
1541 ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
1544 snprintf(status, sizeof(status), "%d", stat);
1545 pbx_builtin_setvar_helper(chan, args.variable, status);
1552 * \brief Dial plan funtcion to retrieve the status of a buddy.
1553 * \param channel The associated ast_channel, if there is one
1554 * \param data The account, buddy JID, and optional timeout
1557 * \retval -1 failure
1559 static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
1561 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1562 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1563 struct ast_xmpp_buddy *buddy;
1564 struct ast_xmpp_resource *resource;
1566 AST_DECLARE_APP_ARGS(args,
1567 AST_APP_ARG(sender);
1570 AST_DECLARE_APP_ARGS(jid,
1571 AST_APP_ARG(screenname);
1572 AST_APP_ARG(resource);
1575 if (ast_strlen_zero(data)) {
1576 ast_log(LOG_ERROR, "Usage: JABBER_STATUS(<sender>,<jid>[/<resource>])\n");
1579 AST_STANDARD_APP_ARGS(args, data);
1581 if (args.argc != 2) {
1582 ast_log(LOG_ERROR, "JABBER_STATUS requires 2 arguments: sender and jid.\n");
1586 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
1587 if (jid.argc < 1 || jid.argc > 2) {
1588 ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
1592 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
1593 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
1597 if (!(buddy = ao2_find(clientcfg->client->buddies, jid.screenname, OBJ_KEY))) {
1598 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
1602 if (ast_strlen_zero(jid.resource) || !(resource = ao2_find(buddy->resources, jid.resource, OBJ_KEY))) {
1603 resource = ao2_callback(buddy->resources, OBJ_NODATA, xmpp_resource_immediate, NULL);
1609 stat = resource->status;
1610 ao2_ref(resource, -1);
1612 ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
1615 snprintf(buf, buflen, "%d", stat);
1620 static struct ast_custom_function jabberstatus_function = {
1621 .name = "JABBER_STATUS",
1622 .read = acf_jabberstatus_read,
1626 * \brief Application to join a chat room
1627 * \param chan ast_channel
1628 * \param data Data is sender|jid|nickname.
1632 static int xmpp_join_exec(struct ast_channel *chan, const char *data)
1634 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1635 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1636 char *s, nick[XMPP_MAX_RESJIDLEN];
1637 AST_DECLARE_APP_ARGS(args,
1638 AST_APP_ARG(sender);
1643 if (ast_strlen_zero(data)) {
1644 ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
1647 s = ast_strdupa(data);
1649 AST_STANDARD_APP_ARGS(args, s);
1650 if (args.argc < 2 || args.argc > 3) {
1651 ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
1655 if (strchr(args.jid, '/')) {
1656 ast_log(LOG_ERROR, "Invalid room name : resource must not be appended\n");
1660 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
1661 ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
1665 if (ast_strlen_zero(args.nick)) {
1666 if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
1667 snprintf(nick, sizeof(nick), "asterisk");
1669 snprintf(nick, sizeof(nick), "%s", clientcfg->client->jid->user);
1672 snprintf(nick, sizeof(nick), "%s", args.nick);
1675 if (!ast_strlen_zero(args.jid) && strchr(args.jid, '@')) {
1676 ast_xmpp_chatroom_join(clientcfg->client, args.jid, nick);
1678 ast_log(LOG_ERROR, "Problem with specified jid of '%s'\n", args.jid);
1685 * \brief Application to leave a chat room
1686 * \param chan ast_channel
1687 * \param data Data is sender|jid|nickname.
1691 static int xmpp_leave_exec(struct ast_channel *chan, const char *data)
1693 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1694 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1695 char *s, nick[XMPP_MAX_RESJIDLEN];
1696 AST_DECLARE_APP_ARGS(args,
1697 AST_APP_ARG(sender);
1702 if (ast_strlen_zero(data)) {
1703 ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
1706 s = ast_strdupa(data);
1708 AST_STANDARD_APP_ARGS(args, s);
1709 if (args.argc < 2 || args.argc > 3) {
1710 ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
1714 if (strchr(args.jid, '/')) {
1715 ast_log(LOG_ERROR, "Invalid room name, resource must not be appended\n");
1719 if (ast_strlen_zero(args.jid) || !strchr(args.jid, '@')) {
1720 ast_log(LOG_ERROR, "No jabber ID specified\n");
1724 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
1725 ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
1729 if (ast_strlen_zero(args.nick)) {
1730 if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
1731 snprintf(nick, sizeof(nick), "asterisk");
1733 snprintf(nick, sizeof(nick), "%s", clientcfg->client->jid->user);
1736 snprintf(nick, sizeof(nick), "%s", args.nick);
1739 ast_xmpp_chatroom_leave(clientcfg->client, args.jid, nick);
1746 * \brief Dial plan function to send a message.
1747 * \param chan ast_channel
1748 * \param data Data is account,jid,message.
1750 * \retval -1 failure
1752 static int xmpp_send_exec(struct ast_channel *chan, const char *data)
1754 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1755 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1757 AST_DECLARE_APP_ARGS(args,
1758 AST_APP_ARG(sender);
1759 AST_APP_ARG(recipient);
1760 AST_APP_ARG(message);
1763 if (ast_strlen_zero(data)) {
1764 ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
1767 s = ast_strdupa(data);
1769 AST_STANDARD_APP_ARGS(args, s);
1771 if ((args.argc < 3) || ast_strlen_zero(args.message) || !strchr(args.recipient, '@')) {
1772 ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
1776 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
1777 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
1781 ast_xmpp_client_send_message(clientcfg->client, args.recipient, args.message);
1787 * \brief Application to send a message to a groupchat.
1788 * \param chan ast_channel
1789 * \param data Data is sender|groupchat|message.
1793 static int xmpp_sendgroup_exec(struct ast_channel *chan, const char *data)
1795 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1796 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1797 char *s, nick[XMPP_MAX_RESJIDLEN];
1798 AST_DECLARE_APP_ARGS(args,
1799 AST_APP_ARG(sender);
1800 AST_APP_ARG(groupchat);
1801 AST_APP_ARG(message);
1805 if (ast_strlen_zero(data)) {
1806 ast_log(LOG_ERROR, "%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
1809 s = ast_strdupa(data);
1811 AST_STANDARD_APP_ARGS(args, s);
1812 if ((args.argc < 3) || (args.argc > 4) || ast_strlen_zero(args.message) || !strchr(args.groupchat, '@')) {
1813 ast_log(LOG_ERROR, "%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
1817 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
1818 ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
1822 if (ast_strlen_zero(args.nick) || args.argc == 3) {
1823 if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
1824 snprintf(nick, sizeof(nick), "asterisk");
1826 snprintf(nick, sizeof(nick), "%s", clientcfg->client->jid->user);
1829 snprintf(nick, sizeof(nick), "%s", args.nick);
1832 ast_xmpp_chatroom_send(clientcfg->client, nick, args.groupchat, args.message);
1839 * \brief Dial plan function to receive a message.
1840 * \param channel The associated ast_channel, if there is one
1841 * \param data The account, JID, and optional timeout
1844 * \retval -1 failure
1846 static int acf_jabberreceive_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
1848 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
1849 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
1850 char *aux = NULL, *parse = NULL;
1851 int timeout, jidlen, resourcelen, found = 0;
1852 struct timeval start;
1854 struct ast_xmpp_message *message;
1855 AST_DECLARE_APP_ARGS(args,
1856 AST_APP_ARG(account);
1858 AST_APP_ARG(timeout);
1860 AST_DECLARE_APP_ARGS(jid,
1861 AST_APP_ARG(screenname);
1862 AST_APP_ARG(resource);
1865 if (ast_strlen_zero(data)) {
1866 ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
1870 parse = ast_strdupa(data);
1871 AST_STANDARD_APP_ARGS(args, parse);
1873 if (args.argc < 2 || args.argc > 3) {
1874 ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
1878 parse = ast_strdupa(args.jid);
1879 AST_NONSTANDARD_APP_ARGS(jid, parse, '/');
1880 if (jid.argc < 1 || jid.argc > 2 || strlen(args.jid) > XMPP_MAX_JIDLEN) {
1881 ast_log(LOG_WARNING, "Invalid JID : %s\n", parse);
1885 if (ast_strlen_zero(args.timeout)) {
1888 sscanf(args.timeout, "%d", &timeout);
1890 ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", args.timeout);
1895 jidlen = strlen(jid.screenname);
1896 resourcelen = ast_strlen_zero(jid.resource) ? 0 : strlen(jid.resource);
1898 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.account))) {
1899 ast_log(LOG_WARNING, "Could not find client %s, exiting\n", args.account);
1903 ast_debug(3, "Waiting for an XMPP message from %s\n", args.jid);
1905 start = ast_tvnow();
1907 if (ast_autoservice_start(chan) < 0) {
1908 ast_log(LOG_WARNING, "Cannot start autoservice for channel %s\n", ast_channel_name(chan));
1912 /* search the messages list, grab the first message that matches with
1913 * the from JID we're expecting, and remove it from the messages list */
1914 while (diff < timeout) {
1915 struct timespec ts = { 0, };
1916 struct timeval wait;
1919 wait = ast_tvadd(start, ast_tv(timeout, 0));
1920 ts.tv_sec = wait.tv_sec;
1921 ts.tv_nsec = wait.tv_usec * 1000;
1923 /* wait up to timeout seconds for an incoming message */
1924 ast_mutex_lock(&messagelock);
1925 if (AST_LIST_EMPTY(&clientcfg->client->messages)) {
1926 res = ast_cond_timedwait(&message_received_condition, &messagelock, &ts);
1928 ast_mutex_unlock(&messagelock);
1929 if (res == ETIMEDOUT) {
1930 ast_debug(3, "No message received from %s in %d seconds\n", args.jid, timeout);
1934 AST_LIST_LOCK(&clientcfg->client->messages);
1935 AST_LIST_TRAVERSE_SAFE_BEGIN(&clientcfg->client->messages, message, list) {
1936 if (jid.argc == 1) {
1937 /* no resource provided, compare bare JIDs */
1938 if (strncasecmp(jid.screenname, message->from, jidlen)) {
1942 /* resource appended, compare bare JIDs and resources */
1943 char *resource = strchr(message->from, '/');
1944 if (!resource || strlen(resource) == 0) {
1945 ast_log(LOG_WARNING, "Remote JID has no resource : %s\n", message->from);
1946 if (strncasecmp(jid.screenname, message->from, jidlen)) {
1951 if (strncasecmp(jid.screenname, message->from, jidlen) || strncmp(jid.resource, resource, resourcelen)) {
1956 /* check if the message is not too old */
1957 if (ast_tvdiff_sec(ast_tvnow(), message->arrived) >= clientcfg->message_timeout) {
1958 ast_debug(3, "Found old message from %s, deleting it\n", message->from);
1959 AST_LIST_REMOVE_CURRENT(list);
1960 xmpp_message_destroy(message);
1964 aux = ast_strdupa(message->message);
1965 AST_LIST_REMOVE_CURRENT(list);
1966 xmpp_message_destroy(message);
1969 AST_LIST_TRAVERSE_SAFE_END;
1970 AST_LIST_UNLOCK(&clientcfg->client->messages);
1976 diff = ast_tvdiff_ms(ast_tvnow(), start);
1979 if (ast_autoservice_stop(chan) < 0) {
1980 ast_log(LOG_WARNING, "Cannot stop autoservice for channel %s\n", ast_channel_name(chan));
1983 /* return if we timed out */
1985 ast_log(LOG_NOTICE, "Timed out : no message received from %s\n", args.jid);
1988 ast_copy_string(buf, aux, buflen);
1993 static struct ast_custom_function jabberreceive_function = {
1994 .name = "JABBER_RECEIVE",
1995 .read = acf_jabberreceive_read,
2000 * \brief Delete old messages from a given JID
2001 * Messages stored during more than client->message_timeout are deleted
2002 * \param client Asterisk's XMPP client
2003 * \param from the JID we received messages from
2004 * \retval the number of deleted messages
2006 static int delete_old_messages(struct ast_xmpp_client *client, char *from)
2008 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2009 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2010 int deleted = 0, isold = 0;
2011 struct ast_xmpp_message *message = NULL;
2013 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
2017 AST_LIST_LOCK(&client->messages);
2018 AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, message, list) {
2020 if (!from || !strncasecmp(from, message->from, strlen(from))) {
2021 AST_LIST_REMOVE_CURRENT(list);
2022 xmpp_message_destroy(message);
2025 } else if (ast_tvdiff_sec(ast_tvnow(), message->arrived) >= clientcfg->message_timeout) {
2027 if (!from || !strncasecmp(from, message->from, strlen(from))) {
2028 AST_LIST_REMOVE_CURRENT(list);
2029 xmpp_message_destroy(message);
2034 AST_LIST_TRAVERSE_SAFE_END;
2035 AST_LIST_UNLOCK(&client->messages);
2040 static int xmpp_send_cb(const struct ast_msg *msg, const char *to, const char *from)
2042 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2043 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2044 char *sender, *dest;
2047 sender = ast_strdupa(from);
2048 strsep(&sender, ":");
2049 dest = ast_strdupa(to);
2052 if (ast_strlen_zero(sender)) {
2053 ast_log(LOG_ERROR, "MESSAGE(from) of '%s' invalid for XMPP\n", from);
2057 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, sender))) {
2058 ast_log(LOG_WARNING, "Could not finder account to send from as '%s'\n", sender);
2062 ast_debug(1, "Sending message to '%s' from '%s'\n", dest, clientcfg->name);
2064 if ((res = ast_xmpp_client_send_message(clientcfg->client, dest, ast_msg_get_body(msg))) != IKS_OK) {
2065 ast_log(LOG_WARNING, "Failed to send XMPP message (%d).\n", res);
2068 return res == IKS_OK ? 0 : -1;
2071 static const struct ast_msg_tech msg_tech = {
2073 .msg_send = xmpp_send_cb,
2076 /*! \brief Internal function which changes the XMPP client state */
2077 static void xmpp_client_change_state(struct ast_xmpp_client *client, int state)
2079 client->state = state;
2082 /*! \brief Internal function which creates a buddy on a client */
2083 static struct ast_xmpp_buddy *xmpp_client_create_buddy(struct ao2_container *container, const char *id)
2085 struct ast_xmpp_buddy *buddy;
2087 if (!(buddy = ao2_alloc(sizeof(*buddy), xmpp_buddy_destructor))) {
2091 if (!(buddy->resources = ao2_container_alloc(RESOURCE_BUCKETS, xmpp_resource_hash, xmpp_resource_cmp))) {
2096 ast_copy_string(buddy->id, id, sizeof(buddy->id));
2098 /* Assume we need to subscribe to get their presence until proven otherwise */
2099 buddy->subscribe = 1;
2101 ao2_link(container, buddy);
2106 /*! \brief Helper function which unsubscribes a user and removes them from the roster */
2107 static int xmpp_client_unsubscribe_user(struct ast_xmpp_client *client, const char *user)
2109 iks *iq, *query = NULL, *item = NULL;
2111 if (ast_xmpp_client_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, user,
2112 "Goodbye. Your status is no longer required.\n"))) {
2116 if (!(iq = iks_new("iq")) || !(query = iks_new("query")) || !(item = iks_new("item"))) {
2117 ast_log(LOG_WARNING, "Could not allocate memory for roster removal of '%s' from client '%s'\n",
2118 user, client->name);
2122 iks_insert_attrib(iq, "from", client->jid->full);
2123 iks_insert_attrib(iq, "type", "set");
2124 iks_insert_attrib(query, "xmlns", "jabber:iq:roster");
2125 iks_insert_node(iq, query);
2126 iks_insert_attrib(item, "jid", user);
2127 iks_insert_attrib(item, "subscription", "remove");
2128 iks_insert_node(query, item);
2130 if (ast_xmpp_client_send(client, iq)) {
2131 ast_log(LOG_WARNING, "Could not send roster removal request of '%s' from client '%s'\n",
2132 user, client->name);
2143 /*! \brief Callback function which subscribes to a user if needed */
2144 static int xmpp_client_subscribe_user(void *obj, void *arg, int flags)
2146 struct ast_xmpp_buddy *buddy = obj;
2147 struct ast_xmpp_client *client = arg;
2149 if (!buddy->subscribe) {
2153 if (ast_xmpp_client_send(client, iks_make_s10n(IKS_TYPE_SUBSCRIBE, buddy->id,
2154 "Greetings! I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"))) {
2155 ast_log(LOG_WARNING, "Could not send subscription for '%s' on client '%s'\n",
2156 buddy->id, client->name);
2159 buddy->subscribe = 0;
2164 /*! \brief Hook function called when roster is received from server */
2165 static int xmpp_roster_hook(void *data, ikspak *pak)
2167 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2168 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2169 struct ast_xmpp_client *client = data;
2172 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
2173 return IKS_FILTER_EAT;
2176 for (item = iks_child(pak->query); item; item = iks_next(item)) {
2177 struct ast_xmpp_buddy *buddy;
2179 if (iks_strcmp(iks_name(item), "item")) {
2183 if (!(buddy = ao2_find(client->buddies, iks_find_attrib(item, "jid"), OBJ_KEY))) {
2184 if (ast_test_flag(&clientcfg->flags, XMPP_AUTOPRUNE)) {
2185 /* The buddy has not been specified in the configuration file, we no longer
2186 * want them on our buddy list or to receive their presence. */
2187 if (xmpp_client_unsubscribe_user(client, iks_find_attrib(item, "jid"))) {
2188 ast_log(LOG_ERROR, "Could not unsubscribe user '%s' on client '%s'\n",
2189 iks_find_attrib(item, "jid"), client->name);
2194 if (!(buddy = xmpp_client_create_buddy(client->buddies, iks_find_attrib(item, "jid")))) {
2195 ast_log(LOG_ERROR, "Could not allocate buddy '%s' on client '%s'\n", iks_find_attrib(item, "jid"),
2201 /* Determine if we need to subscribe to their presence or not */
2202 if (!iks_strcmp(iks_find_attrib(item, "subscription"), "none") ||
2203 !iks_strcmp(iks_find_attrib(item, "subscription"), "from")) {
2204 buddy->subscribe = 1;
2206 buddy->subscribe = 0;
2212 /* If autoregister is enabled we need to go through every buddy that we need to subscribe to and do so */
2213 if (ast_test_flag(&clientcfg->flags, XMPP_AUTOREGISTER)) {
2214 ao2_callback(client->buddies, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_subscribe_user, client);
2217 xmpp_client_change_state(client, XMPP_STATE_CONNECTED);
2219 return IKS_FILTER_EAT;
2222 /*! \brief Internal function which changes the presence status of an XMPP client */
2223 static void xmpp_client_set_presence(struct ast_xmpp_client *client, const char *to, const char *from, int level, const char *desc)
2225 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2226 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2227 iks *presence = NULL, *cnode = NULL, *priority = NULL;
2230 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
2231 !(presence = iks_make_pres(level, desc)) || !(cnode = iks_new("c")) || !(priority = iks_new("priority"))) {
2232 ast_log(LOG_ERROR, "Unable to allocate stanzas for setting presence status for client '%s'\n", client->name);
2236 if (!ast_strlen_zero(to)) {
2237 iks_insert_attrib(presence, "to", to);
2240 if (!ast_strlen_zero(from)) {
2241 iks_insert_attrib(presence, "from", from);
2244 snprintf(priorityS, sizeof(priorityS), "%d", clientcfg->priority);
2245 iks_insert_cdata(priority, priorityS, strlen(priorityS));
2246 iks_insert_node(presence, priority);
2247 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
2248 iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
2249 iks_insert_attrib(cnode, "ext", "voice-v1 video-v1 camera-v1");
2250 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
2251 iks_insert_node(presence, cnode);
2252 ast_xmpp_client_send(client, presence);
2256 iks_delete(presence);
2257 iks_delete(priority);
2260 /*! \brief Hook function called when client receives a service discovery get message */
2261 static int xmpp_client_service_discovery_get_hook(void *data, ikspak *pak)
2263 struct ast_xmpp_client *client = data;
2264 iks *iq, *disco = NULL, *ident = NULL, *google = NULL, *jingle = NULL, *ice = NULL, *rtp = NULL, *audio = NULL, *video = NULL, *query = NULL;
2266 if (!(iq = iks_new("iq")) || !(query = iks_new("query")) || !(ident = iks_new("identity")) || !(disco = iks_new("feature")) ||
2267 !(google = iks_new("feature")) || !(jingle = iks_new("feature")) || !(ice = iks_new("feature")) || !(rtp = iks_new("feature")) ||
2268 !(audio = iks_new("feature")) || !(video = iks_new("feature"))) {
2269 ast_log(LOG_ERROR, "Could not allocate memory for responding to service discovery request from '%s' on client '%s'\n",
2270 pak->from->full, client->name);
2274 iks_insert_attrib(iq, "from", client->jid->full);
2277 iks_insert_attrib(iq, "to", pak->from->full);
2280 iks_insert_attrib(iq, "type", "result");
2281 iks_insert_attrib(iq, "id", pak->id);
2282 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
2283 iks_insert_attrib(ident, "category", "client");
2284 iks_insert_attrib(ident, "type", "pc");
2285 iks_insert_attrib(ident, "name", "asterisk");
2286 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
2288 iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
2289 iks_insert_attrib(jingle, "var", "urn:xmpp:jingle:1");
2290 iks_insert_attrib(ice, "var", "urn:xmpp:jingle:transports:ice-udp:1");
2291 iks_insert_attrib(rtp, "var", "urn:xmpp:jingle:apps:rtp:1");
2292 iks_insert_attrib(audio, "var", "urn:xmpp:jingle:apps:rtp:audio");
2293 iks_insert_attrib(video, "var", "urn:xmpp:jingle:apps:rtp:video");
2294 iks_insert_node(iq, query);
2295 iks_insert_node(query, ident);
2296 iks_insert_node(query, google);
2297 iks_insert_node(query, disco);
2298 iks_insert_node(query, jingle);
2299 iks_insert_node(query, ice);
2300 iks_insert_node(query, rtp);
2301 iks_insert_node(query, audio);
2302 iks_insert_node(query, video);
2303 ast_xmpp_client_send(client, iq);
2317 return IKS_FILTER_EAT;
2320 /*! \brief Hook function called when client receives a service discovery result message */
2321 static int xmpp_client_service_discovery_result_hook(void *data, ikspak *pak)
2323 struct ast_xmpp_client *client = data;
2324 struct ast_xmpp_buddy *buddy;
2325 struct ast_xmpp_resource *resource;
2327 if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY))) {
2328 return IKS_FILTER_EAT;
2331 if (!(resource = ao2_find(buddy->resources, pak->from->resource, OBJ_KEY))) {
2333 return IKS_FILTER_EAT;
2338 if (iks_find_with_attrib(pak->query, "feature", "var", "urn:xmpp:jingle:1")) {
2339 resource->caps.jingle = 1;
2342 ao2_unlock(resource);
2344 ao2_ref(resource, -1);
2347 return IKS_FILTER_EAT;
2350 /*! \brief Hook function called when client finishes authenticating with the server */
2351 static int xmpp_connect_hook(void *data, ikspak *pak)
2353 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2354 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2355 struct ast_xmpp_client *client = data;
2358 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
2362 client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
2364 if (ast_test_flag(&clientcfg->flags, XMPP_DISTRIBUTE_EVENTS)) {
2365 xmpp_init_event_distribution(client);
2368 if (!(roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER))) {
2369 ast_log(LOG_ERROR, "Unable to allocate memory for roster request for client '%s'\n", client->name);
2373 iks_filter_add_rule(client->filter, xmpp_client_service_discovery_get_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2374 iks_filter_add_rule(client->filter, xmpp_client_service_discovery_result_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2376 iks_insert_attrib(roster, "id", "roster");
2377 ast_xmpp_client_send(client, roster);
2379 iks_filter_remove_hook(client->filter, xmpp_connect_hook);
2380 iks_filter_add_rule(client->filter, xmpp_roster_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "roster", IKS_RULE_DONE);
2382 xmpp_client_set_presence(client, NULL, client->jid->full, clientcfg->status, clientcfg->statusmsg);
2383 xmpp_client_change_state(client, XMPP_STATE_ROSTER);
2385 return IKS_FILTER_EAT;
2388 /*! \brief Logging hook function */
2389 static void xmpp_log_hook(void *data, const char *xmpp, size_t size, int incoming)
2391 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2392 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2393 struct ast_xmpp_client *client = data;
2395 if (!ast_strlen_zero(xmpp)) {
2396 manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s\r\n", client->name, xmpp);
2399 if (!debug && (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) || !ast_test_flag(&clientcfg->flags, XMPP_DEBUG))) {
2404 if (strlen(xmpp) == 1) {
2405 if (option_debug > 2 && xmpp[0] == ' ') {
2406 ast_verbose("\n<--- XMPP keep alive from '%s' --->\n", client->name);
2409 ast_verbose("\n<--- XMPP sent to '%s' --->\n%s\n<------------->\n", client->name, xmpp);
2412 ast_verbose("\n<--- XMPP received from '%s' --->\n%s\n<------------->\n", client->name, xmpp);
2416 /*! \brief Internal function which sends a raw message */
2417 static int xmpp_client_send_raw_message(struct ast_xmpp_client *client, const char *message)
2421 int len = strlen(message);
2423 if (xmpp_is_secure(client)) {
2424 ret = SSL_write(client->ssl_session, message, len);
2426 /* Log the message here, because iksemel's logHook is
2428 xmpp_log_hook(client, message, len, 0);
2433 /* If needed, data will be sent unencrypted, and logHook will
2434 be called inside iks_send_raw */
2435 ret = iks_send_raw(client->parser, message);
2436 if (ret != IKS_OK) {
2443 /*! \brief Helper function which sends an XMPP stream header to the server */
2444 static int xmpp_send_stream_header(struct ast_xmpp_client *client, const struct ast_xmpp_client_config *cfg, const char *to)
2446 char *namespace = ast_test_flag(&cfg->flags, XMPP_COMPONENT) ? "jabber:component:accept" : "jabber:client";
2447 char msg[91 + strlen(namespace) + 6 + strlen(to) + 16 + 1];
2449 snprintf(msg, sizeof(msg), "<?xml version='1.0'?>"
2450 "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='"
2451 "%s' to='%s' version='1.0'>", namespace, to);
2453 return xmpp_client_send_raw_message(client, msg);
2456 int ast_xmpp_client_send(struct ast_xmpp_client *client, iks *stanza)
2458 return xmpp_client_send_raw_message(client, iks_string(iks_stack(stanza), stanza));
2461 /*! \brief Internal function called when we need to request TLS support */
2462 static int xmpp_client_request_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2464 /* If the client connection is already secure we can jump straight to authenticating */
2465 if (xmpp_is_secure(client)) {
2466 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATE);
2470 #ifndef HAVE_OPENSSL
2471 ast_log(LOG_ERROR, "TLS connection for client '%s' cannot be established. OpenSSL is not available.\n", client->name);
2474 if (iks_send_raw(client->parser, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>") == IKS_NET_TLSFAIL) {
2475 ast_log(LOG_ERROR, "TLS connection for client '%s' cannot be started.\n", client->name);
2479 client->stream_flags |= TRY_SECURE;
2481 xmpp_client_change_state(client, XMPP_STATE_REQUESTED_TLS);
2487 /*! \brief Internal function called when we receive a response to our TLS initiation request */
2488 static int xmpp_client_requested_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2494 if (!strcmp(iks_name(node), "success")) {
2495 /* TLS is up and working, we can move on to authenticating now */
2496 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATE);
2498 } else if (!strcmp(iks_name(node), "failure")) {
2499 /* TLS negotiation was a failure, close it on down! */
2501 } else if (strcmp(iks_name(node), "proceed")) {
2502 /* Ignore any other responses */
2506 #ifndef HAVE_OPENSSL
2507 ast_log(LOG_ERROR, "Somehow we managed to try to start TLS negotiation on client '%s' without OpenSSL support, disconnecting\n", client->name);
2510 client->ssl_method = SSLv3_method();
2511 if (!(client->ssl_context = SSL_CTX_new((SSL_METHOD *) client->ssl_method))) {
2515 if (!(client->ssl_session = SSL_new(client->ssl_context))) {
2519 sock = iks_fd(client->parser);
2520 if (!SSL_set_fd(client->ssl_session, sock)) {
2524 if (!SSL_connect(client->ssl_session)) {
2528 client->stream_flags &= (~TRY_SECURE);
2529 client->stream_flags |= SECURE;
2531 if (xmpp_send_stream_header(client, cfg, client->jid->server) != IKS_OK) {
2532 ast_log(LOG_ERROR, "TLS connection for client '%s' could not be established, failed to send stream header after negotiation\n",
2537 ast_debug(1, "TLS connection for client '%s' started with server\n", client->name);
2539 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATE);
2544 ast_log(LOG_ERROR, "TLS connection for client '%s' cannot be established. OpenSSL initialization failed.\n", client->name);
2549 /*! \brief Internal function called when we need to authenticate using non-SASL */
2550 static int xmpp_client_authenticate_digest(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2552 iks *iq = NULL, *query = NULL;
2553 char buf[41], sidpass[100];
2555 if (!(iq = iks_new("iq")) || !(query = iks_insert(iq, "query"))) {
2556 ast_log(LOG_ERROR, "Stanzas could not be allocated for authentication on client '%s'\n", client->name);
2561 iks_insert_attrib(iq, "type", "set");
2562 iks_insert_cdata(iks_insert(query, "username"), client->jid->user, 0);
2563 iks_insert_cdata(iks_insert(query, "resource"), client->jid->resource, 0);
2565 iks_insert_attrib(query, "xmlns", "jabber:iq:auth");
2566 snprintf(sidpass, sizeof(sidpass), "%s%s", iks_find_attrib(node, "id"), cfg->password);
2567 ast_sha1_hash(buf, sidpass);
2568 iks_insert_cdata(iks_insert(query, "digest"), buf, 0);
2570 ast_xmpp_client_lock(client);
2571 iks_filter_add_rule(client->filter, xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid, IKS_RULE_DONE);
2572 iks_insert_attrib(iq, "id", client->mid);
2573 ast_xmpp_increment_mid(client->mid);
2574 ast_xmpp_client_unlock(client);
2576 iks_insert_attrib(iq, "to", client->jid->server);
2578 ast_xmpp_client_send(client, iq);
2582 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
2587 /*! \brief Internal function called when we need to authenticate using SASL */
2588 static int xmpp_client_authenticate_sasl(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2590 int features, len = strlen(client->jid->user) + strlen(cfg->password) + 3;
2593 char base64[(len + 2) * 4 / 3];
2595 if (strcmp(iks_name(node), "stream:features")) {
2596 /* Ignore anything beside stream features */
2600 features = iks_stream_features(node);
2602 if ((features & IKS_STREAM_SASL_MD5) && !xmpp_is_secure(client)) {
2603 if (iks_start_sasl(client->parser, IKS_SASL_DIGEST_MD5, (char*)client->jid->user, (char*)cfg->password) != IKS_OK) {
2604 ast_log(LOG_ERROR, "Tried to authenticate client '%s' using SASL DIGEST-MD5 but could not\n", client->name);
2608 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
2612 /* Our only other available option is plain so if they don't support it, bail out now */
2613 if (!(features & IKS_STREAM_SASL_PLAIN)) {
2614 ast_log(LOG_ERROR, "Tried to authenticate client '%s' using SASL PLAIN but server does not support it\n", client->name);
2618 if (!(auth = iks_new("auth"))) {
2619 ast_log(LOG_ERROR, "Could not allocate memory for SASL PLAIN authentication for client '%s'\n", client->name);
2623 iks_insert_attrib(auth, "xmlns", IKS_NS_XMPP_SASL);
2624 iks_insert_attrib(auth, "mechanism", "PLAIN");
2626 if (strchr(client->jid->user, '/')) {
2627 char *user = ast_strdupa(client->jid->user);
2629 snprintf(combined, sizeof(combined), "%c%s%c%s", 0, strsep(&user, "/"), 0, cfg->password);
2631 snprintf(combined, sizeof(combined), "%c%s%c%s", 0, client->jid->user, 0, cfg->password);
2634 ast_base64encode(base64, (const unsigned char *) combined, len - 1, (len + 2) * 4 / 3);
2635 iks_insert_cdata(auth, base64, 0);
2637 ast_xmpp_client_send(client, auth);
2641 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
2646 /*! \brief Internal function called when we need to authenticate */
2647 static int xmpp_client_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2649 return ast_test_flag(&cfg->flags, XMPP_USESASL) ? xmpp_client_authenticate_sasl(client, cfg, type, node) : xmpp_client_authenticate_digest(client, cfg, type, node);
2652 /*! \brief Internal function called when we are authenticating */
2653 static int xmpp_client_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2657 if (!strcmp(iks_name(node), "success")) {
2658 /* Authentication was a success, yay! */
2659 xmpp_send_stream_header(client, cfg, client->jid->server);
2662 } else if (!strcmp(iks_name(node), "failure")) {
2663 /* Authentication was a bust, disconnect and reconnect later */
2665 } else if (strcmp(iks_name(node), "stream:features")) {
2666 /* Ignore any other responses */
2670 features = iks_stream_features(node);
2672 if (features & IKS_STREAM_BIND) {
2675 if (!(auth = iks_make_resource_bind(client->jid))) {
2676 ast_log(LOG_ERROR, "Failed to allocate memory for stream bind on client '%s'\n", client->name);
2680 ast_xmpp_client_lock(client);
2681 iks_insert_attrib(auth, "id", client->mid);
2682 ast_xmpp_increment_mid(client->mid);
2683 ast_xmpp_client_unlock(client);
2684 ast_xmpp_client_send(client, auth);
2688 iks_filter_add_rule(client->filter, xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_DONE);
2691 if (features & IKS_STREAM_SESSION) {
2694 if (!(auth = iks_make_session())) {
2695 ast_log(LOG_ERROR, "Failed to allocate memory for stream session on client '%s'\n", client->name);
2699 iks_insert_attrib(auth, "id", "auth");
2700 ast_xmpp_client_lock(client);
2701 ast_xmpp_increment_mid(client->mid);
2702 ast_xmpp_client_unlock(client);
2703 ast_xmpp_client_send(client, auth);
2707 iks_filter_add_rule(client->filter, xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "auth", IKS_RULE_DONE);
2713 /*! \brief Internal function called when we should authenticate as a component */
2714 static int xmpp_component_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2716 char secret[160], shasum[320], message[344];
2717 ikspak *pak = iks_packet(node);
2719 snprintf(secret, sizeof(secret), "%s%s", pak->id, cfg->password);
2720 ast_sha1_hash(shasum, secret);
2721 snprintf(message, sizeof(message), "<handshake>%s</handshake>", shasum);
2723 if (xmpp_client_send_raw_message(client, message) != IKS_OK) {
2724 ast_log(LOG_ERROR, "Unable to send handshake for component '%s'\n", client->name);
2728 xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
2733 /*! \brief Hook function called when component receives a service discovery get message */
2734 static int xmpp_component_service_discovery_get_hook(void *data, ikspak *pak)
2736 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2737 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2738 struct ast_xmpp_client *client = data;
2739 iks *iq = NULL, *query = NULL, *identity = NULL, *disco = NULL, *reg = NULL, *commands = NULL, *gateway = NULL;
2740 iks *version = NULL, *vcard = NULL, *search = NULL, *item = NULL;
2743 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
2744 !(iq = iks_new("iq")) || !(query = iks_new("query")) || !(identity = iks_new("identity")) || !(disco = iks_new("feature")) ||
2745 !(reg = iks_new("feature")) || !(commands = iks_new("feature")) || !(gateway = iks_new("feature")) || !(version = iks_new("feature")) ||
2746 !(vcard = iks_new("feature")) || !(search = iks_new("search")) || !(item = iks_new("item"))) {
2747 ast_log(LOG_ERROR, "Failed to allocate stanzas for service discovery get response to '%s' on component '%s'\n",
2748 pak->from->partial, client->name);
2752 iks_insert_attrib(iq, "from", clientcfg->user);
2753 iks_insert_attrib(iq, "to", pak->from->full);
2754 iks_insert_attrib(iq, "id", pak->id);
2755 iks_insert_attrib(iq, "type", "result");
2757 if (!(node = iks_find_attrib(pak->query, "node"))) {
2758 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
2759 iks_insert_attrib(identity, "category", "gateway");
2760 iks_insert_attrib(identity, "type", "pstn");
2761 iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
2762 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
2763 iks_insert_attrib(reg, "var", "jabber:iq:register");
2764 iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
2765 iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
2766 iks_insert_attrib(version, "var", "jabber:iq:version");
2767 iks_insert_attrib(vcard, "var", "vcard-temp");
2768 iks_insert_attrib(search, "var", "jabber:iq:search");
2770 iks_insert_node(iq, query);
2771 iks_insert_node(query, identity);
2772 iks_insert_node(query, disco);
2773 iks_insert_node(query, reg);
2774 iks_insert_node(query, commands);
2775 iks_insert_node(query, gateway);
2776 iks_insert_node(query, version);
2777 iks_insert_node(query, vcard);
2778 iks_insert_node(query, search);
2779 } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
2780 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
2781 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
2782 iks_insert_attrib(item, "node", "confirmaccount");
2783 iks_insert_attrib(item, "name", "Confirm account");
2784 iks_insert_attrib(item, "jid", clientcfg->user);
2786 iks_insert_node(iq, query);
2787 iks_insert_node(query, item);
2788 } else if (!strcasecmp(node, "confirmaccount")) {
2789 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
2790 iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
2792 iks_insert_node(iq, query);
2793 iks_insert_node(query, commands);
2795 ast_debug(3, "Unsupported service discovery info request received with node '%s' on component '%s'\n",
2796 node, client->name);
2800 if (ast_xmpp_client_send(client, iq)) {
2801 ast_log(LOG_WARNING, "Could not send response to service discovery request on component '%s'\n",
2808 iks_delete(version);
2809 iks_delete(gateway);
2810 iks_delete(commands);
2813 iks_delete(identity);
2817 return IKS_FILTER_EAT;
2820 /*! \brief Hook function called when the component is queried about registration */
2821 static int xmpp_component_register_get_hook(void *data, ikspak *pak)
2823 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2824 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2825 struct ast_xmpp_client *client = data;
2826 iks *iq = NULL, *query = NULL, *error = NULL, *notacceptable = NULL, *instructions = NULL;
2827 struct ast_xmpp_buddy *buddy;
2830 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
2831 !(iq = iks_new("iq")) || !(query = iks_new("query")) || !(error = iks_new("error")) || !(notacceptable = iks_new("not-acceptable")) ||
2832 !(instructions = iks_new("instructions"))) {
2833 ast_log(LOG_ERROR, "Failed to allocate stanzas for register get response to '%s' on component '%s'\n",
2834 pak->from->partial, client->name);
2838 iks_insert_attrib(iq, "from", clientcfg->user);
2839 iks_insert_attrib(iq, "to", pak->from->full);
2840 iks_insert_attrib(iq, "id", pak->id);
2841 iks_insert_attrib(iq, "type", "result");
2842 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
2843 iks_insert_node(iq, query);
2845 if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY))) {
2846 iks_insert_attrib(error, "code", "406");
2847 iks_insert_attrib(error, "type", "modify");
2848 iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
2850 iks_insert_node(iq, error);
2851 iks_insert_node(error, notacceptable);
2853 ast_log(LOG_ERROR, "Received register attempt from '%s' but buddy is not configured on component '%s'\n",
2854 pak->from->partial, client->name);
2855 } else if (!(node = iks_find_attrib(pak->query, "node"))) {
2856 iks_insert_cdata(instructions, "Welcome to Asterisk - the Open Source PBX.\n", 0);
2857 iks_insert_node(query, instructions);
2860 ast_log(LOG_WARNING, "Received register get to component '%s' using unsupported node '%s' from '%s'\n",
2861 client->name, node, pak->from->partial);
2866 if (ast_xmpp_client_send(client, iq)) {
2867 ast_log(LOG_WARNING, "Could not send response to '%s' for received register get on component '%s'\n",
2868 pak->from->partial, client->name);
2872 iks_delete(instructions);
2873 iks_delete(notacceptable);
2878 return IKS_FILTER_EAT;
2881 /*! \brief Hook function called when someone registers to the component */
2882 static int xmpp_component_register_set_hook(void *data, ikspak *pak)
2884 struct ast_xmpp_client *client = data;
2885 iks *iq, *presence = NULL, *x = NULL;
2887 if (!(iq = iks_new("iq")) || !(presence = iks_new("presence")) || !(x = iks_new("x"))) {
2888 ast_log(LOG_ERROR, "Failed to allocate stanzas for register set response to '%s' on component '%s'\n",
2889 pak->from->partial, client->name);
2893 iks_insert_attrib(iq, "from", client->jid->full);
2894 iks_insert_attrib(iq, "to", pak->from->full);
2895 iks_insert_attrib(iq, "id", pak->id);
2896 iks_insert_attrib(iq, "type", "result");
2898 if (ast_xmpp_client_send(client, iq)) {
2899 ast_log(LOG_WARNING, "Could not send response to '%s' for received register set on component '%s'\n",
2900 pak->from->partial, client->name);
2904 iks_insert_attrib(presence, "from", client->jid->full);
2905 iks_insert_attrib(presence, "to", pak->from->partial);
2906 ast_xmpp_client_lock(client);
2907 iks_insert_attrib(presence, "id", client->mid);
2908 ast_xmpp_increment_mid(client->mid);
2909 ast_xmpp_client_unlock(client);
2910 iks_insert_attrib(presence, "type", "subscribe");
2911 iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
2913 iks_insert_node(presence, x);
2915 if (ast_xmpp_client_send(client, presence)) {
2916 ast_log(LOG_WARNING, "Could not send subscription to '%s' on component '%s'\n",
2917 pak->from->partial, client->name);
2922 iks_delete(presence);
2925 return IKS_FILTER_EAT;
2928 /*! \brief Hook function called when we receive a service discovery items request */
2929 static int xmpp_component_service_discovery_items_hook(void *data, ikspak *pak)
2931 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
2932 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
2933 struct ast_xmpp_client *client = data;
2934 iks *iq = NULL, *query = NULL, *item = NULL, *feature = NULL;
2937 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
2938 !(iq = iks_new("iq")) || !(query = iks_new("query")) || !(item = iks_new("item")) || !(feature = iks_new("feature"))) {
2939 ast_log(LOG_ERROR, "Failed to allocate stanzas for service discovery items response to '%s' on component '%s'\n",
2940 pak->from->partial, client->name);
2944 iks_insert_attrib(iq, "from", clientcfg->user);
2945 iks_insert_attrib(iq, "to", pak->from->full);
2946 iks_insert_attrib(iq, "id", pak->id);
2947 iks_insert_attrib(iq, "type", "result");
2948 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
2949 iks_insert_node(iq, query);
2951 if (!(node = iks_find_attrib(pak->query, "node"))) {
2952 iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
2953 iks_insert_attrib(item, "name", "Asterisk Commands");
2954 iks_insert_attrib(item, "jid", clientcfg->user);
2956 iks_insert_node(query, item);
2957 } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
2958 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
2960 ast_log(LOG_WARNING, "Received service discovery items request to component '%s' using unsupported node '%s' from '%s'\n",
2961 client->name, node, pak->from->partial);
2965 if (ast_xmpp_client_send(client, iq)) {
2966 ast_log(LOG_WARNING, "Could not send response to service discovery items request from '%s' on component '%s'\n",
2967 pak->from->partial, client->name);
2971 iks_delete(feature);
2976 return IKS_FILTER_EAT;
2979 /*! \brief Internal function called when we authenticated as a component */
2980 static int xmpp_component_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
2982 if (strcmp(iks_name(node), "handshake")) {
2983 ast_log(LOG_ERROR, "Failed to authenticate component '%s'\n", client->name);
2987 iks_filter_add_rule(client->filter, xmpp_component_service_discovery_items_hook, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
2989 iks_filter_add_rule(client->filter, xmpp_component_service_discovery_get_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2991 /* This uses the client service discovery result hook on purpose, as the code is common between both */
2992 iks_filter_add_rule(client->filter, xmpp_client_service_discovery_result_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2994 iks_filter_add_rule(client->filter, xmpp_component_register_get_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
2995 iks_filter_add_rule(client->filter, xmpp_component_register_set_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_SET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
2997 xmpp_client_change_state(client, XMPP_STATE_CONNECTED);
3002 /*! \brief Internal function called when a message is received */
3003 static int xmpp_pak_message(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak)
3005 struct ast_xmpp_message *message;
3009 ast_debug(3, "XMPP client '%s' received a message\n", client->name);
3011 if (!(body = iks_find_cdata(pak->x, "body"))) {
3012 /* Message contains no body, ignore it. */
3016 if (!(message = ast_calloc(1, sizeof(*message)))) {
3020 message->arrived = ast_tvnow();
3022 message->message = ast_strdup(body);
3024 ast_copy_string(message->id, S_OR(pak->id, ""), sizeof(message->id));
3025 message->from = !ast_strlen_zero(pak->from->full) ? ast_strdup(pak->from->full) : NULL;
3027 if (ast_test_flag(&cfg->flags, XMPP_SEND_TO_DIALPLAN)) {
3028 struct ast_msg *msg;
3030 if ((msg = ast_msg_alloc())) {
3033 ast_xmpp_client_lock(client);
3035 res = ast_msg_set_to(msg, "xmpp:%s", cfg->user);
3036 res |= ast_msg_set_from(msg, "xmpp:%s", message->from);
3037 res |= ast_msg_set_body(msg, "%s", message->message);
3038 res |= ast_msg_set_context(msg, "%s", cfg->context);
3040 ast_xmpp_client_unlock(client);
3043 ast_msg_destroy(msg);
3050 /* remove old messages received from this JID
3051 * and insert received message */
3052 deleted = delete_old_messages(client, pak->from->partial);
3053 ast_debug(3, "Deleted %d messages for client %s from JID %s\n", deleted, client->name, pak->from->partial);
3054 AST_LIST_LOCK(&client->messages);
3055 AST_LIST_INSERT_HEAD(&client->messages, message, list);
3056 AST_LIST_UNLOCK(&client->messages);
3058 /* wake up threads waiting for messages */
3059 ast_mutex_lock(&messagelock);
3060 ast_cond_broadcast(&message_received_condition);
3061 ast_mutex_unlock(&messagelock);
3066 /*! \brief Helper function which sends a discovery information request to a user */
3067 static int xmpp_client_send_disco_info_request(struct ast_xmpp_client *client, const char *to, const char *from)
3072 if (!(iq = iks_new("iq")) || !(query = iks_new("query"))) {
3077 iks_insert_attrib(iq, "type", "get");
3078 iks_insert_attrib(iq, "to", to);
3079 iks_insert_attrib(iq, "from", from);
3080 ast_xmpp_client_lock(client);
3081 iks_insert_attrib(iq, "id", client->mid);
3082 ast_xmpp_increment_mid(client->mid);
3083 ast_xmpp_client_unlock(client);
3084 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
3085 iks_insert_node(iq, query);
3087 res = ast_xmpp_client_send(client, iq);
3095 /*! \brief Internal function called when a presence message is received */
3096 static int xmpp_pak_presence(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak)
3098 struct ast_xmpp_buddy *buddy;
3099 struct ast_xmpp_resource *resource;
3100 char *type = iks_find_attrib(pak->x, "type");
3101 int status = pak->show ? pak->show : STATUS_DISAPPEAR;
3103 /* If no resource is available this is a general buddy presence update, which we will ignore */
3104 if (!pak->from->resource) {
3108 if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY))) {
3109 /* Only output the message if it is not about us */
3110 if (strcmp(client->jid->partial, pak->from->partial)) {
3111 ast_log(LOG_WARNING, "Received presence information about '%s' despite not having them in roster on client '%s'\n",
3112 pak->from->partial, client->name);
3117 /* If this is a component presence probe request answer immediately with our presence status */
3118 if (ast_test_flag(&cfg->flags, XMPP_COMPONENT) && !ast_strlen_zero(type) && !strcasecmp(type, "probe")) {
3119 xmpp_client_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), cfg->status, cfg->statusmsg);
3122 ao2_lock(buddy->resources);
3124 if (!(resource = ao2_find(buddy->resources, pak->from->resource, OBJ_KEY | OBJ_NOLOCK))) {
3125 /* Only create the new resource if it is not going away - in reality this should not happen */
3126 if (status != STATUS_DISAPPEAR) {
3127 if (!(resource = ao2_alloc(sizeof(*resource), xmpp_resource_destructor))) {
3128 ast_log(LOG_ERROR, "Could not allocate resource object for resource '%s' of buddy '%s' on client '%s'\n",
3129 pak->from->resource, buddy->id, client->name);
3130 ao2_unlock(buddy->resources);
3135 ast_copy_string(resource->resource, pak->from->resource, sizeof(resource->resource));
3138 /* We unlink the resource in case the priority changes or in case they are going away */
3139 ao2_unlink_flags(buddy->resources, resource, OBJ_NOLOCK);
3142 /* Only update the resource and add it back in if it is not going away */
3143 if (resource && (status != STATUS_DISAPPEAR)) {
3146 /* Try to get the XMPP spec node, and fall back to Google if not found */
3147 if (!(node = iks_find_attrib(iks_find(pak->x, "c"), "node"))) {
3148 node = iks_find_attrib(iks_find(pak->x, "caps:c"), "node");
3151 if (!(ver = iks_find_attrib(iks_find(pak->x, "c"), "ver"))) {
3152 ver = iks_find_attrib(iks_find(pak->x, "caps:c"), "ver");
3155 if (resource->description) {
3156 ast_free(resource->description);
3159 if ((node && strcmp(resource->caps.node, node)) || (ver && strcmp(resource->caps.version, ver))) {
3160 ast_copy_string(resource->caps.node, node, sizeof(resource->caps.node));
3161 ast_copy_string(resource->caps.version, ver, sizeof(resource->caps.version));
3163 /* Google Talk places the capabilities information directly in presence, so see if it is there */
3164 if (iks_find_with_attrib(pak->x, "c", "node", "http://www.google.com/xmpp/client/caps") ||
3165 iks_find_with_attrib(pak->x, "caps:c", "node", "http://www.google.com/xmpp/client/caps") ||
3166 iks_find_with_attrib(pak->x, "c", "node", "http://www.android.com/gtalk/client/caps") ||
3167 iks_find_with_attrib(pak->x, "caps:c", "node", "http://www.android.com/gtalk/client/caps") ||
3168 iks_find_with_attrib(pak->x, "c", "node", "http://mail.google.com/xmpp/client/caps") ||
3169 iks_find_with_attrib(pak->x, "caps:c", "node", "http://mail.google.com/xmpp/client/caps")) {
3170 resource->caps.google = 1;
3173 /* To discover if the buddy supports Jingle we need to query, so do so */
3174 if (xmpp_client_send_disco_info_request(client, pak->from->full, client->jid->full)) {
3175 ast_log(LOG_WARNING, "Could not send discovery information request to resource '%s' of buddy '%s' on client '%s', capabilities may be incomplete\n", resource->resource, buddy->id, client->name);
3179 resource->status = status;
3180 resource->description = ast_strdup(iks_find_cdata(pak->x, "status"));
3181 resource->priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
3183 ao2_link_flags(buddy->resources, resource, OBJ_NOLOCK);
3185 manager_event(EVENT_FLAG_USER, "JabberStatus",
3186 "Account: %s\r\nJID: %s\r\nResource: %s\r\nStatus: %d\r\nPriority: %d"
3187 "\r\nDescription: %s\r\n",
3188 client->name, pak->from->partial, resource->resource, resource->status,
3189 resource->priority, S_OR(resource->description, ""));
3191 ao2_ref(resource, -1);
3193 /* This will get hit by presence coming in for an unknown resource, and also when a resource goes away */
3195 ao2_ref(resource, -1);
3198 manager_event(EVENT_FLAG_USER, "JabberStatus",
3199 "Account: %s\r\nJID: %s\r\nStatus: %d\r\n",
3200 client->name, pak->from->partial, pak->show ? pak->show : IKS_SHOW_UNAVAILABLE);
3203 ao2_unlock(buddy->resources);
3210 /*! \brief Internal function called when a subscription message is received */
3211 static int xmpp_pak_s10n(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg,iks *node, ikspak *pak)
3213 struct ast_xmpp_buddy *buddy;
3215 switch (pak->subtype) {
3216 case IKS_TYPE_SUBSCRIBE:
3217 if (ast_test_flag(&cfg->flags, XMPP_AUTOREGISTER)) {
3218 iks *presence, *status = NULL;
3220 if ((presence = iks_new("presence")) && (status = iks_new("status"))) {
3221 iks_insert_attrib(presence, "type", "subscribed");
3222 iks_insert_attrib(presence, "to", pak->from->full);
3223 iks_insert_attrib(presence, "from", client->jid->full);
3226 iks_insert_attrib(presence, "id", pak->id);
3229 iks_insert_cdata(status, "Asterisk has approved your subscription", 0);
3230 iks_insert_node(presence, status);
3232 if (ast_xmpp_client_send(client, presence)) {
3233 ast_log(LOG_ERROR, "Could not send subscription acceptance to '%s' from client '%s'\n",
3234 pak->from->partial, client->name);
3237 ast_log(LOG_ERROR, "Could not allocate presence stanzas for accepting subscription from '%s' to client '%s'\n",
3238 pak->from->partial, client->name);
3242 iks_delete(presence);
3245 if (ast_test_flag(&cfg->flags, XMPP_COMPONENT)) {
3246 xmpp_client_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), cfg->status, cfg->statusmsg);
3248 /* This purposely flows through so we have the subscriber amongst our buddies */
3249 case IKS_TYPE_SUBSCRIBED:
3250 ao2_lock(client->buddies);
3252 if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY | OBJ_NOLOCK))) {
3253 buddy = xmpp_client_create_buddy(client->buddies, pak->from->partial);
3257 ast_log(LOG_WARNING, "Could not find or create buddy '%s' on client '%s'\n",
3258 pak->from->partial, client->name);
3263 ao2_unlock(client->buddies);
3273 /*! \brief Action hook for when things occur */
3274 static int xmpp_action_hook(void *data, int type, iks *node)
3276 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
3277 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
3278 struct ast_xmpp_client *client = data;
3283 ast_log(LOG_ERROR, "xmpp_action_hook was called without a packet\n");
3287 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
3291 /* If the client is disconnecting ignore everything */
3292 if (client->state == XMPP_STATE_DISCONNECTING) {
3296 pak = iks_packet(node);
3298 /* work around iksemel's impossibility to recognize node names
3299 * containing a colon. Set the namespace of the corresponding
3300 * node accordingly. */
3301 if (iks_has_children(node) && strchr(iks_name(iks_child(node)), ':')) {
3302 char *node_ns = NULL;
3303 char attr[XMPP_MAX_ATTRLEN];
3304 char *node_name = iks_name(iks_child(node));
3305 char *aux = strchr(node_name, ':') + 1;
3306 snprintf(attr, strlen("xmlns:") + (strlen(node_name) - strlen(aux)), "xmlns:%s", node_name);
3307 node_ns = iks_find_attrib(iks_child(node), attr);
3310 pak->query = iks_child(node);
3314 /* Process through any state handlers */
3315 for (i = 0; i < ARRAY_LEN(xmpp_state_handlers); i++) {
3316 if ((xmpp_state_handlers[i].state == client->state) && (xmpp_state_handlers[i].component == (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) ? 1 : 0))) {
3317 if (xmpp_state_handlers[i].handler(client, clientcfg, type, node)) {
3318 /* If the handler wants us to stop now, do so */
3325 /* Process through any PAK handlers */
3326 for (i = 0; i < ARRAY_LEN(xmpp_pak_handlers); i++) {
3327 if (xmpp_pak_handlers[i].type == pak->type) {
3328 if (xmpp_pak_handlers[i].handler(client, clientcfg, node, pak)) {
3329 /* If the handler wants us to stop now, do so */
3336 /* Send the packet through the filter in case any filters want to process it */
3337 iks_filter_packet(client->filter, pak);
3344 int ast_xmpp_client_disconnect(struct ast_xmpp_client *client)
3346 if ((client->thread != AST_PTHREADT_NULL) && !pthread_equal(pthread_self(), client->thread)) {
3347 client->state = XMPP_STATE_DISCONNECTING;
3348 pthread_join(client->thread, NULL);
3349 client->thread = AST_PTHREADT_NULL;
3352 if (client->mwi_sub) {
3353 ast_event_unsubscribe(client->mwi_sub);
3354 client->mwi_sub = NULL;
3355 xmpp_pubsub_unsubscribe(client, "message_waiting");
3358 if (client->device_state_sub) {
3359 ast_event_unsubscribe(client->device_state_sub);
3360 client->device_state_sub = NULL;
3361 xmpp_pubsub_unsubscribe(client, "device_state");
3365 if (client->stream_flags & SECURE) {
3366 SSL_shutdown(client->ssl_session);
3367 SSL_CTX_free(client->ssl_context);
3368 SSL_free(client->ssl_session);
3371 client->stream_flags = 0;
3374 if (client->parser) {
3375 iks_disconnect(client->parser);
3378 /* Disconnecting the parser and going back to a disconnected state means any hooks should no longer be present */
3379 if (client->filter) {
3380 iks_filter_delete(client->filter);
3381 client->filter = NULL;
3384 client->state = XMPP_STATE_DISCONNECTED;
3389 /*! \brief Internal function used to reconnect an XMPP client to its server */
3390 static int xmpp_client_reconnect(struct ast_xmpp_client *client)
3392 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
3393 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
3394 int res = IKS_NET_NOCONN;
3396 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
3400 ast_xmpp_client_disconnect(client);
3402 client->timeout = 50;
3403 iks_parser_reset(client->parser);
3405 if (!client->filter && !(client->filter = iks_filter_new())) {
3406 ast_log(LOG_ERROR, "Could not create IKS filter for client connection '%s'\n", client->name);
3410 /* If it's a component connect to user otherwise connect to server */
3411 res = iks_connect_via(client->parser, S_OR(clientcfg->server, client->jid->server), clientcfg->port,
3412 ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) ? clientcfg->user : client->jid->server);
3414 if (res == IKS_NET_NOCONN) {
3415 ast_log(LOG_ERROR, "No XMPP connection available when trying to connect client '%s'\n", client->name);
3417 } else if (res == IKS_NET_NODNS) {
3418 ast_log(LOG_ERROR, "No DNS available for XMPP connection when trying to connect client '%s'\n", client->name);
3422 /* Depending on the configuration of the client we eiher jump to requesting TLS, or authenticating */
3423 xmpp_client_change_state(client, (ast_test_flag(&clientcfg->flags, XMPP_USETLS) ? XMPP_STATE_REQUEST_TLS : XMPP_STATE_AUTHENTICATE));
3428 /*! \brief Internal function which polls on an XMPP client and receives data */
3429 static int xmpp_io_recv(struct ast_xmpp_client *client, char *buffer, size_t buf_len, int timeout)
3431 struct pollfd pfd = { .events = POLLIN };
3435 if (xmpp_is_secure(client)) {
3436 pfd.fd = SSL_get_fd(client->ssl_session);
3441 #endif /* HAVE_OPENSSL */
3442 pfd.fd = iks_fd(client->parser);
3444 res = ast_poll(&pfd, 1, timeout > 0 ? timeout * 1000 : -1);
3447 if (xmpp_is_secure(client)) {
3448 len = SSL_read(client->ssl_session, buffer, buf_len);
3450 #endif /* HAVE_OPENSSL */
3451 len = recv(pfd.fd, buffer, buf_len, 0);
3455 } else if (len <= 0) {
3462 /*! \brief Internal function which receives data from the XMPP client connection */
3463 static int xmpp_client_receive(struct ast_xmpp_client *client, unsigned int timeout)
3465 int len, ret, pos = 0, newbufpos = 0;
3466 char buf[NET_IO_BUF_SIZE - 1] = "";
3467 char newbuf[NET_IO_BUF_SIZE - 1] = "";
3471 len = xmpp_io_recv(client, buf, NET_IO_BUF_SIZE - 2, timeout);
3472 if (len < 0) return IKS_NET_RWERR;
3473 if (len == 0) return IKS_NET_EXPIRED;
3476 /* our iksemel parser won't work as expected if we feed
3477 it with XML packets that contain multiple whitespace
3478 characters between tags */
3481 /* if we stumble on the ending tag character,
3482 we skip any whitespace that follows it*/
3484 while (isspace(buf[pos+1])) {
3488 newbuf[newbufpos] = c;
3495 /* Log the message here, because iksemel's logHook is
3497 xmpp_log_hook(client, buf, len, 1);
3499 /* let iksemel deal with the string length,
3500 and reset our buffer */
3501 ret = iks_parse(client->parser, newbuf, 0, 0);
3502 memset(newbuf, 0, sizeof(newbuf));
3506 ast_log(LOG_WARNING, "Parsing failure: Out of memory.\n");
3509 ast_log(LOG_WARNING, "Parsing failure: Invalid XML.\n");
3512 ast_log(LOG_WARNING, "Parsing failure: Hook returned an error.\n");
3515 if (ret != IKS_OK) {
3518 ast_debug(3, "XML parsing successful\n");
3523 /*! \brief XMPP client connection thread */
3524 static void *xmpp_client_thread(void *data)
3526 struct ast_xmpp_client *client = data;
3527 int res = IKS_NET_RWERR;
3530 if (client->state == XMPP_STATE_DISCONNECTING) {
3534 if (res == IKS_NET_RWERR || client->timeout == 0) {
3535 ast_debug(3, "Connecting client '%s'\n", client->name);
3536 if ((res = xmpp_client_reconnect(client)) != IKS_OK) {
3538 res = IKS_NET_RWERR;
3543 res = xmpp_client_receive(client, 1);
3545 /* Decrease timeout if no data received, and delete
3546 * old messages globally */
3547 if (res == IKS_NET_EXPIRED) {
3551 if (res == IKS_HOOK) {
3552 ast_debug(2, "JABBER: Got hook event.\n");
3553 } else if (res == IKS_NET_TLSFAIL) {
3554 ast_log(LOG_ERROR, "JABBER: Failure in TLS.\n");
3555 } else if (!client->timeout && client->state == XMPP_STATE_CONNECTED) {
3556 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
3557 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
3559 if (cfg && cfg->clients && (clientcfg = xmpp_config_find(cfg->clients, client->name))) {
3560 res = ast_test_flag(&clientcfg->flags, XMPP_KEEPALIVE) ? xmpp_client_send_raw_message(client, " ") : IKS_OK;
3565 if (res == IKS_OK) {
3566 client->timeout = 50;
3568 ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
3570 } else if (res == IKS_NET_RWERR) {
3571 ast_log(LOG_WARNING, "JABBER: socket read error\n");
3579 static int xmpp_client_config_merge_buddies(void *obj, void *arg, int flags)
3581 struct ast_xmpp_buddy *buddy1 = obj, *buddy2;
3582 struct ao2_container *buddies = arg;
3584 /* If the buddy does not already exist link it into the client buddies container */
3585 if (!(buddy2 = ao2_find(buddies, buddy1->id, OBJ_KEY))) {
3586 ao2_link(buddies, buddy1);
3588 ao2_ref(buddy2, -1);
3591 /* All buddies are unlinked from the configuration buddies container, always */
3595 static int xmpp_client_config_post_apply(void *obj, void *arg, int flags)
3597 struct ast_xmpp_client_config *cfg = obj;
3599 /* Merge buddies as need be */
3600 ao2_callback(cfg->buddies, OBJ_MULTIPLE | OBJ_UNLINK, xmpp_client_config_merge_buddies, cfg->client->buddies);
3602 if (cfg->client->reconnect) {
3603 /* Disconnect the existing session since our role is changing, or we are starting up */
3604 ast_xmpp_client_disconnect(cfg->client);
3606 if (!(cfg->client->parser = iks_stream_new(ast_test_flag(&cfg->flags, XMPP_COMPONENT) ? "jabber:component:accept" : "jabber:client", cfg->client,
3607 xmpp_action_hook))) {
3608 ast_log(LOG_ERROR, "Iksemel stream could not be created for client '%s' - client not active\n", cfg->name);
3612 iks_set_log_hook(cfg->client->parser, xmpp_log_hook);
3614 /* Create a JID based on the given user, if no resource is given use the default */
3615 if (!strchr(cfg->user, '/') && !ast_test_flag(&cfg->flags, XMPP_COMPONENT)) {
3616 char resource[strlen(cfg->user) + strlen("/asterisk-xmpp") + 1];
3618 snprintf(resource, sizeof(resource), "%s/asterisk-xmpp", cfg->user);
3619 cfg->client->jid = iks_id_new(cfg->client->stack, resource);
3621 cfg->client->jid = iks_id_new(cfg->client->stack, cfg->user);
3624 if (!cfg->client->jid) {
3625 ast_log(LOG_ERROR, "Jabber identity could not be created for client '%s' - client not active\n", cfg->name);
3629 ast_pthread_create_background(&cfg->client->thread, NULL, xmpp_client_thread, cfg->client);
3631 cfg->client->reconnect = 0;
3632 } else if (cfg->client->state == XMPP_STATE_CONNECTED) {
3633 /* If this client is connected update their presence status since it may have changed */
3634 xmpp_client_set_presence(cfg->client, NULL, cfg->client->jid->full, cfg->status, cfg->statusmsg);
3636 /* Subscribe to the status of any newly added buddies */
3637 if (ast_test_flag(&cfg->flags, XMPP_AUTOREGISTER)) {
3638 ao2_callback(cfg->client->buddies, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_subscribe_user, cfg->client);
3647 * \brief Send a Jabber Message via call from the Manager
3648 * \param s mansession Manager session
3649 * \param m message Message to send
3652 static int manager_jabber_send(struct mansession *s, const struct message *m)
3654 RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
3655 RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
3656 const char *id = astman_get_header(m, "ActionID");
3657 const char *jabber = astman_get_header(m, "Jabber");
3658 const char *screenname = astman_get_header(m, "ScreenName");
3659 const char *message = astman_get_header(m, "Message");
3661 if (ast_strlen_zero(jabber)) {
3662 astman_send_error(s, m, "No transport specified");
3665 if (ast_strlen_zero(screenname)) {
3666 astman_send_error(s, m, "No ScreenName specified");
3669 if (ast_strlen_zero(message)) {
3670 astman_send_error(s, m, "No Message specified");
3674 astman_send_ack(s, m, "Attempting to send Jabber Message");
3676 if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, jabber))) {
3677 astman_send_error(s, m, "Could not find Sender");
3681 if (strchr(screenname, '@') && !ast_xmpp_client_send_message(clientcfg->client, screenname, message)) {
3682 astman_append(s, "Response: Success\r\n");
3684 astman_append(s, "Response: Error\r\n");
3687 if (!ast_strlen_zero(id)) {
3688 astman_append(s, "ActionID: %s\r\n", id);
3691 astman_append(s, "\r\n");
3697 * \brief Build the a node request
3698 * \param client the configured XMPP client we use to connect to a XMPP server
3699 * \param collection name of the collection for request
3702 static iks* xmpp_pubsub_build_node_request(struct ast_xmpp_client *client, const char *collection)
3704 iks *request = xmpp_pubsub_iq_create(client, "get"), *query;
3710 query = iks_insert(request, "query");
3711 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
3714 iks_insert_attrib(query, "node", collection);
3721 * \brief Receive pubsub item lists
3722 * \param data pointer to ast_xmpp_client structure
3723 * \param pak response from pubsub diso#items query
3724 * \return IKS_FILTER_EAT
3726 static int xmpp_pubsub_receive_node_list(void *data, ikspak* pak)
3728 struct ast_xmpp_client *client = data;
3731 if (iks_has_children(pak->query)) {
3732 item = iks_first_tag(pak->query);
3733 ast_verbose("Connection %s: %s\nNode name: %s\n", client->name, client->jid->partial,
3734 iks_find_attrib(item, "node"));
3735 while ((item = iks_next_tag(item))) {
3736 ast_verbose("Node name: %s\n", iks_find_attrib(item, "node"));
3745 return IKS_FILTER_EAT;
3749 * \brief Request item list from pubsub
3750 * \param client the configured XMPP client we use to connect to a XMPP server
3751 * \param collection name of the collection for request
3754 static void xmpp_pubsub_request_nodes(struct ast_xmpp_client *client, const char *collection)
3756 iks *request = xmpp_pubsub_build_node_request(client, collection);
3759 ast_log(LOG_ERROR, "Could not request pubsub nodes on client '%s' - IQ could not be created\n", client->name);
3763 iks_filter_add_rule(client->filter, xmpp_pubsub_receive_node_list, client, IKS_RULE_TYPE,
3764 IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid,