From e2cfbcafd712f1a01c461ce037d7c03005999a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=ADaz?= Date: Mon, 15 Jun 2015 13:05:39 -0500 Subject: [PATCH] xmpp: Get Jabber status for bare JID. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code works fantastically well when a full Jabber ID (user@domain/resource) is provided. In all other cases, a bare JID gives the wrong result because this function would not look up the highest priority resource for the given user. Signed-off-by: Daniel Díaz --- res/res_xmpp.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/res/res_xmpp.c b/res/res_xmpp.c index b5d0e8a..961acc6 100644 --- a/res/res_xmpp.c +++ b/res/res_xmpp.c @@ -1487,6 +1487,30 @@ static int xmpp_resource_immediate(void *obj, void *arg, int flags) return CMP_MATCH | CMP_STOP; } +static int xmpp_resource_prio_cmp(void *obj, void *arg, int flags) +{ + struct ast_xmpp_resource *resource1 = obj; + int *prio = arg; + + if (resource1->priority == *prio) { + return (CMP_MATCH | CMP_STOP); + } + + return 0; +} + +static int xmpp_resource_max_prio_cmp(void *obj, void *arg, int flags) +{ + struct ast_xmpp_resource *resource1 = obj; + int *max_prio = arg; + + if (resource1->priority > *max_prio) { + *max_prio = resource1->priority; + } + + return 0; +} + /* * \internal * \brief Dial plan function status(). puts the status of watched user @@ -1582,6 +1606,7 @@ static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, cha struct ast_xmpp_buddy *buddy; struct ast_xmpp_resource *resource; int stat = 7; + int max_prio = 0; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(sender); AST_APP_ARG(jid); @@ -1619,7 +1644,8 @@ static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, cha } 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_callback(buddy->resources, OBJ_NODATA, xmpp_resource_max_prio_cmp, &max_prio); + resource = ao2_callback(buddy->resources, 0, xmpp_resource_prio_cmp, &max_prio); } ao2_ref(buddy, -1); -- 1.9.1