mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-24 09:16:21 +00:00
Added jid->hash lookup for capabilities
This commit is contained in:
@@ -50,6 +50,7 @@
|
||||
#include "xmpp/form.h"
|
||||
|
||||
static GHashTable *capabilities;
|
||||
static GHashTable *jid_lookup;
|
||||
|
||||
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,
|
||||
(GDestroyNotify)_caps_destroy);
|
||||
jid_lookup = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -66,6 +68,12 @@ caps_add(const char * const ver, Capabilities *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
|
||||
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);
|
||||
}
|
||||
|
||||
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 *
|
||||
caps_create_sha1_str(xmpp_stanza_t * const query)
|
||||
{
|
||||
@@ -365,6 +387,7 @@ static void
|
||||
_caps_close(void)
|
||||
{
|
||||
g_hash_table_destroy(capabilities);
|
||||
g_hash_table_destroy(jid_lookup);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -389,5 +412,6 @@ void
|
||||
capabilities_init_module(void)
|
||||
{
|
||||
caps_get = _caps_get;
|
||||
caps_lookup = _caps_lookup;
|
||||
caps_close = _caps_close;
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
|
||||
void caps_init(void);
|
||||
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);
|
||||
char* caps_create_sha1_str(xmpp_stanza_t * const query);
|
||||
xmpp_stanza_t* caps_create_query_response_stanza(xmpp_ctx_t * const ctx);
|
||||
|
||||
@@ -348,6 +348,12 @@ _caps_response_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza,
|
||||
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);
|
||||
if (node == NULL) {
|
||||
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);
|
||||
|
||||
if (caps_contains(given_sha1)) {
|
||||
log_info("Capabilties cached");
|
||||
log_info("Capabilties cached: %s", given_sha1);
|
||||
} else {
|
||||
log_info("Capabilities not cached, storing");
|
||||
log_info("Capabilities not cached: %s, storing", given_sha1);
|
||||
Capabilities *capabilities = caps_create(query);
|
||||
caps_add(given_sha1, capabilities);
|
||||
}
|
||||
|
||||
caps_map(from, given_sha1);
|
||||
}
|
||||
|
||||
g_free(generated_sha1);
|
||||
|
||||
@@ -592,19 +592,17 @@ _available_handler(xmpp_conn_t * const conn,
|
||||
log_info("Hash %s supported");
|
||||
|
||||
char *ver = stanza_get_caps_ver(stanza);
|
||||
if (caps_contains(ver)) {
|
||||
log_info("Capabilities cached");
|
||||
} else {
|
||||
log_info("Capabilities not cached, sending service discovery request");
|
||||
char *node = stanza_caps_get_node(stanza);
|
||||
char *id = create_unique_id("caps");
|
||||
if (ver) {
|
||||
if (caps_contains(ver)) {
|
||||
log_info("Capabilities cached: %s", ver);
|
||||
caps_map(from, ver);
|
||||
} else {
|
||||
log_info("Capabilities not cached: %s, sending service discovery request", ver);
|
||||
char *node = stanza_caps_get_node(stanza);
|
||||
char *id = create_unique_id("caps");
|
||||
|
||||
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
|
||||
iq_send_caps_request(from, id, node, ver);
|
||||
}
|
||||
}
|
||||
|
||||
// no hash, or not supported
|
||||
@@ -619,17 +617,14 @@ _available_handler(xmpp_conn_t * const conn,
|
||||
}
|
||||
}
|
||||
|
||||
char *caps_key = strdup("hello");
|
||||
|
||||
// create Resource
|
||||
Resource *resource = NULL;
|
||||
resource_presence_t presence = resource_presence_from_string(show_str);
|
||||
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 {
|
||||
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(show_str);
|
||||
|
||||
|
||||
@@ -194,6 +194,7 @@ void (*iq_send_caps_request)(const char * const to, const char * const id,
|
||||
|
||||
// caps functions
|
||||
Capabilities* (*caps_get)(const char * const caps_str);
|
||||
Capabilities* (*caps_lookup)(const char * const jid);
|
||||
void (*caps_close)(void);
|
||||
|
||||
gboolean (*bookmark_add)(const char *jid, const char *nick, const char *password, const char *autojoin_str);
|
||||
|
||||
Reference in New Issue
Block a user