diff --git a/src/bot.rs b/src/bot.rs index deec1ff..be7a56d 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -14,6 +14,7 @@ // along with this program. If not, see . use crate::Error; +use crate::config::Config; use crate::hooks::{Hook, IssueAction, MergeRequestAction, format_hook}; use chrono::{TimeDelta, Utc}; @@ -29,28 +30,21 @@ use xmpp::{ pub struct XmppClient { is_online: bool, agent: Agent, - rooms: Vec, nickname: ResourcePart, - admins: Vec, + config: Config, /// Keep around messages we've sent recently so we're able to prevent spamming the same type of /// messages recent_hooks: Vec, } impl XmppClient { - pub fn new( - jid: BareJid, - password: &str, - rooms: Vec, - nickname: ResourcePart, - admins: Vec, - ) -> XmppClient { - let config = AgentConfig { + pub fn new(jid: BareJid, password: &str, nickname: ResourcePart, config: Config) -> XmppClient { + let agent_config = AgentConfig { bookmarks_autojoin: false, ..AgentConfig::default() }; let agent = ClientBuilder::new(jid, password) - .set_config(config) + .set_config(agent_config) .set_client(ClientType::Bot, "xmpp-rs") .set_website("https://gitlab.com/xmpp-rs/xmpp-rs") .set_default_nick(&nickname) @@ -60,9 +54,8 @@ impl XmppClient { XmppClient { is_online: false, agent, - rooms, nickname, - admins, + config, recent_hooks: Vec::new(), } } @@ -73,7 +66,7 @@ impl XmppClient { Event::Online => { self.is_online = true; debug!("XMPP Online"); - for room in &self.rooms { + for room in &self.config.rooms { self.agent .join_room(JoinRoomSettings { nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())), @@ -83,7 +76,7 @@ impl XmppClient { } } Event::ChatMessage(_id, bare, message, _timeinfo) => { - if !self.admins.contains(&bare) { + if !self.config.admins.contains(&bare) { debug!("Received chat message from {}, not in admins", bare); continue; } @@ -91,7 +84,7 @@ impl XmppClient { debug!("Received chat message from {}: {}", bare, message); if message == "rejoin" { - for room in &self.rooms { + for room in &self.config.rooms { self.agent .join_room(JoinRoomSettings { nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())), @@ -209,7 +202,7 @@ impl XmppClient { } if let Some(display) = format_hook(&wh) { debug!("Hook: {}", display); - for room in &self.rooms { + for room in &self.config.rooms { self.agent .send_raw_message(RawMessageSettings { recipient: Jid::from(room.clone()), diff --git a/src/config.rs b/src/config.rs index 81a8e82..487597c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,7 +23,7 @@ use xmpp::jid::{BareJid, ResourcePart}; use crate::error::Error; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { /// Accounts trusted for admin tasks #[serde(default = "Vec::new")] diff --git a/src/main.rs b/src/main.rs index e9c22d6..8f2f0a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,11 +56,10 @@ async fn main() -> Result<(), Error> { let (value_tx, value_rx) = mpsc::unbounded_channel::(); let mut bot = XmppClient::new( - config.jid, + config.jid.clone(), config.password.as_str(), - config.rooms, - config.nickname, - config.admins, + config.nickname.clone(), + config.clone(), ); let xmpp_handle = tokio::task::spawn(async move {