mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-23 01:46:21 +00:00
add connection_get_jid()
Use a singleton `Jid` inside the connection instead of always re-creating a `Jid` from the same string. Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
@@ -68,6 +68,7 @@ typedef struct prof_conn_t
|
||||
char* presence_message;
|
||||
int priority;
|
||||
char* domain;
|
||||
Jid* jid;
|
||||
GHashTable* available_resources;
|
||||
GHashTable* features_by_jid;
|
||||
GHashTable* requested_features;
|
||||
@@ -135,6 +136,7 @@ connection_init(void)
|
||||
conn.conn_last_event = XMPP_CONN_DISCONNECT;
|
||||
conn.presence_message = NULL;
|
||||
conn.domain = NULL;
|
||||
conn.jid = NULL;
|
||||
conn.features_by_jid = NULL;
|
||||
conn.available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)resource_destroy);
|
||||
conn.requested_features = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
|
||||
@@ -562,6 +564,9 @@ connection_disconnect(void)
|
||||
|
||||
g_free(prof_identifier);
|
||||
prof_identifier = NULL;
|
||||
|
||||
jid_destroy(conn.jid);
|
||||
conn.jid = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -724,17 +729,34 @@ connection_get_fulljid(void)
|
||||
}
|
||||
}
|
||||
|
||||
char*
|
||||
const Jid*
|
||||
connection_get_jid(void)
|
||||
{
|
||||
static const char* fulljid;
|
||||
const char* cur_fulljid = connection_get_fulljid();
|
||||
if (!conn.jid || cur_fulljid != fulljid) {
|
||||
fulljid = cur_fulljid;
|
||||
if (conn.jid)
|
||||
jid_destroy(conn.jid);
|
||||
conn.jid = jid_create(fulljid);
|
||||
if (!conn.jid) {
|
||||
log_error("Could not create connection-wide JID object from \"%s\"", STR_MAYBE_NULL(fulljid));
|
||||
/* In case we failed to create the jid or we're not yet
|
||||
* connected (or whatever else could fail) return the
|
||||
* pointer to a zero-initialized static object, so
|
||||
* de-referencing that pointer won't fail.
|
||||
*/
|
||||
static const Jid jid = { 0 };
|
||||
return &jid;
|
||||
}
|
||||
}
|
||||
return conn.jid;
|
||||
}
|
||||
|
||||
const char*
|
||||
connection_get_barejid(void)
|
||||
{
|
||||
const char* jid = connection_get_fulljid();
|
||||
if (!jid)
|
||||
return NULL;
|
||||
|
||||
auto_jid Jid* jidp = jid_create(jid);
|
||||
char* result = strdup(jidp->barejid);
|
||||
|
||||
return result;
|
||||
return connection_get_jid()->barejid;
|
||||
}
|
||||
|
||||
char*
|
||||
@@ -953,9 +975,8 @@ _connection_handler(xmpp_conn_t* const xmpp_conn, const xmpp_conn_event_t status
|
||||
log_debug("Connection handler: XMPP_CONN_CONNECT");
|
||||
conn.conn_status = JABBER_CONNECTED;
|
||||
|
||||
Jid* my_jid = jid_create(xmpp_conn_get_jid(conn.xmpp_conn));
|
||||
conn.domain = strdup(my_jid->domainpart);
|
||||
jid_destroy(my_jid);
|
||||
connection_get_jid();
|
||||
conn.domain = strdup(conn.jid->domainpart);
|
||||
|
||||
connection_clear_data();
|
||||
conn.features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_hash_table_destroy);
|
||||
@@ -979,10 +1000,10 @@ _connection_handler(xmpp_conn_t* const xmpp_conn, const xmpp_conn_event_t status
|
||||
log_debug("Connection handler: XMPP_CONN_RAW_CONNECT");
|
||||
conn.conn_status = JABBER_RAW_CONNECTED;
|
||||
|
||||
Jid* my_raw_jid = jid_create(xmpp_conn_get_jid(conn.xmpp_conn));
|
||||
log_debug("jid: %s", xmpp_conn_get_jid(conn.xmpp_conn));
|
||||
conn.domain = strdup(my_raw_jid->domainpart);
|
||||
jid_destroy(my_raw_jid);
|
||||
connection_get_jid();
|
||||
conn.jid = jid_create(xmpp_conn_get_jid(conn.xmpp_conn));
|
||||
log_debug("jid: %s", conn.jid->str);
|
||||
conn.domain = strdup(conn.jid->domainpart);
|
||||
|
||||
connection_clear_data();
|
||||
conn.features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_hash_table_destroy);
|
||||
|
||||
@@ -101,9 +101,7 @@ static GHashTable* pubsub_event_handlers;
|
||||
gchar*
|
||||
get_display_name(const ProfMessage* const message, int* flags)
|
||||
{
|
||||
auto_char char* barejid = connection_get_barejid();
|
||||
|
||||
if (g_strcmp0(barejid, message->from_jid->barejid) == 0) {
|
||||
if (g_strcmp0(connection_get_barejid(), message->from_jid->barejid) == 0) {
|
||||
return g_strdup("me");
|
||||
} else {
|
||||
if (flags)
|
||||
@@ -261,11 +259,10 @@ _message_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* con
|
||||
if (carbons) {
|
||||
|
||||
// carbon must come from ourselves
|
||||
auto_char char* mybarejid = connection_get_barejid();
|
||||
const char* const stanza_from = xmpp_stanza_get_from(stanza);
|
||||
|
||||
if (stanza_from) {
|
||||
if (g_strcmp0(mybarejid, stanza_from) != 0) {
|
||||
if (g_strcmp0(connection_get_barejid(), stanza_from) != 0) {
|
||||
log_warning("Invalid carbon received, from: %s", stanza_from);
|
||||
msg_stanza = NULL;
|
||||
} else {
|
||||
@@ -1464,10 +1461,8 @@ _handle_chat(xmpp_stanza_t* const stanza, gboolean is_mam, gboolean is_carbon, c
|
||||
|
||||
if (message->plain || message->body || message->encrypted) {
|
||||
if (is_carbon) {
|
||||
auto_char char* mybarejid = connection_get_barejid();
|
||||
|
||||
// if we are the recipient, treat as standard incoming message
|
||||
if (g_strcmp0(mybarejid, message->to_jid->barejid) == 0) {
|
||||
if (g_strcmp0(connection_get_barejid(), message->to_jid->barejid) == 0) {
|
||||
sv_ev_incoming_carbon(message);
|
||||
// else treat as a sent message
|
||||
} else {
|
||||
|
||||
@@ -882,8 +882,7 @@ muc_members_add(const char* const room, const char* const jid)
|
||||
if (g_hash_table_insert(chat_room->members, strdup(jid), NULL)) {
|
||||
#ifdef HAVE_OMEMO
|
||||
if (chat_room->anonymity_type == MUC_ANONYMITY_TYPE_NONANONYMOUS) {
|
||||
auto_char char* our_barejid = connection_get_barejid();
|
||||
if (strcmp(jid, our_barejid) != 0) {
|
||||
if (strcmp(jid, connection_get_barejid()) != 0) {
|
||||
omemo_start_session(jid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,9 +87,8 @@ omemo_devicelist_configure(void)
|
||||
{
|
||||
xmpp_ctx_t* const ctx = connection_get_ctx();
|
||||
auto_char char* id = connection_create_stanza_id();
|
||||
auto_jid Jid* jid = jid_create(connection_get_fulljid());
|
||||
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_request(ctx, id, jid->barejid, STANZA_NS_OMEMO_DEVICELIST);
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_request(ctx, id, connection_get_jid()->barejid, STANZA_NS_OMEMO_DEVICELIST);
|
||||
|
||||
iq_id_handler_add(id, _omemo_devicelist_configure_submit, NULL, NULL);
|
||||
|
||||
@@ -196,9 +195,7 @@ omemo_start_device_session_handle_bundle(xmpp_stanza_t* const stanza, void* cons
|
||||
}
|
||||
|
||||
if (!from_attr) {
|
||||
Jid* jid = jid_create(connection_get_fulljid());
|
||||
from = strdup(jid->barejid);
|
||||
jid_destroy(jid);
|
||||
from = strdup(connection_get_jid()->barejid);
|
||||
} else {
|
||||
from = strdup(from_attr);
|
||||
}
|
||||
@@ -608,9 +605,8 @@ _omemo_devicelist_configure_submit(xmpp_stanza_t* const stanza, void* const user
|
||||
form_set_value(form, "pubsub#access_model", "open");
|
||||
|
||||
xmpp_ctx_t* const ctx = connection_get_ctx();
|
||||
auto_jid Jid* jid = jid_create(connection_get_fulljid());
|
||||
auto_char char* id = connection_create_stanza_id();
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_submit(ctx, id, jid->barejid, STANZA_NS_OMEMO_DEVICELIST, form);
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_submit(ctx, id, connection_get_jid()->barejid, STANZA_NS_OMEMO_DEVICELIST, form);
|
||||
|
||||
iq_id_handler_add(id, _omemo_devicelist_configure_result, NULL, NULL);
|
||||
|
||||
@@ -633,10 +629,7 @@ _omemo_devicelist_configure_result(xmpp_stanza_t* const stanza, void* const user
|
||||
log_debug("[OMEMO] node configured");
|
||||
|
||||
// Try to publish
|
||||
char* barejid = connection_get_barejid();
|
||||
omemo_devicelist_request(barejid);
|
||||
|
||||
free(barejid);
|
||||
omemo_devicelist_request(connection_get_barejid());
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -660,12 +653,11 @@ _omemo_bundle_publish_result(xmpp_stanza_t* const stanza, void* const userdata)
|
||||
|
||||
log_debug("[OMEMO] cannot publish bundle with open access model, trying to configure node");
|
||||
xmpp_ctx_t* const ctx = connection_get_ctx();
|
||||
auto_jid Jid* jid = jid_create(connection_get_fulljid());
|
||||
auto_char char* id = connection_create_stanza_id();
|
||||
auto_gchar gchar* node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, omemo_device_id());
|
||||
log_debug("[OMEMO] node: %s", node);
|
||||
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_request(ctx, id, jid->barejid, node);
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_request(ctx, id, connection_get_jid()->barejid, node);
|
||||
|
||||
iq_id_handler_add(id, _omemo_bundle_publish_configure, NULL, userdata);
|
||||
|
||||
@@ -700,10 +692,9 @@ _omemo_bundle_publish_configure(xmpp_stanza_t* const stanza, void* const userdat
|
||||
form_set_value(form, "pubsub#access_model", "open");
|
||||
|
||||
xmpp_ctx_t* const ctx = connection_get_ctx();
|
||||
auto_jid Jid* jid = jid_create(connection_get_fulljid());
|
||||
auto_char char* id = connection_create_stanza_id();
|
||||
auto_gchar gchar* node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, omemo_device_id());
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_submit(ctx, id, jid->barejid, node, form);
|
||||
xmpp_stanza_t* iq = stanza_create_pubsub_configure_submit(ctx, id, connection_get_jid()->barejid, node, form);
|
||||
|
||||
iq_id_handler_add(id, _omemo_bundle_publish_configure_result, NULL, userdata);
|
||||
|
||||
|
||||
@@ -200,9 +200,8 @@ roster_set_handler(xmpp_stanza_t* const stanza)
|
||||
}
|
||||
|
||||
// if from attribute exists and it is not current users barejid, ignore push
|
||||
auto_char char* mybarejid = connection_get_barejid();
|
||||
const char* from = xmpp_stanza_get_from(stanza);
|
||||
if (from && (strcmp(from, mybarejid) != 0)) {
|
||||
if (from && (strcmp(from, connection_get_barejid()) != 0)) {
|
||||
log_warning("Received alleged roster push from: %s", from);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -193,7 +193,8 @@ jabber_conn_status_t connection_get_status(void);
|
||||
char* connection_get_presence_msg(void);
|
||||
void connection_set_presence_msg(const char* const message);
|
||||
const char* connection_get_fulljid(void);
|
||||
char* connection_get_barejid(void);
|
||||
const Jid* connection_get_jid(void);
|
||||
const char* connection_get_barejid(void);
|
||||
char* connection_get_user(void);
|
||||
char* connection_create_uuid(void);
|
||||
void connection_free_uuid(char* uuid);
|
||||
|
||||
Reference in New Issue
Block a user