[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

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

View file

@ -129,3 +129,66 @@ ParseeDestroyHeadTable(void)
pthread_mutex_unlock(&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/Sha.h>
#include <Parsee.h>
#include <string.h>
char *
@ -193,7 +195,7 @@ char *
ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
{
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;
if (!data || !stanza)
@ -209,10 +211,10 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
if (!is_chat)
{
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;
occupant = XMLookForTKV(
@ -220,6 +222,15 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
"xmlns", "urn:xmpp:occupant-id:0"
);
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)
@ -230,9 +241,11 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force)
type ? is_chat : false
);
Free(decode_from);
Free(looked);
return user;
}
Free(decode_from);
Free(looked);
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 */
if (jid && !StrEquals(from, resource))
{
from_matrix = ParseeEncodeJID(args->config, jid, false);
from_matrix = ParseeGetBridgedUserI(args, stanza, jid);
ASSetAvatar(args->config, from_matrix, mxc);
}
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 */
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 ParseePushJIDTable(char *muc, char *bare);
extern char *ParseeLookupJID(char *muc);