1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Changed /info command to show client

This commit is contained in:
James Booth 2013-01-20 02:26:54 +00:00
parent 8ff283d44d
commit 17ed139d96
6 changed files with 135 additions and 16 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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);
@ -921,6 +928,55 @@ _roster_handler(xmpp_conn_t * const conn,
return 1;
}
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)
{

View File

@ -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

View File

@ -44,6 +44,7 @@
#include <ncurses.h>
#endif
#include "capabilities.h"
#include "chat_log.h"
#include "chat_session.h"
#include "command.h"
@ -1171,6 +1172,70 @@ cons_show_wins(void)
cons_show("");
}
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)
{