8ab55faa601c6fcaa2e9f2950a3517bff4d1cb9c
[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                                 break;
599                         }
600                         break;
601
602                 case IKS_NODE_NORMAL:
603                         break;
604
605                 case IKS_NODE_ERROR:
606                         ast_log(LOG_ERROR, "JABBER: Node Error\n");
607                         ASTOBJ_UNREF(client, aji_client_destroy);
608                         return IKS_HOOK;
609
610                 case IKS_NODE_STOP:
611                         ast_log(LOG_WARNING, "JABBER: Disconnected\n");
612                         ASTOBJ_UNREF(client, aji_client_destroy);
613                         return IKS_HOOK;
614                 }
615         }
616
617         switch (pak->type) {
618         case IKS_PAK_NONE:
619                 if (option_verbose > 4)
620                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Don't know what to do with you NONE\n");
621                 break;
622         case IKS_PAK_MESSAGE:
623                 aji_handle_message(client, pak);
624                 if (option_verbose > 4)
625                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Don't know what to do with you MESSAGE\n");
626                 break;
627         case IKS_PAK_PRESENCE:
628                 aji_handle_presence(client, pak);
629                 if (option_verbose > 4)
630                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Do know how to handle presence!!\n");
631                 break;
632         case IKS_PAK_S10N:
633                 aji_handle_subscribe(client, pak);
634                 if (option_verbose > 4)
635                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont know S10N subscribe!!\n");
636                 break;
637         case IKS_PAK_IQ:
638                 if (option_verbose > 4)
639                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont have an IQ!!!\n");
640                 aji_handle_iq(client, node);
641                 break;
642         default:
643                 if (option_verbose > 4)
644                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont know %i\n", pak->type);
645                 break;
646         }
647         
648         iks_filter_packet(client->f, pak);
649
650         if (node)
651                 iks_delete(node);
652
653         ASTOBJ_UNREF(client, aji_client_destroy);
654         return IKS_OK;
655 }
656
657 static int aji_register_approve_handler(void *data, ikspak *pak)
658 {
659         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
660         iks *iq = NULL, *query = NULL, *item = NULL;
661
662         iq = iks_new("iq");
663         query = iks_new("query");
664         item = iks_new("item");
665
666         if (client && iq && query && item) {
667                 if (!iks_find(pak->query, "remove")) {
668                         iks_insert_attrib(iq, "from", client->jid->full);
669                         iks_insert_attrib(iq, "to", pak->from->full);
670                         iks_insert_attrib(iq, "id", pak->id);
671                         iks_insert_attrib(iq, "type", "result");
672                         iks_insert_attrib(query, "xmlns", "jabber:iq:register");
673
674                         iks_insert_node(iq, query);
675                         iks_send(client->p, iq);
676                         iks_insert_attrib(iq, "from", pak->from->full);
677                         iks_insert_attrib(iq, "to", pak->from->full);
678                         iks_insert_attrib(iq, "id", client->mid);
679                         ast_aji_increment_mid(client->mid);
680                         iks_insert_attrib(iq, "type", "set");
681                         iks_insert_attrib(query, "xmlns", "jabber:iq:roster");
682                         iks_insert_attrib(item, "subscription", "none");
683                         iks_insert_attrib(item, "jid", client->jid->full);
684                         iks_insert_node(query, item);
685                         iks_send(client->p, iq);
686                 }
687         } else {
688                 ast_log(LOG_ERROR, "Out of memory.\n");
689         }
690         if (iq)
691                 iks_delete(iq);
692         if (query)
693                 iks_delete(query);
694         if (item)
695                 iks_delete(item);
696         ASTOBJ_UNREF(client, aji_client_destroy);
697         return IKS_FILTER_EAT;
698 }
699
700 static int aji_register_query_handler(void *data, ikspak *pak)
701 {
702         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
703         char *node = NULL;
704
705         client = (struct aji_client *) data;
706
707         if (!(node = iks_find_attrib(pak->query, "node"))) {
708                 iks *iq = NULL, *query = NULL, *instructions = NULL;
709                 char *explain = "Welcome to Asterisk - the Open Source PBX.\n";
710                 iq = iks_new("iq");
711                 query = iks_new("query");
712                 instructions = iks_new("instructions");
713                 if (iq && query && instructions && client) {
714                         iks_insert_attrib(iq, "from", client->user);
715                         iks_insert_attrib(iq, "to", pak->from->full);
716                         iks_insert_attrib(iq, "id", pak->id);
717                         iks_insert_attrib(iq, "type", "result");
718                         iks_insert_attrib(query, "xmlns", "jabber:iq:register");
719                         iks_insert_cdata(instructions, explain, 0);
720                         iks_insert_node(iq, query);
721                         iks_insert_node(query, instructions);
722                         iks_send(client->p, iq);
723                 } else {
724                         ast_log(LOG_ERROR, "Out of memory.\n");
725                 }
726                 if (iq)
727                         iks_delete(iq);
728                 if (query)
729                         iks_delete(query);
730                 if (instructions)
731                         iks_delete(instructions);
732         }
733         ASTOBJ_UNREF(client, aji_client_destroy);
734         return IKS_FILTER_EAT;
735 }
736
737 static int aji_ditems_handler(void *data, ikspak *pak)
738 {
739         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
740         char *node = NULL;
741
742         if (!(node = iks_find_attrib(pak->query, "node"))) {
743                 iks *iq = NULL, *query = NULL, *item = NULL;
744                 iq = iks_new("iq");
745                 query = iks_new("query");
746                 item = iks_new("item");
747
748                 if (iq && query && item) {
749                         iks_insert_attrib(iq, "from", client->user);
750                         iks_insert_attrib(iq, "to", pak->from->full);
751                         iks_insert_attrib(iq, "id", pak->id);
752                         iks_insert_attrib(iq, "type", "result");
753                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
754                         iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
755                         iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
756                         iks_insert_attrib(item, "jid", client->user);
757
758                         iks_insert_node(iq, query);
759                         iks_insert_node(query, item);
760                         iks_send(client->p, iq);
761                 } else {
762                         ast_log(LOG_ERROR, "Out of memory.\n");
763                 }
764                 if (iq)
765                         iks_delete(iq);
766                 if (query)
767                         iks_delete(query);
768                 if (item)
769                         iks_delete(item);
770
771         } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
772                 iks *iq, *query, *confirm;
773                 iq = iks_new("iq");
774                 query = iks_new("query");
775                 confirm = iks_new("item");
776                 if (iq && query && confirm && client) {
777                         iks_insert_attrib(iq, "from", client->user);
778                         iks_insert_attrib(iq, "to", pak->from->full);
779                         iks_insert_attrib(iq, "id", pak->id);
780                         iks_insert_attrib(iq, "type", "result");
781                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
782                         iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
783                         iks_insert_attrib(confirm, "node", "confirmaccount");
784                         iks_insert_attrib(confirm, "name", "Confirm AIM account");
785                         iks_insert_attrib(confirm, "jid", "blog.astjab.org");
786
787                         iks_insert_node(iq, query);
788                         iks_insert_node(query, confirm);
789                         iks_send(client->p, iq);
790                 } else {
791                         ast_log(LOG_ERROR, "Out of memory.\n");
792                 }
793                 if (iq)
794                         iks_delete(iq);
795                 if (query)
796                         iks_delete(query);
797                 if (confirm)
798                         iks_delete(confirm);
799
800         } else if (!strcasecmp(node, "confirmaccount")) {
801                 iks *iq = NULL, *query = NULL, *feature = NULL;
802
803                 iq = iks_new("iq");
804                 query = iks_new("query");
805                 feature = iks_new("feature");
806
807                 if (iq && query && feature && client) {
808                         iks_insert_attrib(iq, "from", client->user);
809                         iks_insert_attrib(iq, "to", pak->from->full);
810                         iks_insert_attrib(iq, "id", pak->id);
811                         iks_insert_attrib(iq, "type", "result");
812                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
813                         iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
814                         iks_insert_node(iq, query);
815                         iks_insert_node(query, feature);
816                         iks_send(client->p, iq);
817                 } else {
818                         ast_log(LOG_ERROR, "Out of memory.\n");
819                 }
820                 if (iq)
821                         iks_delete(iq);
822                 if (query)
823                         iks_delete(query);
824                 if (feature)
825                         iks_delete(feature);
826         }
827
828         ASTOBJ_UNREF(client, aji_client_destroy);
829         return IKS_FILTER_EAT;
830
831 }
832
833 static int aji_client_info_handler(void *data, ikspak *pak)
834 {
835         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
836         struct aji_resource *resource = NULL;
837         struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
838
839         resource = aji_find_resource(buddy, pak->from->resource);
840
841         if (pak->subtype == IKS_TYPE_RESULT) {
842                 if (!resource) {
843                         ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full);
844                         ASTOBJ_UNREF(client, aji_client_destroy);
845                         return IKS_FILTER_EAT;
846                 }
847                 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
848                         resource->cap->jingle = 1;
849                 } else
850                         resource->cap->jingle = 0;
851         } else if (pak->subtype == IKS_TYPE_GET) {
852                 iks *iq, *disco, *ident, *google, *query;
853                 iq = iks_new("iq");
854                 query = iks_new("query");
855                 ident = iks_new("identity");
856                 disco = iks_new("feature");
857                 google = iks_new("feature");
858                 if (iq && ident && disco && google) {
859                         iks_insert_attrib(iq, "from", client->jid->full);
860                         iks_insert_attrib(iq, "to", pak->from->full);
861                         iks_insert_attrib(iq, "type", "result");
862                         iks_insert_attrib(iq, "id", pak->id);
863                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
864                         iks_insert_attrib(ident, "category", "client");
865                         iks_insert_attrib(ident, "type", "pc");
866                         iks_insert_attrib(ident, "name", "asterisk");
867                         iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
868                         iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
869                         iks_insert_node(iq, query);
870                         iks_insert_node(query, ident);
871                         iks_insert_node(query, google);
872                         iks_insert_node(query, disco);
873                         iks_send(client->p, iq);
874                 } else
875                         ast_log(LOG_ERROR, "Out of Memory.\n");
876                 if (iq)
877                         iks_delete(iq);
878                 if (query)
879                         iks_delete(query);
880                 if (ident)
881                         iks_delete(ident);
882                 if (google)
883                         iks_delete(google);
884                 if (disco)
885                         iks_delete(disco);
886         } else if (pak->subtype == IKS_TYPE_ERROR) {
887                 ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
888         }
889         ASTOBJ_UNREF(client, aji_client_destroy);
890         return IKS_FILTER_EAT;
891 }
892
893 static int aji_dinfo_handler(void *data, ikspak *pak)
894 {
895         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
896         char *node = NULL;
897         if (!strcasecmp(iks_find_attrib(pak->x,"type"),"error")) {
898                 ast_log(LOG_WARNING, "Recieved error from a client, turn on jabber debug!\n");
899                 return IKS_FILTER_EAT;
900         }
901         if (!(node = iks_find_attrib(pak->query, "node"))) {
902                 iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search;
903
904                 iq = iks_new("iq");
905                 query = iks_new("query");
906                 identity = iks_new("identity");
907                 disco = iks_new("feature");
908                 reg = iks_new("feature");
909                 commands = iks_new("feature");
910                 gateway = iks_new("feature");
911                 version = iks_new("feature");
912                 vcard = iks_new("feature");
913                 search = iks_new("feature");
914
915                 if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
916                         iks_insert_attrib(iq, "from", client->user);
917                         iks_insert_attrib(iq, "to", pak->from->full);
918                         iks_insert_attrib(iq, "id", pak->id);
919                         iks_insert_attrib(iq, "type", "result");
920                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
921                         iks_insert_attrib(identity, "category", "gateway");
922                         iks_insert_attrib(identity, "type", "pstn");
923                         iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
924                         iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
925                         iks_insert_attrib(reg, "var", "jabber:iq:register");
926                         iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
927                         iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
928                         iks_insert_attrib(version, "var", "jabber:iq:version");
929                         iks_insert_attrib(vcard, "var", "vcard-temp");
930                         iks_insert_attrib(search, "var", "jabber:iq:search");
931
932                         iks_insert_node(iq, query);
933                         iks_insert_node(query, identity);
934                         iks_insert_node(query, disco);
935                         iks_insert_node(query, reg);
936                         iks_insert_node(query, commands);
937                         iks_insert_node(query, gateway);
938                         iks_insert_node(query, version);
939                         iks_insert_node(query, vcard);
940                         iks_insert_node(query, search);
941                         iks_send(client->p, iq);
942                 } else {
943                         ast_log(LOG_ERROR, "Out of memory.\n");
944                 }
945
946                 if (iq)
947                         iks_delete(iq);
948                 if (query)
949                         iks_delete(query);
950                 if (identity)
951                         iks_delete(identity);
952                 if (disco)
953                         iks_delete(disco);
954                 if (reg)
955                         iks_delete(reg);
956                 if (commands)
957                         iks_delete(commands);
958                 if (gateway)
959                         iks_delete(gateway);
960                 if (version)
961                         iks_delete(version);
962                 if (vcard)
963                         iks_delete(vcard);
964                 if (search)
965                         iks_delete(search);
966
967         } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
968                 iks *iq, *query, *confirm;
969                 iq = iks_new("iq");
970                 query = iks_new("query");
971                 confirm = iks_new("item");
972
973                 if (iq && query && confirm && client) {
974                         iks_insert_attrib(iq, "from", client->user);
975                         iks_insert_attrib(iq, "to", pak->from->full);
976                         iks_insert_attrib(iq, "id", pak->id);
977                         iks_insert_attrib(iq, "type", "result");
978                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
979                         iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
980                         iks_insert_attrib(confirm, "node", "confirmaccount");
981                         iks_insert_attrib(confirm, "name", "Confirm AIM account");
982                         iks_insert_attrib(confirm, "jid", client->user);
983                         iks_insert_node(iq, query);
984                         iks_insert_node(query, confirm);
985                         iks_send(client->p, iq);
986                 } else {
987                         ast_log(LOG_ERROR, "Out of memory.\n");
988                 }
989                 if (iq)
990                         iks_delete(iq);
991                 if (query)
992                         iks_delete(query);
993                 if (confirm)
994                         iks_delete(confirm);
995
996         } else if (!strcasecmp(node, "confirmaccount")) {
997                 iks *iq, *query, *feature;
998
999                 iq = iks_new("iq");
1000                 query = iks_new("query");
1001                 feature = iks_new("feature");
1002
1003                 if (iq && query && feature && client) {
1004                         iks_insert_attrib(iq, "from", client->user);
1005                         iks_insert_attrib(iq, "to", pak->from->full);
1006                         iks_insert_attrib(iq, "id", pak->id);
1007                         iks_insert_attrib(iq, "type", "result");
1008                         iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1009                         iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
1010                         iks_insert_node(iq, query);
1011                         iks_insert_node(query, feature);
1012                         iks_send(client->p, iq);
1013                 } else {
1014                         ast_log(LOG_ERROR, "Out of memory.\n");
1015                 }
1016                 if (iq)
1017                         iks_delete(iq);
1018                 if (query)
1019                         iks_delete(query);
1020                 if (feature)
1021                         iks_delete(feature);
1022         }
1023
1024         ASTOBJ_UNREF(client, aji_client_destroy);
1025         return IKS_FILTER_EAT;
1026 }
1027
1028 /*!
1029  * \brief Handles <iq> tags.
1030  * \param client structure and the iq node.
1031  * \return void.
1032  */
1033 static void aji_handle_iq(struct aji_client *client, iks *node)
1034 {
1035         /*Nothing to see here */
1036 }
1037
1038 /*!
1039  * \brief Handles presence packets.
1040  * \param client structure and the node.
1041  * \return void.
1042  */
1043 static void aji_handle_message(struct aji_client *client, ikspak *pak)
1044 {
1045         struct aji_message *insert, *tmp;
1046         int flag = 0;
1047         
1048         if (!(insert = ast_calloc(1, sizeof(struct aji_message))))
1049                 return;
1050         time(&insert->arrived);
1051         if (iks_find_cdata(pak->x, "body"))
1052                 insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
1053         if(pak->id)
1054                 ast_copy_string(insert->id, pak->id, sizeof(insert->message));
1055         insert->from = ast_strdup(pak->from->full);
1056         AST_LIST_LOCK(&client->messages);
1057         AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
1058                 if (flag) {
1059                         AST_LIST_REMOVE_CURRENT(&client->messages, list);
1060                         if (tmp->from)
1061                                 free(tmp->from);
1062                         if (tmp->message)
1063                                 free(tmp->message);
1064                 } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) {
1065                         flag = 1;
1066                         AST_LIST_REMOVE_CURRENT(&client->messages, list);
1067                         if (tmp->from)
1068                                 free(tmp->from);
1069                         if (tmp->message)
1070                                 free(tmp->message);
1071                 }
1072         }
1073         AST_LIST_TRAVERSE_SAFE_END;
1074         AST_LIST_INSERT_HEAD(&client->messages, insert, list);
1075         AST_LIST_UNLOCK(&client->messages);
1076 }
1077
1078 static void aji_handle_presence(struct aji_client *client, ikspak *pak)
1079 {
1080         int status, priority;
1081         struct aji_buddy *buddy;
1082         struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
1083         char *ver, *node, *descrip;
1084         
1085         if(client->state != AJI_CONNECTED)
1086                 aji_create_buddy(pak->from->partial, client);
1087
1088         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
1089         if (!buddy) {
1090                 ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial);
1091                 return;
1092         }
1093         ASTOBJ_WRLOCK(buddy);
1094         status = (pak->show) ? pak->show : 6;
1095         priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
1096         tmp = buddy->resources;
1097         descrip = ast_strdup(iks_find_cdata(pak->x,"status"));
1098
1099         while (tmp) {
1100                 if (!strcasecmp(tmp->resource, pak->from->resource)) {
1101                         tmp->status = status;
1102                         if (tmp->description) free(tmp->description);
1103                         tmp->description = descrip;
1104                         found = tmp;
1105                         if (status == 6) {      /* Sign off Destroy resource */
1106                                 if (last && found->next) {
1107                                         last->next = found->next;
1108                                 } else if (!last) {
1109                                         if (found->next)
1110                                                 buddy->resources = found->next;
1111                                         else
1112                                                 buddy->resources = NULL;
1113                                 } else if (!found->next) {
1114                                         if (last)
1115                                                 last->next = NULL;
1116                                         else
1117                                                 buddy->resources = NULL;
1118                                 }
1119                                 free(found);
1120                                 found = NULL;
1121                                 break;
1122                         }
1123                         if (tmp->priority != priority) {
1124                                 found->priority = priority;
1125                                 if (!last && !found->next)
1126                                         break;
1127                                 if (last)
1128                                         last->next = found->next;
1129                                 else
1130                                         buddy->resources = found->next;
1131                                 last = NULL;
1132                                 tmp = buddy->resources;
1133                                 if (!buddy->resources)
1134                                         buddy->resources = found;
1135                                 while (tmp) {
1136                                         if (found->priority > tmp->priority) {
1137                                                 if (last)
1138                                                         last->next = found;
1139                                                 found->next = tmp;
1140                                                 if (!last)
1141                                                         buddy->resources = found;
1142                                                 break;
1143                                         }
1144                                         if (!tmp->next) {
1145                                                 tmp->next = found;
1146                                                 break;
1147                                         }
1148                                         last = tmp;
1149                                         tmp = tmp->next;
1150                                 }
1151                         }
1152                         break;
1153                 }
1154                 last = tmp;
1155                 tmp = tmp->next;
1156         }
1157
1158         if (!found && status != 6) {
1159                 found = (struct aji_resource *) malloc(sizeof(struct aji_resource));
1160                 memset(found, 0, sizeof(struct aji_resource));
1161
1162                 if (!found) {
1163                         ast_log(LOG_ERROR, "Out of memory!\n");
1164                         return;
1165                 }
1166                 ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
1167                 found->status = status;
1168                 found->description = descrip;
1169                 found->priority = priority;
1170                 found->next = NULL;
1171                 last = NULL;
1172                 tmp = buddy->resources;
1173                 while (tmp) {
1174                         if (found->priority > tmp->priority) {
1175                                 if (last)
1176                                         last->next = found;
1177                                 found->next = tmp;
1178                                 if (!last)
1179                                         buddy->resources = found;
1180                                 break;
1181                         }
1182                         if (!tmp->next) {
1183                                 tmp->next = found;
1184                                 break;
1185                         }
1186                         last = tmp;
1187                         tmp = tmp->next;
1188                 }
1189                 if (!tmp)
1190                         buddy->resources = found;
1191         }
1192         ASTOBJ_UNLOCK(buddy);
1193         ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1194
1195         node = iks_find_attrib(iks_find(pak->x, "c"), "node");
1196         ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
1197
1198         if(status !=6 && !found->cap) {
1199                 found->cap = aji_find_version(node, ver, pak);
1200                 if(gtalk_yuck(pak->x)) /* gtalk should do discover */
1201                         found->cap->jingle = 1;
1202                 if(found->cap->jingle && option_debug > 4)
1203                         ast_log(LOG_DEBUG,"Special case for google till they support discover.\n");
1204                 else {
1205                         iks *iq, *query;
1206                         iq = iks_new("iq");
1207                         query = iks_new("query");
1208                         if(query && iq)  {
1209                                 iks_insert_attrib(iq, "type", "get");
1210                                 iks_insert_attrib(iq, "to", pak->from->full);
1211                                 iks_insert_attrib(iq,"from",iks_find_attrib(pak->x,"to"));
1212                                 iks_insert_attrib(iq, "id", client->mid);
1213                                 ast_aji_increment_mid(client->mid);
1214                                 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
1215                                 iks_insert_node(iq, query);
1216                                 iks_send(client->p, iq);
1217                                 
1218                         } else
1219                                 ast_log(LOG_ERROR, "Out of memory.\n");
1220                         if(query)
1221                                 iks_delete(query);
1222                         if(iq)
1223                                 iks_delete(iq);
1224                 }
1225         }
1226         if (option_verbose > 4) {
1227                 switch (pak->subtype) {
1228                 case IKS_TYPE_AVAILABLE:
1229                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am available ^_* %i\n", pak->subtype);
1230                         break;
1231                 case IKS_TYPE_UNAVAILABLE:
1232                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am unavailable ^_* %i\n", pak->subtype);
1233                         break;
1234                 default:
1235                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype);
1236                 }
1237                 switch (pak->show) {
1238                 case IKS_SHOW_UNAVAILABLE:
1239                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1240                         break;
1241                 case IKS_SHOW_AVAILABLE:
1242                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is available\n");
1243                         break;
1244                 case IKS_SHOW_CHAT:
1245                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1246                         break;
1247                 case IKS_SHOW_AWAY:
1248                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is away\n");
1249                         break;
1250                 case IKS_SHOW_XA:
1251                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1252                         break;
1253                 case IKS_SHOW_DND:
1254                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
1255                         break;
1256                 default:
1257                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: Kinky! how did that happen %i\n", pak->show);
1258                 }
1259         }
1260 }
1261
1262 /*!
1263  * \brief handles subscription requests.
1264  * \param aji_client struct and xml packet.
1265  * \return void.
1266  */
1267 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
1268 {
1269         if(pak->subtype == IKS_TYPE_SUBSCRIBE)
1270                 iks_send(client->p, iks_make_s10n(IKS_TYPE_SUBSCRIBED, iks_find_attrib(pak->x, "from"), "Asterisk has approved subscription"));
1271         if (option_verbose > 4) {
1272                 switch (pak->subtype) {
1273                 case IKS_TYPE_SUBSCRIBE:
1274                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1275                         break;
1276                 case IKS_TYPE_SUBSCRIBED:
1277                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1278                         break;
1279                 case IKS_TYPE_UNSUBSCRIBE:
1280                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1281                         break;
1282                 case IKS_TYPE_UNSUBSCRIBED:
1283                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1284                         break;
1285                 default:                                /*IKS_TYPE_ERROR: */
1286                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
1287                         break;
1288                 }
1289         }
1290 }
1291
1292 /*!
1293  * \brief sends messages.
1294  * \param aji_client struct , reciever, message.
1295  * \return 1.
1296  */
1297 int ast_aji_send(struct aji_client *client, char *address, char *message)
1298 {
1299         int res = 0;
1300         iks *message_packet = NULL;
1301         if (client->state == AJI_CONNECTED) {
1302                 message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message);
1303                 if (message_packet) {
1304                         iks_insert_attrib(message_packet, "from", client->jid->full);
1305                         res = iks_send(client->p, message_packet);
1306                 } else {
1307                         ast_log(LOG_ERROR, "Out of memory.\n");
1308                 }
1309                 if (message_packet)
1310                         iks_delete(message_packet);
1311         } else
1312                 ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
1313         return 1;
1314 }
1315
1316 /*!
1317  * \brief create a chatroom.
1318  * \param aji_client struct , room, server, topic for the room.
1319  * \return 0.
1320  */
1321 int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
1322 {
1323         int res = 0;
1324         iks *iq = NULL;
1325         iq = iks_new("iq");
1326         if (iq && client) {
1327                 iks_insert_attrib(iq, "type", "get");
1328                 iks_insert_attrib(iq, "to", server);
1329                 iks_insert_attrib(iq, "id", client->mid);
1330                 ast_aji_increment_mid(client->mid);
1331                 iks_send(client->p, iq);
1332         } else 
1333                 ast_log(LOG_ERROR, "Out of memory.\n");
1334         return res;
1335 }
1336
1337 /*!
1338  * \brief join a chatroom.
1339  * \param aji_client struct , room.
1340  * \return res.
1341  */
1342 int ast_aji_join_chat(struct aji_client *client, char *room)
1343 {
1344         int res = 0;
1345         iks *presence = NULL, *priority = NULL;
1346         presence = iks_new("presence");
1347         priority = iks_new("priority");
1348         if (presence && priority && client) {
1349                 iks_insert_cdata(priority, "0", 1);
1350                 iks_insert_attrib(presence, "to", room);
1351                 iks_insert_node(presence, priority);
1352                 res = iks_send(client->p, presence);
1353                 iks_insert_cdata(priority, "5", 1);
1354                 iks_insert_attrib(presence, "to", room);
1355                 res = iks_send(client->p, presence);
1356         } else 
1357                 ast_log(LOG_ERROR, "Out of memory.\n");
1358         if (presence)
1359                 iks_delete(presence);
1360         if (priority)
1361                 iks_delete(priority);
1362         return res;
1363 }
1364
1365 /*!
1366  * \brief invite to a chatroom.
1367  * \param aji_client struct ,user, room, message.
1368  * \return res.
1369  */
1370 int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
1371 {
1372         int res = 0;
1373         iks *invite, *body, *namespace;
1374
1375         invite = iks_new("message");
1376         body = iks_new("body");
1377         namespace = iks_new("x");
1378         if (client && invite && body && namespace) {
1379                 iks_insert_attrib(invite, "to", user);
1380                 iks_insert_attrib(invite, "id", client->mid);
1381                 ast_aji_increment_mid(client->mid);
1382                 iks_insert_cdata(body, message, strlen(message));
1383                 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
1384                 iks_insert_attrib(namespace, "jid", room);
1385                 iks_insert_node(invite, body);
1386                 iks_insert_node(invite, namespace);
1387                 res = iks_send(client->p, invite);
1388         } else 
1389                 ast_log(LOG_ERROR, "Out of memory.\n");
1390         if (body)
1391                 iks_delete(body);
1392         if (namespace)
1393                 iks_delete(namespace);
1394         if (invite)
1395                 iks_delete(invite);
1396         return res;
1397 }
1398
1399
1400 /*!
1401  * \brief receive message loop.
1402  * \param aji_client struct.
1403  * \return void.
1404  */
1405 static void *aji_recv_loop(void *data)
1406 {
1407         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1408         int res = IKS_HOOK;
1409         do {
1410                 if (res != IKS_OK) {
1411                         while(res != IKS_OK) {
1412                                 if(option_verbose > 3)
1413                                         ast_verbose("JABBER: reconnecting.\n");
1414                                 res = aji_reconnect(client);
1415                                 sleep(4);
1416                         }
1417                 }
1418
1419                 res = iks_recv(client->p, 1);
1420                 client->timeout--;
1421                 if (res == IKS_HOOK) 
1422                         ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
1423                 else if (res == IKS_NET_TLSFAIL)
1424                         ast_log(LOG_WARNING, "JABBER:  Failure in TLS.\n");
1425                 else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
1426                         res = iks_send_raw(client->p, " ");
1427                         if(res == IKS_OK)
1428                                 client->timeout = 50;
1429                         else
1430                                 ast_log(LOG_WARNING, "JABBER:  Network Timeout\n");
1431                 } else if (res == IKS_NET_RWERR)
1432                         ast_log(LOG_WARNING, "JABBER: socket read error\n");
1433         } while (client);
1434         ASTOBJ_UNREF(client, aji_client_destroy);
1435         return 0;
1436 }
1437
1438 /*!
1439  * \brief increments the mid field for messages and other events.
1440  * \param message id.
1441  * \return void.
1442  */
1443 void ast_aji_increment_mid(char *mid)
1444 {
1445         int i = 0;
1446
1447         for (i = strlen(mid) - 1; i >= 0; i--) {
1448                 if (mid[i] != 'z') {
1449                         mid[i] = mid[i] + 1;
1450                         i = 0;
1451                 } else
1452                         mid[i] = 'a';
1453         }
1454 }
1455
1456
1457 /*!
1458  * \brief attempts to register to a transport.
1459  * \param aji_client struct, and xml packet.
1460  * \return IKS_FILTER_EAT.
1461  */
1462 static int aji_register_transport(void *data, ikspak *pak)
1463 {
1464         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1465         int res = 0;
1466         struct aji_buddy *buddy = NULL;
1467         iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
1468
1469         if (client && send) {
1470                 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1471                         ASTOBJ_RDLOCK(iterator); 
1472                         if (iterator->btype == AJI_TRANS) {
1473                                   buddy = iterator;
1474                         }
1475                         ASTOBJ_UNLOCK(iterator);
1476                 });
1477                 iks_filter_remove_hook(client->f, aji_register_transport);
1478                 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);
1479                 iks_insert_attrib(send, "to", buddy->host);
1480                 iks_insert_attrib(send, "id", client->mid);
1481                 ast_aji_increment_mid(client->mid);
1482                 iks_insert_attrib(send, "from", client->user);
1483                 res = iks_send(client->p, send);
1484         } else 
1485                 ast_log(LOG_ERROR, "Out of memory.\n");
1486
1487         if (send)
1488                 iks_delete(send);
1489         ASTOBJ_UNREF(client, aji_client_destroy);
1490         return IKS_FILTER_EAT;
1491
1492 }
1493
1494 /*!
1495  * \brief attempts to register to a transport step 2.
1496  * \param aji_client struct, and xml packet.
1497  * \return IKS_FILTER_EAT.
1498  */
1499 static int aji_register_transport2(void *data, ikspak *pak)
1500 {
1501         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1502         int res = 0;
1503         struct aji_buddy *buddy = NULL;
1504
1505         iks *regiq = iks_new("iq");
1506         iks *regquery = iks_new("query");
1507         iks *reguser = iks_new("username");
1508         iks *regpass = iks_new("password");
1509
1510         if (client && regquery && reguser && regpass && regiq) {
1511                 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1512                         ASTOBJ_RDLOCK(iterator);
1513                         if (iterator->btype == AJI_TRANS)
1514                                 buddy = iterator; ASTOBJ_UNLOCK(iterator);
1515                 });
1516                 iks_filter_remove_hook(client->f, aji_register_transport2);
1517                 iks_insert_attrib(regiq, "to", buddy->host);
1518                 iks_insert_attrib(regiq, "type", "set");
1519                 iks_insert_attrib(regiq, "id", client->mid);
1520                 ast_aji_increment_mid(client->mid);
1521                 iks_insert_attrib(regiq, "from", client->user);
1522                 iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
1523                 iks_insert_cdata(reguser, buddy->user, strlen(buddy->user));
1524                 iks_insert_cdata(regpass, buddy->pass, strlen(buddy->pass));
1525                 iks_insert_node(regiq, regquery);
1526                 iks_insert_node(regquery, reguser);
1527                 iks_insert_node(regquery, regpass);
1528                 res = iks_send(client->p, regiq);
1529         } else
1530                 ast_log(LOG_ERROR, "Out of memory.\n");
1531         if (regiq)
1532                 iks_delete(regiq);
1533         if (regquery)
1534                 iks_delete(regquery);
1535         if (reguser)
1536                 iks_delete(reguser);
1537         if (regpass)
1538                 iks_delete(regpass);
1539         ASTOBJ_UNREF(client, aji_client_destroy);
1540         return IKS_FILTER_EAT;
1541 }
1542
1543 /*!
1544  * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
1545  * \param aji_client struct.
1546  * \return void.
1547  */
1548 static void aji_pruneregister(struct aji_client *client)
1549 {
1550         int res = 0;
1551         iks *removeiq = iks_new("iq");
1552         iks *removequery = iks_new("query");
1553         iks *removeitem = iks_new("item");
1554         iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
1555
1556         if (client && removeiq && removequery && removeitem && send) {
1557                 iks_insert_node(removeiq, removequery);
1558                 iks_insert_node(removequery, removeitem);
1559                 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1560                         ASTOBJ_RDLOCK(iterator);
1561                         /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
1562                          * be called at the same time */
1563                         if (ast_test_flag(iterator, AJI_AUTOPRUNE)) {
1564                                 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
1565                                                 "GoodBye your status is no longer needed by Asterisk the Open Source PBX"
1566                                                 " so I am no longer subscribing to your presence.\n"));
1567                                 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
1568                                                 "GoodBye you are no longer in the asterisk config file so I am removing"
1569                                                 " your access to my presence.\n"));
1570                                 iks_insert_attrib(removeiq, "from", client->jid->full); 
1571                                 iks_insert_attrib(removeiq, "type", "set"); 
1572                                 iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
1573                                 iks_insert_attrib(removeitem, "jid", iterator->name);
1574                                 iks_insert_attrib(removeitem, "subscription", "remove");
1575                                 res = iks_send(client->p, removeiq);
1576                         } else if (ast_test_flag(iterator, AJI_AUTOREGISTER)) {
1577                                 if (iterator->btype == AJI_USER) {      /*if it is not a transport */
1578                                         res = iks_send(client->p, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name, 
1579                                                         "Greetings I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
1580                                 } else {
1581                                         iks_filter_add_rule(client->f, aji_register_transport, client,
1582                                                                   IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS,
1583                                                                   "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
1584                                         iks_insert_attrib(send, "to", iterator->host);
1585                                         iks_insert_attrib(send, "from", client->jid->full);
1586                                         res = iks_send(client->p, send);
1587                                 }
1588                                 ast_clear_flag(iterator, AJI_AUTOREGISTER);
1589                         }
1590                         ASTOBJ_UNLOCK(iterator);
1591                 });
1592         } else
1593                 ast_log(LOG_ERROR, "Out of memory.\n");
1594         if (removeiq)
1595                 iks_delete(removeiq);
1596         if (removequery)
1597                 iks_delete(removequery);
1598         if (removeitem)
1599                 iks_delete(removeitem);
1600         if (send)
1601                 iks_delete(send);
1602         ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy);
1603 }
1604
1605 /*!
1606  * \brief filters the roster packet we get back from server.
1607  * \param aji_client struct, and xml packet.
1608  * \return IKS_FILTER_EAT.
1609  */
1610 static int aji_filter_roster(void *data, ikspak *pak)
1611 {
1612         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1613         int flag = 0;
1614         iks *x = NULL;
1615         struct aji_buddy *buddy;
1616         
1617         client->state = AJI_CONNECTED;
1618         ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1619                 ASTOBJ_RDLOCK(iterator);
1620                 x = iks_child(pak->query);
1621                 flag = 0;
1622                 while (x) {
1623                         if (!iks_strcmp(iks_name(x), "item")) {
1624                                 if (!ast_strlen_zero(iterator->pass)) {
1625                                         if (!strcasecmp(iterator->host, iks_find_attrib(x, "jid"))) {
1626                                                   ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER); 
1627                                                   flag = 1;
1628                                         }
1629                                 } else {
1630                                         if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
1631                                                   flag = 1;
1632                                                   ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
1633                                         }
1634                                 }
1635                         }
1636                         x = iks_next(x);
1637                 }
1638                 if (!flag)
1639                         ast_copy_flags(iterator, client, AJI_AUTOREGISTER);
1640                 if (x)
1641                         iks_delete(x);
1642                 ASTOBJ_UNLOCK(iterator);
1643         });
1644
1645         x = iks_child(pak->query);
1646         while (x) {
1647                 flag = 0;
1648                 if (iks_strcmp(iks_name(x), "item") == 0) {
1649                         ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1650                                 ASTOBJ_RDLOCK(iterator);
1651                                 if (!ast_strlen_zero(iterator->pass)) {
1652                                         if (!strcasecmp(iterator->host, iks_find_attrib(x, "jid")))
1653                                         flag = 1;
1654                                 } else {
1655                                         if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
1656                                                 flag = 1;
1657                                 }
1658                                 ASTOBJ_UNLOCK(iterator);
1659                         });
1660
1661                         if (!flag) {
1662                                 buddy = (struct aji_buddy *) malloc(sizeof(struct aji_buddy));
1663                                 if (!buddy) {
1664                                         ast_log(LOG_WARNING, "Out of memory\n");
1665                                         return 0;
1666                                 }
1667                                 memset(buddy, 0, sizeof(struct aji_buddy));
1668                                 ASTOBJ_INIT(buddy);
1669                                 ASTOBJ_WRLOCK(buddy);
1670                                 ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
1671                                 ast_clear_flag(buddy, AST_FLAGS_ALL);
1672                                 if(ast_test_flag(client, AJI_AUTOPRUNE)) {
1673                                         ast_set_flag(buddy, AJI_AUTOPRUNE);
1674                                         buddy->objflags |= ASTOBJ_FLAG_MARKED;
1675                                 } else
1676                                         ast_set_flag(buddy, AJI_AUTOREGISTER);
1677                                 ASTOBJ_UNLOCK(buddy);
1678                                 if (buddy) {
1679                                         ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
1680                                         ASTOBJ_UNREF(buddy, aji_buddy_destroy);
1681                                 }
1682                         }
1683                 }
1684                 x = iks_next(x);
1685         }
1686         if (x)
1687                 iks_delete(x);
1688         aji_pruneregister(client);
1689
1690         ASTOBJ_UNREF(client, aji_client_destroy);
1691         return IKS_FILTER_EAT;
1692 }
1693
1694 static int aji_reconnect(struct aji_client *client)
1695 {
1696         int res = 0;
1697
1698         if (client->state)
1699                 client->state = AJI_DISCONNECTED;
1700         client->timeout=50;
1701         if (client->p)
1702                 iks_parser_reset(client->p);
1703         if (client->authorized)
1704                 client->authorized = 0;
1705
1706         switch (client->component) {
1707         case AJI_COMPONENT:
1708                 res = aji_component_initialize(client);
1709                 break;
1710         case AJI_CLIENT:
1711                 res = aji_client_initialize(client);
1712                 break;
1713         }
1714         return res;
1715 }
1716
1717 static int aji_get_roster(struct aji_client *client)
1718 {
1719         iks *roster = NULL;
1720         roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
1721         if(roster) {
1722                 iks_insert_attrib(roster, "id", "roster");
1723                 aji_set_presence(client, client->jid->full, 1, client->statusmessage);
1724                 iks_send(client->p, roster);
1725         }
1726         if (roster)
1727                 iks_delete(roster);
1728         return 1;
1729 }
1730
1731 /*!
1732  * \brief connects as a client to jabber server.
1733  * \param aji_client struct, and xml packet.
1734  * \return res.
1735  */
1736 static int aji_client_connect(void *data, ikspak *pak)
1737 {
1738         struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
1739         int res = 0;
1740
1741         if (client) {
1742                 if (client->state == AJI_DISCONNECTED) {
1743                         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);
1744                         client->state = AJI_CONNECTING;
1745                         client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
1746                         iks_filter_remove_hook(client->f, aji_client_connect);
1747                         if(client->component == AJI_CLIENT)
1748                                 aji_get_roster(client);
1749                 }
1750         } else
1751                 ast_log(LOG_ERROR, "Out of memory.\n");
1752
1753         ASTOBJ_UNREF(client, aji_client_destroy);
1754         return res;
1755 }
1756
1757 /*!
1758  * \brief prepares client for connect.
1759  * \param aji_client struct.
1760  * \return 1.
1761  */
1762 static int aji_client_initialize(struct aji_client *client)
1763 {
1764         int connected = 0;
1765
1766         connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->jid->server);
1767
1768         if (connected == IKS_NET_NOCONN) {
1769                 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
1770                 return IKS_HOOK;
1771         } else  if (connected == IKS_NET_NODNS) {
1772                 ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to  %s\n", client->name, S_OR(client->serverhost, client->jid->server));
1773                 return IKS_HOOK;
1774         } else
1775                 iks_recv(client->p, 30);
1776         return IKS_OK;
1777 }
1778
1779 /*!
1780  * \brief prepares component for connect.
1781  * \param aji_client struct.
1782  * \return 1.
1783  */
1784 static int aji_component_initialize(struct aji_client *client)
1785 {
1786         int connected = 1;
1787         connected = iks_connect_via(client->p, client->jid->server, client->port, client->user);
1788         if (connected == IKS_NET_NOCONN) {
1789                 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
1790                 return IKS_HOOK;
1791         } else if (connected == IKS_NET_NODNS) {
1792                 ast_log(LOG_ERROR, "JABBER ERROR: No DNS\n");
1793                 return IKS_HOOK;
1794         } else if (!connected) 
1795                 iks_recv(client->p, 30);
1796         return IKS_OK;
1797 }
1798
1799 /*!
1800  * \brief disconnect from jabber server.
1801  * \param aji_client struct.
1802  * \return 1.
1803  */
1804 int ast_aji_disconnect(struct aji_client *client)
1805 {
1806         if (client) {
1807                 if (option_verbose > 3)
1808                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: Disconnecting\n");
1809                 iks_disconnect(client->p);
1810                 iks_parser_delete(client->p);
1811                 ASTOBJ_UNREF(client, aji_client_destroy);
1812         }
1813
1814         return 1;
1815 }
1816
1817 /*!
1818  * \brief set presence of client.
1819  * \param aji_client struct, user to send it to, level, description.
1820  * \return void.
1821  */
1822 static void aji_set_presence(struct aji_client *client, char *user, int level, char *desc)
1823 {
1824         int res = 0;
1825         iks *presence = iks_make_pres(level, desc);
1826         iks *cnode = iks_new("c");
1827         iks *priority = iks_new("priority");
1828
1829         iks_insert_cdata(priority, "0", 1);
1830         if (presence && cnode && client) {
1831                 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
1832                 iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
1833                 iks_insert_attrib(cnode, "ext", "voice-v1");
1834                 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
1835                 iks_insert_node(presence, cnode);
1836                 res = iks_send(client->p, presence);
1837         } else
1838                 ast_log(LOG_ERROR, "Out of memory.\n");
1839         if (cnode)
1840                 iks_delete(cnode);
1841         if (presence)
1842                 iks_delete(presence);
1843 }
1844
1845 /*!
1846  * \brief turnon console debugging.
1847  * \param fd, number of args, args.
1848  * \return RESULT_SUCCESS.
1849  */
1850 static int aji_do_debug(int fd, int argc, char *argv[])
1851 {
1852         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
1853                 ASTOBJ_RDLOCK(iterator); 
1854                 iterator->debug = 1;
1855                 ASTOBJ_UNLOCK(iterator);
1856         });
1857         ast_cli(fd, "Jabber Debugging Enabled.\n");
1858         return RESULT_SUCCESS;
1859 }
1860
1861 /*!
1862  * \brief reload jabber module.
1863  * \param fd, number of args, args.
1864  * \return RESULT_SUCCESS.
1865  */
1866 static int aji_do_reload(int fd, int argc, char *argv[])
1867 {
1868         aji_reload();
1869         ast_cli(fd, "Jabber Reloaded.\n");
1870         return RESULT_SUCCESS;
1871 }
1872
1873 /*!
1874  * \brief turnoff console debugging.
1875  * \param fd, number of args, args.
1876  * \return RESULT_SUCCESS.
1877  */
1878 static int aji_no_debug(int fd, int argc, char *argv[])
1879 {
1880         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
1881                 ASTOBJ_RDLOCK(iterator);
1882                 iterator->debug = 0;
1883                 ASTOBJ_UNLOCK(iterator);
1884         });
1885         ast_cli(fd, "Jabber Debugging Disabled.\n");
1886         return RESULT_SUCCESS;
1887 }
1888
1889 /*!
1890  * \brief show client status.
1891  * \param fd, number of args, args.
1892  * \return RESULT_SUCCESS.
1893  */
1894 static int aji_show_clients(int fd, int argc, char *argv[])
1895 {
1896         char *status;
1897         int count = 0;
1898         ast_cli(fd, "Jabber Users and their status:\n");
1899         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
1900                 ASTOBJ_RDLOCK(iterator);
1901                 count++;
1902                 switch (iterator->state) {
1903                 case AJI_DISCONNECTED:
1904                         status = "Disconnected";
1905                         break;
1906                 case AJI_CONNECTING:
1907                         status = "Connecting";
1908                         break;
1909                 case AJI_CONNECTED:
1910                         status = "Connected";
1911                         break;
1912                 default:
1913                         status = "Unknown";
1914                 }
1915                 ast_cli(fd, "       User: %s     - %s\n", iterator->user, status);
1916                 ASTOBJ_UNLOCK(iterator);
1917         });
1918         ast_cli(fd, "----\n");
1919         ast_cli(fd, "   Number of users: %d\n", count);
1920         return RESULT_SUCCESS;
1921 }
1922
1923 /*!
1924  * \brief send test message for debugging.
1925  * \param fd, number of args, args.
1926  * \return RESULT_SUCCESS.
1927  */
1928 static int aji_test(int fd, int argc, char *argv[])
1929 {
1930         struct aji_client *client;
1931         struct aji_resource *resource;
1932         const char *name = "asterisk";
1933         struct aji_message *tmp;
1934
1935         if (argc > 3)
1936                 return RESULT_SHOWUSAGE;
1937         else if (argc == 3)
1938                 name = argv[2];
1939
1940         if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
1941                 ast_cli(fd, "Unable to find client '%s'!\n", name);
1942                 return RESULT_FAILURE;
1943         }
1944
1945         /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */
1946         ast_aji_send(client, "mogorman@astjab.org", "blahblah");
1947         ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
1948                 ASTOBJ_RDLOCK(iterator);
1949                 ast_verbose("User: %s\n", iterator->name);
1950                 for (resource = iterator->resources; resource; resource = resource->next) {
1951                         ast_verbose("Resource: %s\n", resource->resource);
1952                         if(resource->cap) {
1953                                 ast_verbose("   client: %s\n", resource->cap->parent->node);
1954                                 ast_verbose("   version: %s\n", resource->cap->version);
1955                                 ast_verbose("   Jingle Capable: %d\n", resource->cap->jingle);
1956                         }
1957                         ast_verbose("   Priority: %d\n", resource->priority);
1958                         ast_verbose("   Status: %d\n", resource->status); 
1959                         ast_verbose("   Message: %s\n", S_OR(resource->description,"")); 
1960                 }
1961                 ASTOBJ_UNLOCK(iterator);
1962         });
1963         ast_verbose("\nOooh a working message stack!\n");
1964         AST_LIST_LOCK(&client->messages);
1965         AST_LIST_TRAVERSE(&client->messages, tmp, list) {
1966                 ast_verbose("   Message from: %s with id %s @ %s        %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, ""));
1967         }
1968         AST_LIST_UNLOCK(&client->messages);
1969         ASTOBJ_UNREF(client, aji_client_destroy);
1970
1971         return RESULT_SUCCESS;
1972 }
1973
1974 /*!
1975  * \brief creates aji_client structure.
1976  * \param label, ast_variable, debug, pruneregister, component/client, aji_client to dump into. 
1977  * \return 0.
1978  */
1979 static int aji_create_client(char *label, struct ast_variable *var, int debug)
1980 {
1981         char *resource;
1982         struct aji_client *client = NULL;
1983         int flag = 0;
1984
1985         client = ASTOBJ_CONTAINER_FIND(&clients,label);
1986         if (!client) {
1987                 flag = 1;
1988                 client = (struct aji_client *) malloc(sizeof(struct aji_client));
1989                 if (!client) {
1990                         ast_log(LOG_ERROR, "Out of memory!\n");
1991                         return 0;
1992                 }
1993                 memset(client, 0, sizeof(struct aji_client));
1994                 ASTOBJ_INIT(client);
1995                 ASTOBJ_WRLOCK(client);
1996                 ASTOBJ_CONTAINER_INIT(&client->buddies);
1997         } else {
1998                 ASTOBJ_WRLOCK(client);
1999                 ASTOBJ_UNMARK(client);
2000         }
2001         ASTOBJ_CONTAINER_MARKALL(&client->buddies);
2002         ast_copy_string(client->name, label, sizeof(client->name));
2003         ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
2004
2005         client->debug = debug;
2006         ast_copy_flags(client, &globalflags, AST_FLAGS_ALL);
2007         client->port = 5222;
2008         client->usetls = 1;
2009         client->usesasl = 1;
2010         client->forcessl = 0;
2011         client->keepalive = 1;
2012         client->timeout = 50;
2013         client->message_timeout = 100;
2014         AST_LIST_HEAD_INIT(&client->messages);
2015         client->component = AJI_CLIENT;
2016         ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage));
2017
2018         if (flag) {
2019                 client->authorized = 0;
2020                 client->state = AJI_DISCONNECTED;
2021         }
2022         while (var) {
2023                 if (!strcasecmp(var->name, "username"))
2024                         ast_copy_string(client->user, var->value, sizeof(client->user));
2025                 else if (!strcasecmp(var->name, "serverhost"))
2026                         ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
2027                 else if (!strcasecmp(var->name, "secret"))
2028                         ast_copy_string(client->password, var->value, sizeof(client->password));
2029                 else if (!strcasecmp(var->name, "statusmessage"))
2030                         ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage));
2031                 else if (!strcasecmp(var->name, "port"))
2032                         client->port = atoi(var->value);
2033                 else if (!strcasecmp(var->name, "timeout"))
2034                         client->message_timeout = atoi(var->value);
2035                 else if (!strcasecmp(var->name, "debug"))
2036                         client->debug = (ast_false(var->value)) ? 0 : 1;
2037                 else if (!strcasecmp(var->name, "type")) {
2038                         if (!strcasecmp(var->value, "component"))
2039                                 client->component = AJI_COMPONENT;
2040                 } else if (!strcasecmp(var->name, "usetls")) {
2041                         client->usetls = (ast_false(var->value)) ? 0 : 1;
2042                 } else if (!strcasecmp(var->name, "usesasl")) {
2043                         client->usesasl = (ast_false(var->value)) ? 0 : 1;
2044                 } else if (!strcasecmp(var->name, "forceoldssl"))
2045                         client->forcessl = (ast_false(var->value)) ? 0 : 1;
2046                 else if (!strcasecmp(var->name, "keepalive"))
2047                         client->keepalive = (ast_false(var->value)) ? 0 : 1;
2048                 else if (!strcasecmp(var->name, "autoprune"))
2049                         ast_set2_flag(client, ast_true(var->value), AJI_AUTOPRUNE);
2050                 else if (!strcasecmp(var->name, "autoregister"))
2051                         ast_set2_flag(client, ast_true(var->value), AJI_AUTOREGISTER);
2052                 else if (!strcasecmp(var->name, "buddy"))
2053                                 aji_create_buddy(var->value, client);
2054                 else if (!strcasecmp(var->name, "transport"))
2055                                 aji_create_transport(var->value, client);
2056                 var = var->next;
2057         }
2058         if (!flag) {
2059                 ASTOBJ_UNLOCK(client);
2060                 ASTOBJ_UNREF(client, aji_client_destroy);
2061                 return 1;
2062         }
2063         client->p = iks_stream_new(((client->component == AJI_CLIENT) ? "jabber:client" : "jabber:component:accept"), client, aji_act_hook);
2064         if (!client->p) {
2065                 ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name);
2066                 return 0;
2067         }
2068         client->stack = iks_stack_new(8192, 8192);
2069         if (!client->stack) {
2070                 ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name);
2071                 return 0;
2072         }
2073         client->f = iks_filter_new();
2074         if (!client->f) {
2075                 ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name);
2076                 return 0;
2077         }
2078         if (!strchr(client->user, '/') && client->component == AJI_CLIENT) {
2079                 resource = NULL;
2080                 asprintf(&resource, "%s/asterisk", client->user);
2081                 if (resource) {
2082                         client->jid = iks_id_new(client->stack, resource);
2083                         free(resource);
2084                 }
2085         } else
2086                 client->jid = iks_id_new(client->stack, client->user);
2087         if (client->component == AJI_COMPONENT) {
2088                 iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2089                 iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
2090                 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);
2091                 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);
2092         } else {
2093                 iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
2094         }
2095         if (!strchr(client->user, '/') && client->component == AJI_CLIENT) {
2096                 resource = NULL;
2097                 asprintf(&resource, "%s/asterisk", client->user);
2098                 if (resource) {
2099                         client->jid = iks_id_new(client->stack, resource);
2100                         free(resource);
2101                 }
2102         } else
2103                 client->jid = iks_id_new(client->stack, client->user);
2104         iks_set_log_hook(client->p, aji_log_hook);
2105         ASTOBJ_UNLOCK(client);
2106         ASTOBJ_CONTAINER_LINK(&clients,client);
2107         return 1;
2108 }
2109
2110 /*!
2111  * \brief creates transport.
2112  * \param label, buddy to dump it into. 
2113  * \return 0.
2114  */
2115 static int aji_create_transport(char *label, struct aji_client *client)
2116 {
2117         char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
2118         struct aji_buddy *buddy = NULL;
2119
2120         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2121         if (!buddy) {
2122                 buddy = malloc(sizeof(struct aji_buddy));
2123                 if(!buddy) {
2124                         ast_log(LOG_WARNING, "Out of memory\n");
2125                         return 0;
2126                 }
2127                 memset(buddy, 0, sizeof(struct aji_buddy));
2128                 ASTOBJ_INIT(buddy);
2129         }
2130         ASTOBJ_WRLOCK(buddy);
2131         server = label;
2132         if ((buddyname = strchr(label, ','))) {
2133                 *buddyname = '\0';
2134                 buddyname++;
2135                 if (buddyname && buddyname[0] != '\0') {
2136                         if ((user = strchr(buddyname, ','))) {
2137                                 *user = '\0';
2138                                 user++;
2139                                 if (user && user[0] != '\0') {
2140                                         if ((pass = strchr(user, ','))) {
2141                                                 *pass = '\0';
2142                                                 pass++;
2143                                                 ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
2144                                                 ast_copy_string(buddy->user, user, sizeof(buddy->user));
2145                                                 ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
2146                                                 ast_copy_string(buddy->server, server, sizeof(buddy->server));
2147                                                 return 1;
2148                                         }
2149                                 }
2150                         }
2151                 }
2152         }
2153         ASTOBJ_UNLOCK(buddy);
2154         ASTOBJ_UNMARK(buddy);
2155         ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2156         return 0;
2157 }
2158
2159 /*!
2160  * \brief creates buddy.
2161  * \param label, buddy to dump it into. 
2162  * \return 0.
2163  */
2164 static int aji_create_buddy(char *label, struct aji_client *client)
2165 {
2166         struct aji_buddy *buddy = NULL;
2167         int flag = 0;
2168         buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
2169         if (!buddy) {
2170                 flag = 1;
2171                 buddy = malloc(sizeof(struct aji_buddy));
2172                 if(!buddy) {
2173                         ast_log(LOG_WARNING, "Out of memory\n");
2174                         return 0;
2175                 }
2176                 memset(buddy, 0, sizeof(struct aji_buddy));
2177                 ASTOBJ_INIT(buddy);
2178         }
2179         ASTOBJ_WRLOCK(buddy);
2180         ast_copy_string(buddy->name, label, sizeof(buddy->name));
2181         ASTOBJ_UNLOCK(buddy);
2182         if(flag)
2183                 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
2184         else {
2185                 ASTOBJ_UNMARK(buddy);
2186                 ASTOBJ_UNREF(buddy, aji_buddy_destroy);
2187         }
2188         return 1;
2189 }
2190
2191 /*!
2192  * \brief load config file.
2193  * \param void. 
2194  * \return 1.
2195  */
2196 static int aji_load_config(void)
2197 {
2198         char *cat = NULL;
2199         int debug = 1;
2200         struct ast_config *cfg = NULL;
2201         struct ast_variable *var = NULL;
2202
2203         cfg = ast_config_load(JABBER_CONFIG);
2204         if (!cfg) {
2205                 ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
2206                 return 0;
2207         }
2208
2209         cat = ast_category_browse(cfg, NULL);
2210         for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
2211                 if (!strcasecmp(var->name, "debug"))
2212                         debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
2213                 else if (!strcasecmp(var->name, "autoprune"))
2214                         ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
2215                 else if (!strcasecmp(var->name, "autoregister"))
2216                         ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
2217         }
2218
2219         while (cat) {
2220                 if (strcasecmp(cat, "general")) {
2221                                 var = ast_variable_browse(cfg, cat);
2222                                 aji_create_client(cat, var, debug);
2223                 }
2224                 cat = ast_category_browse(cfg, cat);
2225         }
2226         return 1;
2227 }
2228
2229 /*!
2230  * \brief grab a aji_client structure by label name.
2231  * \param void. 
2232  * \return 1.
2233  */
2234 struct aji_client *ast_aji_get_client(char *name)
2235 {
2236         struct aji_client *client = NULL;
2237
2238         client = ASTOBJ_CONTAINER_FIND(&clients, name);
2239         if (!client && !strchr(name, '@'))
2240                 client = ASTOBJ_CONTAINER_FIND_FULL(&clients, name, user,,, strcasecmp);
2241         return client;
2242 }
2243
2244 struct aji_client_container *ast_aji_get_clients(void)
2245 {
2246         return &clients;
2247 }
2248
2249 static char mandescr_jabber_send[] =
2250 "Description: Sends a message to a Jabber Client.\n"
2251 "Variables: \n"
2252 "  Jabber:      Client or transport Asterisk uses to connect to JABBER.\n"
2253 "  ScreenName:  User Name to message.\n"
2254 "  Message:     Message to be sent to the buddy\n";
2255
2256 /*! \brief  Send a Jabber Message via call from the Manager */
2257 static int manager_jabber_send( struct mansession *s, struct message *m )
2258 {
2259         struct aji_client *client = NULL;
2260         char *id = astman_get_header(m,"ActionID");
2261         char *jabber = astman_get_header(m,"Jabber");
2262         char *screenname = astman_get_header(m,"ScreenName");
2263         char *message = astman_get_header(m,"Message");
2264
2265         if (ast_strlen_zero(jabber)) {
2266                 astman_send_error(s, m, "No transport specified");
2267                 return 0;
2268         }
2269         if (ast_strlen_zero(screenname)) {
2270                 astman_send_error(s, m, "No ScreenName specified");
2271                 return 0;
2272         }
2273         if (ast_strlen_zero(message)) {
2274                 astman_send_error(s, m, "No Message specified");
2275                 return 0;
2276         }
2277
2278         astman_send_ack(s, m, "Attempting to send Jabber Message");
2279         client = ast_aji_get_client(jabber);
2280         if (!client) {
2281                 astman_send_error(s, m, "Could not find Sender");
2282                 return 0;
2283         }       
2284         if (strchr(screenname, '@') && message){
2285                 ast_aji_send(client, screenname, message);      
2286                 if (!ast_strlen_zero(id))
2287                         astman_append(s, "ActionID: %s\r\n",id);
2288                 astman_append(s, "Response: Success\r\n");
2289                 return 0;
2290         }
2291         if (!ast_strlen_zero(id))
2292                 astman_append(s, "ActionID: %s\r\n",id);
2293         astman_append(s, "Response: Failure\r\n");
2294         return 0;
2295 }
2296
2297
2298 static int aji_reload()
2299 {
2300         ASTOBJ_CONTAINER_MARKALL(&clients);
2301         if (!aji_load_config()) {
2302                 ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
2303                 return 0;
2304         }
2305         ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, aji_client_destroy);
2306         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2307                 ASTOBJ_RDLOCK(iterator);
2308                 if(iterator->state == AJI_DISCONNECTED) {
2309                         if (!iterator->thread)
2310                                 ast_pthread_create(&iterator->thread, NULL, aji_recv_loop, iterator);
2311                 } else if (iterator->state == AJI_CONNECTING)
2312                         aji_get_roster(iterator);
2313                 ASTOBJ_UNLOCK(iterator);
2314         });
2315         
2316         return 1;
2317 }
2318
2319 static int unload_module(void)
2320 {
2321         ast_cli_unregister_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry));
2322         ast_unregister_application(app_ajisend);
2323         ast_manager_unregister("JabberSend");
2324         ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
2325                 ASTOBJ_RDLOCK(iterator);
2326                 if (option_verbose > 2)
2327                         ast_verbose(VERBOSE_PREFIX_3 "JABBER: %s\n", iterator->name);
2328                 iterator->state = AJI_DISCONNECTED;
2329                 ast_aji_disconnect(iterator);
2330                 pthread_join(iterator->thread, NULL);
2331                 ASTOBJ_UNLOCK(iterator);
2332         });
2333
2334         ASTOBJ_CONTAINER_DESTROYALL(&clients, aji_client_destroy);
2335         ASTOBJ_CONTAINER_DESTROY(&clients);
2336
2337         ast_log(LOG_NOTICE, "res_jabber unloaded.\n");
2338         return 0;
2339 }
2340
2341 static int load_module(void)
2342 {
2343         ASTOBJ_CONTAINER_INIT(&clients);
2344         if(!aji_reload())
2345                 return AST_MODULE_LOAD_DECLINE;
2346         ast_manager_register2("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send,
2347                         "Sends a message to a Jabber Client", mandescr_jabber_send);
2348         ast_register_application(app_ajisend, aji_send_exec, ajisend_synopsis, ajisend_descrip);
2349         ast_register_application(app_ajistatus, aji_status_exec, ajistatus_synopsis, ajistatus_descrip);
2350         ast_cli_register_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry));
2351
2352         ast_log(LOG_NOTICE, "res_jabber.so loaded.\n");
2353         return 0;
2354 }
2355
2356 static int reload(void)
2357 {
2358         aji_reload();
2359         return 0;
2360 }
2361
2362 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "AJI - Asterisk Jabber Interface",
2363                 .load = load_module,
2364                 .unload = unload_module,
2365                 .reload = reload,
2366                );