From c1933a2184d6e9ddc65447b6cab852a15da01fa3 Mon Sep 17 00:00:00 2001 From: lda Date: Wed, 29 Nov 2023 13:35:53 +0100 Subject: [PATCH] [MOD/WIP] Take into account room IDs This might need a refactor. --- src/Parser.c | 21 ++++++++++++++++++++- src/Routes/RouteAliasDirectory.c | 32 +++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Parser.c b/src/Parser.c index e514c6d..0e5f1a9 100644 --- a/src/Parser.c +++ b/src/Parser.c @@ -267,6 +267,26 @@ ParseCommonID(char *str, CommonID *id) switch (sigil) { + case '$': + /* For event IDs, it depends on the version, so we're just + * accepting it all. */ + if (!ParseUserLocalpart(&str, &id->local)) + { + return 0; + } + if (*str == ':') + { + (*str)++; + if (!ParseServerName(&str, &id->server)) + { + Free(id->local); + id->local = NULL; + return 0; + } + return 1; + } + break; + case '!': case '#': /* It seems like the localpart should be the same as the user's: everything, except ':'. */ case '@': @@ -280,7 +300,6 @@ ParseCommonID(char *str, CommonID *id) id->local = NULL; return 0; } - /* TODO: Match whenever str is valid. */ if (!ParseServerName(&str, &id->server)) { Free(id->local); diff --git a/src/Routes/RouteAliasDirectory.c b/src/Routes/RouteAliasDirectory.c index 7a4772f..5d447f8 100644 --- a/src/Routes/RouteAliasDirectory.c +++ b/src/Routes/RouteAliasDirectory.c @@ -41,7 +41,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) HashMap *response; Db *db = args->matrixArgs->db; - DbRef *ref; + DbRef *ref = NULL; HashMap *aliases; JsonValue *val; @@ -49,17 +49,23 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) char *msg; User *user = NULL; - CommonID commonID; + CommonID aliasID; + CommonID roomID; Config *config; - commonID.sigil = '\0'; - commonID.local = NULL; - commonID.server.hostname = NULL; - commonID.server.port = NULL; + aliasID.sigil = '\0'; + aliasID.local = NULL; + 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, &commonID) || commonID.sigil != '#') + if (!ParseCommonID(alias, &aliasID) || aliasID.sigil != '#') { msg = "Invalid room alias."; HttpResponseStatus(args->context, HTTP_BAD_REQUEST); @@ -118,7 +124,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) /* Check for server name. * TODO: Take the port into account, that might need a * refactor for it to use a ServerPart */ - if (!StrEquals(commonID.server.hostname, config->serverName)) + if (!StrEquals(aliasID.server.hostname, config->serverName)) { HttpResponseStatus(args->context, HTTP_BAD_REQUEST); response = MatrixErrorCreate(M_INVALID_PARAM, "Invalid servername"); @@ -149,6 +155,13 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) /* TODO: Validate room ID to make sure it is well * formed. */ + if (!ParseCommonID(JsonValueAsString(HashMapGet(request, "room_id")), &roomID) || aliasID.sigil != '!') + { + msg = "Invalid room ID."; + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_INVALID_PARAM, msg); + goto finish; + } newAlias = HashMapCreate(); HashMapSet(newAlias, "createdBy", JsonValueString(UserGetName(user))); @@ -185,7 +198,8 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) } finish: - CommonIDFree(commonID); + CommonIDFree(aliasID); + CommonIDFree(roomID); ConfigUnlock(config); UserUnlock(user); DbUnlock(db, ref);