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,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
}
}
}
}