initial implementation of banning room IDs

takes a full room ID, evicts all our users from that room,
adds room ID to banned room IDs metadata db table, and
forbids any new local users from attempting to join it.

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-02-18 18:57:17 -05:00 committed by June
parent a92f291bbf
commit ed0c8e86f7
6 changed files with 189 additions and 3 deletions

View file

@ -27,14 +27,15 @@ use ruma::{
},
TimelineEventType,
},
EventId, OwnedRoomAliasId, OwnedRoomId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId,
EventId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomAliasId, RoomId, RoomVersionId,
ServerName, UserId,
};
use serde_json::value::to_raw_value;
use tokio::sync::{mpsc, Mutex};
use tracing::warn;
use tracing::{debug, error, warn};
use crate::{
api::client_server::{leave_all_rooms, AUTO_GEN_PASSWORD_LENGTH},
api::client_server::{leave_all_rooms, leave_room, AUTO_GEN_PASSWORD_LENGTH},
services,
utils::{self, HtmlEscape},
Error, PduEvent, Result,
@ -165,6 +166,20 @@ enum RoomCommand {
/// - List all rooms the server knows about
List { page: Option<usize> },
/// - Bans a room ID from local users joining and evicts all our local users from the room
BanRoomId {
#[arg(short, long)]
force: bool,
room_id: Box<RoomId>,
},
/// - Unbans a room ID to allow local users to join again
UnbanRoomId { room_id: Box<RoomId> },
/// - List of all rooms we have banned
ListBannedRooms,
#[command(subcommand)]
/// - Manage rooms' aliases
Alias(RoomAliasCommand),
@ -774,6 +789,103 @@ impl Service {
}
},
AdminCommand::Rooms(command) => match command {
RoomCommand::BanRoomId { force, room_id } => {
// basic syntax checks on room ID
if !&room_id.to_string().starts_with('!')
|| !&room_id.to_string().contains(':')
|| room_id.to_string().contains(char::is_whitespace)
{
return Ok(RoomMessageEventContent::text_plain("Invalid room ID specified. Please note that this requires a full room ID e.g. `!awIh6gGInaS5wLQJwa:example.com`"));
}
services().rooms.metadata.ban_room(&room_id, true)?;
debug!("Making all users leave the room {}", &room_id);
if force {
for local_user in services()
.rooms
.state_cache
.room_members(&room_id)
.filter_map(|user| {
user.ok().filter(|local_user| {
local_user.server_name() == services().globals.server_name()
})
})
.collect::<Vec<OwnedUserId>>()
{
debug!(
"Attempting leave for user {} in room {} (forced, ignoring all errors)",
&local_user, &room_id
);
let _ = leave_room(&local_user, &room_id, None).await;
}
} else {
for local_user in services()
.rooms
.state_cache
.room_members(&room_id)
.filter_map(|user| {
user.ok().filter(|local_user| {
local_user.server_name() == services().globals.server_name()
})
})
.collect::<Vec<OwnedUserId>>()
{
debug!(
"Attempting leave for user {} in room {}",
&local_user, &room_id
);
if let Err(e) = leave_room(&local_user, &room_id, None).await {
error!("Error attempting to make local user {} leave room {} during room banning: {}", &local_user, &room_id, e);
return Ok(RoomMessageEventContent::text_plain(format!("Error attempting to make local user {} leave room {} during room banning (room is still banned but not removing any more users): {}\nIf you would like to ignore errors, use --force", &local_user, &room_id, e)));
}
}
}
RoomMessageEventContent::text_plain("Room banned and removed all our local users, use disable-room to stop receiving new inbound federation events as well if needed.")
}
RoomCommand::UnbanRoomId { room_id } => {
services().rooms.metadata.ban_room(&room_id, false)?;
RoomMessageEventContent::text_plain("Room unbanned, you may need to re-enable federation with the room using enable-room if this is a remote room to make it fully functional.")
}
RoomCommand::ListBannedRooms => {
let rooms: Result<Vec<_>, _> =
services().rooms.metadata.list_banned_rooms().collect();
match rooms {
Ok(room_ids) => {
// TODO: add room name from our state cache if available, default to the room ID as the room name if we dont have it
// TODO: do same if we have a room alias for this
let plain_list =
room_ids.iter().fold(String::new(), |mut output, room_id| {
writeln!(output, "- `{}`", room_id).unwrap();
output
});
let html_list =
room_ids.iter().fold(String::new(), |mut output, room_id| {
writeln!(
output,
"<li><code>{}</code></li>",
escape_html(room_id.as_ref())
)
.unwrap();
output
});
let plain = format!("Rooms:\n{}", plain_list);
let html = format!("Rooms:\n<ul>{}</ul>", html_list);
RoomMessageEventContent::text_html(plain, html)
}
Err(e) => {
error!("Failed to list banned rooms: {}", e);
RoomMessageEventContent::text_plain(format!(
"Unable to list room aliases: {}",
e
))
}
}
}
RoomCommand::List { page } => {
// TODO: i know there's a way to do this with clap, but i can't seem to find it
let page = page.unwrap_or(1);