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