[ADD/WIP] Do leaves right, PLwerk

This commit is contained in:
LDA 2024-07-07 01:21:51 +02:00
commit 4d055f3688
3 changed files with 114 additions and 9 deletions

View file

@ -998,31 +998,87 @@ PresenceStanza(ParseeData *args, XMLElement *stanza)
if ((user_info = XMLookForTKV(stanza, "x", "xmlns", MUC_USER_NS)))
{
XMLElement *item = XMLookForUnique(user_info, "item");
XMLElement *status = XMLookForUnique(user_info, "status");
#define IsStatus(code) (status && \
StrEquals(HashMapGet(status->attrs, "code"), #code))
char *jid = item ? HashMapGet(item->attrs, "jid") : NULL;
char *from;
char *type = HashMapGet(stanza->attrs, "type");
char *room = ParseeGetBridgedRoom(args, stanza);
char *decode_from = ParseeLookupJID(oid);
char *from_matrix = ParseeDecodeMXID(decode_from);
char *affiliation = HashMapGet(item->attrs, "affiliation");
int power_level = 0;
if (!from_matrix || *from_matrix != '@')
{
Free(from_matrix);
from_matrix = ParseeEncodeJID(args->config, decode_from, false);
}
/* TODO: Use affiliation to set PL */
if (StrEquals(affiliation, "owner"))
{
power_level = 98;
}
else if (StrEquals(affiliation, "admin"))
{
power_level = 51;
}
else if (StrEquals(affiliation, "member"))
{
power_level = 0;
}
else if (StrEquals(affiliation, "outcast"))
{
power_level = -1;
}
/* Set the user's PL */
if (room)
{
HashMap *powers = ASGetPL(args->config, room);
int64_t level = GrabInteger(powers, 2, "users", from_matrix);
if (powers && level != power_level)
{
HashMap *users = GrabObject(powers, 1, "users");
JsonValue *val = JsonValueInteger(power_level);
JsonValueFree(HashMapSet(users,
from_matrix, val
));
ASSetPL(args->config, room, powers);
}
else
{
JsonFree(powers);
}
}
if (StrEquals(type, "unavailable"))
{
/* TODO: Treat as a ban if the role is outcast.
* Modify the code to be more accurate later. */
char *room = ParseeGetBridgedRoom(args, stanza);
char *decode_from = ParseeLookupJID(oid);
char *from_matrix = ParseeDecodeMXID(decode_from);
char *affiliation = HashMapGet(item->attrs, "affiliation");
if (StrEquals(affiliation, "outcast"))
/* If not an MXID, use the Parsee user */
if (IsStatus(301))
{
ASBan(args->config, room, from_matrix);
}
else
else if (IsStatus(307))
{
ASKick(args->config, room, from_matrix);
}
else if (IsStatus(303))
{
/* TODO: This is a nick change. */
Log(LOG_WARNING, "Nick change not implemented.");
}
else
{
ASLeave(args->config, room, from_matrix);
}
Free(decode_from);
Free(from_matrix);
Free(room);
}
if (jid)
@ -1030,7 +1086,11 @@ PresenceStanza(ParseeData *args, XMLElement *stanza)
ParseePushJIDTable(oid, jid);
}
from = StrConcat(2, "parsee@", args->config->component_host);
Free(from);
Free(decode_from);
Free(from_matrix);
Free(room);
}
else if (vc)
{