Merged revisions 43466 via svnmerge from
[asterisk/asterisk.git] / res / res_jabber.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Matt O'Gorman <mogorman@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  * \brief A resource for interfacing asterisk directly as a client
21  * or a component to a jabber compliant server.
22  */
23
24 /*** MODULEINFO
25         <depend>iksemel</depend>
26  ***/
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <iksemel.h>
35
36 #include "asterisk/channel.h"
37 #include "asterisk/jabber.h"
38 #include "asterisk/file.h"
39 #include "asterisk/config.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/options.h"
44 #include "asterisk/cli.h"
45 #include "asterisk/app.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/md5.h"
48 #include "asterisk/acl.h"
49 #include "asterisk/utils.h"
50 #include "asterisk/module.h"
51 #include "asterisk/astobj.h"
52 #include "asterisk/astdb.h"
53 #include "asterisk/manager.h"
54
55 #define JABBER_CONFIG "jabber.conf"
56
57 /*-- Forward declarations */
58 static int manager_jabber_send( struct mansession *s, struct message *m );
59 static int aji_highest_bit(int number);
60 static void aji_buddy_destroy(struct aji_buddy *obj);
61 static void aji_client_destroy(struct aji_client *obj);
62 static int aji_send_exec(struct ast_channel *chan, void *data);
63 static int aji_status_exec(struct ast_channel *chan, void *data);
64 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming);
65 static int aji_act_hook(void *data, int type, iks *node);
66 static void aji_handle_iq(struct aji_client *client, iks *node);
67 static void aji_handle_message(struct aji_client *client, ikspak *pak);
68 static void aji_handle_presence(struct aji_client *client, ikspak *pak);
69 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak);
70 static void *aji_recv_loop(void *data);
71 static int aji_component_initialize(struct aji_client *client);
72 static int aji_client_initialize(struct aji_client *client);
73 static int aji_client_connect(void *data, ikspak *pak);
74 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc);
75 static int aji_do_debug(int fd, int argc, char *argv[]);
76 static int aji_do_reload(int fd, int argc, char *argv[]);
77 static int aji_no_debug(int fd, int argc, char *argv[]);
78 static int aji_test(int fd, int argc, char *argv[]);
79 static int aji_show_clients(int fd, int argc, char *argv[]);
80 static int aji_create_client(char *label, struct ast_variable *var, int debug);
81 static int aji_create_buddy(char *label, struct aji_client *client);
82 static int aji_create_transport(char *label, struct aji_client *client);
83 static int aji_reload(void);
84 static int aji_load_config(void);
85 static void aji_pruneregister(struct aji_client *client);
86 static int aji_register_transport(void *data, ikspak *pak);
87 static int aji_register_transport2(void *data, ikspak *pak);
88 static int aji_filter_roster(void *data, ikspak *pak);
89 static int aji_get_roster(struct aji_client *client);
90 static int aji_client_info_handler(void *data, ikspak *pak);
91 static int aji_dinfo_handler(void *data, ikspak *pak);
92 static int aji_ditems_handler(void *data, ikspak *pak);
93 static int aji_register_query_handler(void *data, ikspak *pak);
94 static int aji_register_approve_handler(void *data, ikspak *pak);
95 static int aji_reconnect(struct aji_client *client);
96 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
97
98 static char debug_usage[] = 
99 "Usage: jabber debug\n" 
100 "       Enables dumping of Jabber packets for debugging purposes.\n";
101
102 static char no_debug_usage[] = 
103 "Usage: jabber no debug\n" 
104 "       Disables dumping of Jabber packets for debugging purposes.\n";
105
106 static char reload_usage[] = 
107 "Usage: jabber reload\n" 
108 "       Enables reloading of Jabber module.\n";
109
110 static char test_usage[] = 
111 "Usage: jabber test [client]\n" 
112 "       Sends test message for debugging purposes.  A specific client\n"
113 "       as configured in jabber.conf can be optionally specified.\n";
114
115 static struct ast_cli_entry aji_cli[] = {
116         { { "jabber", "debug", NULL},
117         aji_do_debug, "Enable Jabber debugging",
118         debug_usage },
119
120         { { "jabber", "reload", NULL},
121         aji_do_reload, "Enable Jabber debugging",
122         reload_usage },
123
124         { { "jabber", "show", "connected", NULL},
125         aji_show_clients, "Show state of clients and components",
126         debug_usage },
127
128         { { "jabber", "no", "debug", NULL},
129         aji_no_debug, "Disable Jabber debug",
130         no_debug_usage },
131
132         { { "jabber", "test", NULL},
133         aji_test, "Shows roster, but is generally used for mog's debugging.",
134         test_usage },
135 };
136
137 static char *app_ajisend = "JabberSend";
138
139 static char *ajisend_synopsis = "JabberSend(jabber,screenname,message)";
140
141 static char *ajisend_descrip =
142 "JabberSend(Jabber,ScreenName,Message)\n"
143 "  Jabber - Client or transport Asterisk uses to connect to Jabber\n" 
144 "  ScreenName - User Name to message.\n" 
145 "  Message - Message to be sent to the buddy\n";
146
147 static char *app_ajistatus = "JabberStatus";
148
149 static char *ajistatus_synopsis = "JabberStatus(Jabber,ScreenName,Variable)";
150
151 static char *ajistatus_descrip =
152 "JabberStatus(Jabber,ScreenName,Variable)\n"
153 "  Jabber - Client or transport Asterisk uses to connect to Jabber\n"
154 "  ScreenName - User Name to retrieve status from.\n"
155 "  Variable - Variable to store presence in will be 1-6.\n" 
156 "             In order, Online, Chatty, Away, XAway, DND, Offline\n" 
157 "             If not in roster variable will = 7\n";
158
159 struct aji_client_container clients;
160
161 struct aji_capabilities *capabilities;
162
163 /*! \brief Global flags, initialized to default values */
164 static struct ast_flags globalflags = { AJI_AUTOPRUNE | AJI_AUTOREGISTER };
165
166 /*!
167  * \brief Deletes the aji_client data structure.
168  * \param obj is the structure we will delete.
169  * \return void.
170  */
171 static void aji_client_destroy(struct aji_client *obj)
172 {
173         struct aji_message *tmp;
174         ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, aji_buddy_destroy);
175         ASTOBJ_CONTAINER_DESTROY(&obj->buddies);
176         iks_filter_delete(obj->f);
177         iks_parser_delete(obj->p);
178         iks_stack_delete(obj->stack);
179         AST_LIST_LOCK(&obj->messages);
180         while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) {
181                 if (tmp->from)
182                         free(tmp->from);
183                 if (tmp->message)
184                         free(tmp->message);
185         }
186         AST_LIST_HEAD_DESTROY(&obj->messages);
187         free(obj);
188 }
189
190 /*!
191  * \brief Deletes the aji_buddy data structure.
192  * \param obj is the structure we will delete.
193  * \return void.
194  */
195 static void aji_buddy_destroy(struct aji_buddy *obj)
196 {
197         struct aji_resource *tmp;
198
199         while ((tmp = obj->resources)) {
200                 obj->resources = obj->resources->next;
201                 free(tmp->description);
202                 free(tmp);
203         }
204
205         free(obj);
206 }
207
208 static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
209 {
210         struct aji_capabilities *list = NULL;
211         struct aji_version *res = NULL;
212
213         list = capabilities;
214
215         if(!node)
216                 node = pak->from->full;
217         if(!version)
218                 version = "none supplied.";
219         while(list) {
220                 if(!strcasecmp(list->node, node)) {
221                         res = list->versions;
222                         while(res) {
223                                  if(!strcasecmp(res->version, version))
224                                          return res;
225                                  res = res->next;
226                         }
227                         if(!res) {
228                                 res = (struct aji_version *)malloc(sizeof(struct aji_version));
229                                 if(!res) {
230                                         ast_log(LOG_ERROR, "Out of memory!\n");
231                                         return NULL;
232                                 }
233                                 res->jingle = 0;
234                                 res->parent = list;
235                                 ast_copy_string(res->version, version, sizeof(res->version));
236                                 res->next = list->versions;
237                                 list->versions = res;
238                                 return res;
239                         }
240                 }
241                 list = list->next;
242         }
243         if(!list) {
244                 list = (struct aji_capabilities *)malloc(sizeof(struct aji_capabilities));
245                 if(!list) {
246                         ast_log(LOG_ERROR, "Out of memory!\n");
247                         return NULL;
248                 }
249                 res = (struct aji_version *)malloc(sizeof(struct aji_version));
250                 if(!res) {
251                         ast_log(LOG_ERROR, "Out of memory!\n");
252                         return NULL;
253                 }
254                 ast_copy_string(list->node, node, sizeof(list->node));
255                 ast_copy_string(res->version, version, sizeof(res->version));
256                 res->jingle = 0;
257                 res->parent = list;
258                 res->next = list->versions;
259                 list->versions = res;
260                 list->next = capabilities;
261                 capabilities = list;
262         }
263         return res;
264 }
265
266 static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *rname)
267 {
268         struct aji_resource *res = NULL;
269         if (!buddy || !rname)
270                 return res;
271         res = buddy->resources;
272         while (res) {
273                 if (!strcasecmp(res->resource, rname)) {
274                         break;
275                 }
276                 res = res->next;
277         }
278         return res;
279 }
280
281 static int gtalk_yuck(iks *node)
282 {
283         if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps"))
284                 return 1;
285         return 0;
286 }
287
288 /*!
289  * \brief Detects the highest bit in a number.
290  * \param Number you want to have evaluated.
291  * \return the highest power of 2 that can go into the number.
292  */
293 static int aji_highest_bit(int number)
294 {
295         int x = sizeof(number) * 8 - 1;
296         if (!number)
297                 return 0;
298         for (; x > 0; x--) {
299                 if (number & (1 << x))
300                         break;
301         }
302         return (1 << x);
303 }
304
305 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid)
306 {
307         iks *x, *y;
308         x = iks_new("iq");
309         iks_insert_attrib(x, "type", "set");
310         y = iks_insert(x, "query");
311         iks_insert_attrib(y, "xmlns", IKS_NS_AUTH);
312         iks_insert_cdata(iks_insert(y, "username"), id->user, 0);
313         iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0);
314         if (sid) {
315                 char buf[41];
316                 char sidpass[100];
317                 snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass);
318                 ast_sha1_hash(buf, sidpass);
319                 iks_insert_cdata(iks_insert(y, "digest"), buf, 0);
320         } else {
321                 iks_insert_cdata(iks_insert(y, "password"), pass, 0);
322         }
323         return x;
324 }
325
326 /*!
327  * \brief Dial plan function status(). puts the status of watched user 
328    into a channel variable.
329  * \param channel, and username,watched user, status var
330  * \return 0.
331  */
332 static int aji_status_exec(struct ast_channel *chan, void *data)
333 {
334         struct aji_client *client = NULL;
335         struct aji_buddy *buddy = NULL;
336         struct aji_resource *r = NULL;
337         char *s = NULL, *sender = NULL, *jid = NULL, *screenname = NULL, *resource = NULL, *variable = NULL;
338         int stat = 7;
339         char status[2];
340         if (data) {
341                 s = ast_strdupa((char *) data);
342                 if (s) {
343                         sender = strsep(&s, "|");
344                         if (sender && (sender[0] != '\0')) {
345                                 jid = strsep(&s, "|");
346                                 if (jid && (jid[0] != '\0')) {
347                                         variable = s;
348                                 } else {
349                                         ast_log(LOG_ERROR, "Bad arguments\n");
350                                         return -1;
351                                 }
352                         }
353                 }
354         } else {
355                 ast_log(LOG_ERROR, "Out of memory\n");
356                 return -1;
357         }
358
359         if(!strchr(jid, '/')) {
360                 resource = NULL;
361         } else {
362                 screenname = strsep(&jid, "/");
363                 resource = jid;
364         }
365         client = ast_aji_get_client(sender);
366         if (!client) {
367                 ast_log(LOG_WARNING, "Could not find Connection.\n");
368                 return -1;
369         }
370         if(!&client->buddies) {
371                 ast_log(LOG_WARNING, "No buddies for connection.\n");
372                 return -1;
373         }
374         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, (resource)?screenname:jid);
375         if (!buddy) {
376                 ast_log(LOG_WARNING, "Could not find Buddy in list.\n");
377                 return -1;
378         }
379         r = aji_find_resource(buddy, resource);
380         if(!r && buddy->resources) {
381                 r = buddy->resources;
382         }
383         if(!r){
384                 ast_log(LOG_NOTICE, "Resource %s of buddy %s not found \n", resource, screenname);
385         }
386         stat = r->status;
387         sprintf(status, "%d", stat);
388         pbx_builtin_setvar_helper(chan, variable, status);
389         return 0;
390 }
391
392 /*!
393  * \brief Dial plan function to send a message.
394  * \param channel, and data, data is sender, reciever, message.
395  * \return 0.
396  */
397 static int aji_send_exec(struct ast_channel *chan, void *data)
398 {
399         struct aji_client *client = NULL;
400
401         char *s = NULL, *sender = NULL, *recipient = NULL, *message = NULL;
402
403         if (!data) {
404                 ast_log(LOG_ERROR, "Out of memory\n");
405                 return -1;
406         }
407         s = ast_strdupa((char *) data);
408         if (s) {
409                 sender = strsep(&s, "|");
410                 if (sender && (sender[0] != '\0')) {
411                         recipient = strsep(&s, "|");
412                         if (recipient && (recipient[0] != '\0')) {
413                                 message = s;
414                         } else {
415                                 ast_log(LOG_ERROR, "Bad arguments \n");
416                                 return -1;
417                         }
418                 }
419         }
420         if (!(client = ast_aji_get_client(sender))) {
421                 ast_log(LOG_WARNING, "Could not find Sender.\n");
422                 return -1;
423         }
424         if (strchr(recipient, '@') && message)
425                 ast_aji_send(client, recipient, message);
426         return 0;
427 }
428
429 /*!
430  * \brief the debug loop.
431  * \param aji_client structure, xml data as string, size of string, direction of packet, 1 for inbound 0 for outbound.
432  */
433 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming)
434 {
435         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
436         manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s", client->name, xmpp);
437
438         if (client->debug) {
439                 if (is_incoming)
440                         ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
441                 else {
442                         if( strlen(xmpp) == 1) {
443                                 if(option_debug > 2  && xmpp[0] == ' ')
444                                 ast_verbose("\nJABBER: Keep alive packet\n");
445                         } else
446                                 ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp);
447                 }
448
449         }
450         ASTOBJ_UNREF(client, aji_client_destroy);
451 }
452
453 /*!
454  * \brief The action hook parses the inbound packets, constantly running.
455  * \param aji client structure, type of packet, the actual packet.
456  * \return IKS_OK or IKS_HOOK .
457  */
458 static int aji_act_hook(void *data, int type, iks *node)
459 {
460         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
461         ikspak *pak = NULL;
462         iks *auth = NULL;
463
464         if(!node) {
465                 ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */
466                 ASTOBJ_UNREF(client, aji_client_destroy);
467                 return IKS_HOOK;
468         }
469
470         pak = iks_packet(node);
471
472         if (!client->component) { /*client */
473                 switch (type) {
474                 case IKS_NODE_START:
475                         if (client->usetls && !iks_is_secure(client->p)) {
476                                 if (iks_has_tls())
477                                         iks_start_tls(client->p);
478                                 else
479                                         ast_log(LOG_ERROR, "gnuTLS not installed.\n");
480                                 break;
481                         }
482                         if (!client->usesasl) {
483                                 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);
484                                 auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id"));
485                                 if (auth) {
486                                         iks_insert_attrib(auth, "id", client->mid);
487                                         iks_insert_attrib(auth, "to", client->jid->server);
488                                         ast_aji_increment_mid(client->mid);
489                                         iks_send(client->p, auth);
490                                         iks_delete(auth);
491                                 } else
492                                         ast_log(LOG_ERROR, "Out of memory.\n");
493                         }
494                         break;
495
496                 case IKS_NODE_NORMAL:
497                         {
498                                 int features = 0;
499                                 if (!strcmp("stream:features", iks_name(node))) {
500                                         features = iks_stream_features(node);
501                                         if (client->usesasl) {
502                                                 if (client->usetls && !iks_is_secure(client->p))
503                                                         break;
504                                                 if (client->authorized) {
505                                                         if (features & IKS_STREAM_BIND) {
506                                                                 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);
507                                                                 auth = iks_make_resource_bind(client->jid);
508                                                                 if (auth) {
509                                                                         iks_insert_attrib(auth, "id", client->mid);
510                                                                         ast_aji_increment_mid(client->mid);
511                                                                         iks_send(client->p, auth);
512                                                                         iks_delete(auth);
513                                                                 } else {
514                                                                         ast_log(LOG_ERROR, "Out of memory.\n");
515                                                                         break;
516                                                                 }
517                                                         }
518                                                         if (features & IKS_STREAM_SESSION) {
519                                                                 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);
520                                                                 auth = iks_make_session();
521                                                                 if (auth) {
522                                                                         iks_insert_attrib(auth, "id", "auth");
523                                                                         ast_aji_increment_mid(client->mid);
524                                                                         iks_send(client->p, auth);
525                                                                         iks_delete(auth);
526                                                                 } else {
527                                                                         ast_log(LOG_ERROR, "Out of memory.\n");
528                                                                 }
529                                                         }
530                                                 } else {
531                                                         features = aji_highest_bit(features);
532                                                         if (features == IKS_STREAM_SASL_MD5)
533                                                                 iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, client->jid->user, client->password);
534                                                         else {
535                                                                 if (features == IKS_STREAM_SASL_PLAIN) {
536                                                                         iks *x = NULL;
537                                                                         x = iks_new("auth");
538                                                                         if (x) {
539                                                                                 iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL);
540                                                                                 int len = strlen(client->jid->user) + strlen(client->password) + 3;
541                                                                                 /* XXX Check return values XXX */
542                                                                                 char *s = ast_malloc(80 + len);
543                                                                                 char *base64 = ast_malloc(80 + len * 2);
544                                                                                 iks_insert_attrib(x, "mechanism", "PLAIN");
545                                                                                 sprintf(s, "%c%s%c%s", 0, client->jid->user, 0, client->password);
546                                                                                 ast_base64encode(base64, (const unsigned char *) s, len, len * 2);
547                                                                                 iks_insert_cdata(x, base64, 0);
548                                                                                 iks_send(client->p, x);
549                                                                                 iks_delete(x);
550                                                                                 if (base64)
551                                                                                         free(base64);
552                                                                                 if (s)
553                                                                                         free(s);
554                                                                         } else {
555                                                                                 ast_log(LOG_ERROR, "Out of memory.\n");
556                                                                         }
557                                                                 }
558                                                         }
559                                                 }
560                                         }
561                                 } else if (!strcmp("failure", iks_name(node))) {
562                                         ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n");
563                                 } else if (!strcmp("success", iks_name(node))) {
564                                         client->authorized = 1;
565                                         iks_send_header(client->p, client->jid->server);
566                                 }
567                                 break;
568                         }
569                 case IKS_NODE_ERROR: 
570                                 ast_log(LOG_ERROR, "JABBER: Node Error\n");
571                                 ASTOBJ_UNREF(client, aji_client_destroy);
572                                 return IKS_HOOK;
573                                 break;
574                 case IKS_NODE_STOP: 
575                                 ast_log(LOG_WARNING, "JABBER: Disconnected\n");
576                                 ASTOBJ_UNREF(client, aji_client_destroy);
577                                 return IKS_HOOK;
578                                 break;
579                 }
580         } else if (client->state != AJI_CONNECTED && client->component) {
581                 switch (type) {
582                 case IKS_NODE_START:
583                         if (client->state == AJI_DISCONNECTED) {
584                                 char secret[160], shasum[320], *handshake;
585
586                                 sprintf(secret, "%s%s", pak->id, client->password);
587                                 ast_sha1_hash(shasum, secret);
588                                 handshake = NULL;
589                                 asprintf(&handshake, "<handshake>%s</handshake>", shasum);
590                                 if (handshake) {
591                                         iks_send_raw(client->p, handshake);
592                                         free(handshake);
593                                         handshake = NULL;
594                                 }
595                                 client->state = AJI_CONNECTING;
596                                 if(iks_recv(client->p,1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
597                                         client->state = AJI_CONNECTED;
598                                 else
599                                         ast_log(LOG_WARNING,"Jabber didn't seem to handshake, failed to authenicate.\n");
600                                 break;
601                         }
602                         break;
603
604                 case IKS_NODE_NORMAL:
605                         break;
606
607                 case IKS_NODE_ERROR:
608                         ast_log(LOG_ERROR, "JABBER: Node Error\n");
609                         ASTOBJ_UNREF(client, aji_client_destroy);
610                         return IKS_HOOK;
611
612                 case IKS_NODE_STOP:
613                         ast_log(LOG_WARNING, "JABBER: Disconnected\n");
614                         ASTOBJ_UNREF(client, aji_client_destroy);
615                         return IKS_HOOK;
616                 }
617         }
618
619         switch (pak->type) {
620         case IKS_PAK_NONE:
621                 if (option_verbose > 4)
622                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Don't know what to do with you NONE\n");
623                 break;
624         case IKS_PAK_MESSAGE:
625                 aji_handle_message(client, pak);
626                 if (option_verbose > 4)
627                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Don't know what to do with you MESSAGE\n");
628                 break;
629         case IKS_PAK_PRESENCE:
630                 aji_handle_presence(client, pak);
631                 if (option_verbose > 4)
632                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Do know how to handle presence!!\n");
633                 break;
634         case IKS_PAK_S10N:
635                 aji_handle_subscribe(client, pak);
636                 if (option_verbose > 4)
637                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont know S10N subscribe!!\n");
638                 break;
639         case IKS_PAK_IQ:
640                 if (option_verbose > 4)
641                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont have an IQ!!!\n");
642                 aji_handle_iq(client, node);
643                 break;
644         default:
645                 if (option_verbose > 4)
646                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont know %i\n", pak->type);
647                 break;
648         }
649         
650         iks_filter_packet(client->f, pak);
651
652         if (node)
653                 iks_delete(node);
654
655         ASTOBJ_UNREF(client, aji_client_destroy);
656         return IKS_OK;
657 }
658
659 static int aji_register_approve_handler(void *data, ikspak *pak)
660 {
661         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
662         iks *iq = NULL, *presence = NULL, *query = NULL,  *x = NULL;
663
664         iq = iks_new("iq");
665         presence = iks_new("presence");
666         query = iks_new("query");
667         x = iks_new("x");
668         if (client && iq && presence && query && x) {
669                 if (!iks_find(pak->query, "remove")) {
670                         iks_insert_attrib(iq, "from", client->jid->full);
671                         iks_insert_attrib(iq, "to", pak->from->full);
672                         iks_insert_attrib(iq, "id", pak->id);
673                         iks_insert_attrib(iq, "type", "result");
674                         iks_insert_attrib(query, "xmlns", "jabber:iq:register");
675                         iks_insert_node(iq, query);
676                         iks_send(client->p, iq);
677
678                         iks_insert_attrib(presence, "from", client->jid->full);
679                         iks_insert_attrib(presence, "to", pak->from->partial);
680                         iks_insert_attrib(presence, "id", client->mid);
681                         ast_aji_increment_mid(client->mid);
682                         iks_insert_attrib(presence, "type", "subscribe");
683                         iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
684                         iks_insert_node(presence, x);
685                         iks_send(client->p, presence);
686                 }
687         } else {
688                 ast_log(LOG_ERROR, "Out of memory.\n");
689         }
690
691         if (iq)
692                 iks_delete(iq);
693         if(presence)
694                 iks_delete(presence);
695         if (query)
696                 iks_delete(query);
697         if (x)
698                 iks_delete(x);
699         ASTOBJ_UNREF(client, aji_client_destroy);
700         return IKS_FILTER_EAT;
701 }
702
703 static int aji_register_query_handler(void *data, ikspak *pak)
704 {
705         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
706         char *node = NULL;
707
708         client = (struct aji_client *) data;
709
710         if (!(node = iks_find_attrib(pak->query, "node"))) {
711                 iks *iq = NULL, *query = NULL, *instructions = NULL;
712                 char *explain = "Welcome to Asterisk - the Open Source PBX.\n";
713                 iq = iks_new("iq");
714                 query = iks_new("query");
715                 instructions = iks_new("instructions");
716                 if (iq && query && instructions && client) {
717                         iks_insert_attrib(iq, "from", client->user);
718                         iks_insert_attrib(iq, "to", pak->from->full);
719                         iks_insert_attrib(iq, "id", pak->id);
720                         iks_insert_attrib(iq, "type", "result");
721                         iks_insert_attrib(query, "xmlns", "jabber:iq:register");
722                         iks_insert_cdata(instructions, explain, 0);
723                         iks_insert_node(iq, query);
724                         iks_insert_node(query, instructions);
725                         iks_send(client->p, iq);
726                 } else {
727                         ast_log(LOG_ERROR, "Out of memory.\n");
728                 }
729                 if (iq)
730                         iks_delete(iq);
731                 if (query)
732                         iks_delete(query);
733                 if (instructions)
734                         iks_delete(instructions);
735         }
736         ASTOBJ_UNREF(client, aji_client_destroy);
737         return IKS_FILTER_EAT;
738 }
739
740 static int aji_ditems_handler(void *data, ikspak *pak)
741 {
742         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
743         char *node = NULL;
744
745         if (!(node = iks_find_attrib(pak->query, "node"))) {
746                 iks *iq = NULL, *query = NULL, *item = NULL;
747                 iq = iks_new("iq");
748                 query = iks_new("query");
749                 item = iks_new("item");
750
751                 if (iq && query && item) {
752                         iks_insert_attrib(iq, "from", client->user);
753                         iks_insert_attrib(iq, "to", pak->from->full);
754                         iks_insert_attrib(iq, "id", pak->id);
755                         iks_insert_attrib(iq, "type", "result");
756                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
757                         iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
758                         iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
759                         iks_insert_attrib(item, "jid", client->user);
760
761                         iks_insert_node(iq, query);
762                         iks_insert_node(query, item);
763                         iks_send(client->p, iq);
764                 } else {
765                         ast_log(LOG_ERROR, "Out of memory.\n");
766                 }
767                 if (iq)
768                         iks_delete(iq);
769                 if (query)
770                         iks_delete(query);
771                 if (item)
772                         iks_delete(item);
773
774         } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
775                 iks *iq, *query, *confirm;
776                 iq = iks_new("iq");
777                 query = iks_new("query");
778                 confirm = iks_new("item");
779                 if (iq && query && confirm && client) {
780                         iks_insert_attrib(iq, "from", client->user);
781                         iks_insert_attrib(iq, "to", pak->from->full);
782                         iks_insert_attrib(iq, "id", pak->id);
783                         iks_insert_attrib(iq, "type", "result");
784                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
785                         iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
786                         iks_insert_attrib(confirm, "node", "confirmaccount");
787                         iks_insert_attrib(confirm, "name", "Confirm AIM account");
788                         iks_insert_attrib(confirm, "jid", "blog.astjab.org");
789
790                         iks_insert_node(iq, query);
791                         iks_insert_node(query, confirm);
792                         iks_send(client->p, iq);
793                 } else {
794                         ast_log(LOG_ERROR, "Out of memory.\n");
795                 }
796                 if (iq)
797                         iks_delete(iq);
798                 if (query)
799                         iks_delete(query);
800                 if (confirm)
801                         iks_delete(confirm);
802
803         } else if (!strcasecmp(node, "confirmaccount")) {
804                 iks *iq = NULL, *query = NULL, *feature = NULL;
805
806                 iq = iks_new("iq");
807                 query = iks_new("query");
808                 feature = iks_new("feature");
809
810                 if (iq && query && feature && client) {
811                         iks_insert_attrib(iq, "from", client->user);
812                         iks_insert_attrib(iq, "to", pak->from->full);
813                         iks_insert_attrib(iq, "id", pak->id);
814                         iks_insert_attrib(iq, "type", "result");
815                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
816                         iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
817                         iks_insert_node(iq, query);
818                         iks_insert_node(query, feature);
819                         iks_send(client->p, iq);
820                 } else {
821                         ast_log(LOG_ERROR, "Out of memory.\n");
822                 }
823                 if (iq)
824                         iks_delete(iq);
825                 if (query)
826                         iks_delete(query);
827                 if (feature)
828                         iks_delete(feature);
829         }
830
831         ASTOBJ_UNREF(client, aji_client_destroy);
832         return IKS_FILTER_EAT;
833
834 }
835
836 static int aji_client_info_handler(void *data, ikspak *pak)
837 {
838         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
839         struct aji_resource *resource = NULL;
840         struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
841
842         resource = aji_find_resource(buddy, pak->from->resource);
843
844         if (pak->subtype == IKS_TYPE_RESULT) {
845                 if (!resource) {
846                         ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full);
847                         ASTOBJ_UNREF(client, aji_client_destroy);
848                         return IKS_FILTER_EAT;
849                 }
850                 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
851                         resource->cap->jingle = 1;
852                 } else
853                         resource->cap->jingle = 0;
854         } else if (pak->subtype == IKS_TYPE_GET) {
855                 iks *iq, *disco, *ident, *google, *query;
856                 iq = iks_new("iq");
857                 query = iks_new("query");
858                 ident = iks_new("identity");
859                 disco = iks_new("feature");
860                 google = iks_new("feature");
861                 if (iq && ident && disco && google) {
862                         iks_insert_attrib(iq, "from", client->jid->full);
863                         iks_insert_attrib(iq, "to", pak->from->full);
864                         iks_insert_attrib(iq, "type", "result");
865                         iks_insert_attrib(iq, "id", pak->id);
866                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
867                         iks_insert_attrib(ident, "category", "client");
868                         iks_insert_attrib(ident, "type", "pc");
869                         iks_insert_attrib(ident, "name", "asterisk");
870                         iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
871                         iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
872                         iks_insert_node(iq, query);
873                         iks_insert_node(query, ident);
874                         iks_insert_node(query, google);
875                         iks_insert_node(query, disco);
876                         iks_send(client->p, iq);
877                 } else
878                         ast_log(LOG_ERROR, "Out of Memory.\n");
879                 if (iq)
880                         iks_delete(iq);
881                 if (query)
882                         iks_delete(query);
883                 if (ident)
884                         iks_delete(ident);
885                 if (google)
886                         iks_delete(google);
887                 if (disco)
888                         iks_delete(disco);
889         } else if (pak->subtype == IKS_TYPE_ERROR) {
890                 ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
891         }
892         ASTOBJ_UNREF(client, aji_client_destroy);
893         return IKS_FILTER_EAT;
894 }
895
896 static int aji_dinfo_handler(void *data, ikspak *pak)
897 {
898         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
899         char *node = NULL;
900         if (!strcasecmp(iks_find_attrib(pak->x,"type"),"error")) {
901                 ast_log(LOG_WARNING, "Recieved error from a client, turn on jabber debug!\n");
902                 return IKS_FILTER_EAT;
903         }
904         if (!(node = iks_find_attrib(pak->query, "node"))) {
905                 iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search;
906
907                 iq = iks_new("iq");
908                 query = iks_new("query");
909                 identity = iks_new("identity");
910                 disco = iks_new("feature");
911                 reg = iks_new("feature");
912                 commands = iks_new("feature");
913                 gateway = iks_new("feature");
914                 version = iks_new("feature");
915                 vcard = iks_new("feature");
916                 search = iks_new("feature");
917
918                 if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
919                         iks_insert_attrib(iq, "from", client->user);
920                         iks_insert_attrib(iq, "to", pak->from->full);
921                         iks_insert_attrib(iq, "id", pak->id);
922                         iks_insert_attrib(iq, "type", "result");
923                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
924                         iks_insert_attrib(identity, "category", "gateway");
925                         iks_insert_attrib(identity, "type", "pstn");
926                         iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
927                         iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
928                         iks_insert_attrib(reg, "var", "jabber:iq:register");
929                         iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
930                         iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
931                         iks_insert_attrib(version, "var", "jabber:iq:version");
932                         iks_insert_attrib(vcard, "var", "vcard-temp");
933                         iks_insert_attrib(search, "var", "jabber:iq:search");
934
935                         iks_insert_node(iq, query);
936                         iks_insert_node(query, identity);
937                         iks_insert_node(query, disco);
938                         iks_insert_node(query, reg);
939                         iks_insert_node(query, commands);
940                         iks_insert_node(query, gateway);
941                         iks_insert_node(query, version);
942                         iks_insert_node(query, vcard);
943                         iks_insert_node(query, search);
944                         iks_send(client->p, iq);
945                 } else {
946                         ast_log(LOG_ERROR, "Out of memory.\n");
947                 }
948
949                 if (iq)
950                         iks_delete(iq);
951                 if (query)
952                         iks_delete(query);
953                 if (identity)
954                         iks_delete(identity);
955                 if (disco)
956                         iks_delete(disco);
957                 if (reg)
958                         iks_delete(reg);
959                 if (commands)
960                         iks_delete(commands);
961                 if (gateway)
962                         iks_delete(gateway);
963                 if (version)
964                         iks_delete(version);
965                 if (vcard)
966                         iks_delete(vcard);
967                 if (search)
968                         iks_delete(search);
969
970         } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
971                 iks *iq, *query, *confirm;
972                 iq = iks_new("iq");
973                 query = iks_new("query");
974                 confirm = iks_new("item");
975
976                 if (iq && query && confirm && client) {
977                         iks_insert_attrib(iq, "from", client->user);
978                         iks_insert_attrib(iq, "to", pak->from->full);
979                         iks_insert_attrib(iq, "id", pak->id);
980                         iks_insert_attrib(iq, "type", "result");
981                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
982                         iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
983                         iks_insert_attrib(confirm, "node", "confirmaccount");
984                         iks_insert_attrib(confirm, "name", "Confirm AIM account");
985                         iks_insert_attrib(confirm, "jid", client->user);
986                         iks_insert_node(iq, query);
987                         iks_insert_node(query, confirm);
988                         iks_send(client->p, iq);
989                 } else {
990                         ast_log(LOG_ERROR, "Out of memory.\n");
991                 }
992                 if (iq)
993                         iks_delete(iq);
994                 if (query)
995                         iks_delete(query);
996                 if (confirm)
997                         iks_delete(confirm);
998
999         } else if (!strcasecmp(node, "confirmaccount")) {
1000                 iks *iq, *query, *feature;
1001
1002                 iq = iks_new("iq");
1003                 query = iks_new("query");
1004                 feature = iks_new("feature");
1005
1006                 if (iq && query && feature && client) {
1007                         iks_insert_attrib(iq, "from", client->user);
1008                         iks_insert_attrib(iq, "to", pak->from->full);
1009                         iks_insert_attrib(iq, "id", pak->id);
1010                         iks_insert_attrib(iq, "type", "result");
1011                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1012                         iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1013                         iks_insert_node(iq, query);
1014                         iks_insert_node(query, feature);
1015                         iks_send(client->p, iq);
1016                 } else {
1017                         ast_log(LOG_ERROR, "Out of memory.\n");
1018                 }
1019                 if (iq)
1020                         iks_delete(iq);
1021                 if (query)
1022                         iks_delete(query);
1023                 if (feature)
1024                         iks_delete(feature);
1025         }
1026
1027         ASTOBJ_UNREF(client, aji_client_destroy);
1028         return IKS_FILTER_EAT;
1029 }
1030
1031 /*!
1032  * \brief Handles <iq> tags.
1033  * \param client structure and the iq node.
1034  * \return void.
1035  */
1036 static void aji_handle_iq(struct aji_client *client, iks *node)
1037 {
1038         /*Nothing to see here */
1039 }
1040
1041 /*!
1042  * \brief Handles presence packets.
1043  * \param client structure and the node.
1044  * \return void.
1045  */
1046 static void aji_handle_message(struct aji_client *client, ikspak *pak)
1047 {
1048         struct aji_message *insert, *tmp;
1049         int flag = 0;
1050         
1051         if (!(insert = ast_calloc(1, sizeof(struct aji_message))))
1052                 return;
1053         time(&insert->arrived);
1054         if (iks_find_cdata(pak->x, "body"))
1055                 insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
1056         if(pak->id)
1057                 ast_copy_string(insert->id, pak->id, sizeof(insert->message));
1058         insert->from = ast_strdup(pak->from->full);
1059         AST_LIST_LOCK(&client->messages);
1060         AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
1061                 if (flag) {
1062                         AST_LIST_REMOVE_CURRENT(&client->messages, list);
1063                         if (tmp->from)
1064                                 free(tmp->from);
1065                         if (tmp->message)
1066                                 free(tmp->message);
1067                 } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) {
1068                         flag = 1;
1069                         AST_LIST_REMOVE_CURRENT(&client->messages, list);
1070                         if (tmp->from)
1071                                 free(tmp->from);
1072                         if (tmp->message)
1073                                 free(tmp->message);
1074                 }
1075         }
1076         AST_LIST_TRAVERSE_SAFE_END;
1077         AST_LIST_INSERT_HEAD(&client->messages, insert, list);
1078         AST_LIST_UNLOCK(&client->messages);
1079 }
1080
1081 static void aji_handle_presence(struct aji_client *client, ikspak *pak)
1082 {
1083         int status, priority;
1084         struct aji_buddy *buddy;
1085         struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
1086         char *ver, *node, *descrip;
1087         
1088         if(client->state != AJI_CONNECTED)
1089                 aji_create_buddy(pak->from->partial, client);
1090
1091         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1092         if (!buddy) {
1093                 ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial);
1094                 return;
1095         }
1096         if(client->component && !strcasecmp(iks_find_attrib(pak->x, "type"), "probe")) {
1097                 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), 1, client->statusmessage);
1098                 ast_verbose("what i was looking for \n");
1099         }
1100         ASTOBJ_WRLOCK(buddy);
1101         status = (pak->show) ? pak->show : 6;
1102         priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
1103         tmp = buddy->resources;
1104         descrip = ast_strdup(iks_find_cdata(pak->x,"status"));
1105
1106         while (tmp) {
1107                 if (!strcasecmp(tmp->resource, pak->from->resource)) {
1108                         tmp->status = status;
1109                         if (tmp->description) free(tmp->description);
1110                         tmp->description = descrip;
1111                         found = tmp;
1112                         if (status == 6) {      /* Sign off Destroy resource */
1113                                 if (last && found->next) {
1114                                         last->next = found->next;
1115                                 } else if (!last) {
1116                                         if (found->next)
1117                                                 buddy->resources = found->next;
1118                                         else
1119                                                 buddy->resources = NULL;
1120                                 } else if (!found->next) {
1121                                         if (last)
1122                                                 last->next = NULL;
1123                                         else
1124                                                 buddy->resources = NULL;
1125                                 }
1126                                 free(found);
1127                                 found = NULL;
1128                                 break;
1129                         }
1130                         if (tmp->priority != priority) {
1131                                 found->priority = priority;
1132                                 if (!last && !found->next)
1133                                         break;
1134                                 if (last)
1135                                         last->next = found->next;
1136                                 else
1137                                         buddy->resources = found->next;
1138                                 last = NULL;
1139                                 tmp = buddy->resources;
1140                                 if (!buddy->resources)
1141                                         buddy->resources = found;
1142                                 while (tmp) {
1143                                         if (found->priority > tmp->priority) {
1144                                                 if (last)
1145                                                         last->next = found;
1146                                                 found->next = tmp;
1147                                                 if (!last)
1148                                                         buddy->resources = found;
1149                                                 break;
1150                                         }
1151                                         if (!tmp->next) {
1152                                                 tmp->next = found;
1153                                                 break;
1154                                         }
1155                                         last = tmp;
1156                                         tmp = tmp->next;
1157                                 }
1158                         }
1159                         break;
1160                 }
1161                 last = tmp;
1162                 tmp = tmp->next;
1163         }
1164
1165         if (!found && status != 6) {
1166                 found = (struct aji_resource *) malloc(sizeof(struct aji_resource));
1167                 memset(found, 0, sizeof(struct aji_resource));
1168
1169                 if (!found) {
1170                         ast_log(LOG_ERROR, "Out of memory!\n");
1171                         return;
1172                 }
1173                 ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
1174                 found->status = status;
1175                 found->description = descrip;
1176                 found->priority = priority;
1177                 found->next = NULL;
1178                 last = NULL;
1179                 tmp = buddy->resources;
1180                 while (tmp) {
1181                         if (found->priority > tmp->priority) {
1182                                 if (last)
1183                                         last->next = found;
1184                                 found->next = tmp;
1185                                 if (!last)
1186                                         buddy->resources = found;
1187                                 break;
1188                         }
1189                         if (!tmp->next) {
1190                                 tmp->next = found;
1191                                 break;
1192                         }
1193                         last = tmp;
1194                         tmp = tmp->next;
1195                 }
1196                 if (!tmp)
1197                         buddy->resources = found;
1198         }
1199         ASTOBJ_UNLOCK(buddy);
1200         ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1201
1202         node = iks_find_attrib(iks_find(pak->x, "c"), "node");
1203         ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
1204
1205         if(status !=6 && !found->cap) {
1206                 found->cap = aji_find_version(node, ver, pak);
1207                 if(gtalk_yuck(pak->x)) /* gtalk should do discover */
1208                         found->cap->jingle = 1;
1209                 if(found->cap->jingle && option_debug > 4)
1210                         ast_log(LOG_DEBUG,"Special case for google till they support discover.\n");
1211                 else {
1212                         iks *iq, *query;
1213                         iq = iks_new("iq");
1214                         query = iks_new("query");
1215                         if(query && iq)  {
1216                                 iks_insert_attrib(iq, "type", "get");
1217                                 iks_insert_attrib(iq, "to", pak->from->full);
1218                                 iks_insert_attrib(iq,"from",iks_find_attrib(pak->x,"to"));
1219                                 iks_insert_attrib(iq, "id", client->mid);
1220                                 ast_aji_increment_mid(client->mid);
1221                                 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1222                                 iks_insert_node(iq, query);
1223                                 iks_send(client->p, iq);
1224                                 
1225                         } else
1226                                 ast_log(LOG_ERROR, "Out of memory.\n");
1227                         if(query)
1228                                 iks_delete(query);
1229                         if(iq)
1230                                 iks_delete(iq);
1231                 }
1232         }
1233         if (option_verbose > 4) {
1234                 switch (pak->subtype) {
1235                 case IKS_TYPE_AVAILABLE:
1236                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am available ^_* %i\n", pak->subtype);
1237                         break;
1238                 case IKS_TYPE_UNAVAILABLE:
1239                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am unavailable ^_* %i\n", pak->subtype);
1240                         break;
1241                 default:
1242                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype);
1243                 }
1244                 switch (pak->show) {
1245                 case IKS_SHOW_UNAVAILABLE:
1246                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1247                         break;
1248                 case IKS_SHOW_AVAILABLE:
1249                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is available\n");
1250                         break;
1251                 case IKS_SHOW_CHAT:
1252                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1253                         break;
1254                 case IKS_SHOW_AWAY:
1255                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is away\n");
1256                         break;
1257                 case IKS_SHOW_XA:
1258                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1259                         break;
1260                 case IKS_SHOW_DND:
1261                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1262                         break;
1263                 default:
1264                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: Kinky! how did that happen %i\n", pak->show);
1265                 }
1266         }
1267 }
1268
1269 /*!
1270  * \brief handles subscription requests.
1271  * \param aji_client struct and xml packet.
1272  * \return void.
1273  */
1274 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
1275 {
1276         if(pak->subtype == IKS_TYPE_SUBSCRIBE) { 
1277                 iks *presence = NULL, *status = NULL;
1278                 presence = iks_new("presence");
1279                 status = iks_new("status");
1280                 if(presence && status) {
1281                         iks_insert_attrib(presence, "type", "subscribed");
1282                         iks_insert_attrib(presence, "to", pak->from->full);
1283                         iks_insert_attrib(presence, "from", iks_find_attrib(pak->x, "to"));
1284                         if(pak->id)
1285                                 iks_insert_attrib(presence, "id", pak->id);
1286                         iks_insert_cdata(status, "Asterisk has approved subscription", 0);
1287                         iks_insert_node(presence, status);
1288                         iks_send(client->p, presence);
1289                 } else
1290                         ast_log(LOG_ERROR, "Unable to allocate nodes\n");
1291                 if(presence)
1292                         iks_delete(presence);
1293                 if(status)
1294                         iks_delete(status);
1295                 if(client->component)
1296                         aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), 1, client->statusmessage);
1297         }
1298         if (option_verbose > 4) {
1299                 switch (pak->subtype) {
1300                 case IKS_TYPE_SUBSCRIBE:
1301                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1302                         break;
1303                 case IKS_TYPE_SUBSCRIBED:
1304                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1305                         break;
1306                 case IKS_TYPE_UNSUBSCRIBE:
1307                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1308                         break;
1309                 case IKS_TYPE_UNSUBSCRIBED:
1310                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1311                         break;
1312                 default:                                /*IKS_TYPE_ERROR: */
1313                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1314                         break;
1315                 }
1316         }
1317 }
1318
1319 /*!
1320  * \brief sends messages.
1321  * \param aji_client struct , reciever, message.
1322  * \return 1.
1323  */
1324 int ast_aji_send(struct aji_client *client, char *address, char *message)
1325 {
1326         int res = 0;
1327         iks *message_packet = NULL;
1328         if (client->state == AJI_CONNECTED) {
1329                 message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message);
1330                 if (message_packet) {
1331                         iks_insert_attrib(message_packet, "from", client->jid->full);
1332                         res = iks_send(client->p, message_packet);
1333                 } else {
1334                         ast_log(LOG_ERROR, "Out of memory.\n");
1335                 }
1336                 if (message_packet)
1337                         iks_delete(message_packet);
1338         } else
1339                 ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
1340         return 1;
1341 }
1342
1343 /*!
1344  * \brief create a chatroom.
1345  * \param aji_client struct , room, server, topic for the room.
1346  * \return 0.
1347  */
1348 int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
1349 {
1350         int res = 0;
1351         iks *iq = NULL;
1352         iq = iks_new("iq");
1353         if (iq && client) {
1354                 iks_insert_attrib(iq, "type", "get");
1355                 iks_insert_attrib(iq, "to", server);
1356                 iks_insert_attrib(iq, "id", client->mid);
1357                 ast_aji_increment_mid(client->mid);
1358                 iks_send(client->p, iq);
1359         } else 
1360                 ast_log(LOG_ERROR, "Out of memory.\n");
1361         return res;
1362 }
1363
1364 /*!
1365  * \brief join a chatroom.
1366  * \param aji_client struct , room.
1367  * \return res.
1368  */
1369 int ast_aji_join_chat(struct aji_client *client, char *room)
1370 {
1371         int res = 0;
1372         iks *presence = NULL, *priority = NULL;
1373         presence = iks_new("presence");
1374         priority = iks_new("priority");
1375         if (presence && priority && client) {
1376                 iks_insert_cdata(priority, "0", 1);
1377                 iks_insert_attrib(presence, "to", room);
1378                 iks_insert_node(presence, priority);
1379                 res = iks_send(client->p, presence);
1380                 iks_insert_cdata(priority, "5", 1);
1381                 iks_insert_attrib(presence, "to", room);
1382                 res = iks_send(client->p, presence);
1383         } else 
1384                 ast_log(LOG_ERROR, "Out of memory.\n");
1385         if (presence)
1386                 iks_delete(presence);
1387         if (priority)
1388                 iks_delete(priority);
1389         return res;
1390 }
1391
1392 /*!
1393  * \brief invite to a chatroom.
1394  * \param aji_client struct ,user, room, message.
1395  * \return res.
1396  */
1397 int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
1398 {
1399         int res = 0;
1400         iks *invite, *body, *namespace;
1401
1402         invite = iks_new("message");
1403         body = iks_new("body");
1404         namespace = iks_new("x");
1405         if (client && invite && body && namespace) {
1406                 iks_insert_attrib(invite, "to", user);
1407                 iks_insert_attrib(invite, "id", client->mid);
1408                 ast_aji_increment_mid(client->mid);
1409                 iks_insert_cdata(body, message, 0);
1410                 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
1411                 iks_insert_attrib(namespace, "jid", room);
1412                 iks_insert_node(invite, body);
1413                 iks_insert_node(invite, namespace);
1414                 res = iks_send(client->p, invite);
1415         } else 
1416                 ast_log(LOG_ERROR, "Out of memory.\n");
1417         if (body)
1418                 iks_delete(body);
1419         if (namespace)
1420                 iks_delete(namespace);
1421         if (invite)
1422                 iks_delete(invite);
1423         return res;
1424 }
1425
1426
1427 /*!
1428  * \brief receive message loop.
1429  * \param aji_client struct.
1430  * \return void.
1431  */
1432 static void *aji_recv_loop(void *data)
1433 {
1434         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1435         int res = IKS_HOOK;
1436         do {
1437                 if (res != IKS_OK) {
1438                         while(res != IKS_OK) {
1439                                 if(option_verbose > 3)
1440                                         ast_verbose("JABBER: reconnecting.\n");
1441                                 res = aji_reconnect(client);
1442                                 sleep(4);
1443                         }
1444                 }
1445
1446                 res = iks_recv(client->p, 1);
1447                 client->timeout--;
1448                 if (res == IKS_HOOK) 
1449                         ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
1450                 else if (res == IKS_NET_TLSFAIL)
1451                         ast_log(LOG_WARNING, "JABBER:  Failure in TLS.\n");
1452                 else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
1453                         res = iks_send_raw(client->p, " ");
1454                         if(res == IKS_OK)
1455                                 client->timeout = 50;
1456                         else
1457                                 ast_log(LOG_WARNING, "JABBER:  Network Timeout\n");
1458                 } else if (res == IKS_NET_RWERR)
1459                         ast_log(LOG_WARNING, "JABBER: socket read error\n");
1460         } while (client);
1461         ASTOBJ_UNREF(client, aji_client_destroy);
1462         return 0;
1463 }
1464
1465 /*!
1466  * \brief increments the mid field for messages and other events.
1467  * \param message id.
1468  * \return void.
1469  */
1470 void ast_aji_increment_mid(char *mid)
1471 {
1472         int i = 0;
1473
1474         for (i = strlen(mid) - 1; i >= 0; i--) {
1475                 if (mid[i] != 'z') {
1476                         mid[i] = mid[i] + 1;
1477                         i = 0;
1478                 } else
1479                         mid[i] = 'a';
1480         }
1481 }
1482
1483
1484 /*!
1485  * \brief attempts to register to a transport.
1486  * \param aji_client struct, and xml packet.
1487  * \return IKS_FILTER_EAT.
1488  */
1489 static int aji_register_transport(void *data, ikspak *pak)
1490 {
1491         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1492         int res = 0;
1493         struct aji_buddy *buddy = NULL;
1494         iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
1495
1496         if (client && send) {
1497                 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1498                         ASTOBJ_RDLOCK(iterator); 
1499                         if (iterator->btype == AJI_TRANS) {
1500                                   buddy = iterator;
1501                         }
1502                         ASTOBJ_UNLOCK(iterator);
1503                 });
1504                 iks_filter_remove_hook(client->f, aji_register_transport);
1505                 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);
1506                 iks_insert_attrib(send, "to", buddy->host);
1507                 iks_insert_attrib(send, "id", client->mid);
1508                 ast_aji_increment_mid(client->mid);
1509                 iks_insert_attrib(send, "from", client->user);
1510                 res = iks_send(client->p, send);
1511         } else 
1512                 ast_log(LOG_ERROR, "Out of memory.\n");
1513
1514         if (send)
1515                 iks_delete(send);
1516         ASTOBJ_UNREF(client, aji_client_destroy);
1517         return IKS_FILTER_EAT;
1518
1519 }
1520
1521 /*!
1522  * \brief attempts to register to a transport step 2.
1523  * \param aji_client struct, and xml packet.
1524  * \return IKS_FILTER_EAT.
1525  */
1526 static int aji_register_transport2(void *data, ikspak *pak)
1527 {
1528         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1529         int res = 0;
1530         struct aji_buddy *buddy = NULL;
1531
1532         iks *regiq = iks_new("iq");
1533         iks *regquery = iks_new("query");
1534         iks *reguser = iks_new("username");
1535         iks *regpass = iks_new("password");
1536
1537         if (client && regquery && reguser && regpass && regiq) {
1538                 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1539                         ASTOBJ_RDLOCK(iterator);
1540                         if (iterator->btype == AJI_TRANS)
1541                                 buddy = iterator; ASTOBJ_UNLOCK(iterator);
1542                 });
1543                 iks_filter_remove_hook(client->f, aji_register_transport2);
1544                 iks_insert_attrib(regiq, "to", buddy->host);
1545                 iks_insert_attrib(regiq, "type", "set");
1546                 iks_insert_attrib(regiq, "id", client->mid);
1547                 ast_aji_increment_mid(client->mid);
1548                 iks_insert_attrib(regiq, "from", client->user);
1549                 iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
1550                 iks_insert_cdata(reguser, buddy->user, 0);
1551                 iks_insert_cdata(regpass, buddy->pass, 0);
1552                 iks_insert_node(regiq, regquery);
1553                 iks_insert_node(regquery, reguser);
1554                 iks_insert_node(regquery, regpass);
1555                 res = iks_send(client->p, regiq);
1556         } else
1557                 ast_log(LOG_ERROR, "Out of memory.\n");
1558         if (regiq)
1559                 iks_delete(regiq);
1560         if (regquery)
1561                 iks_delete(regquery);
1562         if (reguser)
1563                 iks_delete(reguser);
1564         if (regpass)
1565                 iks_delete(regpass);
1566         ASTOBJ_UNREF(client, aji_client_destroy);
1567         return IKS_FILTER_EAT;
1568 }
1569
1570 /*!
1571  * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
1572  * \param aji_client struct.
1573  * \return void.
1574  */
1575 static void aji_pruneregister(struct aji_client *client)
1576 {
1577         int res = 0;
1578         iks *removeiq = iks_new("iq");
1579         iks *removequery = iks_new("query");
1580         iks *removeitem = iks_new("item");
1581         iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
1582
1583         if (client && removeiq && removequery && removeitem && send) {
1584                 iks_insert_node(removeiq, removequery);
1585                 iks_insert_node(removequery, removeitem);
1586                 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1587                         ASTOBJ_RDLOCK(iterator);
1588                         /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
1589                          * be called at the same time */
1590                         if (ast_test_flag(iterator, AJI_AUTOPRUNE)) {
1591                                 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
1592                                                 "GoodBye your status is no longer needed by Asterisk the Open Source PBX"
1593                                                 " so I am no longer subscribing to your presence.\n"));
1594                                 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
1595                                                 "GoodBye you are no longer in the asterisk config file so I am removing"
1596                                                 " your access to my presence.\n"));
1597                                 iks_insert_attrib(removeiq, "from", client->jid->full); 
1598                                 iks_insert_attrib(removeiq, "type", "set"); 
1599                                 iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
1600                                 iks_insert_attrib(removeitem, "jid", iterator->name);
1601                                 iks_insert_attrib(removeitem, "subscription", "remove");
1602                                 res = iks_send(client->p, removeiq);
1603                         } else if (ast_test_flag(iterator, AJI_AUTOREGISTER)) {
1604                                 if (iterator->btype == AJI_USER) {      /*if it is not a transport */
1605                                         res = iks_send(client->p, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name, 
1606                                                         "Greetings I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
1607                                 } else {
1608                                         iks_filter_add_rule(client->f, aji_register_transport, client,
1609                                                                   IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS,
1610                                                                   "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
1611                                         iks_insert_attrib(send, "to", iterator->host);
1612                                         iks_insert_attrib(send, "from", client->jid->full);
1613                                         res = iks_send(client->p, send);
1614                                 }
1615                                 ast_clear_flag(iterator, AJI_AUTOREGISTER);
1616                         }
1617                         ASTOBJ_UNLOCK(iterator);
1618                 });
1619         } else
1620                 ast_log(LOG_ERROR, "Out of memory.\n");
1621         if (removeiq)
1622                 iks_delete(removeiq);
1623         if (removequery)
1624                 iks_delete(removequery);
1625         if (removeitem)
1626                 iks_delete(removeitem);
1627         if (send)
1628                 iks_delete(send);
1629         ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy);
1630 }
1631
1632 /*!
1633  * \brief filters the roster packet we get back from server.
1634  * \param aji_client struct, and xml packet.
1635  * \return IKS_FILTER_EAT.
1636  */
1637 static int aji_filter_roster(void *data, ikspak *pak)
1638 {
1639         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1640         int flag = 0;
1641         iks *x = NULL;
1642         struct aji_buddy *buddy;
1643         
1644         client->state = AJI_CONNECTED;
1645         ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1646                 ASTOBJ_RDLOCK(iterator);
1647                 x = iks_child(pak->query);
1648                 flag = 0;
1649                 while (x) {
1650                         if (!iks_strcmp(iks_name(x), "item")) {
1651                                 if (!ast_strlen_zero(iterator->pass)) {
1652                                         if (!strcasecmp(iterator->host, iks_find_attrib(x, "jid"))) {
1653                                                   ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER); 
1654                                                   flag = 1;
1655                                         }
1656                                 } else {
1657                                         if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
1658                                                   flag = 1;
1659                                                   ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
1660                                         }
1661                                 }
1662                         }
1663                         x = iks_next(x);
1664                 }
1665                 if (!flag)
1666                         ast_copy_flags(iterator, client, AJI_AUTOREGISTER);
1667                 if (x)
1668                         iks_delete(x);
1669                 ASTOBJ_UNLOCK(iterator);
1670         });
1671
1672         x = iks_child(pak->query);
1673         while (x) {
1674                 flag = 0;
1675                 if (iks_strcmp(iks_name(x), "item") == 0) {
1676                         ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1677                                 ASTOBJ_RDLOCK(iterator);
1678                                 if (!ast_strlen_zero(iterator->pass)) {
1679                                         if (!strcasecmp(iterator->host, iks_find_attrib(x, "jid")))
1680                                         flag = 1;
1681                                 } else {
1682                                         if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
1683                                                 flag = 1;
1684                                 }
1685                                 ASTOBJ_UNLOCK(iterator);
1686                         });
1687
1688                         if (!flag) {
1689                                 buddy = (struct aji_buddy *) malloc(sizeof(struct aji_buddy));
1690                                 if (!buddy) {
1691                                         ast_log(LOG_WARNING, "Out of memory\n");
1692                                         return 0;
1693                                 }
1694                                 memset(buddy, 0, sizeof(struct aji_buddy));
1695                                 ASTOBJ_INIT(buddy);
1696                                 ASTOBJ_WRLOCK(buddy);
1697                                 ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
1698                                 ast_clear_flag(buddy, AST_FLAGS_ALL);
1699                                 if(ast_test_flag(client, AJI_AUTOPRUNE)) {
1700                                         ast_set_flag(buddy, AJI_AUTOPRUNE);
1701                                         buddy->objflags |= ASTOBJ_FLAG_MARKED;
1702                                 } else
1703                                         ast_set_flag(buddy, AJI_AUTOREGISTER);
1704                                 ASTOBJ_UNLOCK(buddy);
1705                                 if (buddy) {
1706                                         ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
1707                                         ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1708                                 }
1709                         }
1710                 }
1711                 x = iks_next(x);
1712         }
1713         if (x)
1714                 iks_delete(x);
1715         aji_pruneregister(client);
1716
1717         ASTOBJ_UNREF(client, aji_client_destroy);
1718         return IKS_FILTER_EAT;
1719 }
1720
1721 static int aji_reconnect(struct aji_client *client)
1722 {
1723         int res = 0;
1724
1725         if (client->state)
1726                 client->state = AJI_DISCONNECTED;
1727         client->timeout=50;
1728         if (client->p)
1729                 iks_parser_reset(client->p);
1730         if (client->authorized)
1731                 client->authorized = 0;
1732
1733         if(client->component)
1734                 res = aji_component_initialize(client);
1735         else
1736                 res = aji_client_initialize(client);
1737
1738         return res;
1739 }
1740
1741 static int aji_get_roster(struct aji_client *client)
1742 {
1743         iks *roster = NULL;
1744         roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
1745         if(roster) {
1746                 iks_insert_attrib(roster, "id", "roster");
1747                 aji_set_presence(client, NULL, client->jid->full, 1, client->statusmessage);
1748                 iks_send(client->p, roster);
1749         }
1750         if (roster)
1751                 iks_delete(roster);
1752         return 1;
1753 }
1754
1755 /*!
1756  * \brief connects as a client to jabber server.
1757  * \param aji_client struct, and xml packet.
1758  * \return res.
1759  */
1760 static int aji_client_connect(void *data, ikspak *pak)
1761 {
1762         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1763         int res = 0;
1764
1765         if (client) {
1766                 if (client->state == AJI_DISCONNECTED) {
1767                         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);
1768                         client->state = AJI_CONNECTING;
1769                         client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
1770                         iks_filter_remove_hook(client->f, aji_client_connect);
1771                         if(!client->component) /*client*/
1772                                 aji_get_roster(client);
1773                 }
1774         } else
1775                 ast_log(LOG_ERROR, "Out of memory.\n");
1776
1777         ASTOBJ_UNREF(client, aji_client_destroy);
1778         return res;
1779 }
1780
1781 /*!
1782  * \brief prepares client for connect.
1783  * \param aji_client struct.
1784  * \return 1.
1785  */
1786 static int aji_client_initialize(struct aji_client *client)
1787 {
1788         int connected = 0;
1789
1790         connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->jid->server);
1791
1792         if (connected == IKS_NET_NOCONN) {
1793                 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
1794                 return IKS_HOOK;
1795         } else  if (connected == IKS_NET_NODNS) {
1796                 ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to  %s\n", client->name, S_OR(client->serverhost, client->jid->server));
1797                 return IKS_HOOK;
1798         } else
1799                 iks_recv(client->p, 30);
1800         return IKS_OK;
1801 }
1802
1803 /*!
1804  * \brief prepares component for connect.
1805  * \param aji_client struct.
1806  * \return 1.
1807  */
1808 static int aji_component_initialize(struct aji_client *client)
1809 {
1810         int connected = 1;
1811         connected = iks_connect_via(client->p, client->jid->server, client->port, client->user);
1812         if (connected == IKS_NET_NOCONN) {
1813                 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
1814                 return IKS_HOOK;
1815         } else if (connected == IKS_NET_NODNS) {
1816                 ast_log(LOG_ERROR, "JABBER ERROR: No DNS\n");
1817                 return IKS_HOOK;
1818         } else if (!connected) 
1819                 iks_recv(client->p, 30);
1820         return IKS_OK;
1821 }
1822
1823 /*!
1824  * \brief disconnect from jabber server.
1825  * \param aji_client struct.
1826  * \return 1.
1827  */
1828 int ast_aji_disconnect(struct aji_client *client)
1829 {
1830         if (client) {
1831                 if (option_verbose > 3)
1832                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: Disconnecting\n");
1833                 iks_disconnect(client->p);
1834                 iks_parser_delete(client->p);
1835                 ASTOBJ_UNREF(client, aji_client_destroy);
1836         }
1837
1838         return 1;
1839 }
1840
1841 /*!
1842  * \brief set presence of client.
1843  * \param aji_client struct, user to send it to, and from, level, description.
1844  * \return void.
1845  */
1846 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc)
1847 {
1848         int res = 0;
1849         iks *presence = iks_make_pres(level, desc);
1850         iks *cnode = iks_new("c");
1851         iks *priority = iks_new("priority");
1852
1853         iks_insert_cdata(priority, "0", 1);
1854         if (presence && cnode && client) {
1855                 if(to)
1856                         iks_insert_attrib(presence, "to", to);
1857                 if(from)
1858                         iks_insert_attrib(presence, "from", from);
1859                 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
1860                 iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
1861                 iks_insert_attrib(cnode, "ext", "voice-v1");
1862                 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
1863                 iks_insert_node(presence, cnode);
1864                 res = iks_send(client->p, presence);
1865         } else
1866                 ast_log(LOG_ERROR, "Out of memory.\n");
1867         if (cnode)
1868                 iks_delete(cnode);
1869         if (presence)
1870                 iks_delete(presence);
1871 }
1872
1873 /*!
1874  * \brief turnon console debugging.
1875  * \param fd, number of args, args.
1876  * \return RESULT_SUCCESS.
1877  */
1878 static int aji_do_debug(int fd, int argc, char *argv[])
1879 {
1880         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
1881                 ASTOBJ_RDLOCK(iterator); 
1882                 iterator->debug = 1;
1883                 ASTOBJ_UNLOCK(iterator);
1884         });
1885         ast_cli(fd, "Jabber Debugging Enabled.\n");
1886         return RESULT_SUCCESS;
1887 }
1888
1889 /*!
1890  * \brief reload jabber module.
1891  * \param fd, number of args, args.
1892  * \return RESULT_SUCCESS.
1893  */
1894 static int aji_do_reload(int fd, int argc, char *argv[])
1895 {
1896         aji_reload();
1897         ast_cli(fd, "Jabber Reloaded.\n");
1898         return RESULT_SUCCESS;
1899 }
1900
1901 /*!
1902  * \brief turnoff console debugging.
1903  * \param fd, number of args, args.
1904  * \return RESULT_SUCCESS.
1905  */
1906 static int aji_no_debug(int fd, int argc, char *argv[])
1907 {
1908         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
1909                 ASTOBJ_RDLOCK(iterator);
1910                 iterator->debug = 0;
1911                 ASTOBJ_UNLOCK(iterator);
1912         });
1913         ast_cli(fd, "Jabber Debugging Disabled.\n");
1914         return RESULT_SUCCESS;
1915 }
1916
1917 /*!
1918  * \brief show client status.
1919  * \param fd, number of args, args.
1920  * \return RESULT_SUCCESS.
1921  */
1922 static int aji_show_clients(int fd, int argc, char *argv[])
1923 {
1924         char *status;
1925         int count = 0;
1926         ast_cli(fd, "Jabber Users and their status:\n");
1927         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
1928                 ASTOBJ_RDLOCK(iterator);
1929                 count++;
1930                 switch (iterator->state) {
1931                 case AJI_DISCONNECTED:
1932                         status = "Disconnected";
1933                         break;
1934                 case AJI_CONNECTING:
1935                         status = "Connecting";
1936                         break;
1937                 case AJI_CONNECTED:
1938                         status = "Connected";
1939                         break;
1940                 default:
1941                         status = "Unknown";
1942                 }
1943                 ast_cli(fd, "       User: %s     - %s\n", iterator->user, status);
1944                 ASTOBJ_UNLOCK(iterator);
1945         });
1946         ast_cli(fd, "----\n");
1947         ast_cli(fd, "   Number of users: %d\n", count);
1948         return RESULT_SUCCESS;
1949 }
1950
1951 /*!
1952  * \brief send test message for debugging.
1953  * \param fd, number of args, args.
1954  * \return RESULT_SUCCESS.
1955  */
1956 static int aji_test(int fd, int argc, char *argv[])
1957 {
1958         struct aji_client *client;
1959         struct aji_resource *resource;
1960         const char *name = "asterisk";
1961         struct aji_message *tmp;
1962
1963         if (argc > 3)
1964                 return RESULT_SHOWUSAGE;
1965         else if (argc == 3)
1966                 name = argv[2];
1967
1968         if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
1969                 ast_cli(fd, "Unable to find client '%s'!\n", name);
1970                 return RESULT_FAILURE;
1971         }
1972
1973         /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */
1974         ast_aji_send(client, "mogorman@astjab.org", "blahblah");
1975         ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1976                 ASTOBJ_RDLOCK(iterator);
1977                 ast_verbose("User: %s\n", iterator->name);
1978                 for (resource = iterator->resources; resource; resource = resource->next) {
1979                         ast_verbose("Resource: %s\n", resource->resource);
1980                         if(resource->cap) {
1981                                 ast_verbose("   client: %s\n", resource->cap->parent->node);
1982                                 ast_verbose("   version: %s\n", resource->cap->version);
1983                                 ast_verbose("   Jingle Capable: %d\n", resource->cap->jingle);
1984                         }
1985                         ast_verbose("   Priority: %d\n", resource->priority);
1986                         ast_verbose("   Status: %d\n", resource->status); 
1987                         ast_verbose("   Message: %s\n", S_OR(resource->description,"")); 
1988                 }
1989                 ASTOBJ_UNLOCK(iterator);
1990         });
1991         ast_verbose("\nOooh a working message stack!\n");
1992         AST_LIST_LOCK(&client->messages);
1993         AST_LIST_TRAVERSE(&client->messages, tmp, list) {
1994                 ast_verbose("   Message from: %s with id %s @ %s        %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, ""));
1995         }
1996         AST_LIST_UNLOCK(&client->messages);
1997         ASTOBJ_UNREF(client, aji_client_destroy);
1998
1999         return RESULT_SUCCESS;
2000 }
2001
2002 /*!
2003  * \brief creates aji_client structure.
2004  * \param label, ast_variable, debug, pruneregister, component/client, aji_client to dump into. 
2005  * \return 0.
2006  */
2007 static int aji_create_client(char *label, struct ast_variable *var, int debug)
2008 {
2009         char *resource;
2010         struct aji_client *client = NULL;
2011         int flag = 0;
2012
2013         client = ASTOBJ_CONTAINER_FIND(&clients,label);
2014         if (!client) {
2015                 flag = 1;
2016                 client = (struct aji_client *) malloc(sizeof(struct aji_client));
2017                 if (!client) {
2018                         ast_log(LOG_ERROR, "Out of memory!\n");
2019                         return 0;
2020                 }
2021                 memset(client, 0, sizeof(struct aji_client));
2022                 ASTOBJ_INIT(client);
2023                 ASTOBJ_WRLOCK(client);
2024                 ASTOBJ_CONTAINER_INIT(&client->buddies);
2025         } else {
2026                 ASTOBJ_WRLOCK(client);
2027                 ASTOBJ_UNMARK(client);
2028         }
2029         ASTOBJ_CONTAINER_MARKALL(&client->buddies);
2030         ast_copy_string(client->name, label, sizeof(client->name));
2031         ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
2032
2033         client->debug = debug;
2034         ast_copy_flags(client, &globalflags, AST_FLAGS_ALL);
2035         client->port = 5222;
2036         client->usetls = 1;
2037         client->usesasl = 1;
2038         client->forcessl = 0;
2039         client->keepalive = 1;
2040         client->timeout = 50;
2041         client->message_timeout = 100;
2042         AST_LIST_HEAD_INIT(&client->messages);
2043         client->component = 0;
2044         ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage));
2045
2046         if (flag) {
2047                 client->authorized = 0;
2048                 client->state = AJI_DISCONNECTED;
2049         }
2050         while (var) {
2051                 if (!strcasecmp(var->name, "username"))
2052                         ast_copy_string(client->user, var->value, sizeof(client->user));
2053                 else if (!strcasecmp(var->name, "serverhost"))
2054                         ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
2055                 else if (!strcasecmp(var->name, "secret"))
2056                         ast_copy_string(client->password, var->value, sizeof(client->password));
2057                 else if (!strcasecmp(var->name, "statusmessage"))
2058                         ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage));
2059                 else if (!strcasecmp(var->name, "port"))
2060                         client->port = atoi(var->value);
2061                 else if (!strcasecmp(var->name, "timeout"))
2062                         client->message_timeout = atoi(var->value);
2063                 else if (!strcasecmp(var->name, "debug"))
2064                         client->debug = (ast_false(var->value)) ? 0 : 1;
2065                 else if (!strcasecmp(var->name, "type")) {
2066                         if (!strcasecmp(var->value, "component"))
2067                                 client->component = 1;
2068                 } else if (!strcasecmp(var->name, "usetls")) {
2069                         client->usetls = (ast_false(var->value)) ? 0 : 1;
2070                 } else if (!strcasecmp(var->name, "usesasl")) {
2071                         client->usesasl = (ast_false(var->value)) ? 0 : 1;
2072                 } else if (!strcasecmp(var->name, "forceoldssl"))
2073                         client->forcessl = (ast_false(var->value)) ? 0 : 1;
2074                 else if (!strcasecmp(var->name, "keepalive"))
2075                         client->keepalive = (ast_false(var->value)) ? 0 : 1;
2076                 else if (!strcasecmp(var->name, "autoprune"))
2077                         ast_set2_flag(client, ast_true(var->value), AJI_AUTOPRUNE);
2078                 else if (!strcasecmp(var->name, "autoregister"))
2079                         ast_set2_flag(client, ast_true(var->value), AJI_AUTOREGISTER);
2080                 else if (!strcasecmp(var->name, "buddy"))
2081                                 aji_create_buddy(var->value, client);
2082                 else if (!strcasecmp(var->name, "transport"))
2083                                 aji_create_transport(var->value, client);
2084                 var = var->next;
2085         }
2086         if (!flag) {
2087                 ASTOBJ_UNLOCK(client);
2088                 ASTOBJ_UNREF(client, aji_client_destroy);
2089                 return 1;
2090         }
2091         client->p = iks_stream_new(((client->component) ? "jabber:component:accept" : "jabber:client"), client, aji_act_hook);
2092         if (!client->p) {
2093                 ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name);
2094                 return 0;
2095         }
2096         client->stack = iks_stack_new(8192, 8192);
2097         if (!client->stack) {
2098                 ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name);
2099                 return 0;
2100         }
2101         client->f = iks_filter_new();
2102         if (!client->f) {
2103                 ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name);
2104                 return 0;
2105         }
2106         if (!strchr(client->user, '/') && !client->component) { /*client */
2107                 resource = NULL;
2108                 asprintf(&resource, "%s/asterisk", client->user);
2109                 if (resource) {
2110                         client->jid = iks_id_new(client->stack, resource);
2111                         free(resource);
2112                 }
2113         } else
2114                 client->jid = iks_id_new(client->stack, client->user);
2115         if (client->component) {
2116                 iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2117                 iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
2118                 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);
2119                 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);
2120         } else {
2121                 iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2122         }
2123         if (!strchr(client->user, '/') && !client->component) { /*client */
2124                 resource = NULL;
2125                 asprintf(&resource, "%s/asterisk", client->user);
2126                 if (resource) {
2127                         client->jid = iks_id_new(client->stack, resource);
2128                         free(resource);
2129                 }
2130         } else
2131                 client->jid = iks_id_new(client->stack, client->user);
2132         iks_set_log_hook(client->p, aji_log_hook);
2133         ASTOBJ_UNLOCK(client);
2134         ASTOBJ_CONTAINER_LINK(&clients,client);
2135         return 1;
2136 }
2137
2138 /*!
2139  * \brief creates transport.
2140  * \param label, buddy to dump it into. 
2141  * \return 0.
2142  */
2143 static int aji_create_transport(char *label, struct aji_client *client)
2144 {
2145         char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
2146         struct aji_buddy *buddy = NULL;
2147
2148         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2149         if (!buddy) {
2150                 buddy = malloc(sizeof(struct aji_buddy));
2151                 if(!buddy) {
2152                         ast_log(LOG_WARNING, "Out of memory\n");
2153                         return 0;
2154                 }
2155                 memset(buddy, 0, sizeof(struct aji_buddy));
2156                 ASTOBJ_INIT(buddy);
2157         }
2158         ASTOBJ_WRLOCK(buddy);
2159         server = label;
2160         if ((buddyname = strchr(label, ','))) {
2161                 *buddyname = '\0';
2162                 buddyname++;
2163                 if (buddyname && buddyname[0] != '\0') {
2164                         if ((user = strchr(buddyname, ','))) {
2165                                 *user = '\0';
2166                                 user++;
2167                                 if (user && user[0] != '\0') {
2168                                         if ((pass = strchr(user, ','))) {
2169                                                 *pass = '\0';
2170                                                 pass++;
2171                                                 ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
2172                                                 ast_copy_string(buddy->user, user, sizeof(buddy->user));
2173                                                 ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
2174                                                 ast_copy_string(buddy->server, server, sizeof(buddy->server));
2175                                                 return 1;
2176                                         }
2177                                 }
2178                         }
2179                 }
2180         }
2181         ASTOBJ_UNLOCK(buddy);
2182         ASTOBJ_UNMARK(buddy);
2183         ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2184         return 0;
2185 }
2186
2187 /*!
2188  * \brief creates buddy.
2189  * \param label, buddy to dump it into. 
2190  * \return 0.
2191  */
2192 static int aji_create_buddy(char *label, struct aji_client *client)
2193 {
2194         struct aji_buddy *buddy = NULL;
2195         int flag = 0;
2196         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2197         if (!buddy) {
2198                 flag = 1;
2199                 buddy = malloc(sizeof(struct aji_buddy));
2200                 if(!buddy) {
2201                         ast_log(LOG_WARNING, "Out of memory\n");
2202                         return 0;
2203                 }
2204                 memset(buddy, 0, sizeof(struct aji_buddy));
2205                 ASTOBJ_INIT(buddy);
2206         }
2207         ASTOBJ_WRLOCK(buddy);
2208         ast_copy_string(buddy->name, label, sizeof(buddy->name));
2209         ASTOBJ_UNLOCK(buddy);
2210         if(flag)
2211                 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2212         else {
2213                 ASTOBJ_UNMARK(buddy);
2214                 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2215         }
2216         return 1;
2217 }
2218
2219 /*!
2220  * \brief load config file.
2221  * \param void. 
2222  * \return 1.
2223  */
2224 static int aji_load_config(void)
2225 {
2226         char *cat = NULL;
2227         int debug = 1;
2228         struct ast_config *cfg = NULL;
2229         struct ast_variable *var = NULL;
2230
2231         cfg = ast_config_load(JABBER_CONFIG);
2232         if (!cfg) {
2233                 ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
2234                 return 0;
2235         }
2236
2237         cat = ast_category_browse(cfg, NULL);
2238         for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
2239                 if (!strcasecmp(var->name, "debug"))
2240                         debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
2241                 else if (!strcasecmp(var->name, "autoprune"))
2242                         ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
2243                 else if (!strcasecmp(var->name, "autoregister"))
2244                         ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
2245         }
2246
2247         while (cat) {
2248                 if (strcasecmp(cat, "general")) {
2249                                 var = ast_variable_browse(cfg, cat);
2250                                 aji_create_client(cat, var, debug);
2251                 }
2252                 cat = ast_category_browse(cfg, cat);
2253         }
2254         return 1;
2255 }
2256
2257 /*!
2258  * \brief grab a aji_client structure by label name.
2259  * \param void. 
2260  * \return 1.
2261  */
2262 struct aji_client *ast_aji_get_client(char *name)
2263 {
2264         struct aji_client *client = NULL;
2265
2266         client = ASTOBJ_CONTAINER_FIND(&clients, name);
2267         if (!client && !strchr(name, '@'))
2268                 client = ASTOBJ_CONTAINER_FIND_FULL(&clients, name, user,,, strcasecmp);
2269         return client;
2270 }
2271
2272 struct aji_client_container *ast_aji_get_clients(void)
2273 {
2274         return &clients;
2275 }
2276
2277 static char mandescr_jabber_send[] =
2278 "Description: Sends a message to a Jabber Client.\n"
2279 "Variables: \n"
2280 "  Jabber:      Client or transport Asterisk uses to connect to JABBER.\n"
2281 "  ScreenName:  User Name to message.\n"
2282 "  Message:     Message to be sent to the buddy\n";
2283
2284 /*! \brief  Send a Jabber Message via call from the Manager */
2285 static int manager_jabber_send( struct mansession *s, struct message *m )
2286 {
2287         struct aji_client *client = NULL;
2288         char *id = astman_get_header(m,"ActionID");
2289         char *jabber = astman_get_header(m,"Jabber");
2290         char *screenname = astman_get_header(m,"ScreenName");
2291         char *message = astman_get_header(m,"Message");
2292
2293         if (ast_strlen_zero(jabber)) {
2294                 astman_send_error(s, m, "No transport specified");
2295                 return 0;
2296         }
2297         if (ast_strlen_zero(screenname)) {
2298                 astman_send_error(s, m, "No ScreenName specified");
2299                 return 0;
2300         }
2301         if (ast_strlen_zero(message)) {
2302                 astman_send_error(s, m, "No Message specified");
2303                 return 0;
2304         }
2305
2306         astman_send_ack(s, m, "Attempting to send Jabber Message");
2307         client = ast_aji_get_client(jabber);
2308         if (!client) {
2309                 astman_send_error(s, m, "Could not find Sender");
2310                 return 0;
2311         }       
2312         if (strchr(screenname, '@') && message){
2313                 ast_aji_send(client, screenname, message);      
2314                 if (!ast_strlen_zero(id))
2315                         astman_append(s, "ActionID: %s\r\n",id);
2316                 astman_append(s, "Response: Success\r\n");
2317                 return 0;
2318         }
2319         if (!ast_strlen_zero(id))
2320                 astman_append(s, "ActionID: %s\r\n",id);
2321         astman_append(s, "Response: Failure\r\n");
2322         return 0;
2323 }
2324
2325
2326 static int aji_reload()
2327 {
2328         ASTOBJ_CONTAINER_MARKALL(&clients);
2329         if (!aji_load_config()) {
2330                 ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
2331                 return 0;
2332         }
2333         ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, aji_client_destroy);
2334         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2335                 ASTOBJ_RDLOCK(iterator);
2336                 if(iterator->state == AJI_DISCONNECTED) {
2337                         if (!iterator->thread)
2338                                 ast_pthread_create(&iterator->thread, NULL, aji_recv_loop, iterator);
2339                 } else if (iterator->state == AJI_CONNECTING)
2340                         aji_get_roster(iterator);
2341                 ASTOBJ_UNLOCK(iterator);
2342         });
2343         
2344         return 1;
2345 }
2346
2347 static int unload_module(void)
2348 {
2349         ast_cli_unregister_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry));
2350         ast_unregister_application(app_ajisend);
2351         ast_manager_unregister("JabberSend");
2352         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2353                 ASTOBJ_RDLOCK(iterator);
2354                 if (option_verbose > 2)
2355                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: %s\n", iterator->name);
2356                 iterator->state = AJI_DISCONNECTED;
2357                 ast_aji_disconnect(iterator);
2358                 pthread_join(iterator->thread, NULL);
2359                 ASTOBJ_UNLOCK(iterator);
2360         });
2361
2362         ASTOBJ_CONTAINER_DESTROYALL(&clients, aji_client_destroy);
2363         ASTOBJ_CONTAINER_DESTROY(&clients);
2364
2365         ast_log(LOG_NOTICE, "res_jabber unloaded.\n");
2366         return 0;
2367 }
2368
2369 static int load_module(void)
2370 {
2371         ASTOBJ_CONTAINER_INIT(&clients);
2372         if(!aji_reload())
2373                 return AST_MODULE_LOAD_DECLINE;
2374         ast_manager_register2("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send,
2375                         "Sends a message to a Jabber Client", mandescr_jabber_send);
2376         ast_register_application(app_ajisend, aji_send_exec, ajisend_synopsis, ajisend_descrip);
2377         ast_register_application(app_ajistatus, aji_status_exec, ajistatus_synopsis, ajistatus_descrip);
2378         ast_cli_register_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry));
2379
2380         ast_log(LOG_NOTICE, "res_jabber.so loaded.\n");
2381         return 0;
2382 }
2383
2384 static int reload(void)
2385 {
2386         aji_reload();
2387         return 0;
2388 }
2389
2390 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "AJI - Asterisk Jabber Interface",
2391                 .load = load_module,
2392                 .unload = unload_module,
2393                 .reload = reload,
2394                );