[Asterisk-code-review] res xmpp: Correct implementation of JABBER STATUS & JabberSt... (asterisk[13])

Sean Bright asteriskteam at digium.com
Thu Mar 23 09:54:41 CDT 2017


Sean Bright has uploaded a new change for review. ( https://gerrit.asterisk.org/5286 )

Change subject: res_xmpp: Correct implementation of JABBER_STATUS & JabberStatus
......................................................................

res_xmpp: Correct implementation of JABBER_STATUS & JabberStatus

The documentation for JABBER_STATUS (and the deprecated JabberStatus
app) indicate that a return value of 7 indicates that the specified
buddy was not in the roster. It also indicates that you can specify a
"bare" JID (one without a resource). Unfortunately the actual behavior
does not match the documented behavior.

Assuming that our roster includes the buddy online and available
"valid at example.org/Valid" and does *not* include the buddy
"invalid at example.org", the JABBER_STATUS() function returns the
following before this patch:

+------------------------------+------------+--------------------------+
| Buddy                        | Status     | Result                   |
+------------------------------+------------+--------------------------+
| valid at example.org            |  Online    |  7 (Not in roster)       |
| valid at example.org/Valid      |  Online    |  1 (Online)              |
| valid at example.org/Invalid    |  N/A       |  7 (Not in roster)       |
| invalid at example.org          |  N/A       |  Error logged, no return |
| invalid at example.org/Valid    |  N/A       |  Error logged, no return |
+------------------------------+------------+--------------------------+

And after this patch:

+------------------------------+------------+--------------------------+
| Buddy                        | Status     | Result                   |
+------------------------------+------------+--------------------------+
| valid at example.org            |  Online    |  1 (Online)              |
| valid at example.org/Valid      |  Online    |  1 (Online)              |
| valid at example.org/Invalid    |  N/A       |  6 (Offline)             |
| invalid at example.org          |  N/A       |  7 (Not in roster)       |
| invalid at example.org/Valid    |  N/A       |  7 (Not in roster)       |
+------------------------------+------------+--------------------------+

This brings the behavior in line with the documentation.

ASTERISK-23510 #close
Reported by: Anthony Critelli

Change-Id: I9c3241035363ef4a6bdc21fabfd8ffcd9ec657bf
---
M res/res_xmpp.c
1 file changed, 28 insertions(+), 44 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/86/5286/1

diff --git a/res/res_xmpp.c b/res/res_xmpp.c
index 42b3599..5d7fe66 100644
--- a/res/res_xmpp.c
+++ b/res/res_xmpp.c
@@ -1632,6 +1632,32 @@
 	return CMP_MATCH | CMP_STOP;
 }
 
+static int get_buddy_status(struct ast_xmpp_client_config *clientcfg, char *screenname, char *resource)
+{
+	int status = 7;
+	struct ast_xmpp_buddy *buddy = ao2_find(clientcfg->client->buddies, screenname, OBJ_KEY);
+
+	if (buddy) {
+		struct ast_xmpp_resource *res = ao2_callback(
+			buddy->resources,
+			0,
+			ast_strlen_zero(resource) ? xmpp_resource_immediate : xmpp_resource_cmp,
+			resource);
+
+		/* We at least have a buddy */
+		status = 6;
+
+		if (res) {
+			status = res->status;
+			ao2_ref(res, -1);
+		}
+
+		ao2_ref(buddy, -1);
+	}
+
+	return status;
+}
+
 /*
  * \internal
  * \brief Dial plan function status(). puts the status of watched user
@@ -1645,10 +1671,7 @@
 {
 	RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
 	RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
-	struct ast_xmpp_buddy *buddy;
-	struct ast_xmpp_resource *resource;
 	char *s = NULL, status[2];
-	int stat = 7;
 	static int deprecation_warning = 0;
 	AST_DECLARE_APP_ARGS(args,
 			     AST_APP_ARG(sender);
@@ -1687,25 +1710,7 @@
 		return -1;
 	}
 
-	if (!(buddy = ao2_find(clientcfg->client->buddies, jid.screenname, OBJ_KEY))) {
-		ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
-		return -1;
-	}
-
-	if (ast_strlen_zero(jid.resource) || !(resource = ao2_callback(buddy->resources, 0, xmpp_resource_cmp, jid.resource))) {
-		resource = ao2_callback(buddy->resources, OBJ_NODATA, xmpp_resource_immediate, NULL);
-	}
-
-	ao2_ref(buddy, -1);
-
-	if (resource) {
-		stat = resource->status;
-		ao2_ref(resource, -1);
-	} else {
-		ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
-	}
-
-	snprintf(status, sizeof(status), "%d", stat);
+	snprintf(status, sizeof(status), "%d", get_buddy_status(clientcfg, jid.screenname, jid.resource));
 	pbx_builtin_setvar_helper(chan, args.variable, status);
 
 	return 0;
@@ -1724,9 +1729,6 @@
 {
 	RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
 	RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
-	struct ast_xmpp_buddy *buddy;
-	struct ast_xmpp_resource *resource;
-	int stat = 7;
 	AST_DECLARE_APP_ARGS(args,
 			     AST_APP_ARG(sender);
 			     AST_APP_ARG(jid);
@@ -1758,25 +1760,7 @@
 		return -1;
 	}
 
-	if (!(buddy = ao2_find(clientcfg->client->buddies, jid.screenname, OBJ_KEY))) {
-		ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
-		return -1;
-	}
-
-	if (ast_strlen_zero(jid.resource) || !(resource = ao2_callback(buddy->resources, 0, xmpp_resource_cmp, jid.resource))) {
-		resource = ao2_callback(buddy->resources, OBJ_NODATA, xmpp_resource_immediate, NULL);
-	}
-
-	ao2_ref(buddy, -1);
-
-	if (resource) {
-		stat = resource->status;
-		ao2_ref(resource, -1);
-	} else {
-		ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
-	}
-
-	snprintf(buf, buflen, "%d", stat);
+	snprintf(buf, buflen, "%d", get_buddy_status(clientcfg, jid.screenname, jid.resource));
 
 	return 0;
 }

-- 
To view, visit https://gerrit.asterisk.org/5286
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9c3241035363ef4a6bdc21fabfd8ffcd9ec657bf
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>



More information about the asterisk-code-review mailing list