/** 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 = NULL; const char *from; int rc; from = xmpp_stanza_get_from(stanza); if (!from) goto copy_error; 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) < 0) goto copy_error; } xmpp_stanza_del_attribute(copy, "to"); xmpp_stanza_del_attribute(copy, "from"); rc = xmpp_stanza_set_to(copy, from); if (rc != XMPP_EOK) goto copy_error; return copy; copy_error: if (copy) xmpp_stanza_release(copy); return NULL; }
/** 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; }
/** Copy a stanza and its children. * This function copies a stanza along with all its children and returns * the new stanza and children with a reference count of 1. The returned * stanza will have no parent and no siblings. This function is useful * for extracting a child stanza for inclusion in another tree. * * @param stanza a Strophe stanza object * * @return a new Strophe stanza object * * @ingroup Stanza */ xmpp_stanza_t *xmpp_stanza_copy(const xmpp_stanza_t * const stanza) { xmpp_stanza_t *copy, *child, *copychild, *tail; 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; } tail = copy->children; for (child = stanza->children; child; child = child->next) { copychild = xmpp_stanza_copy(child); if (!copychild) goto copy_error; copychild->parent = copy; if (tail) { copychild->prev = tail; tail->next = copychild; } else copy->children = copychild; tail = copychild; } return copy; copy_error: /* release all the hitherto allocated memory */ if (copy) xmpp_stanza_release(copy); return NULL; }