From 9de0293405c139d65dff04ee9dc42ebb6d1b022a Mon Sep 17 00:00:00 2001 From: Tristan Le Guern Date: Wed, 29 Jul 2015 16:11:12 +0200 Subject: [PATCH] Implement xmpp_stanza_reply() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function creates a copy of the given stanza minus its children and set the “to” attribute with the value of the original “from”. --- src/stanza.c | 41 +++++++++++++++++++++++++++++++++++++++++ strophe.h | 4 +--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/stanza.c b/src/stanza.c index 63c894a..f4e9a64 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -1063,3 +1063,44 @@ int xmpp_stanza_del_attribute(xmpp_stanza_t * const stanza, return hash_drop(stanza->attributes, name); } + +/** Create a stanza object in reply to another. + * This function makes a copy of a stanza object with the attribute “to” set + * its original “from”. + * The stanza will have a reference count of one, so the caller does not + * need to clone it. + * + * @param stanza a Strophe stanza object + * + * @return a new Strophe stanza object + * + * @ingroup Stanza + */ +xmpp_stanza_t *xmpp_stanza_reply(xmpp_stanza_t * const stanza) +{ + xmpp_stanza_t *copy; + + copy = xmpp_stanza_new(stanza->ctx); + if (!copy) goto copy_error; + + copy->type = stanza->type; + + if (stanza->data) { + copy->data = xmpp_strdup(stanza->ctx, stanza->data); + if (!copy->data) goto copy_error; + } + + if (stanza->attributes) { + if (_stanza_copy_attributes(copy, stanza) == -1) + goto copy_error; + } + + xmpp_stanza_set_to(copy, xmpp_stanza_get_from(stanza)); + xmpp_stanza_del_attribute(copy, "from"); + + return copy; + +copy_error: + if (copy) xmpp_stanza_release(copy); + return NULL; +} diff --git a/strophe.h b/strophe.h index 6c094a1..91b0669 100644 --- a/strophe.h +++ b/strophe.h @@ -360,9 +360,7 @@ int xmpp_stanza_set_from(xmpp_stanza_t * const stanza, const char * const from); /* allocate and initialize a stanza in reply to another */ -/* unimplemented -xmpp_stanza_t *xmpp_stanza_reply(const xmpp_stanza_t *stanza); -*/ +xmpp_stanza_t *xmpp_stanza_reply(xmpp_stanza_t * const stanza); /* stanza subclasses */ /* unimplemented