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