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

Ensure devicelist access model is configured before updating it

This commit is contained in:
Paul Fariello 2021-07-06 17:47:33 +02:00
parent 4641537721
commit 46fe69de9b
3 changed files with 97 additions and 25 deletions

View File

@ -386,10 +386,12 @@ omemo_publish_crypto_materials(void)
char* barejid = connection_get_barejid();
/* Ensure device list is properly configured */
omemo_devicelist_configure_and_request();
/* Ensure we get our current device list, and it gets updated with our
* device_id */
g_hash_table_insert(omemo_ctx.device_list_handler, strdup(barejid), _handle_own_device_list);
omemo_devicelist_request(barejid);
omemo_bundle_publish(true);

View File

@ -47,12 +47,13 @@
#include "omemo/omemo.h"
static int _omemo_receive_devicelist(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_devicelist_publish_result(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_devicelist_configure(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_devicelist_configure_result(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_bundle_publish_result(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_bundle_publish_configure(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_bundle_publish_configure_result(xmpp_stanza_t* const stanza, void* const userdata);
static int _omemo_device_list_publish_result(xmpp_stanza_t* const stanza, void* const userdata);
void
omemo_devicelist_subscribe(void)
{
@ -69,16 +70,30 @@ omemo_devicelist_publish(GList* device_list)
log_debug("[OMEMO] publish device list");
if (connection_supports(XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS)) {
stanza_attach_publish_options(ctx, iq, "pubsub#access_model", "open");
}
iq_id_handler_add(xmpp_stanza_get_id(iq), _omemo_device_list_publish_result, NULL, NULL);
iq_id_handler_add(xmpp_stanza_get_id(iq), _omemo_devicelist_publish_result, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
omemo_devicelist_configure_and_request(void)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
char* id = connection_create_stanza_id();
Jid* jid = jid_create(connection_get_fulljid());
xmpp_stanza_t* iq = stanza_create_pubsub_configure_request(ctx, id, jid->barejid, STANZA_NS_OMEMO_DEVICELIST);
iq_id_handler_add(id, _omemo_devicelist_configure, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(id);
jid_destroy(jid);
}
void
omemo_devicelist_request(const char* const jid)
{
@ -512,6 +527,76 @@ _omemo_receive_devicelist(xmpp_stanza_t* const stanza, void* const userdata)
return 1;
}
static int
_omemo_devicelist_publish_result(xmpp_stanza_t* const stanza, void* const userdata)
{
const char* type = xmpp_stanza_get_type(stanza);
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
cons_show_error("Unable to publish own OMEMO device list");
log_error("[OMEMO] Publishing device list failed");
return 0;
}
return 0;
}
static int
_omemo_devicelist_configure(xmpp_stanza_t* const stanza, void* const userdata)
{
log_debug("[OMEMO] _omemo_devicelist_configure()");
xmpp_stanza_t* pubsub = xmpp_stanza_get_child_by_name(stanza, "pubsub");
if (!pubsub) {
log_error("[OMEMO] The stanza doesn't contain a 'pubsub' child");
return 0;
}
xmpp_stanza_t* configure = xmpp_stanza_get_child_by_name(pubsub, STANZA_NAME_CONFIGURE);
if (!configure) {
log_error("[OMEMO] The stanza doesn't contain a 'configure' child");
return 0;
}
xmpp_stanza_t* x = xmpp_stanza_get_child_by_name(configure, "x");
if (!x) {
log_error("[OMEMO] The stanza doesn't contain an 'x' child");
return 0;
}
DataForm* form = form_create(x);
form_set_value(form, "pubsub#access_model", "open");
xmpp_ctx_t* const ctx = connection_get_ctx();
Jid* jid = jid_create(connection_get_fulljid());
char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_pubsub_configure_submit(ctx, id, jid->barejid, STANZA_NS_OMEMO_DEVICELIST, form);
iq_id_handler_add(id, _omemo_devicelist_configure_result, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(id);
jid_destroy(jid);
return 0;
}
static int
_omemo_devicelist_configure_result(xmpp_stanza_t* const stanza, void* const userdata)
{
const char* type = xmpp_stanza_get_type(stanza);
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
log_error("[OMEMO] cannot configure device list to an open access model: Result error");
return 0;
}
log_debug("[OMEMO] node configured");
// Try to publish
char* barejid = connection_get_barejid();
omemo_devicelist_request(barejid);
return 0;
}
static int
_omemo_bundle_publish_result(xmpp_stanza_t* const stanza, void* const userdata)
@ -572,11 +657,7 @@ _omemo_bundle_publish_configure(xmpp_stanza_t* const stanza, void* const userdat
}
DataForm* form = form_create(x);
char* tag = g_hash_table_lookup(form->var_to_tag, "pubsub#access_model");
if (!tag) {
log_error("[OMEMO] cannot configure bundle to an open access model");
return 0;
}
form_set_value(form, "pubsub#access_model", "open");
xmpp_ctx_t* const ctx = connection_get_ctx();
Jid* jid = jid_create(connection_get_fulljid());
@ -612,15 +693,3 @@ _omemo_bundle_publish_configure_result(xmpp_stanza_t* const stanza, void* const
return 0;
}
static int
_omemo_device_list_publish_result(xmpp_stanza_t* const stanza, void* const userdata)
{
const char* type = xmpp_stanza_get_type(stanza);
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
cons_show_error("Unable to publish own OMEMO device list");
log_error("[OMEMO] Publishing device list failed");
return 0;
}
return 0;
}

View File

@ -38,6 +38,7 @@
#include "xmpp/iq.h"
void omemo_devicelist_subscribe(void);
void omemo_devicelist_configure_and_request(void);
void omemo_devicelist_publish(GList* device_list);
void omemo_devicelist_request(const char* const jid);
void omemo_bundle_publish(gboolean first);