[MOD/WIP] Take into account room IDs

This might need a refactor.
This commit is contained in:
lda 2023-11-29 13:35:53 +01:00
parent 78daf86eb3
commit c1933a2184
2 changed files with 43 additions and 10 deletions

View file

@ -267,6 +267,26 @@ ParseCommonID(char *str, CommonID *id)
switch (sigil) 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 case '#': /* It seems like the localpart should be the same as the
user's: everything, except ':'. */ user's: everything, except ':'. */
case '@': case '@':
@ -280,7 +300,6 @@ ParseCommonID(char *str, CommonID *id)
id->local = NULL; id->local = NULL;
return 0; return 0;
} }
/* TODO: Match whenever str is valid. */
if (!ParseServerName(&str, &id->server)) if (!ParseServerName(&str, &id->server))
{ {
Free(id->local); Free(id->local);

View file

@ -41,7 +41,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
HashMap *response; HashMap *response;
Db *db = args->matrixArgs->db; Db *db = args->matrixArgs->db;
DbRef *ref; DbRef *ref = NULL;
HashMap *aliases; HashMap *aliases;
JsonValue *val; JsonValue *val;
@ -49,17 +49,23 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
char *msg; char *msg;
User *user = NULL; User *user = NULL;
CommonID commonID; CommonID aliasID;
CommonID roomID;
Config *config; Config *config;
commonID.sigil = '\0'; aliasID.sigil = '\0';
commonID.local = NULL; aliasID.local = NULL;
commonID.server.hostname = NULL; aliasID.server.hostname = NULL;
commonID.server.port = NULL; aliasID.server.port = NULL;
roomID.sigil = '\0';
roomID.local = NULL;
roomID.server.hostname = NULL;
roomID.server.port = NULL;
config = ConfigLock(db); config = ConfigLock(db);
if (!ParseCommonID(alias, &commonID) || commonID.sigil != '#') if (!ParseCommonID(alias, &aliasID) || aliasID.sigil != '#')
{ {
msg = "Invalid room alias."; msg = "Invalid room alias.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
@ -118,7 +124,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
/* Check for server name. /* Check for server name.
* TODO: Take the port into account, that might need a * TODO: Take the port into account, that might need a
* refactor for it to use a ServerPart */ * 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); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, "Invalid servername"); 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 /* TODO: Validate room ID to make sure it is well
* formed. */ * 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(); newAlias = HashMapCreate();
HashMapSet(newAlias, "createdBy", JsonValueString(UserGetName(user))); HashMapSet(newAlias, "createdBy", JsonValueString(UserGetName(user)));
@ -185,7 +198,8 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
} }
finish: finish:
CommonIDFree(commonID); CommonIDFree(aliasID);
CommonIDFree(roomID);
ConfigUnlock(config); ConfigUnlock(config);
UserUnlock(user); UserUnlock(user);
DbUnlock(db, ref); DbUnlock(db, ref);