Add 'rooms' and 'nickname' arguments

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-05-21 23:47:54 +02:00
parent 6074db176a
commit 40a7c95200
2 changed files with 40 additions and 19 deletions

View file

@ -46,6 +46,14 @@ struct Args {
/// Account password /// Account password
#[arg(short, long)] #[arg(short, long)]
password: String, password: String,
/// Rooms to join, e.g., room@chat.example.org
#[arg(short, long = "room", value_name = "ROOM")]
rooms: Vec<BareJid>,
/// Nickname to use in rooms
#[arg(short, long, default_value = "bot")]
nickname: String,
} }
#[tokio::main] #[tokio::main]
@ -70,7 +78,12 @@ async fn main() {
println!("Listening on http://{}", addr); println!("Listening on http://{}", addr);
let _join = tokio::spawn(server); 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 { loop {
tokio::select! { tokio::select! {

View file

@ -24,10 +24,12 @@ use xmpp_parsers::{message::MessageType, BareJid, Jid};
pub struct XmppClient { pub struct XmppClient {
is_online: bool, is_online: bool,
agent: Agent, agent: Agent,
rooms: Vec<BareJid>,
nickname: String,
} }
impl XmppClient { impl XmppClient {
pub fn new(jid: &str, password: &str) -> XmppClient { pub fn new(jid: &str, password: &str, rooms: Vec<BareJid>, nickname: String) -> XmppClient {
let agent = ClientBuilder::new(jid, password) let agent = ClientBuilder::new(jid, password)
.set_client(ClientType::Bot, "xmpp-rs") .set_client(ClientType::Bot, "xmpp-rs")
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs") .set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
@ -39,6 +41,8 @@ impl XmppClient {
XmppClient { XmppClient {
is_online: false, is_online: false,
agent, agent,
rooms,
nickname,
} }
} }
@ -50,15 +54,17 @@ impl XmppClient {
self.is_online = true; self.is_online = true;
debug!("XMPP Online"); debug!("XMPP Online");
self.agent for room in &self.rooms {
.join_room( self.agent
BareJid::from_str("chat@xmpp.rs").unwrap(), .join_room(
Some(String::from("bot")), room.clone(),
None, Some(self.nickname.clone()),
"en", None,
"Hi there!", "en",
) "Hi there!",
.await )
.await
}
} }
_ => { _ => {
debug!("XMPP Event not supported") debug!("XMPP Event not supported")
@ -72,14 +78,16 @@ impl XmppClient {
debug!("Received Webhook"); debug!("Received Webhook");
if let Some(display) = format_webhook(&wh) { if let Some(display) = format_webhook(&wh) {
debug!("Webhook: {}", display); debug!("Webhook: {}", display);
self.agent for room in &self.rooms {
.send_message( self.agent
Jid::Bare(BareJid::from_str("chat@xmpp.rs").unwrap()), .send_message(
MessageType::Groupchat, Jid::Bare(room.clone()),
"en", MessageType::Groupchat,
&display, "en",
) &display,
.await )
.await
}
} }
} }
} }