diff --git a/src/bot.rs b/src/bot.rs index e5e967a..9c2e998 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -21,8 +21,9 @@ use tokio::{signal::ctrl_c, sync::mpsc}; use xmpp::jid::{BareJid, Jid, ResourcePart}; use xmpp::parsers::message::MessageType; use xmpp::{ - message::send::RawMessageSettings, muc::room::JoinRoomSettings, Agent, ClientBuilder, - ClientFeature, ClientType, Event, RoomNick, Config as AgentConfig, + Agent, ClientBuilder, ClientFeature, ClientType, Config as AgentConfig, Event, RoomNick, + message::send::RawMessageSettings, + muc::room::{JoinRoomSettings, LeaveRoomSettings}, }; pub struct XmppClient { @@ -30,6 +31,7 @@ pub struct XmppClient { agent: Agent, rooms: Vec, nickname: ResourcePart, + admins: Vec, } impl XmppClient { @@ -38,6 +40,7 @@ impl XmppClient { password: &str, rooms: Vec, nickname: ResourcePart, + admins: Vec, ) -> XmppClient { let config = AgentConfig { bookmarks_autojoin: false, @@ -56,6 +59,7 @@ impl XmppClient { agent, rooms, nickname, + admins, } } @@ -65,16 +69,19 @@ impl XmppClient { Event::Online => { self.is_online = true; debug!("XMPP Online"); + self.join_rooms().await + } + Event::ChatMessage(_id, bare, message, _timeinfo) => { + if !self.admins.contains(&bare) { + debug!("Received chat message from {}, not in admins", bare); + continue; + } - for room in &self.rooms { - self.agent - .join_room(JoinRoomSettings { - room: room.clone(), - nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())), - password: None, - status: Some(("en", "Hi there!")), - }) - .await + debug!("Received chat message from {}: {}", bare, message); + + if message == "rejoin" { + self.leave_rooms().await; + self.join_rooms().await } } Event::Disconnected(e) => { @@ -88,6 +95,30 @@ impl XmppClient { } } + pub async fn join_rooms(&mut self) { + for room in &self.rooms { + self.agent + .join_room(JoinRoomSettings { + room: room.clone(), + nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())), + password: None, + status: Some(("en", "Hi there!")), + }) + .await + } + } + + pub async fn leave_rooms(&mut self) { + for room in &self.rooms { + self.agent + .leave_room(LeaveRoomSettings { + room: room.clone(), + status: Some(("en", "See you!")), + }) + .await + } + } + pub async fn receive(&mut self, mut rx: mpsc::UnboundedReceiver) { loop { tokio::select! { diff --git a/src/main.rs b/src/main.rs index 21ac84a..e9c22d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,7 @@ async fn main() -> Result<(), Error> { config.password.as_str(), config.rooms, config.nickname, + config.admins, ); let xmpp_handle = tokio::task::spawn(async move {