Possible bug in xmpp_stanza_set_name & xmpp_stanza_set_text call order ?

17 views
Skip to first unread message

Boo Radley

unread,
Apr 14, 2016, 11:35:40 AM4/14/16
to libstrophe
Hi all,

   Just came across this. I'm new here and hope it is not a known prob or (more likely) a bug in my understanding of Strophe.

   If I do these calls like this ...

        xmpp_stanza_set_name(method_name, "methodName");
        xmpp_stanza_set_text(method_name, "examples.getStateName");

        fprintf(stderr, "RPC_CALL method_name text => '%s'\n", xmpp_stanza_get_text(method_name));

... I get ...

RPC_CALL method_name text => '(null)'


   If I reverse the order like ...

        xmpp_stanza_set_text(method_name, "examples.getStateName");
        xmpp_stanza_set_name(method_name, "methodName");

... I get ...

RPC_CALL method_name text => 'examples.getStateName'


... which is much nicer :).


   So, I guess, it seems like a bug ?

Dmitry Podgorny

unread,
Apr 14, 2016, 12:36:58 PM4/14/16
to libstrophe
Hello,

As I see you want to get <methodName>examples.getStateName</methodName>. Inner text here is a stanza too. So, you need to create 2 stanzas:

    xmpp_stanza_t *method_name = xmpp_stanza_new(ctx);
    xmpp_stanza_t *text = xmpp_stanza_new(ctx);

    xmpp_stanza_set_text(text, "examples.getStateName");
    xmpp_stanza_set_name(method_name, "methodName");
    xmpp_stanza_add_child(method_name, text);
    xmpp_stanza_release(text); /* add_child() takes reference */
    xmpp_send(conn, method_name);
    xmpp_stanza_release(method_name);


Note, this example doesn't handle errors.

There are 2 stanza types: XMPP_STANZA_TEXT and XMPP_STANZA_TAG. You use xmpp_stanza_set_name() for the tags and _set_text() for the text. If you try to use both functions with a stanza you will get XMPP_EINVOP error from the 2nd call.

From other side, receiver doesn't have to worry about text stanzas. xmpp_stanza_get_text() recovers text from children if you pass a tag stanza:

    str = xmpp_stanza_get_text(method_name);
    /* str == "examples.getStateName" */
    xmpp_free(ctx, str); /* get_text() allocates memory */
Reply all
Reply to author
Forward
0 new messages