From 19359045e94badf6992f6ab83ca3fbdeb5797ec6 Mon Sep 17 00:00:00 2001 From: LDA Date: Fri, 23 Aug 2024 22:19:49 +0200 Subject: [PATCH] [MOD/FIX] Leaves, fix leaks --- src/Room.c | 4 -- src/Routes.c | 2 +- src/Routes/RouteActRoom.c | 88 ++++++++++++++++++++++++++++----------- src/User.c | 14 ++++++- 4 files changed, 77 insertions(+), 31 deletions(-) diff --git a/src/Room.c b/src/Room.c index 9ccf416..8ea40ca 100644 --- a/src/Room.c +++ b/src/Room.c @@ -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); diff --git a/src/Routes.c b/src/Routes.c index 2ac8f70..52d801e 100644 --- a/src/Routes.c +++ b/src/Routes.c @@ -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); diff --git a/src/Routes/RouteActRoom.c b/src/Routes/RouteActRoom.c index a8b1996..3782af7 100644 --- a/src/Routes/RouteActRoom.c +++ b/src/Routes/RouteActRoom.c @@ -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)) { diff --git a/src/User.c b/src/User.c index 2cec874..e1eee00 100644 --- a/src/User.c +++ b/src/User.c @@ -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())); }