diff --git a/src/main.rs b/src/main.rs index 090d279..02bb5db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,14 @@ struct Args { /// Account password #[arg(short, long)] password: String, + + /// Rooms to join, e.g., room@chat.example.org + #[arg(short, long = "room", value_name = "ROOM")] + rooms: Vec, + + /// Nickname to use in rooms + #[arg(short, long, default_value = "bot")] + nickname: String, } #[tokio::main] @@ -70,7 +78,12 @@ async fn main() { println!("Listening on http://{}", addr); let _join = tokio::spawn(server); - let mut client = XmppClient::new(&String::from(args.jid), args.password.as_str()); + let mut client = XmppClient::new( + &String::from(args.jid), + args.password.as_str(), + args.rooms, + args.nickname, + ); loop { tokio::select! { diff --git a/src/xmpp.rs b/src/xmpp.rs index b909dbd..3d2bf41 100644 --- a/src/xmpp.rs +++ b/src/xmpp.rs @@ -24,10 +24,12 @@ use xmpp_parsers::{message::MessageType, BareJid, Jid}; pub struct XmppClient { is_online: bool, agent: Agent, + rooms: Vec, + nickname: String, } impl XmppClient { - pub fn new(jid: &str, password: &str) -> XmppClient { + pub fn new(jid: &str, password: &str, rooms: Vec, nickname: String) -> XmppClient { let agent = ClientBuilder::new(jid, password) .set_client(ClientType::Bot, "xmpp-rs") .set_website("https://gitlab.com/xmpp-rs/xmpp-rs") @@ -39,6 +41,8 @@ impl XmppClient { XmppClient { is_online: false, agent, + rooms, + nickname, } } @@ -50,15 +54,17 @@ impl XmppClient { self.is_online = true; debug!("XMPP Online"); - self.agent - .join_room( - BareJid::from_str("chat@xmpp.rs").unwrap(), - Some(String::from("bot")), - None, - "en", - "Hi there!", - ) - .await + for room in &self.rooms { + self.agent + .join_room( + room.clone(), + Some(self.nickname.clone()), + None, + "en", + "Hi there!", + ) + .await + } } _ => { debug!("XMPP Event not supported") @@ -72,14 +78,16 @@ impl XmppClient { debug!("Received Webhook"); if let Some(display) = format_webhook(&wh) { debug!("Webhook: {}", display); - self.agent - .send_message( - Jid::Bare(BareJid::from_str("chat@xmpp.rs").unwrap()), - MessageType::Groupchat, - "en", - &display, - ) - .await + for room in &self.rooms { + self.agent + .send_message( + Jid::Bare(room.clone()), + MessageType::Groupchat, + "en", + &display, + ) + .await + } } } }