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