2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Matt O'Gorman <mogorman@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.
20 * \brief A resource for interfacing Asterisk directly as a client
21 * or a component to a XMPP/Jabber compliant server.
24 * - http://www.xmpp.org - The XMPP standards foundation
26 * \extref Iksemel http://code.google.com/p/iksemel/
28 * \todo If you unload this module, chan_gtalk/jingle will be dead. How do we handle that?
29 * \todo Dialplan applications need RETURN variable, like JABBERSENDSTATUS
34 <depend>iksemel</depend>
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
45 #include "asterisk/channel.h"
46 #include "asterisk/jabber.h"
47 #include "asterisk/file.h"
48 #include "asterisk/config.h"
49 #include "asterisk/callerid.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/app.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/md5.h"
55 #include "asterisk/acl.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/module.h"
58 #include "asterisk/astobj.h"
59 #include "asterisk/astdb.h"
60 #include "asterisk/manager.h"
63 <application name="JabberSend" language="en_US">
68 <parameter name="Jabber" required="true">
69 <para>Client or transport Asterisk uses to connect to Jabber.</para>
71 <parameter name="JID" required="true">
72 <para>XMPP/Jabber JID (Name) of recipient.</para>
74 <parameter name="Message" required="true">
75 <para>Message to be sent to the buddy.</para>
79 <para>Allows user to send a message to a receipent via XMPP.</para>
82 <application name="JabberStatus" language="en_US">
84 Retrieve the status of a jabber list member
87 <parameter name="Jabber" required="true">
88 <para>Client or transport Asterisk users to connect to Jabber.</para>
90 <parameter name="JID" required="true">
91 <para>XMPP/Jabber JID (Name) of recipient.</para>
93 <parameter name="Variable" required="true">
94 <para>Variable to store the status of requested user.</para>
98 <para>This application is deprecated. Please use the JABBER_STATUS() function instead.</para>
99 <para>Retrieves the numeric status associated with the specified buddy <replaceable>JID</replaceable>.
100 The return value in the <replaceable>Variable</replaceable>will be one of the following.</para>
112 <para>Extended Away.</para>
115 <para>Do Not Disturb.</para>
118 <para>Offline.</para>
121 <para>Not In Roster.</para>
126 <function name="JABBER_STATUS" language="en_US">
128 Retrieve the status of a jabber list member
131 <parameter name="sender" required="true">
132 <para>XMPP/Jabber ID (Name) of sender.</para>
134 <parameter name="buddy" required="true">
135 <para>XMPP/Jabber JID (Name) of recipient.</para>
137 <parameter name="resource">
138 <para>Client or transport Asterisk users to connect to Jabber.</para>
142 <para>Retrieves the numeric status associated with the specified buddy <replaceable>JID</replaceable>.
143 The return value will be one of the following.</para>
155 <para>Extended Away.</para>
158 <para>Do Not Disturb.</para>
161 <para>Offline.</para>
164 <para>Not In Roster.</para>
171 /*! \todo This should really be renamed to xmpp.conf. For backwards compatibility, we
172 need to read both files */
173 #define JABBER_CONFIG "jabber.conf"
183 /*-- Forward declarations */
184 static void aji_buddy_destroy(struct aji_buddy *obj);
185 static void aji_client_destroy(struct aji_client *obj);
186 static int aji_is_secure(struct aji_client *client);
188 static int aji_start_tls(struct aji_client *client);
189 static int aji_tls_handshake(struct aji_client *client);
191 static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout);
192 static int aji_recv(struct aji_client *client, int timeout);
193 static int aji_send_header(struct aji_client *client, const char *to);
194 static int aji_send_raw(struct aji_client *client, const char *xmlstr);
195 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming);
196 static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass);
197 static int aji_act_hook(void *data, int type, iks *node);
198 static void aji_handle_iq(struct aji_client *client, iks *node);
199 static void aji_handle_message(struct aji_client *client, ikspak *pak);
200 static void aji_handle_presence(struct aji_client *client, ikspak *pak);
201 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak);
202 static void *aji_recv_loop(void *data);
203 static int aji_initialize(struct aji_client *client);
204 static int aji_client_connect(void *data, ikspak *pak);
205 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc);
206 static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
207 static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
208 static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
209 static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
210 static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
211 static int aji_create_client(char *label, struct ast_variable *var, int debug);
212 static int aji_create_buddy(char *label, struct aji_client *client);
213 static int aji_reload(int reload);
214 static int aji_load_config(int reload);
215 static void aji_pruneregister(struct aji_client *client);
216 static int aji_filter_roster(void *data, ikspak *pak);
217 static int aji_get_roster(struct aji_client *client);
218 static int aji_client_info_handler(void *data, ikspak *pak);
219 static int aji_dinfo_handler(void *data, ikspak *pak);
220 static int aji_ditems_handler(void *data, ikspak *pak);
221 static int aji_register_query_handler(void *data, ikspak *pak);
222 static int aji_register_approve_handler(void *data, ikspak *pak);
223 static int aji_reconnect(struct aji_client *client);
224 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
225 /* No transports in this version */
227 static int aji_create_transport(char *label, struct aji_client *client);
228 static int aji_register_transport(void *data, ikspak *pak);
229 static int aji_register_transport2(void *data, ikspak *pak);
232 static struct ast_cli_entry aji_cli[] = {
233 AST_CLI_DEFINE(aji_do_set_debug, "Enable/Disable Jabber debug"),
234 AST_CLI_DEFINE(aji_do_reload, "Reload Jabber configuration"),
235 AST_CLI_DEFINE(aji_show_clients, "Show state of clients and components"),
236 AST_CLI_DEFINE(aji_show_buddies, "Show buddy lists of our clients"),
237 AST_CLI_DEFINE(aji_test, "Shows roster, but is generally used for mog's debugging."),
240 static char *app_ajisend = "JabberSend";
242 static char *app_ajistatus = "JabberStatus";
244 struct aji_client_container clients;
245 struct aji_capabilities *capabilities = NULL;
247 /*! \brief Global flags, initialized to default values */
248 static struct ast_flags globalflags = { AJI_AUTOREGISTER };
251 * \brief Deletes the aji_client data structure.
252 * \param obj aji_client The structure we will delete.
255 static void aji_client_destroy(struct aji_client *obj)
257 struct aji_message *tmp;
258 ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, aji_buddy_destroy);
259 ASTOBJ_CONTAINER_DESTROY(&obj->buddies);
260 iks_filter_delete(obj->f);
261 iks_parser_delete(obj->p);
262 iks_stack_delete(obj->stack);
263 AST_LIST_LOCK(&obj->messages);
264 while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) {
268 ast_free(tmp->message);
270 AST_LIST_HEAD_DESTROY(&obj->messages);
275 * \brief Deletes the aji_buddy data structure.
276 * \param obj aji_buddy The structure we will delete.
279 static void aji_buddy_destroy(struct aji_buddy *obj)
281 struct aji_resource *tmp;
283 while ((tmp = obj->resources)) {
284 obj->resources = obj->resources->next;
285 ast_free(tmp->description);
293 * \brief Find version in XML stream and populate our capabilities list
294 * \param node the node attribute in the caps element we'll look for or add to
296 * \param version the version attribute in the caps element we'll look for or
298 * \param pak struct The XML stanza we're processing
299 * \return a pointer to the added or found aji_version structure
301 static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
303 struct aji_capabilities *list = NULL;
304 struct aji_version *res = NULL;
309 node = pak->from->full;
311 version = "none supplied.";
313 if(!strcasecmp(list->node, node)) {
314 res = list->versions;
316 if(!strcasecmp(res->version, version))
320 /* Specified version not found. Let's add it to
321 this node in our capabilities list */
323 res = ast_malloc(sizeof(*res));
325 ast_log(LOG_ERROR, "Out of memory!\n");
330 ast_copy_string(res->version, version, sizeof(res->version));
331 res->next = list->versions;
332 list->versions = res;
338 /* Specified node not found. Let's add it our capabilities list */
340 list = ast_malloc(sizeof(*list));
342 ast_log(LOG_ERROR, "Out of memory!\n");
345 res = ast_malloc(sizeof(*res));
347 ast_log(LOG_ERROR, "Out of memory!\n");
351 ast_copy_string(list->node, node, sizeof(list->node));
352 ast_copy_string(res->version, version, sizeof(res->version));
356 list->versions = res;
357 list->next = capabilities;
363 * \brief Find the aji_resource we want
364 * \param buddy aji_buddy A buddy
366 * \return aji_resource object
368 static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *name)
370 struct aji_resource *res = NULL;
373 res = buddy->resources;
375 if (!strcasecmp(res->resource, name)) {
384 * \brief Jabber GTalk function
386 * \return 1 on success, 0 on failure.
388 static int gtalk_yuck(iks *node)
390 if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps"))
396 * \brief Setup the authentication struct
398 * \param pass password
402 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid)
406 iks_insert_attrib(x, "type", "set");
407 y = iks_insert(x, "query");
408 iks_insert_attrib(y, "xmlns", IKS_NS_AUTH);
409 iks_insert_cdata(iks_insert(y, "username"), id->user, 0);
410 iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0);
414 snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass);
415 ast_sha1_hash(buf, sidpass);
416 iks_insert_cdata(iks_insert(y, "digest"), buf, 0);
418 iks_insert_cdata(iks_insert(y, "password"), pass, 0);
424 * \brief Dial plan function status(). puts the status of watched user
425 into a channel variable.
426 * \param chan ast_channel
428 * \return 0 on success, -1 on error
430 static int aji_status_exec(struct ast_channel *chan, const char *data)
432 struct aji_client *client = NULL;
433 struct aji_buddy *buddy = NULL;
434 struct aji_resource *r = NULL;
438 static int deprecation_warning = 0;
439 AST_DECLARE_APP_ARGS(args,
442 AST_APP_ARG(variable);
444 AST_DECLARE_APP_ARGS(jid,
445 AST_APP_ARG(screenname);
446 AST_APP_ARG(resource);
449 if (deprecation_warning++ % 10 == 0)
450 ast_log(LOG_WARNING, "JabberStatus is deprecated. Please use the JABBER_STATUS dialplan function in the future.\n");
453 ast_log(LOG_ERROR, "Usage: JabberStatus(<sender>,<jid>[/<resource>],<varname>\n");
456 s = ast_strdupa(data);
457 AST_STANDARD_APP_ARGS(args, s);
459 if (args.argc != 3) {
460 ast_log(LOG_ERROR, "JabberStatus() requires 3 arguments.\n");
464 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
466 if (!(client = ast_aji_get_client(args.sender))) {
467 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
470 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
472 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
475 r = aji_find_resource(buddy, jid.resource);
476 if (!r && buddy->resources)
477 r = buddy->resources;
479 ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
482 snprintf(status, sizeof(status), "%d", stat);
483 pbx_builtin_setvar_helper(chan, args.variable, status);
487 static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
489 struct aji_client *client = NULL;
490 struct aji_buddy *buddy = NULL;
491 struct aji_resource *r = NULL;
493 AST_DECLARE_APP_ARGS(args,
497 AST_DECLARE_APP_ARGS(jid,
498 AST_APP_ARG(screenname);
499 AST_APP_ARG(resource);
503 ast_log(LOG_ERROR, "Usage: JABBER_STATUS(<sender>,<jid>[/<resource>])\n");
506 AST_STANDARD_APP_ARGS(args, data);
508 if (args.argc != 2) {
509 ast_log(LOG_ERROR, "JABBER_STATUS requires 2 arguments: sender and jid.\n");
513 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
515 if (!(client = ast_aji_get_client(args.sender))) {
516 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
519 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
521 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
524 r = aji_find_resource(buddy, jid.resource);
525 if (!r && buddy->resources)
526 r = buddy->resources;
528 ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
531 snprintf(buf, buflen, "%d", stat);
535 static struct ast_custom_function jabberstatus_function = {
536 .name = "JABBER_STATUS",
537 .read = acf_jabberstatus_read,
541 * \brief Dial plan function to send a message.
542 * \param chan ast_channel
543 * \param data Data is sender|reciever|message.
544 * \return 0 on success,-1 on error.
546 static int aji_send_exec(struct ast_channel *chan, const char *data)
548 struct aji_client *client = NULL;
550 AST_DECLARE_APP_ARGS(args,
552 AST_APP_ARG(recipient);
553 AST_APP_ARG(message);
557 ast_log(LOG_ERROR, "Usage: JabberSend(<sender>,<recipient>,<message>)\n");
560 s = ast_strdupa(data);
562 AST_STANDARD_APP_ARGS(args, s);
564 ast_log(LOG_ERROR, "JabberSend requires 3 arguments: '%s'\n", data);
568 if (!(client = ast_aji_get_client(args.sender))) {
569 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
572 if (strchr(args.recipient, '@') && !ast_strlen_zero(args.message))
573 ast_aji_send_chat(client, args.recipient, args.message);
578 * \brief Tests whether the connection is secured or not
579 * \return 0 if the connection is not secured
581 static int aji_is_secure(struct aji_client *client)
584 return client->stream_flags & SECURE;
592 * \brief Starts the TLS procedure
593 * \param client the configured XMPP client we use to connect to a XMPP server
594 * \return IKS_OK on success, an error code if sending failed, IKS_NET_TLSFAIL
595 * if OpenSSL is not installed
597 static int aji_start_tls(struct aji_client *client)
601 /* This is sent not encrypted */
602 ret = iks_send_raw(client->p, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
606 client->stream_flags |= TRY_SECURE;
611 * \brief TLS handshake, OpenSSL initialization
612 * \param client the configured XMPP client we use to connect to a XMPP server
613 * \return IKS_OK on success, IKS_NET_TLSFAIL on failure
615 static int aji_tls_handshake(struct aji_client *client)
620 ast_debug(1, "Starting TLS handshake\n");
622 /* Load encryption, hashing algorithms and error strings */
624 SSL_load_error_strings();
626 /* Choose an SSL/TLS protocol version, create SSL_CTX */
627 client->ssl_method = SSLv3_method();
628 client->ssl_context = SSL_CTX_new(client->ssl_method);
629 if (!client->ssl_context)
630 return IKS_NET_TLSFAIL;
632 /* Create new SSL session */
633 client->ssl_session = SSL_new(client->ssl_context);
634 if (!client->ssl_session)
635 return IKS_NET_TLSFAIL;
637 /* Enforce TLS on our XMPP connection */
638 sock = iks_fd(client->p);
639 ret = SSL_set_fd(client->ssl_session, sock);
641 return IKS_NET_TLSFAIL;
643 /* Perform SSL handshake */
644 ret = SSL_connect(client->ssl_session);
646 return IKS_NET_TLSFAIL;
648 client->stream_flags &= (~TRY_SECURE);
649 client->stream_flags |= SECURE;
651 /* Sent over the established TLS connection */
652 ret = aji_send_header(client, client->jid->server);
654 return IKS_NET_TLSFAIL;
656 ast_debug(1, "TLS started with server\n");
660 #endif /* HAVE_OPENSSL */
663 * \brief Secured or unsecured IO socket receiving function
664 * \param client the configured XMPP client we use to connect to a XMPP server
665 * \param buffer the reception buffer
666 * \param buf_len the size of the buffer
667 * \param timeout the select timer
668 * \return the number of read bytes on success, 0 on timeout expiration,
671 static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout)
675 struct timeval tv, *tvptr = NULL;
679 if (aji_is_secure(client)) {
680 sock = SSL_get_fd(client->ssl_session);
684 #endif /* HAVE_OPENSSL */
685 sock = iks_fd(client->p);
687 memset(&tv, 0, sizeof(struct timeval));
692 /* NULL value for tvptr makes ast_select wait indefinitely */
693 tvptr = (timeout != -1) ? &tv : NULL;
695 /* ast_select emulates linux behaviour in terms of timeout handling */
696 res = ast_select(sock + 1, &fds, NULL, NULL, tvptr);
699 if (aji_is_secure(client)) {
700 len = SSL_read(client->ssl_session, buffer, buf_len);
702 #endif /* HAVE_OPENSSL */
703 len = recv(sock, buffer, buf_len, 0);
707 } else if (len <= 0) {
715 * \brief Tries to receive data from the Jabber server
716 * \param client the configured XMPP client we use to connect to a XMPP server
717 * \param timeout the timeout value
718 * This function receives (encrypted or unencrypted) data from the XMPP server,
719 * and passes it to the parser.
720 * \return IKS_OK on success, IKS_NET_RWERR on IO error, IKS_NET_NOCONN, if no
721 * connection available, IKS_NET_EXPIRED on timeout expiration
723 static int aji_recv (struct aji_client *client, int timeout)
726 char buf[NET_IO_BUF_SIZE - 1];
727 char newbuf[NET_IO_BUF_SIZE - 1];
732 memset(buf, 0, sizeof(buf));
733 memset(newbuf, 0, sizeof(newbuf));
736 len = aji_io_recv(client, buf, NET_IO_BUF_SIZE - 2, timeout);
737 if (len < 0) return IKS_NET_RWERR;
738 if (len == 0) return IKS_NET_EXPIRED;
741 /* our iksemel parser won't work as expected if we feed
742 it with XML packets that contain multiple whitespace
743 characters between tags */
746 /* if we stumble on the ending tag character,
747 we skip any whitespace that follows it*/
749 while (isspace(buf[pos+1])) {
753 newbuf[newbufpos] = c;
760 /* Log the message here, because iksemel's logHook is
762 aji_log_hook(client, buf, len, 1);
764 /* let iksemel deal with the string length,
765 and reset our buffer */
766 ret = iks_parse(client->p, newbuf, 0, 0);
767 memset(newbuf, 0, sizeof(newbuf));
771 ast_log(LOG_WARNING, "Parsing failure: Out of memory.\n");
774 ast_log(LOG_WARNING, "Parsing failure: Invalid XML.\n");
777 ast_log(LOG_WARNING, "Parsing failure: Hook returned an error.\n");
783 ast_debug(3, "XML parsing successful\n");
789 * \brief Sends XMPP header to the server
790 * \param client the configured XMPP client we use to connect to a XMPP server
791 * \param to the target XMPP server
792 * \return IKS_OK on success, any other value on failure
794 static int aji_send_header(struct aji_client *client, const char *to)
799 len = 91 + strlen(client->name_space) + 6 + strlen(to) + 16 + 1;
800 msg = iks_malloc(len);
803 sprintf(msg, "<?xml version='1.0'?>"
804 "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='"
805 "%s' to='%s' version='1.0'>", client->name_space, to);
806 err = aji_send_raw(client, msg);
815 * \brief Wraps raw sending
816 * \param client the configured XMPP client we use to connect to a XMPP server
817 * \param x the XMPP packet to send
818 * \return IKS_OK on success, any other value on failure
820 int ast_aji_send(struct aji_client *client, iks *x)
822 return aji_send_raw(client, iks_string(iks_stack(x), x));
826 * \brief Sends an XML string over an XMPP connection
827 * \param client the configured XMPP client we use to connect to a XMPP server
828 * \param xmlstr the XML string to send
829 * The XML data is sent whether the connection is secured or not. In the
830 * latter case, we just call iks_send_raw().
831 * \return IKS_OK on success, any other value on failure
833 static int aji_send_raw(struct aji_client *client, const char *xmlstr)
837 int len = strlen(xmlstr);
839 if (aji_is_secure(client)) {
840 ret = SSL_write(client->ssl_session, xmlstr, len);
842 /* Log the message here, because iksemel's logHook is
844 aji_log_hook(client, xmlstr, len, 0);
849 /* If needed, data will be sent unencrypted, and logHook will
850 be called inside iks_send_raw */
851 ret = iks_send_raw(client->p, xmlstr);
859 * \brief the debug loop.
861 * \param xmpp xml data as string
862 * \param size size of string
863 * \param is_incoming direction of packet 1 for inbound 0 for outbound.
865 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming)
867 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
869 if (!ast_strlen_zero(xmpp))
870 manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s\r\n", client->name, xmpp);
874 ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
876 if( strlen(xmpp) == 1) {
877 if(option_debug > 2 && xmpp[0] == ' ') {
878 ast_verbose("\nJABBER: Keep alive packet\n");
881 ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp);
885 ASTOBJ_UNREF(client, aji_client_destroy);
889 * \brief A wrapper function for iks_start_sasl
890 * \param client the configured XMPP client we use to connect to a XMPP server
891 * \param type the SASL authentication type. Supported types are PLAIN and MD5
893 * \param pass password.
895 * \return IKS_OK on success, IKSNET_NOTSUPP on failure.
897 static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass)
904 /* trigger SASL DIGEST-MD5 only over an unsecured connection.
905 iks_start_sasl is an iksemel API function and relies on GnuTLS,
906 whereas we use OpenSSL */
907 if ((type & IKS_STREAM_SASL_MD5) && !aji_is_secure(client))
908 return iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, username, pass);
909 if (!(type & IKS_STREAM_SASL_PLAIN)) {
910 ast_log(LOG_ERROR, "Server does not support SASL PLAIN authentication\n");
911 return IKS_NET_NOTSUPP;
916 ast_log(LOG_ERROR, "Out of memory.\n");
917 return IKS_NET_NOTSUPP;
920 iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL);
921 len = strlen(username) + strlen(pass) + 3;
923 base64 = alloca((len + 2) * 4 / 3);
924 iks_insert_attrib(x, "mechanism", "PLAIN");
925 snprintf(s, len, "%c%s%c%s", 0, username, 0, pass);
927 /* exclude the NULL training byte from the base64 encoding operation
928 as some XMPP servers will refuse it.
929 The format for authentication is [authzid]\0authcid\0password
930 not [authzid]\0authcid\0password\0 */
931 ast_base64encode(base64, (const unsigned char *) s, len - 1, (len + 2) * 4 / 3);
932 iks_insert_cdata(x, base64, 0);
933 ast_aji_send(client, x);
940 * \brief The action hook parses the inbound packets, constantly running.
941 * \param data aji client structure
942 * \param type type of packet
943 * \param node the actual packet.
944 * \return IKS_OK or IKS_HOOK .
946 static int aji_act_hook(void *data, int type, iks *node)
948 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
954 ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */
955 ASTOBJ_UNREF(client, aji_client_destroy);
959 if (client->state == AJI_DISCONNECTING) {
960 ASTOBJ_UNREF(client, aji_client_destroy);
964 pak = iks_packet(node);
966 if (!client->component) { /*client */
969 if (client->usetls && !aji_is_secure(client)) {
971 ast_log(LOG_ERROR, "OpenSSL not installed. You need to install OpenSSL on this system, or disable the TLS option in your configuration file\n");
972 ASTOBJ_UNREF(client, aji_client_destroy);
975 if (aji_start_tls(client) == IKS_NET_TLSFAIL) {
976 ast_log(LOG_ERROR, "Could not start TLS\n");
977 ASTOBJ_UNREF(client, aji_client_destroy);
983 if (!client->usesasl) {
984 iks_filter_add_rule(client->f, aji_client_connect, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid, IKS_RULE_DONE);
985 auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id"));
987 iks_insert_attrib(auth, "id", client->mid);
988 iks_insert_attrib(auth, "to", client->jid->server);
989 ast_aji_increment_mid(client->mid);
990 ast_aji_send(client, auth);
993 ast_log(LOG_ERROR, "Out of memory.\n");
997 case IKS_NODE_NORMAL:
999 if (client->stream_flags & TRY_SECURE) {
1000 if (!strcmp("proceed", iks_name(node))) {
1001 return aji_tls_handshake(client);
1005 if (!strcmp("stream:features", iks_name(node))) {
1006 features = iks_stream_features(node);
1007 if (client->usesasl) {
1008 if (client->usetls && !aji_is_secure(client))
1010 if (client->authorized) {
1011 if (features & IKS_STREAM_BIND) {
1012 iks_filter_add_rule(client->f, aji_client_connect, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_DONE);
1013 auth = iks_make_resource_bind(client->jid);
1015 iks_insert_attrib(auth, "id", client->mid);
1016 ast_aji_increment_mid(client->mid);
1017 ast_aji_send(client, auth);
1020 ast_log(LOG_ERROR, "Out of memory.\n");
1024 if (features & IKS_STREAM_SESSION) {
1025 iks_filter_add_rule (client->f, aji_client_connect, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "auth", IKS_RULE_DONE);
1026 auth = iks_make_session();
1028 iks_insert_attrib(auth, "id", "auth");
1029 ast_aji_increment_mid(client->mid);
1030 ast_aji_send(client, auth);
1033 ast_log(LOG_ERROR, "Out of memory.\n");
1038 if (!client->jid->user) {
1039 ast_log(LOG_ERROR, "Malformed Jabber ID : %s (domain missing?)\n", client->jid->full);
1043 ret = aji_start_sasl(client, features, client->jid->user, client->password);
1044 if (ret != IKS_OK) {
1045 ASTOBJ_UNREF(client, aji_client_destroy);
1051 } else if (!strcmp("failure", iks_name(node))) {
1052 ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n");
1053 } else if (!strcmp("success", iks_name(node))) {
1054 client->authorized = 1;
1055 aji_send_header(client, client->jid->server);
1058 case IKS_NODE_ERROR:
1059 ast_log(LOG_ERROR, "JABBER: Node Error\n");
1060 ASTOBJ_UNREF(client, aji_client_destroy);
1064 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
1065 ASTOBJ_UNREF(client, aji_client_destroy);
1069 } else if (client->state != AJI_CONNECTED && client->component) {
1071 case IKS_NODE_START:
1072 if (client->state == AJI_DISCONNECTED) {
1073 char secret[160], shasum[320], *handshake;
1075 sprintf(secret, "%s%s", pak->id, client->password);
1076 ast_sha1_hash(shasum, secret);
1078 if (asprintf(&handshake, "<handshake>%s</handshake>", shasum) >= 0) {
1079 aji_send_raw(client, handshake);
1080 ast_free(handshake);
1083 client->state = AJI_CONNECTING;
1084 if(aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
1085 client->state = AJI_CONNECTED;
1087 ast_log(LOG_WARNING, "Jabber didn't seem to handshake, failed to authenticate.\n");
1092 case IKS_NODE_NORMAL:
1095 case IKS_NODE_ERROR:
1096 ast_log(LOG_ERROR, "JABBER: Node Error\n");
1097 ASTOBJ_UNREF(client, aji_client_destroy);
1101 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
1102 ASTOBJ_UNREF(client, aji_client_destroy);
1107 switch (pak->type) {
1109 ast_debug(1, "JABBER: I don't know what to do with paktype NONE.\n");
1111 case IKS_PAK_MESSAGE:
1112 aji_handle_message(client, pak);
1113 ast_debug(1, "JABBER: Handling paktype MESSAGE.\n");
1115 case IKS_PAK_PRESENCE:
1116 aji_handle_presence(client, pak);
1117 ast_debug(1, "JABBER: Handling paktype PRESENCE\n");
1120 aji_handle_subscribe(client, pak);
1121 ast_debug(1, "JABBER: Handling paktype S10N\n");
1124 ast_debug(1, "JABBER: Handling paktype IQ\n");
1125 aji_handle_iq(client, node);
1128 ast_debug(1, "JABBER: I don't know anything about paktype '%d'\n", pak->type);
1132 iks_filter_packet(client->f, pak);
1137 ASTOBJ_UNREF(client, aji_client_destroy);
1144 * \return IKS_FILTER_EAT.
1146 static int aji_register_approve_handler(void *data, ikspak *pak)
1148 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1149 iks *iq = NULL, *presence = NULL, *x = NULL;
1152 presence = iks_new("presence");
1154 if (client && iq && presence && x) {
1155 if (!iks_find(pak->query, "remove")) {
1156 iks_insert_attrib(iq, "from", client->jid->full);
1157 iks_insert_attrib(iq, "to", pak->from->full);
1158 iks_insert_attrib(iq, "id", pak->id);
1159 iks_insert_attrib(iq, "type", "result");
1160 ast_aji_send(client, iq);
1162 iks_insert_attrib(presence, "from", client->jid->full);
1163 iks_insert_attrib(presence, "to", pak->from->partial);
1164 iks_insert_attrib(presence, "id", client->mid);
1165 ast_aji_increment_mid(client->mid);
1166 iks_insert_attrib(presence, "type", "subscribe");
1167 iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
1168 iks_insert_node(presence, x);
1169 ast_aji_send(client, presence);
1172 ast_log(LOG_ERROR, "Out of memory.\n");
1177 iks_delete(presence);
1180 ASTOBJ_UNREF(client, aji_client_destroy);
1181 return IKS_FILTER_EAT;
1184 * \brief register handler for incoming querys (IQ's)
1185 * \param data incoming aji_client request
1187 * \return IKS_FILTER_EAT.
1189 static int aji_register_query_handler(void *data, ikspak *pak)
1191 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1192 struct aji_buddy *buddy = NULL;
1194 iks *iq = NULL, *query = NULL;
1196 client = (struct aji_client *) data;
1198 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1200 iks *error = NULL, *notacceptable = NULL;
1202 ast_log(LOG_ERROR, "Someone.... %s tried to register but they aren't allowed\n", pak->from->partial);
1204 query = iks_new("query");
1205 error = iks_new("error");
1206 notacceptable = iks_new("not-acceptable");
1207 if(iq && query && error && notacceptable) {
1208 iks_insert_attrib(iq, "type", "error");
1209 iks_insert_attrib(iq, "from", client->user);
1210 iks_insert_attrib(iq, "to", pak->from->full);
1211 iks_insert_attrib(iq, "id", pak->id);
1212 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
1213 iks_insert_attrib(error, "code" , "406");
1214 iks_insert_attrib(error, "type", "modify");
1215 iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
1216 iks_insert_node(iq, query);
1217 iks_insert_node(iq, error);
1218 iks_insert_node(error, notacceptable);
1219 ast_aji_send(client, iq);
1221 ast_log(LOG_ERROR, "Out of memory.\n");
1225 iks_delete(notacceptable);
1226 } else if (!(node = iks_find_attrib(pak->query, "node"))) {
1227 iks *instructions = NULL;
1228 char *explain = "Welcome to Asterisk - the Open Source PBX.\n";
1230 query = iks_new("query");
1231 instructions = iks_new("instructions");
1232 if (iq && query && instructions && client) {
1233 iks_insert_attrib(iq, "from", client->user);
1234 iks_insert_attrib(iq, "to", pak->from->full);
1235 iks_insert_attrib(iq, "id", pak->id);
1236 iks_insert_attrib(iq, "type", "result");
1237 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
1238 iks_insert_cdata(instructions, explain, 0);
1239 iks_insert_node(iq, query);
1240 iks_insert_node(query, instructions);
1241 ast_aji_send(client, iq);
1243 ast_log(LOG_ERROR, "Out of memory.\n");
1246 iks_delete(instructions);
1250 ASTOBJ_UNREF(client, aji_client_destroy);
1251 return IKS_FILTER_EAT;
1255 * \brief Handles stuff
1258 * \return IKS_FILTER_EAT.
1260 static int aji_ditems_handler(void *data, ikspak *pak)
1262 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1265 if (!(node = iks_find_attrib(pak->query, "node"))) {
1266 iks *iq = NULL, *query = NULL, *item = NULL;
1268 query = iks_new("query");
1269 item = iks_new("item");
1271 if (iq && query && item) {
1272 iks_insert_attrib(iq, "from", client->user);
1273 iks_insert_attrib(iq, "to", pak->from->full);
1274 iks_insert_attrib(iq, "id", pak->id);
1275 iks_insert_attrib(iq, "type", "result");
1276 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1277 iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
1278 iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
1279 iks_insert_attrib(item, "jid", client->user);
1281 iks_insert_node(iq, query);
1282 iks_insert_node(query, item);
1283 ast_aji_send(client, iq);
1285 ast_log(LOG_ERROR, "Out of memory.\n");
1292 } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
1293 iks *iq, *query, *confirm;
1295 query = iks_new("query");
1296 confirm = iks_new("item");
1297 if (iq && query && confirm && client) {
1298 iks_insert_attrib(iq, "from", client->user);
1299 iks_insert_attrib(iq, "to", pak->from->full);
1300 iks_insert_attrib(iq, "id", pak->id);
1301 iks_insert_attrib(iq, "type", "result");
1302 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1303 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
1304 iks_insert_attrib(confirm, "node", "confirmaccount");
1305 iks_insert_attrib(confirm, "name", "Confirm AIM account");
1306 iks_insert_attrib(confirm, "jid", "blog.astjab.org");
1308 iks_insert_node(iq, query);
1309 iks_insert_node(query, confirm);
1310 ast_aji_send(client, iq);
1312 ast_log(LOG_ERROR, "Out of memory.\n");
1317 iks_delete(confirm);
1319 } else if (!strcasecmp(node, "confirmaccount")) {
1320 iks *iq = NULL, *query = NULL, *feature = NULL;
1323 query = iks_new("query");
1324 feature = iks_new("feature");
1326 if (iq && query && feature && client) {
1327 iks_insert_attrib(iq, "from", client->user);
1328 iks_insert_attrib(iq, "to", pak->from->full);
1329 iks_insert_attrib(iq, "id", pak->id);
1330 iks_insert_attrib(iq, "type", "result");
1331 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1332 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1333 iks_insert_node(iq, query);
1334 iks_insert_node(query, feature);
1335 ast_aji_send(client, iq);
1337 ast_log(LOG_ERROR, "Out of memory.\n");
1342 iks_delete(feature);
1345 ASTOBJ_UNREF(client, aji_client_destroy);
1346 return IKS_FILTER_EAT;
1350 * \brief Handle add extra info
1353 * \return IKS_FILTER_EAT
1355 static int aji_client_info_handler(void *data, ikspak *pak)
1357 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1358 struct aji_resource *resource = NULL;
1359 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1361 resource = aji_find_resource(buddy, pak->from->resource);
1362 if (pak->subtype == IKS_TYPE_RESULT) {
1364 ast_log(LOG_NOTICE, "JABBER: Received client info from %s when not requested.\n", pak->from->full);
1365 ASTOBJ_UNREF(client, aji_client_destroy);
1366 return IKS_FILTER_EAT;
1368 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
1369 resource->cap->jingle = 1;
1371 resource->cap->jingle = 0;
1372 } else if (pak->subtype == IKS_TYPE_GET) {
1373 iks *iq, *disco, *ident, *google, *query;
1375 query = iks_new("query");
1376 ident = iks_new("identity");
1377 disco = iks_new("feature");
1378 google = iks_new("feature");
1379 if (iq && ident && disco && google) {
1380 iks_insert_attrib(iq, "from", client->jid->full);
1381 iks_insert_attrib(iq, "to", pak->from->full);
1382 iks_insert_attrib(iq, "type", "result");
1383 iks_insert_attrib(iq, "id", pak->id);
1384 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1385 iks_insert_attrib(ident, "category", "client");
1386 iks_insert_attrib(ident, "type", "pc");
1387 iks_insert_attrib(ident, "name", "asterisk");
1388 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
1389 iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
1390 iks_insert_node(iq, query);
1391 iks_insert_node(query, ident);
1392 iks_insert_node(query, google);
1393 iks_insert_node(query, disco);
1394 ast_aji_send(client, iq);
1396 ast_log(LOG_ERROR, "Out of Memory.\n");
1403 } else if (pak->subtype == IKS_TYPE_ERROR) {
1404 ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
1406 ASTOBJ_UNREF(client, aji_client_destroy);
1407 return IKS_FILTER_EAT;
1410 * \brief Handler of the return info packet
1411 * \param data aji_client
1413 * \return IKS_FILTER_EAT
1415 static int aji_dinfo_handler(void *data, ikspak *pak)
1417 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1419 struct aji_resource *resource = NULL;
1420 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1422 resource = aji_find_resource(buddy, pak->from->resource);
1423 if (pak->subtype == IKS_TYPE_ERROR) {
1424 ast_log(LOG_WARNING, "Recieved error from a client, turn on jabber debug!\n");
1425 return IKS_FILTER_EAT;
1427 if (pak->subtype == IKS_TYPE_RESULT) {
1429 ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full);
1430 ASTOBJ_UNREF(client, aji_client_destroy);
1431 return IKS_FILTER_EAT;
1433 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
1434 resource->cap->jingle = 1;
1436 resource->cap->jingle = 0;
1437 } else if (pak->subtype == IKS_TYPE_GET && !(node = iks_find_attrib(pak->query, "node"))) {
1438 iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search;
1441 query = iks_new("query");
1442 identity = iks_new("identity");
1443 disco = iks_new("feature");
1444 reg = iks_new("feature");
1445 commands = iks_new("feature");
1446 gateway = iks_new("feature");
1447 version = iks_new("feature");
1448 vcard = iks_new("feature");
1449 search = iks_new("feature");
1451 if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
1452 iks_insert_attrib(iq, "from", client->user);
1453 iks_insert_attrib(iq, "to", pak->from->full);
1454 iks_insert_attrib(iq, "id", pak->id);
1455 iks_insert_attrib(iq, "type", "result");
1456 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1457 iks_insert_attrib(identity, "category", "gateway");
1458 iks_insert_attrib(identity, "type", "pstn");
1459 iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
1460 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
1461 iks_insert_attrib(reg, "var", "jabber:iq:register");
1462 iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
1463 iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
1464 iks_insert_attrib(version, "var", "jabber:iq:version");
1465 iks_insert_attrib(vcard, "var", "vcard-temp");
1466 iks_insert_attrib(search, "var", "jabber:iq:search");
1468 iks_insert_node(iq, query);
1469 iks_insert_node(query, identity);
1470 iks_insert_node(query, disco);
1471 iks_insert_node(query, reg);
1472 iks_insert_node(query, commands);
1473 iks_insert_node(query, gateway);
1474 iks_insert_node(query, version);
1475 iks_insert_node(query, vcard);
1476 iks_insert_node(query, search);
1477 ast_aji_send(client, iq);
1479 ast_log(LOG_ERROR, "Out of memory.\n");
1484 iks_delete(identity);
1487 iks_delete(commands);
1488 iks_delete(gateway);
1489 iks_delete(version);
1493 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "http://jabber.org/protocol/commands")) {
1494 iks *iq, *query, *confirm;
1496 query = iks_new("query");
1497 confirm = iks_new("item");
1499 if (iq && query && confirm && client) {
1500 iks_insert_attrib(iq, "from", client->user);
1501 iks_insert_attrib(iq, "to", pak->from->full);
1502 iks_insert_attrib(iq, "id", pak->id);
1503 iks_insert_attrib(iq, "type", "result");
1504 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1505 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
1506 iks_insert_attrib(confirm, "node", "confirmaccount");
1507 iks_insert_attrib(confirm, "name", "Confirm AIM account");
1508 iks_insert_attrib(confirm, "jid", client->user);
1509 iks_insert_node(iq, query);
1510 iks_insert_node(query, confirm);
1511 ast_aji_send(client, iq);
1513 ast_log(LOG_ERROR, "Out of memory.\n");
1518 iks_delete(confirm);
1520 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "confirmaccount")) {
1521 iks *iq, *query, *feature;
1524 query = iks_new("query");
1525 feature = iks_new("feature");
1527 if (iq && query && feature && client) {
1528 iks_insert_attrib(iq, "from", client->user);
1529 iks_insert_attrib(iq, "to", pak->from->full);
1530 iks_insert_attrib(iq, "id", pak->id);
1531 iks_insert_attrib(iq, "type", "result");
1532 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1533 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1534 iks_insert_node(iq, query);
1535 iks_insert_node(query, feature);
1536 ast_aji_send(client, iq);
1538 ast_log(LOG_ERROR, "Out of memory.\n");
1543 iks_delete(feature);
1546 ASTOBJ_UNREF(client, aji_client_destroy);
1547 return IKS_FILTER_EAT;
1551 * \brief Handles \verbatim <iq> \endverbatim tags.
1552 * \param client the configured XMPP client we use to connect to a XMPP server
1556 static void aji_handle_iq(struct aji_client *client, iks *node)
1558 /*Nothing to see here */
1562 * \brief Handles presence packets.
1563 * \param client the configured XMPP client we use to connect to a XMPP server
1564 * \param pak ikspak the node
1566 static void aji_handle_message(struct aji_client *client, ikspak *pak)
1568 struct aji_message *insert, *tmp;
1571 if (!(insert = ast_calloc(1, sizeof(*insert))))
1573 time(&insert->arrived);
1574 if (iks_find_cdata(pak->x, "body"))
1575 insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
1577 ast_copy_string(insert->id, pak->id, sizeof(insert->message));
1579 insert->from = ast_strdup(pak->from->full);
1580 AST_LIST_LOCK(&client->messages);
1581 AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
1583 AST_LIST_REMOVE_CURRENT(list);
1585 ast_free(tmp->from);
1587 ast_free(tmp->message);
1588 } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) {
1590 AST_LIST_REMOVE_CURRENT(list);
1592 ast_free(tmp->from);
1594 ast_free(tmp->message);
1597 AST_LIST_TRAVERSE_SAFE_END;
1598 AST_LIST_INSERT_HEAD(&client->messages, insert, list);
1599 AST_LIST_UNLOCK(&client->messages);
1602 * \brief Check the presence info
1603 * \param client the configured XMPP client we use to connect to a XMPP server
1606 static void aji_handle_presence(struct aji_client *client, ikspak *pak)
1608 int status, priority;
1609 struct aji_buddy *buddy;
1610 struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
1611 char *ver, *node, *descrip, *type;
1613 if(client->state != AJI_CONNECTED)
1614 aji_create_buddy(pak->from->partial, client);
1616 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1617 if (!buddy && pak->from->partial) {
1618 /* allow our jid to be used to log in with another resource */
1619 if (!strcmp((const char *)pak->from->partial, (const char *)client->jid->partial))
1620 aji_create_buddy(pak->from->partial, client);
1622 ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial);
1625 type = iks_find_attrib(pak->x, "type");
1626 if(client->component && type &&!strcasecmp("probe", type)) {
1627 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
1628 ast_verbose("what i was looking for \n");
1630 ASTOBJ_WRLOCK(buddy);
1631 status = (pak->show) ? pak->show : 6;
1632 priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
1633 tmp = buddy->resources;
1634 descrip = ast_strdup(iks_find_cdata(pak->x,"status"));
1636 while (tmp && pak->from->resource) {
1637 if (!strcasecmp(tmp->resource, pak->from->resource)) {
1638 tmp->status = status;
1639 if (tmp->description) ast_free(tmp->description);
1640 tmp->description = descrip;
1642 if (status == 6) { /* Sign off Destroy resource */
1643 if (last && found->next) {
1644 last->next = found->next;
1647 buddy->resources = found->next;
1649 buddy->resources = NULL;
1650 } else if (!found->next) {
1654 buddy->resources = NULL;
1660 /* resource list is sorted by descending priority */
1661 if (tmp->priority != priority) {
1662 found->priority = priority;
1663 if (!last && !found->next)
1664 /* resource was found to be unique,
1667 /* search for resource in our list
1668 and take it out for the moment */
1670 last->next = found->next;
1672 buddy->resources = found->next;
1675 tmp = buddy->resources;
1676 if (!buddy->resources)
1677 buddy->resources = found;
1678 /* priority processing */
1680 /* insert resource back according to
1681 its priority value */
1682 if (found->priority > tmp->priority) {
1684 /* insert within list */
1689 buddy->resources = found;
1693 /* insert at the end of the list */
1708 /* resource not found in our list, create it */
1709 if (!found && status != 6 && pak->from->resource) {
1710 found = ast_calloc(1, sizeof(*found));
1713 ast_log(LOG_ERROR, "Out of memory!\n");
1716 ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
1717 found->status = status;
1718 found->description = descrip;
1719 found->priority = priority;
1722 tmp = buddy->resources;
1724 if (found->priority > tmp->priority) {
1729 buddy->resources = found;
1740 buddy->resources = found;
1743 ASTOBJ_UNLOCK(buddy);
1744 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1746 node = iks_find_attrib(iks_find(pak->x, "c"), "node");
1747 ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
1749 /* handle gmail client's special caps:c tag */
1750 if (!node && !ver) {
1751 node = iks_find_attrib(iks_find(pak->x, "caps:c"), "node");
1752 ver = iks_find_attrib(iks_find(pak->x, "caps:c"), "ver");
1755 /* retrieve capabilites of the new resource */
1756 if(status !=6 && found && !found->cap) {
1757 found->cap = aji_find_version(node, ver, pak);
1758 if(gtalk_yuck(pak->x)) /* gtalk should do discover */
1759 found->cap->jingle = 1;
1760 if(found->cap->jingle && option_debug > 4) {
1761 ast_debug(1,"Special case for google till they support discover.\n");
1766 query = iks_new("query");
1768 iks_insert_attrib(iq, "type", "get");
1769 iks_insert_attrib(iq, "to", pak->from->full);
1770 iks_insert_attrib(iq,"from", client->jid->full);
1771 iks_insert_attrib(iq, "id", client->mid);
1772 ast_aji_increment_mid(client->mid);
1773 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1774 iks_insert_node(iq, query);
1775 ast_aji_send(client, iq);
1778 ast_log(LOG_ERROR, "Out of memory.\n");
1784 switch (pak->subtype) {
1785 case IKS_TYPE_AVAILABLE:
1786 ast_debug(3, "JABBER: I am available ^_* %i\n", pak->subtype);
1788 case IKS_TYPE_UNAVAILABLE:
1789 ast_debug(3, "JABBER: I am unavailable ^_* %i\n", pak->subtype);
1792 ast_debug(3, "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype);
1794 switch (pak->show) {
1795 case IKS_SHOW_UNAVAILABLE:
1796 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1798 case IKS_SHOW_AVAILABLE:
1799 ast_debug(3, "JABBER: type is available\n");
1802 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1805 ast_debug(3, "JABBER: type is away\n");
1808 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1811 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1814 ast_debug(3, "JABBER: Kinky! how did that happen %i\n", pak->show);
1819 * \brief handles subscription requests.
1820 * \param client the configured XMPP client we use to connect to a XMPP server
1821 * \param pak ikspak iksemel packet.
1824 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
1826 iks *presence = NULL, *status = NULL;
1827 struct aji_buddy* buddy = NULL;
1829 switch (pak->subtype) {
1830 case IKS_TYPE_SUBSCRIBE:
1831 presence = iks_new("presence");
1832 status = iks_new("status");
1833 if (presence && status) {
1834 iks_insert_attrib(presence, "type", "subscribed");
1835 iks_insert_attrib(presence, "to", pak->from->full);
1836 iks_insert_attrib(presence, "from", client->jid->full);
1838 iks_insert_attrib(presence, "id", pak->id);
1839 iks_insert_cdata(status, "Asterisk has approved subscription", 0);
1840 iks_insert_node(presence, status);
1841 ast_aji_send(client, presence);
1843 ast_log(LOG_ERROR, "Unable to allocate nodes\n");
1845 iks_delete(presence);
1848 if (client->component)
1849 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
1850 case IKS_TYPE_SUBSCRIBED:
1851 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1852 if (!buddy && pak->from->partial) {
1853 aji_create_buddy(pak->from->partial, client);
1856 if (option_verbose > 4) {
1857 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1863 * \brief sends messages.
1864 * \param client the configured XMPP client we use to connect to a XMPP server
1869 int ast_aji_send_chat(struct aji_client *client, const char *address, const char *message)
1872 iks *message_packet = NULL;
1873 if (client->state == AJI_CONNECTED) {
1874 message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message);
1875 if (message_packet) {
1876 iks_insert_attrib(message_packet, "from", client->jid->full);
1877 res = ast_aji_send(client, message_packet);
1879 ast_log(LOG_ERROR, "Out of memory.\n");
1882 iks_delete(message_packet);
1884 ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
1889 * \brief create a chatroom.
1890 * \param client the configured XMPP client we use to connect to a XMPP server
1891 * \param room name of room
1892 * \param server name of server
1893 * \param topic topic for the room.
1896 int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
1903 iks_insert_attrib(iq, "type", "get");
1904 iks_insert_attrib(iq, "to", server);
1905 iks_insert_attrib(iq, "id", client->mid);
1906 ast_aji_increment_mid(client->mid);
1907 ast_aji_send(client, iq);
1909 ast_log(LOG_ERROR, "Out of memory.\n");
1917 * \brief join a chatroom.
1918 * \param client the configured XMPP client we use to connect to a XMPP server
1919 * \param room room to join
1922 int ast_aji_join_chat(struct aji_client *client, char *room)
1925 iks *presence = NULL, *priority = NULL;
1926 presence = iks_new("presence");
1927 priority = iks_new("priority");
1928 if (presence && priority && client) {
1929 iks_insert_cdata(priority, "0", 1);
1930 iks_insert_attrib(presence, "to", room);
1931 iks_insert_node(presence, priority);
1932 res = ast_aji_send(client, presence);
1933 iks_insert_cdata(priority, "5", 1);
1934 iks_insert_attrib(presence, "to", room);
1935 res = ast_aji_send(client, presence);
1937 ast_log(LOG_ERROR, "Out of memory.\n");
1939 iks_delete(presence);
1940 iks_delete(priority);
1946 * \brief invite to a chatroom.
1947 * \param client the configured XMPP client we use to connect to a XMPP server
1953 int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
1956 iks *invite, *body, *namespace;
1958 invite = iks_new("message");
1959 body = iks_new("body");
1960 namespace = iks_new("x");
1961 if (client && invite && body && namespace) {
1962 iks_insert_attrib(invite, "to", user);
1963 iks_insert_attrib(invite, "id", client->mid);
1964 ast_aji_increment_mid(client->mid);
1965 iks_insert_cdata(body, message, 0);
1966 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
1967 iks_insert_attrib(namespace, "jid", room);
1968 iks_insert_node(invite, body);
1969 iks_insert_node(invite, namespace);
1970 res = ast_aji_send(client, invite);
1972 ast_log(LOG_ERROR, "Out of memory.\n");
1975 iks_delete(namespace);
1983 * \brief receive message loop.
1987 static void *aji_recv_loop(void *data)
1989 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1992 while(res != IKS_OK) {
1993 ast_debug(3, "JABBER: Connecting.\n");
1994 res = aji_reconnect(client);
1999 if (res == IKS_NET_RWERR || client->timeout == 0) {
2000 while(res != IKS_OK) {
2001 ast_debug(3, "JABBER: reconnecting.\n");
2002 res = aji_reconnect(client);
2007 res = aji_recv(client, 1);
2009 if (client->state == AJI_DISCONNECTING) {
2010 ast_debug(2, "Ending our Jabber client's thread due to a disconnect\n");
2014 /* Decrease timeout if no data received */
2015 if (res == IKS_NET_EXPIRED)
2018 if (res == IKS_HOOK)
2019 ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
2020 else if (res == IKS_NET_TLSFAIL)
2021 ast_log(LOG_ERROR, "JABBER: Failure in TLS.\n");
2022 else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
2023 res = client->keepalive ? aji_send_raw(client, " ") : IKS_OK;
2025 client->timeout = 50;
2027 ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
2028 } else if (res == IKS_NET_RWERR)
2029 ast_log(LOG_WARNING, "JABBER: socket read error\n");
2031 ASTOBJ_UNREF(client, aji_client_destroy);
2036 * \brief increments the mid field for messages and other events.
2040 void ast_aji_increment_mid(char *mid)
2044 for (i = strlen(mid) - 1; i >= 0; i--) {
2045 if (mid[i] != 'z') {
2046 mid[i] = mid[i] + 1;
2055 * \brief attempts to register to a transport.
2056 * \param aji_client struct, and xml packet.
2057 * \return IKS_FILTER_EAT.
2059 /*allows for registering to transport , was too sketch and is out for now. */
2060 static int aji_register_transport(void *data, ikspak *pak)
2062 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2064 struct aji_buddy *buddy = NULL;
2065 iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
2067 if (client && send) {
2068 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2069 ASTOBJ_RDLOCK(iterator);
2070 if (iterator->btype == AJI_TRANS) {
2073 ASTOBJ_UNLOCK(iterator);
2075 iks_filter_remove_hook(client->f, aji_register_transport);
2076 iks_filter_add_rule(client->f, aji_register_transport2, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS, IKS_NS_REGISTER, IKS_RULE_DONE);
2077 iks_insert_attrib(send, "to", buddy->host);
2078 iks_insert_attrib(send, "id", client->mid);
2079 ast_aji_increment_mid(client->mid);
2080 iks_insert_attrib(send, "from", client->user);
2081 res = ast_aji_send(client, send);
2083 ast_log(LOG_ERROR, "Out of memory.\n");
2087 ASTOBJ_UNREF(client, aji_client_destroy);
2088 return IKS_FILTER_EAT;
2092 * \brief attempts to register to a transport step 2.
2093 * \param aji_client struct, and xml packet.
2094 * \return IKS_FILTER_EAT.
2096 /* more of the same blob of code, too wonky for now*/
2097 static int aji_register_transport2(void *data, ikspak *pak)
2099 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2101 struct aji_buddy *buddy = NULL;
2103 iks *regiq = iks_new("iq");
2104 iks *regquery = iks_new("query");
2105 iks *reguser = iks_new("username");
2106 iks *regpass = iks_new("password");
2108 if (client && regquery && reguser && regpass && regiq) {
2109 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2110 ASTOBJ_RDLOCK(iterator);
2111 if (iterator->btype == AJI_TRANS)
2112 buddy = iterator; ASTOBJ_UNLOCK(iterator);
2114 iks_filter_remove_hook(client->f, aji_register_transport2);
2115 iks_insert_attrib(regiq, "to", buddy->host);
2116 iks_insert_attrib(regiq, "type", "set");
2117 iks_insert_attrib(regiq, "id", client->mid);
2118 ast_aji_increment_mid(client->mid);
2119 iks_insert_attrib(regiq, "from", client->user);
2120 iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
2121 iks_insert_cdata(reguser, buddy->user, 0);
2122 iks_insert_cdata(regpass, buddy->pass, 0);
2123 iks_insert_node(regiq, regquery);
2124 iks_insert_node(regquery, reguser);
2125 iks_insert_node(regquery, regpass);
2126 res = ast_aji_send(client, regiq);
2128 ast_log(LOG_ERROR, "Out of memory.\n");
2132 iks_delete(regquery);
2134 iks_delete(reguser);
2136 iks_delete(regpass);
2137 ASTOBJ_UNREF(client, aji_client_destroy);
2138 return IKS_FILTER_EAT;
2143 * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
2144 * \param client the configured XMPP client we use to connect to a XMPP server
2146 * \note The messages here should be configurable.
2148 static void aji_pruneregister(struct aji_client *client)
2151 iks *removeiq = iks_new("iq");
2152 iks *removequery = iks_new("query");
2153 iks *removeitem = iks_new("item");
2154 iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
2155 if (!client || !removeiq || !removequery || !removeitem || !send) {
2156 ast_log(LOG_ERROR, "Out of memory.\n");
2160 iks_insert_node(removeiq, removequery);
2161 iks_insert_node(removequery, removeitem);
2162 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2163 ASTOBJ_RDLOCK(iterator);
2164 /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
2165 * be called at the same time */
2166 if (ast_test_flag(&iterator->flags, AJI_AUTOPRUNE)) { /* If autoprune is set on jabber.conf */
2167 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
2168 "GoodBye. Your status is no longer needed by Asterisk the Open Source PBX"
2169 " so I am no longer subscribing to your presence.\n"));
2170 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
2171 "GoodBye. You are no longer in the Asterisk config file so I am removing"
2172 " your access to my presence.\n"));
2173 iks_insert_attrib(removeiq, "from", client->jid->full);
2174 iks_insert_attrib(removeiq, "type", "set");
2175 iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
2176 iks_insert_attrib(removeitem, "jid", iterator->name);
2177 iks_insert_attrib(removeitem, "subscription", "remove");
2178 res = ast_aji_send(client, removeiq);
2179 } else if (ast_test_flag(&iterator->flags, AJI_AUTOREGISTER)) {
2180 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name,
2181 "Greetings! I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
2182 ast_clear_flag(&iterator->flags, AJI_AUTOREGISTER);
2184 ASTOBJ_UNLOCK(iterator);
2188 iks_delete(removeiq);
2189 iks_delete(removequery);
2190 iks_delete(removeitem);
2193 ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy);
2197 * \brief filters the roster packet we get back from server.
2199 * \param pak ikspak iksemel packet.
2200 * \return IKS_FILTER_EAT.
2202 static int aji_filter_roster(void *data, ikspak *pak)
2204 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2207 struct aji_buddy *buddy;
2209 client->state = AJI_CONNECTED;
2210 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2211 ASTOBJ_RDLOCK(iterator);
2212 x = iks_child(pak->query);
2215 if (!iks_strcmp(iks_name(x), "item")) {
2216 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
2218 ast_clear_flag(&iterator->flags, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
2224 ast_copy_flags(&iterator->flags, &client->flags, AJI_AUTOREGISTER);
2227 ASTOBJ_UNLOCK(iterator);
2230 x = iks_child(pak->query);
2233 if (iks_strcmp(iks_name(x), "item") == 0) {
2234 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2235 ASTOBJ_RDLOCK(iterator);
2236 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
2238 ASTOBJ_UNLOCK(iterator);
2242 /* found buddy, don't create a new one */
2247 buddy = ast_calloc(1, sizeof(*buddy));
2249 ast_log(LOG_WARNING, "Out of memory\n");
2253 ASTOBJ_WRLOCK(buddy);
2254 ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
2255 ast_clear_flag(&buddy->flags, AST_FLAGS_ALL);
2256 if(ast_test_flag(&client->flags, AJI_AUTOPRUNE)) {
2257 ast_set_flag(&buddy->flags, AJI_AUTOPRUNE);
2259 } else if (!iks_strcmp(iks_find_attrib(x, "subscription"), "none") || !iks_strcmp(iks_find_attrib(x, "subscription"), "from")) {
2260 /* subscribe to buddy's presence only
2261 if we really need to */
2262 ast_set_flag(&buddy->flags, AJI_AUTOREGISTER);
2264 ASTOBJ_UNLOCK(buddy);
2266 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2267 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2274 aji_pruneregister(client);
2276 ASTOBJ_UNREF(client, aji_client_destroy);
2277 return IKS_FILTER_EAT;
2281 * \brief reconnect to jabber server
2282 * \param client the configured XMPP client we use to connect to a XMPP server
2285 static int aji_reconnect(struct aji_client *client)
2290 client->state = AJI_DISCONNECTED;
2293 iks_parser_reset(client->p);
2294 if (client->authorized)
2295 client->authorized = 0;
2297 res = aji_initialize(client);
2303 * \brief Get the roster of jabber users
2304 * \param client the configured XMPP client we use to connect to a XMPP server
2307 static int aji_get_roster(struct aji_client *client)
2310 roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
2313 iks_insert_attrib(roster, "id", "roster");
2314 aji_set_presence(client, NULL, client->jid->full, client->status, client->statusmessage);
2315 ast_aji_send(client, roster);
2324 * \brief connects as a client to jabber server.
2326 * \param pak ikspak iksemel packet
2329 static int aji_client_connect(void *data, ikspak *pak)
2331 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2335 if (client->state == AJI_DISCONNECTED) {
2336 iks_filter_add_rule(client->f, aji_filter_roster, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "roster", IKS_RULE_DONE);
2337 client->state = AJI_CONNECTING;
2338 client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
2339 iks_filter_remove_hook(client->f, aji_client_connect);
2340 if(!client->component) /*client*/
2341 aji_get_roster(client);
2344 ast_log(LOG_ERROR, "Out of memory.\n");
2346 ASTOBJ_UNREF(client, aji_client_destroy);
2351 * \brief prepares client for connect.
2352 * \param client the configured XMPP client we use to connect to a XMPP server
2355 static int aji_initialize(struct aji_client *client)
2357 int connected = IKS_NET_NOCONN;
2360 /* reset stream flags */
2361 client->stream_flags = 0;
2363 /* If it's a component, connect to user, otherwise, connect to server */
2364 connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->component ? client->user : client->jid->server);
2366 if (connected == IKS_NET_NOCONN) {
2367 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
2369 } else if (connected == IKS_NET_NODNS) {
2370 ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to %s\n", client->name, S_OR(client->serverhost, client->jid->server));
2378 * \brief disconnect from jabber server.
2379 * \param client the configured XMPP client we use to connect to a XMPP server
2382 int ast_aji_disconnect(struct aji_client *client)
2385 ast_verb(4, "JABBER: Disconnecting\n");
2387 if (client->stream_flags & SECURE) {
2388 SSL_shutdown(client->ssl_session);
2389 SSL_CTX_free(client->ssl_context);
2390 SSL_free(client->ssl_session);
2393 iks_disconnect(client->p);
2394 iks_parser_delete(client->p);
2395 ASTOBJ_UNREF(client, aji_client_destroy);
2402 * \brief set presence of client.
2403 * \param client the configured XMPP client we use to connect to a XMPP server
2404 * \param to user send it to
2405 * \param from user it came from
2410 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc)
2413 iks *presence = iks_make_pres(level, desc);
2414 iks *cnode = iks_new("c");
2415 iks *priority = iks_new("priority");
2418 if (presence && cnode && client && priority) {
2420 iks_insert_attrib(presence, "to", to);
2422 iks_insert_attrib(presence, "from", from);
2423 snprintf(priorityS, sizeof(priorityS), "%d", client->priority);
2424 iks_insert_cdata(priority, priorityS, strlen(priorityS));
2425 iks_insert_node(presence, priority);
2426 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
2427 iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
2428 iks_insert_attrib(cnode, "ext", "voice-v1");
2429 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
2430 iks_insert_node(presence, cnode);
2431 res = ast_aji_send(client, presence);
2433 ast_log(LOG_ERROR, "Out of memory.\n");
2436 iks_delete(presence);
2437 iks_delete(priority);
2441 * \brief Turn on/off console debugging.
2442 * \return CLI_SUCCESS.
2444 static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2448 e->command = "jabber set debug {on|off}";
2450 "Usage: jabber set debug {on|off}\n"
2451 " Enables/disables dumping of XMPP/Jabber packets for debugging purposes.\n";
2457 if (a->argc != e->args)
2458 return CLI_SHOWUSAGE;
2460 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
2461 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2462 ASTOBJ_RDLOCK(iterator);
2463 iterator->debug = 1;
2464 ASTOBJ_UNLOCK(iterator);
2466 ast_cli(a->fd, "Jabber Debugging Enabled.\n");
2468 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
2469 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2470 ASTOBJ_RDLOCK(iterator);
2471 iterator->debug = 0;
2472 ASTOBJ_UNLOCK(iterator);
2474 ast_cli(a->fd, "Jabber Debugging Disabled.\n");
2477 return CLI_SHOWUSAGE; /* defaults to invalid */
2481 * \brief Reload jabber module.
2482 * \return CLI_SUCCESS.
2484 static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2488 e->command = "jabber reload";
2490 "Usage: jabber reload\n"
2491 " Reloads the Jabber module.\n";
2498 ast_cli(a->fd, "Jabber Reloaded.\n");
2503 * \brief Show client status.
2504 * \return CLI_SUCCESS.
2506 static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2513 e->command = "jabber show connected";
2515 "Usage: jabber show connected\n"
2516 " Shows state of clients and components\n";
2522 ast_cli(a->fd, "Jabber Users and their status:\n");
2523 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2524 ASTOBJ_RDLOCK(iterator);
2526 switch (iterator->state) {
2527 case AJI_DISCONNECTED:
2528 status = "Disconnected";
2530 case AJI_CONNECTING:
2531 status = "Connecting";
2534 status = "Connected";
2539 ast_cli(a->fd, " User: %s - %s\n", iterator->user, status);
2540 ASTOBJ_UNLOCK(iterator);
2542 ast_cli(a->fd, "----\n");
2543 ast_cli(a->fd, " Number of users: %d\n", count);
2548 * \brief Show buddy lists
2549 * \return CLI_SUCCESS.
2551 static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2553 struct aji_resource *resource;
2554 struct aji_client *client;
2558 e->command = "jabber show buddies";
2560 "Usage: jabber show buddies\n"
2561 " Shows buddy lists of our clients\n";
2567 ast_cli(a->fd, "Jabber buddy lists\n");
2568 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2569 ast_cli(a->fd,"Client: %s\n", iterator->user);
2571 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2572 ASTOBJ_RDLOCK(iterator);
2573 ast_cli(a->fd,"\tBuddy:\t%s\n", iterator->name);
2574 if (!iterator->resources)
2575 ast_cli(a->fd,"\t\tResource: None\n");
2576 for (resource = iterator->resources; resource; resource = resource->next) {
2577 ast_cli(a->fd,"\t\tResource: %s\n", resource->resource);
2579 ast_cli(a->fd,"\t\t\tnode: %s\n", resource->cap->parent->node);
2580 ast_cli(a->fd,"\t\t\tversion: %s\n", resource->cap->version);
2581 ast_cli(a->fd,"\t\t\tJingle capable: %s\n", resource->cap->jingle ? "yes" : "no");
2583 ast_cli(a->fd,"\t\tStatus: %d\n", resource->status);
2584 ast_cli(a->fd,"\t\tPriority: %d\n", resource->priority);
2586 ASTOBJ_UNLOCK(iterator);
2594 * \brief Send test message for debugging.
2595 * \return CLI_SUCCESS,CLI_FAILURE.
2597 static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2599 struct aji_client *client;
2600 struct aji_resource *resource;
2601 const char *name = "asterisk";
2602 struct aji_message *tmp;
2606 e->command = "jabber test";
2608 "Usage: jabber test [client]\n"
2609 " Sends test message for debugging purposes. A specific client\n"
2610 " as configured in jabber.conf can be optionally specified.\n";
2617 return CLI_SHOWUSAGE;
2618 else if (a->argc == 3)
2621 if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
2622 ast_cli(a->fd, "Unable to find client '%s'!\n", name);
2626 /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */
2627 ast_aji_send_chat(client, "mogorman@astjab.org", "blahblah");
2628 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2629 ASTOBJ_RDLOCK(iterator);
2630 ast_verbose("User: %s\n", iterator->name);
2631 for (resource = iterator->resources; resource; resource = resource->next) {
2632 ast_verbose("Resource: %s\n", resource->resource);
2634 ast_verbose(" client: %s\n", resource->cap->parent->node);
2635 ast_verbose(" version: %s\n", resource->cap->version);
2636 ast_verbose(" Jingle Capable: %d\n", resource->cap->jingle);
2638 ast_verbose(" Priority: %d\n", resource->priority);
2639 ast_verbose(" Status: %d\n", resource->status);
2640 ast_verbose(" Message: %s\n", S_OR(resource->description,""));
2642 ASTOBJ_UNLOCK(iterator);
2644 ast_verbose("\nOooh a working message stack!\n");
2645 AST_LIST_LOCK(&client->messages);
2646 AST_LIST_TRAVERSE(&client->messages, tmp, list) {
2647 ast_verbose(" Message from: %s with id %s @ %s %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, ""));
2649 AST_LIST_UNLOCK(&client->messages);
2650 ASTOBJ_UNREF(client, aji_client_destroy);
2656 * \brief creates aji_client structure.
2658 * \param var ast_variable
2662 static int aji_create_client(char *label, struct ast_variable *var, int debug)
2665 struct aji_client *client = NULL;
2668 client = ASTOBJ_CONTAINER_FIND(&clients,label);
2671 client = ast_calloc(1, sizeof(*client));
2673 ast_log(LOG_ERROR, "Out of memory!\n");
2676 ASTOBJ_INIT(client);
2677 ASTOBJ_WRLOCK(client);
2678 ASTOBJ_CONTAINER_INIT(&client->buddies);
2680 ASTOBJ_WRLOCK(client);
2681 ASTOBJ_UNMARK(client);
2683 ASTOBJ_CONTAINER_MARKALL(&client->buddies);
2684 ast_copy_string(client->name, label, sizeof(client->name));
2685 ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
2687 /* Set default values for the client object */
2688 client->debug = debug;
2689 ast_copy_flags(&client->flags, &globalflags, AST_FLAGS_ALL);
2690 client->port = 5222;
2692 client->usesasl = 1;
2693 client->forcessl = 0;
2694 client->keepalive = 1;
2695 client->timeout = 50;
2696 client->message_timeout = 100;
2697 AST_LIST_HEAD_INIT(&client->messages);
2698 client->component = 0;
2699 ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage));
2700 client->priority = 0;
2701 client->status = IKS_SHOW_AVAILABLE;
2704 client->authorized = 0;
2705 client->state = AJI_DISCONNECTED;
2708 if (!strcasecmp(var->name, "username"))
2709 ast_copy_string(client->user, var->value, sizeof(client->user));
2710 else if (!strcasecmp(var->name, "serverhost"))
2711 ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
2712 else if (!strcasecmp(var->name, "secret"))
2713 ast_copy_string(client->password, var->value, sizeof(client->password));
2714 else if (!strcasecmp(var->name, "statusmessage"))
2715 ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage));
2716 else if (!strcasecmp(var->name, "port"))
2717 client->port = atoi(var->value);
2718 else if (!strcasecmp(var->name, "timeout"))
2719 client->message_timeout = atoi(var->value);
2720 else if (!strcasecmp(var->name, "debug"))
2721 client->debug = (ast_false(var->value)) ? 0 : 1;
2722 else if (!strcasecmp(var->name, "type")) {
2723 if (!strcasecmp(var->value, "component"))
2724 client->component = 1;
2725 } else if (!strcasecmp(var->name, "usetls")) {
2726 client->usetls = (ast_false(var->value)) ? 0 : 1;
2727 } else if (!strcasecmp(var->name, "usesasl")) {
2728 client->usesasl = (ast_false(var->value)) ? 0 : 1;
2729 } else if (!strcasecmp(var->name, "forceoldssl"))
2730 client->forcessl = (ast_false(var->value)) ? 0 : 1;
2731 else if (!strcasecmp(var->name, "keepalive"))
2732 client->keepalive = (ast_false(var->value)) ? 0 : 1;
2733 else if (!strcasecmp(var->name, "autoprune"))
2734 ast_set2_flag(&client->flags, ast_true(var->value), AJI_AUTOPRUNE);
2735 else if (!strcasecmp(var->name, "autoregister"))
2736 ast_set2_flag(&client->flags, ast_true(var->value), AJI_AUTOREGISTER);
2737 else if (!strcasecmp(var->name, "buddy"))
2738 aji_create_buddy((char *)var->value, client);
2739 else if (!strcasecmp(var->name, "priority"))
2740 client->priority = atoi(var->value);
2741 else if (!strcasecmp(var->name, "status")) {
2742 if (!strcasecmp(var->value, "unavailable"))
2743 client->status = IKS_SHOW_UNAVAILABLE;
2745 if (!strcasecmp(var->value, "available")
2746 || !strcasecmp(var->value, "online"))
2747 client->status = IKS_SHOW_AVAILABLE;
2749 if (!strcasecmp(var->value, "chat")
2750 || !strcasecmp(var->value, "chatty"))
2751 client->status = IKS_SHOW_CHAT;
2753 if (!strcasecmp(var->value, "away"))
2754 client->status = IKS_SHOW_AWAY;
2756 if (!strcasecmp(var->value, "xa")
2757 || !strcasecmp(var->value, "xaway"))
2758 client->status = IKS_SHOW_XA;
2760 if (!strcasecmp(var->value, "dnd"))
2761 client->status = IKS_SHOW_DND;
2763 if (!strcasecmp(var->value, "invisible"))
2764 #ifdef IKS_SHOW_INVISIBLE
2765 client->status = IKS_SHOW_INVISIBLE;
2768 ast_log(LOG_WARNING, "Your iksemel doesn't support invisible status: falling back to DND\n");
2769 client->status = IKS_SHOW_DND;
2773 ast_log(LOG_WARNING, "Unknown presence status: %s\n", var->value);
2775 /* no transport support in this version */
2776 /* else if (!strcasecmp(var->name, "transport"))
2777 aji_create_transport(var->value, client);
2782 ASTOBJ_UNLOCK(client);
2783 ASTOBJ_UNREF(client, aji_client_destroy);
2787 ast_copy_string(client->name_space, (client->component) ? "jabber:component:accept" : "jabber:client", sizeof(client->name_space));
2788 client->p = iks_stream_new(client->name_space, client, aji_act_hook);
2790 ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name);
2793 client->stack = iks_stack_new(8192, 8192);
2794 if (!client->stack) {
2795 ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name);
2798 client->f = iks_filter_new();
2800 ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name);
2803 if (!strchr(client->user, '/') && !client->component) { /*client */
2805 if (asprintf(&resource, "%s/asterisk", client->user) >= 0) {
2806 client->jid = iks_id_new(client->stack, resource);
2810 client->jid = iks_id_new(client->stack, client->user);
2811 if (client->component) {
2812 iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2813 iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
2814 iks_filter_add_rule(client->f, aji_register_query_handler, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
2815 iks_filter_add_rule(client->f, aji_register_approve_handler, client, IKS_RULE_SUBTYPE, IKS_TYPE_SET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
2817 iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2819 iks_set_log_hook(client->p, aji_log_hook);
2820 ASTOBJ_UNLOCK(client);
2821 ASTOBJ_CONTAINER_LINK(&clients,client);
2827 * \brief creates transport.
2828 * \param label, buddy to dump it into.
2831 /* no connecting to transports today */
2832 static int aji_create_transport(char *label, struct aji_client *client)
2834 char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
2835 struct aji_buddy *buddy = NULL;
2837 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2839 buddy = ast_calloc(1, sizeof(*buddy));
2841 ast_log(LOG_WARNING, "Out of memory\n");
2846 ASTOBJ_WRLOCK(buddy);
2848 if ((buddyname = strchr(label, ','))) {
2851 if (buddyname && buddyname[0] != '\0') {
2852 if ((user = strchr(buddyname, ','))) {
2855 if (user && user[0] != '\0') {
2856 if ((pass = strchr(user, ','))) {
2859 ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
2860 ast_copy_string(buddy->user, user, sizeof(buddy->user));
2861 ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
2862 ast_copy_string(buddy->server, server, sizeof(buddy->server));
2869 ASTOBJ_UNLOCK(buddy);
2870 ASTOBJ_UNMARK(buddy);
2871 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2877 * \brief creates buddy.
2878 * \param label char.
2879 * \param client the configured XMPP client we use to connect to a XMPP server
2880 * \return 1 on success, 0 on failure.
2882 static int aji_create_buddy(char *label, struct aji_client *client)
2884 struct aji_buddy *buddy = NULL;
2886 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2889 buddy = ast_calloc(1, sizeof(*buddy));
2891 ast_log(LOG_WARNING, "Out of memory\n");
2896 ASTOBJ_WRLOCK(buddy);
2897 ast_copy_string(buddy->name, label, sizeof(buddy->name));
2898 ASTOBJ_UNLOCK(buddy);
2900 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2902 ASTOBJ_UNMARK(buddy);
2903 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2908 /*!< load config file. \return 1. */
2909 static int aji_load_config(int reload)
2913 struct ast_config *cfg = NULL;
2914 struct ast_variable *var = NULL;
2915 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2917 if ((cfg = ast_config_load(JABBER_CONFIG, config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
2920 /* Reset flags to default value */
2921 ast_set_flag(&globalflags, AJI_AUTOREGISTER);
2923 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
2924 ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
2928 cat = ast_category_browse(cfg, NULL);
2929 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
2930 if (!strcasecmp(var->name, "debug")) {
2931 debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
2932 } else if (!strcasecmp(var->name, "autoprune")) {
2933 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
2934 } else if (!strcasecmp(var->name, "autoregister")) {
2935 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
2940 if (strcasecmp(cat, "general")) {
2941 var = ast_variable_browse(cfg, cat);
2942 aji_create_client(cat, var, debug);
2944 cat = ast_category_browse(cfg, cat);
2946 ast_config_destroy(cfg); /* or leak memory */
2951 * \brief grab a aji_client structure by label name or JID
2952 * (without the resource string)
2953 * \param name label or JID
2954 * \return aji_client.
2956 struct aji_client *ast_aji_get_client(const char *name)
2958 struct aji_client *client = NULL;
2961 client = ASTOBJ_CONTAINER_FIND(&clients, name);
2962 if (!client && strchr(name, '@')) {
2963 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2964 aux = ast_strdupa(iterator->user);
2965 if (strchr(aux, '/')) {
2966 /* strip resource for comparison */
2967 aux = strsep(&aux, "/");
2969 if (!strncasecmp(aux, name, strlen(aux))) {
2978 struct aji_client_container *ast_aji_get_clients(void)
2983 static const char mandescr_jabber_send[] =
2984 "Description: Sends a message to a Jabber Client.\n"
2986 " Jabber: Client or transport Asterisk uses to connect to JABBER\n"
2987 " JID: XMPP/Jabber JID (Name) of recipient\n"
2988 " Message: Message to be sent to the buddy\n";
2991 * \brief Send a Jabber Message via call from the Manager
2992 * \param s mansession Manager session
2993 * \param m message Message to send
2996 static int manager_jabber_send(struct mansession *s, const struct message *m)
2998 struct aji_client *client = NULL;
2999 const char *id = astman_get_header(m,"ActionID");
3000 const char *jabber = astman_get_header(m,"Jabber");
3001 const char *screenname = astman_get_header(m,"ScreenName");
3002 const char *message = astman_get_header(m,"Message");
3004 if (ast_strlen_zero(jabber)) {
3005 astman_send_error(s, m, "No transport specified");
3008 if (ast_strlen_zero(screenname)) {
3009 astman_send_error(s, m, "No ScreenName specified");
3012 if (ast_strlen_zero(message)) {
3013 astman_send_error(s, m, "No Message specified");
3017 astman_send_ack(s, m, "Attempting to send Jabber Message");
3018 client = ast_aji_get_client(jabber);
3020 astman_send_error(s, m, "Could not find Sender");
3023 if (strchr(screenname, '@') && message) {
3024 ast_aji_send_chat(client, screenname, message);
3025 astman_append(s, "Response: Success\r\n");
3027 astman_append(s, "Response: Error\r\n");
3029 if (!ast_strlen_zero(id)) {
3030 astman_append(s, "ActionID: %s\r\n",id);
3032 astman_append(s, "\r\n");
3036 /*! \brief Reload the jabber module */
3037 static int aji_reload(int reload)
3041 ASTOBJ_CONTAINER_MARKALL(&clients);
3042 if (!(res = aji_load_config(reload))) {
3043 ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
3045 } else if (res == -1)
3048 ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, aji_client_destroy);
3049 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
3050 ASTOBJ_RDLOCK(iterator);
3051 if(iterator->state == AJI_DISCONNECTED) {
3052 if (!iterator->thread)
3053 ast_pthread_create_background(&iterator->thread, NULL, aji_recv_loop, iterator);
3054 } else if (iterator->state == AJI_CONNECTING)
3055 aji_get_roster(iterator);
3056 ASTOBJ_UNLOCK(iterator);
3062 /*! \brief Unload the jabber module */
3063 static int unload_module(void)
3066 ast_cli_unregister_multiple(aji_cli, ARRAY_LEN(aji_cli));
3067 ast_unregister_application(app_ajisend);
3068 ast_unregister_application(app_ajistatus);
3069 ast_manager_unregister("JabberSend");
3070 ast_custom_function_unregister(&jabberstatus_function);
3072 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
3073 ASTOBJ_RDLOCK(iterator);
3074 ast_debug(3, "JABBER: Releasing and disconnecting client: %s\n", iterator->name);
3075 iterator->state = AJI_DISCONNECTING;
3076 ast_aji_disconnect(iterator);
3077 pthread_join(iterator->thread, NULL);
3078 ASTOBJ_UNLOCK(iterator);
3081 ASTOBJ_CONTAINER_DESTROYALL(&clients, aji_client_destroy);
3082 ASTOBJ_CONTAINER_DESTROY(&clients);
3086 /*! \brief Unload the jabber module */
3087 static int load_module(void)
3089 ASTOBJ_CONTAINER_INIT(&clients);
3091 return AST_MODULE_LOAD_DECLINE;
3092 ast_manager_register2("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send,
3093 "Sends a message to a Jabber Client", mandescr_jabber_send);
3094 ast_register_application_xml(app_ajisend, aji_send_exec);
3095 ast_register_application_xml(app_ajistatus, aji_status_exec);
3096 ast_cli_register_multiple(aji_cli, ARRAY_LEN(aji_cli));
3097 ast_custom_function_register(&jabberstatus_function);
3102 /*! \brief Wrapper for aji_reload */
3103 static int reload(void)
3109 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "AJI - Asterisk Jabber Interface",
3110 .load = load_module,
3111 .unload = unload_module,