417 #define CLIENT_BUCKETS 53 420 #define BUDDY_BUCKETS 53 423 #define RESOURCE_BUCKETS 53 426 #define XMPP_TLS_NS "urn:ietf:params:xml:ns:xmpp-tls" 429 #define STATUS_DISAPPEAR 6 553 iks_filter_delete(client->
filter);
557 iks_stack_delete(client->
stack);
572 const char *
id = obj;
581 const char *
id = arg;
589 if (state == client->
state) {
621 ast_log(
LOG_ERROR,
"Could not initialize buddy container for '%s'\n", name);
632 if (!(client->
stack = iks_stack_new(8192, 8192))) {
659 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, category))) {
663 ao2_ref(clientcfg->client, +1);
664 return clientcfg->client;
710 const char *
match = (flags &
OBJ_KEY) ? arg : two->name;
756 ast_log(
LOG_ERROR,
"No oauth_clientid or oauth_secret specified, so client '%s' can't be used\n", clientcfg->
name);
761 if (!cfg || !cfg->clients || !(oldclientcfg =
xmpp_config_find(cfg->clients, clientcfg->
name))) {
767 if (strcmp(clientcfg->
user, oldclientcfg->user) ||
768 strcmp(clientcfg->
password, oldclientcfg->password) ||
769 strcmp(clientcfg->
refresh_token, oldclientcfg->refresh_token) ||
770 strcmp(clientcfg->
oauth_clientid, oldclientcfg->oauth_clientid) ||
771 strcmp(clientcfg->
oauth_secret, oldclientcfg->oauth_secret) ||
772 strcmp(clientcfg->
server, oldclientcfg->server) ||
773 (clientcfg->
port != oldclientcfg->port) ||
775 (clientcfg->
priority != oldclientcfg->priority)) {
796 .category =
"general",
805 .category =
"general",
809 .item_offset = offsetof(
struct xmpp_config, clients),
816 .alias =
"jabber.conf",
817 .types =
ACO_TYPES(&global_option, &client_option),
866 return client->stream_flags & SECURE;
877 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, name))) {
881 ao2_ref(clientcfg->client, +1);
882 return clientcfg->client;
910 !(message_packet = iks_make_msg(group ? IKS_TYPE_GROUPCHAT : IKS_TYPE_CHAT, address, message))) {
915 snprintf(from,
sizeof(from),
"%s@%s/%s", nick, client->
jid->full, nick);
917 snprintf(from,
sizeof(from),
"%s", client->
jid->full);
920 iks_insert_attrib(message_packet,
"from", from);
924 iks_delete(message_packet);
937 iks *invite, *body =
NULL, *
namespace =
NULL;
939 if (!(invite = iks_new(
"message")) || !(body = iks_new(
"body")) || !(
namespace = iks_new(
"x"))) {
944 iks_insert_attrib(invite,
"to", user);
946 iks_insert_attrib(invite,
"id", client->
mid);
949 iks_insert_cdata(body, message, 0);
950 iks_insert_node(invite, body);
951 iks_insert_attrib(
namespace,
"xmlns",
"jabber:x:conference");
952 iks_insert_attrib(
namespace,
"jid", room);
953 iks_insert_node(invite,
namespace);
958 iks_delete(
namespace);
974 !(presence = iks_make_pres(level,
NULL)) || !(x = iks_new(
"x"))) {
980 snprintf(from,
sizeof(from),
"%s@%s/%s", nick, client->
jid->full, nick);
981 snprintf(roomid,
sizeof(roomid),
"%s/%s", room, nick);
983 snprintf(from,
sizeof(from),
"%s", client->
jid->full);
984 snprintf(roomid,
sizeof(roomid),
"%s/%s", room,
S_OR(nick, client->
jid->user));
987 iks_insert_attrib(presence,
"to", roomid);
988 iks_insert_attrib(presence,
"from", from);
989 iks_insert_attrib(x,
"xmlns",
"http://jabber.org/protocol/muc");
990 iks_insert_node(presence, x);
996 iks_delete(presence);
1020 for (i = strlen(mid) - 1; i >= 0; i--) {
1021 if (mid[i] !=
'z') {
1022 mid[i] = mid[i] + 1;
1043 !(request = iks_new(
"iq"))) {
1048 iks_insert_attrib(request,
"to", clientcfg->pubsubnode);
1051 iks_insert_attrib(request,
"from", client->
jid->full);
1052 iks_insert_attrib(request,
"type", type);
1055 iks_insert_attrib(request,
"id", client->
mid);
1069 const char *event_type,
unsigned int cachable)
1078 pubsub = iks_insert(request,
"pubsub");
1079 iks_insert_attrib(pubsub,
"xmlns",
"http://jabber.org/protocol/pubsub");
1080 publish = iks_insert(pubsub,
"publish");
1082 item = iks_insert(publish,
"item");
1083 iks_insert_attrib(item,
"id", node);
1086 iks *
options, *x, *field_form_type, *field_persist;
1088 options = iks_insert(pubsub,
"publish-options");
1089 x = iks_insert(options,
"x");
1090 iks_insert_attrib(x,
"xmlns",
"jabber:x:data");
1091 iks_insert_attrib(x,
"type",
"submit");
1092 field_form_type = iks_insert(x,
"field");
1093 iks_insert_attrib(field_form_type,
"var",
"FORM_TYPE");
1094 iks_insert_attrib(field_form_type,
"type",
"hidden");
1095 iks_insert_cdata(iks_insert(field_form_type,
"value"),
"http://jabber.org/protocol/pubsub#publish-options", 0);
1096 field_persist = iks_insert(x,
"field");
1097 iks_insert_attrib(field_persist,
"var",
"pubsub#persist_items");
1098 iks_insert_cdata(iks_insert(field_persist,
"value"),
"0", 1);
1107 iks *configure, *x, *field_owner, *field_node_type, *field_node_config,
1108 *field_deliver_payload, *field_persist_items, *field_access_model,
1109 *field_pubsub_collection;
1110 configure = iks_insert(pubsub,
"configure");
1111 x = iks_insert(configure,
"x");
1112 iks_insert_attrib(x,
"xmlns",
"jabber:x:data");
1113 iks_insert_attrib(x,
"type",
"submit");
1114 field_owner = iks_insert(x,
"field");
1115 iks_insert_attrib(field_owner,
"var",
"FORM_TYPE");
1116 iks_insert_attrib(field_owner,
"type",
"hidden");
1117 iks_insert_cdata(iks_insert(field_owner,
"value"),
1118 "http://jabber.org/protocol/pubsub#owner", 39);
1120 field_node_type = iks_insert(x,
"field");
1121 iks_insert_attrib(field_node_type,
"var",
"pubsub#node_type");
1122 iks_insert_cdata(iks_insert(field_node_type,
"value"), node_type, strlen(node_type));
1124 field_node_config = iks_insert(x,
"field");
1125 iks_insert_attrib(field_node_config,
"var",
"FORM_TYPE");
1126 iks_insert_attrib(field_node_config,
"type",
"hidden");
1127 iks_insert_cdata(iks_insert(field_node_config,
"value"),
1128 "http://jabber.org/protocol/pubsub#node_config", 45);
1129 field_deliver_payload = iks_insert(x,
"field");
1130 iks_insert_attrib(field_deliver_payload,
"var",
"pubsub#deliver_payloads");
1131 iks_insert_cdata(iks_insert(field_deliver_payload,
"value"),
"1", 1);
1132 field_persist_items = iks_insert(x,
"field");
1133 iks_insert_attrib(field_persist_items,
"var",
"pubsub#persist_items");
1134 iks_insert_cdata(iks_insert(field_persist_items,
"value"),
"1", 1);
1135 field_access_model = iks_insert(x,
"field");
1136 iks_insert_attrib(field_access_model,
"var",
"pubsub#access_model");
1137 iks_insert_cdata(iks_insert(field_access_model,
"value"),
"whitelist", 9);
1138 if (node_type && !strcasecmp(node_type,
"leaf")) {
1139 field_pubsub_collection = iks_insert(x,
"field");
1140 iks_insert_attrib(field_pubsub_collection,
"var",
"pubsub#collection");
1141 iks_insert_cdata(iks_insert(field_pubsub_collection,
"value"), collection_name,
1142 strlen(collection_name));
1156 iks *
pubsub, *affiliations, *affiliate;
1160 if (!modify_affiliates) {
1161 ast_log(
LOG_ERROR,
"Could not create IQ for creating affiliations on client '%s'\n", client->
name);
1165 pubsub = iks_insert(modify_affiliates,
"pubsub");
1166 iks_insert_attrib(pubsub,
"xmlns",
"http://jabber.org/protocol/pubsub#owner");
1167 affiliations = iks_insert(pubsub,
"affiliations");
1168 iks_insert_attrib(affiliations,
"node", node);
1172 affiliate = iks_insert(affiliations,
"affiliation");
1173 iks_insert_attrib(affiliate,
"jid", buddy->
id);
1174 iks_insert_attrib(affiliate,
"affiliation",
"owner");
1180 iks_delete(modify_affiliates);
1192 char *
name,
const char *collection_name)
1194 iks *node, *
pubsub, *create;
1200 pubsub = iks_insert(node,
"pubsub");
1201 iks_insert_attrib(pubsub,
"xmlns",
"http://jabber.org/protocol/pubsub");
1202 create = iks_insert(pubsub,
"create");
1203 iks_insert_attrib(create,
"node", name);
1224 pubsub = iks_insert(request,
"pubsub");
1225 iks_insert_attrib(pubsub,
"xmlns",
"http://jabber.org/protocol/pubsub#owner");
1226 delete = iks_insert(pubsub,
"delete");
1227 iks_insert_attrib(
delete,
"node", node_name);
1230 iks_delete(request);
1253 const char *leaf_name)
1267 const char *oldmsgs,
const char *newmsgs)
1279 mailbox_node = iks_insert(request,
"mailbox");
1280 iks_insert_attrib(mailbox_node,
"xmlns",
"http://asterisk.org");
1281 iks_insert_attrib(mailbox_node,
"eid", eid_str);
1282 iks_insert_cdata(iks_insert(mailbox_node,
"NEWMSGS"), newmsgs, strlen(newmsgs));
1283 iks_insert_cdata(iks_insert(mailbox_node,
"OLDMSGS"), oldmsgs, strlen(oldmsgs));
1287 iks_delete(request);
1298 const char *device_state,
unsigned int cachable)
1302 char eid_str[20], cachable_str[2];
1317 state = iks_insert(request,
"state");
1318 iks_insert_attrib(state,
"xmlns",
"http://asterisk.org");
1319 iks_insert_attrib(state,
"eid", eid_str);
1320 snprintf(cachable_str,
sizeof(cachable_str),
"%u", cachable);
1321 iks_insert_attrib(state,
"cachable", cachable_str);
1322 iks_insert_cdata(state, device_state, strlen(device_state));
1324 iks_delete(request);
1336 char oldmsgs[10], newmsgs[10];
1350 snprintf(oldmsgs,
sizeof(oldmsgs),
"%d", mwi_state->
old_msgs);
1351 snprintf(newmsgs,
sizeof(newmsgs),
"%d", mwi_state->
new_msgs);
1391 ast_log(
LOG_ERROR,
"Could not create IQ when creating pubsub unsubscription on client '%s'\n", client->
name);
1395 pubsub = iks_insert(request,
"pubsub");
1396 iks_insert_attrib(pubsub,
"xmlns",
"http://jabber.org/protocol/pubsub");
1397 unsubscribe = iks_insert(pubsub,
"unsubscribe");
1398 iks_insert_attrib(unsubscribe,
"jid", client->
jid->partial);
1399 iks_insert_attrib(unsubscribe,
"node", node);
1402 iks_delete(request);
1417 if (!cfg || !cfg->global || !request) {
1418 ast_log(
LOG_ERROR,
"Could not create IQ when creating pubsub subscription on client '%s'\n", client->
name);
1422 pubsub = iks_insert(request,
"pubsub");
1423 iks_insert_attrib(pubsub,
"xmlns",
"http://jabber.org/protocol/pubsub");
1424 subscribe = iks_insert(pubsub,
"subscribe");
1425 iks_insert_attrib(subscribe,
"jid", client->
jid->partial);
1426 iks_insert_attrib(subscribe,
"node", node);
1428 iks *
options, *x, *sub_options, *sub_type, *sub_depth, *sub_expire;
1429 options = iks_insert(pubsub,
"options");
1430 x = iks_insert(options,
"x");
1431 iks_insert_attrib(x,
"xmlns",
"jabber:x:data");
1432 iks_insert_attrib(x,
"type",
"submit");
1433 sub_options = iks_insert(x,
"field");
1434 iks_insert_attrib(sub_options,
"var",
"FORM_TYPE");
1435 iks_insert_attrib(sub_options,
"type",
"hidden");
1436 iks_insert_cdata(iks_insert(sub_options,
"value"),
1437 "http://jabber.org/protocol/pubsub#subscribe_options", 51);
1438 sub_type = iks_insert(x,
"field");
1439 iks_insert_attrib(sub_type,
"var",
"pubsub#subscription_type");
1440 iks_insert_cdata(iks_insert(sub_type,
"value"),
"items", 5);
1441 sub_depth = iks_insert(x,
"field");
1442 iks_insert_attrib(sub_depth,
"var",
"pubsub#subscription_depth");
1443 iks_insert_cdata(iks_insert(sub_depth,
"value"),
"all", 3);
1444 sub_expire = iks_insert(x,
"field");
1445 iks_insert_attrib(sub_expire,
"var",
"pubsub#expire");
1446 iks_insert_cdata(iks_insert(sub_expire,
"value"),
"presence", 8);
1449 iks_delete(request);
1460 char *item_id, *device_state, *
mailbox, *cachable_str;
1461 int oldmsgs, newmsgs;
1462 iks *
item, *item_content;
1465 item = iks_find(iks_find(iks_find(pak->x,
"event"),
"items"),
"item");
1468 return IKS_FILTER_EAT;
1470 item_id = iks_find_attrib(item,
"id");
1471 item_content = iks_child(item);
1472 ast_str_to_eid(&pubsub_eid, iks_find_attrib(item_content,
"eid"));
1474 ast_debug(1,
"Returning here, eid of incoming event matches ours!\n");
1475 return IKS_FILTER_EAT;
1477 if (!strcasecmp(iks_name(item_content),
"state")) {
1478 if ((cachable_str = iks_find_attrib(item_content,
"cachable"))) {
1479 sscanf(cachable_str,
"%30u", &cachable);
1481 device_state = iks_find_cdata(item,
"state");
1486 return IKS_FILTER_EAT;
1487 }
else if (!strcasecmp(iks_name(item_content),
"mailbox")) {
1488 mailbox =
strsep(&item_id,
"@");
1489 sscanf(iks_find_cdata(item_content,
"OLDMSGS"),
"%10d", &oldmsgs);
1490 sscanf(iks_find_cdata(item_content,
"NEWMSGS"),
"%10d", &newmsgs);
1494 return IKS_FILTER_EAT;
1496 ast_debug(1,
"Don't know how to handle PubSub event of type %s\n",
1497 iks_name(item_content));
1498 return IKS_FILTER_EAT;
1501 return IKS_FILTER_EAT;
1507 char *node_name, *
error;
1509 iks *orig_request, *orig_pubsub = iks_find(pak->x,
"pubsub");
1512 if (!cfg || !cfg->global) {
1514 return IKS_FILTER_EAT;
1518 ast_debug(1,
"Error isn't a PubSub error, why are we here?\n");
1519 return IKS_FILTER_EAT;
1522 orig_request = iks_child(orig_pubsub);
1523 error = iks_find_attrib(iks_find(pak->x,
"error"),
"code");
1524 node_name = iks_find_attrib(orig_request,
"node");
1526 if (!sscanf(error,
"%30d", &error_num)) {
1527 return IKS_FILTER_EAT;
1530 if (error_num > 399 && error_num < 500 && error_num != 404) {
1532 "Error performing operation on PubSub node %s, %s.\n", node_name, error);
1533 return IKS_FILTER_EAT;
1534 }
else if (error_num > 499 && error_num < 600) {
1536 return IKS_FILTER_EAT;
1539 if (!strcasecmp(iks_name(orig_request),
"publish")) {
1543 if (iks_find(iks_find(orig_request,
"item"),
"state")) {
1545 }
else if (iks_find(iks_find(orig_request,
"item"),
"mailbox")) {
1553 iks_insert_node(request, orig_pubsub);
1555 iks_delete(request);
1560 return IKS_FILTER_EAT;
1561 }
else if (!strcasecmp(iks_name(orig_request),
"subscribe")) {
1569 return IKS_FILTER_EAT;
1617 IKS_PAK_MESSAGE, IKS_RULE_FROM, clientcfg->pubsubnode, IKS_RULE_DONE);
1619 IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_ERROR, IKS_RULE_DONE);
1629 #define BUDDY_OFFLINE 6 1630 #define BUDDY_NOT_IN_ROSTER 7 1686 if (
args.argc != 2) {
1687 ast_log(
LOG_ERROR,
"JABBER_STATUS requires 2 arguments: sender and jid.\n");
1692 if (jid.argc < 1 || jid.argc > 2) {
1702 snprintf(buf, buflen,
"%d",
get_buddy_status(clientcfg, jid.screenname, jid.resource));
1708 .
name =
"JABBER_STATUS",
1731 ast_log(
LOG_ERROR,
"%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
1737 if (
args.argc < 2 ||
args.argc > 3) {
1738 ast_log(
LOG_ERROR,
"%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
1742 if (strchr(
args.jid,
'/')) {
1754 snprintf(nick,
sizeof(nick),
"asterisk");
1756 snprintf(nick,
sizeof(nick),
"%s", clientcfg->client->jid->user);
1759 snprintf(nick,
sizeof(nick),
"%s",
args.nick);
1790 ast_log(
LOG_ERROR,
"%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
1796 if (
args.argc < 2 ||
args.argc > 3) {
1797 ast_log(
LOG_ERROR,
"%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
1801 if (strchr(
args.jid,
'/')) {
1818 snprintf(nick,
sizeof(nick),
"asterisk");
1820 snprintf(nick,
sizeof(nick),
"%s", clientcfg->client->jid->user);
1823 snprintf(nick,
sizeof(nick),
"%s",
args.nick);
1893 ast_log(
LOG_ERROR,
"%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
1900 ast_log(
LOG_ERROR,
"%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
1911 snprintf(nick,
sizeof(nick),
"asterisk");
1913 snprintf(nick,
sizeof(nick),
"%s", clientcfg->client->jid->user);
1916 snprintf(nick,
sizeof(nick),
"%s",
args.nick);
1938 int timeout, jidlen, resourcelen, found = 0;
1939 struct timeval start;
1960 if (
args.argc < 2 ||
args.argc > 3) {
1975 sscanf(
args.timeout,
"%d", &timeout);
1982 jidlen = strlen(jid.screenname);
1983 resourcelen =
ast_strlen_zero(jid.resource) ? 0 : strlen(jid.resource);
1990 ast_debug(3,
"Waiting for an XMPP message from %s\n",
args.jid);
2001 while (diff < timeout) {
2002 struct timespec ts = { 0, };
2003 struct timeval wait;
2007 ts.tv_sec = wait.tv_sec;
2008 ts.tv_nsec = wait.tv_usec * 1000;
2016 if (res == ETIMEDOUT) {
2017 ast_debug(3,
"No message received from %s in %d seconds\n",
args.jid, timeout);
2023 if (jid.argc == 1) {
2025 if (strncasecmp(jid.screenname, message->
from, jidlen)) {
2030 char *resource = strchr(message->
from,
'/');
2031 if (!resource || strlen(resource) == 0) {
2033 if (strncasecmp(jid.screenname, message->
from, jidlen)) {
2038 if (strncasecmp(jid.screenname, message->
from, jidlen) || strncmp(jid.resource, resource, resourcelen)) {
2045 ast_debug(3,
"Found old message from %s, deleting it\n", message->
from);
2080 .
name =
"JABBER_RECEIVE",
2096 int deleted = 0, isold = 0;
2106 if (!from || !strncasecmp(from, message->
from, strlen(from))) {
2113 if (!from || !strncasecmp(from, message->
from, strlen(from))) {
2130 char *sender, *dest;
2143 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, sender))) {
2148 ast_debug(1,
"Sending message to '%s' from '%s'\n", dest, clientcfg->name);
2154 return res == IKS_OK ? 0 : -1;
2194 "Goodbye. Your status is no longer required.\n"))) {
2198 if (!(iq = iks_new(
"iq")) || !(query = iks_new(
"query")) || !(item = iks_new(
"item"))) {
2199 ast_log(
LOG_WARNING,
"Could not allocate memory for roster removal of '%s' from client '%s'\n",
2200 user, client->
name);
2204 iks_insert_attrib(iq,
"from", client->
jid->full);
2205 iks_insert_attrib(iq,
"type",
"set");
2206 iks_insert_attrib(query,
"xmlns",
"jabber:iq:roster");
2207 iks_insert_node(iq, query);
2208 iks_insert_attrib(item,
"jid", user);
2209 iks_insert_attrib(item,
"subscription",
"remove");
2210 iks_insert_node(query, item);
2213 ast_log(
LOG_WARNING,
"Could not send roster removal request of '%s' from client '%s'\n",
2214 user, client->
name);
2236 "Greetings! I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"))) {
2238 buddy->
id, client->
name);
2255 return IKS_FILTER_EAT;
2258 for (item = iks_child(pak->query); item; item = iks_next(item)) {
2261 if (iks_strcmp(iks_name(item),
"item")) {
2271 iks_find_attrib(item,
"jid"), client->
name);
2277 ast_log(
LOG_ERROR,
"Could not allocate buddy '%s' on client '%s'\n", iks_find_attrib(item,
"jid"),
2284 if (!iks_strcmp(iks_find_attrib(item,
"subscription"),
"none") ||
2285 !iks_strcmp(iks_find_attrib(item,
"subscription"),
"from")) {
2301 return IKS_FILTER_EAT;
2313 !(presence = iks_make_pres(level, desc)) || !(cnode = iks_new(
"c")) || !(priority = iks_new(
"priority"))) {
2314 ast_log(
LOG_ERROR,
"Unable to allocate stanzas for setting presence status for client '%s'\n", client->
name);
2319 iks_insert_attrib(presence,
"to", to);
2323 iks_insert_attrib(presence,
"from", from);
2326 snprintf(priorityS,
sizeof(priorityS),
"%d", clientcfg->priority);
2327 iks_insert_cdata(priority, priorityS, strlen(priorityS));
2328 iks_insert_node(presence, priority);
2329 iks_insert_attrib(cnode,
"node",
"http://www.asterisk.org/xmpp/client/caps");
2330 iks_insert_attrib(cnode,
"ver",
"asterisk-xmpp");
2331 iks_insert_attrib(cnode,
"ext",
"voice-v1 video-v1 camera-v1");
2332 iks_insert_attrib(cnode,
"xmlns",
"http://jabber.org/protocol/caps");
2333 iks_insert_node(presence, cnode);
2338 iks_delete(presence);
2339 iks_delete(priority);
2348 if (!(iq = iks_new(
"iq")) || !(query = iks_new(
"query")) || !(ident = iks_new(
"identity")) || !(disco = iks_new(
"feature")) ||
2349 !(google = iks_new(
"feature")) || !(jingle = iks_new(
"feature")) || !(ice = iks_new(
"feature")) || !(rtp = iks_new(
"feature")) ||
2350 !(audio = iks_new(
"feature")) || !(video = iks_new(
"feature"))) {
2351 ast_log(
LOG_ERROR,
"Could not allocate memory for responding to service discovery request from '%s' on client '%s'\n",
2352 pak->from->full, client->
name);
2356 iks_insert_attrib(iq,
"from", client->
jid->full);
2359 iks_insert_attrib(iq,
"to", pak->from->full);
2362 iks_insert_attrib(iq,
"type",
"result");
2363 iks_insert_attrib(iq,
"id", pak->id);
2364 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#info");
2365 iks_insert_attrib(ident,
"category",
"client");
2366 iks_insert_attrib(ident,
"type",
"pc");
2367 iks_insert_attrib(ident,
"name",
"asterisk");
2368 iks_insert_attrib(disco,
"var",
"http://jabber.org/protocol/disco#info");
2370 iks_insert_attrib(google,
"var",
"http://www.google.com/xmpp/protocol/voice/v1");
2371 iks_insert_attrib(jingle,
"var",
"urn:xmpp:jingle:1");
2372 iks_insert_attrib(ice,
"var",
"urn:xmpp:jingle:transports:ice-udp:1");
2373 iks_insert_attrib(rtp,
"var",
"urn:xmpp:jingle:apps:rtp:1");
2374 iks_insert_attrib(audio,
"var",
"urn:xmpp:jingle:apps:rtp:audio");
2375 iks_insert_attrib(video,
"var",
"urn:xmpp:jingle:apps:rtp:video");
2376 iks_insert_node(iq, query);
2377 iks_insert_node(query, ident);
2378 iks_insert_node(query, google);
2379 iks_insert_node(query, disco);
2380 iks_insert_node(query, jingle);
2381 iks_insert_node(query, ice);
2382 iks_insert_node(query, rtp);
2383 iks_insert_node(query, audio);
2384 iks_insert_node(query, video);
2399 return IKS_FILTER_EAT;
2410 return IKS_FILTER_EAT;
2415 return IKS_FILTER_EAT;
2420 if (iks_find_with_attrib(pak->query,
"feature",
"var",
"urn:xmpp:jingle:1")) {
2429 return IKS_FILTER_EAT;
2444 client->
jid = (iks_find_cdata(pak->query,
"jid")) ? iks_id_new(client->
stack, iks_find_cdata(pak->query,
"jid")) : client->
jid;
2450 if (!(roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER))) {
2451 ast_log(
LOG_ERROR,
"Unable to allocate memory for roster request for client '%s'\n", client->
name);
2458 iks_insert_attrib(roster,
"id",
"roster");
2462 iks_filter_add_rule(client->
filter,
xmpp_roster_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID,
"roster", IKS_RULE_DONE);
2467 return IKS_FILTER_EAT;
2471 static void xmpp_log_hook(
void *data,
const char *xmpp,
size_t size,
int incoming)
2482 ast_verbose(
"\n<--- XMPP sent to '%s' --->\n%s\n<------------->\n", client->
name, xmpp);
2484 ast_verbose(
"\n<--- XMPP received from '%s' --->\n%s\n<------------->\n", client->
name, xmpp);
2495 return IKS_NET_NOCONN;
2500 int len = strlen(message);
2502 ret = SSL_write(client->ssl_session, message, len);
2513 ret = iks_send_raw(client->
parser, message);
2514 if (ret != IKS_OK) {
2525 char msg[91 + strlen(
namespace) + 6 + strlen(to) + 16 + 1];
2527 snprintf(msg,
sizeof(msg),
"<?xml version='1.0'?>" 2528 "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='" 2529 "%s' to='%s' version='1.0'>",
namespace, to);
2548 #ifndef HAVE_OPENSSL 2549 ast_log(
LOG_ERROR,
"TLS connection for client '%s' cannot be established. OpenSSL is not available.\n", client->
name);
2552 if (iks_send_raw(client->
parser,
"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>") == IKS_NET_TLSFAIL) {
2557 client->stream_flags |= TRY_SECURE;
2566 static char *openssl_error_string(
void)
2570 BIO *bio = BIO_new(BIO_s_mem());
2572 ERR_print_errors(bio);
2573 len = BIO_get_mem_data(bio, &buf);
2576 memcpy(ret, buf, len);
2592 if (!strcmp(iks_name(node),
"success")) {
2596 }
else if (!strcmp(iks_name(node),
"failure")) {
2599 }
else if (strcmp(iks_name(node),
"proceed")) {
2604 #ifndef HAVE_OPENSSL 2605 ast_log(
LOG_ERROR,
"Somehow we managed to try to start TLS negotiation on client '%s' without OpenSSL support, disconnecting\n", client->
name);
2608 client->ssl_method = SSLv23_method();
2609 if (!(client->ssl_context = SSL_CTX_new((SSL_METHOD *) client->ssl_method))) {
2613 ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
2614 SSL_CTX_set_options(client->ssl_context, ssl_opts);
2616 if (!(client->ssl_session = SSL_new(client->ssl_context))) {
2620 sock = iks_fd(client->
parser);
2621 if (!SSL_set_fd(client->ssl_session, sock)) {
2625 if (SSL_connect(client->ssl_session) <= 0) {
2629 client->stream_flags &= (~TRY_SECURE);
2630 client->stream_flags |= SECURE;
2633 ast_log(
LOG_ERROR,
"TLS connection for client '%s' could not be established, failed to send stream header after negotiation\n",
2638 ast_debug(1,
"TLS connection for client '%s' started with server\n", client->
name);
2645 err = openssl_error_string();
2646 ast_log(
LOG_ERROR,
"TLS connection for client '%s' cannot be established. " 2647 "OpenSSL initialization failed: %s\n", client->
name, err);
2657 char buf[41], sidpass[100];
2659 if (!(iq = iks_new(
"iq")) || !(query = iks_insert(iq,
"query"))) {
2660 ast_log(
LOG_ERROR,
"Stanzas could not be allocated for authentication on client '%s'\n", client->
name);
2665 iks_insert_attrib(iq,
"type",
"set");
2666 iks_insert_cdata(iks_insert(query,
"username"), client->
jid->user, 0);
2667 iks_insert_cdata(iks_insert(query,
"resource"), client->
jid->resource, 0);
2669 iks_insert_attrib(query,
"xmlns",
"jabber:iq:auth");
2670 snprintf(sidpass,
sizeof(sidpass),
"%s%s", iks_find_attrib(node,
"id"), cfg->
password);
2672 iks_insert_cdata(iks_insert(query,
"digest"), buf, 0);
2675 iks_filter_add_rule(client->
filter,
xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->
mid, IKS_RULE_DONE);
2676 iks_insert_attrib(iq,
"id", client->
mid);
2680 iks_insert_attrib(iq,
"to", client->
jid->server);
2694 int features,
len = strlen(client->
jid->user) + strlen(cfg->
password) + 3;
2697 char base64[(len + 2) * 4 / 3];
2699 if (strcmp(iks_name(node),
"stream:features")) {
2704 features = iks_stream_features(node);
2706 if ((features & IKS_STREAM_SASL_MD5) && !
xmpp_is_secure(client)) {
2707 if (iks_start_sasl(client->
parser, IKS_SASL_DIGEST_MD5, (
char*)client->
jid->user, (
char*)cfg->
password) != IKS_OK) {
2708 ast_log(
LOG_ERROR,
"Tried to authenticate client '%s' using SASL DIGEST-MD5 but could not\n", client->
name);
2717 if (!(features & IKS_STREAM_SASL_PLAIN)) {
2718 ast_log(
LOG_ERROR,
"Tried to authenticate client '%s' using SASL PLAIN but server does not support it\n", client->
name);
2722 if (!(auth = iks_new(
"auth"))) {
2723 ast_log(
LOG_ERROR,
"Could not allocate memory for SASL PLAIN authentication for client '%s'\n", client->
name);
2727 iks_insert_attrib(auth,
"xmlns", IKS_NS_XMPP_SASL);
2729 iks_insert_attrib(auth,
"mechanism",
"X-OAUTH2");
2730 iks_insert_attrib(auth,
"auth:service",
"oauth2");
2731 iks_insert_attrib(auth,
"xmlns:auth",
"http://www.google.com/talk/protocol/auth");
2733 iks_insert_attrib(auth,
"mechanism",
"PLAIN");
2736 if (strchr(client->
jid->user,
'/')) {
2739 snprintf(combined,
sizeof(combined),
"%c%s%c%s", 0,
strsep(&user,
"/"), 0, cfg->
password);
2741 snprintf(combined,
sizeof(combined),
"%c%s%c%s", 0, client->
jid->user, 0, cfg->
password);
2745 iks_insert_cdata(auth,
base64, 0);
2767 if (!strcmp(iks_name(node),
"success")) {
2772 }
else if (!strcmp(iks_name(node),
"failure")) {
2775 }
else if (strcmp(iks_name(node),
"stream:features")) {
2780 features = iks_stream_features(node);
2782 if (features & IKS_STREAM_BIND) {
2785 if (!(auth = iks_make_resource_bind(client->
jid))) {
2786 ast_log(
LOG_ERROR,
"Failed to allocate memory for stream bind on client '%s'\n", client->
name);
2791 iks_insert_attrib(auth,
"id", client->
mid);
2798 iks_filter_add_rule(client->
filter,
xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_DONE);
2801 if (features & IKS_STREAM_SESSION) {
2804 if (!(auth = iks_make_session())) {
2805 ast_log(
LOG_ERROR,
"Failed to allocate memory for stream session on client '%s'\n", client->
name);
2809 iks_insert_attrib(auth,
"id",
"auth");
2817 iks_filter_add_rule(client->
filter,
xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID,
"auth", IKS_RULE_DONE);
2826 char secret[160], shasum[320],
message[344];
2827 ikspak *pak = iks_packet(node);
2829 snprintf(secret,
sizeof(secret),
"%s%s", pak->id, cfg->
password);
2831 snprintf(message,
sizeof(message),
"<handshake>%s</handshake>", shasum);
2854 !(iq = iks_new(
"iq")) || !(query = iks_new(
"query")) || !(identity = iks_new(
"identity")) || !(disco = iks_new(
"feature")) ||
2855 !(reg = iks_new(
"feature")) || !(commands = iks_new(
"feature")) || !(gateway = iks_new(
"feature")) || !(version = iks_new(
"feature")) ||
2856 !(vcard = iks_new(
"feature")) || !(search = iks_new(
"search")) || !(item = iks_new(
"item"))) {
2857 ast_log(
LOG_ERROR,
"Failed to allocate stanzas for service discovery get response to '%s' on component '%s'\n",
2858 pak->from->partial, client->
name);
2862 iks_insert_attrib(iq,
"from", clientcfg->user);
2863 iks_insert_attrib(iq,
"to", pak->from->full);
2864 iks_insert_attrib(iq,
"id", pak->id);
2865 iks_insert_attrib(iq,
"type",
"result");
2867 if (!(node = iks_find_attrib(pak->query,
"node"))) {
2868 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#info");
2869 iks_insert_attrib(identity,
"category",
"gateway");
2870 iks_insert_attrib(identity,
"type",
"pstn");
2871 iks_insert_attrib(identity,
"name",
"Asterisk The Open Source PBX");
2872 iks_insert_attrib(disco,
"var",
"http://jabber.org/protocol/disco");
2873 iks_insert_attrib(reg,
"var",
"jabber:iq:register");
2874 iks_insert_attrib(commands,
"var",
"http://jabber.org/protocol/commands");
2875 iks_insert_attrib(gateway,
"var",
"jabber:iq:gateway");
2876 iks_insert_attrib(version,
"var",
"jabber:iq:version");
2877 iks_insert_attrib(vcard,
"var",
"vcard-temp");
2878 iks_insert_attrib(search,
"var",
"jabber:iq:search");
2880 iks_insert_node(iq, query);
2881 iks_insert_node(query, identity);
2882 iks_insert_node(query, disco);
2883 iks_insert_node(query, reg);
2884 iks_insert_node(query, commands);
2885 iks_insert_node(query, gateway);
2886 iks_insert_node(query, version);
2887 iks_insert_node(query, vcard);
2888 iks_insert_node(query, search);
2889 }
else if (!strcasecmp(node,
"http://jabber.org/protocol/commands")) {
2890 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#items");
2891 iks_insert_attrib(query,
"node",
"http://jabber.org/protocol/commands");
2892 iks_insert_attrib(item,
"node",
"confirmaccount");
2893 iks_insert_attrib(item,
"name",
"Confirm account");
2894 iks_insert_attrib(item,
"jid", clientcfg->user);
2896 iks_insert_node(iq, query);
2897 iks_insert_node(query, item);
2898 }
else if (!strcasecmp(node,
"confirmaccount")) {
2899 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#info");
2900 iks_insert_attrib(commands,
"var",
"http://jabber.org/protocol/commands");
2902 iks_insert_node(iq, query);
2903 iks_insert_node(query, commands);
2905 ast_debug(3,
"Unsupported service discovery info request received with node '%s' on component '%s'\n",
2906 node, client->
name);
2911 ast_log(
LOG_WARNING,
"Could not send response to service discovery request on component '%s'\n",
2918 iks_delete(version);
2919 iks_delete(gateway);
2920 iks_delete(commands);
2923 iks_delete(identity);
2927 return IKS_FILTER_EAT;
2941 !(iq = iks_new(
"iq")) || !(query = iks_new(
"query")) || !(error = iks_new(
"error")) || !(notacceptable = iks_new(
"not-acceptable")) ||
2942 !(instructions = iks_new(
"instructions"))) {
2943 ast_log(
LOG_ERROR,
"Failed to allocate stanzas for register get response to '%s' on component '%s'\n",
2944 pak->from->partial, client->
name);
2948 iks_insert_attrib(iq,
"from", clientcfg->user);
2949 iks_insert_attrib(iq,
"to", pak->from->full);
2950 iks_insert_attrib(iq,
"id", pak->id);
2951 iks_insert_attrib(iq,
"type",
"result");
2952 iks_insert_attrib(query,
"xmlns",
"jabber:iq:register");
2953 iks_insert_node(iq, query);
2956 iks_insert_attrib(error,
"code",
"406");
2957 iks_insert_attrib(error,
"type",
"modify");
2958 iks_insert_attrib(notacceptable,
"xmlns",
"urn:ietf:params:xml:ns:xmpp-stanzas");
2960 iks_insert_node(iq, error);
2961 iks_insert_node(error, notacceptable);
2963 ast_log(
LOG_ERROR,
"Received register attempt from '%s' but buddy is not configured on component '%s'\n",
2964 pak->from->partial, client->
name);
2965 }
else if (!(node = iks_find_attrib(pak->query,
"node"))) {
2966 iks_insert_cdata(instructions,
"Welcome to Asterisk - the Open Source PBX.\n", 0);
2967 iks_insert_node(query, instructions);
2970 ast_log(
LOG_WARNING,
"Received register get to component '%s' using unsupported node '%s' from '%s'\n",
2971 client->
name, node, pak->from->partial);
2977 ast_log(
LOG_WARNING,
"Could not send response to '%s' for received register get on component '%s'\n",
2978 pak->from->partial, client->
name);
2982 iks_delete(instructions);
2983 iks_delete(notacceptable);
2988 return IKS_FILTER_EAT;
2995 iks *iq, *presence =
NULL, *x =
NULL;
2997 if (!(iq = iks_new(
"iq")) || !(presence = iks_new(
"presence")) || !(x = iks_new(
"x"))) {
2998 ast_log(
LOG_ERROR,
"Failed to allocate stanzas for register set response to '%s' on component '%s'\n",
2999 pak->from->partial, client->
name);
3003 iks_insert_attrib(iq,
"from", client->
jid->full);
3004 iks_insert_attrib(iq,
"to", pak->from->full);
3005 iks_insert_attrib(iq,
"id", pak->id);
3006 iks_insert_attrib(iq,
"type",
"result");
3009 ast_log(
LOG_WARNING,
"Could not send response to '%s' for received register set on component '%s'\n",
3010 pak->from->partial, client->
name);
3014 iks_insert_attrib(presence,
"from", client->
jid->full);
3015 iks_insert_attrib(presence,
"to", pak->from->partial);
3017 iks_insert_attrib(presence,
"id", client->
mid);
3020 iks_insert_attrib(presence,
"type",
"subscribe");
3021 iks_insert_attrib(x,
"xmlns",
"vcard-temp:x:update");
3023 iks_insert_node(presence, x);
3027 pak->from->partial, client->
name);
3032 iks_delete(presence);
3035 return IKS_FILTER_EAT;
3048 !(iq = iks_new(
"iq")) || !(query = iks_new(
"query")) || !(item = iks_new(
"item")) || !(feature = iks_new(
"feature"))) {
3049 ast_log(
LOG_ERROR,
"Failed to allocate stanzas for service discovery items response to '%s' on component '%s'\n",
3050 pak->from->partial, client->
name);
3054 iks_insert_attrib(iq,
"from", clientcfg->user);
3055 iks_insert_attrib(iq,
"to", pak->from->full);
3056 iks_insert_attrib(iq,
"id", pak->id);
3057 iks_insert_attrib(iq,
"type",
"result");
3058 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#items");
3059 iks_insert_node(iq, query);
3061 if (!(node = iks_find_attrib(pak->query,
"node"))) {
3062 iks_insert_attrib(item,
"node",
"http://jabber.org/protocol/commands");
3063 iks_insert_attrib(item,
"name",
"Asterisk Commands");
3064 iks_insert_attrib(item,
"jid", clientcfg->user);
3066 iks_insert_node(query, item);
3067 }
else if (!strcasecmp(node,
"http://jabber.org/protocol/commands")) {
3068 iks_insert_attrib(query,
"node",
"http://jabber.org/protocol/commands");
3070 ast_log(
LOG_WARNING,
"Received service discovery items request to component '%s' using unsupported node '%s' from '%s'\n",
3071 client->
name, node, pak->from->partial);
3076 ast_log(
LOG_WARNING,
"Could not send response to service discovery items request from '%s' on component '%s'\n",
3077 pak->from->partial, client->
name);
3081 iks_delete(feature);
3086 return IKS_FILTER_EAT;
3092 if (!strcmp(iks_name(node),
"stream:features")) {
3096 if (strcmp(iks_name(node),
"handshake")) {
3123 ast_debug(3,
"XMPP client '%s' received a message\n", client->
name);
3125 if (!(body = iks_find_cdata(pak->x,
"body"))) {
3130 if (!(message =
ast_calloc(1,
sizeof(*message)))) {
3178 ast_debug(3,
"Deleted %d messages for client %s from JID %s\n", deleted, client->
name, pak->from->partial);
3197 if (!(iq = iks_new(
"iq")) || !(query = iks_new(
"query"))) {
3202 iks_insert_attrib(iq,
"type",
"get");
3203 iks_insert_attrib(iq,
"to", to);
3204 iks_insert_attrib(iq,
"from", from);
3206 iks_insert_attrib(iq,
"id", client->
mid);
3209 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#info");
3210 iks_insert_node(iq, query);
3234 ast_debug(2,
"JABBER: Sending Keep-Alive Ping for client '%s'\n", client->
name);
3236 if (!(iq = iks_new(
"iq")) || !(ping = iks_new(
"ping"))) {
3241 iks_insert_attrib(iq,
"type",
"get");
3242 iks_insert_attrib(iq,
"to", to);
3243 iks_insert_attrib(iq,
"from", from);
3246 iks_insert_attrib(iq,
"id", client->
mid);
3250 iks_insert_attrib(ping,
"xmlns",
"urn:xmpp:ping");
3251 iks_insert_node(iq, ping);
3267 char *
type = iks_find_attrib(pak->x,
"type");
3277 if (!pak->from->resource) {
3283 if (strcmp(client->
jid->partial, pak->from->partial)) {
3284 ast_log(
LOG_WARNING,
"Received presence information about '%s' despite not having them in roster on client '%s'\n",
3285 pak->from->partial, client->
name);
3296 ast_log(
LOG_ERROR,
"Could not allocate resource object for resource '%s' of buddy '%s' on client '%s'\n",
3297 pak->from->resource, buddy->
id, client->
name);
3315 if (!(node = iks_find_attrib(iks_find(pak->x,
"c"),
"node"))) {
3316 node = iks_find_attrib(iks_find(pak->x,
"caps:c"),
"node");
3319 if (!(ver = iks_find_attrib(iks_find(pak->x,
"c"),
"ver"))) {
3320 ver = iks_find_attrib(iks_find(pak->x,
"caps:c"),
"ver");
3327 if ((node && strcmp(resource->
caps.
node, node)) || (ver && strcmp(resource->
caps.
version, ver))) {
3337 if (iks_find_with_attrib(pak->x,
"c",
"node",
"http://www.google.com/xmpp/client/caps") ||
3338 iks_find_with_attrib(pak->x,
"caps:c",
"node",
"http://www.google.com/xmpp/client/caps") ||
3339 iks_find_with_attrib(pak->x,
"c",
"node",
"http://www.android.com/gtalk/client/caps") ||
3340 iks_find_with_attrib(pak->x,
"caps:c",
"node",
"http://www.android.com/gtalk/client/caps") ||
3341 iks_find_with_attrib(pak->x,
"c",
"node",
"http://mail.google.com/xmpp/client/caps") ||
3342 iks_find_with_attrib(pak->x,
"caps:c",
"node",
"http://mail.google.com/xmpp/client/caps")) {
3348 ast_log(
LOG_WARNING,
"Could not send discovery information request to resource '%s' of buddy '%s' on client '%s', capabilities may be incomplete\n", resource->
resource, buddy->
id, client->
name);
3354 resource->
priority = atoi((iks_find_cdata(pak->x,
"priority")) ? iks_find_cdata(pak->x,
"priority") :
"0");
3359 "Account: %s\r\nJID: %s\r\nResource: %s\r\nStatus: %d\r\nPriority: %d" 3360 "\r\nDescription: %s\r\n",
3372 "Account: %s\r\nJID: %s\r\nStatus: %u\r\n",
3373 client->
name, pak->from->partial, pak->show ? pak->show : IKS_SHOW_UNAVAILABLE);
3396 switch (pak->subtype) {
3397 case IKS_TYPE_SUBSCRIBE:
3401 if ((presence = iks_new(
"presence")) && (status = iks_new(
"status"))) {
3402 iks_insert_attrib(presence,
"type",
"subscribed");
3403 iks_insert_attrib(presence,
"to", pak->from->full);
3404 iks_insert_attrib(presence,
"from", client->
jid->full);
3407 iks_insert_attrib(presence,
"id", pak->id);
3410 iks_insert_cdata(status,
"Asterisk has approved your subscription", 0);
3411 iks_insert_node(presence, status);
3414 ast_log(
LOG_ERROR,
"Could not send subscription acceptance to '%s' from client '%s'\n",
3415 pak->from->partial, client->
name);
3418 ast_log(
LOG_ERROR,
"Could not allocate presence stanzas for accepting subscription from '%s' to client '%s'\n",
3419 pak->from->partial, client->
name);
3423 iks_delete(presence);
3430 case IKS_TYPE_SUBSCRIBED:
3439 pak->from->partial, client->
name);
3477 pak = iks_packet(node);
3482 if (iks_has_children(node) && strchr(iks_name(iks_child(node)),
':')) {
3483 char *node_ns =
NULL;
3485 char *node_name = iks_name(iks_child(node));
3486 char *aux = strchr(node_name,
':') + 1;
3487 snprintf(attr, strlen(
"xmlns:") + (strlen(node_name) - strlen(aux)),
"xmlns:%s", node_name);
3488 node_ns = iks_find_attrib(iks_child(node), attr);
3491 pak->query = iks_child(node);
3518 iks_filter_packet(client->
filter, pak);
3529 pthread_cancel(client->
thread);
3545 if (client->stream_flags & SECURE) {
3546 SSL_shutdown(client->ssl_session);
3547 SSL_CTX_free(client->ssl_context);
3548 SSL_free(client->ssl_session);
3551 client->stream_flags = 0;
3555 iks_disconnect(client->
parser);
3566 struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
3569 int res = IKS_NET_NOCONN;
3578 iks_parser_reset(client->
parser);
3580 if (!client->
filter && !(client->
filter = iks_filter_new())) {
3586 ast_debug(2,
"Obtaining OAuth access token for client '%s'\n", client->
name);
3593 res = iks_connect_via(client->
parser,
S_OR(clientcfg->server, client->
jid->server), clientcfg->port,
3597 setsockopt(iks_fd(client->
parser), SOL_SOCKET, SO_RCVTIMEO, (
char *)&tv,
sizeof(
struct timeval));
3599 if (res == IKS_NET_NOCONN) {
3600 ast_log(
LOG_ERROR,
"No XMPP connection available when trying to connect client '%s'\n", client->
name);
3602 }
else if (res == IKS_NET_NODNS) {
3603 ast_log(
LOG_ERROR,
"No DNS available for XMPP connection when trying to connect client '%s'\n", client->
name);
3616 struct pollfd pfd = { .events = POLLIN };
3621 pfd.fd = SSL_get_fd(client->ssl_session);
3627 pfd.fd = iks_fd(client->
parser);
3629 res =
ast_poll(&pfd, 1, timeout > 0 ? timeout * 1000 : -1);
3633 len = SSL_read(client->ssl_session, buffer, buf_len);
3636 len = recv(pfd.fd, buffer, buf_len, 0);
3640 }
else if (len <= 0) {
3650 int len, ret, pos = 0, newbufpos = 0;
3657 if (len < 0)
return IKS_NET_RWERR;
3669 while (isspace(buf[pos+1])) {
3673 newbuf[newbufpos] =
c;
3685 ast_debug(1,
"JABBER: Detected Google Keep Alive. " 3686 "Sending out Ping request for client '%s'\n", client->
name);
3694 ret = iks_parse(client->
parser, newbuf, 0, 0);
3695 memset(newbuf, 0,
sizeof(newbuf));
3708 if (ret != IKS_OK) {
3711 ast_debug(3,
"XML parsing successful\n");
3719 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,
NULL);
3722 *sleep_time =
MIN(60, *sleep_time * 2);
3724 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
NULL);
3731 int res = IKS_NET_RWERR;
3732 unsigned int sleep_time = 1;
3735 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
NULL);
3743 if (res == IKS_NET_RWERR || client->
timeout == 0) {
3747 res = IKS_NET_RWERR;
3760 if (res == IKS_HOOK) {
3762 }
else if (res == IKS_NET_TLSFAIL) {
3768 if (cfg && cfg->clients) {
3778 if (res == IKS_OK) {
3783 }
else if (res == IKS_NET_RWERR) {
3787 }
else if (res == IKS_NET_NOSOCK) {
3789 }
else if (res == IKS_NET_NOCONN) {
3791 }
else if (res == IKS_NET_NODNS) {
3793 }
else if (res == IKS_NET_NOTSUPP) {
3795 }
else if (res == IKS_NET_DROPPED) {
3797 }
else if (res == IKS_NET_UNKNOWN) {
3799 }
else if (res == IKS_OK) {
3827 char cBuf[1024] =
"";
3828 const char *
url =
"https://www.googleapis.com/oauth2/v3/token";
3833 "CURL(%s,client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token)",
3838 ast_debug(2,
"Performing OAuth 2.0 authentication for client '%s' using command: %s\n",
3842 ast_log(
LOG_ERROR,
"CURL is unavailable. This is required for OAuth 2.0 authentication of XMPP client '%s'. Please ensure it is loaded.\n",
3847 ast_debug(2,
"OAuth 2.0 authentication for client '%s' returned: %s\n", cfg->
name, cBuf);
3858 ast_log(
LOG_ERROR,
"An error occurred while performing OAuth 2.0 authentication for client '%s': %s\n", cfg->
name, cBuf);
3880 ast_log(
LOG_ERROR,
"Iksemel stream could not be created for client '%s' - client not active\n", cfg->
name);
3888 char resource[strlen(cfg->
user) + strlen(
"/asterisk-xmpp") + 1];
3890 snprintf(resource,
sizeof(resource),
"%s/asterisk-xmpp", cfg->
user);
3897 ast_log(
LOG_ERROR,
"Jabber identity '%s' could not be created for client '%s' - client not active\n", cfg->
user, cfg->
name);
3948 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, jabber))) {
3982 query = iks_insert(request,
"query");
3983 iks_insert_attrib(query,
"xmlns",
"http://jabber.org/protocol/disco#items");
3986 iks_insert_attrib(query,
"node", collection);
4003 if (iks_has_children(pak->query)) {
4004 item = iks_first_tag(pak->query);
4005 ast_verbose(
"Connection %s: %s\nNode name: %s\n", client->
name, client->
jid->partial,
4006 iks_find_attrib(item,
"node"));
4007 while ((item = iks_next_tag(item))) {
4008 ast_verbose(
"Node name: %s\n", iks_find_attrib(item,
"node"));
4017 return IKS_FILTER_EAT;
4031 ast_log(
LOG_ERROR,
"Could not request pubsub nodes on client '%s' - IQ could not be created\n", client->
name);
4036 IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->
mid,
4039 iks_delete(request);
4059 e->
command =
"xmpp list nodes";
4061 "Usage: xmpp list nodes <connection> [collection]\n" 4062 " Lists the user's nodes on the respective connection\n" 4063 " ([connection] as configured in xmpp.conf.)\n";
4071 }
else if (a->
argc == 4 || a->
argc == 5) {
4076 collection = a->
argv[4];
4079 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, name))) {
4080 ast_cli(a->
fd,
"Unable to find client '%s'!\n", name);
4084 ast_cli(a->
fd,
"Listing pubsub nodes.\n");
4102 if (iks_has_children(pak->query)) {
4103 item = iks_first_tag(pak->query);
4105 iks_find_attrib(item,
"node"));
4106 while ((item = iks_next_tag(item))) {
4115 return IKS_FILTER_EAT;
4123 IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->
mid,
4126 iks_delete(request);
4145 e->
command =
"xmpp purge nodes";
4147 "Usage: xmpp purge nodes <connection> <node>\n" 4148 " Purges nodes on PubSub server\n" 4149 " as configured in xmpp.conf.\n";
4160 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, name))) {
4161 ast_cli(a->
fd,
"Unable to find client '%s'!\n", name);
4190 e->
command =
"xmpp delete node";
4192 "Usage: xmpp delete node <connection> <node>\n" 4193 " Deletes a node on PubSub server\n" 4194 " as configured in xmpp.conf.\n";
4205 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, name))) {
4206 ast_cli(a->
fd,
"Unable to find client '%s'!\n", name);
4223 const char *
name, *collection_name;
4227 e->
command =
"xmpp create collection";
4229 "Usage: xmpp create collection <connection> <collection>\n" 4230 " Creates a PubSub collection node using the account\n" 4231 " as configured in xmpp.conf.\n";
4241 collection_name = a->
argv[4];
4243 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, name))) {
4244 ast_cli(a->
fd,
"Unable to find client '%s'!\n", name);
4248 ast_cli(a->
fd,
"Creating test PubSub node collection.\n");
4263 const char *
name, *collection_name, *leaf_name;
4267 e->
command =
"xmpp create leaf";
4269 "Usage: xmpp create leaf <connection> <collection> <leaf>\n" 4270 " Creates a PubSub leaf node using the account\n" 4271 " as configured in xmpp.conf.\n";
4281 collection_name = a->
argv[4];
4282 leaf_name = a->
argv[5];
4284 if (!cfg || !cfg->clients || !(clientcfg =
xmpp_config_find(cfg->clients, name))) {
4285 ast_cli(a->
fd,
"Unable to find client '%s'!\n", name);
4289 ast_cli(a->
fd,
"Creating test PubSub node collection.\n");
4305 e->
command =
"xmpp set debug {on|off}";
4307 "Usage: xmpp set debug {on|off}\n" 4308 " Enables/disables dumping of XMPP/Jabber packets for debugging purposes.\n";
4318 if (!strncasecmp(a->
argv[e->
args - 1],
"on", 2)) {
4320 ast_cli(a->
fd,
"XMPP Debugging Enabled.\n");
4322 }
else if (!strncasecmp(a->
argv[e->
args - 1],
"off", 3)) {
4324 ast_cli(a->
fd,
"XMPP Debugging Disabled.\n");
4343 e->
command =
"xmpp show connections";
4345 "Usage: xmpp show connections\n" 4346 " Shows state of client and component connections\n";
4352 if (!cfg || !cfg->clients) {
4356 ast_cli(a->
fd,
"Jabber Users and their status:\n");
4364 state =
"Disconnecting";
4367 state =
"Disconnected";
4370 state =
"Connecting";
4373 state =
"Waiting to request TLS";
4376 state =
"Requested TLS";
4379 state =
"Waiting to authenticate";
4382 state =
"Authenticating";
4385 state =
"Retrieving roster";
4388 state =
"Connected";
4419 e->
command =
"xmpp show buddies";
4421 "Usage: xmpp show buddies\n" 4422 " Shows buddy lists of our clients\n";
4428 if (!cfg || !cfg->clients) {
4507 if (!strcasecmp(var->
name,
"debug")) {
4509 }
else if (!strcasecmp(var->
name,
"autoprune")) {
4511 }
else if (!strcasecmp(var->
name,
"autoregister")) {
4513 }
else if (!strcasecmp(var->
name,
"auth_policy")) {
4515 }
else if (!strcasecmp(var->
name,
"collection_nodes")) {
4517 }
else if (!strcasecmp(var->
name,
"pubsub_autocreate")) {
4530 if (!strcasecmp(var->
name,
"debug")) {
4532 }
else if (!strcasecmp(var->
name,
"type")) {
4534 }
else if (!strcasecmp(var->
name,
"distribute_events")) {
4536 }
else if (!strcasecmp(var->
name,
"usetls")) {
4538 }
else if (!strcasecmp(var->
name,
"usesasl")) {
4540 }
else if (!strcasecmp(var->
name,
"forceoldssl")) {
4542 }
else if (!strcasecmp(var->
name,
"keepalive")) {
4544 }
else if (!strcasecmp(var->
name,
"autoprune")) {
4547 }
else if (!strcasecmp(var->
name,
"autoregister")) {
4550 }
else if (!strcasecmp(var->
name,
"auth_policy")) {
4553 }
else if (!strcasecmp(var->
name,
"sendtodialplan")) {
4566 if (!strcasecmp(var->
value,
"unavailable")) {
4567 cfg->
status = IKS_SHOW_UNAVAILABLE;
4568 }
else if (!strcasecmp(var->
value,
"available") || !strcasecmp(var->
value,
"online")) {
4569 cfg->
status = IKS_SHOW_AVAILABLE;
4570 }
else if (!strcasecmp(var->
value,
"chat") || !strcasecmp(var->
value,
"chatty")) {
4571 cfg->
status = IKS_SHOW_CHAT;
4572 }
else if (!strcasecmp(var->
value,
"away")) {
4573 cfg->
status = IKS_SHOW_AWAY;
4574 }
else if (!strcasecmp(var->
value,
"xa") || !strcasecmp(var->
value,
"xaway")) {
4575 cfg->
status = IKS_SHOW_XA;
4576 }
else if (!strcasecmp(var->
value,
"dnd")) {
4577 cfg->
status = IKS_SHOW_DND;
4578 }
else if (!strcasecmp(var->
value,
"invisible")) {
4579 #ifdef IKS_SHOW_INVISIBLE 4580 cfg->
status = IKS_SHOW_INVISIBLE;
4582 cfg->
status = IKS_SHOW_DND;
4683 ast_log(
LOG_WARNING,
"Entity ID is not set. The distributing device state or MWI will not work.\n");
static struct ast_xmpp_buddy * xmpp_client_create_buddy(struct ao2_container *container, const char *id)
Internal function which creates a buddy on a client.
#define BUDDY_NOT_IN_ROSTER
static int global_bitfield_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
enum sip_cc_notify_state state
int ast_xmpp_chatroom_leave(struct ast_xmpp_client *client, const char *room, const char *nickname)
Leave an XMPP multi-user chatroom.
int ast_func_read(struct ast_channel *chan, const char *function, char *workspace, size_t len)
executes a read operation on a function
Main Channel structure associated with a channel.
static int xmpp_client_service_discovery_get_hook(void *data, ikspak *pak)
Hook function called when client receives a service discovery get message.
int ast_xmpp_chatroom_send(struct ast_xmpp_client *client, const char *nickname, const char *address, const char *message)
Send a message to an XMPP multi-user chatroom.
#define AST_CLI_DEFINE(fn, txt,...)
ast_device_state
Device States.
static int xmpp_resource_hash(const void *obj, const int flags)
Hashing function for XMPP resource.
int ast_msg_set_tech(struct ast_msg *msg, const char *fmt,...)
Set the technology associated with this message.
#define AST_LIST_LOCK(head)
Locks a list.
static int xmpp_client_authenticate_sasl(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
Internal function called when we need to authenticate using SASL.
void astman_append(struct mansession *s, const char *fmt,...)
Asterisk main include file. File version handling, generic pbx functions.
static void xmpp_pubsub_create_collection(struct ast_xmpp_client *client, const char *collection_name)
Create a PubSub collection node.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
static int client_buddy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
static char * xmpp_cli_create_collection(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Method to expose PubSub collection node creation via CLI.
static void sleep_with_backoff(unsigned int *sleep_time)
static struct aco_type global
static void * ast_xmpp_client_config_alloc(const char *cat)
Allocator function for configuration.
XMPP Client Configuration.
CONFIG_INFO_STANDARD(cfg_info, globals, xmpp_config_alloc,.files=ACO_FILES(&res_xmpp_conf),.post_apply_config=xmpp_config_post_apply,)
static int manager_jabber_send(struct mansession *s, const struct message *m)
int ast_msg_set_context(struct ast_msg *msg, const char *fmt,...)
Set the dialplan context for this message.
#define aco_option_register_custom(info, name, matchtype, types, default_val, handler, flags)
Register a config option.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
static int xmpp_client_receive(struct ast_xmpp_client *client, unsigned int timeout)
Internal function which receives data from the XMPP client connection.
static int xmpp_client_unsubscribe_user(struct ast_xmpp_client *client, const char *user)
Helper function which unsubscribes a user and removes them from the roster.
int ast_msg_set_body(struct ast_msg *msg, const char *fmt,...)
Set the 'body' text of a message (in UTF-8)
struct ast_endpoint * ast_endpoint_create(const char *tech, const char *resource)
Create an endpoint struct.
#define ast_set2_flag(p, value, flag)
#define ast_test_flag(p, flag)
static char * xmpp_cli_delete_pubsub_node(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Method to expose PubSub node deletion via CLI.
static iks * xmpp_pubsub_build_node_config(iks *pubsub, const char *node_type, const char *collection_name)
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
static int xmpp_config_prelink(void *newitem)
static int xmpp_client_service_discovery_result_hook(void *data, ikspak *pak)
Hook function called when client receives a service discovery result message.
static int xmpp_pubsub_delete_node_list(void *data, ikspak *pak)
Delete pubsub item lists.
static int xmpp_leave_exec(struct ast_channel *chan, const char *data)
Application to leave a chat room.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
static int xmpp_component_service_discovery_items_hook(void *data, ikspak *pak)
Hook function called when we receive a service discovery items request.
descriptor for a cli entry.
int ast_xmpp_chatroom_invite(struct ast_xmpp_client *client, const char *user, const char *room, const char *message)
Invite a user to an XMPP multi-user chatroom.
static void * xmpp_config_find(struct ao2_container *tmp_container, const char *category)
Find function for configuration.
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
static void xmpp_pubsub_mwi_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg)
Callback function for MWI events.
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
static int debug
Global debug status.
static void xmpp_config_post_apply(void)
static int xmpp_send_cb(const struct ast_msg *msg, const char *to, const char *from)
#define ao2_callback(c, flags, cb_fn, arg)
static AO2_GLOBAL_OBJ_STATIC(globals)
struct ast_msg * ast_msg_alloc(void)
Allocate a message.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.