diff --git a/xmpp/examples/hello_bot.rs b/xmpp/examples/hello_bot.rs index 6812f434c64cb6033b89f7a8edf4603afe9ff659..d23c2fc2330b49d77476cb829e3cd306ed0b664c 100644 --- a/xmpp/examples/hello_bot.rs +++ b/xmpp/examples/hello_bot.rs @@ -7,7 +7,7 @@ use std::env::args; use std::str::FromStr; use tokio_xmpp::jid::BareJid; -use xmpp::muc::room::RoomMessageSettings; +use xmpp::muc::room::{JoinRoomSettings, RoomMessageSettings}; use xmpp::{ClientBuilder, ClientFeature, ClientType, Event}; #[tokio::main] @@ -15,14 +15,30 @@ async fn main() -> Result<(), Option<()>> { env_logger::init(); let args: Vec = args().collect(); - if args.len() != 3 { - println!("Usage: {} ", args[0]); + if args.len() < 3 { + println!("Usage: {} [ROOM...]", args[0]); return Err(None); } let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1])); let password = &args[2]; + // Figure out which rooms to join to say hello + let mut rooms: Vec = Vec::new(); + let mut counter = 3; + if args.len() > 3 { + while counter < args.len() { + match BareJid::from_str(&args[counter]) { + Ok(jid) => rooms.push(jid), + Err(e) => { + log::error!("Requested room {} is not a valid JID: {e}", args[counter]); + std::process::exit(1); + } + } + counter += 1; + } + } + // Client instance let mut client = ClientBuilder::new(jid, password) .set_client(ClientType::Bot, "xmpp-rs") @@ -32,11 +48,24 @@ async fn main() -> Result<(), Option<()>> { .enable_feature(ClientFeature::JoinRooms) .build(); + log::info!("Connecting..."); + loop { for event in client.wait_for_events().await { match event { Event::Online => { log::info!("Online."); + for room in &rooms { + log::info!("Joining room {} from CLI argument…", room); + client + .join_room(JoinRoomSettings { + room: room.clone(), + nick: None, + password: None, + status: Some(("en", "Yet another bot!")), + }) + .await; + } } Event::Disconnected(e) => { log::info!("Disconnected: {}.", e); diff --git a/xmpp/src/muc/room.rs b/xmpp/src/muc/room.rs index d957df183d25c7deff18c85bc7089dafd3d20be8..dc6e9aa510ad67e640d901b378ad29022c246545 100644 --- a/xmpp/src/muc/room.rs +++ b/xmpp/src/muc/room.rs @@ -181,6 +181,11 @@ impl<'a> RoomMessageSettings<'a> { lang: None, } } + + pub fn with_lang(mut self, lang: &'a str) -> Self { + self.lang = Some(lang); + self + } } pub async fn send_room_message<'a, C: ServerConnector>(