diff --git a/src/autojoin.rs b/src/autojoin.rs index d5f17c8..fd22e26 100644 --- a/src/autojoin.rs +++ b/src/autojoin.rs @@ -3,17 +3,19 @@ use std::time::Duration; use matrix_sdk::{ self, async_trait, events::{room::member::MemberEventContent, StrippedStateEvent}, + identifiers::RoomId, Client, EventEmitter, RoomState, }; use tokio::time::sleep; pub struct AutoJoinHandler { client: Client, + room_id: RoomId, } impl AutoJoinHandler { - pub fn new(client: Client) -> Self { - Self { client } + pub fn new(client: Client, room_id: RoomId) -> Self { + Self { client, room_id } } } @@ -30,7 +32,23 @@ impl EventEmitter for AutoJoinHandler { } if let RoomState::Invited(room) = room { - // TODO: only join room if it's the room specified in the configuration + let room_id = room.room_id(); + let room_name = room.display_name().await; + println!( + "Received invitation for room `{}`: `{}`", + room_id, room_name + ); + + if room_id != &self.room_id { + println!( + "Bot isn't authorized to join room `{}`, declining invitation", + room_id + ); + // leaving a room is equivalent to rejecting the invitation, as per + // https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-rooms-roomid-leave + self.client.leave_room(room_id).await.unwrap(); + return; + } println!("Autojoining room {}", room.room_id()); let mut delay = 2; diff --git a/src/bot.rs b/src/bot.rs index b4cbfb8..97f3312 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -32,7 +32,10 @@ impl BadNewsBot { load_or_init_session(&self).await?; self.client - .add_event_emitter(Box::new(AutoJoinHandler::new(self.client.clone()))) + .add_event_emitter(Box::new(AutoJoinHandler::new( + self.client.clone(), + self.config.room_id.clone(), + ))) .await; Ok(()) diff --git a/src/main.rs b/src/main.rs index b06f5d0..1b26d28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{ }; use clap::Clap; +use matrix_sdk::identifiers::RoomId; use serde::Deserialize; use thiserror::Error; use url::Url; @@ -41,6 +42,9 @@ pub struct Config { password: String, /// Path to a directory where the bot will store Matrix state and current session information. state_dir: PathBuf, + /// ID of the Matrix room where the bot should post messages. The bot will only accept + /// invitations to this room. + room_id: RoomId, } #[tokio::main]