aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--build_tools/menuselect-deps.in1
-rw-r--r--channels/Makefile6
-rw-r--r--channels/chan_jingle.c1695
-rw-r--r--configs/extensions.conf.sample2
-rw-r--r--configs/jabber.conf.sample15
-rw-r--r--configs/jingle.conf.sample19
-rw-r--r--configure.ac1
-rw-r--r--doc/jabber.txt13
-rw-r--r--doc/jingle.txt8
-rw-r--r--include/asterisk/jabber.h133
-rw-r--r--include/asterisk/jingle.h45
-rw-r--r--include/autoconfig.h.in3
-rw-r--r--makeopts.in3
-rw-r--r--res/Makefile6
-rw-r--r--res/res_jabber.c2238
15 files changed, 4188 insertions, 0 deletions
diff --git a/build_tools/menuselect-deps.in b/build_tools/menuselect-deps.in
index 6a64f493b..e1555ecfc 100644
--- a/build_tools/menuselect-deps.in
+++ b/build_tools/menuselect-deps.in
@@ -24,4 +24,5 @@ WIN32=@OSISWIN32@
ZLIB=@PBX_LIBZLIB@
ZAPTEL=@PBX_LIBZAPTEL@
LIBGSM=@PBX_LIBgsm@
+IKSEMEL=@PBX_LIBIKSEMEL@
IXJUSER=@PBX_IXJUSER@
diff --git a/channels/Makefile b/channels/Makefile
index b68c0649f..4a8d43b4a 100644
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -131,6 +131,12 @@ chan_vpb.o: chan_vpb.c
chan_vpb.so: chan_vpb.o
$(CXX) $(SOLINK) -o $@ $< -lvpb -lpthread -lm -ldl
+chan_jingle.o: chan_jingle.c
+ $(CC) -c -o $@ $(CFLAGS) $(IKSEMEL_INCLUDE) $<
+
+chan_jingle.so: chan_jingle.o
+ $(CC) $(SOLINK) -o $@ $< $(IKSEMEL_LIB)
+
ifeq ($(OSARCH),Linux)
chan_h323.so: chan_h323.o h323/libchanh323.a h323/Makefile.ast
$(CC) $(SOLINK) $(H323LDFLAGS) -o $@ $< h323/libchanh323.a $(H323LDLIBS) -lstdc++
diff --git a/channels/chan_jingle.c b/channels/chan_jingle.c
new file mode 100644
index 000000000..4b0a38a31
--- /dev/null
+++ b/channels/chan_jingle.c
@@ -0,0 +1,1695 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Matt O'Gorman <mogorman@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \author Matt O'Gorman <mogorman@digium.com>
+ *
+ * \brief Jingle Channel Driver
+ *
+ * \ingroup channel_drivers
+ */
+
+/*** MODULEINFO
+ <depend>iksemel</depend>
+ ***/
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/signal.h>
+#include <iksemel.h>
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk/lock.h"
+#include "asterisk/channel.h"
+#include "asterisk/config.h"
+#include "asterisk/logger.h"
+#include "asterisk/module.h"
+#include "asterisk/pbx.h"
+#include "asterisk/options.h"
+#include "asterisk/lock.h"
+#include "asterisk/sched.h"
+#include "asterisk/io.h"
+#include "asterisk/rtp.h"
+#include "asterisk/acl.h"
+#include "asterisk/callerid.h"
+#include "asterisk/file.h"
+#include "asterisk/cli.h"
+#include "asterisk/app.h"
+#include "asterisk/musiconhold.h"
+#include "asterisk/manager.h"
+#include "asterisk/stringfields.h"
+#include "asterisk/utils.h"
+#include "asterisk/causes.h"
+#include "asterisk/astobj.h"
+#include "asterisk/jabber.h"
+#include "asterisk/jingle.h"
+
+#define JINGLE_CONFIG "jingle.conf"
+
+enum jingle_protocol {
+ AJI_PROTOCOL_UDP = 1,
+ AJI_PROTOCOL_SSLTCP = 2,
+};
+
+enum jingle_connect_type {
+ AJI_CONNECT_STUN = 1,
+ AJI_CONNECT_LOCAL = 2,
+ AJI_CONNECT_RELAY = 3,
+};
+
+struct jingle_pvt {
+ ast_mutex_t lock; /* Channel private lock */
+ time_t laststun;
+ struct jingle *parent; /* Parent client */
+ char sid[100];
+ char from[100];
+ char ring[10]; /* Message ID of ring */
+ iksrule *ringrule; /* Rule for matching RING request */
+ int initiator; /* If we're the initiator */
+ int alreadygone;
+ int capability;
+ struct ast_codec_pref prefs;
+ struct jingle_candidate *theircandidates;
+ struct jingle_candidate *ourcandidates;
+ char cid_num[80]; /*!< Caller ID num */
+ char cid_name[80]; /*!< Caller ID name */
+ char exten[80]; /* Called extension */
+ struct ast_channel *owner; /* Master Channel */
+ struct ast_rtp *rtp; /*!< RTP Session */
+ struct ast_rtp *vrtp;
+ int jointcapability; /*!< Supported capability at both ends (codecs ) */
+ int peercapability;
+ struct jingle_pvt *next; /* Next entity */
+};
+
+struct jingle_candidate {
+ char name[100];
+ enum jingle_protocol protocol;
+ double preference;
+ char username[100];
+ char password[100];
+ enum jingle_connect_type type;
+ char network[6];
+ int generation;
+ char ip[16];
+ int port;
+ int receipt;
+ struct jingle_candidate *next;
+};
+
+struct jingle {
+ ASTOBJ_COMPONENTS(struct jingle);
+ struct aji_client *connection;
+ struct aji_buddy *buddy;
+ struct jingle_pvt *p;
+ struct ast_codec_pref prefs;
+ int amaflags; /*!< AMA Flags */
+ char user[100];
+ char context[100];
+ char accountcode[AST_MAX_ACCOUNT_CODE]; /* Account code */
+ int capability;
+ ast_group_t callgroup; /*!< Call group */
+ ast_group_t pickupgroup; /*!< Pickup group */
+ int callingpres; /*!< Calling presentation */
+ int allowguest;
+ char language[MAX_LANGUAGE]; /*!< Default language for prompts */
+ char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */
+};
+
+struct jingle_container {
+ ASTOBJ_CONTAINER_COMPONENTS(struct jingle);
+};
+
+static const char desc[] = "Jingle Channel";
+static const char type[] = "Jingle";
+static const char tdesc[] = "Jingle Channel Driver";
+
+static int usecnt = 0;
+AST_MUTEX_DEFINE_STATIC(usecnt_lock);
+
+
+static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
+
+/* Protect the interface list (of sip_pvt's) */
+AST_MUTEX_DEFINE_STATIC(jinglelock);
+
+AST_MUTEX_DEFINE_STATIC(rand_lock); /*!< Lock for thread-safe random generator */
+
+
+static struct ast_channel *jingle_request(const char *type, int format, void *data, int *cause);
+static int jingle_digit(struct ast_channel *ast, char digit);
+static int jingle_call(struct ast_channel *ast, char *dest, int timeout);
+static int jingle_hangup(struct ast_channel *ast);
+static int jingle_answer(struct ast_channel *ast);
+static int jingle_newcall(struct jingle *client, ikspak *pak);
+static struct ast_frame *jingle_read(struct ast_channel *ast);
+static int jingle_write(struct ast_channel *ast, struct ast_frame *f);
+static int jingle_indicate(struct ast_channel *ast, int condition);
+static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
+static int jingle_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
+static struct jingle_pvt *jingle_alloc(struct jingle *client, const char *from, const char *sid);
+
+/* PBX interface structure for channel registration */
+static const struct ast_channel_tech jingle_tech = {
+ .type = type,
+ .description = tdesc,
+ .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
+ .requester = jingle_request,
+ .send_digit = jingle_digit,
+// .devicestate = jingle_devicestate,
+// .transfer = jingle_transfer,
+ .bridge = ast_rtp_bridge,
+ .call = jingle_call,
+ .hangup = jingle_hangup,
+ .answer = jingle_answer,
+ .read = jingle_read,
+ .write = jingle_write,
+ .exception = jingle_read,
+ .indicate = jingle_indicate,
+ .fixup = jingle_fixup,
+ .send_html = jingle_sendhtml,
+};
+
+static struct sockaddr_in bindaddr = { 0, }; /*!< The address we bind to */
+
+static struct sched_context *sched; /*!< The scheduling context */
+static struct io_context *io; /*!< The IO context */
+static struct in_addr __ourip;
+/*----- RTP interface functions */
+static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp,
+ struct ast_rtp *vrtp, int codecs, int nat_active);
+static struct ast_rtp *jingle_get_rtp_peer(struct ast_channel *chan);
+static int jingle_get_codec(struct ast_channel *chan);
+
+static struct ast_rtp_protocol jingle_rtp = {
+ type: "jingle",
+ get_rtp_info: jingle_get_rtp_peer,
+ set_rtp_peer: jingle_set_rtp_peer,
+ get_codec: jingle_get_codec,
+};
+
+char externip[16];
+
+struct jingle_container jingles;
+
+static void jingle_member_destroy(struct jingle *obj)
+{
+ free(obj);
+}
+
+static struct jingle *find_jingle(char *name, char *connection)
+{
+ struct jingle *jingle = NULL;
+
+ jingle = ASTOBJ_CONTAINER_FIND(&jingles, name);
+ if (!jingle && strchr(name, '@'))
+ jingle = ASTOBJ_CONTAINER_FIND_FULL(&jingles, name, user,,, strcasecmp);
+
+ if (!jingle) { /* guest call */
+ ASTOBJ_CONTAINER_TRAVERSE(&jingles, 1, {
+ ASTOBJ_WRLOCK(iterator);
+ if (!strcasecmp(iterator->name, "guest")) {
+ if (!strcasecmp(iterator->connection->jid->partial, connection)) {
+ jingle = iterator;
+ break;
+ } else if (!strcasecmp(iterator->connection->name, connection)) {
+ jingle = iterator;
+ break;
+ }
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+
+ }
+ return jingle;
+}
+
+
+static void add_codec_to_answer(const struct jingle_pvt *p, int codec, iks *dcodecs)
+{
+ ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
+ if (!strcasecmp("ulaw", ast_getformatname(codec))) {
+ iks *payload_eg711u, *payload_pcmu;
+ payload_pcmu = iks_new("payload-type");
+ iks_insert_attrib(payload_pcmu, "id", "0");
+ iks_insert_attrib(payload_pcmu, "name", "PCMU");
+ iks_insert_attrib(payload_pcmu, "xmlns", "http://www.google.com/session/phone");
+ payload_eg711u = iks_new("payload-type");
+ iks_insert_attrib(payload_eg711u, "id", "100");
+ iks_insert_attrib(payload_eg711u, "name", "EG711U");
+ iks_insert_attrib(payload_eg711u, "xmlns", "http://www.google.com/session/phone");
+ iks_insert_node(dcodecs, payload_pcmu);
+ // iks_insert_node(dcodecs, payload_eg711u);
+ }
+ if (!strcasecmp("alaw", ast_getformatname(codec))) {
+ iks *payload_eg711a, *payload_pcma;
+ payload_pcma = iks_new("payload-type");
+ iks_insert_attrib(payload_pcma, "id", "8");
+ iks_insert_attrib(payload_pcma, "name", "PCMA");
+ iks_insert_attrib(payload_pcma, "xmlns", "http://www.google.com/session/phone");
+ payload_eg711a = iks_new("payload-type");
+ iks_insert_attrib(payload_eg711a, "id", "101");
+ iks_insert_attrib(payload_eg711a, "name", "EG711A");
+ iks_insert_attrib(payload_eg711a, "xmlns", "http://www.google.com/session/phone");
+ iks_insert_node(dcodecs, payload_pcma);
+ iks_insert_node(dcodecs, payload_eg711a);
+ }
+ if (!strcasecmp("ilbc", ast_getformatname(codec))) {
+ iks *payload_ilbc;
+ payload_ilbc = iks_new("payload-type");
+ iks_insert_attrib(payload_ilbc, "id", "102");
+ iks_insert_attrib(payload_ilbc, "name", "iLBC");
+ iks_insert_attrib(payload_ilbc, "xmlns", "http://www.google.com/session/phone");
+ iks_insert_node(dcodecs, payload_ilbc);
+ }
+ if (!strcasecmp("g723", ast_getformatname(codec))) {
+ iks *payload_g723;
+ payload_g723 = iks_new("payload-type");
+ iks_insert_attrib(payload_g723, "id", "4");
+ iks_insert_attrib(payload_g723, "name", "G723");
+ iks_insert_attrib(payload_g723, "xmlns", "http://www.google.com/session/phone");
+ iks_insert_node(dcodecs, payload_g723);
+ }
+ ast_rtp_lookup_code(p->rtp, 1, codec);
+}
+
+static int jingle_accept_call(struct jingle *client, struct jingle_pvt *p)
+{
+ struct jingle_pvt *tmp = client->p;
+ struct aji_client *c = client->connection;
+ iks *iq, *jingle, *dcodecs, *payload_red, *payload_audio, *payload_cn;
+ int x;
+ int pref_codec = 0;
+ int alreadysent = 0;
+
+ if (p->initiator)
+ return 1;
+
+ iq = iks_new("iq");
+ jingle = iks_new(GOOGLE_NODE);
+ dcodecs = iks_new("description");
+ if (iq && jingle && dcodecs) {
+ iks_insert_attrib(dcodecs, "xmlns", "http://www.google.com/session/phone");
+
+ for (x = 0; x < 32; x++) {
+ if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
+ break;
+ if (!(client->capability & pref_codec))
+ continue;
+ if (alreadysent & pref_codec)
+ continue;
+ if (pref_codec <= AST_FORMAT_MAX_AUDIO)
+ add_codec_to_answer(p, pref_codec, dcodecs);
+ else
+ add_codec_to_answer(p, pref_codec, dcodecs);
+ alreadysent |= pref_codec;
+ }
+ payload_red = iks_new("payload-type");
+ iks_insert_attrib(payload_red, "id", "117");
+ iks_insert_attrib(payload_red, "name", "red");
+ iks_insert_attrib(payload_red, "xmlns", "http://www.google.com/session/phone");
+ payload_audio = iks_new("payload-type");
+ iks_insert_attrib(payload_audio, "id", "106");
+ iks_insert_attrib(payload_audio, "name", "audio/telephone-event");
+ iks_insert_attrib(payload_audio, "xmlns", "http://www.google.com/session/phone");
+ payload_cn = iks_new("payload-type");
+ iks_insert_attrib(payload_cn, "id", "13");
+ iks_insert_attrib(payload_cn, "name", "CN");
+ iks_insert_attrib(payload_cn, "xmlns", "http://www.google.com/session/phone");
+
+
+ iks_insert_attrib(iq, "type", "set");
+ // iks_insert_attrib(iq,"from",client->connection->jid->full);
+ iks_insert_attrib(iq, "to", (p->from) ? p->from : client->user);
+ iks_insert_attrib(iq, "id", client->connection->mid);
+ ast_aji_increment_mid(client->connection->mid);
+
+ iks_insert_attrib(jingle, "xmlns", "http://www.google.com/session");
+ iks_insert_attrib(jingle, "type", JINGLE_ACCEPT);
+ ast_verbose("WOOH %d\n", p->initiator);
+ iks_insert_attrib(jingle, "initiator",
+ p->initiator ? client->connection->jid->full : p->from);
+ iks_insert_attrib(jingle, GOOGLE_SID, tmp->sid);
+ iks_insert_node(iq, jingle);
+ iks_insert_node(jingle, dcodecs);
+ iks_insert_node(dcodecs, payload_red);
+ iks_insert_node(dcodecs, payload_audio);
+ iks_insert_node(dcodecs, payload_cn);
+
+ iks_send(c->p, iq);
+ iks_delete(payload_red);
+ iks_delete(payload_audio);
+ iks_delete(payload_cn);
+ iks_delete(dcodecs);
+ iks_delete(jingle);
+ iks_delete(iq);
+ }
+ return 1;
+}
+
+static int jingle_ringing_ack(void *data, ikspak *pak)
+{
+ struct jingle_pvt *p = data;
+ if (p->ringrule)
+ iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
+ p->ringrule = NULL;
+ if (p->owner)
+ ast_queue_control(p->owner, AST_CONTROL_RINGING);
+ return IKS_FILTER_EAT;
+}
+
+static int jingle_answer(struct ast_channel *ast)
+{
+ struct jingle_pvt *p = ast->tech_pvt;
+ struct jingle *client = p->parent;
+ int res = 0;
+ ast_log(LOG_DEBUG, "Answer!\n");
+ ast_mutex_lock(&p->lock);
+ jingle_accept_call(client, p);
+ ast_mutex_unlock(&p->lock);
+ return res;
+}
+
+static force_inline int thread_safe_rand(void)
+{
+ int val;
+
+ ast_mutex_lock(&rand_lock);
+ val = rand();
+ ast_mutex_unlock(&rand_lock);
+
+ return val;
+}
+
+static struct ast_rtp *jingle_get_rtp_peer(struct ast_channel *chan)
+{
+ struct jingle_pvt *p;
+ struct ast_rtp *rtp = NULL;
+ p = chan->tech_pvt;
+ if (!p)
+ return NULL;
+ ast_mutex_lock(&p->lock);
+ if (p->rtp)
+ rtp = p->rtp;
+ ast_mutex_unlock(&p->lock);
+ return rtp;
+}
+
+static int jingle_get_codec(struct ast_channel *chan)
+{
+ struct jingle_pvt *p = chan->tech_pvt;
+ return p->peercapability;
+}
+
+static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
+{
+ struct jingle_pvt *p;
+
+ p = chan->tech_pvt;
+ if (!p)
+ return -1;
+ ast_mutex_lock(&p->lock);
+
+/* if (rtp)
+ ast_rtp_get_peer(rtp, &p->redirip);
+ else
+ memset(&p->redirip, 0, sizeof(p->redirip));
+ p->redircodecs = codecs; */
+
+ /* Reset lastrtprx timer */
+ ast_mutex_unlock(&p->lock);
+ return 0;
+}
+
+static int jingle_response(struct jingle *client, ikspak *pak, const char *reasonstr)
+{
+ iks *response, *error = NULL, *reason = NULL;
+ int res = -1;
+
+ response = iks_new("iq");
+ if (response) {
+ iks_insert_attrib(response, "type", "result");
+ iks_insert_attrib(response, "from", client->connection->jid->full);
+ iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
+ iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
+ if (reasonstr) {
+ error = iks_new("error");
+ if (error) {
+ iks_insert_attrib(error, "type", "cancel");
+ reason = iks_new(reasonstr);
+ if (reason)
+ iks_insert_node(error, reason);
+ iks_insert_node(response, error);
+ }
+ }
+ iks_send(client->connection->p, response);
+ if (reason)
+ iks_delete(reason);
+ if (error)
+ iks_delete(error);
+ iks_delete(response);
+ res = 0;
+ }
+ return res;
+}
+
+static int jingle_is_answered(struct jingle *client, ikspak *pak)
+{
+ struct jingle_pvt *tmp;
+
+ ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+ tmp = client->p;
+ /* Make sure our new call doesn't exist yet */
+ while (tmp) {
+ ast_verbose("FFFFF %s\n", tmp->sid);
+ if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) {
+ break;
+ }
+ tmp = tmp->next;
+ }
+
+ if (tmp) {
+ if (tmp->owner)
+ ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
+ } else
+ ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
+ jingle_response(client, pak, NULL);
+ return 1;
+}
+
+static int jingle_hangup_farend(struct jingle *client, ikspak *pak)
+{
+ struct jingle_pvt *tmp;
+
+ ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+ tmp = client->p;
+ /* Make sure our new call doesn't exist yet */
+ while (tmp) {
+ if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) {
+ break;
+ }
+ tmp = tmp->next;
+ }
+
+ if (tmp) {
+ tmp->alreadygone = 1;
+ ast_queue_hangup(tmp->owner);
+ } else
+ ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
+ jingle_response(client, pak, NULL);
+ ast_verbose("END CALL\n");
+ return 1;
+}
+
+static int jingle_create_candidates(struct jingle *client, struct jingle_pvt *p, char *sid, char *from)
+{
+ struct jingle_candidate *tmp;
+ struct aji_client *c = client->connection;
+ struct jingle_candidate *ours1 = NULL, *ours2 = NULL;
+ struct sockaddr_in sin;
+ struct sockaddr_in dest;
+ struct in_addr us;
+ char iabuf[INET_ADDRSTRLEN];
+
+ iks *iq, *jingle, *candidate;
+ char user[17], pass[17], preference[5], port[7];
+
+
+ iq = iks_new("iq");
+ jingle = iks_new(GOOGLE_NODE);
+ candidate = iks_new("candidate");
+ ours1 = (struct jingle_candidate *) ast_calloc(1, sizeof(struct jingle_candidate));
+ ours2 = (struct jingle_candidate *) ast_calloc(1, sizeof(struct jingle_candidate));
+ if (!iq || !jingle || !candidate || !ours1 || !ours2) {
+ ast_log(LOG_WARNING, "out of memory!\n");
+ goto safeout;
+ }
+
+ iks_insert_node(iq, jingle);
+ iks_insert_node(jingle, candidate);
+
+ while (p) {
+ if (!strcasecmp(p->sid, sid)) {
+ break;
+ }
+ p = p->next;
+ }
+
+ if (!p) {
+ ast_log(LOG_NOTICE, "No matching jingle session!\n");
+ goto safeout;
+ }
+
+ ast_rtp_get_us(p->rtp, &sin);
+ ast_find_ourip(&us, bindaddr);
+
+ /* Setup our jingle candidates */
+ ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
+ ours1->port = ntohs(sin.sin_port);
+ ours1->preference = 1;
+ snprintf(user, sizeof(user), "%08x%08x", thread_safe_rand(), thread_safe_rand());
+ snprintf(pass, sizeof(pass), "%08x%08x", thread_safe_rand(), thread_safe_rand());
+ ast_copy_string(ours1->username, user, sizeof(ours1->username));
+ ast_copy_string(ours1->password, pass, sizeof(ours1->password));
+ ast_inet_ntoa(ours1->ip, sizeof(ours1->ip), us);
+ ours1->protocol = AJI_PROTOCOL_UDP;
+ ours1->type = AJI_CONNECT_LOCAL;
+ ours1->generation = 0;
+ p->ourcandidates = ours1;
+
+ if (!ast_strlen_zero(externip)) {
+ /* XXX We should really stun for this one not just go with externip XXX */
+ snprintf(user, sizeof(user), "%08x%08x", thread_safe_rand(), thread_safe_rand());
+ snprintf(pass, sizeof(pass), "%08x%08x", thread_safe_rand(), thread_safe_rand());
+ ast_copy_string(ours2->username, user, sizeof(ours2->username));
+ ast_copy_string(ours2->password, pass, sizeof(ours2->password));
+ ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
+ ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
+ ours2->port = ntohs(sin.sin_port);
+ ours2->preference = 0.9;
+ ours2->protocol = AJI_PROTOCOL_UDP;
+ ours2->type = AJI_CONNECT_STUN;
+ ours2->generation = 0;
+ ours1->next = ours2;
+ ours2 = NULL;
+ }
+ ours1 = NULL;
+ dest.sin_addr = __ourip; /// THIS IS BAD NEED TO FIX
+ dest.sin_port = sin.sin_port;
+
+ ast_verbose("We're at %s port %d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), us), ntohs(sin.sin_port)); /// THIS IS BAD NEED TO FIX
+
+ tmp = p->ourcandidates;
+ while (tmp) { /*send standard candidates */
+ snprintf(port, sizeof(port), "%d", tmp->port);
+ snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
+ iks_insert_attrib(iq, "from", c->jid->full);
+ iks_insert_attrib(iq, "to", from);
+ iks_insert_attrib(iq, "type", "set");
+ iks_insert_attrib(iq, "id", c->mid);
+ ast_aji_increment_mid(c->mid);
+ iks_insert_attrib(jingle, "type", "candidates");
+ iks_insert_attrib(jingle, "id", sid);
+ iks_insert_attrib(jingle, "initiator", (p->initiator) ? c->jid->full : from);
+ iks_insert_attrib(jingle, "xmlns", GOOGLE_NS);
+ iks_insert_attrib(candidate, "name", tmp->name);
+ iks_insert_attrib(candidate, "address", tmp->ip);
+ iks_insert_attrib(candidate, "port", port);
+ iks_insert_attrib(candidate, "username", tmp->username);
+ iks_insert_attrib(candidate, "password", tmp->password);
+ iks_insert_attrib(candidate, "preference", preference);
+ if (tmp->protocol == AJI_PROTOCOL_UDP)
+ iks_insert_attrib(candidate, "protocol", "udp");
+ if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
+ iks_insert_attrib(candidate, "protocol", "ssltcp");
+ if (tmp->type == AJI_CONNECT_STUN)
+ iks_insert_attrib(candidate, "type", "stun");
+ if (tmp->type == AJI_CONNECT_LOCAL)
+ iks_insert_attrib(candidate, "type", "local");
+ if (tmp->type == AJI_CONNECT_RELAY)
+ iks_insert_attrib(candidate, "type", "relay");
+ iks_insert_attrib(candidate, "network", "0");
+ iks_insert_attrib(candidate, "generation", "0");
+ iks_send(c->p, iq);
+ tmp = tmp->next;
+ }
+ p->laststun = 0;
+
+safeout:
+ if (ours1)
+ free(ours1);
+ if (ours2)
+ free(ours2);
+ if (iq)
+ iks_delete(iq);
+ if (jingle)
+ iks_delete(jingle);
+ if (candidate)
+ iks_delete(candidate);
+ return 1;
+}
+
+static struct jingle_pvt *jingle_alloc(struct jingle *client, const char *from, const char *sid)
+{
+ struct jingle_pvt *tmp = NULL;
+ struct aji_resource *resources = NULL;
+ struct aji_buddy *buddy;
+ char idroster[200];
+
+ ast_log(LOG_DEBUG, "The client is %s for alloc\n", client->name);
+ if (!sid && !strchr(from, '/')) { /*I started call! */
+ ast_verbose("shouldnt be called on inbound!\n");
+ if (!strcasecmp(client->name, "guest")) {
+ buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, from);
+ if (buddy)
+ resources = buddy->resources;
+ ast_verbose("shouldnt be called on inbound! %s ----- %s ---- %s\n",
+ client->name, from, buddy->name);
+ } else {
+ resources = client->buddy->resources;
+ }
+ while (resources) {
+ if (resources->cap->jingle) {
+ ast_verbose("WOW FOUND\n");
+ break;
+ }
+ resources = resources->next;
+ }
+ if (resources)
+ snprintf(idroster, sizeof(idroster), "%s/%s", from, resources->resource);
+ else {
+ ast_log(LOG_ERROR, "no jingle capable clients to talk to.\n");
+ return NULL;
+ }
+ }
+ if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
+ return NULL;
+ }
+ if (sid) {
+ ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
+ ast_copy_string(tmp->from, from, sizeof(tmp->from));
+ } else {
+ snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random());
+ ast_copy_string(tmp->from, idroster, sizeof(tmp->from));
+ tmp->initiator = 1;
+ }
+ tmp->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
+ tmp->parent = client;
+ if (!tmp->rtp) {
+ ast_log(LOG_WARNING, "Out of RTP sessions?\n");
+ free(tmp);
+ return NULL;
+ }
+ ast_copy_string(tmp->exten, "s", sizeof(tmp->exten));
+ ast_mutex_init(&tmp->lock);
+ ast_mutex_lock(&jinglelock);
+ tmp->next = client->p;
+ client->p = tmp;
+ ast_mutex_unlock(&jinglelock);
+ return tmp;
+}
+
+/*! \brief Start new jingle channel */
+static struct ast_channel *jingle_new(struct jingle *client, struct jingle_pvt *i, int state, const char *title)
+{
+ struct ast_channel *tmp;
+ int fmt;
+ int what;
+
+ tmp = ast_channel_alloc(1);
+ if (!tmp) {
+ ast_log(LOG_WARNING, "Unable to allocate Jingle channel structure!\n");
+ return NULL;
+ }
+ tmp->tech = &jingle_tech;
+
+ /* Select our native format based on codec preference until we receive
+ something from another device to the contrary. */
+ if (i->jointcapability)
+ what = i->jointcapability;
+ else if (i->capability)
+ what = i->capability;
+ else
+ what = global_capability;
+ tmp->nativeformats =
+ ast_codec_choose(&i->prefs, what,
+ 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
+ fmt = ast_best_codec(tmp->nativeformats);
+
+ if (title)
+ ast_string_field_build(tmp, name, "Jingle/%s-%04lx", title,
+ ast_random() & 0xffff);
+ else
+ ast_string_field_build(tmp, name, "Jingle/%s-%04lx", i->from,
+ ast_random() & 0xffff);
+
+ if (i->rtp) {
+ tmp->fds[0] = ast_rtp_fd(i->rtp);
+ tmp->fds[1] = ast_rtcp_fd(i->rtp);
+ }
+ if (i->vrtp) {
+ tmp->fds[2] = ast_rtp_fd(i->vrtp);
+ tmp->fds[3] = ast_rtcp_fd(i->vrtp);
+ }
+ if (state == AST_STATE_RING)
+ tmp->rings = 1;
+ tmp->adsicpe = AST_ADSI_UNAVAILABLE;
+ tmp->writeformat = fmt;
+ tmp->rawwriteformat = fmt;
+ tmp->readformat = fmt;
+ tmp->rawreadformat = fmt;
+ tmp->tech_pvt = i;
+
+ tmp->callgroup = client->callgroup;
+ tmp->pickupgroup = client->pickupgroup;
+ tmp->cid.cid_pres = client->callingpres;
+ if (!ast_strlen_zero(client->accountcode))
+ ast_string_field_set(tmp, accountcode, client->accountcode);
+ if (client->amaflags)
+ tmp->amaflags = client->amaflags;
+ if (!ast_strlen_zero(client->language))
+ ast_string_field_set(tmp, language, client->language);
+ if (!ast_strlen_zero(client->musicclass))
+ ast_string_field_set(tmp, musicclass, client->musicclass);
+ i->owner = tmp;
+ ast_mutex_lock(&usecnt_lock);
+ usecnt++;
+ ast_mutex_unlock(&usecnt_lock);
+ ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
+ ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
+ if (!ast_strlen_zero(i->cid_num))
+ tmp->cid.cid_num = ast_strdup(i->cid_num);
+ if (!ast_strlen_zero(i->cid_name))
+ tmp->cid.cid_name = ast_strdup(i->cid_name);
+ if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
+ tmp->cid.cid_dnid = ast_strdup(i->exten);
+ tmp->priority = 1;
+ ast_setstate(tmp, state);
+ if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
+ ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
+ tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
+ ast_hangup(tmp);
+ tmp = NULL;
+ }
+ return tmp;
+}
+
+static int jingle_action(struct jingle *client, struct jingle_pvt *p, const char *action)
+{
+ iks *request, *session = NULL;
+ int res = -1;
+
+ request = iks_new("iq");
+ if (request) {
+ iks_insert_attrib(request, "type", "set");
+ iks_insert_attrib(request, "from", client->connection->jid->full);
+ iks_insert_attrib(request, "to", p->from);
+ iks_insert_attrib(request, "id", client->connection->mid);
+ ast_aji_increment_mid(client->connection->mid);
+ session = iks_new("session");
+ if (session) {
+ iks_insert_attrib(session, "type", action);
+ iks_insert_attrib(session, "id", p->sid);
+ iks_insert_attrib(session, "initiator",
+ p->initiator ? client->connection->jid->full : p->from);
+ iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
+ iks_insert_node(request, session);
+ iks_send(client->connection->p, request);
+ iks_delete(session);
+ res = 0;
+ }
+ iks_delete(request);
+ }
+ return res;
+}
+
+static void jingle_free_candidates(struct jingle_candidate *candidate)
+{
+ struct jingle_candidate *last;
+ while (candidate) {
+ last = candidate;
+ candidate = candidate->next;
+ free(last);
+ }
+}
+
+static struct jingle_candidate *jingle_dup_candidates(struct jingle_candidate *candidate)
+{
+ struct jingle_candidate *newcan = NULL, *prev = NULL, *tmp;
+ while (candidate) {
+ tmp = malloc(sizeof(struct jingle_candidate));
+ if (tmp) {
+ memcpy(tmp, candidate, sizeof(struct jingle_candidate));
+ tmp->next = NULL;
+ if (prev)
+ prev->next = tmp;
+ else
+ newcan = tmp;
+ prev = tmp;
+ }
+ candidate = candidate->next;
+ }
+ return newcan;
+}
+
+static void jingle_free_pvt(struct jingle *client, struct jingle_pvt *p)
+{
+ struct jingle_pvt *cur, *prev = NULL;
+ cur = client->p;
+ while (cur) {
+ if (cur == p) {
+ if (prev)
+ prev->next = p->next;
+ else
+ client->p = p->next;
+ break;
+ }
+ prev = cur;
+ cur = cur->next;
+ }
+ if (p->ringrule)
+ iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
+ if (p->owner)
+ ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n");
+ if (p->rtp)
+ ast_rtp_destroy(p->rtp);
+ if (p->vrtp)
+ ast_rtp_destroy(p->vrtp);
+ jingle_free_candidates(p->theircandidates);
+ free(p);
+}
+
+
+static int jingle_newcall(struct jingle *client, ikspak *pak)
+{
+ struct jingle_pvt *p, *tmp = client->p;
+ struct ast_channel *chan;
+ int res;
+ iks *codec;
+
+ /* Make sure our new call doesn't exist yet */
+ while (tmp) {
+ if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) {
+ ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid);
+ jingle_response(client, pak, "out-of-order");
+ return -1;
+ }
+ tmp = tmp->next;
+ }
+
+ p = jingle_alloc(client, pak->from->partial, iks_find_attrib(pak->query, GOOGLE_SID));
+ if (!p) {
+ ast_log(LOG_WARNING, "Unable to allocate jingle structure!\n");
+ return -1;
+ }
+ chan = jingle_new(client, p, AST_STATE_DOWN, pak->from->user);
+ if (chan) {
+ ast_mutex_lock(&p->lock);
+ ast_copy_string(p->from, pak->from->full, sizeof(p->from));
+ if (iks_find_attrib(pak->query, GOOGLE_SID)) {
+ ast_copy_string(p->sid, iks_find_attrib(pak->query, GOOGLE_SID),
+ sizeof(p->sid));
+ }
+
+ codec = iks_child(iks_child(iks_child(pak->x)));
+ while (codec) {
+ ast_rtp_set_m_type(p->rtp, atoi(iks_find_attrib(codec, "id")));
+ ast_rtp_set_rtpmap_type(p->rtp, atoi(iks_find_attrib(codec, "id")), "audio",
+ iks_find_attrib(codec, "name"));
+ ast_verbose("yatta!!\n");
+ codec = iks_next(codec);
+ }
+
+ ast_mutex_unlock(&p->lock);
+ ast_setstate(chan, AST_STATE_RING);
+ res = ast_pbx_start(chan);
+
+ switch (res) {
+ case AST_PBX_FAILED:
+ ast_log(LOG_WARNING, "Failed to start PBX :(\n");
+ jingle_response(client, pak, "service-unavailable");
+ break;
+ case AST_PBX_CALL_LIMIT:
+ ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
+ jingle_response(client, pak, "service-unavailable");
+ break;
+ case AST_PBX_SUCCESS:
+ jingle_response(client, pak, NULL);
+ jingle_create_candidates(client, p,
+ iks_find_attrib(pak->query, GOOGLE_SID),
+ iks_find_attrib(pak->x, "from"));
+ /* nothing to do */
+ break;
+ }
+ } else {
+ jingle_free_pvt(client, p);
+ }
+ return 1;
+}
+
+static int jingle_update_stun(struct jingle *client, struct jingle_pvt *p)
+{
+ struct jingle_candidate *tmp;
+ struct hostent *hp;
+ struct ast_hostent ahp;
+ struct sockaddr_in sin;
+
+ if (time(NULL) == p->laststun)
+ return 0;
+
+ tmp = p->theircandidates;
+ p->laststun = time(NULL);
+ while (tmp) {
+ char username[256];
+ hp = ast_gethostbyname(tmp->ip, &ahp);
+ sin.sin_family = AF_INET;
+ memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
+ sin.sin_port = htons(tmp->port);
+ snprintf(username, sizeof(username), "%s%s", tmp->username,
+ p->ourcandidates->username);
+
+ ast_rtp_stun_request(p->rtp, &sin, username);
+ tmp = tmp->next;
+ }
+ return 1;
+}
+
+static int jingle_add_candidate(struct jingle *client, ikspak *pak)
+{
+ struct jingle_pvt *p = NULL, *tmp = NULL;
+ struct aji_client *c = client->connection;
+ struct jingle_candidate *newcandidate = NULL;
+ iks *traversenodes = NULL, *receipt = NULL;
+ newcandidate =
+ (struct jingle_candidate *) ast_calloc(1, sizeof(struct jingle_candidate));
+ if (!newcandidate)
+ return 0;
+ memset(newcandidate, 0, sizeof(struct jingle_candidate));
+ tmp = client->p;
+ while (tmp) {
+ if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) {
+ p = tmp;
+ break;
+ }
+ tmp = tmp->next;
+ }
+
+ if (!p) {
+ ast_verbose("NO MATCH\n");
+ return -1;
+ }
+
+ traversenodes = pak->query;
+ while(traversenodes) {
+ ast_verbose("OOH %s\n", iks_name(traversenodes));
+ if(!strcasecmp(iks_name(traversenodes), "session")) {
+ ast_verbose("XXXX OOH A SESSION\n");
+ traversenodes = iks_child(traversenodes);
+ continue;
+ }
+ if(!strcasecmp(iks_name(traversenodes), "candidate")) {
+ ast_verbose("XXXX OOH A CANDIDATE\n");
+ newcandidate =
+ (struct jingle_candidate *) ast_calloc(1, sizeof(struct jingle_candidate));
+ if (!newcandidate)
+ return 0;
+ memset(newcandidate, 0, sizeof(struct jingle_candidate));
+ ast_copy_string(newcandidate->name, iks_find_attrib(traversenodes, "name"),
+ sizeof(newcandidate->name));
+ ast_copy_string(newcandidate->ip, iks_find_attrib(traversenodes, "address"),
+ sizeof(newcandidate->ip));
+ newcandidate->port = atoi(iks_find_attrib(traversenodes, "port"));
+ ast_copy_string(newcandidate->username, iks_find_attrib(traversenodes, "username"),
+ sizeof(newcandidate->username));
+ ast_copy_string(newcandidate->password, iks_find_attrib(traversenodes, "password"),
+ sizeof(newcandidate->password));
+ newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference"));
+ if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "udp"))
+ newcandidate->protocol = AJI_PROTOCOL_UDP;
+ if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "ssltcp"))
+ newcandidate->protocol = AJI_PROTOCOL_SSLTCP;
+
+ if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "stun"))
+ newcandidate->type = AJI_CONNECT_STUN;
+ if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "local"))
+ newcandidate->type = AJI_CONNECT_LOCAL;
+ if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "relay"))
+ newcandidate->type = AJI_CONNECT_RELAY;
+ ast_copy_string(newcandidate->network, iks_find_attrib(traversenodes, "network"),
+ sizeof(newcandidate->network));
+ newcandidate->generation = atoi(iks_find_attrib(traversenodes, "generation"));
+ newcandidate->next = NULL;
+
+ newcandidate->next = p->theircandidates;
+ p->theircandidates = newcandidate;
+ p->laststun = 0;
+ jingle_update_stun(p->parent, p);
+ newcandidate = NULL;
+ }
+ traversenodes = iks_next(traversenodes);
+ }
+
+ receipt = iks_new("iq");
+ iks_insert_attrib(receipt, "type", "result");
+ iks_insert_attrib(receipt, "from", c->jid->full);
+ iks_insert_attrib(receipt, "to", iks_find_attrib(pak->x, "from"));
+ iks_insert_attrib(receipt, "id", iks_find_attrib(pak->x, "id"));
+ iks_send(c->p, receipt);
+ iks_delete(receipt);
+
+ return 1;
+}
+
+static struct ast_frame *jingle_rtp_read(struct ast_channel *ast, struct jingle_pvt *p)
+{
+ struct ast_frame *f;
+ if (!p->rtp) {
+ return &ast_null_frame;
+ }
+ f = ast_rtp_read(p->rtp);
+ jingle_update_stun(p->parent, p);
+ if (p->owner) {
+ /* We already hold the channel lock */
+ if (f->frametype == AST_FRAME_VOICE) {
+ if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
+ if (option_debug)
+ ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
+ p->owner->nativeformats =
+ (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
+ ast_set_read_format(p->owner, p->owner->readformat);
+ ast_set_write_format(p->owner, p->owner->writeformat);
+ }
+/* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
+ f = ast_dsp_process(p->owner, p->vad, f);
+ if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
+ ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
+ } */
+ }
+ }
+ return f;
+}
+
+static struct ast_frame *jingle_read(struct ast_channel *ast)
+{
+ struct ast_frame *fr;
+ struct jingle_pvt *p = ast->tech_pvt;
+
+ ast_mutex_lock(&p->lock);
+ fr = jingle_rtp_read(ast, p);
+ ast_mutex_unlock(&p->lock);
+ return fr;
+}
+
+/*! \brief Send frame to media channel (rtp) */
+static int jingle_write(struct ast_channel *ast, struct ast_frame *frame)
+{
+ struct jingle_pvt *p = ast->tech_pvt;
+ int res = 0;
+
+ switch (frame->frametype) {
+ case AST_FRAME_VOICE:
+ if (!(frame->subclass & ast->nativeformats)) {
+ ast_log(LOG_WARNING,
+ "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
+ frame->subclass, ast->nativeformats, ast->readformat,
+ ast->writeformat);
+ return 0;
+ }
+ if (p) {
+ ast_mutex_lock(&p->lock);
+ if (p->rtp) {
+ res = ast_rtp_write(p->rtp, frame);
+ }
+ ast_mutex_unlock(&p->lock);
+ }
+ break;
+ case AST_FRAME_VIDEO:
+ if (p) {
+ ast_mutex_lock(&p->lock);
+ if (p->vrtp) {
+ res = ast_rtp_write(p->vrtp, frame);
+ }
+ ast_mutex_unlock(&p->lock);
+ }
+ break;
+ case AST_FRAME_IMAGE:
+ return 0;
+ break;
+ default:
+ ast_log(LOG_WARNING, "Can't send %d type frames with Jingle write\n",
+ frame->frametype);
+ return 0;
+ }
+
+ return res;
+}
+
+static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
+{
+ struct jingle_pvt *p = newchan->tech_pvt;
+ ast_mutex_lock(&p->lock);
+
+ if ((p->owner != oldchan)) {
+ ast_mutex_unlock(&p->lock);
+ return -1;
+ }
+ if (p->owner == oldchan)
+ p->owner = newchan;
+ ast_mutex_unlock(&p->lock);
+ return 0;
+}
+
+static int jingle_indicate(struct ast_channel *ast, int condition)
+{
+ ast_log(LOG_NOTICE, "XXX Implement jingle indicate XXX\n");
+
+ return -1;
+}
+
+static int jingle_digit(struct ast_channel *ast, char digit)
+{
+ ast_log(LOG_NOTICE, "XXX Implement jingle digit XXX\n");
+
+ return -1;
+}
+
+static int jingle_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
+{
+ ast_log(LOG_NOTICE, "XXX Implement jingle sendhtml XXX\n");
+
+ return -1;
+}
+static int jingle_transmit_invite(struct jingle_pvt *p)
+{
+ struct jingle *jingle = NULL;
+ struct aji_client *client = NULL;
+ iks *iq, *desc, *session;
+ iks *payload_eg711u, *payload_pcmu;
+
+ jingle = p->parent;
+ client = jingle->connection;
+ iq = iks_new("iq");
+ desc = iks_new("description");
+ session = iks_new("session");
+ iks_insert_attrib(iq, "type", "set");
+ iks_insert_attrib(iq, "to", p->from);
+ iks_insert_attrib(iq, "from", client->jid->full);
+ iks_insert_attrib(iq, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_insert_attrib(session, "type", "initiate");
+ iks_insert_attrib(session, "id", p->sid);
+ iks_insert_attrib(session, "initiator", client->jid->full);
+ iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
+ iks_insert_attrib(desc, "xmlns", "http://www.google.com/session/phone");
+ payload_pcmu = iks_new("payload-type");
+ iks_insert_attrib(payload_pcmu, "id", "0");
+ iks_insert_attrib(payload_pcmu, "name", "PCMU");
+ payload_eg711u = iks_new("payload-type");
+ iks_insert_attrib(payload_eg711u, "id", "100");
+ iks_insert_attrib(payload_eg711u, "name", "EG711U");
+ iks_insert_node(desc, payload_pcmu);
+ iks_insert_node(desc, payload_eg711u);
+ iks_insert_node(iq, session);
+ iks_insert_node(session, desc);
+ iks_send(client->p, iq);
+ iks_delete(iq);
+ iks_delete(desc);
+ iks_delete(session);
+ iks_delete(payload_eg711u);
+ iks_delete(payload_pcmu);
+ return 0;
+}
+
+static int jingle_auto_congest(void *nothing)
+{
+ struct jingle_pvt *p = nothing;
+
+ ast_mutex_lock(&p->lock);
+ if (p->owner) {
+ /* XXX fails on possible deadlock */
+ if (!ast_channel_trylock(p->owner)) {
+ ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
+ ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
+ ast_channel_unlock(p->owner);
+ }
+ }
+ ast_mutex_unlock(&p->lock);
+ return 0;
+}
+
+/*! \brief Initiate new call, part of PBX interface
+ * dest is the dial string */
+static int jingle_call(struct ast_channel *ast, char *dest, int timeout)
+{
+ struct jingle_pvt *p = ast->tech_pvt;
+
+ if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
+ ast_log(LOG_WARNING, "jingle_call called on %s, neither down nor reserved\n", ast->name);
+ return -1;
+ }
+
+ ast_setstate(ast, AST_STATE_RING);
+ p->jointcapability = p->capability;
+ if (!p->ringrule) {
+ ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
+ p->ringrule = iks_filter_add_rule(p->parent->connection->f, jingle_ringing_ack, p,
+ IKS_RULE_ID, p->ring, IKS_RULE_DONE);
+ } else {
+ ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
+ }
+ jingle_transmit_invite(p);
+ jingle_create_candidates(p->parent, p, p->sid, p->from);
+
+ return 0;
+}
+
+/*! \brief Hangup a call through the jingle proxy channel */
+static int jingle_hangup(struct ast_channel *ast)
+{
+ struct jingle_pvt *p = ast->tech_pvt;
+ struct jingle *client;
+
+ ast_mutex_lock(&p->lock);
+ client = p->parent;
+ p->owner = NULL;
+ ast->tech_pvt = NULL;
+ if (!p->alreadygone)
+ jingle_action(client, p, "terminate");
+ ast_mutex_unlock(&p->lock);
+
+ jingle_free_pvt(client, p);
+ ast_mutex_lock(&usecnt_lock);
+ usecnt--;
+ ast_mutex_unlock(&usecnt_lock);
+
+ return 0;
+}
+
+/*! \brief Part of PBX interface */
+static struct ast_channel *jingle_request(const char *type, int format, void *data, int *cause)
+{
+ struct jingle_pvt *p = NULL;
+ struct jingle *client = NULL;
+ char *sender = NULL, *to = NULL, *s = NULL;
+ struct ast_channel *chan = NULL;
+
+ if (data) {
+ s = ast_strdupa((char *) data);
+ if (s) {
+ sender = strsep(&s, "/");
+ if (sender && (sender[0] != '\0'))
+ to = strsep(&s, "/");
+ if (!to) {
+ ast_log(LOG_ERROR, "Bad arguments\n");
+ if (s)
+ free(s);
+ return NULL;
+ }
+ }
+ }
+ ast_verbose("to : %s\n from :%s\n", to, sender);
+ client = find_jingle(to, sender);
+ if (!client) {
+ ast_log(LOG_WARNING, "Could not find Recipiant.\n");
+ if (s)
+ free(s);
+ return NULL;
+ }
+ p = jingle_alloc(client, to, NULL);
+ if (p) {
+ chan = jingle_new(client, p, AST_STATE_DOWN, to);
+ }
+ return chan;
+}
+
+#if 0
+/*! \brief CLI command "jingle show channels" */
+static int jingle_show(int fd, int argc, char **argv)
+{
+ struct jingle_pvt *p = NULL;
+ struct jingle *peer = NULL;
+ struct jingle_candidate *tmp;
+ struct jingle *client = NULL;
+ client = ast_aji_get_client("asterisk");
+ if (argc != 3)
+ return RESULT_SHOWUSAGE;
+ ast_mutex_lock(&jinglelock);
+ if (client)
+ p = jingles->p;
+ while (p) {
+ ast_mutex_lock(&p->lock);
+ ast_cli(fd, "SID = %s\n", p->sid);
+ tmp = p->candidates;
+ while (tmp) {
+ ast_verbose("port %d\n", tmp->port);
+ tmp = tmp->next;
+ }
+ ast_mutex_unlock(&p->lock);
+ p = p->next;
+ }
+ if (!jingles->p)
+ ast_cli(fd, "No jingle channels in use\n");
+ ast_mutex_unlock(&jinglelock);
+ return RESULT_SUCCESS;
+}
+#endif
+static char show_jingle_usage[] =
+ "Usage: jingle show channels\n"
+ " Provides summary information on active jingle channels.\n";
+
+//static struct ast_cli_entry cli_show_jingle = {
+// { "jingle", "show", "channels", NULL }, jingle_show,
+// "Show status of jingle channels", show_jingle_usage, NULL };
+
+
+static int jingle_parser(void *data, ikspak *pak)
+{
+ struct jingle *client = ASTOBJ_REF((struct jingle *) data);
+
+ ast_verbose("WOOHOO!!!\n");
+ if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", JINGLE_INITIATE)) {
+ /* New call */
+ jingle_newcall(client, pak);
+ } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", GOOGLE_NEGOTIATE)) {
+ ast_log(LOG_DEBUG, "About to add candidate!\n");
+ jingle_add_candidate(client, pak);
+ ast_log(LOG_DEBUG, "Candidate Added!\n");
+ } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", GOOGLE_ACCEPT)) {
+ jingle_is_answered(client, pak);
+ } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", "terminate")) {
+ ast_verbose("not this\n");
+ jingle_hangup_farend(client, pak);
+ } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", "reject")) {
+ ast_verbose("not this\n");
+ jingle_hangup_farend(client, pak);
+ }
+ ASTOBJ_UNREF(client, jingle_member_destroy);
+ return IKS_FILTER_EAT;
+}
+
+static struct jingle_candidate *jingle_create_candidate(char *args)
+{
+ char *name, *type, *preference, *protocol;
+ struct jingle_candidate *res;
+ res = malloc(sizeof(struct jingle_candidate));
+ memset(res, 0, sizeof(struct jingle_candidate));
+ if (args)
+ name = args;
+ if ((args = strchr(args, ','))) {
+ *args = '\0';
+ args++;
+ preference = args;
+ }
+ if ((args = strchr(args, ','))) {
+ *args = '\0';
+ args++;
+ protocol = args;
+ }
+ if ((args = strchr(args, ','))) {
+ *args = '\0';
+ args++;
+ type = args;
+ }
+ if (name)
+ ast_copy_string(res->name, name, sizeof(res->name));
+ if (preference) {
+ res->preference = atof(preference);
+ }
+ if (protocol) {
+ if (!strcasecmp("udp", protocol))
+ res->protocol = AJI_PROTOCOL_UDP;
+ if (!strcasecmp("ssltcp", protocol))
+ res->protocol = AJI_PROTOCOL_SSLTCP;
+ }
+ if (type) {
+ if (!strcasecmp("stun", type))
+ res->type = AJI_CONNECT_STUN;
+ if (!strcasecmp("local", type))
+ res->type = AJI_CONNECT_LOCAL;
+ if (!strcasecmp("relay", type))
+ res->type = AJI_CONNECT_RELAY;
+ }
+
+ return res;
+}
+
+static int jingle_create_member(char *label, struct ast_variable *var, int allowguest,
+ struct ast_codec_pref prefs, char *context,
+ struct jingle *member)
+{
+ struct aji_client *client;
+ if (!member)
+ ast_log(LOG_WARNING, "Out of memory.\n");
+
+ ast_copy_string(member->name, label, sizeof(member->name));
+ ast_copy_string(member->user, label, sizeof(member->user));
+ ast_copy_string(member->context, context, sizeof(member->context));
+ member->allowguest = allowguest;
+ member->prefs = prefs;
+ while (var) {
+#if 0
+ struct jingle_candidate *candidate = NULL;
+#endif
+ if (!strcasecmp(var->name, "username"))
+ ast_copy_string(member->user, var->value, sizeof(member->user));
+ else if (!strcasecmp(var->name, "disallow"))
+ ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
+ else if (!strcasecmp(var->name, "allow"))
+ ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
+ else if (!strcasecmp(var->name, "context"))
+ ast_copy_string(member->context, var->value, sizeof(member->context));
+#if 0
+ else if (!strcasecmp(var->name, "candidate")) {
+ candidate = jingle_create_candidate(var->value);
+ if (candidate) {
+ candidate->next = member->ourcandidates;
+ member->ourcandidates = candidate;
+ }
+ }
+#endif
+ else if (!strcasecmp(var->name, "connection")) {
+ if ((client = ast_aji_get_client(var->value))) {
+ member->connection = client;
+ iks_filter_add_rule(client->f, jingle_parser, member, IKS_RULE_TYPE,
+ IKS_PAK_IQ, IKS_RULE_FROM_PARTIAL, member->user,
+ IKS_RULE_NS, "http://www.google.com/session",
+ IKS_RULE_DONE);
+ } else {
+ ast_log(LOG_ERROR, "connection referenced not found!\n");
+ return 0;
+ }
+ }
+ var = var->next;
+ }
+ if (member->connection && member->user)
+ member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user);
+ else {
+ ast_log(LOG_ERROR, "No Connection or Username!\n");
+ }
+ ast_verbose("LABEL: %s\n member->name %s\n member->user %s\n", label, member->name,
+ member->user);
+ return 1;
+}
+
+static int jingle_load_config(void)
+{
+ char *cat = NULL;
+ struct ast_config *cfg = NULL;
+ char context[100];
+ int allowguest = 1;
+ struct ast_variable *var;
+ struct jingle *member;
+ struct ast_codec_pref prefs;
+ struct aji_client_container *clients;
+ struct jingle_candidate *global_candidates = NULL;
+
+ cfg = ast_config_load(JINGLE_CONFIG);
+ if (!cfg) {
+ ast_log(LOG_WARNING, "No such configuration file %s\n", JINGLE_CONFIG);
+ return 0;
+ }
+
+ cat = ast_category_browse(cfg, NULL);
+ for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
+ if (!strcasecmp(var->name, "allowguest"))
+ allowguest =
+ (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
+ else if (!strcasecmp(var->name, "disallow"))
+ ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
+ else if (!strcasecmp(var->name, "allow"))
+ ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
+ else if (!strcasecmp(var->name, "context"))
+ ast_copy_string(context, var->value, sizeof(context));
+ else if (!strcasecmp(var->name, "externip"))
+ ast_copy_string(externip, var->value, sizeof(externip));
+#if 0
+ else if (!strcasecmp(var->name, "candidate")) {
+ candidate = jingle_create_candidate(var->value);
+ if (candidate) {
+ candidate->next = global_candidates;
+ global_candidates = candidate;
+ }
+ }
+#endif
+ }
+ while (cat) {
+ if (strcasecmp(cat, "general")) {
+ var = ast_variable_browse(cfg, cat);
+ member = (struct jingle *) malloc(sizeof(struct jingle));
+ memset(member, 0, sizeof(struct jingle));
+ ASTOBJ_INIT(member);
+ ASTOBJ_WRLOCK(member);
+ if (!strcasecmp(cat, "guest")) {
+ ast_copy_string(member->name, "guest", sizeof(member->name));
+ ast_copy_string(member->user, "guest", sizeof(member->user));
+ ast_copy_string(member->context, context, sizeof(member->context));
+ member->allowguest = allowguest;
+#if 0
+ member->ourcandidates = jingle_dup_candidates(global_candidates);
+#endif
+ member->prefs = prefs;
+ while (var) {
+ if (!strcasecmp(var->name, "disallow"))
+ ast_parse_allow_disallow(&member->prefs, &member->capability,
+ var->value, 0);
+ else if (!strcasecmp(var->name, "allow"))
+ ast_parse_allow_disallow(&member->prefs, &member->capability,
+ var->value, 1);
+ else if (!strcasecmp(var->name, "context"))
+ ast_copy_string(member->context, var->value,
+ sizeof(member->context));
+#if 0
+ else if (!strcasecmp(var->name, "candidate")) {
+ candidate = jingle_create_candidate(var->value);
+ if (candidate) {
+ candidate->next = member->ourcandidates;
+ member->ourcandidates = candidate;
+ }
+ }
+#endif
+ var = var->next;
+ }
+ ASTOBJ_UNLOCK(member);
+ clients = ast_aji_get_clients();
+ if (clients) {
+ ASTOBJ_CONTAINER_TRAVERSE(clients, 1, {
+ ASTOBJ_WRLOCK(iterator);
+ ASTOBJ_WRLOCK(member);
+ member->connection = iterator;
+ iks_filter_add_rule(iterator->f, jingle_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS,
+ "http://www.google.com/session", IKS_RULE_DONE);
+ ASTOBJ_UNLOCK(member);
+ ASTOBJ_CONTAINER_LINK(&jingles, member);
+ ASTOBJ_UNLOCK(iterator);
+ });
+ } else {
+ ASTOBJ_UNLOCK(member);
+ ASTOBJ_UNREF(member, jingle_member_destroy);
+ }
+ } else {
+ if (jingle_create_member(cat, var, allowguest, prefs, context, member)) {
+ ast_verbose("step member\n");
+ ASTOBJ_UNLOCK(member);
+ ASTOBJ_CONTAINER_LINK(&jingles, member);
+ ASTOBJ_UNREF(member, jingle_member_destroy);
+ } else {
+ ASTOBJ_UNLOCK(member);
+ ASTOBJ_UNREF(member, jingle_member_destroy);
+ }
+ }
+ }
+ cat = ast_category_browse(cfg, cat);
+ }
+ jingle_free_candidates(global_candidates);
+ return 1;
+}
+
+/*! \brief Load module into PBX, register channel */
+static int load_module(void *mod)
+{
+ ast_verbose("step 1\n");
+ ASTOBJ_CONTAINER_INIT(&jingles);
+ if (!jingle_load_config()) {
+ ast_log(LOG_ERROR, "Unable to read config file %s\n", JINGLE_CONFIG);
+ return -1;
+ }
+
+ sched = sched_context_create();
+
+ if (!sched) {
+ ast_log(LOG_WARNING, "Unable to create schedule context\n");
+ }
+
+ io = io_context_create();
+ if (!io) {
+ ast_log(LOG_WARNING, "Unable to create I/O context\n");
+ }
+
+ if (ast_find_ourip(&__ourip, bindaddr)) {
+ ast_log(LOG_WARNING, "Unable to get own IP address, Jingle disabled\n");
+ return 0;
+ }
+
+ ast_rtp_proto_register(&jingle_rtp);
+
+ /* Make sure we can register our channel type */
+ if (ast_channel_register(&jingle_tech)) {
+ ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+ return -1;
+ }
+// ast_cli_register(&cli_show_xmpps);
+ return 0;
+}
+
+/*! \brief Reload module */
+static int reload(void *mod)
+{
+ return 0;
+}
+
+/*! \brief Unload the jingle channel from Asterisk */
+static int unload_module(void *mod)
+{
+// struct jingle_pvt *p = NULL;
+ struct aji_client *client = NULL;
+ client = ast_aji_get_client("asterisk");
+ /* First, take us out of the channel loop */
+// ast_cli_unregister(&cli_show_xmpps);
+ ast_channel_unregister(&jingle_tech);
+ ast_rtp_proto_unregister(&jingle_rtp);
+ if (!ast_mutex_lock(&jinglelock)) {
+ /* Hangup all interfaces if they have an owner */
+ // p = jingles->p;
+ // while(p) {
+ // if (p->owner)
+ // ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
+ // p = p->next;
+ // }
+ // jingles->p = NULL;
+ ast_mutex_unlock(&jinglelock);
+ } else {
+ ast_log(LOG_WARNING, "Unable to lock the monitor\n");
+ return -1;
+ }
+ return 0;
+}
+
+static const char *key(void)
+{
+ return ASTERISK_GPL_KEY;
+}
+
+static const char *description(void)
+{
+ return desc;
+}
+
+STD_MOD(MOD_1, reload, NULL, NULL);
diff --git a/configs/extensions.conf.sample b/configs/extensions.conf.sample
index bc858a953..bce1030e0 100644
--- a/configs/extensions.conf.sample
+++ b/configs/extensions.conf.sample
@@ -505,6 +505,8 @@ include => demo
;exten => 6245,dial+101,Voicemail(6245,b) ; Voicemail (busy)
;exten => 6361,1,Dial(IAX2/JaneDoe,,rm) ; ring without time limit
;exten => 6389,1,Dial(MGCP/aaln/1@192.168.0.14)
+;exten => 6390,1,Dial(JINGLE/caller/callee) ; Dial via jingle using labels
+;exten => 6391,1,Dial(JINGLE/asterisk@digium.com/mogorman@astjab.org) ;Dial via jingle using asterisk as the transport and calling mogorman.
;exten => 6394,1,Dial(Local/6275/n) ; this will dial ${MARK}
;exten => 6275,1,Macro(stdexten,6275,${MARK}) ; assuming ${MARK} is something like Zap/2
diff --git a/configs/jabber.conf.sample b/configs/jabber.conf.sample
new file mode 100644
index 000000000..521513c92
--- /dev/null
+++ b/configs/jabber.conf.sample
@@ -0,0 +1,15 @@
+[general]
+;debug=yes ;;Turn on debugging by default.
+;autoprune=yes ;;Auto remove users from buddy list.
+;autoregister=yes ;;Auto register users from buddy list.
+
+;[asterisk] ;;label
+;type=client ;;Client or Component connection
+;serverhost=astjab.org ;;Route to server for example,
+ ;; talk.google.com
+;username=asterisk@astjab.org/asterisk ;;username with optional roster.
+;secret=blah ;;Password
+;port=5222 ;;Port to use defaults to 5222
+;usetls=yes ;;use tls or not
+;usesasl=yes ;;use sasl or not
+;buddy=mogorman@astjab.org ;;manual addition of buddy to list.
diff --git a/configs/jingle.conf.sample b/configs/jingle.conf.sample
new file mode 100644
index 000000000..da629b626
--- /dev/null
+++ b/configs/jingle.conf.sample
@@ -0,0 +1,19 @@
+;[general]
+;context=default ;;Context to dump call into
+;allowguest=yes ;;Allow calls from people not in
+ ;;list of peers
+;
+;[guest] ;;special account for options on guest account
+;disallow=all
+;allow=ulaw
+;context=guest
+;
+;[ogorman]
+;username=ogorman@gmail.com ;;username of the peer your
+ ;;calling or accepting calls from
+;disallow=all
+;allow=ulaw
+;context=default
+;connection=asterisk ;;client or component in jabber.conf
+ ;;for the call to leave on.
+;
diff --git a/configure.ac b/configure.ac
index 1482aaa76..751526cc1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -190,6 +190,7 @@ AC_SUBST(AST_DEVMODE)
AST_EXT_LIB([asound], [snd_spcm_init], [alsa/asoundlib.h], [ALSA], [Advanced Linux Sound Architecture], [-lm -ldl])
AST_EXT_LIB([curses], [initscr], [curses.h], [CURSES], [curses], [])
+AST_EXT_LIB([iksemel], [iks_start_sasl], [iksemel.h], [IKSEMEL], [Iksemel Jabber Library])
AST_EXT_LIB([nbs], [nbs_connect], [nbs.h], [NBS], [Network Broadcast Sound])
AST_EXT_LIB([ncurses], [initscr], [curses.h], [NCURSES], [ncurses], [])
AST_EXT_LIB([newt], [newtBell], [newt.h], [NEWT], [newt])
diff --git a/doc/jabber.txt b/doc/jabber.txt
new file mode 100644
index 000000000..5889a6e36
--- /dev/null
+++ b/doc/jabber.txt
@@ -0,0 +1,13 @@
+(res_jabber is very experimental!)
+Jabber(xmpp) is an xml based protocol primarily for presence and messaging.
+It is an open standard and there are several open server implementations,
+ejabberd, jabberd(2), wildfire, and many others, as well as several open source
+clients, Psi, gajim, gaim etc. Jabber differs from otherIM applications as it
+is immensly extendable. This allows us to easily integrate asterisk with
+jabber. The Asterisk Jabber Interface is provided by res_jabber. res_jabber
+allows for asterisk to connect to any jabber server via the standard client
+protocol or also as a simple client. Several simple functions are exposed to
+the dial plan, jabberstatus, jabbersend, and soon jabberrecv. res_jabber is also used
+to provide the connection interface for chan_jingle.
+The maintainer of res_jabber is Matthew O'Gorman <mogorman@digum.com> or
+mog_work on irc or prefered mogorman@astjab.org over jabber.
diff --git a/doc/jingle.txt b/doc/jingle.txt
new file mode 100644
index 000000000..b6bdd111c
--- /dev/null
+++ b/doc/jingle.txt
@@ -0,0 +1,8 @@
+(Jingle support in asterisk is experimental)
+Jingle is an xmpp based protocol for signalling the transfer of media.
+Currently asterisk supports the propitery GoogleTalk protocol that is
+very similar to jingle, and hopes to support true jingle specs
+(JEP-166,167,176,177,180,181 etc) as more clients support the true standard.
+Jingle's configuration is very similar to sip.conf only as we are not the
+jabber server in this case you must provide a connection for the peer to
+travel out on.
diff --git a/include/asterisk/jabber.h b/include/asterisk/jabber.h
new file mode 100644
index 000000000..5f7821e90
--- /dev/null
+++ b/include/asterisk/jabber.h
@@ -0,0 +1,133 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Matt O'Gorman <mogorman@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef _ASTERISK_JABBER_H
+#define _ASTERISK_JABBER_H
+
+#include <iksemel.h>
+#include "asterisk/astobj.h"
+
+enum aji_state {
+ AJI_DISCONNECTED=0,
+ AJI_CONNECTING,
+ AJI_ALMOST,
+ AJI_CONNECTED
+};
+
+enum {
+ AJI_AUTOPRUNE = (1 << 0),
+ AJI_AUTOREGISTER = (1 << 1)
+};
+
+enum aji_btype {
+ AJI_USER=0,
+ AJI_TRANS=1,
+ AJI_UTRANS=2
+};
+
+enum aji_type {
+ AJI_COMPONENT,
+ AJI_CLIENT,
+};
+
+struct aji_version {
+ char version[50];
+ int jingle;
+ struct aji_capabilities *parent;
+ struct aji_version *next;
+};
+
+struct aji_capabilities {
+ char node[200];
+ struct aji_version *versions;
+ struct aji_capabilities *next;
+};
+
+struct aji_resource {
+ int status;
+ char resource[80];
+ char description[1000];
+ struct aji_version *cap;
+ int priority;
+ struct aji_resource *next;
+};
+
+struct aji_buddy {
+ ASTOBJ_COMPONENTS(struct aji_buddy);
+ char user[160];
+ char host[160];
+ char pass[160]; /*For Transports*/
+ char server[160]; /*For Transports */
+ char channel[160];
+ struct aji_resource *resources;
+ enum aji_btype btype;
+ unsigned int flags;
+};
+
+struct aji_buddy_container {
+ ASTOBJ_CONTAINER_COMPONENTS(struct aji_buddy);
+};
+
+struct aji_transport_container {
+ ASTOBJ_CONTAINER_COMPONENTS(struct aji_transport);
+};
+
+struct aji_client {
+ ASTOBJ_COMPONENTS(struct aji_client);
+ char password[160];
+ char user[160];
+ char serverhost[160];
+ char context[100];
+ char sid[10]; /* Session ID */
+ char mid[6]; /* Message ID */
+ iksid *jid;
+ iksparser *p;
+ iksfilter *f;
+ ikstack *stack;
+ enum aji_state state;
+ int port;
+ int debug;
+ int usetls;
+ int forcessl;
+ int usesasl;
+ int keepalive;
+ int allowguest;
+ int timeout;
+ int authorized;
+ unsigned int flags;
+ enum aji_type component;
+ struct aji_buddy_container buddies;
+ void *jingle;
+ pthread_t thread;
+};
+
+struct aji_client_container{
+ ASTOBJ_CONTAINER_COMPONENTS(struct aji_client);
+};
+
+int ast_aji_send(struct aji_client *client, char *address, char *message);
+int ast_aji_disconnect(struct aji_client *client);
+int ast_aji_check_roster(void);
+void ast_aji_increment_mid(char *mid);
+int ast_aji_create_chat(struct aji_client *client,char *room, char *server, char *topic);
+int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message);
+int ast_aji_join_chat(struct aji_client *client,char *room);
+struct aji_client *ast_aji_get_client(char *name);
+struct aji_client_container *ast_aji_get_clients(void);
+
+#endif
diff --git a/include/asterisk/jingle.h b/include/asterisk/jingle.h
new file mode 100644
index 000000000..0968b7394
--- /dev/null
+++ b/include/asterisk/jingle.h
@@ -0,0 +1,45 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Matt O'Gorman <mogorman@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef _ASTERISK_JINGLE_H
+#define _ASTERISK_JINGLE_H
+
+#include <iksemel.h>
+#include "asterisk/astobj.h"
+
+
+/* Jingle Constants */
+
+#define JINGLE_NODE "jingle"
+#define GOOGLE_NODE "session"
+
+#define JINGLE_NS "http://jabber.org/protocol/jingle"
+#define GOOGLE_NS "http://www.google.com/session"
+
+#define JINGLE_SID "sid"
+#define GOOGLE_SID "id"
+
+#define JINGLE_INITIATE "initiate"
+
+#define JINGLE_ACCEPT "accept"
+#define GOOGLE_ACCEPT "accept"
+
+#define JINGLE_NEGOTIATE "negotiate"
+#define GOOGLE_NEGOTIATE "candidates"
+
+#endif
diff --git a/include/autoconfig.h.in b/include/autoconfig.h.in
index b8847cc74..a7048c39c 100644
--- a/include/autoconfig.h.in
+++ b/include/autoconfig.h.in
@@ -112,6 +112,9 @@
/* Define if your system has the GTK libraries. */
#undef HAVE_GTK
+/* Define to indicate the Iksemel Jabber Library library */
+#undef HAVE_IKSEMEL
+
/* Define to 1 if you have the `inet_ntoa' function. */
#undef HAVE_INET_NTOA
diff --git a/makeopts.in b/makeopts.in
index 19518693e..19a64ba08 100644
--- a/makeopts.in
+++ b/makeopts.in
@@ -88,6 +88,9 @@ NEWT_INCLUDE=@NEWT_INCLUDE@
PWLIB_LIB=@PWLIB_LIB@
PWLIB_INCLUDE=@PWLIB_INCLUDE@
+IKSEMEL_LIB=@IKSEMEL_LIB@
+IKSEMEL_INCLUDE=@IKSEMEL_INCLUDE@
+
SSL_LIB=@OPENSSL_LIB@
SSL_INCLUDE=@OPENSSL_INCLUDE@
diff --git a/res/Makefile b/res/Makefile
index ea90998b2..3081a8bef 100644
--- a/res/Makefile
+++ b/res/Makefile
@@ -73,6 +73,12 @@ res_odbc.so: res_odbc.o
res_odbc.o: res_odbc.c
$(CC) -c -o $@ $(CFLAGS) $(ODBC_INCLUDE) $<
+res_jabber.o: res_jabber.c
+ $(CC) -c -o $@ $(CFLAGS) $(IKSEMEL_INCLUDE) $<
+
+res_jabber.so: res_jabber.o
+ $(CC) $(SOLINK) -o $@ $< $(IKSEMEL_LIB)
+
res_osp.so: res_osp.o
$(CC) $(SOLINK) -o $@ $< $(OSPTK_LIB)
diff --git a/res/res_jabber.c b/res/res_jabber.c
new file mode 100644
index 000000000..64eaf66bd
--- /dev/null
+++ b/res/res_jabber.c
@@ -0,0 +1,2238 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2006, Digium, Inc.
+ *
+ * Matt O'Gorman <mogorman@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief A resource for interfacing asterisk directly as a client
+ * or a component to a jabber compliant server.
+ */
+
+/*** MODULEINFO
+ <depend>iksemel</depend>
+ ***/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <iksemel.h>
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk/channel.h"
+#include "asterisk/jabber.h"
+#include "asterisk/file.h"
+#include "asterisk/config.h"
+#include "asterisk/callerid.h"
+#include "asterisk/lock.h"
+#include "asterisk/logger.h"
+#include "asterisk/options.h"
+#include "asterisk/cli.h"
+#include "asterisk/app.h"
+#include "asterisk/pbx.h"
+#include "asterisk/md5.h"
+#include "asterisk/acl.h"
+#include "asterisk/utils.h"
+#include "asterisk/module.h"
+#include "asterisk/astobj.h"
+#include "asterisk/astdb.h"
+
+#define JABBER_CONFIG "jabber.conf"
+
+static int aji_highest_bit(int number);
+static void aji_buddy_destroy(struct aji_buddy *obj);
+static void aji_client_destroy(struct aji_client *obj);
+static int aji_send_exec(struct ast_channel *chan, void *data);
+static int aji_status_exec(struct ast_channel *chan, void *data);
+static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming);
+static int aji_act_hook(void *data, int type, iks *node);
+static void aji_handle_iq(struct aji_client *client, iks *node);
+static void aji_handle_presence(struct aji_client *client, ikspak *pak);
+static void aji_handle_subscribe(struct aji_client *client, ikspak *pak);
+static void *aji_recv_loop(void *data);
+static int aji_component_initialize(struct aji_client *client);
+static int aji_client_initialize(struct aji_client *client);
+static int aji_client_connect(void *data, ikspak *pak);
+static void aji_set_presence(struct aji_client *client, char *user, int level, char *desc);
+static int aji_do_debug(int fd, int argc, char *argv[]);
+static int aji_do_reload(int fd, int argc, char *argv[]);
+static int aji_no_debug(int fd, int argc, char *argv[]);
+static int aji_test(int fd, int argc, char *argv[]);
+static int aji_show_clients(int fd, int argc, char *argv[]);
+static int aji_create_client(char *label, struct ast_variable *var, int debug);
+static int aji_create_buddy(char *label, struct aji_client *client);
+static int aji_create_transport(char *label, struct aji_client *client);
+static void aji_reload(void);
+static int aji_load_config(void);
+static void aji_pruneregister(struct aji_client *client);
+static int aji_register_transport(void *data, ikspak *pak);
+static int aji_register_transport2(void *data, ikspak *pak);
+static int aji_filter_roster(void *data, ikspak *pak);
+static int aji_get_roster(struct aji_client *client);
+static int aji_client_info_handler(void *data, ikspak *pak);
+static int aji_dinfo_handler(void *data, ikspak *pak);
+static int aji_ditems_handler(void *data, ikspak *pak);
+static int aji_register_query_handler(void *data, ikspak *pak);
+static int aji_register_approve_handler(void *data, ikspak *pak);
+static int aji_reconnect(struct aji_client *client);
+static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
+
+static char debug_usage[] =
+"Usage: JABBER debug\n"
+" Enables dumping of JABBER packets for debugging purposes.\n";
+
+static char no_debug_usage[] =
+"Usage: JABBER no debug\n"
+" Disables dumping of JABBER packets for debugging purposes.\n";
+
+static char reload_usage[] =
+"Usage: JABBER reload\n"
+" Enables reloading of JABBER module.\n";
+
+static char test_usage[] =
+"Usage: JABBER test [client]\n"
+" Sends test massage for debugging purposes. A specific client\n"
+" as configured in jabber.conf can be optionally specified.\n";
+
+static struct ast_cli_entry aji_cli[] = {
+ {{ "jabber", "debug", NULL}, aji_do_debug, "Enable JABBER debugging", debug_usage },
+ {{ "jabber", "reload", NULL}, aji_do_reload, "Enable JABBER debugging", reload_usage },
+ {{ "jabber", "show", "connected", NULL}, aji_show_clients, "Show state of clients and components", debug_usage },
+ {{ "jabber", "no", "debug", NULL}, aji_no_debug, "Disable JABBER debug", no_debug_usage },
+ {{ "jabber", "test", NULL}, aji_test, "Shows roster, but is genearlly used for mog's debugging.", test_usage },
+ };
+
+static const char *tdesc = "AJI - Asterisk JABBER Interface";
+
+static char *app_ajisend = "JABBERSend";
+
+static char *ajisend_synopsis = "JABBERSend(JABBER,ScreenName,Message)";
+
+static char *ajisend_descrip =
+"JABBERSend(JABBER,ScreenName,Message)\n"
+" JABBER - Client or transport Asterisk uses to connect to JABBER\n"
+" ScreenName - User Name to message.\n"
+" Message - Message to be sent to the buddy\n";
+
+static char *app_ajistatus = "JABBERStatus";
+
+static char *ajistatus_synopsis = "JABBERStatus(JABBER,ScreenName,Variable)";
+
+static char *ajistatus_descrip =
+"JABBERStatus(JABBER,ScreenName,Variable)\n"
+" JABBER - Client or transport Asterisk uses to connect to JABBER\n"
+" ScreenName - User Name to retrieve status from.\n"
+" Variable - Variable to store presence in will be 1-6.\n"
+" In order, Online, Chatty, Away, XAway, DND, Offline\n"
+" If not in roster variable will = 7\n";
+
+struct aji_client_container clients;
+
+struct aji_capabilities *capabilities;
+
+/*! Global flags, initialized to default values */
+static struct ast_flags globalflags = { AJI_AUTOPRUNE | AJI_AUTOREGISTER };
+
+/*!
+ * \brief Deletes the aji_client data structure.
+ * \param obj is the structure we will delete.
+ * \return void.
+ */
+static void aji_client_destroy(struct aji_client *obj)
+{
+ ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, aji_buddy_destroy);
+ ASTOBJ_CONTAINER_DESTROY(&obj->buddies);
+ free(obj);
+}
+
+/*!
+ * \brief Deletes the aji_buddy data structure.
+ * \param obj is the structure we will delete.
+ * \return void.
+ */
+static void aji_buddy_destroy(struct aji_buddy *obj)
+{
+ struct aji_resource *tmp;
+
+ while ((tmp = obj->resources)) {
+ obj->resources = obj->resources->next;
+ free(tmp);
+ }
+
+ free(obj);
+}
+
+static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
+{
+ struct aji_capabilities *list = NULL;
+ struct aji_version *res = NULL;
+
+ list = capabilities;
+
+ if(!node)
+ node = pak->from->full;
+ if(!version)
+ version = "none supplied.";
+ while(list) {
+ if(!strcasecmp(list->node, node)) {
+ res = list->versions;
+ while(res) {
+ if(!strcasecmp(res->version, version))
+ return res;
+ res = res->next;
+ }
+ if(!res) {
+ res = (struct aji_version *)malloc(sizeof(struct aji_version));
+ if(!res) {
+ ast_log(LOG_ERROR, "Out of memory!\n");
+ return NULL;
+ }
+ res->jingle = 0;
+ res->parent = list;
+ ast_copy_string(res->version, version, sizeof(res->version));
+ res->next = list->versions;
+ list->versions = res;
+ return res;
+ }
+ }
+ list = list->next;
+ }
+ if(!list) {
+ list = (struct aji_capabilities *)malloc(sizeof(struct aji_capabilities));
+ if(!list) {
+ ast_log(LOG_ERROR, "Out of memory!\n");
+ return NULL;
+ }
+ res = (struct aji_version *)malloc(sizeof(struct aji_version));
+ if(!res) {
+ ast_log(LOG_ERROR, "Out of memory!\n");
+ return NULL;
+ }
+ ast_copy_string(list->node, node, sizeof(list->node));
+ ast_copy_string(res->version, version, sizeof(res->version));
+ res->jingle = 0;
+ res->parent = list;
+ res->next = list->versions;
+ list->versions = res;
+ list->next = capabilities;
+ capabilities = list;
+ }
+ return res;
+}
+
+static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *rname)
+{
+ struct aji_resource *res = NULL;
+ if (!buddy)
+ return res;
+ res = buddy->resources;
+ while (res) {
+ if (!strcasecmp(res->resource, rname)) {
+ break;
+ }
+ res = res->next;
+ }
+ return res;
+}
+
+static int gtalk_yuck(iks *node)
+{
+ if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps"))
+ return 1;
+ return 0;
+}
+
+/*!
+ * \brief Detects the highest bit in a number.
+ * \param Number you want to have evaluated.
+ * \return the highest power of 2 that can go into the number.
+ */
+static int aji_highest_bit(int number)
+{
+ int x = sizeof(number) * 8 - 1;
+ if (!number)
+ return 0;
+ for (; x > 0; x--) {
+ if (number & (1 << x))
+ break;
+ }
+ return (1 << x);
+}
+
+static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid)
+{
+ iks *x, *y;
+ x = iks_new("iq");
+ iks_insert_attrib(x, "type", "set");
+ y = iks_insert(x, "query");
+ iks_insert_attrib(y, "xmlns", IKS_NS_AUTH);
+ iks_insert_cdata(iks_insert(y, "username"), id->user, 0);
+ iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0);
+ if (sid) {
+ char buf[41];
+ char sidpass[100];
+ snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass);
+ ast_sha1_hash(buf, sidpass);
+ ast_verbose("\n");
+ iks_insert_cdata(iks_insert(y, "digest"), buf, 0);
+ } else {
+ iks_insert_cdata(iks_insert(y, "password"), pass, 0);
+ }
+ return x;
+}
+
+/*!
+ * \brief Dial plan function status(). puts the status of watched user
+ into a channel variable.
+ * \param channel, and username,watched user, status var
+ * \return 0.
+ */
+static int aji_status_exec(struct ast_channel *chan, void *data)
+{
+ struct aji_client *client = NULL;
+ struct aji_resource *r = NULL;
+ char *s = NULL, *sender = NULL, *screenname = NULL, *resource = NULL, *variable = NULL;
+ int stat = 7, found = 0;
+ char status[2];
+ if (data) {
+ s = ast_strdupa((char *) data);
+ if (s) {
+ sender = strsep(&s, "|");
+ if (sender && (sender[0] != '\0')) {
+ screenname = strsep(&s, "|");
+ if (screenname && (screenname[0] != '\0')) {
+ variable = s;
+ } else {
+ ast_log(LOG_ERROR, "Bad arguments\n");
+ return -1;
+ }
+ }
+ }
+ } else {
+ ast_log(LOG_ERROR, "Out of memory\n");
+ return -1;
+ }
+
+ resource = strsep(&screenname, "/");
+
+ client = ast_aji_get_client(sender);
+ if (!client) {
+ ast_log(LOG_WARNING, "Could not find Connection.\n");
+ return -1;
+ }
+
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ if (!strcasecmp(iterator->user, screenname)) {
+ found = 1;
+ r = iterator->resources;
+ if (!r) { /* client hasnt signed on */
+ break;
+ } else {
+ if (resource) {
+ while (r) {
+ if (!strcasecmp(r->resource, resource)) {
+ stat = r->status;
+ break;
+ }
+ r = r->next;
+ }
+ ast_log(LOG_WARNING, "Resource not found %s\n", resource);
+ } else {
+ stat = r->status;
+ break;
+ }
+ }
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+
+ if (!found) { /* just a label */
+ ast_log(LOG_WARNING, "Could not find Buddy in list.\n");
+ return -1;
+ }
+ sprintf(status, "%d", stat);
+ pbx_builtin_setvar_helper(chan, variable, status);
+ return 0;
+}
+
+/*!
+ * \brief Dial plan function to send a message.
+ * \param channel, and data, data is sender, reciever, message.
+ * \return 0.
+ */
+static int aji_send_exec(struct ast_channel *chan, void *data)
+{
+ struct aji_client *client = NULL;
+
+ char *s = NULL, *sender = NULL, *recipiant = NULL, *message = NULL;
+ if (data) {
+ s = ast_strdupa((char *) data);
+ if (s) {
+ sender = strsep(&s, "|");
+ if (sender && (sender[0] != '\0')) {
+ recipiant = strsep(&s, "|");
+ if (recipiant && (recipiant[0] != '\0')) {
+ message = s;
+ } else {
+ ast_log(LOG_ERROR, "Bad arguments \n");
+ return -1;
+ }
+ }
+ }
+ client = ast_aji_get_client(sender);
+ if (!client) {
+ ast_log(LOG_WARNING, "Could not find Sender.\n");
+ return -1;
+ }
+ } else {
+ ast_log(LOG_ERROR, "Out of memory\n");
+ return -1;
+ }
+ if (strchr(recipiant, '@') && message)
+ ast_aji_send(client, recipiant, message);
+ return 0;
+}
+
+/*!
+ * \brief the debug loop.
+ * \param aji_client structure, xml data as string, size of string, direction of packet, 1 for inbound 0 for outbound.
+ */
+static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ if (client->debug == 1) {
+ if (is_incoming)
+ ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
+ else
+ ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp);
+
+ }
+ ASTOBJ_UNREF(client, aji_client_destroy);
+}
+
+/*!
+ * \brief The action hook parses the inbound packets, constantly running.
+ * \param aji client structure, type of packet, the actual packet.
+ * \return IKS_OK or IKS_HOOK .
+ */
+static int aji_act_hook(void *data, int type, iks *node)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ ikspak *pak = NULL;
+ iks *auth = NULL;
+
+ pak = iks_packet(node);
+
+
+ if (client->component == AJI_CLIENT) {
+ switch (type) {
+ case IKS_NODE_START:
+ if (client->usetls && !iks_is_secure(client->p)) {
+ if (iks_has_tls())
+ iks_start_tls(client->p);
+ else
+ ast_log(LOG_ERROR, "gnuTLS not installed.\n");
+ break;
+ }
+ if (!client->usesasl) {
+ 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);
+ auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id"));
+ if (auth) {
+ iks_insert_attrib(auth, "id", client->mid);
+ iks_insert_attrib(auth, "to", client->jid->server);
+ ast_aji_increment_mid(client->mid);
+ iks_send(client->p, auth);
+ iks_delete(auth);
+ } else
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ break;
+
+ case IKS_NODE_NORMAL:
+ {
+ int features = 0;
+ if (!strcmp("stream:features", iks_name(node))) {
+ features = iks_stream_features(node);
+ if (client->usesasl) {
+ if (client->usetls && !iks_is_secure(client->p))
+ break;
+ if (client->authorized) {
+ if (features & IKS_STREAM_BIND) {
+ 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);
+ auth = iks_make_resource_bind(client->jid);
+ if (auth) {
+ iks_insert_attrib(auth, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_send(client->p, auth);
+ iks_delete(auth);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ break;
+ }
+ }
+ if (features & IKS_STREAM_SESSION) {
+ 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);
+ auth = iks_make_session();
+ if (auth) {
+ iks_insert_attrib(auth, "id", "auth");
+ ast_aji_increment_mid(client->mid);
+ iks_send(client->p, auth);
+ iks_delete(auth);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ }
+ } else {
+ features = aji_highest_bit(features);
+ if (features == IKS_STREAM_SASL_MD5)
+ iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, client->jid->user, client->password);
+ else {
+ if (features == IKS_STREAM_SASL_PLAIN) {
+ iks *x = NULL;
+ x = iks_new("auth");
+ if (x) {
+ iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL);
+ int len = strlen(client->jid->user) + strlen(client->password) + 3;
+ /* XXX Check return values XXX */
+ char *s = ast_malloc(80 + len);
+ char *base64 = ast_malloc(80 + len * 2);
+ iks_insert_attrib(x, "mechanism", "PLAIN");
+ sprintf(s, "%c%s%c%s", 0, client->jid->user, 0, client->password);
+ ast_base64encode(base64, s, len, len * 2);
+ iks_insert_cdata(x, base64, 0);
+ iks_send(client->p, x);
+ iks_delete(x);
+ if (base64)
+ free(base64);
+ if (s)
+ free(s);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ }
+ }
+ }
+ }
+ } else if (!strcmp("failure", iks_name(node))) {
+ ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n");
+ } else if (!strcmp("success", iks_name(node))) {
+ client->authorized = 1;
+ iks_send_header(client->p, client->jid->server);
+ }
+ break;
+ }
+ case IKS_NODE_ERROR:{
+ ast_log(LOG_ERROR, "JABBER: Node Error\n");
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_HOOK;
+ break;
+ }
+ case IKS_NODE_STOP:{
+ ast_log(LOG_WARNING, "JABBER: Disconnected\n");
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_HOOK;
+ break;
+ }
+ }
+ } else if (client->state != AJI_CONNECTED && client->component == AJI_COMPONENT) {
+ switch (type) {
+ case IKS_NODE_START:{
+ char secret[160], shasum[320], *handshake;
+ switch (client->state) {
+ case AJI_DISCONNECTED:
+ sprintf(secret, "%s%s", pak->id, client->password);
+ ast_sha1_hash(shasum, secret);
+ handshake = NULL;
+ asprintf(&handshake, "<handshake>%s</handshake>", shasum);
+ if (handshake) {
+ iks_send_raw(client->p, handshake);
+ free(handshake);
+ handshake = NULL;
+ }
+ client->state = AJI_CONNECTED;
+ break;
+ case AJI_ALMOST:
+ client->state = AJI_CONNECTED;
+ break;
+ case AJI_CONNECTING:
+ case AJI_CONNECTED:
+ break;
+ };
+ break;
+ }
+
+ case IKS_NODE_NORMAL:{
+ break;
+ }
+
+ case IKS_NODE_ERROR:{
+ ast_log(LOG_ERROR, "JABBER: Node Error\n");
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_HOOK;
+ break;
+ }
+ case IKS_NODE_STOP:{
+ ast_log(LOG_WARNING, "JABBER: Disconnected\n");
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_HOOK;
+ break;
+ }
+ }
+ }
+
+ switch (pak->type) {
+ case IKS_PAK_NONE:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Don't know what to do with you NONE\n");
+ break;
+ case IKS_PAK_MESSAGE:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Don't know what to do with you MESSAGE\n");
+ break;
+ case IKS_PAK_PRESENCE:
+ aji_handle_presence(client, pak);
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Do know how to handle presence!!\n");
+ break;
+ case IKS_PAK_S10N:
+ aji_handle_subscribe(client, pak);
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont know S10N subscribe!!\n");
+ break;
+ case IKS_PAK_IQ:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont have an IQ!!!\n");
+ aji_handle_iq(client, node);
+ break;
+ default:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I Dont know %i\n", pak->type);
+ break;
+ }
+
+ iks_filter_packet(client->f, pak);
+
+ if (node)
+ iks_delete(node);
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_OK;
+}
+
+static int aji_register_approve_handler(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ iks *iq = NULL, *query = NULL, *item = NULL;
+
+ iq = iks_new("iq");
+ query = iks_new("query");
+ item = iks_new("item");
+
+ if (client && iq && query && item) {
+ if (!iks_find(pak->query, "remove")) {
+ iks_insert_attrib(iq, "from", client->jid->full);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "jabber:iq:register");
+
+ iks_insert_node(iq, query);
+ iks_send(client->p, iq);
+ iks_insert_attrib(iq, "from", pak->from->full);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_insert_attrib(iq, "type", "set");
+ iks_insert_attrib(query, "xmlns", "jabber:iq:roster");
+ iks_insert_attrib(item, "subscription", "none");
+ iks_insert_attrib(item, "jid", client->jid->full);
+ iks_insert_node(query, item);
+ iks_send(client->p, iq);
+ }
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (item)
+ iks_delete(item);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+}
+
+static int aji_register_query_handler(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ char *node = NULL;
+
+ client = (struct aji_client *) data;
+
+ if (!(node = iks_find_attrib(pak->query, "node"))) {
+ iks *iq = NULL, *query = NULL, *instructions = NULL;
+ char *explain = "Welcome to Asterisk the Open Source PBX.\n";
+ iq = iks_new("iq");
+ query = iks_new("query");
+ instructions = iks_new("instructions");
+ if (iq && query && instructions && client) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "jabber:iq:register");
+ iks_insert_cdata(instructions, explain, 0);
+ iks_insert_node(iq, query);
+ iks_insert_node(query, instructions);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (instructions)
+ iks_delete(instructions);
+ }
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+}
+
+static int aji_ditems_handler(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ char *node = NULL;
+
+ if (!(node = iks_find_attrib(pak->query, "node"))) {
+ iks *iq = NULL, *query = NULL, *item = NULL;
+ iq = iks_new("iq");
+ query = iks_new("query");
+ item = iks_new("item");
+
+ if (iq && query && item) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
+ iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
+ iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
+ iks_insert_attrib(item, "jid", client->user);
+
+ iks_insert_node(iq, query);
+ iks_insert_node(query, item);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (item)
+ iks_delete(item);
+
+ } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
+ iks *iq, *query, *confirm;
+ iq = iks_new("iq");
+ query = iks_new("query");
+ confirm = iks_new("item");
+ if (iq && query && confirm && client) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
+ iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
+ iks_insert_attrib(confirm, "node", "confirmaccount");
+ iks_insert_attrib(confirm, "name", "Confirm AIM account");
+ iks_insert_attrib(confirm, "jid", "blog.astjab.org");
+
+ iks_insert_node(iq, query);
+ iks_insert_node(query, confirm);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (confirm)
+ iks_delete(confirm);
+
+ } else if (!strcasecmp(node, "confirmaccount")) {
+ iks *iq = NULL, *query = NULL, *feature = NULL;
+
+ iq = iks_new("iq");
+ query = iks_new("query");
+ feature = iks_new("feature");
+
+ if (iq && query && feature && client) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
+ iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
+ iks_insert_node(iq, query);
+ iks_insert_node(query, feature);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (feature)
+ iks_delete(feature);
+ }
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+
+}
+
+static int aji_client_info_handler(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ struct aji_buddy *buddy = NULL;
+ struct aji_resource *resource = NULL;
+ buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
+
+ resource = aji_find_resource(buddy, pak->from->resource);
+
+ if (pak->subtype == IKS_TYPE_RESULT) {
+ if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
+ ast_verbose("NO I AM JINGLECAPABLE!!!\n");
+ resource->cap->jingle = 1;
+ } else
+ resource->cap->jingle = 0;
+ } else if (pak->subtype == IKS_TYPE_GET) {
+ iks *iq, *disco, *ident, *google, *query;
+ iq = iks_new("iq");
+ query = iks_new("query");
+ ident = iks_new("identity");
+ disco = iks_new("feature");
+ google = iks_new("feature");
+ if (iq && ident && disco && google) {
+ iks_insert_attrib(iq, "from", client->jid->full);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(query, "xmlns", "xmlns='http://jabber.org/protocol/disco#info");
+ iks_insert_attrib(ident, "category", "client");
+ iks_insert_attrib(ident, "type", "pc");
+ iks_insert_attrib(ident, "name", "asterisk");
+ iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
+ iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
+ iks_insert_node(iq, query);
+ iks_insert_node(query, ident);
+ iks_insert_node(query, disco);
+ iks_insert_node(query, google);
+ iks_send(client->p, iq);
+ } else
+ ast_log(LOG_ERROR, "Out of Memory.\n");
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (ident)
+ iks_delete(ident);
+ if (google)
+ iks_delete(google);
+ if (disco)
+ iks_delete(disco);
+ } else if (pak->subtype == IKS_TYPE_ERROR) {
+ ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
+ }
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+}
+
+static int aji_dinfo_handler(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ char *node = NULL;
+
+ if (!(node = iks_find_attrib(pak->query, "node"))) {
+ iks *iq = NULL, *query = NULL, *identity = NULL, *disco = NULL, *reg = NULL, *commands = NULL, *gateway = NULL, *version = NULL, *vcard = NULL, *search = NULL;
+
+ iq = iks_new("iq");
+ query = iks_new("query");
+ identity = iks_new("identity");
+ disco = iks_new("feature");
+ reg = iks_new("feature");
+ commands = iks_new("feature");
+ gateway = iks_new("feature");
+ version = iks_new("feature");
+ vcard = iks_new("feature");
+ search = iks_new("feature");
+
+ if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
+ iks_insert_attrib(identity, "category", "gateway");
+ iks_insert_attrib(identity, "type", "pstn");
+ iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
+ iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
+ iks_insert_attrib(reg, "var", "jabber:iq:register");
+ iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
+ iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
+ iks_insert_attrib(version, "var", "jabber:iq:version");
+ iks_insert_attrib(vcard, "var", "vcard-temp");
+ iks_insert_attrib(search, "var", "jabber:iq:search");
+
+ iks_insert_node(iq, query);
+ iks_insert_node(query, identity);
+ iks_insert_node(query, disco);
+ iks_insert_node(query, reg);
+ iks_insert_node(query, commands);
+ iks_insert_node(query, gateway);
+ iks_insert_node(query, version);
+ iks_insert_node(query, vcard);
+ iks_insert_node(query, search);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (identity)
+ iks_delete(identity);
+ if (disco)
+ iks_delete(disco);
+ if (reg)
+ iks_delete(reg);
+ if (commands)
+ iks_delete(commands);
+ if (gateway)
+ iks_delete(gateway);
+ if (version)
+ iks_delete(version);
+ if (vcard)
+ iks_delete(vcard);
+ if (search)
+ iks_delete(search);
+
+ } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
+ iks *iq = NULL, *query = NULL, *confirm = NULL;
+ iq = iks_new("iq");
+ query = iks_new("query");
+ confirm = iks_new("item");
+
+ if (iq && query && confirm && client) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
+ iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
+ iks_insert_attrib(confirm, "node", "confirmaccount");
+ iks_insert_attrib(confirm, "name", "Confirm AIM account");
+ iks_insert_attrib(confirm, "jid", client->user);
+ iks_insert_node(iq, query);
+ iks_insert_node(query, confirm);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (confirm)
+ iks_delete(confirm);
+
+ } else if (!strcasecmp(node, "confirmaccount")) {
+ iks *iq = NULL, *query = NULL, *feature = NULL;
+
+ iq = iks_new("iq");
+ query = iks_new("query");
+ feature = iks_new("feature");
+ if (iq && query && feature && client) {
+ iks_insert_attrib(iq, "from", client->user);
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", pak->id);
+ iks_insert_attrib(iq, "type", "result");
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
+ iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
+ iks_insert_node(iq, query);
+ iks_insert_node(query, feature);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (iq)
+ iks_delete(iq);
+ if (query)
+ iks_delete(query);
+ if (feature)
+ iks_delete(feature);
+ }
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+}
+
+/*!
+ * \brief Handles <iq> tags.
+ * \param client structure and the iq node.
+ * \return void.
+ */
+static void aji_handle_iq(struct aji_client *client, iks *node)
+{
+ if (option_verbose > 77)
+ ast_verbose("MWAHHAHAHA NOTHING TO SEE HERE!\n");
+}
+
+/*!
+ * \brief Handles presence packets.
+ * \param client structure and the node.
+ * \return void.
+ */
+static void aji_handle_presence(struct aji_client *client, ikspak *pak)
+{
+ int status, priority;
+ struct aji_buddy *buddy = NULL;
+ struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
+ char *ver, *node;
+
+ buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
+ if (!buddy) {
+ ast_log(LOG_WARNING, "Got presence packet from %s, somone not in our roster!!!!\n", pak->from->partial);
+ return;
+ }
+ status = (pak->show) ? pak->show : 6;
+ priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
+ tmp = buddy->resources;
+
+ while (tmp) {
+ if (!strcasecmp(tmp->resource, pak->from->resource)) {
+ tmp->status = status;
+ found = tmp;
+ if (status == 6) { /* Sign off Destroy resource */
+ ast_verbose("KILL THE SIGN OFF!\n");
+ if (last && found->next) {
+ last->next = found->next;
+ } else if (!last) {
+ if (found->next)
+ buddy->resources = found->next;
+ else
+ buddy->resources = NULL;
+ } else if (!found->next) {
+ if (last)
+ last->next = NULL;
+ else
+ buddy->resources = NULL;
+ }
+ free(found);
+ found = NULL;
+ break;
+ }
+ if (tmp->priority != priority) {
+ found->priority = priority;
+ if (!last && !found->next)
+ break;
+ if (last)
+ last->next = found->next;
+ else
+ buddy->resources = found->next;
+ last = NULL;
+ tmp = buddy->resources;
+ if (!buddy->resources)
+ buddy->resources = found;
+ while (tmp) {
+ if (found->priority > tmp->priority) {
+ if (last)
+ last->next = found;
+ found->next = tmp;
+ if (!last)
+ buddy->resources = found;
+ break;
+ }
+ if (!tmp->next) {
+ tmp->next = found;
+ break;
+ }
+ last = tmp;
+ tmp = tmp->next;
+ }
+ }
+ break;
+ }
+ last = tmp;
+ tmp = tmp->next;
+ }
+
+ if (!found && status != 6) {
+ found = (struct aji_resource *) malloc(sizeof(struct aji_resource));
+ if (!found) {
+ ast_log(LOG_ERROR, "Out of memory!\n");
+ return;
+ }
+ ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
+ found->status = status;
+ found->priority = priority;
+ found->next = NULL;
+ last = NULL;
+ tmp = buddy->resources;
+ while (tmp) {
+ if (found->priority > tmp->priority) {
+ if (last)
+ last->next = found;
+ found->next = tmp;
+ if (!last)
+ buddy->resources = found;
+ break;
+ }
+ if (!tmp->next) {
+ tmp->next = found;
+ break;
+ }
+ last = tmp;
+ tmp = tmp->next;
+ }
+ if (!tmp)
+ buddy->resources = found;
+ }
+
+ node = iks_find_attrib(iks_find(pak->x, "c"), "node");
+ ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
+
+ if(status !=6 && !found->cap) {
+ found->cap = aji_find_version(node, ver, pak);
+ if(gtalk_yuck(pak->x)) /* gtalk should do discover */
+ found->cap->jingle = 1;
+ if(found->cap->jingle)
+ ast_log(LOG_DEBUG,"Special case for google till they support discover.\n");
+ else {
+ iks *iq, *query;
+ iq = iks_new("iq");
+ query = iks_new("query");
+ if(query && iq) {
+ iks_insert_attrib(iq, "type", "get");
+ iks_insert_attrib(iq, "to", pak->from->full);
+ iks_insert_attrib(iq, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
+ iks_insert_node(iq, query);
+ iks_send(client->p, iq);
+
+ } else
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ if(query)
+ iks_delete(query);
+ if(iq)
+ iks_delete(iq);
+ }
+ }
+
+ switch (pak->subtype) {
+ case IKS_TYPE_AVAILABLE:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am available ^_* %i\n", pak->subtype);
+ break;
+ case IKS_TYPE_UNAVAILABLE:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: I am unavailable ^_* %i\n", pak->subtype);
+ break;
+ default:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: Ohh sexy and the wrong type%i\n", pak->subtype);
+ }
+ switch (pak->show) {
+ case IKS_SHOW_UNAVAILABLE:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
+ break;
+ case IKS_SHOW_AVAILABLE:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is available\n");
+ break;
+ case IKS_SHOW_CHAT:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
+ break;
+ case IKS_SHOW_AWAY:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: type is away\n");
+ break;
+ case IKS_SHOW_XA:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
+ break;
+ case IKS_SHOW_DND:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
+ break;
+ default:
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: Kinky! how did that happen %i\n", pak->show);
+ }
+}
+
+/*!
+ * \brief handles subscription requests.
+ * \param aji_client struct and xml packet.
+ * \return void.
+ */
+static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
+{
+ int res = 0;
+ switch (pak->subtype) {
+ case IKS_TYPE_SUBSCRIBE:
+ res = iks_send(client->p, iks_make_s10n(IKS_TYPE_SUBSCRIBED, iks_find_attrib(pak->x, "from"), "Asterisk has approved subscription"));
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
+ break;
+ case IKS_TYPE_SUBSCRIBED:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
+ break;
+ case IKS_TYPE_UNSUBSCRIBE:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
+ break;
+ case IKS_TYPE_UNSUBSCRIBED:
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
+ break;
+ default: /*IKS_TYPE_ERROR: */
+ if (option_verbose > 30)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
+ break;
+ }
+}
+
+/*!
+ * \brief sends messages.
+ * \param aji_client struct , reciever, message.
+ * \return 1.
+ */
+int ast_aji_send(struct aji_client *client, char *address, char *message)
+{
+ int res = 0;
+ iks *message_packet = NULL;
+ if (client->state == AJI_CONNECTED) {
+ message_packet = iks_make_msg(IKS_TYPE_CHAT, address, message);
+ if (message_packet) {
+ iks_insert_attrib(message_packet, "from", client->jid->full);
+ res = iks_send(client->p, message_packet);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (message_packet)
+ iks_delete(message_packet);
+ } else
+ ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
+ return 1;
+}
+
+/*!
+ * \brief create a chatroom.
+ * \param aji_client struct , room, server, topic for the room.
+ * \return 0.
+ */
+int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
+{
+ int res = 0;
+ iks *iq = NULL;
+ iq = iks_new("iq");
+ if (iq && client) {
+ iks_insert_attrib(iq, "type", "get");
+ iks_insert_attrib(iq, "to", server);
+ iks_insert_attrib(iq, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_send(client->p, iq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ return res;
+}
+
+/*!
+ * \brief join a chatroom.
+ * \param aji_client struct , room.
+ * \return res.
+ */
+int ast_aji_join_chat(struct aji_client *client, char *room)
+{
+ int res = 0;
+ iks *presence = NULL, *priority = NULL;
+ presence = iks_new("presence");
+ priority = iks_new("priority");
+ if (presence && priority && client) {
+ iks_insert_cdata(priority, "0", 1);
+ iks_insert_attrib(presence, "to", room);
+ iks_insert_node(presence, priority);
+ res = iks_send(client->p, presence);
+ iks_insert_cdata(priority, "5", 1);
+ iks_insert_attrib(presence, "to", room);
+ res = iks_send(client->p, presence);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (presence)
+ iks_delete(presence);
+ if (priority)
+ iks_delete(priority);
+ return res;
+}
+
+/*!
+ * \brief invite to a chatroom.
+ * \param aji_client struct ,user, room, message.
+ * \return res.
+ */
+int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
+{
+ int res = 0;
+ iks *invite = NULL, *body = NULL, *namespace = NULL;
+ invite = iks_new("message");
+ body = iks_new("body");
+ namespace = iks_new("x");
+ if (client && invite && body && namespace) {
+ iks_insert_attrib(invite, "to", user);
+ iks_insert_attrib(invite, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_insert_cdata(body, message, strlen(message));
+ iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
+ iks_insert_attrib(namespace, "jid", room);
+ iks_insert_node(invite, body);
+ iks_insert_node(invite, namespace);
+ res = iks_send(client->p, invite);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (body)
+ iks_delete(body);
+ if (namespace)
+ iks_delete(namespace);
+ if (invite)
+ iks_delete(invite);
+ return res;
+}
+
+
+/*!
+ * \brief receive message loop.
+ * \param aji_client struct.
+ * \return void.
+ */
+static void *aji_recv_loop(void *data)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ int res = 0;
+ while (res == IKS_OK) {
+ res = iks_recv(client->p, 1);
+ client->timeout--;
+ if (res == IKS_HOOK) {
+ ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
+ break;
+ } else if (res == IKS_NET_TLSFAIL) {
+ ast_log(LOG_ERROR, "JABBER: Failure in tls.\n");
+ break;
+ } else if (client->timeout == 0 && client->state != AJI_CONNECTED) {
+ res = -1;
+ ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
+ } else if (res == IKS_NET_RWERR) {
+ ast_log(LOG_ERROR, "JABBER: socket read error\n");
+ }
+
+ if (res != IKS_OK) {
+ ast_verbose("reconnecting %d\n", res);
+ aji_reconnect(client);
+ res = IKS_OK;
+ }
+
+ }
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return 0;
+}
+
+/*!
+ * \brief increments the mid field for messages and other events.
+ * \param message id.
+ * \return void.
+ */
+void ast_aji_increment_mid(char *mid)
+{
+ int i = 0;
+ for (i = strlen(mid) - 1; i >= 0; i--) {
+ if (mid[i] != 'z') {
+ mid[i] = mid[i] + 1;
+ i = 0;
+ } else
+ mid[i] = 'a';
+ }
+}
+
+
+/*!
+ * \brief attempts to register to a transport.
+ * \param aji_client struct, and xml packet.
+ * \return IKS_FILTER_EAT.
+ */
+static int aji_register_transport(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ int res = 0;
+ struct aji_buddy *buddy = NULL;
+ iks *send = NULL;
+ send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
+ if (client && send) {
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ if (iterator->btype == AJI_TRANS) {
+ buddy = iterator;
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+ iks_filter_remove_hook(client->f, aji_register_transport);
+ 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);
+ iks_insert_attrib(send, "to", buddy->host);
+ iks_insert_attrib(send, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_insert_attrib(send, "from", client->user);
+ res = iks_send(client->p, send);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (send)
+ iks_delete(send);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+
+}
+
+/*!
+ * \brief attempts to register to a transport step 2.
+ * \param aji_client struct, and xml packet.
+ * \return IKS_FILTER_EAT.
+ */
+static int aji_register_transport2(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ int res = 0;
+ struct aji_buddy *buddy = NULL;
+ iks *regquery = NULL, *reguser = NULL, *regpass = NULL, *regiq = NULL;
+ regiq = iks_new("iq");
+ regquery = iks_new("query");
+ reguser = iks_new("username");
+ regpass = iks_new("password");
+
+ if (client && regquery && reguser && regpass && regiq) {
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ if (iterator->btype == AJI_TRANS)
+ buddy = iterator; ASTOBJ_UNLOCK(iterator);
+ });
+ iks_filter_remove_hook(client->f, aji_register_transport2);
+ iks_insert_attrib(regiq, "to", buddy->host);
+ iks_insert_attrib(regiq, "type", "set");
+ iks_insert_attrib(regiq, "id", client->mid);
+ ast_aji_increment_mid(client->mid);
+ iks_insert_attrib(regiq, "from", client->user);
+ iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
+ iks_insert_cdata(reguser, buddy->user, strlen(buddy->user));
+ iks_insert_cdata(regpass, buddy->pass, strlen(buddy->pass));
+ iks_insert_node(regiq, regquery);
+ iks_insert_node(regquery, reguser);
+ iks_insert_node(regquery, regpass);
+ res = iks_send(client->p, regiq);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (regiq)
+ iks_delete(regiq);
+ if (regquery)
+ iks_delete(regquery);
+ if (reguser)
+ iks_delete(reguser);
+ if (regpass)
+ iks_delete(regpass);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+}
+
+/*!
+ * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
+ * \param aji_client struct.
+ * \return void.
+ */
+static void aji_pruneregister(struct aji_client *client)
+{
+ int res = 0;
+ iks *removeiq = NULL, *removequery = NULL, *removeitem = NULL, *send = NULL;
+ removeiq = iks_new("iq");
+ removequery = iks_new("query");
+ removeitem = iks_new("item");
+ send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
+
+ if (client && removeiq && removequery && removeitem && send) {
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
+ * be called at the same time */
+ if (ast_test_flag(iterator, AJI_AUTOPRUNE)) {
+ res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
+ "GoodBye your status is no longer needed by Asterisk the Open Source PBX"
+ " so I am no longer subscribing to your presence.\n"));
+ res = iks_send(client->p, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
+ "GoodBye you are no longer in the asterisk config file so I am removing"
+ " your access to my presence.\n"));
+ iks_insert_attrib(removeiq, "from", client->jid->full);
+ iks_insert_attrib(removeiq, "type", "set");
+ iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
+ iks_insert_attrib(removeitem, "jid", iterator->name);
+ iks_insert_attrib(removeitem, "subscription", "remove");
+ iks_insert_node(removeiq, removequery);
+ iks_insert_node(removequery, removeitem);
+ res = iks_send(client->p, removeiq);
+ } else if (ast_test_flag(iterator, AJI_AUTOREGISTER)) {
+ if (iterator->btype == AJI_USER) { /*if it is not a transport */
+ res = iks_send(client->p, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name,
+ "Greetings I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
+ } else {
+ iks_filter_add_rule(client->f, aji_register_transport, client,
+ IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS,
+ "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
+ iks_insert_attrib(send, "to", iterator->host);
+ iks_insert_attrib(send, "from", client->jid->full);
+ res = iks_send(client->p, send);
+ }
+ ast_clear_flag(iterator, AJI_AUTOREGISTER);
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (removeiq)
+ iks_delete(removeiq);
+ if (removequery)
+ iks_delete(removequery);
+ if (removeitem)
+ iks_delete(removeitem);
+ if (send)
+ iks_delete(send);
+ ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, aji_buddy_destroy);
+}
+
+/*!
+ * \brief filters the roster packet we get back from server.
+ * \param aji_client struct, and xml packet.
+ * \return IKS_FILTER_EAT.
+ */
+static int aji_filter_roster(void *data, ikspak *pak)
+{
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ int flag = 0;
+ iks *x = NULL;
+ struct aji_buddy *buddy;
+
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ x = iks_child(pak->query);
+ flag = 0;
+ while (x) {
+ if (!iks_strcmp(iks_name(x), "item")) {
+ if (!ast_strlen_zero(iterator->pass)) {
+ if (!strcasecmp(iterator->host, iks_find_attrib(x, "jid"))) {
+ ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
+ flag = 1;
+ }
+ } else {
+ if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
+ flag = 1;
+ ast_clear_flag(iterator, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
+ }
+ }
+ }
+ x = iks_next(x);
+ }
+ if (!flag)
+ ast_copy_flags(iterator, client, AJI_AUTOREGISTER);
+ if (x)
+ iks_delete(x);
+ ASTOBJ_UNLOCK(iterator);
+ });
+
+ x = iks_child(pak->query);
+ while (x) {
+ flag = 0;
+ if (iks_strcmp(iks_name(x), "item") == 0) {
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ if (!ast_strlen_zero(iterator->pass)) {
+ if (!strcasecmp(iterator->host, iks_find_attrib(x, "jid")))
+ flag = 1;
+ } else {
+ if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
+ flag = 1;
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+
+ if (!flag) {
+ if (ast_test_flag(client, AJI_AUTOPRUNE)) {
+ buddy = (struct aji_buddy *) malloc(sizeof(struct aji_buddy));
+ if (!buddy)
+ ast_log(LOG_WARNING, "Out of memory\n");
+ memset(buddy, 0, sizeof(struct aji_buddy));
+ ASTOBJ_INIT(buddy);
+ ASTOBJ_WRLOCK(buddy);
+ ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
+ ast_copy_string(buddy->user, iks_find_attrib(x, "jid"), sizeof(buddy->user));
+ ast_clear_flag(buddy, AST_FLAGS_ALL);
+ ast_set_flag(buddy, AJI_AUTOPRUNE);
+ buddy->objflags |= ASTOBJ_FLAG_MARKED;
+ ASTOBJ_UNLOCK(buddy);
+ if (buddy) {
+ ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
+ ASTOBJ_UNREF(buddy, aji_buddy_destroy);
+ }
+ }
+ }
+ }
+ x = iks_next(x);
+ }
+ if (x)
+ iks_delete(x);
+ aji_pruneregister(client);
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return IKS_FILTER_EAT;
+}
+
+static int aji_reconnect(struct aji_client *client)
+{
+ int res = 0;
+
+ if (client->state)
+ client->state = AJI_DISCONNECTED;
+ if (client->p)
+ iks_parser_reset(client->p);
+ if (client->authorized)
+ client->authorized = 0;
+
+ switch (client->component) {
+ case AJI_COMPONENT:
+ res = aji_component_initialize(client);
+ break;
+ case AJI_CLIENT:
+ res = aji_client_initialize(client);
+ break;
+ }
+ return res;
+}
+
+static int aji_get_roster(struct aji_client *client)
+{
+ iks *roster = NULL;
+ roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
+ if(roster) {
+ iks_insert_attrib(roster, "id", "roster");
+ aji_set_presence(client, client->jid->full, 1, "im available");
+ iks_send(client->p, roster);
+ }
+ if (roster)
+ iks_delete(roster);
+ return 1;
+}
+
+/*!
+ * \brief connects as a client to jabber server.
+ * \param aji_client struct, and xml packet.
+ * \return res.
+ */
+static int aji_client_connect(void *data, ikspak *pak)
+{
+ ast_verbose("I SUCK!\n");
+ struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+ int res = 0;
+ if (client) {
+ if (client->state == AJI_DISCONNECTED) {
+ 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);
+ client->state = AJI_CONNECTED;
+ client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
+ iks_filter_remove_hook(client->f, aji_client_connect);
+ if(client->component == AJI_CLIENT)
+ aji_get_roster(client);
+ }
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return res;
+}
+
+/*!
+ * \brief prepares client for connect.
+ * \param aji_client struct.
+ * \return 1.
+ */
+static int aji_client_initialize(struct aji_client *client)
+{
+ int connected = 0;
+
+ connected = iks_connect_via(client->p, client->serverhost, client->port, client->jid->server);
+
+ if (connected == IKS_NET_NOCONN) {
+ ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
+ return 0;
+ }
+ if (connected == IKS_NET_NODNS) {
+ ast_log(LOG_ERROR, "JABBER ERROR: No DNS\n");
+ return 0;
+ } else
+ iks_recv(client->p, 30);
+ return 1;
+}
+
+/*!
+ * \brief prepares component for connect.
+ * \param aji_client struct.
+ * \return 1.
+ */
+static int aji_component_initialize(struct aji_client *client)
+{
+ int connected = 1;
+ connected = iks_connect_via(client->p, client->jid->server, client->port, client->user);
+ if (connected == IKS_NET_NOCONN)
+ ast_log(LOG_ERROR, "JABBER ERROR: No Connection");
+ if (connected == IKS_NET_NODNS)
+ ast_log(LOG_ERROR, "JABBER ERROR: No DNS");
+ if (!connected)
+ iks_recv(client->p, 30);
+ return 1;
+}
+
+/*!
+ * \brief disconnect from jabber server.
+ * \param aji_client struct.
+ * \return 1.
+ */
+int ast_aji_disconnect(struct aji_client *client)
+{
+ if (client) {
+ if (option_verbose > 3)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: Disconnecting\n");
+ iks_disconnect(client->p);
+ iks_parser_delete(client->p);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ }
+
+ return 1;
+}
+
+/*!
+ * \brief set presence of client.
+ * \param aji_client struct, user to send it to, level, description.
+ * \return void.
+ */
+static void aji_set_presence(struct aji_client *client, char *user, int level, char *desc)
+{
+ int res = 0;
+ iks *presence = NULL, *priority, *cnode = NULL;
+ presence = iks_make_pres(level, desc);
+ cnode = iks_new("c");
+ priority = iks_new("priority");
+ iks_insert_cdata(priority, "0", 1);
+ if (presence && cnode && client) {
+ iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
+ iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
+ iks_insert_attrib(cnode, "ext", "voice-v1");
+ iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
+ iks_insert_node(presence, cnode);
+ res = iks_send(client->p, presence);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory.\n");
+ }
+ if (cnode)
+ iks_delete(cnode);
+ if (presence)
+ iks_delete(presence);
+}
+
+/*!
+ * \brief turnon console debugging.
+ * \param fd, number of args, args.
+ * \return RESULT_SUCCESS.
+ */
+static int aji_do_debug(int fd, int argc, char *argv[])
+{
+ ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ iterator->debug = 1;
+ ASTOBJ_UNLOCK(iterator);
+ });
+ if (option_verbose > 3)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: Debugging Enabled.\n");
+ return RESULT_SUCCESS;
+}
+
+/*!
+ * \brief reload jabber module.
+ * \param fd, number of args, args.
+ * \return RESULT_SUCCESS.
+ */
+static int aji_do_reload(int fd, int argc, char *argv[])
+{
+ aji_reload();
+ if (option_verbose > 3)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: Reloaded.\n");
+ return RESULT_SUCCESS;
+}
+
+/*!
+ * \brief turnoff console debugging.
+ * \param fd, number of args, args.
+ * \return RESULT_SUCCESS.
+ */
+static int aji_no_debug(int fd, int argc, char *argv[])
+{
+ ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ iterator->debug = 0;
+ ASTOBJ_UNLOCK(iterator);
+ });
+ if (option_verbose > 2)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: Debugging Disabled\n");
+ return RESULT_SUCCESS;
+}
+
+/*!
+ * \brief show client status.
+ * \param fd, number of args, args.
+ * \return RESULT_SUCCESS.
+ */
+static int aji_show_clients(int fd, int argc, char *argv[])
+{
+ char *status = "";
+ ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ switch (iterator->state) {
+ case AJI_DISCONNECTED:
+ status = "Disconnected";
+ break;
+ case AJI_CONNECTING:
+ case AJI_ALMOST:
+ status = "Connecting";
+ break;
+ case AJI_CONNECTED:
+ status = "Connected";
+ break;
+ default:
+ status = "Unknown";
+ }
+ ast_verbose("JABBER: User: %s is %s\n", iterator->user, status);
+ ASTOBJ_UNLOCK(iterator);
+ });
+ return RESULT_SUCCESS;
+}
+
+/*!
+ * \brief send test message for debuging.
+ * \param fd, number of args, args.
+ * \return RESULT_SUCCESS.
+ */
+static int aji_test(int fd, int argc, char *argv[])
+{
+ struct aji_client *client;
+ struct aji_resource *resource;
+ const char *name = "asterisk";
+
+ if (argc > 3)
+ return RESULT_SHOWUSAGE;
+ else if (argc == 3)
+ name = argv[2];
+
+ if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
+ ast_cli(fd, "Unable to find client '%s'!\n", name);
+ return RESULT_FAILURE;
+ }
+
+ ast_aji_send(client, "mogorman@astjab.org", "blahblah");
+ ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ ast_verbose("User: %s\n", iterator->name);
+ ast_verbose("User: %s\n", iterator->user);
+ ast_verbose("Pass: %s\n", iterator->pass);
+ ast_verbose("Host: %s\n", iterator->host);
+ for (resource = iterator->resources; resource; resource = resource->next) {
+ ast_verbose("Resource: %s\n", resource->resource);
+ if(resource->cap) {
+ ast_verbose(" client: %s\n", resource->cap->parent->node);
+ ast_verbose(" version: %s\n", resource->cap->version);
+ ast_verbose(" Jingle Capable: %d\n", resource->cap->jingle);
+ }
+ ast_verbose(" Priority: %d\n", resource->priority);
+ ast_verbose(" Status: %d\n", resource->status);
+ ast_verbose(" Message: %s\n", resource->description);
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+
+ return RESULT_SUCCESS;
+}
+
+/*!
+ * \brief creates aji_client structure.
+ * \param label, ast_variable, debug, pruneregister, component/client, aji_client to dump into.
+ * \return 0.
+ */
+static int aji_create_client(char *label, struct ast_variable *var, int debug)
+{
+ char *resource;
+ struct aji_client *client = NULL;
+ int flag = 0;
+ client = ASTOBJ_CONTAINER_FIND(&clients,label);
+ if(!client) {
+ ast_verbose("CLIENT NOT FOUND!\n");
+ flag = 1;
+ client = (struct aji_client *) malloc(sizeof(struct aji_client));
+ if(client) {
+ memset(client, 0, sizeof(struct aji_client));
+ ASTOBJ_INIT(client);
+ ASTOBJ_WRLOCK(client);
+ ASTOBJ_CONTAINER_INIT(&client->buddies);
+ } else {
+ ast_log(LOG_ERROR, "Out of memory!\n");
+ return 0;
+ }
+ } else {
+ ASTOBJ_WRLOCK(client);
+ ASTOBJ_UNMARK(client);
+ }
+ ASTOBJ_CONTAINER_MARKALL(&client->buddies);
+ ast_copy_string(client->name, label, sizeof(client->name));
+ ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
+
+ client->debug = debug;
+ ast_copy_flags(client, &globalflags, AST_FLAGS_ALL);
+ client->port = 5222;
+ client->usetls = 1;
+ client->forcessl = 0;
+ client->keepalive = 1;
+ client->timeout = 20;
+ client->component = AJI_CLIENT;
+ if (flag) client->authorized = 0;
+ client->usesasl = 0;
+ if (flag) client->state = AJI_DISCONNECTED;
+ while (var) {
+ ast_verbose("var->value: %s\n",var->value);
+ if (!strcasecmp(var->name, "username"))
+ ast_copy_string(client->user, var->value, sizeof(client->user));
+ else if (!strcasecmp(var->name, "serverhost"))
+ ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
+ else if (!strcasecmp(var->name, "secret"))
+ ast_copy_string(client->password, var->value, sizeof(client->password));
+ else if (!strcasecmp(var->name, "port"))
+ client->port = atoi(var->value);
+ else if (!strcasecmp(var->name, "debug"))
+ client->debug = (ast_false(var->value)) ? 0 : 1;
+ else if (!strcasecmp(var->name, "type")){
+ if (!strcasecmp(var->value, "component"))
+ client->component = AJI_COMPONENT;
+ } else if (!strcasecmp(var->name, "usetls")) {
+ client->usetls = (ast_false(var->value)) ? 0 : 1;
+ ast_verbose("USETLS = %d\n",client->usetls);
+ } else if (!strcasecmp(var->name, "usesasl")) {
+ client->usesasl = (ast_false(var->value)) ? 0 : 1;
+ ast_verbose("USESASL = %d\n",client->usesasl);
+ } else if (!strcasecmp(var->name, "forceoldssl"))
+ client->forcessl = (ast_false(var->value)) ? 0 : 1;
+ else if (!strcasecmp(var->name, "keepalive"))
+ client->keepalive = (ast_false(var->value)) ? 0 : 1;
+ else if (!strcasecmp(var->name, "autoprune"))
+ ast_set2_flag(client, ast_true(var->value), AJI_AUTOPRUNE);
+ else if (!strcasecmp(var->name, "autoregister"))
+ ast_set2_flag(client, ast_true(var->value), AJI_AUTOREGISTER);
+ else if (!strcasecmp(var->name, "buddy"))
+ aji_create_buddy(var->value, client);
+ else if (!strcasecmp(var->name, "transport"))
+ aji_create_transport(var->value, client);
+ var = var->next;
+ }
+ if(flag) {
+ client->p = iks_stream_new(((client->component == AJI_CLIENT) ? "jabber:client" : "jabber:component:accept"), client, aji_act_hook);
+ if (!client->p) {
+ ast_log(LOG_WARNING, "Failed to create stream for client '%s'!\n", client->name);
+ return 0;
+ }
+ client->stack = iks_stack_new(8192, 8192);
+ if (!client->stack) {
+ ast_log(LOG_WARNING, "Failed to allocate stack for client '%s'\n", client->name);
+ return 0;
+ }
+ client->f = iks_filter_new();
+ if (!client->f) {
+ ast_log(LOG_WARNING, "Failed to create filter for client '%s'\n", client->name);
+ return 0;
+ }
+ if (!strchr(client->user, '/') && client->component == AJI_CLIENT) {
+ resource = NULL;
+ asprintf(&resource, "%s/asterisk", client->user);
+ if (resource) {
+ client->jid = iks_id_new(client->stack, resource);
+ free(resource);
+ }
+ } else
+ client->jid = iks_id_new(client->stack, client->user);
+ if (client->component == AJI_COMPONENT) {
+ iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
+ iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
+ 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);
+ 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);
+ } else {
+ iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
+ }
+ if (!strchr(client->user, '/') && client->component == AJI_CLIENT) {
+ resource = NULL;
+ asprintf(&resource, "%s/asterisk", client->user);
+ if (resource) {
+ client->jid = iks_id_new(client->stack, resource);
+ free(resource);
+ }
+ } else
+ client->jid = iks_id_new(client->stack, client->user);
+ iks_set_log_hook(client->p, aji_log_hook);
+ ASTOBJ_UNLOCK(client);
+ ASTOBJ_CONTAINER_LINK(&clients,client);
+ } else {
+ ASTOBJ_UNLOCK(client);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ }
+ return 1;
+}
+
+/*!
+ * \brief creates transport.
+ * \param label, buddy to dump it into.
+ * \return 0.
+ */
+static int aji_create_transport(char *label, struct aji_client *client)
+{
+ char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
+ struct aji_buddy *buddy = NULL;
+
+ buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
+ if (!buddy) {
+ buddy = malloc(sizeof(struct aji_buddy));
+ if(!buddy) {
+ ast_log(LOG_WARNING, "Out of memory\n");
+ return 0;
+ } else {
+ memset(buddy, 0, sizeof(struct aji_buddy));
+ ASTOBJ_INIT(buddy);
+
+ }
+ }
+ ASTOBJ_WRLOCK(buddy);
+ server = label;
+ if ((buddyname = strchr(label, ','))) {
+ *buddyname = '\0';
+ buddyname++;
+ if (buddyname && buddyname[0] != '\0') {
+ if ((user = strchr(buddyname, ','))) {
+ *user = '\0';
+ user++;
+ if (user && user[0] != '\0') {
+ if ((pass = strchr(user, ','))) {
+ *pass = '\0';
+ pass++;
+ ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
+ ast_copy_string(buddy->user, user, sizeof(buddy->user));
+ ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
+ ast_copy_string(buddy->server, server, sizeof(buddy->server));
+ return 1;
+ }
+ }
+ }
+ }
+ }
+ ASTOBJ_UNLOCK(buddy);
+ ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
+ return 0;
+}
+
+/*!
+ * \brief creates buddy.
+ * \param label, buddy to dump it into.
+ * \return 0.
+ */
+static int aji_create_buddy(char *label, struct aji_client *client)
+{
+ struct aji_buddy *buddy = NULL;
+ int flag = 0;
+ buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
+ if (!buddy) {
+ flag = 1;
+ buddy = malloc(sizeof(struct aji_buddy));
+ if(!buddy) {
+ ast_log(LOG_WARNING, "Out of memory\n");
+ return 0;
+ } else {
+ memset(buddy, 0, sizeof(struct aji_buddy));
+ ASTOBJ_INIT(buddy);
+
+ }
+ }
+ ASTOBJ_WRLOCK(buddy);
+ ast_copy_string(buddy->name, label, sizeof(buddy->name));
+ ASTOBJ_UNLOCK(buddy);
+ if(flag)
+ ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
+ else
+ ASTOBJ_UNREF(buddy, aji_buddy_destroy);
+ return 1;
+}
+
+/*!
+ * \brief load config file.
+ * \param void.
+ * \return 1.
+ */
+static int aji_load_config(void)
+{
+ char *cat = NULL;
+ int debug = 1;
+ struct ast_config *cfg = NULL;
+ struct ast_variable *var = NULL;
+
+ cfg = ast_config_load(JABBER_CONFIG);
+ if (!cfg) {
+ ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
+ return 0;
+ }
+
+ cat = ast_category_browse(cfg, NULL);
+ for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
+ if (!strcasecmp(var->name, "debug"))
+ debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
+ else if (!strcasecmp(var->name, "autoprune"))
+ ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
+ else if (!strcasecmp(var->name, "autoregister"))
+ ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
+ }
+
+ while (cat) {
+ if (strcasecmp(cat, "general")) {
+ var = ast_variable_browse(cfg, cat);
+ aji_create_client(cat, var, debug);
+ }
+ cat = ast_category_browse(cfg, cat);
+ }
+ return 1;
+}
+
+/*!
+ * \brief grab a aji_client structure by label name.
+ * \param void.
+ * \return 1.
+ */
+
+struct aji_client *ast_aji_get_client(char *name)
+{
+ struct aji_client *client = NULL;
+ client = ASTOBJ_CONTAINER_FIND(&clients, name);
+ if (!client && !strchr(name, '@'))
+ client = ASTOBJ_CONTAINER_FIND_FULL(&clients, name, user,,, strcasecmp);
+ return client;
+}
+
+struct aji_client_container *ast_aji_get_clients(void)
+{
+ return &clients;
+}
+
+static void aji_reload()
+{
+ int res = -1;
+ ASTOBJ_CONTAINER_MARKALL(&clients);
+ if (!aji_load_config())
+ ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
+ else {
+ ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, aji_client_destroy);
+ ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ if(iterator->state == AJI_DISCONNECTED) {
+ res = aji_reconnect(iterator);
+ if (res != -1 && !iterator->thread)
+ ast_pthread_create(&iterator->thread, NULL, aji_recv_loop, iterator);
+ } else if (iterator->state == AJI_CONNECTED) {
+ aji_get_roster(iterator);
+ }
+ ASTOBJ_UNLOCK(iterator);
+ });
+ }
+}
+
+static int unload_module(void *mod)
+{
+ ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
+ ASTOBJ_RDLOCK(iterator);
+ if (option_verbose > 2)
+ ast_verbose(VERBOSE_PREFIX_3 "JABBER: %s\n", iterator->name);
+ iterator->state = AJI_DISCONNECTED;
+ ast_aji_disconnect(iterator);
+ pthread_join(iterator->thread, NULL);
+ ASTOBJ_UNLOCK(iterator);
+ });
+
+ ASTOBJ_CONTAINER_DESTROYALL(&clients, aji_client_destroy);
+ ASTOBJ_CONTAINER_DESTROY(&clients);
+
+ STANDARD_HANGUP_LOCALUSERS;
+ ast_cli_unregister_multiple(aji_cli, sizeof(aji_cli) / sizeof(aji_cli[0]));
+ ast_unregister_application(app_ajisend);
+ ast_log(LOG_NOTICE, "res_jabber unloaded.\n");
+ return 0;
+}
+
+static int load_module(void *mod)
+{
+ ASTOBJ_CONTAINER_INIT(&clients);
+ aji_reload();
+ ast_register_application(app_ajisend, aji_send_exec, ajisend_synopsis, ajisend_descrip);
+ ast_register_application(app_ajistatus, aji_status_exec, ajistatus_synopsis, ajistatus_descrip);
+ ast_cli_register_multiple(aji_cli, sizeof(aji_cli) / sizeof(aji_cli[0]));
+ ast_log(LOG_NOTICE, "res_jabber.so loaded.\n");
+ return 0;
+}
+
+static int reload(void *mod)
+{
+ aji_reload();
+ ast_log(LOG_ERROR, "ooh reload magic!!!\n");
+ return 0;
+}
+
+static const char *description(void)
+{
+ return tdesc;
+}
+
+static const char *key(void)
+{
+ return ASTERISK_GPL_KEY;
+}
+
+STD_MOD(MOD_0, reload, NULL, NULL);