diff --git a/src/jabber.c b/src/jabber.c index 2c5f4f44..a139dedc 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -392,13 +392,20 @@ _message_handler(xmpp_conn_t * const conn, if(g_time_val_from_iso8601(utc_stamp, &tv_stamp)) { xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body"); + if (body != NULL) { - char *message = xmpp_stanza_get_text(body); - char **tokens = g_strsplit(from, "/", 0); - char *room_jid = tokens[0]; - char *nick = tokens[1]; - prof_handle_room_history(room_jid, nick, tv_stamp, message); - g_strfreev(tokens); + char *room = NULL; + char *nick = NULL; + gboolean parse_success = room_parse_room_jid(from, &room, &nick); + + if (parse_success) { + char *message = xmpp_stanza_get_text(body); + prof_handle_room_history(room, nick, tv_stamp, message); + g_free(room); + g_free(nick); + } else { + log_error("Could not parse room jid: %s", from); + } } } else { log_error("Couldn't parse datetime string receiving room history: %s", utc_stamp); @@ -408,12 +415,18 @@ _message_handler(xmpp_conn_t * const conn, } else { xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body"); if (body != NULL) { - char *message = xmpp_stanza_get_text(body); - char **tokens = g_strsplit(from, "/", 0); - char *room_jid = tokens[0]; - char *nick = tokens[1]; - prof_handle_room_message(room_jid, nick, message); - g_strfreev(tokens); + char *room = NULL; + char *nick = NULL; + gboolean parse_success = room_parse_room_jid(from, &room, &nick); + + if (parse_success) { + char *message = xmpp_stanza_get_text(body); + prof_handle_room_message(room, nick, message); + g_free(room); + g_free(nick); + } else { + log_error("Could not parse room jid: %s", from); + } } } @@ -729,3 +742,4 @@ _jabber_send_state(const char * const recipient, const char * const state) chat_session_set_sent(recipient); } + diff --git a/src/room_chat.c b/src/room_chat.c index a8125874..5031d8ad 100644 --- a/src/room_chat.c +++ b/src/room_chat.c @@ -90,6 +90,23 @@ room_get_nick_for_room(const char * const jid) } } +gboolean +room_parse_room_jid(const char * const room_jid, char **room, char **nick) +{ + char **tokens = g_strsplit(room_jid, "/", 0); + + if (tokens == NULL || tokens[0] == NULL || tokens[1] == NULL) { + return FALSE; + } else { + *room = strdup(tokens[0]); + *nick = strdup(tokens[1]); + + g_strfreev(tokens); + + return TRUE; + } +} + static void _room_free(muc_room *room) { diff --git a/src/room_chat.h b/src/room_chat.h index c8be1f5b..04f6300e 100644 --- a/src/room_chat.h +++ b/src/room_chat.h @@ -26,3 +26,5 @@ void room_join(const char * const jid, const char * const nick); void room_leave(const char * const jid); gboolean room_jid_is_room_chat(const char * const jid); char * room_get_nick_for_room(const char * const jid); +gboolean room_parse_room_jid(const char * const room_jid, char **room, + char **nick);