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