From 90a595f8ef542e15cd578de0afa2af3dc5b8e62a Mon Sep 17 00:00:00 2001 From: Tristan Le Guern Date: Wed, 29 Jul 2015 15:44:18 +0200 Subject: [PATCH] Implements helpers for to and from attributes xmpp_stanza_{get,set}_{from,to} --- src/stanza.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ strophe.h | 11 ++++---- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/stanza.c b/src/stanza.c index 5e3c7f7..acfa1e0 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -737,6 +737,48 @@ char *xmpp_stanza_get_type(xmpp_stanza_t * const stanza) return (char *)hash_get(stanza->attributes, "type"); } +/** Get the 'to' attribute of the stanza object. + * This is a convenience function equivalent to: + * xmpp_stanza_get_attribute(stanza, "to"); + * + * @param stanza a Strophe stanza object + * + * @return a string with the 'to' attribute value + * + * @ingroup Stanza + */ +char *xmpp_stanza_get_to(xmpp_stanza_t * const stanza) +{ + if (stanza->type != XMPP_STANZA_TAG) + return NULL; + + if (!stanza->attributes) + return NULL; + + return (char *)hash_get(stanza->attributes, "to"); +} + +/** Get the 'from' attribute of the stanza object. + * This is a convenience function equivalent to: + * xmpp_stanza_get_attribute(stanza, "from"); + * + * @param stanza a Strophe stanza object + * + * @return a string with the 'from' attribute value + * + * @ingroup Stanza + */ +char *xmpp_stanza_get_from(xmpp_stanza_t * const stanza) +{ + if (stanza->type != XMPP_STANZA_TAG) + return NULL; + + if (!stanza->attributes) + return NULL; + + return (char *)hash_get(stanza->attributes, "from"); +} + /** Get the first child of stanza with name. * This function searches all the immediate children of stanza for a child * stanza that matches the name. The first matching child is returned. @@ -919,6 +961,42 @@ int xmpp_stanza_set_type(xmpp_stanza_t * const stanza, return xmpp_stanza_set_attribute(stanza, "type", type); } +/** Set the 'to' attribute of a stanza. + * + * This is a convenience function for: + * xmpp_stanza_set_attribute(stanza, 'to', to); + * + * @param stanza a Strophe stanza object + * @param to a string containing the 'to' value + * + * @return XMPP_EOK (0) on success or a number less than 0 on failure + * + * @ingroup Stanza + */ +int xmpp_stanza_set_to(xmpp_stanza_t * const stanza, + const char * const to) +{ + return xmpp_stanza_set_attribute(stanza, "to", to); +} + +/** Set the 'from' attribute of a stanza. + * + * This is a convenience function for: + * xmpp_stanza_set_attribute(stanza, 'from', from); + * + * @param stanza a Strophe stanza object + * @param from a string containing the 'from' value + * + * @return XMPP_EOK (0) on success or a number less than 0 on failure + * + * @ingroup Stanza + */ +int xmpp_stanza_set_from(xmpp_stanza_t * const stanza, + const char * const from) +{ + return xmpp_stanza_set_attribute(stanza, "from", from); +} + /** Get an attribute from a stanza. * This function returns a pointer to the attribute value. If the caller * wishes to save this value it must make its own copy. diff --git a/strophe.h b/strophe.h index de62573..7f2f80e 100644 --- a/strophe.h +++ b/strophe.h @@ -345,15 +345,16 @@ int xmpp_stanza_set_text_with_size(xmpp_stanza_t *stanza, /* common stanza helpers */ char *xmpp_stanza_get_type(xmpp_stanza_t * const stanza); char *xmpp_stanza_get_id(xmpp_stanza_t * const stanza); +char *xmpp_stanza_get_to(xmpp_stanza_t * const stanza); +char *xmpp_stanza_get_from(xmpp_stanza_t * const stanza); int xmpp_stanza_set_id(xmpp_stanza_t * const stanza, const char * const id); int xmpp_stanza_set_type(xmpp_stanza_t * const stanza, const char * const type); - -/* unimplemented -int xmpp_stanza_set_to(); -int xmpp_stanza_set_from(); -*/ +int xmpp_stanza_set_to(xmpp_stanza_t * const stanza, + const char * const to); +int xmpp_stanza_set_from(xmpp_stanza_t * const stanza, + const char * const from); /* allocate and initialize a stanza in reply to another */ /* unimplemented