Compare commits

..

2 commits

Author SHA1 Message Date
lda
cacc72bf84 [ADD/WIP] Basic preset code
Some checks are pending
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run
I still need to find a solution to a problem related to j2s.
Right now I kind of want to have a way for users to send events
from the endpoints.
2024-05-19 15:17:56 +02:00
lda
a99798a312 [ADD/WIP] Start abstracting aliasing 2024-05-19 00:30:28 +02:00
4 changed files with 249 additions and 74 deletions

View file

@ -88,6 +88,11 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s)
JsonValue *val;
size_t i;
char *join_rules_preset = NULL;
char *history_visibility_preset = NULL;
char *guest_access_preset = NULL;
/* TODO: Manage trusted private chat */
sender.sigil = '@';
sender.local = UserGetName(user);
sender.server = s;
@ -135,15 +140,58 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s)
switch (req->preset)
{
case ROOM_CREATE_PUBLIC:
/* TODO */
join_rules_preset = "public";
history_visibility_preset = "shared";
guest_access_preset = "forbidden";
break;
case ROOM_CREATE_TRUSTED:
/* TODO */
break;
/* Fallthrough */
case ROOM_CREATE_PRIVATE:
/* TODO */
join_rules_preset = "invite";
history_visibility_preset = "shared";
guest_access_preset = "can_join";
break;
default:
switch (req->visibility)
{
case ROOM_PUBLIC:
join_rules_preset = "public";
history_visibility_preset = "shared";
guest_access_preset = "forbidden";
break;
case ROOM_PRIVATE:
join_rules_preset = "invite";
history_visibility_preset = "shared";
guest_access_preset = "can_join";
break;
}
break;
}
/* Write out presets */
#define SetIfExistent(pset) do { \
if (pset##_preset) \
{ \
content = HashMapCreate(); \
JsonSet( \
content, \
JsonValueString(join_rules_preset) \
, 1, #pset); \
event = RoomEventCreate( \
sender_str, \
"m.room." #pset, "", content); \
JsonFree(RoomEventSend(room, event)); \
JsonFree(event); \
} \
} \
while (0)
SetIfExistent(join_rules);
SetIfExistent(history_visibility);
SetIfExistent(guest_access);
/* TODO: On a trusted room, set invitees PLs to the owner's. */
#undef SetIfExistent
/* User-provided initial states */
initial_states = req->initial_state;
@ -181,55 +229,19 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s)
}
/* Custom alias */
if (req->room_alias_name)
{
DbRef *aliasesRef = DbLock(room->db, 1, "aliases");
HashMap *aliasesJson;
if (!aliasesRef)
{
aliasesRef = DbCreate(room->db, 1, "aliases");
}
if (aliasesRef)
if (req->room_alias_name && !RoomResolveAlias(room->db, room->id))
{
CommonID full;
char *fullStr, *serverStr;
HashMap *alias, *idObject;
Array *servers, *ids;
full.sigil = '#';
full.local = req->room_alias_name;
full.server = s;
aliasesJson = DbJson(aliasesRef);
fullStr = ParserRecomposeCommonID(full);
servers = ArrayCreate();
serverStr = ParserRecomposeServerPart(room->creator);
ArrayAdd(servers, JsonValueString(serverStr));
Free(serverStr);
alias = HashMapCreate();
HashMapSet(alias, "createdBy", JsonValueString(sender_str));
HashMapSet(alias, "id", JsonValueString(room->id));
HashMapSet(alias, "servers", JsonValueArray(servers));
JsonValueFree(JsonSet(
aliasesJson, JsonValueObject(alias), 2,
"aliases", fullStr));
if (!(idObject =
JsonValueAsObject(JsonGet(aliasesJson, 2, "id", room->id))))
{
ids = ArrayCreate();
idObject = HashMapCreate();
HashMapSet(idObject, "aliases", JsonValueArray(ids));
JsonSet(
aliasesJson,
JsonValueObject(idObject),
2, "id", room->id);
}
ids = JsonValueAsArray(HashMapGet(idObject, "aliases"));
ArrayAdd(ids, JsonValueString(fullStr));
RoomAddAlias(room->db, fullStr, room->id, sender_str, serverStr);
content = HashMapCreate();
JsonSet(content, JsonValueString(fullStr), 1, "alias");
@ -240,8 +252,7 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s)
JsonFree(event);
Free(fullStr);
DbUnlock(room->db, aliasesRef);
}
Free(serverStr);
}
/* TODO: The rest of the events mandated by the specification on
* POST /createRoom, and error management. */
@ -582,9 +593,8 @@ PopulateEventV1(Room * room, HashMap * event, PduV1 * pdu, ServerPart serv)
JsonDuplicate(JsonValueAsObject(JsonGet(event, 1, "content")));
pdu->room_id = StrDuplicate(room->id);
pdu->signatures = HashMapCreate();
pdu->depth = RoomGetDepth(room) + 1; /* TODO: Clamp this value. */
pdu->depth = pdu->depth >= (int64_t) INT64_MAX ?
(int64_t) INT64_MAX : pdu->depth;
pdu->depth = RoomGetDepth(room) + 1;
pdu->depth = pdu->depth >= INT64_MAX ? INT64_MAX : pdu->depth;
/* Create a random event ID for this PDU.
* TODO: Optionally verify whenever it's already used, but that
@ -1804,3 +1814,129 @@ RoomGetDepth(Room *room)
return max;
}
void
RoomAddAlias(Db *db, char *roomAlias, char *roomId, char *sender, char *serv)
{
DbRef *aliasRef;
HashMap *json, *alias, *idObject;
Array *servers, *ids;
if (!db || !roomAlias || !roomId || !sender || !serv)
{
return;
}
aliasRef = DbLock(db, 1, "aliases");
if (!aliasRef)
{
aliasRef = DbCreate(db, 1, "aliases");
if (!aliasRef)
{
return;
}
}
json = DbJson(aliasRef);
servers = ArrayCreate();
ArrayAdd(servers, JsonValueString(serv));
/* alias => ID */
alias = HashMapCreate();
JsonSet(alias, JsonValueString(roomId), 1, "id");
JsonSet(alias, JsonValueString(sender), 1, "createdBy");
JsonSet(alias, JsonValueArray(servers), 1, "servers");
JsonValueFree(
JsonSet(json, JsonValueObject(alias), 2, "aliases", roomAlias));
/* ID => alias(es) */
if (!(idObject =
JsonValueAsObject(JsonGet(json, 2, "id", roomId))))
{
ids = ArrayCreate();
idObject = HashMapCreate();
HashMapSet(idObject, "aliases", JsonValueArray(ids));
JsonSet(
json,
JsonValueObject(idObject),
2, "id", roomId);
}
ids = JsonValueAsArray(HashMapGet(idObject, "aliases"));
ArrayAdd(ids, JsonValueString(roomAlias));
DbUnlock(db, aliasRef);
}
char *
RoomResolveAlias(Db *db, char *roomAlias)
{
DbRef *aliasRef;
HashMap *json;
char *ret;
if (!db || !roomAlias)
{
return NULL;
}
aliasRef = DbLock(db, 1, "aliases");
if (!aliasRef)
{
return NULL;
}
json = DbJson(aliasRef);
ret = JsonValueAsString(JsonGet(json, 3, "aliases", roomAlias, "id"));
ret = StrDuplicate(ret);
DbUnlock(db, aliasRef);
return ret;
}
Array *
RoomReverseAlias(Db *db, char *roomId)
{
DbRef *aliasRef;
HashMap *json, *idObject;
Array *ret, *original;
size_t i;
if (!db || !roomId)
{
return NULL;
}
aliasRef = DbLock(db, 1, "aliases");
if (!aliasRef)
{
return NULL;
}
json = DbJson(aliasRef);
if (!(idObject =
JsonValueAsObject(JsonGet(json, 2, "id", roomId))))
{
DbUnlock(db, aliasRef);
return NULL;
}
original = JsonValueAsArray(HashMapGet(idObject, "aliases"));
ret = ArrayCreate();
for (i = 0; i < ArraySize(original); i++)
{
JsonValue *v = ArrayGet(original, i);
ArrayAdd(ret, StrDuplicate(JsonValueAsString(v)));
}
DbUnlock(db, aliasRef);
return ret;
}
void
RoomFreeReverse(Array *arr)
{
size_t i;
if (!arr)
{
return;
}
for (i = 0; i < ArraySize(arr); i++)
{
Free(ArrayGet(arr, i));
}
ArrayFree(arr);
}

View file

@ -81,6 +81,18 @@ ROUTE_IMPL(RouteCreateRoom, path, argp)
goto finish;
}
/* j2s hack: Setting to a default value like that.
* It's definitely NOT good(you can't rely on enums to
* store values outside their limits in the C standard,
* and this line will need to be changed everytime the
* preset list is changed).
*
* I do believe a decent solution to this would be to
* add a 'default' type(maybe initialised to 0 so that
* memsets would work as intended), that *wouldn't* be
* returned by j2s itself.
* TODO. */
parsed.preset = ROOM_CREATE_PRIVATE + 1;
if (!RoomCreateRequestFromJson(request, &parsed, &err))
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);

View file

@ -31,6 +31,7 @@
#include <Matrix.h>
#include <User.h>
#include <Room.h>
ROUTE_IMPL(RouteRoomAliases, path, argp)
{
@ -40,16 +41,15 @@ ROUTE_IMPL(RouteRoomAliases, path, argp)
char *msg;
HashMap *response = NULL;
HashMap *aliases = NULL;
HashMap *reversealias = NULL;
JsonValue *val;
Array *alias = NULL, *arr;
Db *db = args->matrixArgs->db;
DbRef *ref = NULL;
User *user = NULL;
size_t i;
if (HttpRequestMethodGet(args->context) != HTTP_GET)
{
msg = "Route only accepts GET.";
@ -82,10 +82,9 @@ ROUTE_IMPL(RouteRoomAliases, path, argp)
goto finish;
}
ref = DbLock(db, 1, "aliases");
aliases = DbJson(ref);
reversealias = JsonValueAsObject(JsonGet(aliases, 2, "id", roomId));
if (!reversealias)
alias = RoomReverseAlias(db, roomId);
if (!alias)
{
/* We do not know about the room ID. */
msg = "Unknown room ID.";
@ -93,12 +92,17 @@ ROUTE_IMPL(RouteRoomAliases, path, argp)
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
arr = ArrayCreate();
for (i = 0; i < ArraySize(alias); i++)
{
char *str = ArrayGet(alias, i);
ArrayAdd(arr, JsonValueString(str));
}
response = HashMapCreate();
val = JsonGet(reversealias, 1, "aliases");
HashMapSet(response, "aliases", JsonValueDuplicate(val));
HashMapSet(response, "aliases", JsonValueArray(arr));
finish:
DbUnlock(db, ref);
RoomFreeReverse(alias);
UserUnlock(user);
return response;
}

View file

@ -176,4 +176,27 @@ extern HashMap * RoomEventCreate(char *, char *, char *, HashMap *);
*/
extern uint64_t RoomGetDepth(Room *);
/**
* Tries to find an alias from room alias to an ID stored
* on the heap, or NULL if it does not exist.
*/
extern char * RoomResolveAlias(Db *, char *);
/**
* Tries to resolve a list of aliases from a room ID into
* an array of strings stored on the heap.
*/
extern Array * RoomReverseAlias(Db *, char *);
/**
* Frees the array returned by
* .Fn RoomReverseAlias .
*/
extern void RoomFreeReverse(Array *);
/**
* Adds or overwrites a room alias.
*/
extern void RoomAddAlias(Db *, char *, char *, char *, char *);
#endif /* TELODENDRIA_ROOM_H */