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

Added jid->hash lookup for capabilities

This commit is contained in:
James Booth 2014-09-20 23:50:19 +01:00
parent 6e19476f6f
commit 2fb9989123
11 changed files with 172 additions and 153 deletions

View File

@ -454,8 +454,7 @@ muc_nick_in_roster(const char * const room, const char * const nick)
*/ */
gboolean gboolean
muc_add_to_roster(const char * const room, const char * const nick, muc_add_to_roster(const char * const room, const char * const nick,
const char * const show, const char * const status, const char * const show, const char * const status)
const char * const caps_str)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
gboolean updated = FALSE; gboolean updated = FALSE;
@ -472,7 +471,7 @@ muc_add_to_roster(const char * const room, const char * const nick,
} }
PContact contact = p_contact_new(nick, NULL, NULL, NULL, NULL, FALSE); PContact contact = p_contact_new(nick, NULL, NULL, NULL, NULL, FALSE);
resource_presence_t resource_presence = resource_presence_from_string(show); resource_presence_t resource_presence = resource_presence_from_string(show);
Resource *resource = resource_new(nick, resource_presence, status, 0, caps_str); Resource *resource = resource_new(nick, resource_presence, status, 0);
p_contact_set_presence(contact, resource); p_contact_set_presence(contact, resource);
g_hash_table_replace(chat_room->roster, strdup(nick), contact); g_hash_table_replace(chat_room->roster, strdup(nick), contact);
} }

View File

@ -40,7 +40,7 @@
#include <resource.h> #include <resource.h>
Resource * resource_new(const char * const name, resource_presence_t presence, Resource * resource_new(const char * const name, resource_presence_t presence,
const char * const status, const int priority, const char * const caps_str) const char * const status, const int priority)
{ {
assert(name != NULL); assert(name != NULL);
Resource *new_resource = malloc(sizeof(struct resource_t)); Resource *new_resource = malloc(sizeof(struct resource_t));
@ -52,11 +52,6 @@ Resource * resource_new(const char * const name, resource_presence_t presence,
new_resource->status = NULL; new_resource->status = NULL;
} }
new_resource->priority = priority; new_resource->priority = priority;
if (caps_str != NULL) {
new_resource->caps_str = strdup(caps_str);
} else {
new_resource->caps_str = NULL;
}
return new_resource; return new_resource;
} }
@ -96,7 +91,6 @@ void resource_destroy(Resource *resource)
if (resource != NULL) { if (resource != NULL) {
free(resource->name); free(resource->name);
free(resource->status); free(resource->status);
free(resource->caps_str);
free(resource); free(resource);
} }
} }

View File

@ -42,11 +42,10 @@ typedef struct resource_t {
resource_presence_t presence; resource_presence_t presence;
char *status; char *status;
int priority; int priority;
char *caps_str;
} Resource; } Resource;
Resource * resource_new(const char * const name, resource_presence_t presence, Resource * resource_new(const char * const name, resource_presence_t presence,
const char * const status, const int priority, const char * const caps_str); const char * const status, const int priority);
void resource_destroy(Resource *resource); void resource_destroy(Resource *resource);
int resource_compare_availability(Resource *first, Resource *second); int resource_compare_availability(Resource *first, Resource *second);

View File

@ -282,19 +282,19 @@ _cons_show_info(PContact pcontact)
} }
static void static void
_cons_show_caps(const char * const contact, Resource *resource) _cons_show_caps(const char * const fulljid, Resource *resource)
{ {
ProfWin *console = wins_get_console(); ProfWin *console = wins_get_console();
cons_show(""); cons_show("");
const char *resource_presence = string_from_resource_presence(resource->presence); const char *resource_presence = string_from_resource_presence(resource->presence);
int presence_colour = win_presence_colour(resource_presence); int presence_colour = win_presence_colour(resource_presence);
win_save_vprint(console, '-', NULL, NO_EOL, presence_colour, "", "%s", contact); win_save_vprint(console, '-', NULL, NO_EOL, presence_colour, "", "%s", fulljid);
win_save_print(console, '-', NULL, NO_DATE, 0, "", ":"); win_save_print(console, '-', NULL, NO_DATE, 0, "", ":");
if (resource->caps_str != NULL) { Capabilities *caps = caps_lookup(fulljid);
Capabilities *caps = caps_get(resource->caps_str);
if (caps != NULL) { if (caps) {
// show identity // show identity
if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) { if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) {
win_save_print(console, '-', NULL, NO_EOL, 0, "", "Identity: "); win_save_print(console, '-', NULL, NO_EOL, 0, "", "Identity: ");
@ -343,7 +343,6 @@ _cons_show_caps(const char * const contact, Resource *resource)
} }
} }
} }
}
cons_alert(); cons_alert();
} }
@ -733,9 +732,9 @@ _cons_show_account(ProfAccount *account)
win_save_vprint(console, '-', NULL, NO_DATE | NO_EOL, presence_colour, "", ", \"%s\"", resource->status); win_save_vprint(console, '-', NULL, NO_DATE | NO_EOL, presence_colour, "", ", \"%s\"", resource->status);
} }
win_save_vprint(console, '-', NULL, NO_DATE, 0, "", ""); win_save_vprint(console, '-', NULL, NO_DATE, 0, "", "");
Jid *jidp = jid_create_from_bare_and_resource(account->jid, resource->name);
Capabilities *caps = caps_lookup(jidp->fulljid);
if (resource->caps_str != NULL) {
Capabilities *caps = caps_get(resource->caps_str);
if (caps != NULL) { if (caps != NULL) {
// show identity // show identity
if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) { if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) {
@ -776,7 +775,6 @@ _cons_show_account(ProfAccount *account)
win_save_newline(console); win_save_newline(console);
} }
} }
}
ordered_resources = g_list_next(ordered_resources); ordered_resources = g_list_next(ordered_resources);
} }

View File

@ -234,7 +234,7 @@ void (*cons_show_roster_group)(const char * const group, GSList * list);
void (*cons_show_wins)(void); void (*cons_show_wins)(void);
void (*cons_show_status)(const char * const barejid); void (*cons_show_status)(const char * const barejid);
void (*cons_show_info)(PContact pcontact); void (*cons_show_info)(PContact pcontact);
void (*cons_show_caps)(const char * const contact, Resource *resource); void (*cons_show_caps)(const char * const fulljid, Resource *resource);
void (*cons_show_themes)(GSList *themes); void (*cons_show_themes)(GSList *themes);
void (*cons_show_aliases)(GList *aliases); void (*cons_show_aliases)(GList *aliases);
void (*cons_show_login_success)(ProfAccount *account); void (*cons_show_login_success)(ProfAccount *account);

View File

@ -235,9 +235,10 @@ win_show_info(ProfWin *window, PContact contact)
} }
win_save_newline(window); win_save_newline(window);
if (resource->caps_str != NULL) { Jid *jidp = jid_create_from_bare_and_resource(barejid, resource->name);
Capabilities *caps = caps_get(resource->caps_str); Capabilities *caps = caps_lookup(jidp->fulljid);
if (caps != NULL) {
if (caps) {
// show identity // show identity
if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) { if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) {
win_save_print(window, '-', NULL, NO_EOL, 0, "", " Identity: "); win_save_print(window, '-', NULL, NO_EOL, 0, "", " Identity: ");
@ -277,7 +278,6 @@ win_show_info(ProfWin *window, PContact contact)
win_save_newline(window); win_save_newline(window);
} }
} }
}
ordered_resources = g_list_next(ordered_resources); ordered_resources = g_list_next(ordered_resources);
} }

View File

@ -50,6 +50,7 @@
#include "xmpp/form.h" #include "xmpp/form.h"
static GHashTable *capabilities; static GHashTable *capabilities;
static GHashTable *jid_lookup;
static void _caps_destroy(Capabilities *caps); static void _caps_destroy(Capabilities *caps);
@ -58,6 +59,7 @@ caps_init(void)
{ {
capabilities = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, capabilities = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)_caps_destroy); (GDestroyNotify)_caps_destroy);
jid_lookup = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
} }
void void
@ -66,6 +68,12 @@ caps_add(const char * const ver, Capabilities *caps)
g_hash_table_insert(capabilities, strdup(ver), caps); g_hash_table_insert(capabilities, strdup(ver), caps);
} }
void
caps_map(const char * const jid, const char * const ver)
{
g_hash_table_insert(jid_lookup, strdup(jid), strdup(ver));
}
gboolean gboolean
caps_contains(const char * const caps_ver) caps_contains(const char * const caps_ver)
{ {
@ -78,6 +86,20 @@ _caps_get(const char * const caps_str)
return g_hash_table_lookup(capabilities, caps_str); return g_hash_table_lookup(capabilities, caps_str);
} }
static Capabilities *
_caps_lookup(const char * const jid)
{
char *ver = g_hash_table_lookup(jid_lookup, jid);
if (ver) {
Capabilities *caps = g_hash_table_lookup(capabilities, ver);
if (caps) {
return caps;
}
}
return NULL;
}
char * char *
caps_create_sha1_str(xmpp_stanza_t * const query) caps_create_sha1_str(xmpp_stanza_t * const query)
{ {
@ -365,6 +387,7 @@ static void
_caps_close(void) _caps_close(void)
{ {
g_hash_table_destroy(capabilities); g_hash_table_destroy(capabilities);
g_hash_table_destroy(jid_lookup);
} }
static void static void
@ -389,5 +412,6 @@ void
capabilities_init_module(void) capabilities_init_module(void)
{ {
caps_get = _caps_get; caps_get = _caps_get;
caps_lookup = _caps_lookup;
caps_close = _caps_close; caps_close = _caps_close;
} }

View File

@ -41,6 +41,7 @@
void caps_init(void); void caps_init(void);
void caps_add(const char * const ver, Capabilities *caps); void caps_add(const char * const ver, Capabilities *caps);
void caps_map(const char * const jid, const char * const ver);
gboolean caps_contains(const char * const caps_ver); gboolean caps_contains(const char * const caps_ver);
char* caps_create_sha1_str(xmpp_stanza_t * const query); char* caps_create_sha1_str(xmpp_stanza_t * const query);
xmpp_stanza_t* caps_create_query_response_stanza(xmpp_ctx_t * const ctx); xmpp_stanza_t* caps_create_query_response_stanza(xmpp_ctx_t * const ctx);

View File

@ -348,6 +348,12 @@ _caps_response_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza,
log_info("Capabilities response handler fired"); log_info("Capabilities response handler fired");
} }
const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
if (!from) {
log_info("No from attribute");
return 0;
}
char *node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE); char *node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE);
if (node == NULL) { if (node == NULL) {
log_warning("No node attribute found"); log_warning("No node attribute found");
@ -367,12 +373,14 @@ _caps_response_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza,
log_info("Valid SHA-1 hash found: %s", given_sha1); log_info("Valid SHA-1 hash found: %s", given_sha1);
if (caps_contains(given_sha1)) { if (caps_contains(given_sha1)) {
log_info("Capabilties cached"); log_info("Capabilties cached: %s", given_sha1);
} else { } else {
log_info("Capabilities not cached, storing"); log_info("Capabilities not cached: %s, storing", given_sha1);
Capabilities *capabilities = caps_create(query); Capabilities *capabilities = caps_create(query);
caps_add(given_sha1, capabilities); caps_add(given_sha1, capabilities);
} }
caps_map(from, given_sha1);
} }
g_free(generated_sha1); g_free(generated_sha1);

View File

@ -592,19 +592,17 @@ _available_handler(xmpp_conn_t * const conn,
log_info("Hash %s supported"); log_info("Hash %s supported");
char *ver = stanza_get_caps_ver(stanza); char *ver = stanza_get_caps_ver(stanza);
if (ver) {
if (caps_contains(ver)) { if (caps_contains(ver)) {
log_info("Capabilities cached"); log_info("Capabilities cached: %s", ver);
caps_map(from, ver);
} else { } else {
log_info("Capabilities not cached, sending service discovery request"); log_info("Capabilities not cached: %s, sending service discovery request", ver);
char *node = stanza_caps_get_node(stanza); char *node = stanza_caps_get_node(stanza);
char *id = create_unique_id("caps"); char *id = create_unique_id("caps");
iq_send_caps_request(from, id, node, ver); iq_send_caps_request(from, id, node, ver);
}
// send service discovery request
// with id handler to validate response,
// generate hash,
// if match, cache against hash
} }
// no hash, or not supported // no hash, or not supported
@ -619,17 +617,14 @@ _available_handler(xmpp_conn_t * const conn,
} }
} }
char *caps_key = strdup("hello");
// create Resource // create Resource
Resource *resource = NULL; Resource *resource = NULL;
resource_presence_t presence = resource_presence_from_string(show_str); resource_presence_t presence = resource_presence_from_string(show_str);
if (from_jid->resourcepart == NULL) { // hack for servers that do not send full jid if (from_jid->resourcepart == NULL) { // hack for servers that do not send full jid
resource = resource_new("__prof_default", presence, status_str, priority, caps_key); resource = resource_new("__prof_default", presence, status_str, priority);
} else { } else {
resource = resource_new(from_jid->resourcepart, presence, status_str, priority, caps_key); resource = resource_new(from_jid->resourcepart, presence, status_str, priority);
} }
free(caps_key);
free(status_str); free(status_str);
free(show_str); free(show_str);

View File

@ -194,6 +194,7 @@ void (*iq_send_caps_request)(const char * const to, const char * const id,
// caps functions // caps functions
Capabilities* (*caps_get)(const char * const caps_str); Capabilities* (*caps_get)(const char * const caps_str);
Capabilities* (*caps_lookup)(const char * const jid);
void (*caps_close)(void); void (*caps_close)(void);
gboolean (*bookmark_add)(const char *jid, const char *nick, const char *password, const char *autojoin_str); gboolean (*bookmark_add)(const char *jid, const char *nick, const char *password, const char *autojoin_str);