[asterisk-commits] phsultan: trunk r220457 - in /trunk: ./ channels/ configs/ doc/ include/aster...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Sep 25 05:54:59 CDT 2009
Author: phsultan
Date: Fri Sep 25 05:54:42 2009
New Revision: 220457
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=220457
Log:
Add JABBER_RECEIVE as a dialplan function, implement SendText in Jingle channels
JABBER_RECEIVE (along with JabberSend) makes Asterisk interact with users over
XMPP to process calls.
SendText can be used instead of JabberSend in the context of XMPP based voice
channels (chan_gtalk and chan_jingle).
(closes issue #12569)
Reported by: eech55
Tested by: phsultan, asannucci, lmadsen, jtodd, maxgo
Review: https://reviewboard.asterisk.org/r/88/
Modified:
trunk/CHANGES
trunk/channels/chan_gtalk.c
trunk/channels/chan_jingle.c
trunk/configs/jabber.conf.sample
trunk/doc/jabber.txt
trunk/include/asterisk/jabber.h
trunk/res/res_jabber.c
Modified: trunk/CHANGES
URL: http://svnview.digium.com/svn/asterisk/trunk/CHANGES?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/CHANGES (original)
+++ trunk/CHANGES Fri Sep 25 05:54:42 2009
@@ -117,6 +117,8 @@
mode=multirow. If rowlimit is set, then additional rows may be retrieved
from the same query by using the name of the function which retrieved the
first row as an argument to ODBC_FETCH().
+ * Added JABBER_RECEIVE, which permits receiving XMPP messages from the
+ dialplan. This function returns the content of the received message.
Dialplan Variables
------------------
@@ -264,6 +266,11 @@
Asterisk component that reports security events. However, SIP support will be
coming soon. For more information on the security events framework, see the
"Security Events" chapter of the included documentation - doc/tex/asterisk.pdf.
+
+Miscellaneous
+-------------
+ * SendText is now implemented in chan_gtalk and chan_jingle. It will simply send
+ XMPP text messages to the remote JID.
------------------------------------------------------------------------------
--- Functionality changes from Asterisk 1.6.1 to Asterisk 1.6.2 -------------
@@ -414,7 +421,7 @@
can connect calls in passthrough mode, as well as record and play back files.
* Successful and unsuccessful call pickup can now be alerted through sounds, by
using pickupsound and pickupfailsound in features.conf.
- * ASTVARRUNDIR is now set to $(localstatedir)/run/asterisk by default.
+ * ASTVARRUNDIR is now set to $(localstatedir)/run/asterisk by default.
This means the asterisk pid file will now be in /var/run/asterisk/asterisk.pid on LINUX
instead of the /var/run/asterisk.pid where it used to be. This will make
installs as non-root easier to manage.
Modified: trunk/channels/chan_gtalk.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_gtalk.c?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/channels/chan_gtalk.c (original)
+++ trunk/channels/chan_gtalk.c Fri Sep 25 05:54:42 2009
@@ -168,6 +168,7 @@
/* Forward declarations */
static struct ast_channel *gtalk_request(const char *type, int format, const struct ast_channel *requestor, void *data, int *cause);
static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration);
+static int gtalk_sendtext(struct ast_channel *ast, const char *text);
static int gtalk_digit_begin(struct ast_channel *ast, char digit);
static int gtalk_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int gtalk_call(struct ast_channel *ast, char *dest, int timeout);
@@ -191,6 +192,7 @@
.description = "Gtalk Channel Driver",
.capabilities = AST_FORMAT_AUDIO_MASK,
.requester = gtalk_request,
+ .send_text = gtalk_sendtext,
.send_digit_begin = gtalk_digit_begin,
.send_digit_end = gtalk_digit_end,
.bridge = ast_rtp_instance_bridge,
@@ -1496,6 +1498,25 @@
res = -1;
}
+ return res;
+}
+
+static int gtalk_sendtext(struct ast_channel *chan, const char *text)
+{
+ int res = 0;
+ struct aji_client *client = NULL;
+ struct gtalk_pvt *p = chan->tech_pvt;
+
+ if (!p->parent) {
+ ast_log(LOG_ERROR, "Parent channel not found\n");
+ return -1;
+ }
+ if (!p->parent->connection) {
+ ast_log(LOG_ERROR, "XMPP client not found\n");
+ return -1;
+ }
+ client = p->parent->connection;
+ res = ast_aji_send_chat(client, p->them, text);
return res;
}
Modified: trunk/channels/chan_jingle.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_jingle.c?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/channels/chan_jingle.c (original)
+++ trunk/channels/chan_jingle.c Fri Sep 25 05:54:42 2009
@@ -169,6 +169,7 @@
/* Forward declarations */
static struct ast_channel *jingle_request(const char *type, int format, const struct ast_channel *requestor, void *data, int *cause);
+static int jingle_sendtext(struct ast_channel *ast, const char *text);
static int jingle_digit_begin(struct ast_channel *ast, char digit);
static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int jingle_call(struct ast_channel *ast, char *dest, int timeout);
@@ -190,6 +191,7 @@
.description = "Jingle Channel Driver",
.capabilities = AST_FORMAT_AUDIO_MASK,
.requester = jingle_request,
+ .send_text = jingle_sendtext,
.send_digit_begin = jingle_digit_begin,
.send_digit_end = jingle_digit_end,
.bridge = ast_rtp_instance_bridge,
@@ -1270,6 +1272,26 @@
res = -1;
}
+ return res;
+}
+
+static int jingle_sendtext(struct ast_channel *chan, const char *text)
+{
+ int res = 0;
+ struct aji_client *client = NULL;
+ struct jingle_pvt *p = chan->tech_pvt;
+
+
+ if (!p->parent) {
+ ast_log(LOG_ERROR, "Parent channel not found\n");
+ return -1;
+ }
+ if (!p->parent->connection) {
+ ast_log(LOG_ERROR, "XMPP client not found\n");
+ return -1;
+ }
+ client = p->parent->connection;
+ res = ast_aji_send_chat(client, p->them, text);
return res;
}
Modified: trunk/configs/jabber.conf.sample
URL: http://svnview.digium.com/svn/asterisk/trunk/configs/jabber.conf.sample?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/configs/jabber.conf.sample (original)
+++ trunk/configs/jabber.conf.sample Fri Sep 25 05:54:42 2009
@@ -20,4 +20,7 @@
;; xaway, or dnd
;statusmessage="I am available" ;;Have custom status message for
;;Asterisk.
-;timeout=100 ;;Timeout on the message stack.
+;timeout=5 ;;Timeout (in seconds) on the message stack, defaults to 5.
+ ;;Messages stored longer than this value will be deleted by Asterisk.
+ ;;This option applies to incoming messages only, which are intended to
+ ;;be processed by the JABBER_RECEIVE dialplan function.
Modified: trunk/doc/jabber.txt
URL: http://svnview.digium.com/svn/asterisk/trunk/doc/jabber.txt?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/doc/jabber.txt (original)
+++ trunk/doc/jabber.txt Fri Sep 25 05:54:42 2009
@@ -1,15 +1,107 @@
-(res_jabber is very experimental!)
+XMPP (Jabber) 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), openfire, and many others, as well as several open source
+clients, Psi, gajim, gaim etc. XMPP differs from other IM applications as it
+is immensly extendable. This allows us to easily integrate Asterisk with
+XMPP. The Asterisk XMPP Interface is provided by res_jabber.so.
-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 other IM applications as it
-is immensly extendable. This allows us to easily integrate Asterisk with
-jabber. The Asterisk Jabber Interface is provided by res_jabber.so. 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.
+res_jabber allows for Asterisk to connect to any XMPP (Jabber) server and
+is also used to provide the connection interface for chan_jingle and
+chan_gtalk.
-The maintainer of res_jabber is Matthew O'Gorman <mogorman at digium.com> or
-mog_work on irc or (preferred) mogorman at astjab.org over jabber.
+Functions (JABBER_STATUS, JABBER_RECEIVE) and applications (JabberSend)
+are exposed to the dialplan.
+
+You'll find examples of how to use these functions/applications
+hereafter. We assume that 'asterisk-xmpp' is properly configured in
+jabber.conf.
+
+**** JabberSend ****
+
+JabberSend sends an XMPP message to a buddy. Example :
+
+context default {
+ _XXXX => {
+ JabberSend(asterisk-xmpp,buddy at gmail.com,${CALLERID(name)} is calling ${EXTEN});
+ Dial(SIP/${EXTEN}, 30);
+ Hangup();
+ }
+}
+
+**** JABBER_STATUS ****
+
+Note : as of version 1.6, the corresponding application JabberStatus is still
+available, but marked as deprecated in favor of this function.
+
+JABBER_STATUS stores the status of a buddy in a dialplan variable for
+further use. Here is an AEL example of how to use it :
+
+1234 => {
+ Set(STATUS=${JABBER_STATUS(asterisk-xmpp,buddy at gmail.com)});
+ if (${STATUS}=1) {
+ NoOp(User is online and active, ring his Gtalk client.);
+ Dial(Gtalk/asterisk-xmpp/buddy at gmail.com);
+ } else {
+ NoOp(Prefer the SIP phone);
+ Dial(SIP/1234);
+ }
+}
+
+**** JABBER_RECEIVE ****
+
+JABBER_RECEIVE waits (up to X seconds) for a XMPP message and returns
+its content. Used along with JabberSend (or SendText,
+provided it's implemented in the corresponding channel type),
+JABBER_RECEIVE helps Asterisk interact with users while calls flow
+through the dialplan.
+
+JABBER_RECEIVE/JabberSend are not tied to the XMPP media modules
+chan_gtalk and chan_jingle, and can be used anywhere in the dialplan.
+In the following example, calls targeted to extension 1234 (be it
+accessed from SIP, DAHDI or whatever channel type) are controlled by
+user bob at domain.com. Asterisk notifies him that a call is coming, and
+asks him to take an action. This dialog takes place over an XMPP chat.
+
+context from-ext {
+ 1234 => {
+ Answer();
+ JabberSend(asterisk-xmpp,bob at jabber.org,Call from $CALLERID(num) - choose an option to process the call);
+ JabberSend(asterisk-xmpp,bob at jabber.org,1 : forward to cellphone);
+ JabberSend(asterisk-xmpp,bob at jabber.org,2 : forward to work phone);
+ JabberSend(asterisk-xmpp,bob at jabber.org,Default action : forward to your voicemail);
+ Set(OPTION=${JABBER_RECEIVE(asterisk-xmpp,bob at jabber.org,20)});
+ switch (${OPTION}) {
+ case 1:
+ JabberSend(asterisk-xmpp,bob at jabber.org,(Calling cellphone...);
+ Dial(SIP/987654321);
+ break;
+ case 2:
+ JabberSend(asterisk-xmpp,bob at jabber.org,(Calling workphone...);
+ Dial(SIP/${EXTEN});
+ break;
+ default:
+ Voicemail(${EXTEN}|u)
+ }
+ }
+}
+
+When calling from a GoogleTalk or Jingle client, the CALLERID(name)
+is set to the XMPP id of the caller (i.e. his JID). In the
+following example, Asterisk chats back with the caller identified by the
+caller id. We also take advantage of the SendText implementation in
+chan_gtalk (available in chan_jingle, and chan_sip as well), to
+allow the caller to establish SIP calls from his GoogleTalk client :
+
+context gtalk-in {
+ s => {
+ NoOp(Caller id : ${CALLERID(all)});
+ Answer();
+ SendText(Please enter the number you wish to call);
+ Set(NEWEXTEN=${JABBER_RECEIVE(asterisk-xmpp,${CALLERID(name)})});
+ SendText(Calling ${NEWEXTEN} ...);
+ Dial(SIP/${NEWEXTEN);
+ Hangup();
+ }
+}
+
+The maintainer of res_jabber is Philippe Sultan <philippe.sultan at gmail.com>.
Modified: trunk/include/asterisk/jabber.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/jabber.h?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/include/asterisk/jabber.h (original)
+++ trunk/include/asterisk/jabber.h Fri Sep 25 05:54:42 2009
@@ -117,7 +117,7 @@
char *from;
char *message;
char id[25];
- time_t arrived;
+ struct timeval arrived;
AST_LIST_ENTRY(aji_message) list;
};
Modified: trunk/res/res_jabber.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_jabber.c?view=diff&rev=220457&r1=220456&r2=220457
==============================================================================
--- trunk/res/res_jabber.c (original)
+++ trunk/res/res_jabber.c Fri Sep 25 05:54:42 2009
@@ -33,6 +33,97 @@
/*** MODULEINFO
<depend>iksemel</depend>
<use>openssl</use>
+ ***/
+/*** DOCUMENTATION
+ <application name="JabberSend" language="en_US">
+ <synopsis>
+ Sends an XMPP message to a buddy.
+ </synopsis>
+ <syntax>
+ <parameter name="account" required="true">
+ <para>The local named account to listen on (specified in
+ jabber.conf)</para>
+ </parameter>
+ <parameter name="jid" required="true">
+ <para>Jabber ID of the buddy to send the message to. It can be a
+ bare JID (username at domain) or a full JID (username at domain/resource).</para>
+ </parameter>
+ <parameter name="message" required="true">
+ <para>The message to send.</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Sends the content of <replaceable>message</replaceable> as text message
+ from the given <replaceable>account</replaceable> to the buddy identified by
+ <replaceable>jid</replaceable></para>
+ <para>Example: JabberSend(asterisk,bob at domain.com,Hello world) sends "Hello world"
+ to <replaceable>bob at domain.com</replaceable> as an XMPP message from the account
+ <replaceable>asterisk</replaceable>, configured in jabber.conf.</para>
+ </description>
+ <see-also>
+ <ref type="function">JABBER_STATUS</ref>
+ <ref type="function">JABBER_RECEIVE</ref>
+ </see-also>
+ </application>
+ <function name="JABBER_RECEIVE" language="en_US">
+ <synopsis>
+ Reads XMPP messages.
+ </synopsis>
+ <syntax>
+ <parameter name="account" required="true">
+ <para>The local named account to listen on (specified in
+ jabber.conf)</para>
+ </parameter>
+ <parameter name="jid" required="true">
+ <para>Jabber ID of the buddy to receive message from. It can be a
+ bare JID (username at domain) or a full JID (username at domain/resource).</para>
+ </parameter>
+ <parameter name="timeout">
+ <para>In seconds, defaults to <literal>20</literal>.</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Receives a text message on the given <replaceable>account</replaceable>
+ from the buddy identified by <replaceable>jid</replaceable> and returns the contents.</para>
+ <para>Example: ${JABBER_RECEIVE(asterisk,bob at domain.com)} returns an XMPP message
+ sent from <replaceable>bob at domain.com</replaceable> (or nothing in case of a time out), to
+ the <replaceable>asterisk</replaceable> XMPP account configured in jabber.conf.</para>
+ </description>
+ <see-also>
+ <ref type="function">JABBER_STATUS</ref>
+ <ref type="application">JabberSend</ref>
+ </see-also>
+ </function>
+ <function name="JABBER_STATUS" language="en_US">
+ <synopsis>
+ Retrieves a buddy's status.
+ </synopsis>
+ <syntax>
+ <parameter name="account" required="true">
+ <para>The local named account to listen on (specified in
+ jabber.conf)</para>
+ </parameter>
+ <parameter name="jid" required="true">
+ <para>Jabber ID of the buddy to receive message from. It can be a
+ bare JID (username at domain) or a full JID (username at domain/resource).</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Retrieves the numeric status associated with the buddy identified
+ by <replaceable>jid</replaceable>.
+ If the buddy does not exist in the buddylist, returns 7.</para>
+ <para>Status will be 1-7.</para>
+ <para>1=Online, 2=Chatty, 3=Away, 4=XAway, 5=DND, 6=Offline</para>
+ <para>If not in roster variable will be set to 7.</para>
+ <para>Example: ${JABBER_STATUS(asterisk,bob at domain.com)} returns 1 if
+ <replaceable>bob at domain.com</replaceable> is online. <replaceable>asterisk</replaceable> is
+ the associated XMPP account configured in jabber.conf.</para>
+ </description>
+ <see-also>
+ <ref type="function">JABBER_RECEIVE</ref>
+ <ref type="application">JabberSend</ref>
+ </see-also>
+ </function>
***/
#include "asterisk.h"
@@ -193,6 +284,7 @@
#define JABBER_CONFIG "jabber.conf"
/*-- Forward declarations */
+static void aji_message_destroy(struct aji_message *obj);
static void aji_buddy_destroy(struct aji_buddy *obj);
static void aji_client_destroy(struct aji_client *obj);
static int aji_is_secure(struct aji_client *client);
@@ -255,11 +347,14 @@
static struct aji_client_container clients;
static struct aji_capabilities *capabilities = NULL;
+static ast_cond_t message_received_condition;
+static ast_mutex_t messagelock;
/*! \brief Global flags, initialized to default values */
static struct ast_flags globalflags = { AJI_AUTOREGISTER };
/*!
+ * \internal
* \brief Deletes the aji_client data structure.
* \param obj aji_client The structure we will delete.
* \return void.
@@ -274,16 +369,14 @@
iks_stack_delete(obj->stack);
AST_LIST_LOCK(&obj->messages);
while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) {
- if (tmp->from)
- ast_free(tmp->from);
- if (tmp->message)
- ast_free(tmp->message);
+ aji_message_destroy(tmp);
}
AST_LIST_HEAD_DESTROY(&obj->messages);
ast_free(obj);
}
/*!
+ * \internal
* \brief Deletes the aji_buddy data structure.
* \param obj aji_buddy The structure we will delete.
* \return void.
@@ -302,14 +395,32 @@
}
/*!
+ * \internal
+ * \brief Deletes the aji_message data structure.
+ * \param obj aji_message The structure we will delete.
+ * \return void.
+ */
+static void aji_message_destroy(struct aji_message *obj)
+{
+ if (obj->from) {
+ ast_free(obj->from);
+ }
+ if (obj->message) {
+ ast_free(obj->message);
+ }
+ ast_free(obj);
+}
+
+/*!
+ * \internal
* \brief Find version in XML stream and populate our capabilities list
- * \param node the node attribute in the caps element we'll look for or add to
+ * \param node the node attribute in the caps element we'll look for or add to
* our list
- * \param version the version attribute in the caps element we'll look for or
+ * \param version the version attribute in the caps element we'll look for or
* add to our list
* \param pak struct The XML stanza we're processing
* \return a pointer to the added or found aji_version structure
- */
+ */
static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
{
struct aji_capabilities *list = NULL;
@@ -317,23 +428,23 @@
list = capabilities;
- if(!node)
+ if (!node)
node = pak->from->full;
- if(!version)
+ if (!version)
version = "none supplied.";
while(list) {
- if(!strcasecmp(list->node, node)) {
+ if (!strcasecmp(list->node, node)) {
res = list->versions;
while(res) {
- if(!strcasecmp(res->version, version))
+ if (!strcasecmp(res->version, version))
return res;
res = res->next;
}
- /* Specified version not found. Let's add it to
+ /* Specified version not found. Let's add it to
this node in our capabilities list */
- if(!res) {
+ if (!res) {
res = ast_malloc(sizeof(*res));
- if(!res) {
+ if (!res) {
ast_log(LOG_ERROR, "Out of memory!\n");
return NULL;
}
@@ -348,14 +459,14 @@
list = list->next;
}
/* Specified node not found. Let's add it our capabilities list */
- if(!list) {
+ if (!list) {
list = ast_malloc(sizeof(*list));
- if(!list) {
+ if (!list) {
ast_log(LOG_ERROR, "Out of memory!\n");
return NULL;
}
res = ast_malloc(sizeof(*res));
- if(!res) {
+ if (!res) {
ast_log(LOG_ERROR, "Out of memory!\n");
ast_free(list);
return NULL;
@@ -371,7 +482,9 @@
}
return res;
}
-/*!
+
+/*!
+ * \internal
* \brief Find the aji_resource we want
* \param buddy aji_buddy A buddy
* \param name
@@ -393,6 +506,7 @@
}
/*!
+ * \internal
* \brief Jabber GTalk function
* \param node iks
* \return 1 on success, 0 on failure.
@@ -405,6 +519,7 @@
}
/*!
+ * \internal
* \brief Setup the authentication struct
* \param id iksid
* \param pass password
@@ -433,11 +548,13 @@
}
/*!
+ * \internal
* \brief Dial plan function status(). puts the status of watched user
- into a channel variable.
+ * into a channel variable.
* \param chan ast_channel
* \param data
- * \return 0 on success, -1 on error
+ * \retval 0 success
+ * \retval -1 error
*/
static int aji_status_exec(struct ast_channel *chan, const char *data)
{
@@ -474,6 +591,10 @@
}
AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
+ if (jid.argc < 1 || jid.argc > 2) {
+ ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
+ return -1;
+ }
if (!(client = ast_aji_get_client(args.sender))) {
ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
@@ -496,6 +617,15 @@
return 0;
}
+/*!
+ * \internal
+ * \brief Dial plan funtcion to retrieve the status of a buddy.
+ * \param channel The associated ast_channel, if there is one
+ * \param data The account, buddy JID, and optional timeout
+ * timeout.
+ * \retval 0 success
+ * \retval -1 failure
+ */
static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
{
struct aji_client *client = NULL;
@@ -523,6 +653,10 @@
}
AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
+ if (jid.argc < 1 || jid.argc > 2) {
+ ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
+ return -1;
+ }
if (!(client = ast_aji_get_client(args.sender))) {
ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
@@ -550,10 +684,237 @@
};
/*!
+ * \internal
+ * \brief Dial plan function to receive a message.
+ * \param channel The associated ast_channel, if there is one
+ * \param data The account, JID, and optional timeout
+ * timeout.
+ * \retval 0 success
+ * \retval -1 failure
+ */
+static int acf_jabberreceive_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
+{
+ char *aux = NULL, *parse = NULL;
+ int timeout;
+ int jidlen, resourcelen;
+ struct timeval start;
+ long diff = 0;
+ struct aji_client *client = NULL;
+ int found = 0;
+ struct aji_message *tmp = NULL;
+ AST_DECLARE_APP_ARGS(args,
+ AST_APP_ARG(account);
+ AST_APP_ARG(jid);
+ AST_APP_ARG(timeout);
+ );
+ AST_DECLARE_APP_ARGS(jid,
+ AST_APP_ARG(screenname);
+ AST_APP_ARG(resource);
+ );
+
+ if (ast_strlen_zero(data)) {
+ ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
+ return -1;
+ }
+
+ parse = ast_strdupa(data);
+ AST_STANDARD_APP_ARGS(args, parse);
+
+ if (args.argc < 2 || args.argc > 3) {
+ ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
+ return -1;
+ }
+
+ client = ast_aji_get_client(args.account);
+ if (!client) {
+ ast_log(LOG_WARNING, "Could not find client %s, exiting\n", args.account);
+ return -1;
+ }
+
+ parse = ast_strdupa(args.jid);
+ AST_NONSTANDARD_APP_ARGS(jid, parse, '/');
+ if (jid.argc < 1 || jid.argc > 2 || strlen(args.jid) > AJI_MAX_JIDLEN) {
+ ast_log(LOG_WARNING, "Invalid JID : %s\n", parse);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return -1;
+ }
+
+ if (ast_strlen_zero(args.timeout)) {
+ timeout = 20;
+ } else {
+ sscanf(args.timeout, "%d", &timeout);
+ if (timeout <= 0) {
+ ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", args.timeout);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return -1;
+ }
+ }
+
+ jidlen = strlen(jid.screenname);
+ resourcelen = ast_strlen_zero(jid.resource) ? 0 : strlen(jid.resource);
+
+ ast_debug(3, "Waiting for an XMPP message from %s\n", args.jid);
+
+ start = ast_tvnow();
+
+ if (ast_autoservice_start(chan) < 0) {
+ ast_log(LOG_WARNING, "Cannot start autoservice for channel %s\n", chan->name);
+ return -1;
+ }
+
+ /* search the messages list, grab the first message that matches with
+ * the from JID we're expecting, and remove it from the messages list */
+ while (diff < timeout) {
+ struct timespec ts = { 0, };
+ struct timeval wait;
+ int res;
+
+ wait = ast_tvadd(start, ast_tv(timeout, 0));
+ ts.tv_sec = wait.tv_sec;
+ ts.tv_nsec = wait.tv_usec * 1000;
+
+ /* wait up to timeout seconds for an incoming message */
+ ast_mutex_lock(&messagelock);
+ res = ast_cond_timedwait(&message_received_condition, &messagelock, &ts);
+ ast_mutex_unlock(&messagelock);
+ if (res == ETIMEDOUT) {
+ ast_debug(3, "No message received from %s in %d seconds\n", args.jid, timeout);
+ break;
+ };
+
+ AST_LIST_LOCK(&client->messages);
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
+ if (jid.argc == 1) {
+ /* no resource provided, compare bare JIDs */
+ if (strncasecmp(jid.screenname, tmp->from, jidlen)) {
+ continue;
+ }
+ } else {
+ /* resource appended, compare bare JIDs and resources */
+ char *resource = strchr(tmp->from, '/');
+ if (!resource || strlen(resource) == 0) {
+ ast_log(LOG_WARNING, "Remote JID has no resource : %s\n", tmp->from);
+ if (strncasecmp(jid.screenname, tmp->from, jidlen)) {
+ continue;
+ }
+ } else {
+ resource ++;
+ if (strncasecmp(jid.screenname, tmp->from, jidlen) || strncmp(jid.resource, resource, resourcelen)) {
+ continue;
+ }
+ }
+ }
+ /* check if the message is not too old */
+ if (ast_tvdiff_sec(ast_tvnow(), tmp->arrived) >= client->message_timeout) {
+ ast_debug(3, "Found old message from %s, deleting it\n", tmp->from);
+ AST_LIST_REMOVE_CURRENT(list);
+ aji_message_destroy(tmp);
+ continue;
+ }
+ found = 1;
+ aux = ast_strdupa(tmp->message);
+ AST_LIST_REMOVE_CURRENT(list);
+ aji_message_destroy(tmp);
+ break;
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+ AST_LIST_UNLOCK(&client->messages);
+ if (found) {
+ break;
+ }
+
+ /* check timeout */
+ diff = ast_tvdiff_ms(ast_tvnow(), start);
+ }
+
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ if (ast_autoservice_stop(chan) < 0) {
+ ast_log(LOG_WARNING, "Cannot stop autoservice for channel %s\n", chan->name);
+ }
+
+ /* return if we timed out */
+ if (!found) {
+ ast_log(LOG_NOTICE, "Timed out : no message received from %s\n", args.jid);
+ return -1;
+ }
+ ast_copy_string(buf, aux, buflen);
+
+ return 0;
+}
+
+static struct ast_custom_function jabberreceive_function = {
+ .name = "JABBER_RECEIVE",
+ .read = acf_jabberreceive_read,
+};
+
+/*!
+ * \internal
+ * \brief Delete old messages from a given JID
+ * Messages stored during more than client->message_timeout are deleted
+ * \param client Asterisk's XMPP client
+ * \param from the JID we received messages from
+ * \retval the number of deleted messages
+ * \retval -1 failure
+ */
+static int delete_old_messages(struct aji_client *client, char *from)
+{
+ int deleted = 0;
+ int isold = 0;
+ struct aji_message *tmp = NULL;
+ if (!client) {
+ ast_log(LOG_ERROR, "Cannot find our XMPP client\n");
+ return -1;
+ }
+
+ /* remove old messages */
+ AST_LIST_LOCK(&client->messages);
+ if (AST_LIST_EMPTY(&client->messages)) {
+ AST_LIST_UNLOCK(&client->messages);
+ return 0;
+ }
+
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
+ if (isold) {
+ if (!from || !strncasecmp(from, tmp->from, strlen(from))) {
+ AST_LIST_REMOVE_CURRENT(list);
+ aji_message_destroy(tmp);
+ deleted ++;
+ }
+ } else if (ast_tvdiff_sec(ast_tvnow(), tmp->arrived) >= client->message_timeout) {
+ isold = 1;
+ if (!from || !strncasecmp(from, tmp->from, strlen(from))) {
+ AST_LIST_REMOVE_CURRENT(list);
+ aji_message_destroy(tmp);
+ deleted ++;
+ }
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+ AST_LIST_UNLOCK(&client->messages);
+
+ return deleted;
+}
+
+/*!
+ * \internal
+ * \brief Delete old messages
+ * Messages stored during more than client->message_timeout are deleted
+ * \param client Asterisk's XMPP client
+ * \retval the number of deleted messages
+ * \retval -1 failure
+ */
+static int delete_old_messages_all(struct aji_client *client)
+{
+ return delete_old_messages(client, NULL);
+}
+
+/*!
+ * \internal
* \brief Dial plan function to send a message.
* \param chan ast_channel
- * \param data Data is sender|receiver|message.
- * \return 0 on success,-1 on error.
+ * \param data Data is account,jid,message.
+ * \retval 0 success
+ * \retval -1 failure
*/
static int aji_send_exec(struct ast_channel *chan, const char *data)
{
@@ -566,14 +927,14 @@
);
if (!data) {
- ast_log(LOG_ERROR, "Usage: JabberSend(<sender>,<recipient>,<message>)\n");
- return 0;
+ ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
+ return -1;
}
s = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, s);
if (args.argc < 3) {
- ast_log(LOG_ERROR, "JabberSend requires 3 arguments: '%s'\n", data);
+ ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
return -1;
}
@@ -581,12 +942,14 @@
ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
return -1;
}
- if (strchr(args.recipient, '@') && !ast_strlen_zero(args.message))
+ if (strchr(args.recipient, '@') && !ast_strlen_zero(args.message)) {
ast_aji_send_chat(client, args.recipient, args.message);
+ }
return 0;
}
-/*!
+/*!
+ * \internal
* \brief Tests whether the connection is secured or not
* \return 0 if the connection is not secured
*/
@@ -601,6 +964,7 @@
#ifdef HAVE_OPENSSL
/*!
+ * \internal
* \brief Starts the TLS procedure
* \param client the configured XMPP client we use to connect to a XMPP server
* \return IKS_OK on success, an error code if sending failed, IKS_NET_TLSFAIL
@@ -620,6 +984,7 @@
}
/*!
+ * \internal
* \brief TLS handshake, OpenSSL initialization
* \param client the configured XMPP client we use to connect to a XMPP server
* \return IKS_OK on success, IKS_NET_TLSFAIL on failure
@@ -668,13 +1033,15 @@
#endif /* HAVE_OPENSSL */
/*!
+ * \internal
* \brief Secured or unsecured IO socket receiving function
* \param client the configured XMPP client we use to connect to a XMPP server
* \param buffer the reception buffer
* \param buf_len the size of the buffer
* \param timeout the select timer
- * \return the number of read bytes on success, 0 on timeout expiration,
- * -1 on error
+ * \retval the number of read bytes
+ * \retval 0 timeout expiration
+ * \retval -1 error
*/
static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout)
{
@@ -720,13 +1087,16 @@
}
/*!
+ * \internal
* \brief Tries to receive data from the Jabber server
* \param client the configured XMPP client we use to connect to a XMPP server
* \param timeout the timeout value
* This function receives (encrypted or unencrypted) data from the XMPP server,
* and passes it to the parser.
- * \return IKS_OK on success, IKS_NET_RWERR on IO error, IKS_NET_NOCONN, if no
- * connection available, IKS_NET_EXPIRED on timeout expiration
+ * \retval IKS_OK success
+ * \retval IKS_NET_RWERR IO error
+ * \retval IKS_NET_NOCONN no connection available
+ * \retval IKS_NET_EXPIRED timeout expiration
*/
static int aji_recv (struct aji_client *client, int timeout)
{
@@ -794,6 +1164,7 @@
}
/*!
+ * \internal
* \brief Sends XMPP header to the server
* \param client the configured XMPP client we use to connect to a XMPP server
* \param to the target XMPP server
@@ -831,6 +1202,7 @@
}
/*!
+ * \internal
* \brief Sends an XML string over an XMPP connection
* \param client the configured XMPP client we use to connect to a XMPP server
* \param xmlstr the XML string to send
@@ -864,6 +1236,7 @@
}
/*!
+ * \internal
* \brief the debug loop.
* \param data void
* \param xmpp xml data as string
@@ -881,8 +1254,8 @@
if (is_incoming)
ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
else {
- if( strlen(xmpp) == 1) {
- if(option_debug > 2 && xmpp[0] == ' ') {
+ if (strlen(xmpp) == 1) {
+ if (option_debug > 2 && xmpp[0] == ' ') {
ast_verbose("\nJABBER: Keep alive packet\n");
}
} else
@@ -894,6 +1267,7 @@
}
/*!
+ * \internal
* \brief A wrapper function for iks_start_sasl
* \param client the configured XMPP client we use to connect to a XMPP server
* \param type the SASL authentication type. Supported types are PLAIN and MD5
@@ -945,6 +1319,7 @@
}
/*!
+ * \internal
* \brief The action hook parses the inbound packets, constantly running.
* \param data aji client structure
* \param type type of packet
@@ -958,7 +1333,7 @@
iks *auth = NULL;
int features = 0;
- if(!node) {
+ if (!node) {
ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */
ASTOBJ_UNREF(client, aji_client_destroy);
return IKS_HOOK;
@@ -1089,7 +1464,7 @@
handshake = NULL;
}
client->state = AJI_CONNECTING;
- if(aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
+ if (aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
client->state = AJI_CONNECTED;
else
ast_log(LOG_WARNING, "Jabber didn't seem to handshake, failed to authenticate.\n");
@@ -1146,6 +1521,7 @@
return IKS_OK;
}
/*!
+ * \internal
* \brief Unknown
* \param data void
* \param pak ikspak
@@ -1189,6 +1565,7 @@
return IKS_FILTER_EAT;
}
/*!
+ * \internal
* \brief register handler for incoming querys (IQ's)
* \param data incoming aji_client request
* \param pak ikspak
@@ -1212,7 +1589,7 @@
query = iks_new("query");
error = iks_new("error");
notacceptable = iks_new("not-acceptable");
- if(iq && query && error && notacceptable) {
+ if (iq && query && error && notacceptable) {
iks_insert_attrib(iq, "type", "error");
iks_insert_attrib(iq, "from", client->user);
iks_insert_attrib(iq, "to", pak->from->full);
@@ -1260,6 +1637,7 @@
}
/*!
+ * \internal
* \brief Handles stuff
* \param data void
* \param pak ikspak
@@ -1354,7 +1732,9 @@
return IKS_FILTER_EAT;
}
-/*!
+
+/*!
+ * \internal
* \brief Handle add extra info
* \param data void
* \param pak ikspak
@@ -1414,7 +1794,9 @@
ASTOBJ_UNREF(client, aji_client_destroy);
return IKS_FILTER_EAT;
}
-/*!
+
+/*!
+ * \internal
* \brief Handler of the return info packet
* \param data aji_client
* \param pak ikspak
@@ -1556,9 +1938,10 @@
}
/*!
- * \brief Handles \verbatim <iq> \endverbatim tags.
+ * \internal
+ * \brief Handles \verbatim <iq> \endverbatim stanzas.
* \param client the configured XMPP client we use to connect to a XMPP server
- * \param node iks
+ * \param node iks
* \return void.
*/
static void aji_handle_iq(struct aji_client *client, iks *node)
@@ -1567,58 +1950,69 @@
}
/*!
- * \brief Handles presence packets.
+ * \internal
+ * \brief Handles \verbatim <message>\endverbatim stanzas.
+ * Adds the incoming message to the client's message list.
* \param client the configured XMPP client we use to connect to a XMPP server
* \param pak ikspak the node
*/
static void aji_handle_message(struct aji_client *client, ikspak *pak)
{
- struct aji_message *insert, *tmp;
- int flag = 0;
-
- if (!(insert = ast_calloc(1, sizeof(*insert))))
+ struct aji_message *insert;
+ int deleted = 0;
+
+ ast_debug(3, "client %s received a message\n", client->name);
+
+ if (!(insert = ast_calloc(1, sizeof(*insert)))) {
return;
- time(&insert->arrived);
- if (iks_find_cdata(pak->x, "body"))
+ }
+
+ insert->arrived = ast_tvnow();
+
+ /* wake up threads waiting for messages */
+ ast_mutex_lock(&messagelock);
+ ast_cond_broadcast(&message_received_condition);
+ ast_mutex_unlock(&messagelock);
+
+ if (iks_find_cdata(pak->x, "body")) {
insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
- if (pak->id)
- ast_copy_string(insert->id, pak->id, sizeof(insert->message));
- if (pak->from)
+ }
+ if (pak->id) {
+ ast_copy_string(insert->id, pak->id, sizeof(insert->id));
+ }
+ if (pak->from){
+ /* insert will furtherly be added to message list */
insert->from = ast_strdup(pak->from->full);
+ if (!insert->from) {
+ ast_log(LOG_ERROR, "Memory allocation failure\n");
+ return;
+ }
+ ast_debug(3, "message comes from %s\n", insert->from);
+ }
+
+ /* remove old messages received from this JID
+ * and insert received message */
+ deleted = delete_old_messages(client, pak->from->partial);
+ ast_debug(3, "Deleted %d messages for client %s from JID %s\n", deleted, client->name, pak->from->partial);
AST_LIST_LOCK(&client->messages);
- AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
- if (flag) {
- AST_LIST_REMOVE_CURRENT(list);
- if (tmp->from)
- ast_free(tmp->from);
- if (tmp->message)
- ast_free(tmp->message);
- } else if (difftime(time(NULL), tmp->arrived) >= client->message_timeout) {
- flag = 1;
- AST_LIST_REMOVE_CURRENT(list);
- if (tmp->from)
- ast_free(tmp->from);
- if (tmp->message)
- ast_free(tmp->message);
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_INSERT_HEAD(&client->messages, insert, list);
AST_LIST_UNLOCK(&client->messages);
}
-/*!
- * \brief Check the presence info
+
+/*!
+ * \internal
+ * \brief handles \verbatim <presence>\endverbatim stanzas.
* \param client the configured XMPP client we use to connect to a XMPP server
* \param pak ikspak
-*/
+ */
static void aji_handle_presence(struct aji_client *client, ikspak *pak)
{
int status, priority;
struct aji_buddy *buddy;
struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
char *ver, *node, *descrip, *type;
-
- if(client->state != AJI_CONNECTED)
+
+ if (client->state != AJI_CONNECTED)
aji_create_buddy(pak->from->partial, client);
buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
@@ -1631,7 +2025,7 @@
return;
}
type = iks_find_attrib(pak->x, "type");
- if(client->component && type &&!strcasecmp("probe", type)) {
+ if (client->component && type &&!strcasecmp("probe", type)) {
aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
ast_verbose("what i was looking for \n");
}
@@ -1761,18 +2155,18 @@
}
/* retrieve capabilites of the new resource */
- if(status !=6 && found && !found->cap) {
+ if (status != 6 && found && !found->cap) {
found->cap = aji_find_version(node, ver, pak);
- if(gtalk_yuck(pak->x)) /* gtalk should do discover */
+ if (gtalk_yuck(pak->x)) /* gtalk should do discover */
found->cap->jingle = 1;
- if(found->cap->jingle && option_debug > 4) {
+ if (found->cap->jingle && option_debug > 4) {
ast_debug(1,"Special case for google till they support discover.\n");
}
else {
iks *iq, *query;
iq = iks_new("iq");
query = iks_new("query");
- if(query && iq) {
+ if (query && iq) {
iks_insert_attrib(iq, "type", "get");
iks_insert_attrib(iq, "to", pak->from->full);
iks_insert_attrib(iq,"from", client->jid->full);
@@ -1824,6 +2218,7 @@
}
/*!
+ * \internal
* \brief handles subscription requests.
* \param client the configured XMPP client we use to connect to a XMPP server
* \param pak ikspak iksemel packet.
@@ -1872,25 +2267,30 @@
* \param client the configured XMPP client we use to connect to a XMPP server
* \param address
* \param message
- * \return 1.
[... 412 lines stripped ...]
More information about the asterisk-commits
mailing list