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