1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Handle requested device list for session start

This commit is contained in:
Paul Fariello 2019-03-04 07:38:32 +01:40
parent 421d1b1560
commit 20ed86c58c

View File

@ -28,6 +28,10 @@ static void omemo_generate_short_term_crypto_materials(ProfAccount *account);
static void lock(void *user_data); static void lock(void *user_data);
static void unlock(void *user_data); static void unlock(void *user_data);
static void omemo_log(int level, const char *message, size_t len, void *user_data); static void omemo_log(int level, const char *message, size_t len, void *user_data);
static gboolean handle_own_device_list(const char *const jid, GList *device_list);
static gboolean handle_device_list_start_session(const char *const jid, GList *device_list);
typedef gboolean (*OmemoDeviceListHandler)(const char *const jid, GList *device_list);
struct omemo_context_t { struct omemo_context_t {
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
@ -35,6 +39,7 @@ struct omemo_context_t {
signal_context *signal; signal_context *signal;
uint32_t device_id; uint32_t device_id;
GHashTable *device_list; GHashTable *device_list;
GHashTable *device_list_handler;
ratchet_identity_key_pair *identity_key_pair; ratchet_identity_key_pair *identity_key_pair;
uint32_t registration_id; uint32_t registration_id;
signal_protocol_key_helper_pre_key_list_node *pre_keys_head; signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
@ -145,6 +150,7 @@ omemo_init(void)
loaded = FALSE; loaded = FALSE;
omemo_ctx.device_list = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_list_free); omemo_ctx.device_list = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_list_free);
omemo_ctx.device_list_handler = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
} }
void void
@ -253,7 +259,9 @@ omemo_generate_short_term_crypto_materials(ProfAccount *account)
* device_id */ * device_id */
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
char *barejid = xmpp_jid_bare(ctx, session_get_account_name()); char *barejid = xmpp_jid_bare(ctx, session_get_account_name());
g_hash_table_insert(omemo_ctx.device_list_handler, strdup(barejid), handle_own_device_list);
omemo_devicelist_request(barejid); omemo_devicelist_request(barejid);
free(barejid);
omemo_bundle_publish(); omemo_bundle_publish();
} }
@ -264,7 +272,7 @@ omemo_start_session(const char *const barejid)
GList *device_list = g_hash_table_lookup(omemo_ctx.device_list, barejid); GList *device_list = g_hash_table_lookup(omemo_ctx.device_list, barejid);
if (!device_list) { if (!device_list) {
omemo_devicelist_request(barejid); omemo_devicelist_request(barejid);
/* TODO handle response */ g_hash_table_insert(omemo_ctx.device_list_handler, strdup(barejid), handle_device_list_start_session);
return; return;
} }
@ -338,16 +346,19 @@ void
omemo_set_device_list(const char *const jid, GList * device_list) omemo_set_device_list(const char *const jid, GList * device_list)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
char *barejid = xmpp_jid_bare(ctx, session_get_account_name()); char *barejid = xmpp_jid_bare(ctx, jid);
if (g_strcmp0(jid, barejid) == 0) { g_hash_table_insert(omemo_ctx.device_list, strdup(barejid), device_list);
if (!g_list_find(device_list, GINT_TO_POINTER(omemo_ctx.device_id))) {
device_list = g_list_append(device_list, GINT_TO_POINTER(omemo_ctx.device_id)); OmemoDeviceListHandler handler = g_hash_table_lookup(omemo_ctx.device_list_handler, barejid);
omemo_devicelist_publish(device_list); if (handler) {
gboolean keep = handler(barejid, device_list);
if (!keep) {
g_hash_table_remove(omemo_ctx.device_list_handler, barejid);
} }
} }
g_hash_table_insert(omemo_ctx.device_list, strdup(jid), device_list); free(barejid);
} }
void void
@ -535,3 +546,26 @@ omemo_log(int level, const char *message, size_t len, void *user_data)
{ {
cons_show(message); cons_show(message);
} }
static gboolean
handle_own_device_list(const char *const jid, GList *device_list)
{
if (!g_list_find(device_list, GINT_TO_POINTER(omemo_ctx.device_id))) {
gpointer original_jid;
g_hash_table_steal_extended(omemo_ctx.device_list, jid, &original_jid, NULL);
free(original_jid);
device_list = g_list_append(device_list, GINT_TO_POINTER(omemo_ctx.device_id));
g_hash_table_insert(omemo_ctx.device_list, strdup(jid), device_list);
omemo_devicelist_publish(device_list);
}
return TRUE;
}
static gboolean
handle_device_list_start_session(const char *const jid, GList *device_list)
{
omemo_start_session(jid);
return FALSE;
}