examples/bot: fix memory leaks and use stanza helpers
This commit is contained in:
156
examples/bot.c
156
examples/bot.c
@@ -25,105 +25,103 @@
|
|||||||
|
|
||||||
int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
|
int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
{
|
{
|
||||||
xmpp_stanza_t *reply, *query, *name, *version, *text;
|
xmpp_stanza_t *reply, *query, *name, *version, *text;
|
||||||
const char *ns;
|
const char *ns;
|
||||||
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
|
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
|
||||||
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);
|
printf("Received version request from %s\n", xmpp_stanza_get_from(stanza));
|
||||||
xmpp_stanza_set_name(query, "query");
|
|
||||||
|
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));
|
ns = xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza));
|
||||||
if (ns) {
|
if (ns) {
|
||||||
xmpp_stanza_set_ns(query, ns);
|
xmpp_stanza_set_ns(query, ns);
|
||||||
}
|
}
|
||||||
|
|
||||||
name = xmpp_stanza_new(ctx);
|
name = xmpp_stanza_new(ctx);
|
||||||
xmpp_stanza_set_name(name, "name");
|
xmpp_stanza_set_name(name, "name");
|
||||||
xmpp_stanza_add_child(query, 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);
|
|
||||||
|
|
||||||
version = xmpp_stanza_new(ctx);
|
|
||||||
xmpp_stanza_set_name(version, "version");
|
|
||||||
xmpp_stanza_add_child(query, version);
|
|
||||||
|
|
||||||
text = xmpp_stanza_new(ctx);
|
|
||||||
xmpp_stanza_set_text(text, "1.0");
|
|
||||||
xmpp_stanza_add_child(version, text);
|
|
||||||
|
|
||||||
xmpp_stanza_add_child(reply, query);
|
|
||||||
|
|
||||||
xmpp_send(conn, reply);
|
text = xmpp_stanza_new(ctx);
|
||||||
xmpp_stanza_release(reply);
|
xmpp_stanza_set_text(text, "libstrophe example bot");
|
||||||
return 1;
|
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)
|
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
{
|
{
|
||||||
xmpp_stanza_t *reply, *body, *text;
|
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
|
||||||
char *intext, *replytext;
|
xmpp_stanza_t *reply;
|
||||||
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
|
char *intext, *replytext;
|
||||||
|
|
||||||
if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
|
|
||||||
if(xmpp_stanza_get_type(stanza) !=NULL && !strcmp(xmpp_stanza_get_type(stanza), "error")) return 1;
|
|
||||||
|
|
||||||
intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
body = xmpp_stanza_new(ctx);
|
if (!xmpp_stanza_get_child_by_name(stanza, "body"))
|
||||||
xmpp_stanza_set_name(body, "body");
|
return 1;
|
||||||
|
if (xmpp_stanza_get_type(stanza) != NULL && !strcmp(xmpp_stanza_get_type(stanza), "error"))
|
||||||
replytext = (char *) malloc(strlen(" to you too!") + strlen(intext) + 1);
|
return 1;
|
||||||
strcpy(replytext, intext);
|
|
||||||
strcat(replytext, " to you too!");
|
intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
|
||||||
xmpp_free(ctx, intext);
|
|
||||||
|
printf("Incoming message from %s: %s\n", xmpp_stanza_get_from(stanza), intext);
|
||||||
text = xmpp_stanza_new(ctx);
|
|
||||||
xmpp_stanza_set_text(text, replytext);
|
reply = xmpp_stanza_reply(stanza);
|
||||||
xmpp_stanza_add_child(body, text);
|
if (xmpp_stanza_get_type(reply) == NULL)
|
||||||
xmpp_stanza_add_child(reply, body);
|
xmpp_stanza_set_type(reply, "chat");
|
||||||
xmpp_stanza_release(body);
|
|
||||||
xmpp_stanza_release(text);
|
replytext = (char *) malloc(strlen(" to you too!") + strlen(intext) + 1);
|
||||||
|
strcpy(replytext, intext);
|
||||||
xmpp_send(conn, reply);
|
strcat(replytext, " to you too!");
|
||||||
xmpp_stanza_release(reply);
|
xmpp_free(ctx, intext);
|
||||||
free(replytext);
|
xmpp_message_set_body(reply, replytext);
|
||||||
return 1;
|
|
||||||
|
xmpp_send(conn, reply);
|
||||||
|
xmpp_stanza_release(reply);
|
||||||
|
free(replytext);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* define a handler for connection events */
|
/* define a handler for connection events */
|
||||||
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
|
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
|
||||||
const int error, xmpp_stream_error_t * const stream_error,
|
const int error, xmpp_stream_error_t * const stream_error,
|
||||||
void * const userdata)
|
void * const userdata)
|
||||||
{
|
{
|
||||||
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
|
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
|
||||||
|
|
||||||
if (status == XMPP_CONN_CONNECT) {
|
if (status == XMPP_CONN_CONNECT) {
|
||||||
xmpp_stanza_t* pres;
|
xmpp_stanza_t* pres;
|
||||||
fprintf(stderr, "DEBUG: connected\n");
|
fprintf(stderr, "DEBUG: connected\n");
|
||||||
xmpp_handler_add(conn,version_handler, "jabber:iq:version", "iq", NULL, ctx);
|
xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL, ctx);
|
||||||
xmpp_handler_add(conn,message_handler, NULL, "message", NULL, ctx);
|
xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);
|
||||||
|
|
||||||
/* Send initial <presence/> so that we appear online to contacts */
|
/* Send initial <presence/> so that we appear online to contacts */
|
||||||
pres = xmpp_stanza_new(ctx);
|
pres = xmpp_presence_new(ctx);
|
||||||
xmpp_stanza_set_name(pres, "presence");
|
xmpp_send(conn, pres);
|
||||||
xmpp_send(conn, pres);
|
xmpp_stanza_release(pres);
|
||||||
xmpp_stanza_release(pres);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "DEBUG: disconnected\n");
|
fprintf(stderr, "DEBUG: disconnected\n");
|
||||||
xmpp_stop(ctx);
|
xmpp_stop(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,10 +134,10 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
/* take a jid and password on the command line */
|
/* take a jid and password on the command line */
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
fprintf(stderr, "Usage: bot <jid> <pass>\n\n");
|
fprintf(stderr, "Usage: bot <jid> <pass>\n\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
jid = argv[1];
|
jid = argv[1];
|
||||||
pass = argv[2];
|
pass = argv[2];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user