[MOD] Make vcard-avatars XEP-0421 tier

This commit is contained in:
LDA 2024-07-21 16:31:32 +02:00
commit db10a70313
6 changed files with 89 additions and 4 deletions

View file

@ -42,6 +42,8 @@ TODO
we _might_ just manage to get some system that can at least emulate that, we _might_ just manage to get some system that can at least emulate that,
and hopefully be reasonably faster than the filesystem, with some added and hopefully be reasonably faster than the filesystem, with some added
reliability. reliability.
- You can always concat paths, and search by prefix with LMDB. ~512 bytes
a key is more than enough. I'll just need to write a good abstraction layer.
- Get rid of the '?'-syntax and use another invalid Matrix char/valid XMPP char - Get rid of the '?'-syntax and use another invalid Matrix char/valid XMPP char
('$'?) for escaped ('$'?) for escaped

View file

@ -75,6 +75,7 @@ Main(void)
Log(LOG_NOTICE, "Creating volatile tables..."); Log(LOG_NOTICE, "Creating volatile tables...");
ParseeInitialiseJIDTable(); ParseeInitialiseJIDTable();
ParseeInitialiseOIDTable();
ParseeInitialiseHeadTable(); ParseeInitialiseHeadTable();
Log(LOG_NOTICE, "Setting up local Matrix user..."); Log(LOG_NOTICE, "Setting up local Matrix user...");
@ -125,6 +126,7 @@ end:
CronStop(cron); CronStop(cron);
CronFree(cron); CronFree(cron);
ParseeFreeData(conf.handlerArgs); ParseeFreeData(conf.handlerArgs);
ParseeDestroyOIDTable();
ParseeDestroyHeadTable(); ParseeDestroyHeadTable();
ParseeDestroyJIDTable(); ParseeDestroyJIDTable();
return 0; return 0;

View file

@ -129,3 +129,66 @@ ParseeDestroyHeadTable(void)
pthread_mutex_unlock(&head_lock); pthread_mutex_unlock(&head_lock);
pthread_mutex_destroy(&head_lock); pthread_mutex_destroy(&head_lock);
} }
static pthread_mutex_t oid_lock;
static HashMap *oid_table = NULL;
void
ParseeInitialiseOIDTable(void)
{
if (oid_table)
{
return;
}
pthread_mutex_init(&oid_lock, NULL);
pthread_mutex_lock(&oid_lock);
oid_table = HashMapCreate();
pthread_mutex_unlock(&oid_lock);
}
void
ParseePushOIDTable(char *muc, char *bare)
{
if (!muc || !bare || !oid_table)
{
return;
}
pthread_mutex_lock(&oid_lock);
bare = StrDuplicate(bare);
Free(HashMapSet(oid_table, muc, bare));
pthread_mutex_unlock(&oid_lock);
}
char *
ParseeLookupOID(char *muc)
{
char *bare;
if (!muc || !oid_table)
{
return NULL;
}
pthread_mutex_lock(&oid_lock);
bare = StrDuplicate(HashMapGet(oid_table, muc));
pthread_mutex_unlock(&oid_lock);
return bare;
}
void
ParseeDestroyOIDTable(void)
{
char *key;
void *val;
if (!oid_table)
{
return;
}
pthread_mutex_lock(&oid_lock);
while (HashMapIterate(oid_table, &key, &val))
{
Free(val);
}
HashMapFree(oid_table);
oid_table = NULL;
pthread_mutex_unlock(&oid_lock);
pthread_mutex_destroy(&oid_lock);
}

View file

@ -5,6 +5,8 @@
#include <Cytoplasm/Log.h> #include <Cytoplasm/Log.h>
#include <Cytoplasm/Sha.h> #include <Cytoplasm/Sha.h>
#include <Parsee.h>
#include <string.h> #include <string.h>
char * char *
@ -193,7 +195,7 @@ char *
ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force) ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
{ {
char *user, *xmpp_from, *type; char *user, *xmpp_from, *type;
char *decode_from, *occ_id = NULL; char *decode_from, *occ_id = NULL, *looked = NULL;
bool is_chat, has_anon = false, is_anon = false; bool is_chat, has_anon = false, is_anon = false;
if (!data || !stanza) if (!data || !stanza)
@ -209,10 +211,10 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
if (!is_chat) if (!is_chat)
{ {
has_anon = ServerHasXEP421(data, xmpp_from); has_anon = ServerHasXEP421(data, xmpp_from);
is_anon = has_anon && StrEquals(xmpp_from, decode_from); is_anon = has_anon;// && StrEquals(xmpp_from, decode_from);
} }
if (is_anon) if (is_anon || force)
{ {
XMLElement *occupant; XMLElement *occupant;
occupant = XMLookForTKV( occupant = XMLookForTKV(
@ -220,6 +222,15 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
"xmlns", "urn:xmpp:occupant-id:0" "xmlns", "urn:xmpp:occupant-id:0"
); );
occ_id = occupant ? HashMapGet(occupant->attrs, "id") : NULL; occ_id = occupant ? HashMapGet(occupant->attrs, "id") : NULL;
if (!occ_id)
{
occ_id = ParseeLookupOID(xmpp_from);
looked = occ_id;
}
else
{
ParseePushOIDTable(xmpp_from, occ_id);
}
} }
if (!occ_id) if (!occ_id)
@ -230,9 +241,11 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
type ? is_chat : false type ? is_chat : false
); );
Free(decode_from); Free(decode_from);
Free(looked);
return user; return user;
} }
Free(decode_from); Free(decode_from);
Free(looked);
return ScrambleOID(data, occ_id); return ScrambleOID(data, occ_id);
} }

View file

@ -248,7 +248,7 @@ IQResult(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
/* TODO: More reliable system for telling the difference appart */ /* TODO: More reliable system for telling the difference appart */
if (jid && !StrEquals(from, resource)) if (jid && !StrEquals(from, resource))
{ {
from_matrix = ParseeEncodeJID(args->config, jid, false); from_matrix = ParseeGetBridgedUserI(args, stanza, jid);
ASSetAvatar(args->config, from_matrix, mxc); ASSetAvatar(args->config, from_matrix, mxc);
} }
else if (room) else if (room)

View file

@ -203,6 +203,11 @@ extern char * ParseeDMEventFromSID(ParseeData *d, char *r_id, char *ori_id);
/* Gets a Parsee "shim" link to an MXC, usable as unauth for a limited time */ /* Gets a Parsee "shim" link to an MXC, usable as unauth for a limited time */
extern char * ParseeToUnauth(ParseeData *data, char *mxc); extern char * ParseeToUnauth(ParseeData *data, char *mxc);
extern void ParseeInitialiseOIDTable(void);
extern void ParseePushOIDTable(char *muc, char *occupant);
extern char *ParseeLookupOID(char *muc);
extern void ParseeDestroyOIDTable(void);
extern void ParseeInitialiseJIDTable(void); extern void ParseeInitialiseJIDTable(void);
extern void ParseePushJIDTable(char *muc, char *bare); extern void ParseePushJIDTable(char *muc, char *bare);
extern char *ParseeLookupJID(char *muc); extern char *ParseeLookupJID(char *muc);