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