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