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>
169 <manager name="JabberSend" language="en_US">
171 Sends a message to a Jabber Client.
174 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
175 <parameter name="Jabber" required="true">
176 <para>Client or transport Asterisk uses to connect to JABBER.</para>
178 <parameter name="JID" required="true">
179 <para>XMPP/Jabber JID (Name) of recipient.</para>
181 <parameter name="Message" required="true">
182 <para>Message to be sent to the buddy.</para>
186 <para>Sends a message to a Jabber Client.</para>
191 /*! \todo This should really be renamed to xmpp.conf. For backwards compatibility, we
192 need to read both files */
193 #define JABBER_CONFIG "jabber.conf"
203 /*-- Forward declarations */
204 static void aji_buddy_destroy(struct aji_buddy *obj);
205 static void aji_client_destroy(struct aji_client *obj);
206 static int aji_is_secure(struct aji_client *client);
208 static int aji_start_tls(struct aji_client *client);
209 static int aji_tls_handshake(struct aji_client *client);
211 static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout);
212 static int aji_recv(struct aji_client *client, int timeout);
213 static int aji_send_header(struct aji_client *client, const char *to);
214 static int aji_send_raw(struct aji_client *client, const char *xmlstr);
215 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming);
216 static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass);
217 static int aji_act_hook(void *data, int type, iks *node);
218 static void aji_handle_iq(struct aji_client *client, iks *node);
219 static void aji_handle_message(struct aji_client *client, ikspak *pak);
220 static void aji_handle_presence(struct aji_client *client, ikspak *pak);
221 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak);
222 static void *aji_recv_loop(void *data);
223 static int aji_initialize(struct aji_client *client);
224 static int aji_client_connect(void *data, ikspak *pak);
225 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc);
226 static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
227 static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
228 static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
229 static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
230 static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
231 static int aji_create_client(char *label, struct ast_variable *var, int debug);
232 static int aji_create_buddy(char *label, struct aji_client *client);
233 static int aji_reload(int reload);
234 static int aji_load_config(int reload);
235 static void aji_pruneregister(struct aji_client *client);
236 static int aji_filter_roster(void *data, ikspak *pak);
237 static int aji_get_roster(struct aji_client *client);
238 static int aji_client_info_handler(void *data, ikspak *pak);
239 static int aji_dinfo_handler(void *data, ikspak *pak);
240 static int aji_ditems_handler(void *data, ikspak *pak);
241 static int aji_register_query_handler(void *data, ikspak *pak);
242 static int aji_register_approve_handler(void *data, ikspak *pak);
243 static int aji_reconnect(struct aji_client *client);
244 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
245 /* No transports in this version */
247 static int aji_create_transport(char *label, struct aji_client *client);
248 static int aji_register_transport(void *data, ikspak *pak);
249 static int aji_register_transport2(void *data, ikspak *pak);
252 static struct ast_cli_entry aji_cli[] = {
253 AST_CLI_DEFINE(aji_do_set_debug, "Enable/Disable Jabber debug"),
254 AST_CLI_DEFINE(aji_do_reload, "Reload Jabber configuration"),
255 AST_CLI_DEFINE(aji_show_clients, "Show state of clients and components"),
256 AST_CLI_DEFINE(aji_show_buddies, "Show buddy lists of our clients"),
257 AST_CLI_DEFINE(aji_test, "Shows roster, but is generally used for mog's debugging."),
260 static char *app_ajisend = "JabberSend";
262 static char *app_ajistatus = "JabberStatus";
264 struct aji_client_container clients;
265 struct aji_capabilities *capabilities = NULL;
267 /*! \brief Global flags, initialized to default values */
268 static struct ast_flags globalflags = { AJI_AUTOREGISTER };
271 * \brief Deletes the aji_client data structure.
272 * \param obj aji_client The structure we will delete.
275 static void aji_client_destroy(struct aji_client *obj)
277 struct aji_message *tmp;
278 ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, aji_buddy_destroy);
279 ASTOBJ_CONTAINER_DESTROY(&obj->buddies);
280 iks_filter_delete(obj->f);
281 iks_parser_delete(obj->p);
282 iks_stack_delete(obj->stack);
283 AST_LIST_LOCK(&obj->messages);
284 while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) {
288 ast_free(tmp->message);
290 AST_LIST_HEAD_DESTROY(&obj->messages);
295 * \brief Deletes the aji_buddy data structure.
296 * \param obj aji_buddy The structure we will delete.
299 static void aji_buddy_destroy(struct aji_buddy *obj)
301 struct aji_resource *tmp;
303 while ((tmp = obj->resources)) {
304 obj->resources = obj->resources->next;
305 ast_free(tmp->description);
313 * \brief Find version in XML stream and populate our capabilities list
314 * \param node the node attribute in the caps element we'll look for or add to
316 * \param version the version attribute in the caps element we'll look for or
318 * \param pak struct The XML stanza we're processing
319 * \return a pointer to the added or found aji_version structure
321 static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
323 struct aji_capabilities *list = NULL;
324 struct aji_version *res = NULL;
329 node = pak->from->full;
331 version = "none supplied.";
333 if(!strcasecmp(list->node, node)) {
334 res = list->versions;
336 if(!strcasecmp(res->version, version))
340 /* Specified version not found. Let's add it to
341 this node in our capabilities list */
343 res = ast_malloc(sizeof(*res));
345 ast_log(LOG_ERROR, "Out of memory!\n");
350 ast_copy_string(res->version, version, sizeof(res->version));
351 res->next = list->versions;
352 list->versions = res;
358 /* Specified node not found. Let's add it our capabilities list */
360 list = ast_malloc(sizeof(*list));
362 ast_log(LOG_ERROR, "Out of memory!\n");
365 res = ast_malloc(sizeof(*res));
367 ast_log(LOG_ERROR, "Out of memory!\n");
371 ast_copy_string(list->node, node, sizeof(list->node));
372 ast_copy_string(res->version, version, sizeof(res->version));
376 list->versions = res;
377 list->next = capabilities;
383 * \brief Find the aji_resource we want
384 * \param buddy aji_buddy A buddy
386 * \return aji_resource object
388 static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *name)
390 struct aji_resource *res = NULL;
393 res = buddy->resources;
395 if (!strcasecmp(res->resource, name)) {
404 * \brief Jabber GTalk function
406 * \return 1 on success, 0 on failure.
408 static int gtalk_yuck(iks *node)
410 if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps"))
416 * \brief Setup the authentication struct
418 * \param pass password
422 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid)
426 iks_insert_attrib(x, "type", "set");
427 y = iks_insert(x, "query");
428 iks_insert_attrib(y, "xmlns", IKS_NS_AUTH);
429 iks_insert_cdata(iks_insert(y, "username"), id->user, 0);
430 iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0);
434 snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass);
435 ast_sha1_hash(buf, sidpass);
436 iks_insert_cdata(iks_insert(y, "digest"), buf, 0);
438 iks_insert_cdata(iks_insert(y, "password"), pass, 0);
444 * \brief Dial plan function status(). puts the status of watched user
445 into a channel variable.
446 * \param chan ast_channel
448 * \return 0 on success, -1 on error
450 static int aji_status_exec(struct ast_channel *chan, const char *data)
452 struct aji_client *client = NULL;
453 struct aji_buddy *buddy = NULL;
454 struct aji_resource *r = NULL;
458 static int deprecation_warning = 0;
459 AST_DECLARE_APP_ARGS(args,
462 AST_APP_ARG(variable);
464 AST_DECLARE_APP_ARGS(jid,
465 AST_APP_ARG(screenname);
466 AST_APP_ARG(resource);
469 if (deprecation_warning++ % 10 == 0)
470 ast_log(LOG_WARNING, "JabberStatus is deprecated. Please use the JABBER_STATUS dialplan function in the future.\n");
473 ast_log(LOG_ERROR, "Usage: JabberStatus(<sender>,<jid>[/<resource>],<varname>\n");
476 s = ast_strdupa(data);
477 AST_STANDARD_APP_ARGS(args, s);
479 if (args.argc != 3) {
480 ast_log(LOG_ERROR, "JabberStatus() requires 3 arguments.\n");
484 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
486 if (!(client = ast_aji_get_client(args.sender))) {
487 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
490 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
492 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
495 r = aji_find_resource(buddy, jid.resource);
496 if (!r && buddy->resources)
497 r = buddy->resources;
499 ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
502 snprintf(status, sizeof(status), "%d", stat);
503 pbx_builtin_setvar_helper(chan, args.variable, status);
507 static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
509 struct aji_client *client = NULL;
510 struct aji_buddy *buddy = NULL;
511 struct aji_resource *r = NULL;
513 AST_DECLARE_APP_ARGS(args,
517 AST_DECLARE_APP_ARGS(jid,
518 AST_APP_ARG(screenname);
519 AST_APP_ARG(resource);
523 ast_log(LOG_ERROR, "Usage: JABBER_STATUS(<sender>,<jid>[/<resource>])\n");
526 AST_STANDARD_APP_ARGS(args, data);
528 if (args.argc != 2) {
529 ast_log(LOG_ERROR, "JABBER_STATUS requires 2 arguments: sender and jid.\n");
533 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
535 if (!(client = ast_aji_get_client(args.sender))) {
536 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
539 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
541 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
544 r = aji_find_resource(buddy, jid.resource);
545 if (!r && buddy->resources)
546 r = buddy->resources;
548 ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
551 snprintf(buf, buflen, "%d", stat);
555 static struct ast_custom_function jabberstatus_function = {
556 .name = "JABBER_STATUS",
557 .read = acf_jabberstatus_read,
561 * \brief Dial plan function to send a message.
562 * \param chan ast_channel
563 * \param data Data is sender|reciever|message.
564 * \return 0 on success,-1 on error.
566 static int aji_send_exec(struct ast_channel *chan, const char *data)
568 struct aji_client *client = NULL;
570 AST_DECLARE_APP_ARGS(args,
572 AST_APP_ARG(recipient);
573 AST_APP_ARG(message);
577 ast_log(LOG_ERROR, "Usage: JabberSend(<sender>,<recipient>,<message>)\n");
580 s = ast_strdupa(data);
582 AST_STANDARD_APP_ARGS(args, s);
584 ast_log(LOG_ERROR, "JabberSend requires 3 arguments: '%s'\n", data);
588 if (!(client = ast_aji_get_client(args.sender))) {
589 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
592 if (strchr(args.recipient, '@') && !ast_strlen_zero(args.message))
593 ast_aji_send_chat(client, args.recipient, args.message);
598 * \brief Tests whether the connection is secured or not
599 * \return 0 if the connection is not secured
601 static int aji_is_secure(struct aji_client *client)
604 return client->stream_flags & SECURE;
612 * \brief Starts the TLS procedure
613 * \param client the configured XMPP client we use to connect to a XMPP server
614 * \return IKS_OK on success, an error code if sending failed, IKS_NET_TLSFAIL
615 * if OpenSSL is not installed
617 static int aji_start_tls(struct aji_client *client)
621 /* This is sent not encrypted */
622 ret = iks_send_raw(client->p, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
626 client->stream_flags |= TRY_SECURE;
631 * \brief TLS handshake, OpenSSL initialization
632 * \param client the configured XMPP client we use to connect to a XMPP server
633 * \return IKS_OK on success, IKS_NET_TLSFAIL on failure
635 static int aji_tls_handshake(struct aji_client *client)
640 ast_debug(1, "Starting TLS handshake\n");
642 /* Load encryption, hashing algorithms and error strings */
644 SSL_load_error_strings();
646 /* Choose an SSL/TLS protocol version, create SSL_CTX */
647 client->ssl_method = SSLv3_method();
648 client->ssl_context = SSL_CTX_new(client->ssl_method);
649 if (!client->ssl_context)
650 return IKS_NET_TLSFAIL;
652 /* Create new SSL session */
653 client->ssl_session = SSL_new(client->ssl_context);
654 if (!client->ssl_session)
655 return IKS_NET_TLSFAIL;
657 /* Enforce TLS on our XMPP connection */
658 sock = iks_fd(client->p);
659 ret = SSL_set_fd(client->ssl_session, sock);
661 return IKS_NET_TLSFAIL;
663 /* Perform SSL handshake */
664 ret = SSL_connect(client->ssl_session);
666 return IKS_NET_TLSFAIL;
668 client->stream_flags &= (~TRY_SECURE);
669 client->stream_flags |= SECURE;
671 /* Sent over the established TLS connection */
672 ret = aji_send_header(client, client->jid->server);
674 return IKS_NET_TLSFAIL;
676 ast_debug(1, "TLS started with server\n");
680 #endif /* HAVE_OPENSSL */
683 * \brief Secured or unsecured IO socket receiving function
684 * \param client the configured XMPP client we use to connect to a XMPP server
685 * \param buffer the reception buffer
686 * \param buf_len the size of the buffer
687 * \param timeout the select timer
688 * \return the number of read bytes on success, 0 on timeout expiration,
691 static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout)
695 struct timeval tv, *tvptr = NULL;
699 if (aji_is_secure(client)) {
700 sock = SSL_get_fd(client->ssl_session);
704 #endif /* HAVE_OPENSSL */
705 sock = iks_fd(client->p);
707 memset(&tv, 0, sizeof(struct timeval));
712 /* NULL value for tvptr makes ast_select wait indefinitely */
713 tvptr = (timeout != -1) ? &tv : NULL;
715 /* ast_select emulates linux behaviour in terms of timeout handling */
716 res = ast_select(sock + 1, &fds, NULL, NULL, tvptr);
719 if (aji_is_secure(client)) {
720 len = SSL_read(client->ssl_session, buffer, buf_len);
722 #endif /* HAVE_OPENSSL */
723 len = recv(sock, buffer, buf_len, 0);
727 } else if (len <= 0) {
735 * \brief Tries to receive data from the Jabber server
736 * \param client the configured XMPP client we use to connect to a XMPP server
737 * \param timeout the timeout value
738 * This function receives (encrypted or unencrypted) data from the XMPP server,
739 * and passes it to the parser.
740 * \return IKS_OK on success, IKS_NET_RWERR on IO error, IKS_NET_NOCONN, if no
741 * connection available, IKS_NET_EXPIRED on timeout expiration
743 static int aji_recv (struct aji_client *client, int timeout)
746 char buf[NET_IO_BUF_SIZE - 1];
747 char newbuf[NET_IO_BUF_SIZE - 1];
752 memset(buf, 0, sizeof(buf));
753 memset(newbuf, 0, sizeof(newbuf));
756 len = aji_io_recv(client, buf, NET_IO_BUF_SIZE - 2, timeout);
757 if (len < 0) return IKS_NET_RWERR;
758 if (len == 0) return IKS_NET_EXPIRED;
761 /* our iksemel parser won't work as expected if we feed
762 it with XML packets that contain multiple whitespace
763 characters between tags */
766 /* if we stumble on the ending tag character,
767 we skip any whitespace that follows it*/
769 while (isspace(buf[pos+1])) {
773 newbuf[newbufpos] = c;
780 /* Log the message here, because iksemel's logHook is
782 aji_log_hook(client, buf, len, 1);
784 /* let iksemel deal with the string length,
785 and reset our buffer */
786 ret = iks_parse(client->p, newbuf, 0, 0);
787 memset(newbuf, 0, sizeof(newbuf));
791 ast_log(LOG_WARNING, "Parsing failure: Out of memory.\n");
794 ast_log(LOG_WARNING, "Parsing failure: Invalid XML.\n");
797 ast_log(LOG_WARNING, "Parsing failure: Hook returned an error.\n");
803 ast_debug(3, "XML parsing successful\n");
809 * \brief Sends XMPP header to the server
810 * \param client the configured XMPP client we use to connect to a XMPP server
811 * \param to the target XMPP server
812 * \return IKS_OK on success, any other value on failure
814 static int aji_send_header(struct aji_client *client, const char *to)
819 len = 91 + strlen(client->name_space) + 6 + strlen(to) + 16 + 1;
820 msg = iks_malloc(len);
823 sprintf(msg, "<?xml version='1.0'?>"
824 "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='"
825 "%s' to='%s' version='1.0'>", client->name_space, to);
826 err = aji_send_raw(client, msg);
835 * \brief Wraps raw sending
836 * \param client the configured XMPP client we use to connect to a XMPP server
837 * \param x the XMPP packet to send
838 * \return IKS_OK on success, any other value on failure
840 int ast_aji_send(struct aji_client *client, iks *x)
842 return aji_send_raw(client, iks_string(iks_stack(x), x));
846 * \brief Sends an XML string over an XMPP connection
847 * \param client the configured XMPP client we use to connect to a XMPP server
848 * \param xmlstr the XML string to send
849 * The XML data is sent whether the connection is secured or not. In the
850 * latter case, we just call iks_send_raw().
851 * \return IKS_OK on success, any other value on failure
853 static int aji_send_raw(struct aji_client *client, const char *xmlstr)
857 int len = strlen(xmlstr);
859 if (aji_is_secure(client)) {
860 ret = SSL_write(client->ssl_session, xmlstr, len);
862 /* Log the message here, because iksemel's logHook is
864 aji_log_hook(client, xmlstr, len, 0);
869 /* If needed, data will be sent unencrypted, and logHook will
870 be called inside iks_send_raw */
871 ret = iks_send_raw(client->p, xmlstr);
879 * \brief the debug loop.
881 * \param xmpp xml data as string
882 * \param size size of string
883 * \param is_incoming direction of packet 1 for inbound 0 for outbound.
885 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming)
887 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
889 if (!ast_strlen_zero(xmpp))
890 manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s\r\n", client->name, xmpp);
894 ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
896 if( strlen(xmpp) == 1) {
897 if(option_debug > 2 && xmpp[0] == ' ') {
898 ast_verbose("\nJABBER: Keep alive packet\n");
901 ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp);
905 ASTOBJ_UNREF(client, aji_client_destroy);
909 * \brief A wrapper function for iks_start_sasl
910 * \param client the configured XMPP client we use to connect to a XMPP server
911 * \param type the SASL authentication type. Supported types are PLAIN and MD5
913 * \param pass password.
915 * \return IKS_OK on success, IKSNET_NOTSUPP on failure.
917 static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass)
924 /* trigger SASL DIGEST-MD5 only over an unsecured connection.
925 iks_start_sasl is an iksemel API function and relies on GnuTLS,
926 whereas we use OpenSSL */
927 if ((type & IKS_STREAM_SASL_MD5) && !aji_is_secure(client))
928 return iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, username, pass);
929 if (!(type & IKS_STREAM_SASL_PLAIN)) {
930 ast_log(LOG_ERROR, "Server does not support SASL PLAIN authentication\n");
931 return IKS_NET_NOTSUPP;
936 ast_log(LOG_ERROR, "Out of memory.\n");
937 return IKS_NET_NOTSUPP;
940 iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL);
941 len = strlen(username) + strlen(pass) + 3;
943 base64 = alloca((len + 2) * 4 / 3);
944 iks_insert_attrib(x, "mechanism", "PLAIN");
945 snprintf(s, len, "%c%s%c%s", 0, username, 0, pass);
947 /* exclude the NULL training byte from the base64 encoding operation
948 as some XMPP servers will refuse it.
949 The format for authentication is [authzid]\0authcid\0password
950 not [authzid]\0authcid\0password\0 */
951 ast_base64encode(base64, (const unsigned char *) s, len - 1, (len + 2) * 4 / 3);
952 iks_insert_cdata(x, base64, 0);
953 ast_aji_send(client, x);
960 * \brief The action hook parses the inbound packets, constantly running.
961 * \param data aji client structure
962 * \param type type of packet
963 * \param node the actual packet.
964 * \return IKS_OK or IKS_HOOK .
966 static int aji_act_hook(void *data, int type, iks *node)
968 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
974 ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */
975 ASTOBJ_UNREF(client, aji_client_destroy);
979 if (client->state == AJI_DISCONNECTING) {
980 ASTOBJ_UNREF(client, aji_client_destroy);
984 pak = iks_packet(node);
986 if (!client->component) { /*client */
989 if (client->usetls && !aji_is_secure(client)) {
991 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");
992 ASTOBJ_UNREF(client, aji_client_destroy);
995 if (aji_start_tls(client) == IKS_NET_TLSFAIL) {
996 ast_log(LOG_ERROR, "Could not start TLS\n");
997 ASTOBJ_UNREF(client, aji_client_destroy);
1003 if (!client->usesasl) {
1004 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);
1005 auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id"));
1007 iks_insert_attrib(auth, "id", client->mid);
1008 iks_insert_attrib(auth, "to", client->jid->server);
1009 ast_aji_increment_mid(client->mid);
1010 ast_aji_send(client, auth);
1013 ast_log(LOG_ERROR, "Out of memory.\n");
1017 case IKS_NODE_NORMAL:
1019 if (client->stream_flags & TRY_SECURE) {
1020 if (!strcmp("proceed", iks_name(node))) {
1021 return aji_tls_handshake(client);
1025 if (!strcmp("stream:features", iks_name(node))) {
1026 features = iks_stream_features(node);
1027 if (client->usesasl) {
1028 if (client->usetls && !aji_is_secure(client))
1030 if (client->authorized) {
1031 if (features & IKS_STREAM_BIND) {
1032 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);
1033 auth = iks_make_resource_bind(client->jid);
1035 iks_insert_attrib(auth, "id", client->mid);
1036 ast_aji_increment_mid(client->mid);
1037 ast_aji_send(client, auth);
1040 ast_log(LOG_ERROR, "Out of memory.\n");
1044 if (features & IKS_STREAM_SESSION) {
1045 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);
1046 auth = iks_make_session();
1048 iks_insert_attrib(auth, "id", "auth");
1049 ast_aji_increment_mid(client->mid);
1050 ast_aji_send(client, auth);
1053 ast_log(LOG_ERROR, "Out of memory.\n");
1058 if (!client->jid->user) {
1059 ast_log(LOG_ERROR, "Malformed Jabber ID : %s (domain missing?)\n", client->jid->full);
1063 ret = aji_start_sasl(client, features, client->jid->user, client->password);
1064 if (ret != IKS_OK) {
1065 ASTOBJ_UNREF(client, aji_client_destroy);
1071 } else if (!strcmp("failure", iks_name(node))) {
1072 ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n");
1073 } else if (!strcmp("success", iks_name(node))) {
1074 client->authorized = 1;
1075 aji_send_header(client, client->jid->server);
1078 case IKS_NODE_ERROR:
1079 ast_log(LOG_ERROR, "JABBER: Node Error\n");
1080 ASTOBJ_UNREF(client, aji_client_destroy);
1084 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
1085 ASTOBJ_UNREF(client, aji_client_destroy);
1089 } else if (client->state != AJI_CONNECTED && client->component) {
1091 case IKS_NODE_START:
1092 if (client->state == AJI_DISCONNECTED) {
1093 char secret[160], shasum[320], *handshake;
1095 sprintf(secret, "%s%s", pak->id, client->password);
1096 ast_sha1_hash(shasum, secret);
1098 if (asprintf(&handshake, "<handshake>%s</handshake>", shasum) >= 0) {
1099 aji_send_raw(client, handshake);
1100 ast_free(handshake);
1103 client->state = AJI_CONNECTING;
1104 if(aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
1105 client->state = AJI_CONNECTED;
1107 ast_log(LOG_WARNING, "Jabber didn't seem to handshake, failed to authenticate.\n");
1112 case IKS_NODE_NORMAL:
1115 case IKS_NODE_ERROR:
1116 ast_log(LOG_ERROR, "JABBER: Node Error\n");
1117 ASTOBJ_UNREF(client, aji_client_destroy);
1121 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
1122 ASTOBJ_UNREF(client, aji_client_destroy);
1127 switch (pak->type) {
1129 ast_debug(1, "JABBER: I don't know what to do with paktype NONE.\n");
1131 case IKS_PAK_MESSAGE:
1132 aji_handle_message(client, pak);
1133 ast_debug(1, "JABBER: Handling paktype MESSAGE.\n");
1135 case IKS_PAK_PRESENCE:
1136 aji_handle_presence(client, pak);
1137 ast_debug(1, "JABBER: Handling paktype PRESENCE\n");
1140 aji_handle_subscribe(client, pak);
1141 ast_debug(1, "JABBER: Handling paktype S10N\n");
1144 ast_debug(1, "JABBER: Handling paktype IQ\n");
1145 aji_handle_iq(client, node);
1148 ast_debug(1, "JABBER: I don't know anything about paktype '%d'\n", pak->type);
1152 iks_filter_packet(client->f, pak);
1157 ASTOBJ_UNREF(client, aji_client_destroy);
1164 * \return IKS_FILTER_EAT.
1166 static int aji_register_approve_handler(void *data, ikspak *pak)
1168 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1169 iks *iq = NULL, *presence = NULL, *x = NULL;
1172 presence = iks_new("presence");
1174 if (client && iq && presence && x) {
1175 if (!iks_find(pak->query, "remove")) {
1176 iks_insert_attrib(iq, "from", client->jid->full);
1177 iks_insert_attrib(iq, "to", pak->from->full);
1178 iks_insert_attrib(iq, "id", pak->id);
1179 iks_insert_attrib(iq, "type", "result");
1180 ast_aji_send(client, iq);
1182 iks_insert_attrib(presence, "from", client->jid->full);
1183 iks_insert_attrib(presence, "to", pak->from->partial);
1184 iks_insert_attrib(presence, "id", client->mid);
1185 ast_aji_increment_mid(client->mid);
1186 iks_insert_attrib(presence, "type", "subscribe");
1187 iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
1188 iks_insert_node(presence, x);
1189 ast_aji_send(client, presence);
1192 ast_log(LOG_ERROR, "Out of memory.\n");
1197 iks_delete(presence);
1200 ASTOBJ_UNREF(client, aji_client_destroy);
1201 return IKS_FILTER_EAT;
1204 * \brief register handler for incoming querys (IQ's)
1205 * \param data incoming aji_client request
1207 * \return IKS_FILTER_EAT.
1209 static int aji_register_query_handler(void *data, ikspak *pak)
1211 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1212 struct aji_buddy *buddy = NULL;
1214 iks *iq = NULL, *query = NULL;
1216 client = (struct aji_client *) data;
1218 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1220 iks *error = NULL, *notacceptable = NULL;
1222 ast_log(LOG_ERROR, "Someone.... %s tried to register but they aren't allowed\n", pak->from->partial);
1224 query = iks_new("query");
1225 error = iks_new("error");
1226 notacceptable = iks_new("not-acceptable");
1227 if(iq && query && error && notacceptable) {
1228 iks_insert_attrib(iq, "type", "error");
1229 iks_insert_attrib(iq, "from", client->user);
1230 iks_insert_attrib(iq, "to", pak->from->full);
1231 iks_insert_attrib(iq, "id", pak->id);
1232 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
1233 iks_insert_attrib(error, "code" , "406");
1234 iks_insert_attrib(error, "type", "modify");
1235 iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
1236 iks_insert_node(iq, query);
1237 iks_insert_node(iq, error);
1238 iks_insert_node(error, notacceptable);
1239 ast_aji_send(client, iq);
1241 ast_log(LOG_ERROR, "Out of memory.\n");
1245 iks_delete(notacceptable);
1246 } else if (!(node = iks_find_attrib(pak->query, "node"))) {
1247 iks *instructions = NULL;
1248 char *explain = "Welcome to Asterisk - the Open Source PBX.\n";
1250 query = iks_new("query");
1251 instructions = iks_new("instructions");
1252 if (iq && query && instructions && client) {
1253 iks_insert_attrib(iq, "from", client->user);
1254 iks_insert_attrib(iq, "to", pak->from->full);
1255 iks_insert_attrib(iq, "id", pak->id);
1256 iks_insert_attrib(iq, "type", "result");
1257 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
1258 iks_insert_cdata(instructions, explain, 0);
1259 iks_insert_node(iq, query);
1260 iks_insert_node(query, instructions);
1261 ast_aji_send(client, iq);
1263 ast_log(LOG_ERROR, "Out of memory.\n");
1266 iks_delete(instructions);
1270 ASTOBJ_UNREF(client, aji_client_destroy);
1271 return IKS_FILTER_EAT;
1275 * \brief Handles stuff
1278 * \return IKS_FILTER_EAT.
1280 static int aji_ditems_handler(void *data, ikspak *pak)
1282 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1285 if (!(node = iks_find_attrib(pak->query, "node"))) {
1286 iks *iq = NULL, *query = NULL, *item = NULL;
1288 query = iks_new("query");
1289 item = iks_new("item");
1291 if (iq && query && item) {
1292 iks_insert_attrib(iq, "from", client->user);
1293 iks_insert_attrib(iq, "to", pak->from->full);
1294 iks_insert_attrib(iq, "id", pak->id);
1295 iks_insert_attrib(iq, "type", "result");
1296 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1297 iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
1298 iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
1299 iks_insert_attrib(item, "jid", client->user);
1301 iks_insert_node(iq, query);
1302 iks_insert_node(query, item);
1303 ast_aji_send(client, iq);
1305 ast_log(LOG_ERROR, "Out of memory.\n");
1312 } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
1313 iks *iq, *query, *confirm;
1315 query = iks_new("query");
1316 confirm = iks_new("item");
1317 if (iq && query && confirm && client) {
1318 iks_insert_attrib(iq, "from", client->user);
1319 iks_insert_attrib(iq, "to", pak->from->full);
1320 iks_insert_attrib(iq, "id", pak->id);
1321 iks_insert_attrib(iq, "type", "result");
1322 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1323 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
1324 iks_insert_attrib(confirm, "node", "confirmaccount");
1325 iks_insert_attrib(confirm, "name", "Confirm AIM account");
1326 iks_insert_attrib(confirm, "jid", "blog.astjab.org");
1328 iks_insert_node(iq, query);
1329 iks_insert_node(query, confirm);
1330 ast_aji_send(client, iq);
1332 ast_log(LOG_ERROR, "Out of memory.\n");
1337 iks_delete(confirm);
1339 } else if (!strcasecmp(node, "confirmaccount")) {
1340 iks *iq = NULL, *query = NULL, *feature = NULL;
1343 query = iks_new("query");
1344 feature = iks_new("feature");
1346 if (iq && query && feature && client) {
1347 iks_insert_attrib(iq, "from", client->user);
1348 iks_insert_attrib(iq, "to", pak->from->full);
1349 iks_insert_attrib(iq, "id", pak->id);
1350 iks_insert_attrib(iq, "type", "result");
1351 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1352 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1353 iks_insert_node(iq, query);
1354 iks_insert_node(query, feature);
1355 ast_aji_send(client, iq);
1357 ast_log(LOG_ERROR, "Out of memory.\n");
1362 iks_delete(feature);
1365 ASTOBJ_UNREF(client, aji_client_destroy);
1366 return IKS_FILTER_EAT;
1370 * \brief Handle add extra info
1373 * \return IKS_FILTER_EAT
1375 static int aji_client_info_handler(void *data, ikspak *pak)
1377 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1378 struct aji_resource *resource = NULL;
1379 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1381 resource = aji_find_resource(buddy, pak->from->resource);
1382 if (pak->subtype == IKS_TYPE_RESULT) {
1384 ast_log(LOG_NOTICE, "JABBER: Received client info from %s when not requested.\n", pak->from->full);
1385 ASTOBJ_UNREF(client, aji_client_destroy);
1386 return IKS_FILTER_EAT;
1388 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
1389 resource->cap->jingle = 1;
1391 resource->cap->jingle = 0;
1392 } else if (pak->subtype == IKS_TYPE_GET) {
1393 iks *iq, *disco, *ident, *google, *query;
1395 query = iks_new("query");
1396 ident = iks_new("identity");
1397 disco = iks_new("feature");
1398 google = iks_new("feature");
1399 if (iq && ident && disco && google) {
1400 iks_insert_attrib(iq, "from", client->jid->full);
1401 iks_insert_attrib(iq, "to", pak->from->full);
1402 iks_insert_attrib(iq, "type", "result");
1403 iks_insert_attrib(iq, "id", pak->id);
1404 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1405 iks_insert_attrib(ident, "category", "client");
1406 iks_insert_attrib(ident, "type", "pc");
1407 iks_insert_attrib(ident, "name", "asterisk");
1408 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
1409 iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
1410 iks_insert_node(iq, query);
1411 iks_insert_node(query, ident);
1412 iks_insert_node(query, google);
1413 iks_insert_node(query, disco);
1414 ast_aji_send(client, iq);
1416 ast_log(LOG_ERROR, "Out of Memory.\n");
1423 } else if (pak->subtype == IKS_TYPE_ERROR) {
1424 ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
1426 ASTOBJ_UNREF(client, aji_client_destroy);
1427 return IKS_FILTER_EAT;
1430 * \brief Handler of the return info packet
1431 * \param data aji_client
1433 * \return IKS_FILTER_EAT
1435 static int aji_dinfo_handler(void *data, ikspak *pak)
1437 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1439 struct aji_resource *resource = NULL;
1440 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1442 resource = aji_find_resource(buddy, pak->from->resource);
1443 if (pak->subtype == IKS_TYPE_ERROR) {
1444 ast_log(LOG_WARNING, "Recieved error from a client, turn on jabber debug!\n");
1445 return IKS_FILTER_EAT;
1447 if (pak->subtype == IKS_TYPE_RESULT) {
1449 ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full);
1450 ASTOBJ_UNREF(client, aji_client_destroy);
1451 return IKS_FILTER_EAT;
1453 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
1454 resource->cap->jingle = 1;
1456 resource->cap->jingle = 0;
1457 } else if (pak->subtype == IKS_TYPE_GET && !(node = iks_find_attrib(pak->query, "node"))) {
1458 iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search;
1461 query = iks_new("query");
1462 identity = iks_new("identity");
1463 disco = iks_new("feature");
1464 reg = iks_new("feature");
1465 commands = iks_new("feature");
1466 gateway = iks_new("feature");
1467 version = iks_new("feature");
1468 vcard = iks_new("feature");
1469 search = iks_new("feature");
1471 if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
1472 iks_insert_attrib(iq, "from", client->user);
1473 iks_insert_attrib(iq, "to", pak->from->full);
1474 iks_insert_attrib(iq, "id", pak->id);
1475 iks_insert_attrib(iq, "type", "result");
1476 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1477 iks_insert_attrib(identity, "category", "gateway");
1478 iks_insert_attrib(identity, "type", "pstn");
1479 iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
1480 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
1481 iks_insert_attrib(reg, "var", "jabber:iq:register");
1482 iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
1483 iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
1484 iks_insert_attrib(version, "var", "jabber:iq:version");
1485 iks_insert_attrib(vcard, "var", "vcard-temp");
1486 iks_insert_attrib(search, "var", "jabber:iq:search");
1488 iks_insert_node(iq, query);
1489 iks_insert_node(query, identity);
1490 iks_insert_node(query, disco);
1491 iks_insert_node(query, reg);
1492 iks_insert_node(query, commands);
1493 iks_insert_node(query, gateway);
1494 iks_insert_node(query, version);
1495 iks_insert_node(query, vcard);
1496 iks_insert_node(query, search);
1497 ast_aji_send(client, iq);
1499 ast_log(LOG_ERROR, "Out of memory.\n");
1504 iks_delete(identity);
1507 iks_delete(commands);
1508 iks_delete(gateway);
1509 iks_delete(version);
1513 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "http://jabber.org/protocol/commands")) {
1514 iks *iq, *query, *confirm;
1516 query = iks_new("query");
1517 confirm = iks_new("item");
1519 if (iq && query && confirm && client) {
1520 iks_insert_attrib(iq, "from", client->user);
1521 iks_insert_attrib(iq, "to", pak->from->full);
1522 iks_insert_attrib(iq, "id", pak->id);
1523 iks_insert_attrib(iq, "type", "result");
1524 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1525 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
1526 iks_insert_attrib(confirm, "node", "confirmaccount");
1527 iks_insert_attrib(confirm, "name", "Confirm AIM account");
1528 iks_insert_attrib(confirm, "jid", client->user);
1529 iks_insert_node(iq, query);
1530 iks_insert_node(query, confirm);
1531 ast_aji_send(client, iq);
1533 ast_log(LOG_ERROR, "Out of memory.\n");
1538 iks_delete(confirm);
1540 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "confirmaccount")) {
1541 iks *iq, *query, *feature;
1544 query = iks_new("query");
1545 feature = iks_new("feature");
1547 if (iq && query && feature && client) {
1548 iks_insert_attrib(iq, "from", client->user);
1549 iks_insert_attrib(iq, "to", pak->from->full);
1550 iks_insert_attrib(iq, "id", pak->id);
1551 iks_insert_attrib(iq, "type", "result");
1552 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1553 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1554 iks_insert_node(iq, query);
1555 iks_insert_node(query, feature);
1556 ast_aji_send(client, iq);
1558 ast_log(LOG_ERROR, "Out of memory.\n");
1563 iks_delete(feature);
1566 ASTOBJ_UNREF(client, aji_client_destroy);
1567 return IKS_FILTER_EAT;
1571 * \brief Handles \verbatim <iq> \endverbatim tags.
1572 * \param client the configured XMPP client we use to connect to a XMPP server
1576 static void aji_handle_iq(struct aji_client *client, iks *node)
1578 /*Nothing to see here */
1582 * \brief Handles presence packets.
1583 * \param client the configured XMPP client we use to connect to a XMPP server
1584 * \param pak ikspak the node
1586 static void aji_handle_message(struct aji_client *client, ikspak *pak)
1588 struct aji_message *insert, *tmp;
1591 if (!(insert = ast_calloc(1, sizeof(*insert))))
1593 time(&insert->arrived);
1594 if (iks_find_cdata(pak->x, "body"))
1595 insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
1597 ast_copy_string(insert->id, pak->id, sizeof(insert->message));
1599 insert->from = ast_strdup(pak->from->full);
1600 AST_LIST_LOCK(&client->messages);
1601 AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
1603 AST_LIST_REMOVE_CURRENT(list);
1605 ast_free(tmp->from);
1607 ast_free(tmp->message);
1608 } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) {
1610 AST_LIST_REMOVE_CURRENT(list);
1612 ast_free(tmp->from);
1614 ast_free(tmp->message);
1617 AST_LIST_TRAVERSE_SAFE_END;
1618 AST_LIST_INSERT_HEAD(&client->messages, insert, list);
1619 AST_LIST_UNLOCK(&client->messages);
1622 * \brief Check the presence info
1623 * \param client the configured XMPP client we use to connect to a XMPP server
1626 static void aji_handle_presence(struct aji_client *client, ikspak *pak)
1628 int status, priority;
1629 struct aji_buddy *buddy;
1630 struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
1631 char *ver, *node, *descrip, *type;
1633 if(client->state != AJI_CONNECTED)
1634 aji_create_buddy(pak->from->partial, client);
1636 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1637 if (!buddy && pak->from->partial) {
1638 /* allow our jid to be used to log in with another resource */
1639 if (!strcmp((const char *)pak->from->partial, (const char *)client->jid->partial))
1640 aji_create_buddy(pak->from->partial, client);
1642 ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial);
1645 type = iks_find_attrib(pak->x, "type");
1646 if(client->component && type &&!strcasecmp("probe", type)) {
1647 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
1648 ast_verbose("what i was looking for \n");
1650 ASTOBJ_WRLOCK(buddy);
1651 status = (pak->show) ? pak->show : 6;
1652 priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
1653 tmp = buddy->resources;
1654 descrip = ast_strdup(iks_find_cdata(pak->x,"status"));
1656 while (tmp && pak->from->resource) {
1657 if (!strcasecmp(tmp->resource, pak->from->resource)) {
1658 tmp->status = status;
1659 if (tmp->description) ast_free(tmp->description);
1660 tmp->description = descrip;
1662 if (status == 6) { /* Sign off Destroy resource */
1663 if (last && found->next) {
1664 last->next = found->next;
1667 buddy->resources = found->next;
1669 buddy->resources = NULL;
1670 } else if (!found->next) {
1674 buddy->resources = NULL;
1680 /* resource list is sorted by descending priority */
1681 if (tmp->priority != priority) {
1682 found->priority = priority;
1683 if (!last && !found->next)
1684 /* resource was found to be unique,
1687 /* search for resource in our list
1688 and take it out for the moment */
1690 last->next = found->next;
1692 buddy->resources = found->next;
1695 tmp = buddy->resources;
1696 if (!buddy->resources)
1697 buddy->resources = found;
1698 /* priority processing */
1700 /* insert resource back according to
1701 its priority value */
1702 if (found->priority > tmp->priority) {
1704 /* insert within list */
1709 buddy->resources = found;
1713 /* insert at the end of the list */
1728 /* resource not found in our list, create it */
1729 if (!found && status != 6 && pak->from->resource) {
1730 found = ast_calloc(1, sizeof(*found));
1733 ast_log(LOG_ERROR, "Out of memory!\n");
1736 ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
1737 found->status = status;
1738 found->description = descrip;
1739 found->priority = priority;
1742 tmp = buddy->resources;
1744 if (found->priority > tmp->priority) {
1749 buddy->resources = found;
1760 buddy->resources = found;
1763 ASTOBJ_UNLOCK(buddy);
1764 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1766 node = iks_find_attrib(iks_find(pak->x, "c"), "node");
1767 ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
1769 /* handle gmail client's special caps:c tag */
1770 if (!node && !ver) {
1771 node = iks_find_attrib(iks_find(pak->x, "caps:c"), "node");
1772 ver = iks_find_attrib(iks_find(pak->x, "caps:c"), "ver");
1775 /* retrieve capabilites of the new resource */
1776 if(status !=6 && found && !found->cap) {
1777 found->cap = aji_find_version(node, ver, pak);
1778 if(gtalk_yuck(pak->x)) /* gtalk should do discover */
1779 found->cap->jingle = 1;
1780 if(found->cap->jingle && option_debug > 4) {
1781 ast_debug(1,"Special case for google till they support discover.\n");
1786 query = iks_new("query");
1788 iks_insert_attrib(iq, "type", "get");
1789 iks_insert_attrib(iq, "to", pak->from->full);
1790 iks_insert_attrib(iq,"from", client->jid->full);
1791 iks_insert_attrib(iq, "id", client->mid);
1792 ast_aji_increment_mid(client->mid);
1793 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1794 iks_insert_node(iq, query);
1795 ast_aji_send(client, iq);
1798 ast_log(LOG_ERROR, "Out of memory.\n");
1804 switch (pak->subtype) {
1805 case IKS_TYPE_AVAILABLE:
1806 ast_debug(3, "JABBER: I am available ^_* %i\n", pak->subtype);
1808 case IKS_TYPE_UNAVAILABLE:
1809 ast_debug(3, "JABBER: I am unavailable ^_* %i\n", pak->subtype);
1812 ast_debug(3, "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype);
1814 switch (pak->show) {
1815 case IKS_SHOW_UNAVAILABLE:
1816 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1818 case IKS_SHOW_AVAILABLE:
1819 ast_debug(3, "JABBER: type is available\n");
1822 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1825 ast_debug(3, "JABBER: type is away\n");
1828 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1831 ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1834 ast_debug(3, "JABBER: Kinky! how did that happen %i\n", pak->show);
1839 * \brief handles subscription requests.
1840 * \param client the configured XMPP client we use to connect to a XMPP server
1841 * \param pak ikspak iksemel packet.
1844 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
1846 iks *presence = NULL, *status = NULL;
1847 struct aji_buddy* buddy = NULL;
1849 switch (pak->subtype) {
1850 case IKS_TYPE_SUBSCRIBE:
1851 presence = iks_new("presence");
1852 status = iks_new("status");
1853 if (presence && status) {
1854 iks_insert_attrib(presence, "type", "subscribed");
1855 iks_insert_attrib(presence, "to", pak->from->full);
1856 iks_insert_attrib(presence, "from", client->jid->full);
1858 iks_insert_attrib(presence, "id", pak->id);
1859 iks_insert_cdata(status, "Asterisk has approved subscription", 0);
1860 iks_insert_node(presence, status);
1861 ast_aji_send(client, presence);
1863 ast_log(LOG_ERROR, "Unable to allocate nodes\n");
1865 iks_delete(presence);
1868 if (client->component)
1869 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
1870 case IKS_TYPE_SUBSCRIBED:
1871 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1872 if (!buddy && pak->from->partial) {
1873 aji_create_buddy(pak->from->partial, client);
1876 if (option_verbose > 4) {
1877 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1883 * \brief sends messages.
1884 * \param client the configured XMPP client we use to connect to a XMPP server
1889 int ast_aji_send_chat(struct aji_client *client, const char *address, const char *message)
1892 iks *message_packet = NULL;
1893 if (client->state == AJI_CONNECTED) {
1894 message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message);
1895 if (message_packet) {
1896 iks_insert_attrib(message_packet, "from", client->jid->full);
1897 res = ast_aji_send(client, message_packet);
1899 ast_log(LOG_ERROR, "Out of memory.\n");
1902 iks_delete(message_packet);
1904 ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
1909 * \brief create a chatroom.
1910 * \param client the configured XMPP client we use to connect to a XMPP server
1911 * \param room name of room
1912 * \param server name of server
1913 * \param topic topic for the room.
1916 int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
1923 iks_insert_attrib(iq, "type", "get");
1924 iks_insert_attrib(iq, "to", server);
1925 iks_insert_attrib(iq, "id", client->mid);
1926 ast_aji_increment_mid(client->mid);
1927 ast_aji_send(client, iq);
1929 ast_log(LOG_ERROR, "Out of memory.\n");
1937 * \brief join a chatroom.
1938 * \param client the configured XMPP client we use to connect to a XMPP server
1939 * \param room room to join
1942 int ast_aji_join_chat(struct aji_client *client, char *room)
1945 iks *presence = NULL, *priority = NULL;
1946 presence = iks_new("presence");
1947 priority = iks_new("priority");
1948 if (presence && priority && client) {
1949 iks_insert_cdata(priority, "0", 1);
1950 iks_insert_attrib(presence, "to", room);
1951 iks_insert_node(presence, priority);
1952 res = ast_aji_send(client, presence);
1953 iks_insert_cdata(priority, "5", 1);
1954 iks_insert_attrib(presence, "to", room);
1955 res = ast_aji_send(client, presence);
1957 ast_log(LOG_ERROR, "Out of memory.\n");
1959 iks_delete(presence);
1960 iks_delete(priority);
1966 * \brief invite to a chatroom.
1967 * \param client the configured XMPP client we use to connect to a XMPP server
1973 int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
1976 iks *invite, *body, *namespace;
1978 invite = iks_new("message");
1979 body = iks_new("body");
1980 namespace = iks_new("x");
1981 if (client && invite && body && namespace) {
1982 iks_insert_attrib(invite, "to", user);
1983 iks_insert_attrib(invite, "id", client->mid);
1984 ast_aji_increment_mid(client->mid);
1985 iks_insert_cdata(body, message, 0);
1986 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
1987 iks_insert_attrib(namespace, "jid", room);
1988 iks_insert_node(invite, body);
1989 iks_insert_node(invite, namespace);
1990 res = ast_aji_send(client, invite);
1992 ast_log(LOG_ERROR, "Out of memory.\n");
1995 iks_delete(namespace);
2003 * \brief receive message loop.
2007 static void *aji_recv_loop(void *data)
2009 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2012 while(res != IKS_OK) {
2013 ast_debug(3, "JABBER: Connecting.\n");
2014 res = aji_reconnect(client);
2019 if (res == IKS_NET_RWERR || client->timeout == 0) {
2020 while(res != IKS_OK) {
2021 ast_debug(3, "JABBER: reconnecting.\n");
2022 res = aji_reconnect(client);
2027 res = aji_recv(client, 1);
2029 if (client->state == AJI_DISCONNECTING) {
2030 ast_debug(2, "Ending our Jabber client's thread due to a disconnect\n");
2034 /* Decrease timeout if no data received */
2035 if (res == IKS_NET_EXPIRED)
2038 if (res == IKS_HOOK)
2039 ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
2040 else if (res == IKS_NET_TLSFAIL)
2041 ast_log(LOG_ERROR, "JABBER: Failure in TLS.\n");
2042 else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
2043 res = client->keepalive ? aji_send_raw(client, " ") : IKS_OK;
2045 client->timeout = 50;
2047 ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
2048 } else if (res == IKS_NET_RWERR)
2049 ast_log(LOG_WARNING, "JABBER: socket read error\n");
2051 ASTOBJ_UNREF(client, aji_client_destroy);
2056 * \brief increments the mid field for messages and other events.
2060 void ast_aji_increment_mid(char *mid)
2064 for (i = strlen(mid) - 1; i >= 0; i--) {
2065 if (mid[i] != 'z') {
2066 mid[i] = mid[i] + 1;
2075 * \brief attempts to register to a transport.
2076 * \param aji_client struct, and xml packet.
2077 * \return IKS_FILTER_EAT.
2079 /*allows for registering to transport , was too sketch and is out for now. */
2080 static int aji_register_transport(void *data, ikspak *pak)
2082 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2084 struct aji_buddy *buddy = NULL;
2085 iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
2087 if (client && send) {
2088 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2089 ASTOBJ_RDLOCK(iterator);
2090 if (iterator->btype == AJI_TRANS) {
2093 ASTOBJ_UNLOCK(iterator);
2095 iks_filter_remove_hook(client->f, aji_register_transport);
2096 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);
2097 iks_insert_attrib(send, "to", buddy->host);
2098 iks_insert_attrib(send, "id", client->mid);
2099 ast_aji_increment_mid(client->mid);
2100 iks_insert_attrib(send, "from", client->user);
2101 res = ast_aji_send(client, send);
2103 ast_log(LOG_ERROR, "Out of memory.\n");
2107 ASTOBJ_UNREF(client, aji_client_destroy);
2108 return IKS_FILTER_EAT;
2112 * \brief attempts to register to a transport step 2.
2113 * \param aji_client struct, and xml packet.
2114 * \return IKS_FILTER_EAT.
2116 /* more of the same blob of code, too wonky for now*/
2117 static int aji_register_transport2(void *data, ikspak *pak)
2119 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2121 struct aji_buddy *buddy = NULL;
2123 iks *regiq = iks_new("iq");
2124 iks *regquery = iks_new("query");
2125 iks *reguser = iks_new("username");
2126 iks *regpass = iks_new("password");
2128 if (client && regquery && reguser && regpass && regiq) {
2129 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2130 ASTOBJ_RDLOCK(iterator);
2131 if (iterator->btype == AJI_TRANS)
2132 buddy = iterator; ASTOBJ_UNLOCK(iterator);
2134 iks_filter_remove_hook(client->f, aji_register_transport2);
2135 iks_insert_attrib(regiq, "to", buddy->host);
2136 iks_insert_attrib(regiq, "type", "set");
2137 iks_insert_attrib(regiq, "id", client->mid);
2138 ast_aji_increment_mid(client->mid);
2139 iks_insert_attrib(regiq, "from", client->user);
2140 iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
2141 iks_insert_cdata(reguser, buddy->user, 0);
2142 iks_insert_cdata(regpass, buddy->pass, 0);
2143 iks_insert_node(regiq, regquery);
2144 iks_insert_node(regquery, reguser);
2145 iks_insert_node(regquery, regpass);
2146 res = ast_aji_send(client, regiq);
2148 ast_log(LOG_ERROR, "Out of memory.\n");
2152 iks_delete(regquery);
2154 iks_delete(reguser);
2156 iks_delete(regpass);
2157 ASTOBJ_UNREF(client, aji_client_destroy);
2158 return IKS_FILTER_EAT;
2163 * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
2164 * \param client the configured XMPP client we use to connect to a XMPP server
2166 * \note The messages here should be configurable.
2168 static void aji_pruneregister(struct aji_client *client)
2171 iks *removeiq = iks_new("iq");
2172 iks *removequery = iks_new("query");
2173 iks *removeitem = iks_new("item");
2174 iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
2175 if (!client || !removeiq || !removequery || !removeitem || !send) {
2176 ast_log(LOG_ERROR, "Out of memory.\n");
2180 iks_insert_node(removeiq, removequery);
2181 iks_insert_node(removequery, removeitem);
2182 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2183 ASTOBJ_RDLOCK(iterator);
2184 /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
2185 * be called at the same time */
2186 if (ast_test_flag(&iterator->flags, AJI_AUTOPRUNE)) { /* If autoprune is set on jabber.conf */
2187 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
2188 "GoodBye. Your status is no longer needed by Asterisk the Open Source PBX"
2189 " so I am no longer subscribing to your presence.\n"));
2190 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
2191 "GoodBye. You are no longer in the Asterisk config file so I am removing"
2192 " your access to my presence.\n"));
2193 iks_insert_attrib(removeiq, "from", client->jid->full);
2194 iks_insert_attrib(removeiq, "type", "set");
2195 iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
2196 iks_insert_attrib(removeitem, "jid", iterator->name);
2197 iks_insert_attrib(removeitem, "subscription", "remove");
2198 res = ast_aji_send(client, removeiq);
2199 } else if (ast_test_flag(&iterator->flags, AJI_AUTOREGISTER)) {
2200 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name,
2201 "Greetings! I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
2202 ast_clear_flag(&iterator->flags, AJI_AUTOREGISTER);
2204 ASTOBJ_UNLOCK(iterator);
2208 iks_delete(removeiq);
2209 iks_delete(removequery);
2210 iks_delete(removeitem);
2213 ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy);
2217 * \brief filters the roster packet we get back from server.
2219 * \param pak ikspak iksemel packet.
2220 * \return IKS_FILTER_EAT.
2222 static int aji_filter_roster(void *data, ikspak *pak)
2224 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2227 struct aji_buddy *buddy;
2229 client->state = AJI_CONNECTED;
2230 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2231 ASTOBJ_RDLOCK(iterator);
2232 x = iks_child(pak->query);
2235 if (!iks_strcmp(iks_name(x), "item")) {
2236 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
2238 ast_clear_flag(&iterator->flags, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
2244 ast_copy_flags(&iterator->flags, &client->flags, AJI_AUTOREGISTER);
2247 ASTOBJ_UNLOCK(iterator);
2250 x = iks_child(pak->query);
2253 if (iks_strcmp(iks_name(x), "item") == 0) {
2254 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2255 ASTOBJ_RDLOCK(iterator);
2256 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
2258 ASTOBJ_UNLOCK(iterator);
2262 /* found buddy, don't create a new one */
2267 buddy = ast_calloc(1, sizeof(*buddy));
2269 ast_log(LOG_WARNING, "Out of memory\n");
2273 ASTOBJ_WRLOCK(buddy);
2274 ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
2275 ast_clear_flag(&buddy->flags, AST_FLAGS_ALL);
2276 if(ast_test_flag(&client->flags, AJI_AUTOPRUNE)) {
2277 ast_set_flag(&buddy->flags, AJI_AUTOPRUNE);
2279 } else if (!iks_strcmp(iks_find_attrib(x, "subscription"), "none") || !iks_strcmp(iks_find_attrib(x, "subscription"), "from")) {
2280 /* subscribe to buddy's presence only
2281 if we really need to */
2282 ast_set_flag(&buddy->flags, AJI_AUTOREGISTER);
2284 ASTOBJ_UNLOCK(buddy);
2286 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2287 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2294 aji_pruneregister(client);
2296 ASTOBJ_UNREF(client, aji_client_destroy);
2297 return IKS_FILTER_EAT;
2301 * \brief reconnect to jabber server
2302 * \param client the configured XMPP client we use to connect to a XMPP server
2305 static int aji_reconnect(struct aji_client *client)
2310 client->state = AJI_DISCONNECTED;
2313 iks_parser_reset(client->p);
2314 if (client->authorized)
2315 client->authorized = 0;
2317 res = aji_initialize(client);
2323 * \brief Get the roster of jabber users
2324 * \param client the configured XMPP client we use to connect to a XMPP server
2327 static int aji_get_roster(struct aji_client *client)
2330 roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
2333 iks_insert_attrib(roster, "id", "roster");
2334 aji_set_presence(client, NULL, client->jid->full, client->status, client->statusmessage);
2335 ast_aji_send(client, roster);
2344 * \brief connects as a client to jabber server.
2346 * \param pak ikspak iksemel packet
2349 static int aji_client_connect(void *data, ikspak *pak)
2351 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2355 if (client->state == AJI_DISCONNECTED) {
2356 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);
2357 client->state = AJI_CONNECTING;
2358 client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
2359 iks_filter_remove_hook(client->f, aji_client_connect);
2360 if(!client->component) /*client*/
2361 aji_get_roster(client);
2364 ast_log(LOG_ERROR, "Out of memory.\n");
2366 ASTOBJ_UNREF(client, aji_client_destroy);
2371 * \brief prepares client for connect.
2372 * \param client the configured XMPP client we use to connect to a XMPP server
2375 static int aji_initialize(struct aji_client *client)
2377 int connected = IKS_NET_NOCONN;
2380 /* reset stream flags */
2381 client->stream_flags = 0;
2383 /* If it's a component, connect to user, otherwise, connect to server */
2384 connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->component ? client->user : client->jid->server);
2386 if (connected == IKS_NET_NOCONN) {
2387 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
2389 } else if (connected == IKS_NET_NODNS) {
2390 ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to %s\n", client->name, S_OR(client->serverhost, client->jid->server));
2398 * \brief disconnect from jabber server.
2399 * \param client the configured XMPP client we use to connect to a XMPP server
2402 int ast_aji_disconnect(struct aji_client *client)
2405 ast_verb(4, "JABBER: Disconnecting\n");
2407 if (client->stream_flags & SECURE) {
2408 SSL_shutdown(client->ssl_session);
2409 SSL_CTX_free(client->ssl_context);
2410 SSL_free(client->ssl_session);
2413 iks_disconnect(client->p);
2414 iks_parser_delete(client->p);
2415 ASTOBJ_UNREF(client, aji_client_destroy);
2422 * \brief set presence of client.
2423 * \param client the configured XMPP client we use to connect to a XMPP server
2424 * \param to user send it to
2425 * \param from user it came from
2430 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc)
2433 iks *presence = iks_make_pres(level, desc);
2434 iks *cnode = iks_new("c");
2435 iks *priority = iks_new("priority");
2438 if (presence && cnode && client && priority) {
2440 iks_insert_attrib(presence, "to", to);
2442 iks_insert_attrib(presence, "from", from);
2443 snprintf(priorityS, sizeof(priorityS), "%d", client->priority);
2444 iks_insert_cdata(priority, priorityS, strlen(priorityS));
2445 iks_insert_node(presence, priority);
2446 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
2447 iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
2448 iks_insert_attrib(cnode, "ext", "voice-v1");
2449 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
2450 iks_insert_node(presence, cnode);
2451 res = ast_aji_send(client, presence);
2453 ast_log(LOG_ERROR, "Out of memory.\n");
2456 iks_delete(presence);
2457 iks_delete(priority);
2461 * \brief Turn on/off console debugging.
2462 * \return CLI_SUCCESS.
2464 static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2468 e->command = "jabber set debug {on|off}";
2470 "Usage: jabber set debug {on|off}\n"
2471 " Enables/disables dumping of XMPP/Jabber packets for debugging purposes.\n";
2477 if (a->argc != e->args)
2478 return CLI_SHOWUSAGE;
2480 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
2481 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2482 ASTOBJ_RDLOCK(iterator);
2483 iterator->debug = 1;
2484 ASTOBJ_UNLOCK(iterator);
2486 ast_cli(a->fd, "Jabber Debugging Enabled.\n");
2488 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
2489 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2490 ASTOBJ_RDLOCK(iterator);
2491 iterator->debug = 0;
2492 ASTOBJ_UNLOCK(iterator);
2494 ast_cli(a->fd, "Jabber Debugging Disabled.\n");
2497 return CLI_SHOWUSAGE; /* defaults to invalid */
2501 * \brief Reload jabber module.
2502 * \return CLI_SUCCESS.
2504 static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2508 e->command = "jabber reload";
2510 "Usage: jabber reload\n"
2511 " Reloads the Jabber module.\n";
2518 ast_cli(a->fd, "Jabber Reloaded.\n");
2523 * \brief Show client status.
2524 * \return CLI_SUCCESS.
2526 static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2533 e->command = "jabber show connected";
2535 "Usage: jabber show connected\n"
2536 " Shows state of clients and components\n";
2542 ast_cli(a->fd, "Jabber Users and their status:\n");
2543 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2544 ASTOBJ_RDLOCK(iterator);
2546 switch (iterator->state) {
2547 case AJI_DISCONNECTED:
2548 status = "Disconnected";
2550 case AJI_CONNECTING:
2551 status = "Connecting";
2554 status = "Connected";
2559 ast_cli(a->fd, " User: %s - %s\n", iterator->user, status);
2560 ASTOBJ_UNLOCK(iterator);
2562 ast_cli(a->fd, "----\n");
2563 ast_cli(a->fd, " Number of users: %d\n", count);
2568 * \brief Show buddy lists
2569 * \return CLI_SUCCESS.
2571 static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2573 struct aji_resource *resource;
2574 struct aji_client *client;
2578 e->command = "jabber show buddies";
2580 "Usage: jabber show buddies\n"
2581 " Shows buddy lists of our clients\n";
2587 ast_cli(a->fd, "Jabber buddy lists\n");
2588 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2589 ast_cli(a->fd,"Client: %s\n", iterator->user);
2591 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2592 ASTOBJ_RDLOCK(iterator);
2593 ast_cli(a->fd,"\tBuddy:\t%s\n", iterator->name);
2594 if (!iterator->resources)
2595 ast_cli(a->fd,"\t\tResource: None\n");
2596 for (resource = iterator->resources; resource; resource = resource->next) {
2597 ast_cli(a->fd,"\t\tResource: %s\n", resource->resource);
2599 ast_cli(a->fd,"\t\t\tnode: %s\n", resource->cap->parent->node);
2600 ast_cli(a->fd,"\t\t\tversion: %s\n", resource->cap->version);
2601 ast_cli(a->fd,"\t\t\tJingle capable: %s\n", resource->cap->jingle ? "yes" : "no");
2603 ast_cli(a->fd,"\t\tStatus: %d\n", resource->status);
2604 ast_cli(a->fd,"\t\tPriority: %d\n", resource->priority);
2606 ASTOBJ_UNLOCK(iterator);
2614 * \brief Send test message for debugging.
2615 * \return CLI_SUCCESS,CLI_FAILURE.
2617 static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2619 struct aji_client *client;
2620 struct aji_resource *resource;
2621 const char *name = "asterisk";
2622 struct aji_message *tmp;
2626 e->command = "jabber test";
2628 "Usage: jabber test [client]\n"
2629 " Sends test message for debugging purposes. A specific client\n"
2630 " as configured in jabber.conf can be optionally specified.\n";
2637 return CLI_SHOWUSAGE;
2638 else if (a->argc == 3)
2641 if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
2642 ast_cli(a->fd, "Unable to find client '%s'!\n", name);
2646 /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */
2647 ast_aji_send_chat(client, "mogorman@astjab.org", "blahblah");
2648 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2649 ASTOBJ_RDLOCK(iterator);
2650 ast_verbose("User: %s\n", iterator->name);
2651 for (resource = iterator->resources; resource; resource = resource->next) {
2652 ast_verbose("Resource: %s\n", resource->resource);
2654 ast_verbose(" client: %s\n", resource->cap->parent->node);
2655 ast_verbose(" version: %s\n", resource->cap->version);
2656 ast_verbose(" Jingle Capable: %d\n", resource->cap->jingle);
2658 ast_verbose(" Priority: %d\n", resource->priority);
2659 ast_verbose(" Status: %d\n", resource->status);
2660 ast_verbose(" Message: %s\n", S_OR(resource->description,""));
2662 ASTOBJ_UNLOCK(iterator);
2664 ast_verbose("\nOooh a working message stack!\n");
2665 AST_LIST_LOCK(&client->messages);
2666 AST_LIST_TRAVERSE(&client->messages, tmp, list) {
2667 ast_verbose(" Message from: %s with id %s @ %s %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, ""));
2669 AST_LIST_UNLOCK(&client->messages);
2670 ASTOBJ_UNREF(client, aji_client_destroy);
2676 * \brief creates aji_client structure.
2678 * \param var ast_variable
2682 static int aji_create_client(char *label, struct ast_variable *var, int debug)
2685 struct aji_client *client = NULL;
2688 client = ASTOBJ_CONTAINER_FIND(&clients,label);
2691 client = ast_calloc(1, sizeof(*client));
2693 ast_log(LOG_ERROR, "Out of memory!\n");
2696 ASTOBJ_INIT(client);
2697 ASTOBJ_WRLOCK(client);
2698 ASTOBJ_CONTAINER_INIT(&client->buddies);
2700 ASTOBJ_WRLOCK(client);
2701 ASTOBJ_UNMARK(client);
2703 ASTOBJ_CONTAINER_MARKALL(&client->buddies);
2704 ast_copy_string(client->name, label, sizeof(client->name));
2705 ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
2707 /* Set default values for the client object */
2708 client->debug = debug;
2709 ast_copy_flags(&client->flags, &globalflags, AST_FLAGS_ALL);
2710 client->port = 5222;
2712 client->usesasl = 1;
2713 client->forcessl = 0;
2714 client->keepalive = 1;
2715 client->timeout = 50;
2716 client->message_timeout = 100;
2717 AST_LIST_HEAD_INIT(&client->messages);
2718 client->component = 0;
2719 ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage));
2720 client->priority = 0;
2721 client->status = IKS_SHOW_AVAILABLE;
2724 client->authorized = 0;
2725 client->state = AJI_DISCONNECTED;
2728 if (!strcasecmp(var->name, "username"))
2729 ast_copy_string(client->user, var->value, sizeof(client->user));
2730 else if (!strcasecmp(var->name, "serverhost"))
2731 ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
2732 else if (!strcasecmp(var->name, "secret"))
2733 ast_copy_string(client->password, var->value, sizeof(client->password));
2734 else if (!strcasecmp(var->name, "statusmessage"))
2735 ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage));
2736 else if (!strcasecmp(var->name, "port"))
2737 client->port = atoi(var->value);
2738 else if (!strcasecmp(var->name, "timeout"))
2739 client->message_timeout = atoi(var->value);
2740 else if (!strcasecmp(var->name, "debug"))
2741 client->debug = (ast_false(var->value)) ? 0 : 1;
2742 else if (!strcasecmp(var->name, "type")) {
2743 if (!strcasecmp(var->value, "component"))
2744 client->component = 1;
2745 } else if (!strcasecmp(var->name, "usetls")) {
2746 client->usetls = (ast_false(var->value)) ? 0 : 1;
2747 } else if (!strcasecmp(var->name, "usesasl")) {
2748 client->usesasl = (ast_false(var->value)) ? 0 : 1;
2749 } else if (!strcasecmp(var->name, "forceoldssl"))
2750 client->forcessl = (ast_false(var->value)) ? 0 : 1;
2751 else if (!strcasecmp(var->name, "keepalive"))
2752 client->keepalive = (ast_false(var->value)) ? 0 : 1;
2753 else if (!strcasecmp(var->name, "autoprune"))
2754 ast_set2_flag(&client->flags, ast_true(var->value), AJI_AUTOPRUNE);
2755 else if (!strcasecmp(var->name, "autoregister"))
2756 ast_set2_flag(&client->flags, ast_true(var->value), AJI_AUTOREGISTER);
2757 else if (!strcasecmp(var->name, "buddy"))
2758 aji_create_buddy((char *)var->value, client);
2759 else if (!strcasecmp(var->name, "priority"))
2760 client->priority = atoi(var->value);
2761 else if (!strcasecmp(var->name, "status")) {
2762 if (!strcasecmp(var->value, "unavailable"))
2763 client->status = IKS_SHOW_UNAVAILABLE;
2765 if (!strcasecmp(var->value, "available")
2766 || !strcasecmp(var->value, "online"))
2767 client->status = IKS_SHOW_AVAILABLE;
2769 if (!strcasecmp(var->value, "chat")
2770 || !strcasecmp(var->value, "chatty"))
2771 client->status = IKS_SHOW_CHAT;
2773 if (!strcasecmp(var->value, "away"))
2774 client->status = IKS_SHOW_AWAY;
2776 if (!strcasecmp(var->value, "xa")
2777 || !strcasecmp(var->value, "xaway"))
2778 client->status = IKS_SHOW_XA;
2780 if (!strcasecmp(var->value, "dnd"))
2781 client->status = IKS_SHOW_DND;
2783 if (!strcasecmp(var->value, "invisible"))
2784 #ifdef IKS_SHOW_INVISIBLE
2785 client->status = IKS_SHOW_INVISIBLE;
2788 ast_log(LOG_WARNING, "Your iksemel doesn't support invisible status: falling back to DND\n");
2789 client->status = IKS_SHOW_DND;
2793 ast_log(LOG_WARNING, "Unknown presence status: %s\n", var->value);
2795 /* no transport support in this version */
2796 /* else if (!strcasecmp(var->name, "transport"))
2797 aji_create_transport(var->value, client);
2802 ASTOBJ_UNLOCK(client);
2803 ASTOBJ_UNREF(client, aji_client_destroy);
2807 ast_copy_string(client->name_space, (client->component) ? "jabber:component:accept" : "jabber:client", sizeof(client->name_space));
2808 client->p = iks_stream_new(client->name_space, client, aji_act_hook);
2810 ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name);
2813 client->stack = iks_stack_new(8192, 8192);
2814 if (!client->stack) {
2815 ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name);
2818 client->f = iks_filter_new();
2820 ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name);
2823 if (!strchr(client->user, '/') && !client->component) { /*client */
2825 if (asprintf(&resource, "%s/asterisk", client->user) >= 0) {
2826 client->jid = iks_id_new(client->stack, resource);
2830 client->jid = iks_id_new(client->stack, client->user);
2831 if (client->component) {
2832 iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2833 iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
2834 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);
2835 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);
2837 iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2839 iks_set_log_hook(client->p, aji_log_hook);
2840 ASTOBJ_UNLOCK(client);
2841 ASTOBJ_CONTAINER_LINK(&clients,client);
2847 * \brief creates transport.
2848 * \param label, buddy to dump it into.
2851 /* no connecting to transports today */
2852 static int aji_create_transport(char *label, struct aji_client *client)
2854 char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
2855 struct aji_buddy *buddy = NULL;
2857 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2859 buddy = ast_calloc(1, sizeof(*buddy));
2861 ast_log(LOG_WARNING, "Out of memory\n");
2866 ASTOBJ_WRLOCK(buddy);
2868 if ((buddyname = strchr(label, ','))) {
2871 if (buddyname && buddyname[0] != '\0') {
2872 if ((user = strchr(buddyname, ','))) {
2875 if (user && user[0] != '\0') {
2876 if ((pass = strchr(user, ','))) {
2879 ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
2880 ast_copy_string(buddy->user, user, sizeof(buddy->user));
2881 ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
2882 ast_copy_string(buddy->server, server, sizeof(buddy->server));
2889 ASTOBJ_UNLOCK(buddy);
2890 ASTOBJ_UNMARK(buddy);
2891 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2897 * \brief creates buddy.
2898 * \param label char.
2899 * \param client the configured XMPP client we use to connect to a XMPP server
2900 * \return 1 on success, 0 on failure.
2902 static int aji_create_buddy(char *label, struct aji_client *client)
2904 struct aji_buddy *buddy = NULL;
2906 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2909 buddy = ast_calloc(1, sizeof(*buddy));
2911 ast_log(LOG_WARNING, "Out of memory\n");
2916 ASTOBJ_WRLOCK(buddy);
2917 ast_copy_string(buddy->name, label, sizeof(buddy->name));
2918 ASTOBJ_UNLOCK(buddy);
2920 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2922 ASTOBJ_UNMARK(buddy);
2923 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2928 /*!< load config file. \return 1. */
2929 static int aji_load_config(int reload)
2933 struct ast_config *cfg = NULL;
2934 struct ast_variable *var = NULL;
2935 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2937 if ((cfg = ast_config_load(JABBER_CONFIG, config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
2940 /* Reset flags to default value */
2941 ast_set_flag(&globalflags, AJI_AUTOREGISTER);
2943 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
2944 ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
2948 cat = ast_category_browse(cfg, NULL);
2949 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
2950 if (!strcasecmp(var->name, "debug")) {
2951 debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
2952 } else if (!strcasecmp(var->name, "autoprune")) {
2953 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
2954 } else if (!strcasecmp(var->name, "autoregister")) {
2955 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
2960 if (strcasecmp(cat, "general")) {
2961 var = ast_variable_browse(cfg, cat);
2962 aji_create_client(cat, var, debug);
2964 cat = ast_category_browse(cfg, cat);
2966 ast_config_destroy(cfg); /* or leak memory */
2971 * \brief grab a aji_client structure by label name or JID
2972 * (without the resource string)
2973 * \param name label or JID
2974 * \return aji_client.
2976 struct aji_client *ast_aji_get_client(const char *name)
2978 struct aji_client *client = NULL;
2981 client = ASTOBJ_CONTAINER_FIND(&clients, name);
2982 if (!client && strchr(name, '@')) {
2983 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2984 aux = ast_strdupa(iterator->user);
2985 if (strchr(aux, '/')) {
2986 /* strip resource for comparison */
2987 aux = strsep(&aux, "/");
2989 if (!strncasecmp(aux, name, strlen(aux))) {
2998 struct aji_client_container *ast_aji_get_clients(void)
3004 * \brief Send a Jabber Message via call from the Manager
3005 * \param s mansession Manager session
3006 * \param m message Message to send
3009 static int manager_jabber_send(struct mansession *s, const struct message *m)
3011 struct aji_client *client = NULL;
3012 const char *id = astman_get_header(m,"ActionID");
3013 const char *jabber = astman_get_header(m,"Jabber");
3014 const char *screenname = astman_get_header(m,"ScreenName");
3015 const char *message = astman_get_header(m,"Message");
3017 if (ast_strlen_zero(jabber)) {
3018 astman_send_error(s, m, "No transport specified");
3021 if (ast_strlen_zero(screenname)) {
3022 astman_send_error(s, m, "No ScreenName specified");
3025 if (ast_strlen_zero(message)) {
3026 astman_send_error(s, m, "No Message specified");
3030 astman_send_ack(s, m, "Attempting to send Jabber Message");
3031 client = ast_aji_get_client(jabber);
3033 astman_send_error(s, m, "Could not find Sender");
3036 if (strchr(screenname, '@') && message) {
3037 ast_aji_send_chat(client, screenname, message);
3038 astman_append(s, "Response: Success\r\n");
3040 astman_append(s, "Response: Error\r\n");
3042 if (!ast_strlen_zero(id)) {
3043 astman_append(s, "ActionID: %s\r\n",id);
3045 astman_append(s, "\r\n");
3049 /*! \brief Reload the jabber module */
3050 static int aji_reload(int reload)
3054 ASTOBJ_CONTAINER_MARKALL(&clients);
3055 if (!(res = aji_load_config(reload))) {
3056 ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
3058 } else if (res == -1)