hello_bot accepts a list of room JIDs to join to say hello
This commit is contained in:
parent
fdec34afde
commit
8c3c8c7c97
2 changed files with 37 additions and 3 deletions
|
|
@ -7,7 +7,7 @@
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio_xmpp::jid::BareJid;
|
use tokio_xmpp::jid::BareJid;
|
||||||
use xmpp::muc::room::RoomMessageSettings;
|
use xmpp::muc::room::{JoinRoomSettings, RoomMessageSettings};
|
||||||
use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
|
use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|
@ -15,14 +15,30 @@ async fn main() -> Result<(), Option<()>> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let args: Vec<String> = args().collect();
|
let args: Vec<String> = args().collect();
|
||||||
if args.len() != 3 {
|
if args.len() < 3 {
|
||||||
println!("Usage: {} <jid> <password>", args[0]);
|
println!("Usage: {} <jid> <password> [ROOM...]", args[0]);
|
||||||
return Err(None);
|
return Err(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
|
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
|
||||||
let password = &args[2];
|
let password = &args[2];
|
||||||
|
|
||||||
|
// Figure out which rooms to join to say hello
|
||||||
|
let mut rooms: Vec<BareJid> = 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
|
// Client instance
|
||||||
let mut client = ClientBuilder::new(jid, password)
|
let mut client = ClientBuilder::new(jid, password)
|
||||||
.set_client(ClientType::Bot, "xmpp-rs")
|
.set_client(ClientType::Bot, "xmpp-rs")
|
||||||
|
|
@ -32,11 +48,24 @@ async fn main() -> Result<(), Option<()>> {
|
||||||
.enable_feature(ClientFeature::JoinRooms)
|
.enable_feature(ClientFeature::JoinRooms)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
log::info!("Connecting...");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for event in client.wait_for_events().await {
|
for event in client.wait_for_events().await {
|
||||||
match event {
|
match event {
|
||||||
Event::Online => {
|
Event::Online => {
|
||||||
log::info!("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) => {
|
Event::Disconnected(e) => {
|
||||||
log::info!("Disconnected: {}.", e);
|
log::info!("Disconnected: {}.", e);
|
||||||
|
|
|
||||||
|
|
@ -181,6 +181,11 @@ impl<'a> RoomMessageSettings<'a> {
|
||||||
lang: None,
|
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>(
|
pub async fn send_room_message<'a, C: ServerConnector>(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue