![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
res_jabber.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, Digium, Inc. 00005 * 00006 * Matt O'Gorman <mogorman@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * \brief A resource for interfacing asterisk directly as a client 00021 * or a component to a jabber compliant server. 00022 */ 00023 00024 /*** MODULEINFO 00025 <depend>iksemel</depend> 00026 ***/ 00027 00028 #include "asterisk.h" 00029 00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 49691 $") 00031 00032 #include <stdlib.h> 00033 #include <stdio.h> 00034 #include <iksemel.h> 00035 00036 #include "asterisk/channel.h" 00037 #include "asterisk/jabber.h" 00038 #include "asterisk/file.h" 00039 #include "asterisk/config.h" 00040 #include "asterisk/callerid.h" 00041 #include "asterisk/lock.h" 00042 #include "asterisk/logger.h" 00043 #include "asterisk/options.h" 00044 #include "asterisk/cli.h" 00045 #include "asterisk/app.h" 00046 #include "asterisk/pbx.h" 00047 #include "asterisk/md5.h" 00048 #include "asterisk/acl.h" 00049 #include "asterisk/utils.h" 00050 #include "asterisk/module.h" 00051 #include "asterisk/astobj.h" 00052 #include "asterisk/astdb.h" 00053 #include "asterisk/manager.h" 00054 00055 #define JABBER_CONFIG "jabber.conf" 00056 00057 /*-- Forward declarations */ 00058 static int aji_highest_bit(int number); 00059 static void aji_buddy_destroy(struct aji_buddy *obj); 00060 static void aji_client_destroy(struct aji_client *obj); 00061 static int aji_send_exec(struct ast_channel *chan, void *data); 00062 static int aji_status_exec(struct ast_channel *chan, void *data); 00063 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming); 00064 static int aji_act_hook(void *data, int type, iks *node); 00065 static void aji_handle_iq(struct aji_client *client, iks *node); 00066 static void aji_handle_message(struct aji_client *client, ikspak *pak); 00067 static void aji_handle_presence(struct aji_client *client, ikspak *pak); 00068 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak); 00069 static void *aji_recv_loop(void *data); 00070 static int aji_component_initialize(struct aji_client *client); 00071 static int aji_client_initialize(struct aji_client *client); 00072 static int aji_client_connect(void *data, ikspak *pak); 00073 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc); 00074 static int aji_do_debug(int fd, int argc, char *argv[]); 00075 static int aji_do_reload(int fd, int argc, char *argv[]); 00076 static int aji_no_debug(int fd, int argc, char *argv[]); 00077 static int aji_test(int fd, int argc, char *argv[]); 00078 static int aji_show_clients(int fd, int argc, char *argv[]); 00079 static int aji_create_client(char *label, struct ast_variable *var, int debug); 00080 static int aji_create_buddy(char *label, struct aji_client *client); 00081 static int aji_reload(void); 00082 static int aji_load_config(void); 00083 static void aji_pruneregister(struct aji_client *client); 00084 static int aji_filter_roster(void *data, ikspak *pak); 00085 static int aji_get_roster(struct aji_client *client); 00086 static int aji_client_info_handler(void *data, ikspak *pak); 00087 static int aji_dinfo_handler(void *data, ikspak *pak); 00088 static int aji_ditems_handler(void *data, ikspak *pak); 00089 static int aji_register_query_handler(void *data, ikspak *pak); 00090 static int aji_register_approve_handler(void *data, ikspak *pak); 00091 static int aji_reconnect(struct aji_client *client); 00092 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid); 00093 /* No transports in this version */ 00094 /* 00095 static int aji_create_transport(char *label, struct aji_client *client); 00096 static int aji_register_transport(void *data, ikspak *pak); 00097 static int aji_register_transport2(void *data, ikspak *pak); 00098 */ 00099 00100 static const char debug_usage[] = 00101 "Usage: jabber debug\n" 00102 " Enables dumping of Jabber packets for debugging purposes.\n"; 00103 00104 static const char no_debug_usage[] = 00105 "Usage: jabber debug off\n" 00106 " Disables dumping of Jabber packets for debugging purposes.\n"; 00107 00108 static const char reload_usage[] = 00109 "Usage: jabber reload\n" 00110 " Enables reloading of Jabber module.\n"; 00111 00112 static const char test_usage[] = 00113 "Usage: jabber test [client]\n" 00114 " Sends test message for debugging purposes. A specific client\n" 00115 " as configured in jabber.conf can be optionally specified.\n"; 00116 00117 static struct ast_cli_entry aji_cli[] = { 00118 { { "jabber", "debug", NULL}, 00119 aji_do_debug, "Enable Jabber debugging", 00120 debug_usage }, 00121 00122 { { "jabber", "reload", NULL}, 00123 aji_do_reload, "Enable Jabber debugging", 00124 reload_usage }, 00125 00126 { { "jabber", "show", "connected", NULL}, 00127 aji_show_clients, "Show state of clients and components", 00128 debug_usage }, 00129 00130 { { "jabber", "debug", "off", NULL}, 00131 aji_no_debug, "Disable Jabber debug", 00132 no_debug_usage }, 00133 00134 { { "jabber", "test", NULL}, 00135 aji_test, "Shows roster, but is generally used for mog's debugging.", 00136 test_usage }, 00137 }; 00138 00139 static char *app_ajisend = "JabberSend"; 00140 00141 static char *ajisend_synopsis = "JabberSend(jabber,screenname,message)"; 00142 00143 static char *ajisend_descrip = 00144 "JabberSend(Jabber,ScreenName,Message)\n" 00145 " Jabber - Client or transport Asterisk uses to connect to Jabber\n" 00146 " ScreenName - User Name to message.\n" 00147 " Message - Message to be sent to the buddy\n"; 00148 00149 static char *app_ajistatus = "JabberStatus"; 00150 00151 static char *ajistatus_synopsis = "JabberStatus(Jabber,ScreenName,Variable)"; 00152 00153 static char *ajistatus_descrip = 00154 "JabberStatus(Jabber,ScreenName,Variable)\n" 00155 " Jabber - Client or transport Asterisk uses to connect to Jabber\n" 00156 " ScreenName - User Name to retrieve status from.\n" 00157 " Variable - Variable to store presence in will be 1-6.\n" 00158 " In order, Online, Chatty, Away, XAway, DND, Offline\n" 00159 " If not in roster variable will = 7\n"; 00160 00161 struct aji_client_container clients; 00162 00163 struct aji_capabilities *capabilities; 00164 00165 /*! \brief Global flags, initialized to default values */ 00166 static struct ast_flags globalflags = { AJI_AUTOPRUNE | AJI_AUTOREGISTER }; 00167 00168 /*! 00169 * \brief Deletes the aji_client data structure. 00170 * \param obj is the structure we will delete. 00171 * \return void. 00172 */ 00173 static void aji_client_destroy(struct aji_client *obj) 00174 { 00175 struct aji_message *tmp; 00176 ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, aji_buddy_destroy); 00177 ASTOBJ_CONTAINER_DESTROY(&obj->buddies); 00178 iks_filter_delete(obj->f); 00179 iks_parser_delete(obj->p); 00180 iks_stack_delete(obj->stack); 00181 AST_LIST_LOCK(&obj->messages); 00182 while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) { 00183 if (tmp->from) 00184 free(tmp->from); 00185 if (tmp->message) 00186 free(tmp->message); 00187 } 00188 AST_LIST_HEAD_DESTROY(&obj->messages); 00189 free(obj); 00190 } 00191 00192 /*! 00193 * \brief Deletes the aji_buddy data structure. 00194 * \param obj is the structure we will delete. 00195 * \return void. 00196 */ 00197 static void aji_buddy_destroy(struct aji_buddy *obj) 00198 { 00199 struct aji_resource *tmp; 00200 00201 while ((tmp = obj->resources)) { 00202 obj->resources = obj->resources->next; 00203 free(tmp->description); 00204 free(tmp); 00205 } 00206 00207 free(obj); 00208 } 00209 00210 static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak) 00211 { 00212 struct aji_capabilities *list = NULL; 00213 struct aji_version *res = NULL; 00214 00215 list = capabilities; 00216 00217 if(!node) 00218 node = pak->from->full; 00219 if(!version) 00220 version = "none supplied."; 00221 while(list) { 00222 if(!strcasecmp(list->node, node)) { 00223 res = list->versions; 00224 while(res) { 00225 if(!strcasecmp(res->version, version)) 00226 return res; 00227 res = res->next; 00228 } 00229 if(!res) { 00230 res = (struct aji_version *)malloc(sizeof(struct aji_version)); 00231 if(!res) { 00232 ast_log(LOG_ERROR, "Out of memory!\n"); 00233 return NULL; 00234 } 00235 res->jingle = 0; 00236 res->parent = list; 00237 ast_copy_string(res->version, version, sizeof(res->version)); 00238 res->next = list->versions; 00239 list->versions = res; 00240 return res; 00241 } 00242 } 00243 list = list->next; 00244 } 00245 if(!list) { 00246 list = (struct aji_capabilities *)malloc(sizeof(struct aji_capabilities)); 00247 if(!list) { 00248 ast_log(LOG_ERROR, "Out of memory!\n"); 00249 return NULL; 00250 } 00251 res = (struct aji_version *)malloc(sizeof(struct aji_version)); 00252 if(!res) { 00253 ast_log(LOG_ERROR, "Out of memory!\n"); 00254 return NULL; 00255 } 00256 ast_copy_string(list->node, node, sizeof(list->node)); 00257 ast_copy_string(res->version, version, sizeof(res->version)); 00258 res->jingle = 0; 00259 res->parent = list; 00260 res->next = list->versions; 00261 list->versions = res; 00262 list->next = capabilities; 00263 capabilities = list; 00264 } 00265 return res; 00266 } 00267 00268 static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *name) 00269 { 00270 struct aji_resource *res = NULL; 00271 if (!buddy || !name) 00272 return res; 00273 res = buddy->resources; 00274 while (res) { 00275 if (!strcasecmp(res->resource, name)) { 00276 break; 00277 } 00278 res = res->next; 00279 } 00280 return res; 00281 } 00282 00283 static int gtalk_yuck(iks *node) 00284 { 00285 if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps")) 00286 return 1; 00287 return 0; 00288 } 00289 00290 /*! 00291 * \brief Detects the highest bit in a number. 00292 * \param Number you want to have evaluated. 00293 * \return the highest power of 2 that can go into the number. 00294 */ 00295 static int aji_highest_bit(int number) 00296 { 00297 int x = sizeof(number) * 8 - 1; 00298 if (!number) 00299 return 0; 00300 for (; x > 0; x--) { 00301 if (number & (1 << x)) 00302 break; 00303 } 00304 return (1 << x); 00305 } 00306 00307 static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid) 00308 { 00309 iks *x, *y; 00310 x = iks_new("iq"); 00311 iks_insert_attrib(x, "type", "set"); 00312 y = iks_insert(x, "query"); 00313 iks_insert_attrib(y, "xmlns", IKS_NS_AUTH); 00314 iks_insert_cdata(iks_insert(y, "username"), id->user, 0); 00315 iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0); 00316 if (sid) { 00317 char buf[41]; 00318 char sidpass[100]; 00319 snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass); 00320 ast_sha1_hash(buf, sidpass); 00321 iks_insert_cdata(iks_insert(y, "digest"), buf, 0); 00322 } else { 00323 iks_insert_cdata(iks_insert(y, "password"), pass, 0); 00324 } 00325 return x; 00326 } 00327 00328 /*! 00329 * \brief Dial plan function status(). puts the status of watched user 00330 into a channel variable. 00331 * \param channel, and username,watched user, status var 00332 * \return 0. 00333 */ 00334 static int aji_status_exec(struct ast_channel *chan, void *data) 00335 { 00336 struct aji_client *client = NULL; 00337 struct aji_buddy *buddy = NULL; 00338 struct aji_resource *r = NULL; 00339 char *s = NULL, *sender = NULL, *jid = NULL, *screenname = NULL, *resource = NULL, *variable = NULL; 00340 int stat = 7; 00341 char status[2]; 00342 if (data) { 00343 s = ast_strdupa((char *) data); 00344 if (s) { 00345 sender = strsep(&s, "|"); 00346 if (sender && (sender[0] != '\0')) { 00347 jid = strsep(&s, "|"); 00348 if (jid && (jid[0] != '\0')) { 00349 variable = s; 00350 } else { 00351 ast_log(LOG_ERROR, "Bad arguments\n"); 00352 return -1; 00353 } 00354 } 00355 } 00356 } else { 00357 ast_log(LOG_ERROR, "Out of memory\n"); 00358 return -1; 00359 } 00360 00361 if(!strchr(jid, '/')) { 00362 resource = NULL; 00363 } else { 00364 screenname = strsep(&jid, "/"); 00365 resource = jid; 00366 } 00367 client = ast_aji_get_client(sender); 00368 if (!client) { 00369 ast_log(LOG_WARNING, "Could not find Connection.\n"); 00370 return -1; 00371 } 00372 if(!&client->buddies) { 00373 ast_log(LOG_WARNING, "No buddies for connection.\n"); 00374 return -1; 00375 } 00376 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, (resource)?screenname:jid); 00377 if (!buddy) { 00378 ast_log(LOG_WARNING, "Could not find Buddy in list.\n"); 00379 return -1; 00380 } 00381 r = aji_find_resource(buddy, resource); 00382 if(!r && buddy->resources) { 00383 r = buddy->resources; 00384 } 00385 if(!r){ 00386 ast_log(LOG_NOTICE, "Resource %s of buddy %s not found \n", resource, screenname); 00387 } 00388 stat = r->status; 00389 sprintf(status, "%d", stat); 00390 pbx_builtin_setvar_helper(chan, variable, status); 00391 return 0; 00392 } 00393 00394 /*! 00395 * \brief Dial plan function to send a message. 00396 * \param channel, and data, data is sender, reciever, message. 00397 * \return 0. 00398 */ 00399 static int aji_send_exec(struct ast_channel *chan, void *data) 00400 { 00401 struct aji_client *client = NULL; 00402 00403 char *s = NULL, *sender = NULL, *recipient = NULL, *message = NULL; 00404 00405 if (!data) { 00406 ast_log(LOG_ERROR, "Out of memory\n"); 00407 return -1; 00408 } 00409 s = ast_strdupa((char *) data); 00410 if (s) { 00411 sender = strsep(&s, "|"); 00412 if (sender && (sender[0] != '\0')) { 00413 recipient = strsep(&s, "|"); 00414 if (recipient && (recipient[0] != '\0')) { 00415 message = s; 00416 } else { 00417 ast_log(LOG_ERROR, "Bad arguments \n"); 00418 return -1; 00419 } 00420 } 00421 } 00422 if (!(client = ast_aji_get_client(sender))) { 00423 ast_log(LOG_WARNING, "Could not find Sender.\n"); 00424 return -1; 00425 } 00426 if (strchr(recipient, '@') && message) 00427 ast_aji_send(client, recipient, message); 00428 return 0; 00429 } 00430 00431 /*! 00432 * \brief the debug loop. 00433 * \param aji_client structure, xml data as string, size of string, direction of packet, 1 for inbound 0 for outbound. 00434 */ 00435 static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming) 00436 { 00437 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00438 manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s", client->name, xmpp); 00439 00440 if (client->debug) { 00441 if (is_incoming) 00442 ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp); 00443 else { 00444 if( strlen(xmpp) == 1) { 00445 if(option_debug > 2 && xmpp[0] == ' ') 00446 ast_verbose("\nJABBER: Keep alive packet\n"); 00447 } else 00448 ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp); 00449 } 00450 00451 } 00452 ASTOBJ_UNREF(client, aji_client_destroy); 00453 } 00454 00455 /*! 00456 * \brief The action hook parses the inbound packets, constantly running. 00457 * \param aji client structure, type of packet, the actual packet. 00458 * \return IKS_OK or IKS_HOOK . 00459 */ 00460 static int aji_act_hook(void *data, int type, iks *node) 00461 { 00462 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00463 ikspak *pak = NULL; 00464 iks *auth = NULL; 00465 00466 if(!node) { 00467 ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */ 00468 ASTOBJ_UNREF(client, aji_client_destroy); 00469 return IKS_HOOK; 00470 } 00471 00472 pak = iks_packet(node); 00473 00474 if (!client->component) { /*client */ 00475 switch (type) { 00476 case IKS_NODE_START: 00477 if (client->usetls && !iks_is_secure(client->p)) { 00478 if (iks_has_tls()) 00479 iks_start_tls(client->p); 00480 else 00481 ast_log(LOG_ERROR, "gnuTLS not installed.\n"); 00482 break; 00483 } 00484 if (!client->usesasl) { 00485 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); 00486 auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id")); 00487 if (auth) { 00488 iks_insert_attrib(auth, "id", client->mid); 00489 iks_insert_attrib(auth, "to", client->jid->server); 00490 ast_aji_increment_mid(client->mid); 00491 iks_send(client->p, auth); 00492 iks_delete(auth); 00493 } else 00494 ast_log(LOG_ERROR, "Out of memory.\n"); 00495 } 00496 break; 00497 00498 case IKS_NODE_NORMAL: 00499 { 00500 int features = 0; 00501 if (!strcmp("stream:features", iks_name(node))) { 00502 features = iks_stream_features(node); 00503 if (client->usesasl) { 00504 if (client->usetls && !iks_is_secure(client->p)) 00505 break; 00506 if (client->authorized) { 00507 if (features & IKS_STREAM_BIND) { 00508 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); 00509 auth = iks_make_resource_bind(client->jid); 00510 if (auth) { 00511 iks_insert_attrib(auth, "id", client->mid); 00512 ast_aji_increment_mid(client->mid); 00513 iks_send(client->p, auth); 00514 iks_delete(auth); 00515 } else { 00516 ast_log(LOG_ERROR, "Out of memory.\n"); 00517 break; 00518 } 00519 } 00520 if (features & IKS_STREAM_SESSION) { 00521 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); 00522 auth = iks_make_session(); 00523 if (auth) { 00524 iks_insert_attrib(auth, "id", "auth"); 00525 ast_aji_increment_mid(client->mid); 00526 iks_send(client->p, auth); 00527 iks_delete(auth); 00528 } else { 00529 ast_log(LOG_ERROR, "Out of memory.\n"); 00530 } 00531 } 00532 } else { 00533 features = aji_highest_bit(features); 00534 if (features == IKS_STREAM_SASL_MD5) 00535 iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, client->jid->user, client->password); 00536 else { 00537 if (features == IKS_STREAM_SASL_PLAIN) { 00538 iks *x = NULL; 00539 x = iks_new("auth"); 00540 if (x) { 00541 int len = strlen(client->jid->user) + strlen(client->password) + 3; 00542 /* XXX Check return values XXX */ 00543 char *s = ast_malloc(80 + len); 00544 char *base64 = ast_malloc(80 + len * 2); 00545 00546 iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL); 00547 iks_insert_attrib(x, "mechanism", "PLAIN"); 00548 sprintf(s, "%c%s%c%s", 0, client->jid->user, 0, client->password); 00549 ast_base64encode(base64, (const unsigned char *) s, len, len * 2); 00550 iks_insert_cdata(x, base64, 0); 00551 iks_send(client->p, x); 00552 iks_delete(x); 00553 if (base64) 00554 free(base64); 00555 if (s) 00556 free(s); 00557 } else { 00558 ast_log(LOG_ERROR, "Out of memory.\n"); 00559 } 00560 } 00561 } 00562 } 00563 } 00564 } else if (!strcmp("failure", iks_name(node))) { 00565 ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n"); 00566 } else if (!strcmp("success", iks_name(node))) { 00567 client->authorized = 1; 00568 iks_send_header(client->p, client->jid->server); 00569 } 00570 break; 00571 } 00572 case IKS_NODE_ERROR: 00573 ast_log(LOG_ERROR, "JABBER: Node Error\n"); 00574 ASTOBJ_UNREF(client, aji_client_destroy); 00575 return IKS_HOOK; 00576 break; 00577 case IKS_NODE_STOP: 00578 ast_log(LOG_WARNING, "JABBER: Disconnected\n"); 00579 ASTOBJ_UNREF(client, aji_client_destroy); 00580 return IKS_HOOK; 00581 break; 00582 } 00583 } else if (client->state != AJI_CONNECTED && client->component) { 00584 switch (type) { 00585 case IKS_NODE_START: 00586 if (client->state == AJI_DISCONNECTED) { 00587 char secret[160], shasum[320], *handshake; 00588 00589 sprintf(secret, "%s%s", pak->id, client->password); 00590 ast_sha1_hash(shasum, secret); 00591 handshake = NULL; 00592 asprintf(&handshake, "<handshake>%s</handshake>", shasum); 00593 if (handshake) { 00594 iks_send_raw(client->p, handshake); 00595 free(handshake); 00596 handshake = NULL; 00597 } 00598 client->state = AJI_CONNECTING; 00599 if(iks_recv(client->p,1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/ 00600 client->state = AJI_CONNECTED; 00601 else 00602 ast_log(LOG_WARNING,"Jabber didn't seem to handshake, failed to authenicate.\n"); 00603 break; 00604 } 00605 break; 00606 00607 case IKS_NODE_NORMAL: 00608 break; 00609 00610 case IKS_NODE_ERROR: 00611 ast_log(LOG_ERROR, "JABBER: Node Error\n"); 00612 ASTOBJ_UNREF(client, aji_client_destroy); 00613 return IKS_HOOK; 00614 00615 case IKS_NODE_STOP: 00616 ast_log(LOG_WARNING, "JABBER: Disconnected\n"); 00617 ASTOBJ_UNREF(client, aji_client_destroy); 00618 return IKS_HOOK; 00619 } 00620 } 00621 00622 switch (pak->type) { 00623 case IKS_PAK_NONE: 00624 if (option_debug) 00625 ast_log(LOG_DEBUG, "JABBER: I Don't know what to do with you NONE\n"); 00626 break; 00627 case IKS_PAK_MESSAGE: 00628 aji_handle_message(client, pak); 00629 if (option_debug) 00630 ast_log(LOG_DEBUG, "JABBER: I Don't know what to do with you MESSAGE\n"); 00631 break; 00632 case IKS_PAK_PRESENCE: 00633 aji_handle_presence(client, pak); 00634 if (option_debug) 00635 ast_log(LOG_DEBUG, "JABBER: I Do know how to handle presence!!\n"); 00636 break; 00637 case IKS_PAK_S10N: 00638 aji_handle_subscribe(client, pak); 00639 if (option_debug) 00640 ast_log(LOG_DEBUG, "JABBER: I Dont know S10N subscribe!!\n"); 00641 break; 00642 case IKS_PAK_IQ: 00643 if (option_debug) 00644 ast_log(LOG_DEBUG, "JABBER: I Dont have an IQ!!!\n"); 00645 aji_handle_iq(client, node); 00646 break; 00647 default: 00648 if (option_debug) 00649 ast_log(LOG_DEBUG, "JABBER: I Dont know %i\n", pak->type); 00650 break; 00651 } 00652 00653 iks_filter_packet(client->f, pak); 00654 00655 if (node) 00656 iks_delete(node); 00657 00658 ASTOBJ_UNREF(client, aji_client_destroy); 00659 return IKS_OK; 00660 } 00661 00662 static int aji_register_approve_handler(void *data, ikspak *pak) 00663 { 00664 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00665 iks *iq = NULL, *presence = NULL, *x = NULL; 00666 00667 iq = iks_new("iq"); 00668 presence = iks_new("presence"); 00669 x = iks_new("x"); 00670 if (client && iq && presence && x) { 00671 if (!iks_find(pak->query, "remove")) { 00672 iks_insert_attrib(iq, "from", client->jid->full); 00673 iks_insert_attrib(iq, "to", pak->from->full); 00674 iks_insert_attrib(iq, "id", pak->id); 00675 iks_insert_attrib(iq, "type", "result"); 00676 iks_send(client->p, iq); 00677 00678 iks_insert_attrib(presence, "from", client->jid->full); 00679 iks_insert_attrib(presence, "to", pak->from->partial); 00680 iks_insert_attrib(presence, "id", client->mid); 00681 ast_aji_increment_mid(client->mid); 00682 iks_insert_attrib(presence, "type", "subscribe"); 00683 iks_insert_attrib(x, "xmlns", "vcard-temp:x:update"); 00684 iks_insert_node(presence, x); 00685 iks_send(client->p, presence); 00686 } 00687 } else { 00688 ast_log(LOG_ERROR, "Out of memory.\n"); 00689 } 00690 00691 if (iq) 00692 iks_delete(iq); 00693 if(presence) 00694 iks_delete(presence); 00695 if (x) 00696 iks_delete(x); 00697 ASTOBJ_UNREF(client, aji_client_destroy); 00698 return IKS_FILTER_EAT; 00699 } 00700 00701 static int aji_register_query_handler(void *data, ikspak *pak) 00702 { 00703 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00704 struct aji_buddy *buddy = NULL; 00705 char *node = NULL; 00706 00707 client = (struct aji_client *) data; 00708 00709 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial); 00710 if (!buddy) { 00711 iks *iq = NULL, *query = NULL, *error = NULL, *notacceptable = NULL; 00712 00713 ast_verbose("Someone.... %s tried to register but they aren't allowed\n", pak->from->partial); 00714 iq = iks_new("iq"); 00715 query = iks_new("query"); 00716 error = iks_new("error"); 00717 notacceptable = iks_new("not-acceptable"); 00718 if(iq && query && error && notacceptable) { 00719 iks_insert_attrib(iq, "type", "error"); 00720 iks_insert_attrib(iq, "from", client->user); 00721 iks_insert_attrib(iq, "to", pak->from->full); 00722 iks_insert_attrib(iq, "id", pak->id); 00723 iks_insert_attrib(query, "xmlns", "jabber:iq:register"); 00724 iks_insert_attrib(error, "code" , "406"); 00725 iks_insert_attrib(error, "type", "modify"); 00726 iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); 00727 iks_insert_node(iq, query); 00728 iks_insert_node(iq, error); 00729 iks_insert_node(error, notacceptable); 00730 iks_send(client->p, iq); 00731 } else { 00732 ast_log(LOG_ERROR, "Out of memory.\n"); 00733 } 00734 if (iq) 00735 iks_delete(iq); 00736 if (query) 00737 iks_delete(query); 00738 if (error) 00739 iks_delete(error); 00740 if (notacceptable) 00741 iks_delete(notacceptable); 00742 } else if (!(node = iks_find_attrib(pak->query, "node"))) { 00743 iks *iq = NULL, *query = NULL, *instructions = NULL; 00744 char *explain = "Welcome to Asterisk - the Open Source PBX.\n"; 00745 iq = iks_new("iq"); 00746 query = iks_new("query"); 00747 instructions = iks_new("instructions"); 00748 if (iq && query && instructions && client) { 00749 iks_insert_attrib(iq, "from", client->user); 00750 iks_insert_attrib(iq, "to", pak->from->full); 00751 iks_insert_attrib(iq, "id", pak->id); 00752 iks_insert_attrib(iq, "type", "result"); 00753 iks_insert_attrib(query, "xmlns", "jabber:iq:register"); 00754 iks_insert_cdata(instructions, explain, 0); 00755 iks_insert_node(iq, query); 00756 iks_insert_node(query, instructions); 00757 iks_send(client->p, iq); 00758 } else { 00759 ast_log(LOG_ERROR, "Out of memory.\n"); 00760 } 00761 if (iq) 00762 iks_delete(iq); 00763 if (query) 00764 iks_delete(query); 00765 if (instructions) 00766 iks_delete(instructions); 00767 } 00768 ASTOBJ_UNREF(client, aji_client_destroy); 00769 return IKS_FILTER_EAT; 00770 } 00771 00772 static int aji_ditems_handler(void *data, ikspak *pak) 00773 { 00774 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00775 char *node = NULL; 00776 00777 if (!(node = iks_find_attrib(pak->query, "node"))) { 00778 iks *iq = NULL, *query = NULL, *item = NULL; 00779 iq = iks_new("iq"); 00780 query = iks_new("query"); 00781 item = iks_new("item"); 00782 00783 if (iq && query && item) { 00784 iks_insert_attrib(iq, "from", client->user); 00785 iks_insert_attrib(iq, "to", pak->from->full); 00786 iks_insert_attrib(iq, "id", pak->id); 00787 iks_insert_attrib(iq, "type", "result"); 00788 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items"); 00789 iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands"); 00790 iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands"); 00791 iks_insert_attrib(item, "jid", client->user); 00792 00793 iks_insert_node(iq, query); 00794 iks_insert_node(query, item); 00795 iks_send(client->p, iq); 00796 } else { 00797 ast_log(LOG_ERROR, "Out of memory.\n"); 00798 } 00799 if (iq) 00800 iks_delete(iq); 00801 if (query) 00802 iks_delete(query); 00803 if (item) 00804 iks_delete(item); 00805 00806 } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) { 00807 iks *iq, *query, *confirm; 00808 iq = iks_new("iq"); 00809 query = iks_new("query"); 00810 confirm = iks_new("item"); 00811 if (iq && query && confirm && client) { 00812 iks_insert_attrib(iq, "from", client->user); 00813 iks_insert_attrib(iq, "to", pak->from->full); 00814 iks_insert_attrib(iq, "id", pak->id); 00815 iks_insert_attrib(iq, "type", "result"); 00816 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items"); 00817 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands"); 00818 iks_insert_attrib(confirm, "node", "confirmaccount"); 00819 iks_insert_attrib(confirm, "name", "Confirm AIM account"); 00820 iks_insert_attrib(confirm, "jid", "blog.astjab.org"); 00821 00822 iks_insert_node(iq, query); 00823 iks_insert_node(query, confirm); 00824 iks_send(client->p, iq); 00825 } else { 00826 ast_log(LOG_ERROR, "Out of memory.\n"); 00827 } 00828 if (iq) 00829 iks_delete(iq); 00830 if (query) 00831 iks_delete(query); 00832 if (confirm) 00833 iks_delete(confirm); 00834 00835 } else if (!strcasecmp(node, "confirmaccount")) { 00836 iks *iq = NULL, *query = NULL, *feature = NULL; 00837 00838 iq = iks_new("iq"); 00839 query = iks_new("query"); 00840 feature = iks_new("feature"); 00841 00842 if (iq && query && feature && client) { 00843 iks_insert_attrib(iq, "from", client->user); 00844 iks_insert_attrib(iq, "to", pak->from->full); 00845 iks_insert_attrib(iq, "id", pak->id); 00846 iks_insert_attrib(iq, "type", "result"); 00847 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items"); 00848 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands"); 00849 iks_insert_node(iq, query); 00850 iks_insert_node(query, feature); 00851 iks_send(client->p, iq); 00852 } else { 00853 ast_log(LOG_ERROR, "Out of memory.\n"); 00854 } 00855 if (iq) 00856 iks_delete(iq); 00857 if (query) 00858 iks_delete(query); 00859 if (feature) 00860 iks_delete(feature); 00861 } 00862 00863 ASTOBJ_UNREF(client, aji_client_destroy); 00864 return IKS_FILTER_EAT; 00865 00866 } 00867 00868 static int aji_client_info_handler(void *data, ikspak *pak) 00869 { 00870 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00871 struct aji_resource *resource = NULL; 00872 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial); 00873 00874 resource = aji_find_resource(buddy, pak->from->resource); 00875 if (pak->subtype == IKS_TYPE_RESULT) { 00876 if (!resource) { 00877 ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full); 00878 ASTOBJ_UNREF(client, aji_client_destroy); 00879 return IKS_FILTER_EAT; 00880 } 00881 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) { 00882 resource->cap->jingle = 1; 00883 } else 00884 resource->cap->jingle = 0; 00885 } else if (pak->subtype == IKS_TYPE_GET) { 00886 iks *iq, *disco, *ident, *google, *query; 00887 iq = iks_new("iq"); 00888 query = iks_new("query"); 00889 ident = iks_new("identity"); 00890 disco = iks_new("feature"); 00891 google = iks_new("feature"); 00892 if (iq && ident && disco && google) { 00893 iks_insert_attrib(iq, "from", client->jid->full); 00894 iks_insert_attrib(iq, "to", pak->from->full); 00895 iks_insert_attrib(iq, "type", "result"); 00896 iks_insert_attrib(iq, "id", pak->id); 00897 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info"); 00898 iks_insert_attrib(ident, "category", "client"); 00899 iks_insert_attrib(ident, "type", "pc"); 00900 iks_insert_attrib(ident, "name", "asterisk"); 00901 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info"); 00902 iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1"); 00903 iks_insert_node(iq, query); 00904 iks_insert_node(query, ident); 00905 iks_insert_node(query, google); 00906 iks_insert_node(query, disco); 00907 iks_send(client->p, iq); 00908 } else 00909 ast_log(LOG_ERROR, "Out of Memory.\n"); 00910 if (iq) 00911 iks_delete(iq); 00912 if (query) 00913 iks_delete(query); 00914 if (ident) 00915 iks_delete(ident); 00916 if (google) 00917 iks_delete(google); 00918 if (disco) 00919 iks_delete(disco); 00920 } else if (pak->subtype == IKS_TYPE_ERROR) { 00921 ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full); 00922 } 00923 ASTOBJ_UNREF(client, aji_client_destroy); 00924 return IKS_FILTER_EAT; 00925 } 00926 00927 static int aji_dinfo_handler(void *data, ikspak *pak) 00928 { 00929 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 00930 char *node = NULL; 00931 struct aji_resource *resource = NULL; 00932 struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial); 00933 00934 resource = aji_find_resource(buddy, pak->from->resource); 00935 if (pak->subtype == IKS_TYPE_ERROR) { 00936 ast_log(LOG_WARNING, "Recieved error from a client, turn on jabber debug!\n"); 00937 return IKS_FILTER_EAT; 00938 } 00939 if (pak->subtype == IKS_TYPE_RESULT) { 00940 if (!resource) { 00941 ast_log(LOG_NOTICE,"JABBER: Received client info from %s when not requested.\n", pak->from->full); 00942 ASTOBJ_UNREF(client, aji_client_destroy); 00943 return IKS_FILTER_EAT; 00944 } 00945 if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) { 00946 resource->cap->jingle = 1; 00947 } else 00948 resource->cap->jingle = 0; 00949 } else if (pak->subtype == IKS_TYPE_GET && !(node = iks_find_attrib(pak->query, "node"))) { 00950 iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search; 00951 00952 iq = iks_new("iq"); 00953 query = iks_new("query"); 00954 identity = iks_new("identity"); 00955 disco = iks_new("feature"); 00956 reg = iks_new("feature"); 00957 commands = iks_new("feature"); 00958 gateway = iks_new("feature"); 00959 version = iks_new("feature"); 00960 vcard = iks_new("feature"); 00961 search = iks_new("feature"); 00962 00963 if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) { 00964 iks_insert_attrib(iq, "from", client->user); 00965 iks_insert_attrib(iq, "to", pak->from->full); 00966 iks_insert_attrib(iq, "id", pak->id); 00967 iks_insert_attrib(iq, "type", "result"); 00968 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info"); 00969 iks_insert_attrib(identity, "category", "gateway"); 00970 iks_insert_attrib(identity, "type", "pstn"); 00971 iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX"); 00972 iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco"); 00973 iks_insert_attrib(reg, "var", "jabber:iq:register"); 00974 iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands"); 00975 iks_insert_attrib(gateway, "var", "jabber:iq:gateway"); 00976 iks_insert_attrib(version, "var", "jabber:iq:version"); 00977 iks_insert_attrib(vcard, "var", "vcard-temp"); 00978 iks_insert_attrib(search, "var", "jabber:iq:search"); 00979 00980 iks_insert_node(iq, query); 00981 iks_insert_node(query, identity); 00982 iks_insert_node(query, disco); 00983 iks_insert_node(query, reg); 00984 iks_insert_node(query, commands); 00985 iks_insert_node(query, gateway); 00986 iks_insert_node(query, version); 00987 iks_insert_node(query, vcard); 00988 iks_insert_node(query, search); 00989 iks_send(client->p, iq); 00990 } else { 00991 ast_log(LOG_ERROR, "Out of memory.\n"); 00992 } 00993 00994 if (iq) 00995 iks_delete(iq); 00996 if (query) 00997 iks_delete(query); 00998 if (identity) 00999 iks_delete(identity); 01000 if (disco) 01001 iks_delete(disco); 01002 if (reg) 01003 iks_delete(reg); 01004 if (commands) 01005 iks_delete(commands); 01006 if (gateway) 01007 iks_delete(gateway); 01008 if (version) 01009 iks_delete(version); 01010 if (vcard) 01011 iks_delete(vcard); 01012 if (search) 01013 iks_delete(search); 01014 01015 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "http://jabber.org/protocol/commands")) { 01016 iks *iq, *query, *confirm; 01017 iq = iks_new("iq"); 01018 query = iks_new("query"); 01019 confirm = iks_new("item"); 01020 01021 if (iq && query && confirm && client) { 01022 iks_insert_attrib(iq, "from", client->user); 01023 iks_insert_attrib(iq, "to", pak->from->full); 01024 iks_insert_attrib(iq, "id", pak->id); 01025 iks_insert_attrib(iq, "type", "result"); 01026 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items"); 01027 iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands"); 01028 iks_insert_attrib(confirm, "node", "confirmaccount"); 01029 iks_insert_attrib(confirm, "name", "Confirm AIM account"); 01030 iks_insert_attrib(confirm, "jid", client->user); 01031 iks_insert_node(iq, query); 01032 iks_insert_node(query, confirm); 01033 iks_send(client->p, iq); 01034 } else { 01035 ast_log(LOG_ERROR, "Out of memory.\n"); 01036 } 01037 if (iq) 01038 iks_delete(iq); 01039 if (query) 01040 iks_delete(query); 01041 if (confirm) 01042 iks_delete(confirm); 01043 01044 } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "confirmaccount")) { 01045 iks *iq, *query, *feature; 01046 01047 iq = iks_new("iq"); 01048 query = iks_new("query"); 01049 feature = iks_new("feature"); 01050 01051 if (iq && query && feature && client) { 01052 iks_insert_attrib(iq, "from", client->user); 01053 iks_insert_attrib(iq, "to", pak->from->full); 01054 iks_insert_attrib(iq, "id", pak->id); 01055 iks_insert_attrib(iq, "type", "result"); 01056 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info"); 01057 iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands"); 01058 iks_insert_node(iq, query); 01059 iks_insert_node(query, feature); 01060 iks_send(client->p, iq); 01061 } else { 01062 ast_log(LOG_ERROR, "Out of memory.\n"); 01063 } 01064 if (iq) 01065 iks_delete(iq); 01066 if (query) 01067 iks_delete(query); 01068 if (feature) 01069 iks_delete(feature); 01070 } 01071 01072 ASTOBJ_UNREF(client, aji_client_destroy); 01073 return IKS_FILTER_EAT; 01074 } 01075 01076 /*! 01077 * \brief Handles <iq> tags. 01078 * \param client structure and the iq node. 01079 * \return void. 01080 */ 01081 static void aji_handle_iq(struct aji_client *client, iks *node) 01082 { 01083 /*Nothing to see here */ 01084 } 01085 01086 /*! 01087 * \brief Handles presence packets. 01088 * \param client structure and the node. 01089 * \return void. 01090 */ 01091 static void aji_handle_message(struct aji_client *client, ikspak *pak) 01092 { 01093 struct aji_message *insert, *tmp; 01094 int flag = 0; 01095 01096 if (!(insert = ast_calloc(1, sizeof(struct aji_message)))) 01097 return; 01098 time(&insert->arrived); 01099 if (iks_find_cdata(pak->x, "body")) 01100 insert->message = ast_strdup(iks_find_cdata(pak->x, "body")); 01101 if(pak->id) 01102 ast_copy_string(insert->id, pak->id, sizeof(insert->message)); 01103 if (pak->from) 01104 insert->from = ast_strdup(pak->from->full); 01105 AST_LIST_LOCK(&client->messages); 01106 AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) { 01107 if (flag) { 01108 AST_LIST_REMOVE_CURRENT(&client->messages, list); 01109 if (tmp->from) 01110 free(tmp->from); 01111 if (tmp->message) 01112 free(tmp->message); 01113 } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) { 01114 flag = 1; 01115 AST_LIST_REMOVE_CURRENT(&client->messages, list); 01116 if (tmp->from) 01117 free(tmp->from); 01118 if (tmp->message) 01119 free(tmp->message); 01120 } 01121 } 01122 AST_LIST_TRAVERSE_SAFE_END; 01123 AST_LIST_INSERT_HEAD(&client->messages, insert, list); 01124 AST_LIST_UNLOCK(&client->messages); 01125 } 01126 01127 static void aji_handle_presence(struct aji_client *client, ikspak *pak) 01128 { 01129 int status, priority; 01130 struct aji_buddy *buddy; 01131 struct aji_resource *tmp = NULL, *last = NULL, *found = NULL; 01132 char *ver, *node, *descrip, *type; 01133 01134 if(client->state != AJI_CONNECTED) 01135 aji_create_buddy(pak->from->partial, client); 01136 01137 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial); 01138 if (!buddy) { 01139 ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial); 01140 return; 01141 } 01142 type = iks_find_attrib(pak->x, "type"); 01143 if(client->component && type &&!strcasecmp("probe", type)) { 01144 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), 1, client->statusmessage); 01145 ast_verbose("what i was looking for \n"); 01146 } 01147 ASTOBJ_WRLOCK(buddy); 01148 status = (pak->show) ? pak->show : 6; 01149 priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0"); 01150 tmp = buddy->resources; 01151 descrip = ast_strdup(iks_find_cdata(pak->x,"status")); 01152 01153 while (tmp) { 01154 if (!strcasecmp(tmp->resource, pak->from->resource)) { 01155 tmp->status = status; 01156 if (tmp->description) free(tmp->description); 01157 tmp->description = descrip; 01158 found = tmp; 01159 if (status == 6) { /* Sign off Destroy resource */ 01160 if (last && found->next) { 01161 last->next = found->next; 01162 } else if (!last) { 01163 if (found->next) 01164 buddy->resources = found->next; 01165 else 01166 buddy->resources = NULL; 01167 } else if (!found->next) { 01168 if (last) 01169 last->next = NULL; 01170 else 01171 buddy->resources = NULL; 01172 } 01173 free(found); 01174 found = NULL; 01175 break; 01176 } 01177 if (tmp->priority != priority) { 01178 found->priority = priority; 01179 if (!last && !found->next) 01180 break; 01181 if (last) 01182 last->next = found->next; 01183 else 01184 buddy->resources = found->next; 01185 last = NULL; 01186 tmp = buddy->resources; 01187 if (!buddy->resources) 01188 buddy->resources = found; 01189 while (tmp) { 01190 if (found->priority > tmp->priority) { 01191 if (last) 01192 last->next = found; 01193 found->next = tmp; 01194 if (!last) 01195 buddy->resources = found; 01196 break; 01197 } 01198 if (!tmp->next) { 01199 tmp->next = found; 01200 break; 01201 } 01202 last = tmp; 01203 tmp = tmp->next; 01204 } 01205 } 01206 break; 01207 } 01208 last = tmp; 01209 tmp = tmp->next; 01210 } 01211 01212 if (!found && status != 6) { 01213 found = (struct aji_resource *) malloc(sizeof(struct aji_resource)); 01214 memset(found, 0, sizeof(struct aji_resource)); 01215 01216 if (!found) { 01217 ast_log(LOG_ERROR, "Out of memory!\n"); 01218 return; 01219 } 01220 ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource)); 01221 found->status = status; 01222 found->description = descrip; 01223 found->priority = priority; 01224 found->next = NULL; 01225 last = NULL; 01226 tmp = buddy->resources; 01227 while (tmp) { 01228 if (found->priority > tmp->priority) { 01229 if (last) 01230 last->next = found; 01231 found->next = tmp; 01232 if (!last) 01233 buddy->resources = found; 01234 break; 01235 } 01236 if (!tmp->next) { 01237 tmp->next = found; 01238 break; 01239 } 01240 last = tmp; 01241 tmp = tmp->next; 01242 } 01243 if (!tmp) 01244 buddy->resources = found; 01245 } 01246 ASTOBJ_UNLOCK(buddy); 01247 ASTOBJ_UNREF(buddy, aji_buddy_destroy); 01248 01249 node = iks_find_attrib(iks_find(pak->x, "c"), "node"); 01250 ver = iks_find_attrib(iks_find(pak->x, "c"), "ver"); 01251 01252 if(status !=6 && !found->cap) { 01253 found->cap = aji_find_version(node, ver, pak); 01254 if(gtalk_yuck(pak->x)) /* gtalk should do discover */ 01255 found->cap->jingle = 1; 01256 if(found->cap->jingle && option_debug > 4) { 01257 if (option_debug) 01258 ast_log(LOG_DEBUG,"Special case for google till they support discover.\n"); 01259 } 01260 else { 01261 iks *iq, *query; 01262 iq = iks_new("iq"); 01263 query = iks_new("query"); 01264 if(query && iq) { 01265 iks_insert_attrib(iq, "type", "get"); 01266 iks_insert_attrib(iq, "to", pak->from->full); 01267 iks_insert_attrib(iq,"from",iks_find_attrib(pak->x,"to")); 01268 iks_insert_attrib(iq, "id", client->mid); 01269 ast_aji_increment_mid(client->mid); 01270 iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info"); 01271 iks_insert_node(iq, query); 01272 iks_send(client->p, iq); 01273 01274 } else 01275 ast_log(LOG_ERROR, "Out of memory.\n"); 01276 if(query) 01277 iks_delete(query); 01278 if(iq) 01279 iks_delete(iq); 01280 } 01281 } 01282 if (option_verbose > 4) { 01283 switch (pak->subtype) { 01284 case IKS_TYPE_AVAILABLE: 01285 ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am available ^_* %i\n", pak->subtype); 01286 break; 01287 case IKS_TYPE_UNAVAILABLE: 01288 ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am unavailable ^_* %i\n", pak->subtype); 01289 break; 01290 default: 01291 ast_verbose(VERBOSE_PREFIX_3 "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype); 01292 } 01293 switch (pak->show) { 01294 case IKS_SHOW_UNAVAILABLE: 01295 ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show); 01296 break; 01297 case IKS_SHOW_AVAILABLE: 01298 ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is available\n"); 01299 break; 01300 case IKS_SHOW_CHAT: 01301 ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show); 01302 break; 01303 case IKS_SHOW_AWAY: 01304 ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is away\n"); 01305 break; 01306 case IKS_SHOW_XA: 01307 ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show); 01308 break; 01309 case IKS_SHOW_DND: 01310 ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show); 01311 break; 01312 default: 01313 ast_verbose(VERBOSE_PREFIX_3 "JABBER: Kinky! how did that happen %i\n", pak->show); 01314 } 01315 } 01316 } 01317 01318 /*! 01319 * \brief handles subscription requests. 01320 * \param aji_client struct and xml packet. 01321 * \return void. 01322 */ 01323 static void aji_handle_subscribe(struct aji_client *client, ikspak *pak) 01324 { 01325 if(pak->subtype == IKS_TYPE_SUBSCRIBE) { 01326 iks *presence = NULL, *status = NULL; 01327 presence = iks_new("presence"); 01328 status = iks_new("status"); 01329 if(presence && status) { 01330 iks_insert_attrib(presence, "type", "subscribed"); 01331 iks_insert_attrib(presence, "to", pak->from->full); 01332 iks_insert_attrib(presence, "from", iks_find_attrib(pak->x, "to")); 01333 if(pak->id) 01334 iks_insert_attrib(presence, "id", pak->id); 01335 iks_insert_cdata(status, "Asterisk has approved subscription", 0); 01336 iks_insert_node(presence, status); 01337 iks_send(client->p, presence); 01338 } else 01339 ast_log(LOG_ERROR, "Unable to allocate nodes\n"); 01340 if(presence) 01341 iks_delete(presence); 01342 if(status) 01343 iks_delete(status); 01344 if(client->component) 01345 aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), 1, client->statusmessage); 01346 } 01347 if (option_verbose > 4) { 01348 switch (pak->subtype) { 01349 case IKS_TYPE_SUBSCRIBE: 01350 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype); 01351 break; 01352 case IKS_TYPE_SUBSCRIBED: 01353 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype); 01354 break; 01355 case IKS_TYPE_UNSUBSCRIBE: 01356 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype); 01357 break; 01358 case IKS_TYPE_UNSUBSCRIBED: 01359 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype); 01360 break; 01361 default: /*IKS_TYPE_ERROR: */ 01362 ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype); 01363 break; 01364 } 01365 } 01366 } 01367 01368 /*! 01369 * \brief sends messages. 01370 * \param aji_client struct , reciever, message. 01371 * \return 1. 01372 */ 01373 int ast_aji_send(struct aji_client *client, const char *address, const char *message) 01374 { 01375 int res = 0; 01376 iks *message_packet = NULL; 01377 if (client->state == AJI_CONNECTED) { 01378 message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message); 01379 if (message_packet) { 01380 iks_insert_attrib(message_packet, "from", client->jid->full); 01381 res = iks_send(client->p, message_packet); 01382 } else { 01383 ast_log(LOG_ERROR, "Out of memory.\n"); 01384 } 01385 if (message_packet) 01386 iks_delete(message_packet); 01387 } else 01388 ast_log(LOG_WARNING, "JABBER: Not connected can't send\n"); 01389 return 1; 01390 } 01391 01392 /*! 01393 * \brief create a chatroom. 01394 * \param aji_client struct , room, server, topic for the room. 01395 * \return 0. 01396 */ 01397 int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic) 01398 { 01399 int res = 0; 01400 iks *iq = NULL; 01401 iq = iks_new("iq"); 01402 if (iq && client) { 01403 iks_insert_attrib(iq, "type", "get"); 01404 iks_insert_attrib(iq, "to", server); 01405 iks_insert_attrib(iq, "id", client->mid); 01406 ast_aji_increment_mid(client->mid); 01407 iks_send(client->p, iq); 01408 } else 01409 ast_log(LOG_ERROR, "Out of memory.\n"); 01410 return res; 01411 } 01412 01413 /*! 01414 * \brief join a chatroom. 01415 * \param aji_client struct , room. 01416 * \return res. 01417 */ 01418 int ast_aji_join_chat(struct aji_client *client, char *room) 01419 { 01420 int res = 0; 01421 iks *presence = NULL, *priority = NULL; 01422 presence = iks_new("presence"); 01423 priority = iks_new("priority"); 01424 if (presence && priority && client) { 01425 iks_insert_cdata(priority, "0", 1); 01426 iks_insert_attrib(presence, "to", room); 01427 iks_insert_node(presence, priority); 01428 res = iks_send(client->p, presence); 01429 iks_insert_cdata(priority, "5", 1); 01430 iks_insert_attrib(presence, "to", room); 01431 res = iks_send(client->p, presence); 01432 } else 01433 ast_log(LOG_ERROR, "Out of memory.\n"); 01434 if (presence) 01435 iks_delete(presence); 01436 if (priority) 01437 iks_delete(priority); 01438 return res; 01439 } 01440 01441 /*! 01442 * \brief invite to a chatroom. 01443 * \param aji_client struct ,user, room, message. 01444 * \return res. 01445 */ 01446 int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message) 01447 { 01448 int res = 0; 01449 iks *invite, *body, *namespace; 01450 01451 invite = iks_new("message"); 01452 body = iks_new("body"); 01453 namespace = iks_new("x"); 01454 if (client && invite && body && namespace) { 01455 iks_insert_attrib(invite, "to", user); 01456 iks_insert_attrib(invite, "id", client->mid); 01457 ast_aji_increment_mid(client->mid); 01458 iks_insert_cdata(body, message, 0); 01459 iks_insert_attrib(namespace, "xmlns", "jabber:x:conference"); 01460 iks_insert_attrib(namespace, "jid", room); 01461 iks_insert_node(invite, body); 01462 iks_insert_node(invite, namespace); 01463 res = iks_send(client->p, invite); 01464 } else 01465 ast_log(LOG_ERROR, "Out of memory.\n"); 01466 if (body) 01467 iks_delete(body); 01468 if (namespace) 01469 iks_delete(namespace); 01470 if (invite) 01471 iks_delete(invite); 01472 return res; 01473 } 01474 01475 01476 /*! 01477 * \brief receive message loop. 01478 * \param aji_client struct. 01479 * \return void. 01480 */ 01481 static void *aji_recv_loop(void *data) 01482 { 01483 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 01484 int res = IKS_HOOK; 01485 do { 01486 if (res != IKS_OK) { 01487 while(res != IKS_OK) { 01488 if(option_verbose > 3) 01489 ast_verbose("JABBER: reconnecting.\n"); 01490 res = aji_reconnect(client); 01491 sleep(4); 01492 } 01493 } 01494 01495 res = iks_recv(client->p, 1); 01496 client->timeout--; 01497 if (res == IKS_HOOK) 01498 ast_log(LOG_WARNING, "JABBER: Got hook event.\n"); 01499 else if (res == IKS_NET_TLSFAIL) 01500 ast_log(LOG_WARNING, "JABBER: Failure in TLS.\n"); 01501 else if (client->timeout == 0 && client->state == AJI_CONNECTED) { 01502 res = iks_send_raw(client->p, " "); 01503 if(res == IKS_OK) 01504 client->timeout = 50; 01505 else 01506 ast_log(LOG_WARNING, "JABBER: Network Timeout\n"); 01507 } else if (res == IKS_NET_RWERR) 01508 ast_log(LOG_WARNING, "JABBER: socket read error\n"); 01509 } while (client); 01510 ASTOBJ_UNREF(client, aji_client_destroy); 01511 return 0; 01512 } 01513 01514 /*! 01515 * \brief increments the mid field for messages and other events. 01516 * \param message id. 01517 * \return void. 01518 */ 01519 void ast_aji_increment_mid(char *mid) 01520 { 01521 int i = 0; 01522 01523 for (i = strlen(mid) - 1; i >= 0; i--) { 01524 if (mid[i] != 'z') { 01525 mid[i] = mid[i] + 1; 01526 i = 0; 01527 } else 01528 mid[i] = 'a'; 01529 } 01530 } 01531 01532 01533 /*! 01534 * \brief attempts to register to a transport. 01535 * \param aji_client struct, and xml packet. 01536 * \return IKS_FILTER_EAT. 01537 */ 01538 /*allows for registering to transport , was too sketch and is out for now. */ 01539 /*static int aji_register_transport(void *data, ikspak *pak) 01540 { 01541 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 01542 int res = 0; 01543 struct aji_buddy *buddy = NULL; 01544 iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register"); 01545 01546 if (client && send) { 01547 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, { 01548 ASTOBJ_RDLOCK(iterator); 01549 if (iterator->btype == AJI_TRANS) { 01550 buddy = iterator; 01551 } 01552 ASTOBJ_UNLOCK(iterator); 01553 }); 01554 iks_filter_remove_hook(client->f, aji_register_transport); 01555 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); 01556 iks_insert_attrib(send, "to", buddy->host); 01557 iks_insert_attrib(send, "id", client->mid); 01558 ast_aji_increment_mid(client->mid); 01559 iks_insert_attrib(send, "from", client->user); 01560 res = iks_send(client->p, send); 01561 } else 01562 ast_log(LOG_ERROR, "Out of memory.\n"); 01563 01564 if (send) 01565 iks_delete(send); 01566 ASTOBJ_UNREF(client, aji_client_destroy); 01567 return IKS_FILTER_EAT; 01568 01569 } 01570 */ 01571 /*! 01572 * \brief attempts to register to a transport step 2. 01573 * \param aji_client struct, and xml packet. 01574 * \return IKS_FILTER_EAT. 01575 */ 01576 /* more of the same blob of code, too wonky for now*/ 01577 /* static int aji_register_transport2(void *data, ikspak *pak) 01578 { 01579 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 01580 int res = 0; 01581 struct aji_buddy *buddy = NULL; 01582 01583 iks *regiq = iks_new("iq"); 01584 iks *regquery = iks_new("query"); 01585 iks *reguser = iks_new("username"); 01586 iks *regpass = iks_new("password"); 01587 01588 if (client && regquery && reguser && regpass && regiq) { 01589 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, { 01590 ASTOBJ_RDLOCK(iterator); 01591 if (iterator->btype == AJI_TRANS) 01592 buddy = iterator; ASTOBJ_UNLOCK(iterator); 01593 }); 01594 iks_filter_remove_hook(client->f, aji_register_transport2); 01595 iks_insert_attrib(regiq, "to", buddy->host); 01596 iks_insert_attrib(regiq, "type", "set"); 01597 iks_insert_attrib(regiq, "id", client->mid); 01598 ast_aji_increment_mid(client->mid); 01599 iks_insert_attrib(regiq, "from", client->user); 01600 iks_insert_attrib(regquery, "xmlns", "jabber:iq:register"); 01601 iks_insert_cdata(reguser, buddy->user, 0); 01602 iks_insert_cdata(regpass, buddy->pass, 0); 01603 iks_insert_node(regiq, regquery); 01604 iks_insert_node(regquery, reguser); 01605 iks_insert_node(regquery, regpass); 01606 res = iks_send(client->p, regiq); 01607 } else 01608 ast_log(LOG_ERROR, "Out of memory.\n"); 01609 if (regiq) 01610 iks_delete(regiq); 01611 if (regquery) 01612 iks_delete(regquery); 01613 if (reguser) 01614 iks_delete(reguser); 01615 if (regpass) 01616 iks_delete(regpass); 01617 ASTOBJ_UNREF(client, aji_client_destroy); 01618 return IKS_FILTER_EAT; 01619 } 01620 */ 01621 /*! 01622 * \brief goes through roster and prunes users not needed in list, or adds them accordingly. 01623 * \param aji_client struct. 01624 * \return void. 01625 */ 01626 static void aji_pruneregister(struct aji_client *client) 01627 { 01628 int res = 0; 01629 iks *removeiq = iks_new("iq"); 01630 iks *removequery = iks_new("query"); 01631 iks *removeitem = iks_new("item"); 01632 iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items"); 01633 01634 if (client && removeiq && removequery && removeitem && send) { 01635 iks_insert_node(removeiq, removequery); 01636 iks_insert_node(removequery, removeitem); 01637 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, { 01638 ASTOBJ_RDLOCK(iterator); 01639 /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never 01640 * be called at the same time */ 01641 if (ast_test_flag(iterator, AJI_AUTOPRUNE)) { 01642 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name, 01643 "GoodBye your status is no longer needed by Asterisk the Open Source PBX" 01644 " so I am no longer subscribing to your presence.\n")); 01645 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name, 01646 "GoodBye you are no longer in the asterisk config file so I am removing" 01647 " your access to my presence.\n")); 01648 iks_insert_attrib(removeiq, "from", client->jid->full); 01649 iks_insert_attrib(removeiq, "type", "set"); 01650 iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster"); 01651 iks_insert_attrib(removeitem, "jid", iterator->name); 01652 iks_insert_attrib(removeitem, "subscription", "remove"); 01653 res = iks_send(client->p, removeiq); 01654 } else if (ast_test_flag(iterator, AJI_AUTOREGISTER)) { 01655 res = iks_send(client->p, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name, 01656 "Greetings I am the Asterisk Open Source PBX and I want to subscribe to your presence\n")); 01657 ast_clear_flag(iterator, AJI_AUTOREGISTER); 01658 } 01659 ASTOBJ_UNLOCK(iterator); 01660 }); 01661 } else 01662 ast_log(LOG_ERROR, "Out of memory.\n"); 01663 if (removeiq) 01664 iks_delete(removeiq); 01665 if (removequery) 01666 iks_delete(removequery); 01667 if (removeitem) 01668 iks_delete(removeitem); 01669 if (send) 01670 iks_delete(send); 01671 ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy); 01672 } 01673 01674 /*! 01675 * \brief filters the roster packet we get back from server. 01676 * \param aji_client struct, and xml packet. 01677 * \return IKS_FILTER_EAT. 01678 */ 01679 static int aji_filter_roster(void *data, ikspak *pak) 01680 { 01681 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 01682 int flag = 0; 01683 iks *x = NULL; 01684 struct aji_buddy *buddy; 01685 01686 client->state = AJI_CONNECTED; 01687 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, { 01688 ASTOBJ_RDLOCK(iterator); 01689 x = iks_child(pak->query); 01690 flag = 0; 01691 while (x) { 01692 if (!iks_strcmp(iks_name(x), "item")) { 01693 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) { 01694 flag = 1; 01695 ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER); 01696 } 01697 } 01698 x = iks_next(x); 01699 } 01700 if (!flag) 01701 ast_copy_flags(iterator, client, AJI_AUTOREGISTER); 01702 if (x) 01703 iks_delete(x); 01704 ASTOBJ_UNLOCK(iterator); 01705 }); 01706 01707 x = iks_child(pak->query); 01708 while (x) { 01709 flag = 0; 01710 if (iks_strcmp(iks_name(x), "item") == 0) { 01711 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, { 01712 ASTOBJ_RDLOCK(iterator); 01713 if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) 01714 flag = 1; 01715 ASTOBJ_UNLOCK(iterator); 01716 }); 01717 01718 if (!flag) { 01719 buddy = (struct aji_buddy *) malloc(sizeof(struct aji_buddy)); 01720 if (!buddy) { 01721 ast_log(LOG_WARNING, "Out of memory\n"); 01722 return 0; 01723 } 01724 memset(buddy, 0, sizeof(struct aji_buddy)); 01725 ASTOBJ_INIT(buddy); 01726 ASTOBJ_WRLOCK(buddy); 01727 ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name)); 01728 ast_clear_flag(buddy, AST_FLAGS_ALL); 01729 if(ast_test_flag(client, AJI_AUTOPRUNE)) { 01730 ast_set_flag(buddy, AJI_AUTOPRUNE); 01731 buddy->objflags |= ASTOBJ_FLAG_MARKED; 01732 } else 01733 ast_set_flag(buddy, AJI_AUTOREGISTER); 01734 ASTOBJ_UNLOCK(buddy); 01735 if (buddy) { 01736 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy); 01737 ASTOBJ_UNREF(buddy, aji_buddy_destroy); 01738 } 01739 } 01740 } 01741 x = iks_next(x); 01742 } 01743 if (x) 01744 iks_delete(x); 01745 aji_pruneregister(client); 01746 01747 ASTOBJ_UNREF(client, aji_client_destroy); 01748 return IKS_FILTER_EAT; 01749 } 01750 01751 static int aji_reconnect(struct aji_client *client) 01752 { 01753 int res = 0; 01754 01755 if (client->state) 01756 client->state = AJI_DISCONNECTED; 01757 client->timeout=50; 01758 if (client->p) 01759 iks_parser_reset(client->p); 01760 if (client->authorized) 01761 client->authorized = 0; 01762 01763 if(client->component) 01764 res = aji_component_initialize(client); 01765 else 01766 res = aji_client_initialize(client); 01767 01768 return res; 01769 } 01770 01771 static int aji_get_roster(struct aji_client *client) 01772 { 01773 iks *roster = NULL; 01774 roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER); 01775 if(roster) { 01776 iks_insert_attrib(roster, "id", "roster"); 01777 aji_set_presence(client, NULL, client->jid->full, 1, client->statusmessage); 01778 iks_send(client->p, roster); 01779 } 01780 if (roster) 01781 iks_delete(roster); 01782 return 1; 01783 } 01784 01785 /*! 01786 * \brief connects as a client to jabber server. 01787 * \param aji_client struct, and xml packet. 01788 * \return res. 01789 */ 01790 static int aji_client_connect(void *data, ikspak *pak) 01791 { 01792 struct aji_client *client = ASTOBJ_REF((struct aji_client *) data); 01793 int res = 0; 01794 01795 if (client) { 01796 if (client->state == AJI_DISCONNECTED) { 01797 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); 01798 client->state = AJI_CONNECTING; 01799 client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid; 01800 iks_filter_remove_hook(client->f, aji_client_connect); 01801 if(!client->component) /*client*/ 01802 aji_get_roster(client); 01803 } 01804 } else 01805 ast_log(LOG_ERROR, "Out of memory.\n"); 01806 01807 ASTOBJ_UNREF(client, aji_client_destroy); 01808 return res; 01809 } 01810 01811 /*! 01812 * \brief prepares client for connect. 01813 * \param aji_client struct. 01814 * \return 1. 01815 */ 01816 static int aji_client_initialize(struct aji_client *client) 01817 { 01818 int connected = 0; 01819 01820 connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->jid->server); 01821 01822 if (connected == IKS_NET_NOCONN) { 01823 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n"); 01824 return IKS_HOOK; 01825 } else if (connected == IKS_NET_NODNS) { 01826 ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to %s\n", client->name, S_OR(client->serverhost, client->jid->server)); 01827 return IKS_HOOK; 01828 } else 01829 iks_recv(client->p, 30); 01830 return IKS_OK; 01831 } 01832 01833 /*! 01834 * \brief prepares component for connect. 01835 * \param aji_client struct. 01836 * \return 1. 01837 */ 01838 static int aji_component_initialize(struct aji_client *client) 01839 { 01840 int connected = 1; 01841 connected = iks_connect_via(client->p, client->jid->server, client->port, client->user); 01842 if (connected == IKS_NET_NOCONN) { 01843 ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n"); 01844 return IKS_HOOK; 01845 } else if (connected == IKS_NET_NODNS) { 01846 ast_log(LOG_ERROR, "JABBER ERROR: No DNS\n"); 01847 return IKS_HOOK; 01848 } else if (!connected) 01849 iks_recv(client->p, 30); 01850 return IKS_OK; 01851 } 01852 01853 /*! 01854 * \brief disconnect from jabber server. 01855 * \param aji_client struct. 01856 * \return 1. 01857 */ 01858 int ast_aji_disconnect(struct aji_client *client) 01859 { 01860 if (client) { 01861 if (option_verbose > 3) 01862 ast_verbose(VERBOSE_PREFIX_3 "JABBER: Disconnecting\n"); 01863 iks_disconnect(client->p); 01864 iks_parser_delete(client->p); 01865 ASTOBJ_UNREF(client, aji_client_destroy); 01866 } 01867 01868 return 1; 01869 } 01870 01871 /*! 01872 * \brief set presence of client. 01873 * \param aji_client struct, user to send it to, and from, level, description. 01874 * \return void. 01875 */ 01876 static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc) 01877 { 01878 int res = 0; 01879 iks *presence = iks_make_pres(level, desc); 01880 iks *cnode = iks_new("c"); 01881 iks *priority = iks_new("priority"); 01882 01883 iks_insert_cdata(priority, "0", 1); 01884 if (presence && cnode && client) { 01885 if(to) 01886 iks_insert_attrib(presence, "to", to); 01887 if(from) 01888 iks_insert_attrib(presence, "from", from); 01889 iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps"); 01890 iks_insert_attrib(cnode, "ver", "asterisk-xmpp"); 01891 iks_insert_attrib(cnode, "ext", "voice-v1"); 01892 iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps"); 01893 iks_insert_node(presence, cnode); 01894 res = iks_send(client->p, presence); 01895 } else 01896 ast_log(LOG_ERROR, "Out of memory.\n"); 01897 if (cnode) 01898 iks_delete(cnode); 01899 if (presence) 01900 iks_delete(presence); 01901 } 01902 01903 /*! 01904 * \brief turnon console debugging. 01905 * \param fd, number of args, args. 01906 * \return RESULT_SUCCESS. 01907 */ 01908 static int aji_do_debug(int fd, int argc, char *argv[]) 01909 { 01910 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, { 01911 ASTOBJ_RDLOCK(iterator); 01912 iterator->debug = 1; 01913 ASTOBJ_UNLOCK(iterator); 01914 }); 01915 ast_cli(fd, "Jabber Debugging Enabled.\n"); 01916 return RESULT_SUCCESS; 01917 } 01918 01919 /*! 01920 * \brief reload jabber module. 01921 * \param fd, number of args, args. 01922 * \return RESULT_SUCCESS. 01923 */ 01924 static int aji_do_reload(int fd, int argc, char *argv[]) 01925 { 01926 aji_reload(); 01927 ast_cli(fd, "Jabber Reloaded.\n"); 01928 return RESULT_SUCCESS; 01929 } 01930 01931 /*! 01932 * \brief turnoff console debugging. 01933 * \param fd, number of args, args. 01934 * \return RESULT_SUCCESS. 01935 */ 01936 static int aji_no_debug(int fd, int argc, char *argv[]) 01937 { 01938 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, { 01939 ASTOBJ_RDLOCK(iterator); 01940 iterator->debug = 0; 01941 ASTOBJ_UNLOCK(iterator); 01942 }); 01943 ast_cli(fd, "Jabber Debugging Disabled.\n"); 01944 return RESULT_SUCCESS; 01945 } 01946 01947 /*! 01948 * \brief show client status. 01949 * \param fd, number of args, args. 01950 * \return RESULT_SUCCESS. 01951 */ 01952 static int aji_show_clients(int fd, int argc, char *argv[]) 01953 { 01954 char *status; 01955 int count = 0; 01956 ast_cli(fd, "Jabber Users and their status:\n"); 01957 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, { 01958 ASTOBJ_RDLOCK(iterator); 01959 count++; 01960 switch (iterator->state) { 01961 case AJI_DISCONNECTED: 01962 status = "Disconnected"; 01963 break; 01964 case AJI_CONNECTING: 01965 status = "Connecting"; 01966 break; 01967 case AJI_CONNECTED: 01968 status = "Connected"; 01969 break; 01970 default: 01971 status = "Unknown"; 01972 } 01973 ast_cli(fd, " User: %s - %s\n", iterator->user, status); 01974 ASTOBJ_UNLOCK(iterator); 01975 }); 01976 ast_cli(fd, "----\n"); 01977 ast_cli(fd, " Number of users: %d\n", count); 01978 return RESULT_SUCCESS; 01979 } 01980 01981 /*! 01982 * \brief send test message for debugging. 01983 * \param fd, number of args, args. 01984 * \return RESULT_SUCCESS. 01985 */ 01986 static int aji_test(int fd, int argc, char *argv[]) 01987 { 01988 struct aji_client *client; 01989 struct aji_resource *resource; 01990 const char *name = "asterisk"; 01991 struct aji_message *tmp; 01992 01993 if (argc > 3) 01994 return RESULT_SHOWUSAGE; 01995 else if (argc == 3) 01996 name = argv[2]; 01997 01998 if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) { 01999 ast_cli(fd, "Unable to find client '%s'!\n", name); 02000 return RESULT_FAILURE; 02001 } 02002 02003 /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */ 02004 ast_aji_send(client, "mogorman@astjab.org", "blahblah"); 02005 ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, { 02006 ASTOBJ_RDLOCK(iterator); 02007 ast_verbose("User: %s\n", iterator->name); 02008 for (resource = iterator->resources; resource; resource = resource->next) { 02009 ast_verbose("Resource: %s\n", resource->resource); 02010 if(resource->cap) { 02011 ast_verbose(" client: %s\n", resource->cap->parent->node); 02012 ast_verbose(" version: %s\n", resource->cap->version); 02013 ast_verbose(" Jingle Capable: %d\n", resource->cap->jingle); 02014 } 02015 ast_verbose(" Priority: %d\n", resource->priority); 02016 ast_verbose(" Status: %d\n", resource->status); 02017 ast_verbose(" Message: %s\n", S_OR(resource->description,"")); 02018 } 02019 ASTOBJ_UNLOCK(iterator); 02020 }); 02021 ast_verbose("\nOooh a working message stack!\n"); 02022 AST_LIST_LOCK(&client->messages); 02023 AST_LIST_TRAVERSE(&client->messages, tmp, list) { 02024 ast_verbose(" Message from: %s with id %s @ %s %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, "")); 02025 } 02026 AST_LIST_UNLOCK(&client->messages); 02027 ASTOBJ_UNREF(client, aji_client_destroy); 02028 02029 return RESULT_SUCCESS; 02030 } 02031 02032 /*! 02033 * \brief creates aji_client structure. 02034 * \param label, ast_variable, debug, pruneregister, component/client, aji_client to dump into. 02035 * \return 0. 02036 */ 02037 static int aji_create_client(char *label, struct ast_variable *var, int debug) 02038 { 02039 char *resource; 02040 struct aji_client *client = NULL; 02041 int flag = 0; 02042 02043 client = ASTOBJ_CONTAINER_FIND(&clients,label); 02044 if (!client) { 02045 flag = 1; 02046 client = (struct aji_client *) malloc(sizeof(struct aji_client)); 02047 if (!client) { 02048 ast_log(LOG_ERROR, "Out of memory!\n"); 02049 return 0; 02050 } 02051 memset(client, 0, sizeof(struct aji_client)); 02052 ASTOBJ_INIT(client); 02053 ASTOBJ_WRLOCK(client); 02054 ASTOBJ_CONTAINER_INIT(&client->buddies); 02055 } else { 02056 ASTOBJ_WRLOCK(client); 02057 ASTOBJ_UNMARK(client); 02058 } 02059 ASTOBJ_CONTAINER_MARKALL(&client->buddies); 02060 ast_copy_string(client->name, label, sizeof(client->name)); 02061 ast_copy_string(client->mid, "aaaaa", sizeof(client->mid)); 02062 02063 client->debug = debug; 02064 ast_copy_flags(client, &globalflags, AST_FLAGS_ALL); 02065 client->port = 5222; 02066 client->usetls = 1; 02067 client->usesasl = 1; 02068 client->forcessl = 0; 02069 client->keepalive = 1; 02070 client->timeout = 50; 02071 client->message_timeout = 100; 02072 AST_LIST_HEAD_INIT(&client->messages); 02073 client->component = 0; 02074 ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage)); 02075 02076 if (flag) { 02077 client->authorized = 0; 02078 client->state = AJI_DISCONNECTED; 02079 } 02080 while (var) { 02081 if (!strcasecmp(var->name, "username")) 02082 ast_copy_string(client->user, var->value, sizeof(client->user)); 02083 else if (!strcasecmp(var->name, "serverhost")) 02084 ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost)); 02085 else if (!strcasecmp(var->name, "secret")) 02086 ast_copy_string(client->password, var->value, sizeof(client->password)); 02087 else if (!strcasecmp(var->name, "statusmessage")) 02088 ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage)); 02089 else if (!strcasecmp(var->name, "port")) 02090 client->port = atoi(var->value); 02091 else if (!strcasecmp(var->name, "timeout")) 02092 client->message_timeout = atoi(var->value); 02093 else if (!strcasecmp(var->name, "debug")) 02094 client->debug = (ast_false(var->value)) ? 0 : 1; 02095 else if (!strcasecmp(var->name, "type")) { 02096 if (!strcasecmp(var->value, "component")) 02097 client->component = 1; 02098 } else if (!strcasecmp(var->name, "usetls")) { 02099 client->usetls = (ast_false(var->value)) ? 0 : 1; 02100 } else if (!strcasecmp(var->name, "usesasl")) { 02101 client->usesasl = (ast_false(var->value)) ? 0 : 1; 02102 } else if (!strcasecmp(var->name, "forceoldssl")) 02103 client->forcessl = (ast_false(var->value)) ? 0 : 1; 02104 else if (!strcasecmp(var->name, "keepalive")) 02105 client->keepalive = (ast_false(var->value)) ? 0 : 1; 02106 else if (!strcasecmp(var->name, "autoprune")) 02107 ast_set2_flag(client, ast_true(var->value), AJI_AUTOPRUNE); 02108 else if (!strcasecmp(var->name, "autoregister")) 02109 ast_set2_flag(client, ast_true(var->value), AJI_AUTOREGISTER); 02110 else if (!strcasecmp(var->name, "buddy")) 02111 aji_create_buddy(var->value, client); 02112 /* no transport support in this version */ 02113 /* else if (!strcasecmp(var->name, "transport")) 02114 aji_create_transport(var->value, client); 02115 */ 02116 var = var->next; 02117 } 02118 if (!flag) { 02119 ASTOBJ_UNLOCK(client); 02120 ASTOBJ_UNREF(client, aji_client_destroy); 02121 return 1; 02122 } 02123 client->p = iks_stream_new(((client->component) ? "jabber:component:accept" : "jabber:client"), client, aji_act_hook); 02124 if (!client->p) { 02125 ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name); 02126 return 0; 02127 } 02128 client->stack = iks_stack_new(8192, 8192); 02129 if (!client->stack) { 02130 ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name); 02131 return 0; 02132 } 02133 client->f = iks_filter_new(); 02134 if (!client->f) { 02135 ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name); 02136 return 0; 02137 } 02138 if (!strchr(client->user, '/') && !client->component) { /*client */ 02139 resource = NULL; 02140 asprintf(&resource, "%s/asterisk", client->user); 02141 if (resource) { 02142 client->jid = iks_id_new(client->stack, resource); 02143 free(resource); 02144 } 02145 } else 02146 client->jid = iks_id_new(client->stack, client->user); 02147 if (client->component) { 02148 iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE); 02149 iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE); 02150 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); 02151 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); 02152 } else { 02153 iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE); 02154 } 02155 if (!strchr(client->user, '/') && !client->component) { /*client */ 02156 resource = NULL; 02157 asprintf(&resource, "%s/asterisk", client->user); 02158 if (resource) { 02159 client->jid = iks_id_new(client->stack, resource); 02160 free(resource); 02161 } 02162 } else 02163 client->jid = iks_id_new(client->stack, client->user); 02164 iks_set_log_hook(client->p, aji_log_hook); 02165 ASTOBJ_UNLOCK(client); 02166 ASTOBJ_CONTAINER_LINK(&clients,client); 02167 return 1; 02168 } 02169 02170 /*! 02171 * \brief creates transport. 02172 * \param label, buddy to dump it into. 02173 * \return 0. 02174 */ 02175 /* no connecting to transports today */ 02176 /* 02177 static int aji_create_transport(char *label, struct aji_client *client) 02178 { 02179 char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL; 02180 struct aji_buddy *buddy = NULL; 02181 02182 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label); 02183 if (!buddy) { 02184 buddy = malloc(sizeof(struct aji_buddy)); 02185 if(!buddy) { 02186 ast_log(LOG_WARNING, "Out of memory\n"); 02187 return 0; 02188 } 02189 memset(buddy, 0, sizeof(struct aji_buddy)); 02190 ASTOBJ_INIT(buddy); 02191 } 02192 ASTOBJ_WRLOCK(buddy); 02193 server = label; 02194 if ((buddyname = strchr(label, ','))) { 02195 *buddyname = '\0'; 02196 buddyname++; 02197 if (buddyname && buddyname[0] != '\0') { 02198 if ((user = strchr(buddyname, ','))) { 02199 *user = '\0'; 02200 user++; 02201 if (user && user[0] != '\0') { 02202 if ((pass = strchr(user, ','))) { 02203 *pass = '\0'; 02204 pass++; 02205 ast_copy_string(buddy->pass, pass, sizeof(buddy->pass)); 02206 ast_copy_string(buddy->user, user, sizeof(buddy->user)); 02207 ast_copy_string(buddy->name, buddyname, sizeof(buddy->name)); 02208 ast_copy_string(buddy->server, server, sizeof(buddy->server)); 02209 return 1; 02210 } 02211 } 02212 } 02213 } 02214 } 02215 ASTOBJ_UNLOCK(buddy); 02216 ASTOBJ_UNMARK(buddy); 02217 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy); 02218 return 0; 02219 } 02220 */ 02221 02222 /*! 02223 * \brief creates buddy. 02224 * \param label, buddy to dump it into. 02225 * \return 0. 02226 */ 02227 static int aji_create_buddy(char *label, struct aji_client *client) 02228 { 02229 struct aji_buddy *buddy = NULL; 02230 int flag = 0; 02231 buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label); 02232 if (!buddy) { 02233 flag = 1; 02234 buddy = malloc(sizeof(struct aji_buddy)); 02235 if(!buddy) { 02236 ast_log(LOG_WARNING, "Out of memory\n"); 02237 return 0; 02238 } 02239 memset(buddy, 0, sizeof(struct aji_buddy)); 02240 ASTOBJ_INIT(buddy); 02241 } 02242 ASTOBJ_WRLOCK(buddy); 02243 ast_copy_string(buddy->name, label, sizeof(buddy->name)); 02244 ASTOBJ_UNLOCK(buddy); 02245 if(flag) 02246 ASTOBJ_CONTAINER_LINK(&client->buddies, buddy); 02247 else { 02248 ASTOBJ_UNMARK(buddy); 02249 ASTOBJ_UNREF(buddy, aji_buddy_destroy); 02250 } 02251 return 1; 02252 } 02253 02254 /*! 02255 * \brief load config file. 02256 * \param void. 02257 * \return 1. 02258 */ 02259 static int aji_load_config(void) 02260 { 02261 char *cat = NULL; 02262 int debug = 1; 02263 struct ast_config *cfg = NULL; 02264 struct ast_variable *var = NULL; 02265 02266 cfg = ast_config_load(JABBER_CONFIG); 02267 if (!cfg) { 02268 ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG); 02269 return 0; 02270 } 02271 02272 cat = ast_category_browse(cfg, NULL); 02273 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) { 02274 if (!strcasecmp(var->name, "debug")) 02275 debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1; 02276 else if (!strcasecmp(var->name, "autoprune")) 02277 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE); 02278 else if (!strcasecmp(var->name, "autoregister")) 02279 ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER); 02280 } 02281 02282 while (cat) { 02283 if (strcasecmp(cat, "general")) { 02284 var = ast_variable_browse(cfg, cat); 02285 aji_create_client(cat, var, debug); 02286 } 02287 cat = ast_category_browse(cfg, cat); 02288 } 02289 return 1; 02290 } 02291 02292 /*! 02293 * \brief grab a aji_client structure by label name. 02294 * \param void. 02295 * \return 1. 02296 */ 02297 struct aji_client *ast_aji_get_client(const char *name) 02298 { 02299 struct aji_client *client = NULL; 02300 02301 client = ASTOBJ_CONTAINER_FIND(&clients, name); 02302 if (!client && !strchr(name, '@')) 02303 client = ASTOBJ_CONTAINER_FIND_FULL(&clients, name, user,,, strcasecmp); 02304 return client; 02305 } 02306 02307 struct aji_client_container *ast_aji_get_clients(void) 02308 { 02309 return &clients; 02310 } 02311 02312 static char mandescr_jabber_send[] = 02313 "Description: Sends a message to a Jabber Client.\n" 02314 "Variables: \n" 02315 " Jabber: Client or transport Asterisk uses to connect to JABBER.\n" 02316 " ScreenName: User Name to message.\n" 02317 " Message: Message to be sent to the buddy\n"; 02318 02319 /*! \brief Send a Jabber Message via call from the Manager */ 02320 static int manager_jabber_send(struct mansession *s, const struct message *m) 02321 { 02322 struct aji_client *client = NULL; 02323 const char *id = astman_get_header(m,"ActionID"); 02324 const char *jabber = astman_get_header(m,"Jabber"); 02325 const char *screenname = astman_get_header(m,"ScreenName"); 02326 const char *message = astman_get_header(m,"Message"); 02327 02328 if (ast_strlen_zero(jabber)) { 02329 astman_send_error(s, m, "No transport specified"); 02330 return 0; 02331 } 02332 if (ast_strlen_zero(screenname)) { 02333 astman_send_error(s, m, "No ScreenName specified"); 02334 return 0; 02335 } 02336 if (ast_strlen_zero(message)) { 02337 astman_send_error(s, m, "No Message specified"); 02338 return 0; 02339 } 02340 02341 astman_send_ack(s, m, "Attempting to send Jabber Message"); 02342 client = ast_aji_get_client(jabber); 02343 if (!client) { 02344 astman_send_error(s, m, "Could not find Sender"); 02345 return 0; 02346 } 02347 if (strchr(screenname, '@') && message){ 02348 ast_aji_send(client, screenname, message); 02349 if (!ast_strlen_zero(id)) 02350 astman_append(s, "ActionID: %s\r\n",id); 02351 astman_append(s, "Response: Success\r\n"); 02352 return 0; 02353 } 02354 if (!ast_strlen_zero(id)) 02355 astman_append(s, "ActionID: %s\r\n",id); 02356 astman_append(s, "Response: Failure\r\n"); 02357 return 0; 02358 } 02359 02360 02361 static int aji_reload() 02362 { 02363 ASTOBJ_CONTAINER_MARKALL(&clients); 02364 if (!aji_load_config()) { 02365 ast_log(LOG_ERROR, "JABBER: Failed to load config.\n"); 02366 return 0; 02367 } 02368 ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, aji_client_destroy); 02369 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, { 02370 ASTOBJ_RDLOCK(iterator); 02371 if(iterator->state == AJI_DISCONNECTED) { 02372 if (!iterator->thread) 02373 ast_pthread_create_background(&iterator->thread, NULL, aji_recv_loop, iterator); 02374 } else if (iterator->state == AJI_CONNECTING) 02375 aji_get_roster(iterator); 02376 ASTOBJ_UNLOCK(iterator); 02377 }); 02378 02379 return 1; 02380 } 02381 02382 static int unload_module(void) 02383 { 02384 ast_cli_unregister_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry)); 02385 ast_unregister_application(app_ajisend); 02386 ast_unregister_application(app_ajistatus); 02387 ast_manager_unregister("JabberSend"); 02388 ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, { 02389 ASTOBJ_RDLOCK(iterator); 02390 if (option_verbose > 2) 02391 ast_verbose(VERBOSE_PREFIX_3 "JABBER: %s\n", iterator->name); 02392 iterator->state = AJI_DISCONNECTED; 02393 ast_aji_disconnect(iterator); 02394 pthread_join(iterator->thread, NULL); 02395 ASTOBJ_UNLOCK(iterator); 02396 }); 02397 02398 ASTOBJ_CONTAINER_DESTROYALL(&clients, aji_client_destroy); 02399 ASTOBJ_CONTAINER_DESTROY(&clients); 02400 02401 ast_log(LOG_NOTICE, "res_jabber unloaded.\n"); 02402 return 0; 02403 } 02404 02405 static int load_module(void) 02406 { 02407 ASTOBJ_CONTAINER_INIT(&clients); 02408 if(!aji_reload()) 02409 return AST_MODULE_LOAD_DECLINE; 02410 ast_manager_register2("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send, 02411 "Sends a message to a Jabber Client", mandescr_jabber_send); 02412 ast_register_application(app_ajisend, aji_send_exec, ajisend_synopsis, ajisend_descrip); 02413 ast_register_application(app_ajistatus, aji_status_exec, ajistatus_synopsis, ajistatus_descrip); 02414 ast_cli_register_multiple(aji_cli, sizeof(aji_cli) / sizeof(struct ast_cli_entry)); 02415 02416 ast_log(LOG_NOTICE, "res_jabber.so loaded.\n"); 02417 return 0; 02418 } 02419 02420 static int reload(void) 02421 { 02422 aji_reload(); 02423 return 0; 02424 } 02425 02426 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "AJI - Asterisk Jabber Interface", 02427 .load = load_module, 02428 .unload = unload_module, 02429 .reload = reload, 02430 );