Skip to content

Commit

Permalink
Add xmpp_stanza_get_child_by_name_and_ns()
Browse files Browse the repository at this point in the history
I think it's fairly common in the XMPP world to actually want a stanza
by it's NS but also want a certain name.

For example this was needed in Profanity:
profanity-im/profanity@68af0aa
  • Loading branch information
jubalh authored and pasis committed Nov 25, 2019
1 parent 296df2f commit cbe6701
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/stanza.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,39 @@ xmpp_stanza_t *xmpp_stanza_get_child_by_ns(xmpp_stanza_t * const stanza,
return child;
}

/** Get the first child of stanza with name and a given namespace.
* This function searches all the immediate children of stanza for a child
* stanza that matches the name and namespace provided.
* The first matching child is returned.
*
* @param stanza a Strophe stanza object
* @param name a string with the name to match
* @param ns a string with the namespace to match
*
* @return the matching child stanza object or NULL if no match was found
*
* @ingroup Stanza
*/
xmpp_stanza_t *xmpp_stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza,
const char * const name,
const char * const ns)
{
xmpp_stanza_t *child;
const char *child_ns;

for (child = stanza->children; child; child = child->next) {
if (child->type == XMPP_STANZA_TAG &&
(strcmp(name, xmpp_stanza_get_name(child)) == 0)) {
child_ns = xmpp_stanza_get_ns(child);
if (child_ns && strcmp(ns, child_ns) == 0) {
break;
}
}
}

return child;
}

/** Get the list of children.
* This function returns the first child of the stanza object. The rest
* of the children can be obtained by calling xmpp_stanza_get_next() to
Expand Down
3 changes: 3 additions & 0 deletions strophe.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ xmpp_stanza_t *xmpp_stanza_get_child_by_name(xmpp_stanza_t * const stanza,
const char * const name);
xmpp_stanza_t *xmpp_stanza_get_child_by_ns(xmpp_stanza_t * const stanza,
const char * const ns);
xmpp_stanza_t *xmpp_stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza,
const char * const name,
const char * const ns);
xmpp_stanza_t *xmpp_stanza_get_next(xmpp_stanza_t * const stanza);
int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child);

Expand Down

0 comments on commit cbe6701

Please sign in to comment.