Re: addhandler of libstrophe example bot.c not trigger

15 views
Skip to first unread message
Message has been deleted

Dmitry Podgorny

unread,
May 5, 2020, 1:49:15 PM5/5/20
to libstrophe
Hi,

Do you see your message stanza in the debug logs? On the both sending and receiving sides? As an option, you can share debug logs from libstrophe - they will help to find the issue faster. Just remove all private information from the logs!

On Monday, May 4, 2020 at 11:04:47 PM UTC+3, John wrote:
Hello, everyone.This is my first time to use libstrophe-master.I have some question about sending and receiving message.
Here is my question:I create two openfire server,both of them have their own user.I hope one can send message to the other.The receiving end use example bot.c,but it seems like the addhandler not trigger. Although I surf on internet,I still don't know how to trigger it.
If somebody can help me. I will appreciate.Thank you very much.

============================================================================================================================================
/* simple.c
** libstrophe XMPP client library -- basic usage example
**
** Copyright (C) 2005-2009 Collecta, Inc.
**
**  This software is provided AS-IS with no warranty, either express
**  or implied.
**
**  This software is distributed under license and may not be copied,
**  modified or distributed except as expressly authorized under the
**  terms of the license contained in the file LICENSE.txt in this
**  distribution.
*/

/* simple message example
**
** This example was provided by Elmo Todurov <tod...@gmail.com>
**
** This progam just sends a simple message to the given Jabber ID.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <strophe.h>


/** Send a single text message.
 *  This function sends a simple text message to a given recipient.
 *
 *  @param conn a Strophe connection object
 *  @param to the JID of the recipient
 *  @param message the text of the message to send
 *
 *  @ingroup Connections
 */

struct msgdata {
    char* to;
    char* msg;
};


void xmpp_send_message(xmpp_conn_t *conn,
             const char * const to,
             const char * const message)
{
   xmpp_stanza_t *msg, *body, *text;
   xmpp_ctx_t *ctx = xmpp_conn_get_context(conn);  

   msg = xmpp_stanza_new(ctx);
   xmpp_stanza_set_name(msg, "message");
   xmpp_stanza_set_type(msg, "chat");
   xmpp_stanza_set_attribute(msg, "to", to);

   body = xmpp_stanza_new(ctx);
   xmpp_stanza_set_name(body, "body");

   text = xmpp_stanza_new(ctx);
   xmpp_stanza_set_text(text, message);
   xmpp_stanza_add_child(body, text);
   xmpp_stanza_add_child(msg, body);

   xmpp_send(conn, msg);
   xmpp_stanza_release(msg);
}

/* define a handler for connection events */
void conn_handler(xmpp_conn_t * const conn, 
  const xmpp_conn_event_t status,
            const int error, 
  xmpp_stream_error_t * const stream_error,
            void * const userdata)
{
    xmpp_ctx_t *ctx = xmpp_conn_get_context(conn);
    struct msgdata* msg = (struct msgdata*)userdata;

    (void)error;
    (void)stream_error;

    if (status == XMPP_CONN_CONNECT) {
    xmpp_stanza_t* pres;
    fprintf(stderr, "DEBUG: connected\n");

    /* Send initial <presence/> so that we appear online to contacts */
    pres = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(pres, "presence");
    xmpp_send(conn, pres);
    xmpp_stanza_release(pres);

    /* Send a message */
    //xmpp_send_simple_message(conn, msg->to, msg->msg);
    xmpp_send_message(conn, msg->to, msg->msg);
    /*if(check == 'i')
    {
        xmpp_send_iq(conn, msg->to, msg->msg);
    }
    else
    {
        xmpp_send_message(conn, msg->to, msg->msg);
    }*/
    free(msg);

    /* Causes the conn_handler to be executed, where the event loop gets shut down */
    xmpp_disconnect(conn);
    }
    else {
    fprintf(stderr, "DEBUG: disconnected\n");
    xmpp_stop(ctx);
    }
}

int main()
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    //char *jid, *pass;
    char jid[100] = "pi...@openfire.pi.com";
    char pass[100] = "00000000";
    long flags = 0;
    flags |= XMPP_CONN_FLAG_TRUST_TLS;


    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass XMPP_LEVEL_ERROR instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* configure connection properties (optional) */
    xmpp_conn_set_flags(conn, flags);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* Prepare the message for sending */
    struct msgdata* msg = (struct msgdata*)malloc(sizeof(struct msgdata));
    msg->to = "pi...@openfire.pi.com";
    msg->msg = "10";
    printf("to: %s\n", msg->to);
    printf("msg: %s\n", msg->msg);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, msg);

    /* enter the event loop -
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;

============================================================================================================================================


Program for receiving:
/* bot.c
** libstrophe XMPP client library -- basic usage example
**
** Copyright (C) 2005-2009 Collecta, Inc.
**
**  This software is provided AS-IS with no warranty, either express
**  or implied.
**
** This program is dual licensed under the MIT and GPLv3 licenses.
*/

/* simple bot example
**
** This example was provided by Matthew Wild <mwi...@gmail.com>.
**
** This bot responds to basic messages and iq version requests.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <strophe.h>

int version_handler(xmpp_conn_t *const conn,
                    xmpp_stanza_t *const stanza,
                    void *const userdata)
{
    xmpp_stanza_t *reply, *query, *name, *version, *text;
    const char *ns;
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;

    printf("\nNow is in version_handler\n");
    printf("Received version request from %s\n", xmpp_stanza_get_from(stanza));

    reply = xmpp_stanza_reply(stanza);
    xmpp_stanza_set_type(reply, "result");

    query = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(query, "query");
    ns = xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza));
    if (ns) {
        xmpp_stanza_set_ns(query, ns);
    }

    name = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(name, "name");
    xmpp_stanza_add_child(query, name);
    xmpp_stanza_release(name);

    text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text, "libstrophe example bot");
    xmpp_stanza_add_child(name, text);
    xmpp_stanza_release(text);

    version = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(version, "version");
    xmpp_stanza_add_child(query, version);
    xmpp_stanza_release(version);

    text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text, "1.0");
    xmpp_stanza_add_child(version, text);
    xmpp_stanza_release(text);

    xmpp_stanza_add_child(reply, query);
    xmpp_stanza_release(query);

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);
    return 1;
}

int message_handler(xmpp_conn_t *const conn,
                    xmpp_stanza_t *const stanza,
                    void *const userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
    xmpp_stanza_t *body, *reply;
    const char *type;
    char *intext, *replytext;
    int quit = 0;

    printf("\nNow is in message handler\n");

    body = xmpp_stanza_get_child_by_name(stanza, "body");
    if (body == NULL)
        return 1;
    
    type = xmpp_stanza_get_type(stanza);
    if (type != NULL && strcmp(type, "error") == 0)
        return 1;

    intext = xmpp_stanza_get_text(body);
    //printf("\nNow is to print something we get\n");
    printf("Incoming message from %s: %s\n", xmpp_stanza_get_from(stanza), intext);

    reply = xmpp_stanza_reply(stanza);
    if (xmpp_stanza_get_type(reply) == NULL)
        xmpp_stanza_set_type(reply, "chat");

    if (strcmp(intext, "quit") == 0) {
        replytext = strdup("bye!");
        quit = 1;
    } else {
        replytext = (char *)malloc(strlen(" to you too!") + strlen(intext) + 1);
        strcpy(replytext, intext);
        strcat(replytext, " to you too!");
    }
    xmpp_free(ctx, intext);
    xmpp_message_set_body(reply, replytext);

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);
    free(replytext);

    if (quit)
        xmpp_disconnect(conn);

    return 1;
}

/* define a handler for connection events */
void conn_handler(xmpp_conn_t *const conn,
                  const xmpp_conn_event_t status,
                  const int error,
                  xmpp_stream_error_t *const stream_error,
                  void *const userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;

    (void)error;
    (void)stream_error;

    if (status == XMPP_CONN_CONNECT) {

        xmpp_stanza_t *pres;
        fprintf(stderr, "DEBUG: connected\n");
        //xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL, ctx);
        xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);
        /* Send initial <presence/> so that we appear online to contacts */
        pres = xmpp_presence_new(ctx);
        xmpp_send(conn, pres);
        xmpp_stanza_release(pres);
    } else {
        fprintf(stderr, "DEBUG: disconnected\n");
        xmpp_stop(ctx);
    }
}

int main()
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    //char *jid, *pass;
    char jid[100] = "clie...@openfire.client.com";
    char pass[100] = "00000000";
    long flags = 0;
    flags |= XMPP_CONN_FLAG_TRUST_TLS;

    /* take a jid and password on the command line */
    /*if (argc != 3) {
        fprintf(stderr, "Usage: bot <jid> <pass>\n\n");
        return 1;
    }

    jid = argv[1];
    pass = argv[2];*/


    /* init library */
    xmpp_initialize();

    /* pass NULL instead to silence output */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    /* create a context */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* configure connection properties (optional) */
    xmpp_conn_set_flags(conn, flags);

    /*
     * also you can disable TLS support or force legacy SSL
     * connection without STARTTLS
     *
     * see xmpp_conn_set_flags() or examples/basic.c
     */

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);
    printf("\n\nJID: %s\n", jid);
    printf("pass: %s\n", pass);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

    /* enter the event loop -
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
============================================================================================================================================

John

unread,
May 6, 2020, 11:03:05 PM5/6/20
to libstrophe


John於 2020年5月5日星期二 UTC+8上午4時04分47秒寫道:
To Dmitry Podgorny:
Thank you for your opinion.I will try things what you say.Thank you very much. 
Reply all
Reply to author
Forward
0 new messages