mirror of
https://github.com/strophe/libstrophe.git
synced 2026-08-04 11:06:20 +00:00
examples/bot: fix memory leaks and use stanza helpers
This commit is contained in:
@@ -28,6 +28,7 @@ int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void
|
|||||||
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));
|
printf("Received version request from %s\n", xmpp_stanza_get_from(stanza));
|
||||||
|
|
||||||
reply = xmpp_stanza_reply(stanza);
|
reply = xmpp_stanza_reply(stanza);
|
||||||
@@ -43,20 +44,25 @@ int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void
|
|||||||
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);
|
text = xmpp_stanza_new(ctx);
|
||||||
xmpp_stanza_set_text(text, "libstrophe example bot");
|
xmpp_stanza_set_text(text, "libstrophe example bot");
|
||||||
xmpp_stanza_add_child(name, text);
|
xmpp_stanza_add_child(name, text);
|
||||||
|
xmpp_stanza_release(text);
|
||||||
|
|
||||||
version = xmpp_stanza_new(ctx);
|
version = xmpp_stanza_new(ctx);
|
||||||
xmpp_stanza_set_name(version, "version");
|
xmpp_stanza_set_name(version, "version");
|
||||||
xmpp_stanza_add_child(query, version);
|
xmpp_stanza_add_child(query, version);
|
||||||
|
xmpp_stanza_release(version);
|
||||||
|
|
||||||
text = xmpp_stanza_new(ctx);
|
text = xmpp_stanza_new(ctx);
|
||||||
xmpp_stanza_set_text(text, "1.0");
|
xmpp_stanza_set_text(text, "1.0");
|
||||||
xmpp_stanza_add_child(version, text);
|
xmpp_stanza_add_child(version, text);
|
||||||
|
xmpp_stanza_release(text);
|
||||||
|
|
||||||
xmpp_stanza_add_child(reply, query);
|
xmpp_stanza_add_child(reply, query);
|
||||||
|
xmpp_stanza_release(query);
|
||||||
|
|
||||||
xmpp_send(conn, reply);
|
xmpp_send(conn, reply);
|
||||||
xmpp_stanza_release(reply);
|
xmpp_stanza_release(reply);
|
||||||
@@ -66,12 +72,14 @@ int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void
|
|||||||
|
|
||||||
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;
|
|
||||||
char *intext, *replytext;
|
|
||||||
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
|
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
|
||||||
|
xmpp_stanza_t *reply;
|
||||||
|
char *intext, *replytext;
|
||||||
|
|
||||||
if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
|
if (!xmpp_stanza_get_child_by_name(stanza, "body"))
|
||||||
if(xmpp_stanza_get_type(stanza) !=NULL && !strcmp(xmpp_stanza_get_type(stanza), "error")) return 1;
|
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"));
|
intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
|
||||||
|
|
||||||
@@ -81,20 +89,11 @@ int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void
|
|||||||
if (xmpp_stanza_get_type(reply) == NULL)
|
if (xmpp_stanza_get_type(reply) == NULL)
|
||||||
xmpp_stanza_set_type(reply, "chat");
|
xmpp_stanza_set_type(reply, "chat");
|
||||||
|
|
||||||
body = xmpp_stanza_new(ctx);
|
|
||||||
xmpp_stanza_set_name(body, "body");
|
|
||||||
|
|
||||||
replytext = (char *) malloc(strlen(" to you too!") + strlen(intext) + 1);
|
replytext = (char *) malloc(strlen(" to you too!") + strlen(intext) + 1);
|
||||||
strcpy(replytext, intext);
|
strcpy(replytext, intext);
|
||||||
strcat(replytext, " to you too!");
|
strcat(replytext, " to you too!");
|
||||||
xmpp_free(ctx, intext);
|
xmpp_free(ctx, intext);
|
||||||
|
xmpp_message_set_body(reply, replytext);
|
||||||
text = xmpp_stanza_new(ctx);
|
|
||||||
xmpp_stanza_set_text(text, replytext);
|
|
||||||
xmpp_stanza_add_child(body, text);
|
|
||||||
xmpp_stanza_add_child(reply, body);
|
|
||||||
xmpp_stanza_release(body);
|
|
||||||
xmpp_stanza_release(text);
|
|
||||||
|
|
||||||
xmpp_send(conn, reply);
|
xmpp_send(conn, reply);
|
||||||
xmpp_stanza_release(reply);
|
xmpp_stanza_release(reply);
|
||||||
@@ -116,8 +115,7 @@ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user