[MOD/FIX] Leaves, fix leaks
Some checks failed
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Has been cancelled
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Has been cancelled

This commit is contained in:
LDA 2024-08-23 22:19:49 +02:00
parent fde6f092b4
commit 19359045e9
4 changed files with 77 additions and 31 deletions

View file

@ -2341,10 +2341,6 @@ RoomJoin(Room *room, User *user)
pdu = RoomEventSend(room, event);
ret = !!pdu;
if (ret)
{
UserAddJoin(user, room->id);
}
end:
UserIdFree(userId);
JsonFree(event);

View file

@ -81,7 +81,7 @@ RouterBuild(void)
R("/_matrix/client/v3/rooms/(.*)/state/(.*)/(.*)", RouteSendState);
R("/_matrix/client/v3/rooms/(.*)/state/(.*)", RouteSendState);
R("/_matrix/client/v3/rooms/(.*)/event/(.*)", RouteFetchEvent);
R("/_matrix/client/v3/rooms/(.*)/join", RouteJoinRoom);
R("/_matrix/client/v3/rooms/(.*)/(join|leave)", RouteJoinRoom);
R("/_matrix/client/v3/rooms/(.*)/(kick|ban)", RouteKickRoom);
R("/_matrix/client/v3/rooms/(.*)/messages", RouteMessages);

View file

@ -51,6 +51,7 @@ ROUTE_IMPL(RouteJoinRoom, path, argp)
CommonID *id = NULL;
char *roomId = ArrayGet(path, 0);
char *action = ArrayGet(path, 1);
char *sender = NULL, *serverName = NULL;
Room *room = NULL;
@ -107,37 +108,75 @@ ROUTE_IMPL(RouteJoinRoom, path, argp)
room = RoomLock(db, roomId);
if (!room)
{
err = "Room ID does not exist.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNKNOWN, err);
goto finish;
}
if (RoomContainsUser(room, sender))
if (StrEquals(action, "join"))
{
err = "User is already in the room.";
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
response = MatrixErrorCreate(M_FORBIDDEN, err);
goto finish;
}
if (!RoomCanJoin(room, sender))
{
err = "User cannot be in the room.";
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
response = MatrixErrorCreate(M_FORBIDDEN, err);
goto finish;
}
/* TODO: Custom reason parameter. */
if (!RoomJoin(room, user))
{
err = "User could not be the room due to unknown reasons.";
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, err);
goto finish;
}
if (RoomContainsUser(room, sender))
{
err = "User is already in the room.";
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
response = MatrixErrorCreate(M_FORBIDDEN, err);
goto finish;
}
if (!RoomCanJoin(room, sender))
{
err = "User cannot be in the room.";
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
response = MatrixErrorCreate(M_FORBIDDEN, err);
goto finish;
}
/* TODO: Custom reason parameter. */
if (!RoomJoin(room, user))
{
err = "User could not be the room due to unknown reasons.";
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, err);
goto finish;
}
response = HashMapCreate();
JsonSet(response, JsonValueString(roomId), 1, "room_id");
response = HashMapCreate();
JsonSet(response, JsonValueString(roomId), 1, "room_id");
}
else if (StrEquals(action, "leave"))
{
HashMap *membership, *content;
HashMap *pduResponse;
if (!RoomContainsUser(room, sender))
{
err = "User is not already in the room.";
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
response = MatrixErrorCreate(M_FORBIDDEN, err);
goto finish;
}
content = HashMapCreate();
membership = RoomEventCreate(
sender, "m.room.member", sender,
content
);
HashMapSet(content, "membership", JsonValueString("leave"));
pduResponse = RoomEventSend(room, membership);
if (!pduResponse)
{
err = "Couldn't accept leave event";
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, err);
JsonFree(membership);
goto finish;
}
JsonFree(pduResponse);
JsonFree(membership);
response = HashMapCreate();
}
finish:
UserIdFree(id);
@ -237,7 +276,6 @@ ROUTE_IMPL(RouteKickRoom, path, argp)
id->sigil = '@';
sender = ParserRecomposeCommonID(*id);
Log(LOG_INFO, "sender=%s", sender);
room = RoomLock(db, roomId);
if (!RoomContainsUser(room, sender))
{

View file

@ -1038,6 +1038,13 @@ UserAddInvite(User *user, char *roomId)
return;
}
if (!user->inviteRef)
{
user->inviteRef = DbCreate(user->db,
3, "users", user->name, "invites"
);
}
data = DbJson(user->inviteRef);
JsonValueFree(HashMapSet(data, roomId, JsonValueNull()));
@ -1087,9 +1094,14 @@ UserAddJoin(User *user, char *roomId)
return;
}
if (!user->joinRef)
{
user->joinRef = DbCreate(user->db, 3, "users", user->name, "joins");
}
data = DbJson(user->joinRef);
if (!HashMapGet(data, roomId))
if (data && !HashMapGet(data, roomId))
{
JsonValueFree(HashMapSet(data, roomId, JsonValueNull()));
}