From 53053eab1eca35100c5955540595f692407c2a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 22 May 2023 20:03:22 +0200 Subject: [PATCH] Expose Actions instead of methods on Agent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- xmpp/examples/hello_bot.rs | 26 +++++++--------- xmpp/src/lib.rs | 62 ++++++++++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/xmpp/examples/hello_bot.rs b/xmpp/examples/hello_bot.rs index 74b56c1..82df18c 100644 --- a/xmpp/examples/hello_bot.rs +++ b/xmpp/examples/hello_bot.rs @@ -6,9 +6,7 @@ use env_logger; use std::env::args; -use std::str::FromStr; -use xmpp::{ClientBuilder, ClientFeature, ClientType, Event}; -use xmpp_parsers::{message::MessageType, BareJid, Jid}; +use xmpp::{Action, ClientBuilder, ClientFeature, ClientType, Event}; #[tokio::main] async fn main() -> Result<(), Option<()>> { @@ -57,15 +55,14 @@ async fn main() -> Result<(), Option<()>> { } Event::JoinRoom(jid, conference) => { println!("Joining room {} ({:?})…", jid, conference.name); - client - .join_room( - jid, - conference.nick, - conference.password, - "en", - "Yet another bot!", - ) - .await; + let join = Action::JoinRoom( + jid, + conference.nick, + conference.password.as_deref(), + "en", + "Yet another bot!", + ); + let _ = client.send(join).await; } Event::LeaveRoom(jid) => { println!("Leaving room {}…", jid); @@ -75,9 +72,8 @@ async fn main() -> Result<(), Option<()>> { } Event::RoomJoined(jid) => { println!("Joined room {}.", jid); - client - .send_message(Jid::Bare(jid), MessageType::Groupchat, "en", "Hello world!") - .await; + let msg = Action::GroupchatMessage(jid, "en", "Hello world!"); + let _ = client.send(msg).await; } Event::RoomLeft(jid) => { println!("Left room {}.", jid); diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index d9096e6..f3a490d 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -73,6 +73,10 @@ pub enum ClientFeature { pub type Id = Option; pub type RoomNick = String; +pub type Password<'a> = &'a str; +pub type Lang<'a> = &'a str; +pub type TextMessage<'a> = &'a str; +pub type PresenceStatus<'a> = &'a str; #[derive(Debug)] pub enum Event { @@ -97,6 +101,21 @@ pub enum Event { HttpUploadedFile(String), } +#[derive(Debug)] +pub enum Action<'a> { + Disconnect, + ChatMessage(Jid, Lang<'a>, TextMessage<'a>), + GroupchatMessage(BareJid, Lang<'a>, TextMessage<'a>), + JoinRoom( + BareJid, + Option, + Option>, + Lang<'a>, + PresenceStatus<'a>, + ), +} + +#[derive(Default)] pub struct ClientBuilder<'a> { jid: BareJid, password: &'a str, @@ -215,11 +234,7 @@ pub struct Agent { } impl Agent { - pub async fn disconnect(&mut self) -> Result<(), Error> { - self.client.send_end().await - } - - pub async fn join_room( + async fn join_room( &mut self, room: BareJid, nick: Option, @@ -240,13 +255,7 @@ impl Agent { let _ = self.client.send_stanza(presence.into()).await; } - pub async fn send_message( - &mut self, - recipient: Jid, - type_: MessageType, - lang: &str, - text: &str, - ) { + async fn send_message(&mut self, recipient: Jid, type_: MessageType, lang: &str, text: &str) { let mut message = Message::new(Some(recipient)); message.type_ = type_; message @@ -281,6 +290,35 @@ impl Agent { presence } + pub async fn send(&mut self, action: Action<'_>) -> Result<(), Error> { + Ok(match action { + Action::Disconnect => self.client.send_end().await?, + Action::ChatMessage(jid, lang, msg) => { + self.send_message(jid, MessageType::Chat, lang.as_ref(), msg.as_ref()) + .await + } + Action::GroupchatMessage(jid, lang, msg) => { + self.send_message( + Jid::Bare(jid), + MessageType::Groupchat, + lang.as_ref(), + msg.as_ref(), + ) + .await + } + Action::JoinRoom(bare, nick, password, lang, status) => { + self.join_room( + bare, + nick, + password.map(String::from), + lang.as_ref(), + status.as_ref(), + ) + .await + } + }) + } + async fn handle_iq(&mut self, iq: Iq) -> Vec { let mut events = vec![]; let from = iq