diff --git a/src/bot/context.c b/src/bot/context.c index d13b002..718e5ec 100644 --- a/src/bot/context.c +++ b/src/bot/context.c @@ -3,6 +3,12 @@ #include "context.h" +void bot_free_user_data(struct BotUserInfo user, bool freeSegment) +{ + g_string_free(user.jid, freeSegment); + g_string_free(user.pass, freeSegment); +} + int bot_xmpp_startup(struct BotUserInfo user, struct BotXmppCtx * const bctx) { xmpp_log_t* newLog; diff --git a/src/bot/context.h b/src/bot/context.h index 479ab06..a11bb50 100644 --- a/src/bot/context.h +++ b/src/bot/context.h @@ -24,6 +24,8 @@ xmpp_conn_t* conn; }; +void bot_free_user_data(struct BotUserInfo user, bool freeSegment); + /* perform xmpp startup, store in bctx if completed successfully */ int bot_xmpp_startup(struct BotUserInfo user, struct BotXmppCtx * const bctx); diff --git a/src/bot/handlers.c b/src/bot/handlers.c index db6948b..4ef20a6 100644 --- a/src/bot/handlers.c +++ b/src/bot/handlers.c @@ -1,5 +1,6 @@ /* bot/handlers.c */ + #include "handlers.h" int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) @@ -49,15 +50,16 @@ 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; + GString *intext; + GString *replytext; if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1; if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1; - intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body")); + intext = g_string_new(xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"))); - printf("Incoming message from %s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext); + printf("Incoming message from %s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext->str); reply = xmpp_stanza_new(ctx); xmpp_stanza_set_name(reply, "message"); @@ -67,12 +69,12 @@ body = xmpp_stanza_new(ctx); xmpp_stanza_set_name(body, "body"); - replytext = malloc(strlen(" to you too!") + strlen(intext) + 1); - strcpy(replytext, intext); - strcat(replytext, " to you too!"); + replytext = g_string_new(" to you too!"); + strcpy(replytext->str, intext->str); + strcat(replytext->str, " to you too!"); text = xmpp_stanza_new(ctx); - xmpp_stanza_set_text(text, replytext); + xmpp_stanza_set_text(text, replytext->str); xmpp_stanza_add_child(body, text); xmpp_stanza_add_child(reply, body); diff --git a/src/bot/handlers.h b/src/bot/handlers.h index 1ebbdcb..07a3618 100644 --- a/src/bot/handlers.h +++ b/src/bot/handlers.h @@ -1,7 +1,12 @@ +/* bot/handlers.h + */ + +#pragma once #include #include #include #include +#include /* respond to version queries */ int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); diff --git a/src/main.c b/src/main.c index 6650ad5..0a35a2b 100644 --- a/src/main.c +++ b/src/main.c @@ -7,9 +7,6 @@ int main(int argc, char **argv) { - xmpp_ctx_t *ctx; - xmpp_conn_t *conn; - xmpp_log_t *log; struct BotUserInfo user; struct BotXmppCtx bctx; int startup_code; @@ -43,5 +40,8 @@ /* final shutdown of the library */ bot_xmpp_shutdown(&bctx); + /* Free user data, leave underlying strings as they are part of argv*/ + bot_free_user_data(user, false); + return 0; }