From 56d348454e403bbb767e90064e0eb2fa5c794256 Mon Sep 17 00:00:00 2001 From: lda Date: Wed, 29 Nov 2023 22:42:23 +0100 Subject: [PATCH] [ADD] Implement barebones of GET /rooms/{id}/alias --- src/Parser.c | 16 ++++++++++++ src/Routes/RouteAliasDirectory.c | 12 +-------- src/Routes/RouteRoomAliases.c | 45 +++++++++++++++++++++++++++----- src/include/Parser.h | 7 ++++- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/Parser.c b/src/Parser.c index 0e5f1a9..f120578 100644 --- a/src/Parser.c +++ b/src/Parser.c @@ -327,3 +327,19 @@ CommonIDFree(CommonID id) Free(id.server.port); } } +int +ValidCommonID(char *str, char sigil) +{ + CommonID id; + int ret; + memset(&id, 0, sizeof(CommonID)); + if (!str) + { + return 0; + } + + ret = ParseCommonID(str, &id) && id.sigil == sigil; + + CommonIDFree(id); + return ret; +} diff --git a/src/Routes/RouteAliasDirectory.c b/src/Routes/RouteAliasDirectory.c index 620f7ca..fe442cc 100644 --- a/src/Routes/RouteAliasDirectory.c +++ b/src/Routes/RouteAliasDirectory.c @@ -53,7 +53,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) User *user = NULL; CommonID aliasID; - CommonID roomID; Config *config; aliasID.sigil = '\0'; @@ -61,16 +60,10 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) aliasID.server.hostname = NULL; aliasID.server.port = NULL; - roomID.sigil = '\0'; - roomID.local = NULL; - roomID.server.hostname = NULL; - roomID.server.port = NULL; - config = ConfigLock(db); if (!ParseCommonID(alias, &aliasID) || aliasID.sigil != '#') { - Log(LOG_INFO, "sigil: ' %c '", aliasID.sigil); msg = "Invalid room alias."; HttpResponseStatus(args->context, HTTP_BAD_REQUEST); response = MatrixErrorCreate(M_INVALID_PARAM, msg); @@ -161,7 +154,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) goto finish; } - if (!ParseCommonID(id, &roomID) || roomID.sigil != '!') + if (!ValidCommonID(id, '!')) { msg = "Invalid room ID."; HttpResponseStatus(args->context, HTTP_BAD_REQUEST); @@ -201,7 +194,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) } roomAlias = JsonValueAsObject(val); id = StrDuplicate(JsonValueAsString(HashMapGet(roomAlias, "id"))); - Log(LOG_INFO, "id: %s", id); if (!(UserGetPrivileges(user) & USER_ALIAS) && !StrEquals(UserGetName(user), JsonValueAsString(JsonGet(roomAlias, 1, "createdBy")))) { @@ -229,7 +221,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) } } Free(id); - } response = HashMapCreate(); @@ -242,7 +233,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) finish: CommonIDFree(aliasID); - CommonIDFree(roomID); ConfigUnlock(config); UserUnlock(user); DbUnlock(db, ref); diff --git a/src/Routes/RouteRoomAliases.c b/src/Routes/RouteRoomAliases.c index 25a080a..c41795f 100644 --- a/src/Routes/RouteRoomAliases.c +++ b/src/Routes/RouteRoomAliases.c @@ -25,25 +25,58 @@ #include +#include #include +#include + +#include +#include ROUTE_IMPL(RouteRoomAliases, path, argp) { RouteArgs *args = argp; - char *roomId = ArrayGet(path, 0); + char *roomId = HttpUrlDecode(ArrayGet(path, 0)); + char *token; + char *msg; - HashMap *request = NULL; HashMap *response = NULL; + HashMap *aliases = NULL; + HashMap *reversealias = NULL; + + JsonValue *val; Db *db = args->matrixArgs->db; DbRef *ref = NULL; - (void) roomId; + User *user = NULL; - /* TODO: Placeholder; remove. */ - goto finish; + /* TODO: Also check permissions. */ + response = MatrixGetAccessToken(args->context, &token); + if (response) + { + goto finish; + } + user = UserAuthenticate(db, token); + /* TODO: Check if user is authorised. */ + + ref = DbLock(db, 1, "aliases"); + aliases = DbJson(ref); + reversealias = JsonValueAsObject(JsonGet(aliases, 2, "id", roomId)); + if (!reversealias) + { + /* We do not know about the room ID. */ + msg = "Unknown room ID."; + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_INVALID_PARAM, msg); + goto finish; + } + + response = HashMapCreate(); + val = JsonGet(reversealias, 1, "aliases"); + HashMapSet(response, "aliases", JsonValueDuplicate(val)); finish: DbUnlock(db, ref); - JsonFree(request); + UserUnlock(user); + Free(roomId); return response; } diff --git a/src/include/Parser.h b/src/include/Parser.h index 68853b5..fb9ede5 100644 --- a/src/include/Parser.h +++ b/src/include/Parser.h @@ -58,9 +58,14 @@ typedef struct CommonID { * by the [matrix] specification. */ extern int ParseCommonID(char *, CommonID *); +/** + * Checks whenever the string is a valid common ID with the correct sigil. + */ +extern int ValidCommonID(char *, char); /** - * Frees a CommonID's values. Note that it doesn't free the CommonID itself. */ + * Frees a CommonID's values. Note that it doesn't free the CommonID itself. + */ extern void CommonIDFree(CommonID id);