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

Fix devicelist subscription and handle pubsub event

Devicelist subscription can be done directly with caps_add feature.
This commit is contained in:
Paul Fariello 2019-02-22 19:57:26 +01:40
parent 79bb5016c2
commit 756fefb09a
9 changed files with 106 additions and 44 deletions

View File

@ -11,6 +11,8 @@
#include "omemo/crypto.h" #include "omemo/crypto.h"
#include "omemo/omemo.h" #include "omemo/omemo.h"
#include "ui/ui.h" #include "ui/ui.h"
#include "xmpp/xmpp.h"
#include "xmpp/connection.h"
#include "xmpp/omemo.h" #include "xmpp/omemo.h"
static gboolean loaded; static gboolean loaded;
@ -23,7 +25,7 @@ struct omemo_context_t {
pthread_mutex_t lock; pthread_mutex_t lock;
signal_context *signal; signal_context *signal;
uint32_t device_id; uint32_t device_id;
GList *device_list; GHashTable *device_list;
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;
@ -72,14 +74,23 @@ omemo_init(void)
signal_context_set_locking_functions(omemo_ctx.signal, lock, unlock); signal_context_set_locking_functions(omemo_ctx.signal, lock, unlock);
loaded = FALSE; loaded = FALSE;
omemo_ctx.device_list = NULL; omemo_ctx.device_list = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_list_free);
} }
void void
omemo_generate_crypto_materials(ProfAccount *account) omemo_generate_crypto_materials(ProfAccount *account)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx();
char *barejid = xmpp_jid_bare(ctx, session_get_account_name());
GList *device_list = g_hash_table_lookup(omemo_ctx.device_list, barejid);
g_hash_table_steal(omemo_ctx.device_list, barejid);
omemo_ctx.device_id = randombytes_uniform(0x80000000); omemo_ctx.device_id = randombytes_uniform(0x80000000);
omemo_ctx.device_list = g_list_append(omemo_ctx.device_list, GINT_TO_POINTER(omemo_ctx.device_id));
device_list = g_list_append(device_list, GINT_TO_POINTER(omemo_ctx.device_id));
g_hash_table_insert(omemo_ctx.device_list, strdup(barejid), device_list);
signal_protocol_key_helper_generate_identity_key_pair(&omemo_ctx.identity_key_pair, omemo_ctx.signal); signal_protocol_key_helper_generate_identity_key_pair(&omemo_ctx.identity_key_pair, omemo_ctx.signal);
signal_protocol_key_helper_generate_registration_id(&omemo_ctx.registration_id, 0, omemo_ctx.signal); signal_protocol_key_helper_generate_registration_id(&omemo_ctx.registration_id, 0, omemo_ctx.signal);
signal_protocol_key_helper_generate_pre_keys(&omemo_ctx.pre_keys_head, randombytes_random(), 100, omemo_ctx.signal); signal_protocol_key_helper_generate_pre_keys(&omemo_ctx.pre_keys_head, randombytes_random(), 100, omemo_ctx.signal);
@ -91,7 +102,7 @@ omemo_generate_crypto_materials(ProfAccount *account)
loaded = TRUE; loaded = TRUE;
omemo_devicelist_publish(); omemo_devicelist_publish(device_list);
omemo_bundle_publish(); omemo_bundle_publish();
} }
@ -107,12 +118,6 @@ omemo_loaded(void)
return loaded; return loaded;
} }
GList * const
omemo_device_list(void)
{
return omemo_ctx.device_list;
}
uint32_t uint32_t
omemo_device_id(void) omemo_device_id(void)
{ {
@ -167,6 +172,13 @@ omemo_prekeys(GList ** const prekeys, GList ** const ids, GList ** const lengths
} }
} }
void
omemo_set_device_list(const char *const jid, GList * const device_list)
{
/* TODO handle self device_list to ensure we still are on the list */
g_hash_table_insert(omemo_ctx.device_list, strdup(jid), device_list);
}
static void static void
lock(void *user_data) lock(void *user_data)
{ {

View File

@ -1,3 +1,5 @@
#include <glib.h>
#include "config/account.h" #include "config/account.h"
typedef struct omemo_context_t omemo_context; typedef struct omemo_context_t omemo_context;
@ -5,12 +7,12 @@ typedef struct omemo_context_t omemo_context;
void omemo_init(void); void omemo_init(void);
void omemo_generate_crypto_materials(ProfAccount *account); void omemo_generate_crypto_materials(ProfAccount *account);
GList * const omemo_device_list(void);
uint32_t omemo_device_id(void); uint32_t omemo_device_id(void);
void omemo_identity_key(unsigned char **output, size_t *length); void omemo_identity_key(unsigned char **output, size_t *length);
void omemo_signed_prekey(unsigned char **output, size_t *length); void omemo_signed_prekey(unsigned char **output, size_t *length);
void omemo_signed_prekey_signature(unsigned char **output, size_t *length); void omemo_signed_prekey_signature(unsigned char **output, size_t *length);
void omemo_prekeys(GList ** const prekeys, GList ** const ids, GList ** const lengths); void omemo_prekeys(GList ** const prekeys, GList ** const ids, GList ** const lengths);
void omemo_set_device_list(const char *const jid, GList * const device_list);
void omemo_start_session(ProfAccount *account, char *barejid); void omemo_start_session(ProfAccount *account, char *barejid);
gboolean omemo_loaded(void); gboolean omemo_loaded(void);

View File

@ -76,7 +76,6 @@ static void _handel_muc_user(xmpp_stanza_t *const stanza);
static void _handle_conference(xmpp_stanza_t *const stanza); static void _handle_conference(xmpp_stanza_t *const stanza);
static void _handle_captcha(xmpp_stanza_t *const stanza); static void _handle_captcha(xmpp_stanza_t *const stanza);
static void _handle_receipt_received(xmpp_stanza_t *const stanza); static void _handle_receipt_received(xmpp_stanza_t *const stanza);
static void _handle_pubsub_event(xmpp_stanza_t *const stanza);
static void _handle_chat(xmpp_stanza_t *const stanza); static void _handle_chat(xmpp_stanza_t *const stanza);
static void _send_message_stanza(xmpp_stanza_t *const stanza); static void _send_message_stanza(xmpp_stanza_t *const stanza);
@ -129,7 +128,20 @@ _message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *con
xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT); xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT);
if (event) { if (event) {
_handle_pubsub_event(event); xmpp_stanza_t *child = xmpp_stanza_get_children(event);
if (child) {
const char *node = xmpp_stanza_get_attribute(child, STANZA_ATTR_NODE);
if (node) {
ProfMessageHandler *handler = g_hash_table_lookup(pubsub_event_handlers, node);
if (handler) {
int keep = handler->func(stanza, handler->userdata);
if (!keep) {
free(handler);
g_hash_table_remove(pubsub_event_handlers, node);
}
}
}
}
} }
_handle_chat(stanza); _handle_chat(stanza);
@ -665,25 +677,6 @@ _handle_receipt_received(xmpp_stanza_t *const stanza)
jid_destroy(jidp); jid_destroy(jidp);
} }
static void
_handle_pubsub_event(xmpp_stanza_t *const event)
{
xmpp_stanza_t *child = xmpp_stanza_get_children(event);
if (child) {
const char *node = xmpp_stanza_get_attribute(event, STANZA_ATTR_NODE);
if (node) {
ProfMessageHandler *handler = g_hash_table_lookup(pubsub_event_handlers, node);
if (handler) {
int keep = handler->func(event, handler->userdata);
if (!keep) {
free(handler);
g_hash_table_remove(pubsub_event_handlers, node);
}
}
}
}
}
void void
_receipt_request_handler(xmpp_stanza_t *const stanza) _receipt_request_handler(xmpp_stanza_t *const stanza)
{ {

View File

@ -1,30 +1,36 @@
#include <glib.h>
#include "xmpp/connection.h" #include "xmpp/connection.h"
#include "xmpp/message.h"
#include "xmpp/iq.h" #include "xmpp/iq.h"
#include "xmpp/stanza.h" #include "xmpp/stanza.h"
#include "omemo/omemo.h" #include "omemo/omemo.h"
static int _omemo_receive_devicelist(xmpp_stanza_t *const stanza, void *const userdata);
void void
omemo_devicelist_subscribe(void) omemo_devicelist_subscribe(void)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx(); message_pubsub_event_handler_add(STANZA_NS_OMEMO_DEVICELIST, _omemo_receive_devicelist, NULL, NULL);
char *barejid = xmpp_jid_bare(ctx, session_get_account_name());
xmpp_stanza_t *iq = stanza_create_omemo_devicelist_subscribe(ctx, barejid);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(barejid); caps_add_feature(XMPP_FEATURE_OMEMO_DEVICELIST_NOTIFY);
} }
void void
omemo_devicelist_publish(void) omemo_devicelist_publish(GList *device_list)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_stanza_t *iq = stanza_create_omemo_devicelist_publish(ctx, omemo_device_list()); xmpp_stanza_t *iq = stanza_create_omemo_devicelist_publish(ctx, device_list);
iq_send_stanza(iq); iq_send_stanza(iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
} }
void
omemo_devicelist_fetch(void)
{
}
void void
omemo_bundle_publish(void) omemo_bundle_publish(void)
{ {
@ -53,3 +59,47 @@ omemo_bundle_publish(void)
free(signed_prekey); free(signed_prekey);
free(signed_prekey_signature); free(signed_prekey_signature);
} }
void
omemo_bundles_fetch(const char * const jid)
{
}
static int
_omemo_receive_devicelist(xmpp_stanza_t *const stanza, void *const userdata)
{
GList *device_list = NULL;
const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
if (!from) {
return 1;
}
xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT);
if (!event) {
return 1;
}
xmpp_stanza_t *items = xmpp_stanza_get_child_by_name(event, "items");
if (!items) {
return 1;
}
xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(items, "item");
if (!item) {
return 1;
}
xmpp_stanza_t *list = xmpp_stanza_get_child_by_ns(item, STANZA_NS_OMEMO);
if (!list) {
return 1;
}
xmpp_stanza_t *device;
for (device = xmpp_stanza_get_children(list); device != NULL; device = xmpp_stanza_get_next(device)) {
const char *id = xmpp_stanza_get_id(device);
device_list = g_list_append(device_list, GINT_TO_POINTER(strtoul(id, NULL, 10)));
}
omemo_set_device_list(from, device_list);
return 1;
}

View File

@ -1,3 +1,5 @@
#include <glib.h>
void omemo_devicelist_subscribe(void); void omemo_devicelist_subscribe(void);
void omemo_devicelist_publish(void); void omemo_devicelist_publish(GList *device_list);
void omemo_bundle_publish(void); void omemo_bundle_publish(void);

View File

@ -321,7 +321,7 @@ session_login_success(gboolean secured)
#ifdef HAVE_OMEMO #ifdef HAVE_OMEMO
omemo_devicelist_subscribe(); omemo_devicelist_subscribe();
if (omemo_loaded()) { if (omemo_loaded()) {
omemo_devicelist_publish(); /* TODO: update devicelist */
} }
#endif #endif

View File

@ -2105,7 +2105,7 @@ stanza_create_omemo_devicelist_subscribe(xmpp_ctx_t *ctx, const char *const jid)
xmpp_stanza_t *subscribe = xmpp_stanza_new(ctx); xmpp_stanza_t *subscribe = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(subscribe, STANZA_NAME_SUBSCRIBE); xmpp_stanza_set_name(subscribe, STANZA_NAME_SUBSCRIBE);
xmpp_stanza_set_attribute(subscribe, "node", "eu.siacs.conversations.axolotl.devicelist"); xmpp_stanza_set_attribute(subscribe, "node", STANZA_NS_OMEMO_DEVICELIST);
xmpp_stanza_set_attribute(subscribe, "jid", jid); xmpp_stanza_set_attribute(subscribe, "jid", jid);
xmpp_stanza_add_child(pubsub, subscribe); xmpp_stanza_add_child(pubsub, subscribe);
@ -2130,7 +2130,7 @@ stanza_create_omemo_devicelist_publish(xmpp_ctx_t *ctx, GList *const ids)
xmpp_stanza_t *publish = xmpp_stanza_new(ctx); xmpp_stanza_t *publish = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH); xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH);
xmpp_stanza_set_attribute(publish, "node", "eu.siacs.conversations.axolotl.devicelist"); xmpp_stanza_set_attribute(publish, "node", STANZA_NS_OMEMO_DEVICELIST);
xmpp_stanza_t *item = xmpp_stanza_new(ctx); xmpp_stanza_t *item = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(item, STANZA_NAME_ITEM); xmpp_stanza_set_name(item, STANZA_NAME_ITEM);

View File

@ -191,6 +191,8 @@
#define STANZA_NS_X_OOB "jabber:x:oob" #define STANZA_NS_X_OOB "jabber:x:oob"
#define STANZA_NS_BLOCKING "urn:xmpp:blocking" #define STANZA_NS_BLOCKING "urn:xmpp:blocking"
#define STANZA_NS_COMMAND "http://jabber.org/protocol/commands" #define STANZA_NS_COMMAND "http://jabber.org/protocol/commands"
#define STANZA_NS_OMEMO "eu.siacs.conversations.axolotl"
#define STANZA_NS_OMEMO_DEVICELIST "eu.siacs.conversations.axolotl.devicelist"
#define STANZA_DATAFORM_SOFTWARE "urn:xmpp:dataforms:softwareinfo" #define STANZA_DATAFORM_SOFTWARE "urn:xmpp:dataforms:softwareinfo"

View File

@ -61,6 +61,7 @@
#define XMPP_FEATURE_LASTACTIVITY "jabber:iq:last" #define XMPP_FEATURE_LASTACTIVITY "jabber:iq:last"
#define XMPP_FEATURE_MUC "http://jabber.org/protocol/muc" #define XMPP_FEATURE_MUC "http://jabber.org/protocol/muc"
#define XMPP_FEATURE_COMMANDS "http://jabber.org/protocol/commands" #define XMPP_FEATURE_COMMANDS "http://jabber.org/protocol/commands"
#define XMPP_FEATURE_OMEMO_DEVICELIST_NOTIFY "eu.siacs.conversations.axolotl.devicelist+notify"
typedef enum { typedef enum {
JABBER_CONNECTING, JABBER_CONNECTING,