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 jabber compliant server.
23 * \extref Iksemel http://code.google.com/p/iksemel/
25 * \todo If you unload this module, chan_gtalk/jingle will be dead. How do we handle that?
30 <depend>iksemel</depend>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/channel.h"
41 #include "asterisk/jabber.h"
42 #include "asterisk/file.h"
43 #include "asterisk/config.h"
44 #include "asterisk/callerid.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/app.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/md5.h"
50 #include "asterisk/acl.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/module.h"
53 #include "asterisk/astobj.h"
54 #include "asterisk/astdb.h"
55 #include "asterisk/manager.h"
57 #define JABBER_CONFIG "jabber.conf"
67 /*-- Forward declarations */
68 static void aji_buddy_destroy(struct aji_buddy *obj);
69 static void aji_client_destroy(struct aji_client *obj);
70 static int aji_send_exec(struct ast_channel *chan, void *data);
71 static int aji_status_exec(struct ast_channel *chan, void *data);
72 static int aji_is_secure(struct aji_client *client);
73 static int aji_start_tls(struct aji_client *client);
74 static int aji_tls_handshake(struct aji_client *client);
75 static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout);
76 static int aji_recv(struct aji_client *client, int timeout);
77 static int aji_send_header(struct aji_client *client, const char *to);
78 static int aji_send_raw(struct aji_client *client, const char *xmlstr);
79 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming);
80 static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass);
81 static int aji_act_hook(void *data, int type, iks *node);
82 static void aji_handle_iq(struct aji_client *client, iks *node);
83 static void aji_handle_message(struct aji_client *client, ikspak *pak);
84 static void aji_handle_presence(struct aji_client *client, ikspak *pak);
85 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak);
86 static void *aji_recv_loop(void *data);
87 static int aji_initialize(struct aji_client *client);
88 static int aji_client_connect(void *data, ikspak *pak);
89 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc);
90 static char *aji_do_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
91 static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
92 static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
93 static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
94 static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
95 static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
96 static int aji_create_client(char *label, struct ast_variable *var, int debug);
97 static int aji_create_buddy(char *label, struct aji_client *client);
98 static int aji_reload(int reload);
99 static int aji_load_config(int reload);
100 static void aji_pruneregister(struct aji_client *client);
101 static int aji_filter_roster(void *data, ikspak *pak);
102 static int aji_get_roster(struct aji_client *client);
103 static int aji_client_info_handler(void *data, ikspak *pak);
104 static int aji_dinfo_handler(void *data, ikspak *pak);
105 static int aji_ditems_handler(void *data, ikspak *pak);
106 static int aji_register_query_handler(void *data, ikspak *pak);
107 static int aji_register_approve_handler(void *data, ikspak *pak);
108 static int aji_reconnect(struct aji_client *client);
109 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
110 /* No transports in this version */
112 static int aji_create_transport(char *label, struct aji_client *client);
113 static int aji_register_transport(void *data, ikspak *pak);
114 static int aji_register_transport2(void *data, ikspak *pak);
117 static struct ast_cli_entry cli_aji_do_debug_deprecated = AST_CLI_DEFINE(aji_do_debug_deprecated, "Enable/disable jabber debugging");
118 static struct ast_cli_entry aji_cli[] = {
119 AST_CLI_DEFINE(aji_do_set_debug, "Enable/Disable Jabber debug", .deprecate_cmd = &cli_aji_do_debug_deprecated),
120 AST_CLI_DEFINE(aji_do_reload, "Reload Jabber configuration"),
121 AST_CLI_DEFINE(aji_show_clients, "Show state of clients and components"),
122 AST_CLI_DEFINE(aji_show_buddies, "Show buddy lists of our clients"),
123 AST_CLI_DEFINE(aji_test, "Shows roster, but is generally used for mog's debugging."),
126 static char *app_ajisend = "JabberSend";
128 static char *ajisend_synopsis = "JabberSend(jabber,screenname,message)";
130 static char *ajisend_descrip =
131 "JabberSend(Jabber,ScreenName,Message)\n"
132 " Jabber - Client or transport Asterisk uses to connect to Jabber\n"
133 " ScreenName - User Name to message.\n"
134 " Message - Message to be sent to the buddy\n";
136 static char *app_ajistatus = "JabberStatus";
138 static char *ajistatus_synopsis = "JabberStatus(Jabber,ScreenName,Variable)";
140 static char *ajistatus_descrip =
141 "JabberStatus(Jabber,ScreenName,Variable)\n"
142 " Jabber - Client or transport Asterisk uses to connect to Jabber\n"
143 " ScreenName - User Name to retrieve status from.\n"
144 " Variable - Variable to store presence in will be 1-6.\n"
145 " In order, Online, Chatty, Away, XAway, DND, Offline\n"
146 " If not in roster variable will = 7\n";
148 struct aji_client_container clients;
149 struct aji_capabilities *capabilities = NULL;
151 /*! \brief Global flags, initialized to default values */
152 static struct ast_flags globalflags = { AJI_AUTOPRUNE | AJI_AUTOREGISTER };
155 * \brief Deletes the aji_client data structure.
156 * \param obj aji_client The structure we will delete.
159 static void aji_client_destroy(struct aji_client *obj)
161 struct aji_message *tmp;
162 ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, aji_buddy_destroy);
163 ASTOBJ_CONTAINER_DESTROY(&obj->buddies);
164 iks_filter_delete(obj->f);
165 iks_parser_delete(obj->p);
166 iks_stack_delete(obj->stack);
167 AST_LIST_LOCK(&obj->messages);
168 while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) {
172 ast_free(tmp->message);
174 AST_LIST_HEAD_DESTROY(&obj->messages);
179 * \brief Deletes the aji_buddy data structure.
180 * \param obj aji_buddy The structure we will delete.
183 static void aji_buddy_destroy(struct aji_buddy *obj)
185 struct aji_resource *tmp;
187 while ((tmp = obj->resources)) {
188 obj->resources = obj->resources->next;
189 ast_free(tmp->description);
197 * \brief Find version in XML stream and populate our capabilities list
198 * \param node the node attribute in the caps element we'll look for or add to
200 * \param version the version attribute in the caps element we'll look for or
202 * \param pak struct The XML stanza we're processing
203 * \return a pointer to the added or found aji_version structure
205 static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
207 struct aji_capabilities *list = NULL;
208 struct aji_version *res = NULL;
213 node = pak->from->full;
215 version = "none supplied.";
217 if(!strcasecmp(list->node, node)) {
218 res = list->versions;
220 if(!strcasecmp(res->version, version))
224 /* Specified version not found. Let's add it to
225 this node in our capabilities list */
227 res = ast_malloc(sizeof(*res));
229 ast_log(LOG_ERROR, "Out of memory!\n");
234 ast_copy_string(res->version, version, sizeof(res->version));
235 res->next = list->versions;
236 list->versions = res;
242 /* Specified node not found. Let's add it our capabilities list */
244 list = ast_malloc(sizeof(*list));
246 ast_log(LOG_ERROR, "Out of memory!\n");
249 res = ast_malloc(sizeof(*res));
251 ast_log(LOG_ERROR, "Out of memory!\n");
255 ast_copy_string(list->node, node, sizeof(list->node));
256 ast_copy_string(res->version, version, sizeof(res->version));
260 list->versions = res;
261 list->next = capabilities;
267 * \brief Find the aji_resource we want
268 * \param buddy aji_buddy A buddy
270 * \return aji_resource object
272 static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *name)
274 struct aji_resource *res = NULL;
277 res = buddy->resources;
279 if (!strcasecmp(res->resource, name)) {
288 * \brief Jabber GTalk function
290 * \return 1 on success, 0 on failure.
292 static int gtalk_yuck(iks *node)
294 if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps"))
300 * \brief Setup the authentication struct
302 * \param pass password
306 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid)
310 iks_insert_attrib(x, "type", "set");
311 y = iks_insert(x, "query");
312 iks_insert_attrib(y, "xmlns", IKS_NS_AUTH);
313 iks_insert_cdata(iks_insert(y, "username"), id->user, 0);
314 iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0);
318 snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass);
319 ast_sha1_hash(buf, sidpass);
320 iks_insert_cdata(iks_insert(y, "digest"), buf, 0);
322 iks_insert_cdata(iks_insert(y, "password"), pass, 0);
328 * \brief Dial plan function status(). puts the status of watched user
329 into a channel variable.
330 * \param chan ast_channel
332 * \return 0 on success, -1 on error
334 static int aji_status_exec(struct ast_channel *chan, void *data)
336 struct aji_client *client = NULL;
337 struct aji_buddy *buddy = NULL;
338 struct aji_resource *r = NULL;
342 static int deprecation_warning = 0;
343 AST_DECLARE_APP_ARGS(args,
346 AST_APP_ARG(variable);
348 AST_DECLARE_APP_ARGS(jid,
349 AST_APP_ARG(screenname);
350 AST_APP_ARG(resource);
353 if (deprecation_warning++ % 10 == 0)
354 ast_log(LOG_WARNING, "JabberStatus is deprecated. Please use the JABBER_STATUS dialplan function in the future.\n");
357 ast_log(LOG_ERROR, "Usage: JabberStatus(<sender>,<screenname>[/<resource>],<varname>\n");
360 s = ast_strdupa(data);
361 AST_STANDARD_APP_ARGS(args, s);
363 if (args.argc != 3) {
364 ast_log(LOG_ERROR, "JabberStatus() requires 3 arguments.\n");
368 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
370 if (!(client = ast_aji_get_client(args.sender))) {
371 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
374 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
376 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
379 r = aji_find_resource(buddy, jid.resource);
380 if (!r && buddy->resources)
381 r = buddy->resources;
383 ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
386 snprintf(status, sizeof(status), "%d", stat);
387 pbx_builtin_setvar_helper(chan, args.variable, status);
391 static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
393 struct aji_client *client = NULL;
394 struct aji_buddy *buddy = NULL;
395 struct aji_resource *r = NULL;
397 AST_DECLARE_APP_ARGS(args,
401 AST_DECLARE_APP_ARGS(jid,
402 AST_APP_ARG(screenname);
403 AST_APP_ARG(resource);
407 ast_log(LOG_ERROR, "Usage: JABBER_STATUS(<sender>,<screenname>[/<resource>])\n");
410 AST_STANDARD_APP_ARGS(args, data);
412 if (args.argc != 2) {
413 ast_log(LOG_ERROR, "JABBER_STATUS requires 2 arguments.\n");
417 AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
419 if (!(client = ast_aji_get_client(args.sender))) {
420 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
423 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
425 ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
428 r = aji_find_resource(buddy, jid.resource);
429 if (!r && buddy->resources)
430 r = buddy->resources;
432 ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
435 snprintf(buf, buflen, "%d", stat);
439 static struct ast_custom_function jabberstatus_function = {
440 .name = "JABBER_STATUS",
441 .synopsis = "Retrieve buddy status",
442 .syntax = "JABBER_STATUS(<sender>,<buddy>[/<resource>])",
443 .read = acf_jabberstatus_read,
445 "Retrieves the numeric status associated with the specified buddy. If the\n"
446 "buddy does not exist in the buddylist, returns 7.\n",
450 * \brief Dial plan function to send a message.
451 * \param chan ast_channel
452 * \param data Data is sender|reciever|message.
453 * \return 0 on success,-1 on error.
455 static int aji_send_exec(struct ast_channel *chan, void *data)
457 struct aji_client *client = NULL;
459 AST_DECLARE_APP_ARGS(args,
461 AST_APP_ARG(recipient);
462 AST_APP_ARG(message);
466 ast_log(LOG_ERROR, "Usage: JabberSend(<sender>,<recipient>,<message>)\n");
469 s = ast_strdupa(data);
471 AST_STANDARD_APP_ARGS(args, s);
473 ast_log(LOG_ERROR, "JabberSend requires 3 arguments: '%s'\n", (char *) data);
477 if (!(client = ast_aji_get_client(args.sender))) {
478 ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
481 if (strchr(args.recipient, '@') && !ast_strlen_zero(args.message))
482 ast_aji_send_chat(client, args.recipient, args.message);
487 * \brief Tests whether the connection is secured or not
488 * \return 0 if the connection is not secured
490 static int aji_is_secure(struct aji_client *client)
493 return client->stream_flags & SECURE;
501 * \brief Starts the TLS procedure
502 * \param client the configured XMPP client we use to connect to a XMPP server
503 * \return IKS_OK on success, an error code if sending failed, IKS_NET_TLSFAIL
504 * if OpenSSL is not installed
506 static int aji_start_tls(struct aji_client *client)
510 return IKS_NET_TLSFAIL;
512 /* This is sent not encrypted */
513 ret = iks_send_raw(client->p, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
516 client->stream_flags |= TRY_SECURE;
522 * \brief TLS handshake, OpenSSL initialization
523 * \param client the configured XMPP client we use to connect to a XMPP server
524 * \return IKS_OK on success, IKS_NET_TLSFAIL on failure
526 static int aji_tls_handshake(struct aji_client *client)
532 return IKS_NET_TLSFAIL;
535 ast_debug(1, "Starting TLS handshake\n");
537 /* Load encryption, hashing algorithms and error strings */
539 SSL_load_error_strings();
541 /* Choose an SSL/TLS protocol version, create SSL_CTX */
542 client->ssl_method = SSLv3_method();
543 client->ssl_context = SSL_CTX_new(client->ssl_method);
544 if (!client->ssl_context)
545 return IKS_NET_TLSFAIL;
547 /* Create new SSL session */
548 client->ssl_session = SSL_new(client->ssl_context);
549 if (!client->ssl_session)
550 return IKS_NET_TLSFAIL;
552 /* Enforce TLS on our XMPP connection */
553 sock = iks_fd(client->p);
554 ret = SSL_set_fd(client->ssl_session, sock);
556 return IKS_NET_TLSFAIL;
558 /* Perform SSL handshake */
559 ret = SSL_connect(client->ssl_session);
561 return IKS_NET_TLSFAIL;
563 client->stream_flags &= (~TRY_SECURE);
564 client->stream_flags |= SECURE;
566 /* Sent over the established TLS connection */
567 ret = aji_send_header(client, client->jid->server);
569 return IKS_NET_TLSFAIL;
571 ast_debug(1, "TLS started with server\n");
577 * \brief Secured or unsecured IO socket receiving function
578 * \param client the configured XMPP client we use to connect to a XMPP server
579 * \param buffer the reception buffer
580 * \param buf_len the size of the buffer
581 * \param timeout the select timer
582 * \return the number of read bytes on success, 0 on timeout expiration,
585 static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout)
589 struct timeval tv, *tvptr = NULL;
593 if (aji_is_secure(client)) {
594 sock = SSL_get_fd(client->ssl_session);
598 #endif /* HAVE_OPENSSL */
599 sock = iks_fd(client->p);
601 memset(&tv, 0, sizeof(struct timeval));
606 /* NULL value for tvptr makes ast_select wait indefinitely */
607 tvptr = (timeout != -1) ? &tv : NULL;
609 /* ast_select emulates linux behaviour in terms of timeout handling */
610 res = ast_select(sock + 1, &fds, NULL, NULL, tvptr);
613 if (aji_is_secure(client)) {
614 len = SSL_read(client->ssl_session, buffer, buf_len);
616 #endif /* HAVE_OPENSSL */
617 len = recv(sock, buffer, buf_len, 0);
621 } else if (len <= 0) {
629 * \brief Tries to receive data from the Jabber server
630 * \param client the configured XMPP client we use to connect to a XMPP server
631 * \param timeout the timeout value
632 * This function receives (encrypted or unencrypted) data from the XMPP server,
633 * and passes it to the parser.
634 * \return IKS_OK on success, IKS_NET_RWERR on IO error, IKS_NET_NOCONN, if no
635 * connection available, IKS_NET_EXPIRED on timeout expiration
637 static int aji_recv (struct aji_client *client, int timeout)
640 char buf[NET_IO_BUF_SIZE -1];
642 memset(buf, 0, sizeof(buf));
645 len = aji_io_recv(client, buf, NET_IO_BUF_SIZE - 1, timeout);
646 if (len < 0) return IKS_NET_RWERR;
647 if (len == 0) return IKS_NET_EXPIRED;
650 /* Log the message here, because iksemel's logHook is
652 aji_log_hook(client, buf, len, 1);
654 ret = iks_parse(client->p, buf, len, 0);
663 * \brief Sends XMPP header to the server
664 * \param client the configured XMPP client we use to connect to a XMPP server
665 * \param to the target XMPP server
666 * \return IKS_OK on success, any other value on failure
668 static int aji_send_header(struct aji_client *client, const char *to)
673 len = 91 + strlen(client->name_space) + 6 + strlen(to) + 16 + 1;
674 msg = iks_malloc(len);
677 sprintf(msg, "<?xml version='1.0'?>"
678 "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='"
679 "%s' to='%s' version='1.0'>", client->name_space, to);
680 err = aji_send_raw(client, msg);
689 * \brief Wraps raw sending
690 * \param client the configured XMPP client we use to connect to a XMPP server
691 * \param x the XMPP packet to send
692 * \return IKS_OK on success, any other value on failure
694 int ast_aji_send(struct aji_client *client, iks *x)
696 return aji_send_raw(client, iks_string(iks_stack(x), x));
700 * \brief Sends an XML string over an XMPP connection
701 * \param client the configured XMPP client we use to connect to a XMPP server
702 * \param xmlstr the XML string to send
703 * The XML data is sent whether the connection is secured or not. In the
704 * latter case, we just call iks_send_raw().
705 * \return IKS_OK on success, any other value on failure
707 static int aji_send_raw(struct aji_client *client, const char *xmlstr)
711 int len = strlen(xmlstr);
713 if (aji_is_secure(client)) {
714 ret = SSL_write(client->ssl_session, xmlstr, len);
716 /* Log the message here, because iksemel's logHook is
718 aji_log_hook(client, xmlstr, len, 0);
723 /* If needed, data will be sent unencrypted, and logHook will
724 be called inside iks_send_raw */
725 ret = iks_send_raw(client->p, xmlstr);
733 * \brief the debug loop.
735 * \param xmpp xml data as string
736 * \param size size of string
737 * \param is_incoming direction of packet 1 for inbound 0 for outbound.
739 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming)
741 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
743 if (!ast_strlen_zero(xmpp))
744 manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s\r\n", client->name, xmpp);
748 ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
750 if( strlen(xmpp) == 1) {
751 if(option_debug > 2 && xmpp[0] == ' ')
752 ast_verbose("\nJABBER: Keep alive packet\n");
754 ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp);
758 ASTOBJ_UNREF(client, aji_client_destroy);
762 * \brief A wrapper function for iks_start_sasl
763 * \param client the configured XMPP client we use to connect to a XMPP server
764 * \param type the SASL authentication type. Supported types are PLAIN and MD5
766 * \param pass password.
768 * \return IKS_OK on success, IKSNET_NOTSUPP on failure.
770 static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass)
777 /* trigger SASL DIGEST-MD5 only over an unsecured connection.
778 iks_start_sasl is an iksemel API function and relies on GnuTLS,
779 whereas we use OpenSSL */
780 if ((type & IKS_STREAM_SASL_MD5) && !aji_is_secure(client))
781 return iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, username, pass);
782 if (!(type & IKS_STREAM_SASL_PLAIN)) {
783 ast_log(LOG_ERROR, "Server does not support SASL PLAIN authentication\n");
784 return IKS_NET_NOTSUPP;
789 ast_log(LOG_ERROR, "Out of memory.\n");
790 return IKS_NET_NOTSUPP;
793 iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL);
794 len = strlen(username) + strlen(pass) + 3;
796 base64 = alloca((len + 2) * 4 / 3);
797 iks_insert_attrib(x, "mechanism", "PLAIN");
798 snprintf(s, len, "%c%s%c%s", 0, username, 0, pass);
800 /* exclude the NULL training byte from the base64 encoding operation
801 as some XMPP servers will refuse it.
802 The format for authentication is [authzid]\0authcid\0password
803 not [authzid]\0authcid\0password\0 */
804 ast_base64encode(base64, (const unsigned char *) s, len - 1, (len + 2) * 4 / 3);
805 iks_insert_cdata(x, base64, 0);
806 ast_aji_send(client, x);
813 * \brief The action hook parses the inbound packets, constantly running.
814 * \param data aji client structure
815 * \param type type of packet
816 * \param node the actual packet.
817 * \return IKS_OK or IKS_HOOK .
819 static int aji_act_hook(void *data, int type, iks *node)
821 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
827 ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */
828 ASTOBJ_UNREF(client, aji_client_destroy);
832 if (client->state == AJI_DISCONNECTING) {
833 ASTOBJ_UNREF(client, aji_client_destroy);
837 pak = iks_packet(node);
839 if (!client->component) { /*client */
842 if (client->usetls && !aji_is_secure(client)) {
843 if (aji_start_tls(client) == IKS_NET_TLSFAIL) {
844 ast_log(LOG_ERROR, "OpenSSL not installed. You need to install OpenSSL on this system\n");
845 ASTOBJ_UNREF(client, aji_client_destroy);
851 if (!client->usesasl) {
852 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);
853 auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id"));
855 iks_insert_attrib(auth, "id", client->mid);
856 iks_insert_attrib(auth, "to", client->jid->server);
857 ast_aji_increment_mid(client->mid);
858 ast_aji_send(client, auth);
861 ast_log(LOG_ERROR, "Out of memory.\n");
865 case IKS_NODE_NORMAL:
866 if (client->stream_flags & TRY_SECURE) {
867 if (!strcmp("proceed", iks_name(node))) {
868 return aji_tls_handshake(client);
872 if (!strcmp("stream:features", iks_name(node))) {
873 features = iks_stream_features(node);
874 if (client->usesasl) {
875 if (client->usetls && !aji_is_secure(client))
877 if (client->authorized) {
878 if (features & IKS_STREAM_BIND) {
879 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);
880 auth = iks_make_resource_bind(client->jid);
882 iks_insert_attrib(auth, "id", client->mid);
883 ast_aji_increment_mid(client->mid);
884 ast_aji_send(client, auth);
887 ast_log(LOG_ERROR, "Out of memory.\n");
891 if (features & IKS_STREAM_SESSION) {
892 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);
893 auth = iks_make_session();
895 iks_insert_attrib(auth, "id", "auth");
896 ast_aji_increment_mid(client->mid);
897 ast_aji_send(client, auth);
900 ast_log(LOG_ERROR, "Out of memory.\n");
905 if (!client->jid->user) {
906 ast_log(LOG_ERROR, "Malformed Jabber ID : %s (domain missing?)\n", client->jid->full);
910 ret = aji_start_sasl(client, features, client->jid->user, client->password);
912 ASTOBJ_UNREF(client, aji_client_destroy);
918 } else if (!strcmp("failure", iks_name(node))) {
919 ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n");
920 } else if (!strcmp("success", iks_name(node))) {
921 client->authorized = 1;
922 aji_send_header(client, client->jid->server);
926 ast_log(LOG_ERROR, "JABBER: Node Error\n");
927 ASTOBJ_UNREF(client, aji_client_destroy);
931 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
932 ASTOBJ_UNREF(client, aji_client_destroy);
936 } else if (client->state != AJI_CONNECTED && client->component) {
939 if (client->state == AJI_DISCONNECTED) {
940 char secret[160], shasum[320], *handshake;
942 sprintf(secret, "%s%s", pak->id, client->password);
943 ast_sha1_hash(shasum, secret);
945 asprintf(&handshake, "<handshake>%s</handshake>", shasum);
947 aji_send_raw(client, handshake);
951 client->state = AJI_CONNECTING;
952 if(aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
953 client->state = AJI_CONNECTED;
955 ast_log(LOG_WARNING, "Jabber didn't seem to handshake, failed to authenticate.\n");
960 case IKS_NODE_NORMAL:
964 ast_log(LOG_ERROR, "JABBER: Node Error\n");
965 ASTOBJ_UNREF(client, aji_client_destroy);
969 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
970 ASTOBJ_UNREF(client, aji_client_destroy);
977 ast_debug(1, "JABBER: I don't know what to do with paktype NONE.\n");
979 case IKS_PAK_MESSAGE:
980 aji_handle_message(client, pak);
981 ast_debug(1, "JABBER: Handling paktype MESSAGE.\n");
983 case IKS_PAK_PRESENCE:
984 aji_handle_presence(client, pak);
985 ast_debug(1, "JABBER: Handling paktype PRESENCE\n");
988 aji_handle_subscribe(client, pak);
989 ast_debug(1, "JABBER: Handling paktype S10N\n");
992 ast_debug(1, "JABBER: Handling paktype IQ\n");
993 aji_handle_iq(client, node);
996 ast_debug(1, "JABBER: I don't know anything about paktype '%d'\n", pak->type);
1000 iks_filter_packet(client->f, pak);
1005 ASTOBJ_UNREF(client, aji_client_destroy);
1012 * \return IKS_FILTER_EAT.
1014 static int aji_register_approve_handler(void *data, ikspak *pak)
1016 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1017 iks *iq = NULL, *presence = NULL, *x = NULL;
1020 presence = iks_new("presence");
1022 if (client && iq && presence && x) {
1023 if (!iks_find(pak->query, "remove")) {
1024 iks_insert_attrib(iq, "from", client->jid->full);
1025 iks_insert_attrib(iq, "to", pak->from->full);
1026 iks_insert_attrib(iq, "id", pak->id);
1027 iks_insert_attrib(iq, "type", "result");
1028 ast_aji_send(client, iq);
1030 iks_insert_attrib(presence, "from", client->jid->full);
1031 iks_insert_attrib(presence, "to", pak->from->partial);
1032 iks_insert_attrib(presence, "id", client->mid);
1033 ast_aji_increment_mid(client->mid);
1034 iks_insert_attrib(presence, "type", "subscribe");
1035 iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
1036 iks_insert_node(presence, x);
1037 ast_aji_send(client, presence);
1040 ast_log(LOG_ERROR, "Out of memory.\n");
1046 iks_delete(presence);
1049 ASTOBJ_UNREF(client, aji_client_destroy);
1050 return IKS_FILTER_EAT;
1053 * \brief register query
1054 * \param data incoming aji_client request
1056 * \return IKS_FILTER_EAT.
1058 static int aji_register_query_handler(void *data, ikspak *pak)
1060 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1061 struct aji_buddy *buddy = NULL;
1064 client = (struct aji_client *) data;
1066 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1068 iks *iq = NULL, *query = NULL, *error = NULL, *notacceptable = NULL;
1070 ast_verbose("Someone.... %s tried to register but they aren't allowed\n", pak->from->partial);
1072 query = iks_new("query");
1073 error = iks_new("error");
1074 notacceptable = iks_new("not-acceptable");
1075 if(iq && query && error && notacceptable) {
1076 iks_insert_attrib(iq, "type", "error");
1077 iks_insert_attrib(iq, "from", client->user);
1078 iks_insert_attrib(iq, "to", pak->from->full);
1079 iks_insert_attrib(iq, "id", pak->id);
1080 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
1081 iks_insert_attrib(error, "code" , "406");
1082 iks_insert_attrib(error, "type", "modify");
1083 iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
1084 iks_insert_node(iq, query);
1085 iks_insert_node(iq, error);
1086 iks_insert_node(error, notacceptable);
1087 ast_aji_send(client, iq);
1089 ast_log(LOG_ERROR, "Out of memory.\n");
1098 iks_delete(notacceptable);
1099 } else if (!(node = iks_find_attrib(pak->query, "node"))) {
1100 iks *iq = NULL, *query = NULL, *instructions = NULL;
1101 char *explain = "Welcome to Asterisk - the Open Source PBX.\n";
1103 query = iks_new("query");
1104 instructions = iks_new("instructions");
1105 if (iq && query && instructions && client) {
1106 iks_insert_attrib(iq, "from", client->user);
1107 iks_insert_attrib(iq, "to", pak->from->full);
1108 iks_insert_attrib(iq, "id", pak->id);
1109 iks_insert_attrib(iq, "type", "result");
1110 iks_insert_attrib(query, "xmlns", "jabber:iq:register");
1111 iks_insert_cdata(instructions, explain, 0);
1112 iks_insert_node(iq, query);
1113 iks_insert_node(query, instructions);
1114 ast_aji_send(client, iq);
1116 ast_log(LOG_ERROR, "Out of memory.\n");
1123 iks_delete(instructions);
1125 ASTOBJ_UNREF(client, aji_client_destroy);
1126 return IKS_FILTER_EAT;
1130 * \brief Handles stuff
1133 * \return IKS_FILTER_EAT.
1135 static int aji_ditems_handler(void *data, ikspak *pak)
1137 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1140 if (!(node = iks_find_attrib(pak->query, "node"))) {
1141 iks *iq = NULL, *query = NULL, *item = NULL;
1143 query = iks_new("query");
1144 item = iks_new("item");
1146 if (iq && query && item) {
1147 iks_insert_attrib(iq, "from", client->user);
1148 iks_insert_attrib(iq, "to", pak->from->full);
1149 iks_insert_attrib(iq, "id", pak->id);
1150 iks_insert_attrib(iq, "type", "result");
1151 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1152 iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
1153 iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
1154 iks_insert_attrib(item, "jid", client->user);
1156 iks_insert_node(iq, query);
1157 iks_insert_node(query, item);
1158 ast_aji_send(client, iq);
1160 ast_log(LOG_ERROR, "Out of memory.\n");
1169 } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
1170 iks *iq, *query, *confirm;
1172 query = iks_new("query");
1173 confirm = iks_new("item");
1174 if (iq && query && confirm && client) {
1175 iks_insert_attrib(iq, "from", client->user);
1176 iks_insert_attrib(iq, "to", pak->from->full);
1177 iks_insert_attrib(iq, "id", pak->id);
1178 iks_insert_attrib(iq, "type", "result");
1179 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1180 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
1181 iks_insert_attrib(confirm, "node", "confirmaccount");
1182 iks_insert_attrib(confirm, "name", "Confirm AIM account");
1183 iks_insert_attrib(confirm, "jid", "blog.astjab.org");
1185 iks_insert_node(iq, query);
1186 iks_insert_node(query, confirm);
1187 ast_aji_send(client, iq);
1189 ast_log(LOG_ERROR, "Out of memory.\n");
1196 iks_delete(confirm);
1198 } else if (!strcasecmp(node, "confirmaccount")) {
1199 iks *iq = NULL, *query = NULL, *feature = NULL;
1202 query = iks_new("query");
1203 feature = iks_new("feature");
1205 if (iq && query && feature && client) {
1206 iks_insert_attrib(iq, "from", client->user);
1207 iks_insert_attrib(iq, "to", pak->from->full);
1208 iks_insert_attrib(iq, "id", pak->id);
1209 iks_insert_attrib(iq, "type", "result");
1210 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1211 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1212 iks_insert_node(iq, query);
1213 iks_insert_node(query, feature);
1214 ast_aji_send(client, iq);
1216 ast_log(LOG_ERROR, "Out of memory.\n");
1223 iks_delete(feature);
1226 ASTOBJ_UNREF(client, aji_client_destroy);
1227 return IKS_FILTER_EAT;
1231 * \brief Handle add extra info
1234 * \return IKS_FILTER_EAT
1236 static int aji_client_info_handler(void *data, ikspak *pak)
1238 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1239 struct aji_resource *resource = NULL;
1240 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1242 resource = aji_find_resource(buddy, pak->from->resource);
1243 if (pak->subtype == IKS_TYPE_RESULT) {
1245 ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full);
1246 ASTOBJ_UNREF(client, aji_client_destroy);
1247 return IKS_FILTER_EAT;
1249 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
1250 resource->cap->jingle = 1;
1252 resource->cap->jingle = 0;
1253 } else if (pak->subtype == IKS_TYPE_GET) {
1254 iks *iq, *disco, *ident, *google, *query;
1256 query = iks_new("query");
1257 ident = iks_new("identity");
1258 disco = iks_new("feature");
1259 google = iks_new("feature");
1260 if (iq && ident && disco && google) {
1261 iks_insert_attrib(iq, "from", client->jid->full);
1262 iks_insert_attrib(iq, "to", pak->from->full);
1263 iks_insert_attrib(iq, "type", "result");
1264 iks_insert_attrib(iq, "id", pak->id);
1265 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1266 iks_insert_attrib(ident, "category", "client");
1267 iks_insert_attrib(ident, "type", "pc");
1268 iks_insert_attrib(ident, "name", "asterisk");
1269 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
1270 iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
1271 iks_insert_node(iq, query);
1272 iks_insert_node(query, ident);
1273 iks_insert_node(query, google);
1274 iks_insert_node(query, disco);
1275 ast_aji_send(client, iq);
1277 ast_log(LOG_ERROR, "Out of Memory.\n");
1288 } else if (pak->subtype == IKS_TYPE_ERROR) {
1289 ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
1291 ASTOBJ_UNREF(client, aji_client_destroy);
1292 return IKS_FILTER_EAT;
1295 * \brief Handler of the return info packet
1296 * \param data aji_client
1298 * \return IKS_FILTER_EAT
1300 static int aji_dinfo_handler(void *data, ikspak *pak)
1302 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1304 struct aji_resource *resource = NULL;
1305 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1307 resource = aji_find_resource(buddy, pak->from->resource);
1308 if (pak->subtype == IKS_TYPE_ERROR) {
1309 ast_log(LOG_WARNING, "Recieved error from a client, turn on jabber debug!\n");
1310 return IKS_FILTER_EAT;
1312 if (pak->subtype == IKS_TYPE_RESULT) {
1314 ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full);
1315 ASTOBJ_UNREF(client, aji_client_destroy);
1316 return IKS_FILTER_EAT;
1318 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
1319 resource->cap->jingle = 1;
1321 resource->cap->jingle = 0;
1322 } else if (pak->subtype == IKS_TYPE_GET && !(node = iks_find_attrib(pak->query, "node"))) {
1323 iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search;
1326 query = iks_new("query");
1327 identity = iks_new("identity");
1328 disco = iks_new("feature");
1329 reg = iks_new("feature");
1330 commands = iks_new("feature");
1331 gateway = iks_new("feature");
1332 version = iks_new("feature");
1333 vcard = iks_new("feature");
1334 search = iks_new("feature");
1336 if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
1337 iks_insert_attrib(iq, "from", client->user);
1338 iks_insert_attrib(iq, "to", pak->from->full);
1339 iks_insert_attrib(iq, "id", pak->id);
1340 iks_insert_attrib(iq, "type", "result");
1341 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1342 iks_insert_attrib(identity, "category", "gateway");
1343 iks_insert_attrib(identity, "type", "pstn");
1344 iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
1345 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
1346 iks_insert_attrib(reg, "var", "jabber:iq:register");
1347 iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
1348 iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
1349 iks_insert_attrib(version, "var", "jabber:iq:version");
1350 iks_insert_attrib(vcard, "var", "vcard-temp");
1351 iks_insert_attrib(search, "var", "jabber:iq:search");
1353 iks_insert_node(iq, query);
1354 iks_insert_node(query, identity);
1355 iks_insert_node(query, disco);
1356 iks_insert_node(query, reg);
1357 iks_insert_node(query, commands);
1358 iks_insert_node(query, gateway);
1359 iks_insert_node(query, version);
1360 iks_insert_node(query, vcard);
1361 iks_insert_node(query, search);
1362 ast_aji_send(client, iq);
1364 ast_log(LOG_ERROR, "Out of memory.\n");
1372 iks_delete(identity);
1378 iks_delete(commands);
1380 iks_delete(gateway);
1382 iks_delete(version);
1388 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "http://jabber.org/protocol/commands")) {
1389 iks *iq, *query, *confirm;
1391 query = iks_new("query");
1392 confirm = iks_new("item");
1394 if (iq && query && confirm && client) {
1395 iks_insert_attrib(iq, "from", client->user);
1396 iks_insert_attrib(iq, "to", pak->from->full);
1397 iks_insert_attrib(iq, "id", pak->id);
1398 iks_insert_attrib(iq, "type", "result");
1399 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
1400 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
1401 iks_insert_attrib(confirm, "node", "confirmaccount");
1402 iks_insert_attrib(confirm, "name", "Confirm AIM account");
1403 iks_insert_attrib(confirm, "jid", client->user);
1404 iks_insert_node(iq, query);
1405 iks_insert_node(query, confirm);
1406 ast_aji_send(client, iq);
1408 ast_log(LOG_ERROR, "Out of memory.\n");
1415 iks_delete(confirm);
1417 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "confirmaccount")) {
1418 iks *iq, *query, *feature;
1421 query = iks_new("query");
1422 feature = iks_new("feature");
1424 if (iq && query && feature && client) {
1425 iks_insert_attrib(iq, "from", client->user);
1426 iks_insert_attrib(iq, "to", pak->from->full);
1427 iks_insert_attrib(iq, "id", pak->id);
1428 iks_insert_attrib(iq, "type", "result");
1429 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1430 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1431 iks_insert_node(iq, query);
1432 iks_insert_node(query, feature);
1433 ast_aji_send(client, iq);
1435 ast_log(LOG_ERROR, "Out of memory.\n");
1442 iks_delete(feature);
1445 ASTOBJ_UNREF(client, aji_client_destroy);
1446 return IKS_FILTER_EAT;
1450 * \brief Handles \verbatim <iq> \endverbatim tags.
1451 * \param client the configured XMPP client we use to connect to a XMPP server
1455 static void aji_handle_iq(struct aji_client *client, iks *node)
1457 /*Nothing to see here */
1461 * \brief Handles presence packets.
1462 * \param client the configured XMPP client we use to connect to a XMPP server
1463 * \param pak ikspak the node
1465 static void aji_handle_message(struct aji_client *client, ikspak *pak)
1467 struct aji_message *insert, *tmp;
1470 if (!(insert = ast_calloc(1, sizeof(*insert))))
1472 time(&insert->arrived);
1473 if (iks_find_cdata(pak->x, "body"))
1474 insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
1476 ast_copy_string(insert->id, pak->id, sizeof(insert->message));
1478 insert->from = ast_strdup(pak->from->full);
1479 AST_LIST_LOCK(&client->messages);
1480 AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
1482 AST_LIST_REMOVE_CURRENT(list);
1484 ast_free(tmp->from);
1486 ast_free(tmp->message);
1487 } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) {
1489 AST_LIST_REMOVE_CURRENT(list);
1491 ast_free(tmp->from);
1493 ast_free(tmp->message);
1496 AST_LIST_TRAVERSE_SAFE_END;
1497 AST_LIST_INSERT_HEAD(&client->messages, insert, list);
1498 AST_LIST_UNLOCK(&client->messages);
1501 * \brief Check the presence info
1502 * \param client the configured XMPP client we use to connect to a XMPP server
1505 static void aji_handle_presence(struct aji_client *client, ikspak *pak)
1507 int status, priority;
1508 struct aji_buddy *buddy;
1509 struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
1510 char *ver, *node, *descrip, *type;
1512 if(client->state != AJI_CONNECTED)
1513 aji_create_buddy(pak->from->partial, client);
1515 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1516 if (!buddy && pak->from->partial) {
1517 /* allow our jid to be used to log in with another resource */
1518 if (!strcmp((const char *)pak->from->partial, (const char *)client->jid->partial))
1519 aji_create_buddy(pak->from->partial, client);
1521 ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial);
1524 type = iks_find_attrib(pak->x, "type");
1525 if(client->component && type &&!strcasecmp("probe", type)) {
1526 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
1527 ast_verbose("what i was looking for \n");
1529 ASTOBJ_WRLOCK(buddy);
1530 status = (pak->show) ? pak->show : 6;
1531 priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
1532 tmp = buddy->resources;
1533 descrip = ast_strdup(iks_find_cdata(pak->x,"status"));
1535 while (tmp && pak->from->resource) {
1536 if (!strcasecmp(tmp->resource, pak->from->resource)) {
1537 tmp->status = status;
1538 if (tmp->description) ast_free(tmp->description);
1539 tmp->description = descrip;
1541 if (status == 6) { /* Sign off Destroy resource */
1542 if (last && found->next) {
1543 last->next = found->next;
1546 buddy->resources = found->next;
1548 buddy->resources = NULL;
1549 } else if (!found->next) {
1553 buddy->resources = NULL;
1559 /* resource list is sorted by descending priority */
1560 if (tmp->priority != priority) {
1561 found->priority = priority;
1562 if (!last && !found->next)
1563 /* resource was found to be unique,
1566 /* search for resource in our list
1567 and take it out for the moment */
1569 last->next = found->next;
1571 buddy->resources = found->next;
1574 tmp = buddy->resources;
1575 if (!buddy->resources)
1576 buddy->resources = found;
1577 /* priority processing */
1579 /* insert resource back according to
1580 its priority value */
1581 if (found->priority > tmp->priority) {
1583 /* insert within list */
1588 buddy->resources = found;
1592 /* insert at the end of the list */
1607 /* resource not found in our list, create it */
1608 if (!found && status != 6 && pak->from->resource) {
1609 found = ast_calloc(1, sizeof(*found));
1612 ast_log(LOG_ERROR, "Out of memory!\n");
1615 ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
1616 found->status = status;
1617 found->description = descrip;
1618 found->priority = priority;
1621 tmp = buddy->resources;
1623 if (found->priority > tmp->priority) {
1628 buddy->resources = found;
1639 buddy->resources = found;
1642 ASTOBJ_UNLOCK(buddy);
1643 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1645 node = iks_find_attrib(iks_find(pak->x, "c"), "node");
1646 ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
1648 /* handle gmail client's special caps:c tag */
1649 if (!node && !ver) {
1650 node = iks_find_attrib(iks_find(pak->x, "caps:c"), "node");
1651 ver = iks_find_attrib(iks_find(pak->x, "caps:c"), "ver");
1654 /* retrieve capabilites of the new resource */
1655 if(status !=6 && found && !found->cap) {
1656 found->cap = aji_find_version(node, ver, pak);
1657 if(gtalk_yuck(pak->x)) /* gtalk should do discover */
1658 found->cap->jingle = 1;
1659 if(found->cap->jingle && option_debug > 4) {
1660 ast_debug(1,"Special case for google till they support discover.\n");
1665 query = iks_new("query");
1667 iks_insert_attrib(iq, "type", "get");
1668 iks_insert_attrib(iq, "to", pak->from->full);
1669 iks_insert_attrib(iq,"from", client->jid->full);
1670 iks_insert_attrib(iq, "id", client->mid);
1671 ast_aji_increment_mid(client->mid);
1672 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1673 iks_insert_node(iq, query);
1674 ast_aji_send(client, iq);
1677 ast_log(LOG_ERROR, "Out of memory.\n");
1684 switch (pak->subtype) {
1685 case IKS_TYPE_AVAILABLE:
1686 ast_verb(5, "JABBER: I am available ^_* %i\n", pak->subtype);
1688 case IKS_TYPE_UNAVAILABLE:
1689 ast_verb(5, "JABBER: I am unavailable ^_* %i\n", pak->subtype);
1692 ast_verb(5, "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype);
1694 switch (pak->show) {
1695 case IKS_SHOW_UNAVAILABLE:
1696 ast_verb(5, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1698 case IKS_SHOW_AVAILABLE:
1699 ast_verb(5, "JABBER: type is available\n");
1702 ast_verb(5, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1705 ast_verb(5, "JABBER: type is away\n");
1708 ast_verb(5, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1711 ast_verb(5, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1714 ast_verb(5, "JABBER: Kinky! how did that happen %i\n", pak->show);
1719 * \brief handles subscription requests.
1720 * \param client the configured XMPP client we use to connect to a XMPP server
1721 * \param pak ikspak iksemel packet.
1724 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
1726 if (pak->subtype == IKS_TYPE_SUBSCRIBE) {
1727 iks *presence = NULL, *status = NULL;
1728 presence = iks_new("presence");
1729 status = iks_new("status");
1730 if (presence && status) {
1731 iks_insert_attrib(presence, "type", "subscribed");
1732 iks_insert_attrib(presence, "to", pak->from->full);
1733 iks_insert_attrib(presence, "from", client->jid->full);
1735 iks_insert_attrib(presence, "id", pak->id);
1736 iks_insert_cdata(status, "Asterisk has approved subscription", 0);
1737 iks_insert_node(presence, status);
1738 ast_aji_send(client, presence);
1740 ast_log(LOG_ERROR, "Unable to allocate nodes\n");
1742 iks_delete(presence);
1745 if (client->component)
1746 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
1749 switch (pak->subtype) {
1750 case IKS_TYPE_SUBSCRIBE:
1751 ast_verb(5, "JABBER: Subscribe handled.\n");
1753 case IKS_TYPE_SUBSCRIBED:
1754 ast_verb(5, "JABBER: Subscribed (%d) not handled.\n", pak->subtype);
1756 case IKS_TYPE_UNSUBSCRIBE:
1757 ast_verb(5, "JABBER: Unsubscribe (%d) not handled.\n", pak->subtype);
1759 case IKS_TYPE_UNSUBSCRIBED:
1760 ast_verb(5, "JABBER: Unsubscribed (%d) not handled.\n", pak->subtype);
1762 default: /*IKS_TYPE_ERROR: */
1763 ast_verb(5, "JABBER: Unknown pak subtype %d.\n", pak->subtype);
1769 * \brief sends messages.
1770 * \param client the configured XMPP client we use to connect to a XMPP server
1775 int ast_aji_send_chat(struct aji_client *client, const char *address, const char *message)
1778 iks *message_packet = NULL;
1779 if (client->state == AJI_CONNECTED) {
1780 message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message);
1781 if (message_packet) {
1782 iks_insert_attrib(message_packet, "from", client->jid->full);
1783 res = ast_aji_send(client, message_packet);
1785 ast_log(LOG_ERROR, "Out of memory.\n");
1788 iks_delete(message_packet);
1790 ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
1795 * \brief create a chatroom.
1796 * \param client the configured XMPP client we use to connect to a XMPP server
1797 * \param room name of room
1798 * \param server name of server
1799 * \param topic topic for the room.
1802 int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
1808 iks_insert_attrib(iq, "type", "get");
1809 iks_insert_attrib(iq, "to", server);
1810 iks_insert_attrib(iq, "id", client->mid);
1811 ast_aji_increment_mid(client->mid);
1812 ast_aji_send(client, iq);
1814 ast_log(LOG_ERROR, "Out of memory.\n");
1819 * \brief join a chatroom.
1820 * \param client the configured XMPP client we use to connect to a XMPP server
1821 * \param room room to join
1824 int ast_aji_join_chat(struct aji_client *client, char *room)
1827 iks *presence = NULL, *priority = NULL;
1828 presence = iks_new("presence");
1829 priority = iks_new("priority");
1830 if (presence && priority && client) {
1831 iks_insert_cdata(priority, "0", 1);
1832 iks_insert_attrib(presence, "to", room);
1833 iks_insert_node(presence, priority);
1834 res = ast_aji_send(client, presence);
1835 iks_insert_cdata(priority, "5", 1);
1836 iks_insert_attrib(presence, "to", room);
1837 res = ast_aji_send(client, presence);
1839 ast_log(LOG_ERROR, "Out of memory.\n");
1841 iks_delete(presence);
1843 iks_delete(priority);
1848 * \brief invite to a chatroom.
1849 * \param client the configured XMPP client we use to connect to a XMPP server
1855 int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
1858 iks *invite, *body, *namespace;
1860 invite = iks_new("message");
1861 body = iks_new("body");
1862 namespace = iks_new("x");
1863 if (client && invite && body && namespace) {
1864 iks_insert_attrib(invite, "to", user);
1865 iks_insert_attrib(invite, "id", client->mid);
1866 ast_aji_increment_mid(client->mid);
1867 iks_insert_cdata(body, message, 0);
1868 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
1869 iks_insert_attrib(namespace, "jid", room);
1870 iks_insert_node(invite, body);
1871 iks_insert_node(invite, namespace);
1872 res = ast_aji_send(client, invite);
1874 ast_log(LOG_ERROR, "Out of memory.\n");
1878 iks_delete(namespace);
1886 * \brief receive message loop.
1890 static void *aji_recv_loop(void *data)
1892 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1895 while(res != IKS_OK) {
1896 ast_verb(4, "JABBER: Connecting.\n");
1897 res = aji_reconnect(client);
1902 if (res == IKS_NET_RWERR || client->timeout == 0) {
1903 while(res != IKS_OK) {
1904 ast_verb(4, "JABBER: reconnecting.\n");
1905 res = aji_reconnect(client);
1910 res = aji_recv(client, 1);
1912 if (client->state == AJI_DISCONNECTING) {
1913 ast_debug(2, "Ending our Jabber client's thread due to a disconnect\n");
1917 /* Decrease timeout if no data received */
1918 if (res == IKS_NET_EXPIRED)
1921 if (res == IKS_HOOK)
1922 ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
1923 else if (res == IKS_NET_TLSFAIL)
1924 ast_log(LOG_WARNING, "JABBER: Failure in TLS.\n");
1925 else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
1926 res = aji_send_raw(client, " ");
1928 client->timeout = 50;
1930 ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
1931 } else if (res == IKS_NET_RWERR)
1932 ast_log(LOG_WARNING, "JABBER: socket read error\n");
1934 ASTOBJ_UNREF(client, aji_client_destroy);
1939 * \brief increments the mid field for messages and other events.
1943 void ast_aji_increment_mid(char *mid)
1947 for (i = strlen(mid) - 1; i >= 0; i--) {
1948 if (mid[i] != 'z') {
1949 mid[i] = mid[i] + 1;
1958 * \brief attempts to register to a transport.
1959 * \param aji_client struct, and xml packet.
1960 * \return IKS_FILTER_EAT.
1962 /*allows for registering to transport , was too sketch and is out for now. */
1963 static int aji_register_transport(void *data, ikspak *pak)
1965 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1967 struct aji_buddy *buddy = NULL;
1968 iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
1970 if (client && send) {
1971 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1972 ASTOBJ_RDLOCK(iterator);
1973 if (iterator->btype == AJI_TRANS) {
1976 ASTOBJ_UNLOCK(iterator);
1978 iks_filter_remove_hook(client->f, aji_register_transport);
1979 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);
1980 iks_insert_attrib(send, "to", buddy->host);
1981 iks_insert_attrib(send, "id", client->mid);
1982 ast_aji_increment_mid(client->mid);
1983 iks_insert_attrib(send, "from", client->user);
1984 res = ast_aji_send(client, send);
1986 ast_log(LOG_ERROR, "Out of memory.\n");
1990 ASTOBJ_UNREF(client, aji_client_destroy);
1991 return IKS_FILTER_EAT;
1995 * \brief attempts to register to a transport step 2.
1996 * \param aji_client struct, and xml packet.
1997 * \return IKS_FILTER_EAT.
1999 /* more of the same blob of code, too wonky for now*/
2000 static int aji_register_transport2(void *data, ikspak *pak)
2002 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2004 struct aji_buddy *buddy = NULL;
2006 iks *regiq = iks_new("iq");
2007 iks *regquery = iks_new("query");
2008 iks *reguser = iks_new("username");
2009 iks *regpass = iks_new("password");
2011 if (client && regquery && reguser && regpass && regiq) {
2012 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2013 ASTOBJ_RDLOCK(iterator);
2014 if (iterator->btype == AJI_TRANS)
2015 buddy = iterator; ASTOBJ_UNLOCK(iterator);
2017 iks_filter_remove_hook(client->f, aji_register_transport2);
2018 iks_insert_attrib(regiq, "to", buddy->host);
2019 iks_insert_attrib(regiq, "type", "set");
2020 iks_insert_attrib(regiq, "id", client->mid);
2021 ast_aji_increment_mid(client->mid);
2022 iks_insert_attrib(regiq, "from", client->user);
2023 iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
2024 iks_insert_cdata(reguser, buddy->user, 0);
2025 iks_insert_cdata(regpass, buddy->pass, 0);
2026 iks_insert_node(regiq, regquery);
2027 iks_insert_node(regquery, reguser);
2028 iks_insert_node(regquery, regpass);
2029 res = ast_aji_send(client, regiq);
2031 ast_log(LOG_ERROR, "Out of memory.\n");
2035 iks_delete(regquery);
2037 iks_delete(reguser);
2039 iks_delete(regpass);
2040 ASTOBJ_UNREF(client, aji_client_destroy);
2041 return IKS_FILTER_EAT;
2046 * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
2047 * \param client the configured XMPP client we use to connect to a XMPP server
2050 static void aji_pruneregister(struct aji_client *client)
2053 iks *removeiq = iks_new("iq");
2054 iks *removequery = iks_new("query");
2055 iks *removeitem = iks_new("item");
2056 iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
2058 if (client && removeiq && removequery && removeitem && send) {
2059 iks_insert_node(removeiq, removequery);
2060 iks_insert_node(removequery, removeitem);
2061 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2062 ASTOBJ_RDLOCK(iterator);
2063 /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
2064 * be called at the same time */
2065 if (ast_test_flag(iterator, AJI_AUTOPRUNE)) {
2066 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
2067 "GoodBye your status is no longer needed by Asterisk the Open Source PBX"
2068 " so I am no longer subscribing to your presence.\n"));
2069 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
2070 "GoodBye you are no longer in the asterisk config file so I am removing"
2071 " your access to my presence.\n"));
2072 iks_insert_attrib(removeiq, "from", client->jid->full);
2073 iks_insert_attrib(removeiq, "type", "set");
2074 iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
2075 iks_insert_attrib(removeitem, "jid", iterator->name);
2076 iks_insert_attrib(removeitem, "subscription", "remove");
2077 res = ast_aji_send(client, removeiq);
2078 } else if (ast_test_flag(iterator, AJI_AUTOREGISTER)) {
2079 res = ast_aji_send(client, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name,
2080 "Greetings I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
2081 ast_clear_flag(iterator, AJI_AUTOREGISTER);
2083 ASTOBJ_UNLOCK(iterator);
2086 ast_log(LOG_ERROR, "Out of memory.\n");
2088 iks_delete(removeiq);
2090 iks_delete(removequery);
2092 iks_delete(removeitem);
2095 ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy);
2099 * \brief filters the roster packet we get back from server.
2101 * \param pak ikspak iksemel packet.
2102 * \return IKS_FILTER_EAT.
2104 static int aji_filter_roster(void *data, ikspak *pak)
2106 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2109 struct aji_buddy *buddy;
2111 client->state = AJI_CONNECTED;
2112 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2113 ASTOBJ_RDLOCK(iterator);
2114 x = iks_child(pak->query);
2117 if (!iks_strcmp(iks_name(x), "item")) {
2118 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
2120 ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
2126 ast_copy_flags(iterator, client, AJI_AUTOREGISTER);
2129 ASTOBJ_UNLOCK(iterator);
2132 x = iks_child(pak->query);
2135 if (iks_strcmp(iks_name(x), "item") == 0) {
2136 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2137 ASTOBJ_RDLOCK(iterator);
2138 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
2140 ASTOBJ_UNLOCK(iterator);
2144 buddy = ast_calloc(1, sizeof(*buddy));
2146 ast_log(LOG_WARNING, "Out of memory\n");
2150 ASTOBJ_WRLOCK(buddy);
2151 ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
2152 ast_clear_flag(buddy, AST_FLAGS_ALL);
2153 if(ast_test_flag(client, AJI_AUTOPRUNE)) {
2154 ast_set_flag(buddy, AJI_AUTOPRUNE);
2155 buddy->objflags |= ASTOBJ_FLAG_MARKED;
2157 ast_set_flag(buddy, AJI_AUTOREGISTER);
2158 ASTOBJ_UNLOCK(buddy);
2160 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2161 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2169 aji_pruneregister(client);
2171 ASTOBJ_UNREF(client, aji_client_destroy);
2172 return IKS_FILTER_EAT;
2175 * \brief reconnect to jabber server
2176 * \param client the configured XMPP client we use to connect to a XMPP server
2179 static int aji_reconnect(struct aji_client *client)
2184 client->state = AJI_DISCONNECTED;
2187 iks_parser_reset(client->p);
2188 if (client->authorized)
2189 client->authorized = 0;
2191 res = aji_initialize(client);
2196 * \brief Get the roster of jabber users
2197 * \param client the configured XMPP client we use to connect to a XMPP server
2200 static int aji_get_roster(struct aji_client *client)
2203 roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
2205 iks_insert_attrib(roster, "id", "roster");
2206 aji_set_presence(client, NULL, client->jid->full, client->status, client->statusmessage);
2207 ast_aji_send(client, roster);
2215 * \brief connects as a client to jabber server.
2217 * \param pak ikspak iksemel packet
2220 static int aji_client_connect(void *data, ikspak *pak)
2222 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
2226 if (client->state == AJI_DISCONNECTED) {
2227 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);
2228 client->state = AJI_CONNECTING;
2229 client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
2230 iks_filter_remove_hook(client->f, aji_client_connect);
2231 if(!client->component) /*client*/
2232 aji_get_roster(client);
2235 ast_log(LOG_ERROR, "Out of memory.\n");
2237 ASTOBJ_UNREF(client, aji_client_destroy);
2242 * \brief prepares client for connect.
2243 * \param client the configured XMPP client we use to connect to a XMPP server
2246 static int aji_initialize(struct aji_client *client)
2248 int connected = IKS_NET_NOCONN;
2250 /* reset stream flags */
2251 client->stream_flags = 0;
2253 /* If it's a component, connect to user, otherwise, connect to server */
2254 connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->component ? client->user : client->jid->server);
2256 if (connected == IKS_NET_NOCONN) {
2257 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
2259 } else if (connected == IKS_NET_NODNS) {
2260 ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to %s\n", client->name, S_OR(client->serverhost, client->jid->server));
2268 * \brief disconnect from jabber server.
2269 * \param client the configured XMPP client we use to connect to a XMPP server
2272 int ast_aji_disconnect(struct aji_client *client)
2275 ast_verb(4, "JABBER: Disconnecting\n");
2277 if (client->stream_flags & SECURE) {
2278 SSL_shutdown(client->ssl_session);
2279 SSL_CTX_free(client->ssl_context);
2280 SSL_free(client->ssl_session);
2283 iks_disconnect(client->p);
2284 iks_parser_delete(client->p);
2285 ASTOBJ_UNREF(client, aji_client_destroy);
2292 * \brief set presence of client.
2293 * \param client the configured XMPP client we use to connect to a XMPP server
2294 * \param to user send it to
2295 * \param from user it came from
2300 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc)
2303 iks *presence = iks_make_pres(level, desc);
2304 iks *cnode = iks_new("c");
2305 iks *priority = iks_new("priority");
2308 if (presence && cnode && client && priority) {
2310 iks_insert_attrib(presence, "to", to);
2312 iks_insert_attrib(presence, "from", from);
2313 snprintf(priorityS, sizeof(priorityS), "%d", client->priority);
2314 iks_insert_cdata(priority, priorityS, strlen(priorityS));
2315 iks_insert_node(presence, priority);
2316 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
2317 iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
2318 iks_insert_attrib(cnode, "ext", "voice-v1");
2319 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
2320 iks_insert_node(presence, cnode);
2321 res = ast_aji_send(client, presence);
2323 ast_log(LOG_ERROR, "Out of memory.\n");
2327 iks_delete(presence);
2329 iks_delete(priority);
2333 * \brief Turn on/off console debugging.
2334 * \return CLI_SUCCESS.
2336 static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2340 e->command = "jabber set debug {on|off}";
2342 "Usage: jabber set debug {on|off}\n"
2343 " Enables/disables dumping of Jabber packets for debugging purposes.\n";
2349 if (a->argc != e->args)
2350 return CLI_SHOWUSAGE;
2352 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
2353 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2354 ASTOBJ_RDLOCK(iterator);
2355 iterator->debug = 1;
2356 ASTOBJ_UNLOCK(iterator);
2358 ast_cli(a->fd, "Jabber Debugging Enabled.\n");
2360 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
2361 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2362 ASTOBJ_RDLOCK(iterator);
2363 iterator->debug = 0;
2364 ASTOBJ_UNLOCK(iterator);
2366 ast_cli(a->fd, "Jabber Debugging Disabled.\n");
2369 return CLI_SHOWUSAGE; /* defaults to invalid */
2373 * \brief Turn on/off console debugging (deprecated, use aji_do_set_debug).
2374 * \return CLI_SUCCESS.
2376 static char *aji_do_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2381 e->command = "jabber debug [off]";
2383 "Usage: jabber debug [off]\n"
2384 " Enables/disables dumping of Jabber packets for debugging purposes.\n";
2391 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2392 ASTOBJ_RDLOCK(iterator);
2393 iterator->debug = 1;
2394 ASTOBJ_UNLOCK(iterator);
2396 ast_cli(a->fd, "Jabber Debugging Enabled.\n");
2398 } else if (a->argc == 3) {
2399 if (!strncasecmp(a->argv[2], "off", 3)) {
2400 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2401 ASTOBJ_RDLOCK(iterator);
2402 iterator->debug = 0;
2403 ASTOBJ_UNLOCK(iterator);
2405 ast_cli(a->fd, "Jabber Debugging Disabled.\n");
2409 return CLI_SHOWUSAGE; /* defaults to invalid */
2413 * \brief Reload jabber module.
2414 * \return CLI_SUCCESS.
2416 static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2420 e->command = "jabber reload";
2422 "Usage: jabber reload\n"
2423 " Reloads the Jabber module.\n";
2430 ast_cli(a->fd, "Jabber Reloaded.\n");
2435 * \brief Show client status.
2436 * \return CLI_SUCCESS.
2438 static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2445 e->command = "jabber show connected";
2447 "Usage: jabber show connected\n"
2448 " Shows state of clients and components\n";
2454 ast_cli(a->fd, "Jabber Users and their status:\n");
2455 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2456 ASTOBJ_RDLOCK(iterator);
2458 switch (iterator->state) {
2459 case AJI_DISCONNECTED:
2460 status = "Disconnected";
2462 case AJI_CONNECTING:
2463 status = "Connecting";
2466 status = "Connected";
2471 ast_cli(a->fd, " User: %s - %s\n", iterator->user, status);
2472 ASTOBJ_UNLOCK(iterator);
2474 ast_cli(a->fd, "----\n");
2475 ast_cli(a->fd, " Number of users: %d\n", count);
2480 * \brief Show buddy lists
2481 * \return CLI_SUCCESS.
2483 static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2485 struct aji_resource *resource;
2486 struct aji_client *client;
2490 e->command = "jabber show buddies";
2492 "Usage: jabber show buddies\n"
2493 " Shows buddy lists of our clients\n";
2499 ast_cli(a->fd, "Jabber buddy lists\n");
2500 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2501 ast_cli(a->fd,"Client: %s\n", iterator->user);
2503 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2504 ASTOBJ_RDLOCK(iterator);
2505 ast_cli(a->fd,"\tBuddy:\t%s\n", iterator->name);
2506 if (!iterator->resources)
2507 ast_cli(a->fd,"\t\tResource: None\n");
2508 for (resource = iterator->resources; resource; resource = resource->next) {
2509 ast_cli(a->fd,"\t\tResource: %s\n", resource->resource);
2511 ast_cli(a->fd,"\t\t\tnode: %s\n", resource->cap->parent->node);
2512 ast_cli(a->fd,"\t\t\tversion: %s\n", resource->cap->version);
2513 ast_cli(a->fd,"\t\t\tJingle capable: %s\n", resource->cap->jingle ? "yes" : "no");
2515 ast_cli(a->fd,"\t\tStatus: %d\n", resource->status);
2516 ast_cli(a->fd,"\t\tPriority: %d\n", resource->priority);
2518 ASTOBJ_UNLOCK(iterator);
2526 * \brief Send test message for debugging.
2527 * \return CLI_SUCCESS,CLI_FAILURE.
2529 static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2531 struct aji_client *client;
2532 struct aji_resource *resource;
2533 const char *name = "asterisk";
2534 struct aji_message *tmp;
2538 e->command = "jabber test";
2540 "Usage: jabber test [client]\n"
2541 " Sends test message for debugging purposes. A specific client\n"
2542 " as configured in jabber.conf can be optionally specified.\n";
2549 return CLI_SHOWUSAGE;
2550 else if (a->argc == 3)
2553 if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
2554 ast_cli(a->fd, "Unable to find client '%s'!\n", name);
2558 /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */
2559 ast_aji_send_chat(client, "mogorman@astjab.org", "blahblah");
2560 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
2561 ASTOBJ_RDLOCK(iterator);
2562 ast_verbose("User: %s\n", iterator->name);
2563 for (resource = iterator->resources; resource; resource = resource->next) {
2564 ast_verbose("Resource: %s\n", resource->resource);
2566 ast_verbose(" client: %s\n", resource->cap->parent->node);
2567 ast_verbose(" version: %s\n", resource->cap->version);
2568 ast_verbose(" Jingle Capable: %d\n", resource->cap->jingle);
2570 ast_verbose(" Priority: %d\n", resource->priority);
2571 ast_verbose(" Status: %d\n", resource->status);
2572 ast_verbose(" Message: %s\n", S_OR(resource->description,""));
2574 ASTOBJ_UNLOCK(iterator);
2576 ast_verbose("\nOooh a working message stack!\n");
2577 AST_LIST_LOCK(&client->messages);
2578 AST_LIST_TRAVERSE(&client->messages, tmp, list) {
2579 ast_verbose(" Message from: %s with id %s @ %s %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, ""));
2581 AST_LIST_UNLOCK(&client->messages);
2582 ASTOBJ_UNREF(client, aji_client_destroy);
2588 * \brief creates aji_client structure.
2590 * \param var ast_variable
2594 static int aji_create_client(char *label, struct ast_variable *var, int debug)
2597 struct aji_client *client = NULL;
2600 client = ASTOBJ_CONTAINER_FIND(&clients,label);
2603 client = ast_calloc(1, sizeof(*client));
2605 ast_log(LOG_ERROR, "Out of memory!\n");
2608 ASTOBJ_INIT(client);
2609 ASTOBJ_WRLOCK(client);
2610 ASTOBJ_CONTAINER_INIT(&client->buddies);
2612 ASTOBJ_WRLOCK(client);
2613 ASTOBJ_UNMARK(client);
2615 ASTOBJ_CONTAINER_MARKALL(&client->buddies);
2616 ast_copy_string(client->name, label, sizeof(client->name));
2617 ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
2619 /* Set default values for the client object */
2620 client->debug = debug;
2621 ast_copy_flags(client, &globalflags, AST_FLAGS_ALL);
2622 client->port = 5222;
2624 client->usesasl = 1;
2625 client->forcessl = 0;
2626 client->keepalive = 1;
2627 client->timeout = 50;
2628 client->message_timeout = 100;
2629 AST_LIST_HEAD_INIT(&client->messages);
2630 client->component = 0;
2631 ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage));
2632 client->priority = 0;
2633 client->status = IKS_SHOW_AVAILABLE;
2636 client->authorized = 0;
2637 client->state = AJI_DISCONNECTED;
2640 if (!strcasecmp(var->name, "username"))
2641 ast_copy_string(client->user, var->value, sizeof(client->user));
2642 else if (!strcasecmp(var->name, "serverhost"))
2643 ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
2644 else if (!strcasecmp(var->name, "secret"))
2645 ast_copy_string(client->password, var->value, sizeof(client->password));
2646 else if (!strcasecmp(var->name, "statusmessage"))
2647 ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage));
2648 else if (!strcasecmp(var->name, "port"))
2649 client->port = atoi(var->value);
2650 else if (!strcasecmp(var->name, "timeout"))
2651 client->message_timeout = atoi(var->value);
2652 else if (!strcasecmp(var->name, "debug"))
2653 client->debug = (ast_false(var->value)) ? 0 : 1;
2654 else if (!strcasecmp(var->name, "type")) {
2655 if (!strcasecmp(var->value, "component"))
2656 client->component = 1;
2657 } else if (!strcasecmp(var->name, "usetls")) {
2658 client->usetls = (ast_false(var->value)) ? 0 : 1;
2659 } else if (!strcasecmp(var->name, "usesasl")) {
2660 client->usesasl = (ast_false(var->value)) ? 0 : 1;
2661 } else if (!strcasecmp(var->name, "forceoldssl"))
2662 client->forcessl = (ast_false(var->value)) ? 0 : 1;
2663 else if (!strcasecmp(var->name, "keepalive"))
2664 client->keepalive = (ast_false(var->value)) ? 0 : 1;
2665 else if (!strcasecmp(var->name, "autoprune"))
2666 ast_set2_flag(client, ast_true(var->value), AJI_AUTOPRUNE);
2667 else if (!strcasecmp(var->name, "autoregister"))
2668 ast_set2_flag(client, ast_true(var->value), AJI_AUTOREGISTER);
2669 else if (!strcasecmp(var->name, "buddy"))
2670 aji_create_buddy((char *)var->value, client);
2671 else if (!strcasecmp(var->name, "priority"))
2672 client->priority = atoi(var->value);
2673 else if (!strcasecmp(var->name, "status")) {
2674 if (!strcasecmp(var->value, "unavailable"))
2675 client->status = IKS_SHOW_UNAVAILABLE;
2677 if (!strcasecmp(var->value, "available")
2678 || !strcasecmp(var->value, "online"))
2679 client->status = IKS_SHOW_AVAILABLE;
2681 if (!strcasecmp(var->value, "chat")
2682 || !strcasecmp(var->value, "chatty"))
2683 client->status = IKS_SHOW_CHAT;
2685 if (!strcasecmp(var->value, "away"))
2686 client->status = IKS_SHOW_AWAY;
2688 if (!strcasecmp(var->value, "xa")
2689 || !strcasecmp(var->value, "xaway"))
2690 client->status = IKS_SHOW_XA;
2692 if (!strcasecmp(var->value, "dnd"))
2693 client->status = IKS_SHOW_DND;
2695 if (!strcasecmp(var->value, "invisible"))
2696 #ifdef IKS_SHOW_INVISIBLE
2697 client->status = IKS_SHOW_INVISIBLE;
2700 ast_log(LOG_WARNING, "Your iksemel doesn't support invisible status: falling back to DND\n");
2701 client->status = IKS_SHOW_DND;
2705 ast_log(LOG_WARNING, "Unknown presence status: %s\n", var->value);
2707 /* no transport support in this version */
2708 /* else if (!strcasecmp(var->name, "transport"))
2709 aji_create_transport(var->value, client);
2714 ASTOBJ_UNLOCK(client);
2715 ASTOBJ_UNREF(client, aji_client_destroy);
2719 ast_copy_string(client->name_space, (client->component) ? "jabber:component:accept" : "jabber:client", sizeof(client->name_space));
2720 client->p = iks_stream_new(client->name_space, client, aji_act_hook);
2722 ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name);
2725 client->stack = iks_stack_new(8192, 8192);
2726 if (!client->stack) {
2727 ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name);
2730 client->f = iks_filter_new();
2732 ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name);
2735 if (!strchr(client->user, '/') && !client->component) { /*client */
2737 asprintf(&resource, "%s/asterisk", client->user);
2739 client->jid = iks_id_new(client->stack, resource);
2743 client->jid = iks_id_new(client->stack, client->user);
2744 if (client->component) {
2745 iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2746 iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
2747 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);
2748 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);
2750 iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2752 if (!strchr(client->user, '/') && !client->component) { /*client */
2754 asprintf(&resource, "%s/asterisk", client->user);
2756 client->jid = iks_id_new(client->stack, resource);
2760 client->jid = iks_id_new(client->stack, client->user);
2761 iks_set_log_hook(client->p, aji_log_hook);
2762 ASTOBJ_UNLOCK(client);
2763 ASTOBJ_CONTAINER_LINK(&clients,client);
2769 * \brief creates transport.
2770 * \param label, buddy to dump it into.
2773 /* no connecting to transports today */
2774 static int aji_create_transport(char *label, struct aji_client *client)
2776 char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
2777 struct aji_buddy *buddy = NULL;
2779 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2781 buddy = ast_calloc(1, sizeof(*buddy));
2783 ast_log(LOG_WARNING, "Out of memory\n");
2788 ASTOBJ_WRLOCK(buddy);
2790 if ((buddyname = strchr(label, ','))) {
2793 if (buddyname && buddyname[0] != '\0') {
2794 if ((user = strchr(buddyname, ','))) {
2797 if (user && user[0] != '\0') {
2798 if ((pass = strchr(user, ','))) {
2801 ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
2802 ast_copy_string(buddy->user, user, sizeof(buddy->user));
2803 ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
2804 ast_copy_string(buddy->server, server, sizeof(buddy->server));
2811 ASTOBJ_UNLOCK(buddy);
2812 ASTOBJ_UNMARK(buddy);
2813 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2819 * \brief creates buddy.
2820 * \param label char.
2821 * \param client the configured XMPP client we use to connect to a XMPP server
2822 * \return 1 on success, 0 on failure.
2824 static int aji_create_buddy(char *label, struct aji_client *client)
2826 struct aji_buddy *buddy = NULL;
2828 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2831 buddy = ast_calloc(1, sizeof(*buddy));
2833 ast_log(LOG_WARNING, "Out of memory\n");
2838 ASTOBJ_WRLOCK(buddy);
2839 ast_copy_string(buddy->name, label, sizeof(buddy->name));
2840 ASTOBJ_UNLOCK(buddy);
2842 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2844 ASTOBJ_UNMARK(buddy);
2845 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2850 /*!< load config file. \return 1. */
2851 static int aji_load_config(int reload)
2855 struct ast_config *cfg = NULL;
2856 struct ast_variable *var = NULL;
2857 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2859 if ((cfg = ast_config_load(JABBER_CONFIG, config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
2862 /* Reset flags to default value */
2863 ast_set_flag(&globalflags, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
2866 ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
2870 cat = ast_category_browse(cfg, NULL);
2871 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
2872 if (!strcasecmp(var->name, "debug"))
2873 debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
2874 else if (!strcasecmp(var->name, "autoprune"))
2875 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
2876 else if (!strcasecmp(var->name, "autoregister"))
2877 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
2881 if (strcasecmp(cat, "general")) {
2882 var = ast_variable_browse(cfg, cat);
2883 aji_create_client(cat, var, debug);
2885 cat = ast_category_browse(cfg, cat);
2887 ast_config_destroy(cfg); /* or leak memory */
2892 * \brief grab a aji_client structure by label name.
2893 * \param name label name
2894 * \return aji_client.
2896 struct aji_client *ast_aji_get_client(const char *name)
2898 struct aji_client *client = NULL;
2900 client = ASTOBJ_CONTAINER_FIND(&clients, name);
2901 if (!client && !strchr(name, '@'))
2902 client = ASTOBJ_CONTAINER_FIND_FULL(&clients, name, user,,, strcasecmp);
2906 struct aji_client_container *ast_aji_get_clients(void)
2911 static char mandescr_jabber_send[] =
2912 "Description: Sends a message to a Jabber Client.\n"
2914 " Jabber: Client or transport Asterisk uses to connect to JABBER.\n"
2915 " ScreenName: User Name to message.\n"
2916 " Message: Message to be sent to the buddy\n";
2919 * \brief Send a Jabber Message via call from the Manager
2920 * \param s mansession Manager session
2921 * \param m message Message to send
2924 static int manager_jabber_send(struct mansession *s, const struct message *m)
2926 struct aji_client *client = NULL;
2927 const char *id = astman_get_header(m,"ActionID");
2928 const char *jabber = astman_get_header(m,"Jabber");
2929 const char *screenname = astman_get_header(m,"ScreenName");
2930 const char *message = astman_get_header(m,"Message");
2932 if (ast_strlen_zero(jabber)) {
2933 astman_send_error(s, m, "No transport specified");
2936 if (ast_strlen_zero(screenname)) {
2937 astman_send_error(s, m, "No ScreenName specified");
2940 if (ast_strlen_zero(message)) {
2941 astman_send_error(s, m, "No Message specified");
2945 astman_send_ack(s, m, "Attempting to send Jabber Message");
2946 client = ast_aji_get_client(jabber);
2948 astman_send_error(s, m, "Could not find Sender");
2951 if (strchr(screenname, '@') && message){
2952 ast_aji_send_chat(client, screenname, message);
2953 astman_append(s, "Response: Success\r\n");
2954 if (!ast_strlen_zero(id))
2955 astman_append(s, "ActionID: %s\r\n",id);
2958 astman_append(s, "Response: Error\r\n");
2959 if (!ast_strlen_zero(id))
2960 astman_append(s, "ActionID: %s\r\n",id);
2964 /*! \brief Reload the jabber module */
2965 static int aji_reload(int reload)
2969 ASTOBJ_CONTAINER_MARKALL(&clients);
2970 if (!(res = aji_load_config(reload))) {
2971 ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
2973 } else if (res == -1)
2976 ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, aji_client_destroy);
2977 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2978 ASTOBJ_RDLOCK(iterator);
2979 if(iterator->state == AJI_DISCONNECTED) {
2980 if (!iterator->thread)
2981 ast_pthread_create_background(&iterator->thread, NULL, aji_recv_loop, iterator);
2982 } else if (iterator->state == AJI_CONNECTING)
2983 aji_get_roster(iterator);
2984 ASTOBJ_UNLOCK(iterator);
2990 /*! \brief Unload the jabber module */
2991 static int unload_module(void)
2994 ast_cli_unregister_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry));
2995 ast_unregister_application(app_ajisend);
2996 ast_unregister_application(app_ajistatus);
2997 ast_manager_unregister("JabberSend");
2998 ast_custom_function_unregister(&jabberstatus_function);
3000 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
3001 ASTOBJ_RDLOCK(iterator);
3002 ast_debug(3, "JABBER: Releasing and disconnecting client: %s\n", iterator->name);
3003 iterator->state = AJI_DISCONNECTING;
3004 ast_aji_disconnect(iterator);
3005 pthread_join(iterator->thread, NULL);
3006 ASTOBJ_UNLOCK(iterator);
3009 ASTOBJ_CONTAINER_DESTROYALL(&clients, aji_client_destroy);
3010 ASTOBJ_CONTAINER_DESTROY(&clients);
3014 /*! \brief Unload the jabber module */
3015 static int load_module(void)
3017 ASTOBJ_CONTAINER_INIT(&clients);
3019 return AST_MODULE_LOAD_DECLINE;
3020 ast_manager_register2("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send,
3021 "Sends a message to a Jabber Client", mandescr_jabber_send);
3022 ast_register_application(app_ajisend, aji_send_exec, ajisend_synopsis, ajisend_descrip);
3023 ast_register_application(app_ajistatus, aji_status_exec, ajistatus_synopsis, ajistatus_descrip);
3024 ast_cli_register_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry));
3025 ast_custom_function_register(&jabberstatus_function);
3030 /*! \brief Wrapper for aji_reload */
3031 static int reload(void)
3037 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "AJI - Asterisk Jabber Interface",
3038 .load = load_module,
3039 .unload = unload_module,