From 28b172387605a4f2175898177334e6b8cc4c0db3 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 2 Oct 2012 01:04:53 +0100 Subject: [PATCH] Moved roster output handling to profanity module --- src/jabber.c | 31 ++++++++++++++----------------- src/profanity.c | 36 ++++++++++++++++++++++++++++++++++++ src/profanity.h | 6 ++++++ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/jabber.c b/src/jabber.c index 16131fb0..b29658d8 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -399,36 +399,33 @@ _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_stanza_t *query, *item; - char *type, *name, *jid; - - type = xmpp_stanza_get_type(stanza); + char *type = xmpp_stanza_get_type(stanza); if (strcmp(type, "error") == 0) log_error("Roster query failed"); else { query = xmpp_stanza_get_child_by_name(stanza, "query"); - cons_show("Roster:"); - + GSList *roster = NULL; item = xmpp_stanza_get_children(query); + while (item != NULL) { - name = xmpp_stanza_get_attribute(item, "name"); - jid = xmpp_stanza_get_attribute(item, "jid"); + const char *name = xmpp_stanza_get_attribute(item, "name"); + const char *jid = xmpp_stanza_get_attribute(item, "jid"); + + jabber_roster_entry *entry = malloc(sizeof(jabber_roster_entry)); if (name != NULL) { - char line[strlen(name) + 2 + strlen(jid) + 1 + 1]; - sprintf(line, "%s (%s)", name, jid); - cons_show(line); - + entry->name = strdup(name); } else { - char line[strlen(jid) + 1]; - sprintf(line, "%s", jid); - cons_show(line); + entry->name = NULL; } - - item = xmpp_stanza_get_next(item); + entry->jid = strdup(jid); - win_page_off(); + roster = g_slist_append(roster, entry); + item = xmpp_stanza_get_next(item); } + + prof_handle_roster(roster); } return 1; diff --git a/src/profanity.c b/src/profanity.c index 01be4289..5da18675 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -35,11 +35,13 @@ #include "log.h" #include "preferences.h" #include "profanity.h" +#include "jabber.h" #include "ui.h" static log_level_t _get_log_level(char *log_level); gboolean _process_input(char *inp); static void _create_config_directory(); +static void _free_roster_entry(jabber_roster_entry *entry); void profanity_run(void) @@ -247,6 +249,30 @@ prof_handle_contact_offline(char *contact, char *show, char *status) win_page_off(); } +void prof_handle_roster(GSList *roster) +{ + cons_show("Roster:"); + while (roster != NULL) { + jabber_roster_entry *entry = roster->data; + if (entry->name != NULL) { + char line[strlen(entry->name) + 2 + strlen(entry->jid) + 1 + 1]; + sprintf(line, "%s (%s)", entry->name, entry->jid); + cons_show(line); + + } else { + char line[strlen(entry->jid) + 1]; + sprintf(line, "%s", entry->jid); + cons_show(line); + } + + roster = g_slist_next(roster); + + win_page_off(); + } + + g_slist_free_full(roster, (GDestroyNotify)_free_roster_entry); +} + static void _create_config_directory() { @@ -256,3 +282,13 @@ _create_config_directory() g_string_free(dir, TRUE); } +static void +_free_roster_entry(jabber_roster_entry *entry) +{ + if (entry->name != NULL) { + free(entry->name); + entry->name = NULL; + } + free(entry->jid); +} + diff --git a/src/profanity.h b/src/profanity.h index af6b3e11..16fae9d0 100644 --- a/src/profanity.h +++ b/src/profanity.h @@ -32,7 +32,13 @@ void prof_handle_typing(char *from); void prof_handle_contact_online(char *contact, char *show, char *status); void prof_handle_contact_offline(char *contact, char *show, char *status); void prof_handle_incoming_message(char *from, char *message); +void prof_handle_roster(GSList *roster); void profanity_shutdown_init(void); void profanity_shutdown(void); +typedef struct roster_entry_t { + char *name; + char *jid; +} jabber_roster_entry; + #endif