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
#[arg(short, long)]
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]
@ -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! {

View file

@ -24,10 +24,12 @@ use xmpp_parsers::{message::MessageType, BareJid, Jid};
pub struct XmppClient {
is_online: bool,
agent: Agent,
rooms: Vec<BareJid>,
nickname: String,
}
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)
.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,16 +54,18 @@ impl XmppClient {
self.is_online = true;
debug!("XMPP Online");
for room in &self.rooms {
self.agent
.join_room(
BareJid::from_str("chat@xmpp.rs").unwrap(),
Some(String::from("bot")),
room.clone(),
Some(self.nickname.clone()),
None,
"en",
"Hi there!",
)
.await
}
}
_ => {
debug!("XMPP Event not supported")
}
@ -72,9 +78,10 @@ impl XmppClient {
debug!("Received Webhook");
if let Some(display) = format_webhook(&wh) {
debug!("Webhook: {}", display);
for room in &self.rooms {
self.agent
.send_message(
Jid::Bare(BareJid::from_str("chat@xmpp.rs").unwrap()),
Jid::Bare(room.clone()),
MessageType::Groupchat,
"en",
&display,
@ -82,4 +89,5 @@ impl XmppClient {
.await
}
}
}
}