[asterisk-commits] phsultan: branch phsultan/jabberreceive r166743 - in /team/phsultan/jabberrec...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Dec 24 09:21:38 CST 2008
Author: phsultan
Date: Wed Dec 24 09:21:38 2008
New Revision: 166743
URL: http://svn.digium.com/view/asterisk?view=rev&rev=166743
Log:
Modifications to adress Russell's review.
- updated the CHANGES file ;
- fixed several memory/reference leaks ;
- fixed spacing and documentation formatting ;
- changed the message_timeout value from 100 to 2 seconds.
XMPP messages sent to Asterisk are kept in a structure not to loose them.
Here is how messages are processed :
1) if we don't receive any XMPP data for 2 seconds, all stored messages are
deleted.
2) Upon XMPP message reception from a given JID, all messages stored more
than 2 seconds from this JID are deleted, and the new message is stored.
3) acf_jabberreceive_read gets the first (most recent) message from the given
JID, and returns its content to the dialplan.
(issue #12569)
Modified:
team/phsultan/jabberreceive/CHANGES
team/phsultan/jabberreceive/doc/jabber.txt
team/phsultan/jabberreceive/res/res_jabber.c
Modified: team/phsultan/jabberreceive/CHANGES
URL: http://svn.digium.com/view/asterisk/team/phsultan/jabberreceive/CHANGES?view=diff&rev=166743&r1=166742&r2=166743
==============================================================================
--- team/phsultan/jabberreceive/CHANGES (original)
+++ team/phsultan/jabberreceive/CHANGES Wed Dec 24 09:21:38 2008
@@ -73,6 +73,8 @@
Totally. Hopefully, that means more to you than it does to me.
* Added AUDIOHOOK_INHERIT. For information on its use, please see the output
of "core show function AUDIOHOOK_INHERIT" from the CLI
+ * Added JABBER_RECEIVE, which permits receiving XMPP messages from the
+ dialplan. This function returns the content of the received message.
Applications
------------
@@ -112,6 +114,8 @@
timezones, especially if those daylight savings time ranges vary from your
machine's native timezone. See GotoIfTime, ExecIfTime, IFTIME(), and timed
includes.
+ * SendText is now implemented in chan_gtalk and chan_jingle. It will simply send
+ XMPP text messages to the remote JID.
Asterisk Manager Interface
--------------------------
Modified: team/phsultan/jabberreceive/doc/jabber.txt
URL: http://svn.digium.com/view/asterisk/team/phsultan/jabberreceive/doc/jabber.txt?view=diff&rev=166743&r1=166742&r2=166743
==============================================================================
--- team/phsultan/jabberreceive/doc/jabber.txt (original)
+++ team/phsultan/jabberreceive/doc/jabber.txt Wed Dec 24 09:21:38 2008
@@ -9,7 +9,7 @@
is also used to provide the connection interface for chan_jingle and
chan_gtalk.
-Functions (JABBER_STATUS, JABBER_RECEIVE) and applications (JabberSend)
+Functions (JABBER_STATUS, JABBER_RECEIVE) and applications (JabberSend)
are exposed to the dialplan.
You'll find examples of how to use these functions/applications
Modified: team/phsultan/jabberreceive/res/res_jabber.c
URL: http://svn.digium.com/view/asterisk/team/phsultan/jabberreceive/res/res_jabber.c?view=diff&rev=166743&r1=166742&r2=166743
==============================================================================
--- team/phsultan/jabberreceive/res/res_jabber.c (original)
+++ team/phsultan/jabberreceive/res/res_jabber.c Wed Dec 24 09:21:38 2008
@@ -96,7 +96,7 @@
</function>
<function name="JABBER_STATUS" language="en_US">
<synopsis>
- Retrieves a buddy's status.
+ Retrieves a buddy's status.
</synopsis>
<syntax>
<parameter name="account" required="true">
@@ -113,7 +113,7 @@
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>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
@@ -165,7 +165,9 @@
/*-- Forward declarations */
static void aji_buddy_destroy(struct aji_buddy *obj);
static void aji_client_destroy(struct aji_client *obj);
-static int delete_old_messages(struct aji_client *client);
+static void aji_message_destroy(struct aji_message *obj);
+static int delete_old_messages(struct aji_client *client, char *from);
+static int delete_old_messages_all(struct aji_client *client);
static int aji_send_exec(struct ast_channel *chan, void *data);
static int aji_status_exec(struct ast_channel *chan, void *data);
static int aji_is_secure(struct aji_client *client);
@@ -258,10 +260,7 @@
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);
@@ -282,6 +281,22 @@
ast_free(tmp);
}
+ ast_free(obj);
+}
+
+/*!
+ * \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);
}
@@ -458,6 +473,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);
@@ -507,6 +526,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);
@@ -535,9 +558,11 @@
/*!
* \brief Dial plan function to receive a message.
- * \param channel, and data, data is account, JID and optional
+ * \param channel The associated ast_channel, if there is one
+ * \param data The account, JID, and optional timeout
* timeout.
- * \return 0 on success, -1 on failure.
+ * \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)
{
@@ -547,12 +572,20 @@
struct aji_client *client = NULL;
int found = 0;
struct aji_message *tmp = NULL;
- iksid *id = 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_autoservice_start(chan) < 0) {
+ ast_log(LOG_WARNING, "Cannot start autoservice for channel %s\n", chan->name);
+ return -1;
+ }
time(&triggered);
if (ast_strlen_zero(data)) {
@@ -574,18 +607,21 @@
return -1;
}
- id = iks_id_new(client->stack, args.jid);
- if (!id) {
- ast_log(LOG_WARNING, "Could not build a valid Jabber Identifier from %s, exiting\n", args.jid);
+ parse = ast_strdupa(args.jid);
+ AST_NONSTANDARD_APP_ARGS(jid, parse, '/');
+ if (jid.argc < 1 || jid.argc > 2 || strlen(args.jid) > AJI_MAX_JIDLEN || strlen(jid.resource) > AJI_MAX_RESJIDLEN) {
+ 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 {
- timeout = atoi(args.timeout);
+ 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;
}
}
@@ -597,29 +633,35 @@
while (difftime(time(NULL), triggered) < timeout) {
AST_LIST_LOCK(&client->messages);
AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
- iksid *from = iks_id_new(client->stack, tmp->from);
- if (!from) {
- ast_log(LOG_WARNING, "Could not build a valid Jabber Identifier from %s, exiting\n", tmp->from);
- return -1;
- }
- if (strchr(args.jid, '/')) {
- if (iks_id_cmp(from, id, IKS_ID_RESOURCE | IKS_ID_USER | IKS_ID_SERVER)) {
+ if (jid.argc == 1) {
+ /* no resource provided, compare bare JIDs */
+ if (strncasecmp(jid.screenname, tmp->from, strlen(jid.screenname))) {
continue;
}
} else {
- if (iks_id_cmp(from, id, IKS_ID_USER | IKS_ID_SERVER)) {
+ /* 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);
+ ASTOBJ_UNREF(client, aji_client_destroy);
+ return -1;
+ }
+ resource ++;
+ if (strncasecmp(jid.screenname, tmp->from, strlen(jid.screenname)) || strncmp(jid.resource, resource, strlen(jid.resource))) {
continue;
}
+ }
+ /* check if the message is not too old */
+ if (difftime(time(NULL), 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);
- if (tmp->from) {
- ast_free(tmp->from);
- }
- if (tmp->message) {
- ast_free(tmp->message);
- }
+ aji_message_destroy(tmp);
break;
}
AST_LIST_TRAVERSE_SAFE_END;
@@ -627,8 +669,14 @@
if (found) {
break;
}
- ast_safe_sleep(chan, 500);
- }
+ usleep(500000);
+ }
+
+ 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);
@@ -644,21 +692,24 @@
};
/*!
- * \brief Delete old messages
+ * \brief Delete old messages from a given JID
* Messages stored during more than client->message_timeout are deleted
* \param client Asterisk's XMPP client
- * \return the number of deleted messages, or -1 in case of failure
- */
-static int delete_old_messages(struct aji_client *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 flag = 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 */
+
+ /* remove old messages */
AST_LIST_LOCK(&client->messages);
if (AST_LIST_EMPTY(&client->messages)) {
AST_LIST_UNLOCK(&client->messages);
@@ -666,21 +717,19 @@
}
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);
- deleted ++;
+ if (isold) {
+ if (!from || !strncasecmp(from, tmp->from, strlen(from))) {
+ AST_LIST_REMOVE_CURRENT(list);
+ aji_message_destroy(tmp);
+ deleted ++;
+ }
} 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);
- deleted ++;
+ 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;
@@ -690,10 +739,23 @@
}
/*!
+ * \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);
+}
+
+/*!
* \brief Dial plan function to send a message.
* \param chan ast_channel
- * \param data Data is sender|reciever|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, void *data)
{
@@ -818,8 +880,9 @@
* \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)
{
@@ -870,8 +933,10 @@
* \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)
{
@@ -1715,7 +1780,6 @@
ast_debug(3, "client %s received a message\n", client->name);
if (!(insert = ast_calloc(1, sizeof(*insert)))) {
- ast_log(LOG_ERROR, "Out of memory.\n");
return;
}
@@ -1729,12 +1793,17 @@
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, and insert received message */
- deleted = delete_old_messages(client);
- ast_debug(3, "Deleted %d messages for client %s\n", deleted, client->name);
+ /* 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_INSERT_HEAD(&client->messages, insert, list);
AST_LIST_UNLOCK(&client->messages);
@@ -2152,22 +2221,26 @@
pthread_exit(NULL);
}
- /* Decrease timeout if no data received */
- if (res == IKS_NET_EXPIRED)
+ /* Decrease timeout if no data received, and delete
+ * old messages globally */
+ if (res == IKS_NET_EXPIRED) {
client->timeout--;
-
- if (res == IKS_HOOK)
+ delete_old_messages_all(client);
+ }
+ if (res == IKS_HOOK) {
ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
- else if (res == IKS_NET_TLSFAIL)
+ } else if (res == IKS_NET_TLSFAIL) {
ast_log(LOG_ERROR, "JABBER: Failure in TLS.\n");
- else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
+ } else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
res = client->keepalive ? aji_send_raw(client, " ") : IKS_OK;
- if(res == IKS_OK)
+ if(res == IKS_OK) {
client->timeout = 50;
- else
+ } else {
ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
- } else if (res == IKS_NET_RWERR)
+ }
+ } else if (res == IKS_NET_RWERR) {
ast_log(LOG_WARNING, "JABBER: socket read error\n");
+ }
} while (client);
ASTOBJ_UNREF(client, aji_client_destroy);
return 0;
@@ -3210,7 +3283,7 @@
ast_manager_unregister("JabberSend");
ast_custom_function_unregister(&jabberstatus_function);
ast_custom_function_unregister(&jabberreceive_function);
-
+
ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
ASTOBJ_RDLOCK(iterator);
ast_debug(3, "JABBER: Releasing and disconnecting client: %s\n", iterator->name);
More information about the asterisk-commits
mailing list