diff options
author | James Booth <boothj5@gmail.com> | 2013-01-20 02:26:54 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2013-01-20 02:26:54 +0000 |
commit | 17ed139d968a93838c0ee00e1b79373f7fdbffdb (patch) | |
tree | 3df04f10c0ee24efe1d59681ce5dde772cb1ce16 | |
parent | 8ff283d44dc38ae10be089412292b738afcdbeb2 (diff) | |
download | profani-tty-17ed139d968a93838c0ee00e1b79373f7fdbffdb.tar.gz |
Changed /info command to show client
-rw-r--r-- | src/capabilities.c | 16 | ||||
-rw-r--r-- | src/capabilities.h | 5 | ||||
-rw-r--r-- | src/command.c | 2 | ||||
-rw-r--r-- | src/jabber.c | 62 | ||||
-rw-r--r-- | src/ui.h | 1 | ||||
-rw-r--r-- | src/windows.c | 65 |
6 files changed, 135 insertions, 16 deletions
diff --git a/src/capabilities.c b/src/capabilities.c index e28112e7..61cd83ed 100644 --- a/src/capabilities.c +++ b/src/capabilities.c @@ -40,8 +40,7 @@ caps_init(void) } void -caps_add(const char * const caps_str, const char * const client, - const char * const version) +caps_add(const char * const caps_str, const char * const client) { Capabilities *new_caps = malloc(sizeof(struct capabilities_t)); @@ -51,12 +50,6 @@ caps_add(const char * const caps_str, const char * const client, new_caps->client = NULL; } - if (version != NULL) { - new_caps->version = strdup(version); - } else { - new_caps->version = NULL; - } - g_hash_table_insert(capabilities, strdup(caps_str), new_caps); } @@ -66,6 +59,12 @@ caps_contains(const char * const caps_str) return (g_hash_table_lookup(capabilities, caps_str) != NULL); } +Capabilities * +caps_get(const char * const caps_str) +{ + return g_hash_table_lookup(capabilities, caps_str); +} + void caps_close(void) { @@ -77,7 +76,6 @@ _caps_destroy(Capabilities *caps) { if (caps != NULL) { FREE_SET_NULL(caps->client); - FREE_SET_NULL(caps->version); FREE_SET_NULL(caps); } } diff --git a/src/capabilities.h b/src/capabilities.h index 128fb2e1..18d7edef 100644 --- a/src/capabilities.h +++ b/src/capabilities.h @@ -27,13 +27,12 @@ typedef struct capabilities_t { char *client; - char *version; } Capabilities; void caps_init(void); -void caps_add(const char * const caps_str, const char * const client, - const char * const version); +void caps_add(const char * const caps_str, const char * const client); gboolean caps_contains(const char * const caps_str); +Capabilities* caps_get(const char * const caps_str); void caps_close(void); #endif diff --git a/src/command.c b/src/command.c index 4e79b00d..c755c72e 100644 --- a/src/command.c +++ b/src/command.c @@ -1691,7 +1691,7 @@ _cmd_info(gchar **args, struct cmd_help_t help) } } else { if (usr != NULL) { - cons_show_status(usr); + cons_show_info(usr); } else { cons_show("Usage: %s", help.usage); } diff --git a/src/jabber.c b/src/jabber.c index cab8aaf6..48b51730 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -85,6 +85,8 @@ static int _iq_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _disco_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata); static int _presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata); @@ -784,11 +786,16 @@ _iq_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); + char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); // handle the initial roster request - if ((id != NULL) && (strcmp(id, "roster") == 0)) { + if (g_strcmp0(id, "roster") == 0) { return _roster_handler(conn, stanza, userdata); + // handle the initial roster request + } else if ((g_strcmp0(id, "disco") == 0) && (g_strcmp0(type, "result") == 0)) { + return _disco_handler(conn, stanza, userdata); + // handle iq } else { char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); @@ -878,8 +885,8 @@ _iq_handler(xmpp_conn_t * const conn, } static int -_roster_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata) +_roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) { xmpp_stanza_t *query, *item; char *type = xmpp_stanza_get_type(stanza); @@ -922,6 +929,55 @@ _roster_handler(xmpp_conn_t * const conn, } static int +_disco_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + char *type = xmpp_stanza_get_type(stanza); + + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + log_error("Roster query failed"); + return 1; + } else { + xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + const char *node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE); + + if (node == NULL) { + return 1; + } + + // already cached + if (caps_contains(node)) { + log_info("Client info already cached."); + return 1; + } + + xmpp_stanza_t *identity = xmpp_stanza_get_child_by_name(query, "identity"); + + if (identity == NULL) { + return 1; + } + + const char *category = xmpp_stanza_get_attribute(identity, "category"); + if (category == NULL) { + return 1; + } + + if (strcmp(category, "client") != 0) { + return 1; + } + + const char *name = xmpp_stanza_get_attribute(identity, "name"); + if (name == 0) { + return 1; + } + + caps_add(node, name); + + return 1; + } +} + +static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) { if (jabber_conn.conn_status == JABBER_CONNECTED) { diff --git a/src/ui.h b/src/ui.h index b69c1f19..eb528e67 100644 --- a/src/ui.h +++ b/src/ui.h @@ -161,6 +161,7 @@ void cons_show_contacts(GSList * list); void cons_check_version(gboolean not_available_msg); void cons_show_wins(void); void cons_show_status(const char * const contact); +void cons_show_info(const char * const contact); void cons_show_themes(GSList *themes); // status bar actions diff --git a/src/windows.c b/src/windows.c index 32b48fb8..08936344 100644 --- a/src/windows.c +++ b/src/windows.c @@ -44,6 +44,7 @@ #include <ncurses.h> #endif +#include "capabilities.h" #include "chat_log.h" #include "chat_session.h" #include "command.h" @@ -1172,6 +1173,70 @@ cons_show_wins(void) } void +cons_show_info(const char * const contact) +{ + PContact pcontact = contact_list_get_contact(contact); + + if (pcontact != NULL) { + const char *jid = p_contact_jid(pcontact); + const char *name = p_contact_name(pcontact); + const char *presence = p_contact_presence(pcontact); + const char *status = p_contact_status(pcontact); + const char *caps_str = p_contact_caps_str(pcontact); + GDateTime *last_activity = p_contact_last_activity(pcontact); + + cons_show(""); + cons_show("JID: %s", jid); + + if (name != NULL) { + cons_show("Name: %s", name); + } + + cons_show("Presence: %s", presence); + + if (status != NULL) { + cons_show("Message: %s", status); + } + + if (last_activity != NULL) { + GDateTime *now = g_date_time_new_now_local(); + GTimeSpan span = g_date_time_difference(now, last_activity); + + _win_show_time(console->win, '-'); + wprintw(console->win, "Last activity: "); + + int hours = span / G_TIME_SPAN_HOUR; + span = span - hours * G_TIME_SPAN_HOUR; + if (hours > 0) { + wprintw(console->win, "%dh", hours); + } + + int minutes = span / G_TIME_SPAN_MINUTE; + span = span - minutes * G_TIME_SPAN_MINUTE; + wprintw(console->win, "%dm", minutes); + + int seconds = span / G_TIME_SPAN_SECOND; + wprintw(console->win, "%ds", seconds); + + wprintw(console->win, "\n"); + + g_date_time_unref(now); + } + + if (caps_str != NULL) { + Capabilities *caps = caps_get(caps_str); + if ((caps != NULL) && (caps->client != NULL)) { + cons_show("Client: %s", caps->client); + } + } + + cons_show(""); + } else { + cons_show("No such contact \"%s\" in roster.", contact); + } +} + +void cons_show_status(const char * const contact) { PContact pcontact = contact_list_get_contact(contact); |