diff --git a/xmpp/examples/hello_bot.rs b/xmpp/examples/hello_bot.rs index 791e33f8..4e9d536f 100644 --- a/xmpp/examples/hello_bot.rs +++ b/xmpp/examples/hello_bot.rs @@ -6,8 +6,7 @@ use env_logger; use std::env::args; -use xmpp::{ClientBuilder, ClientFeature, ClientType, Event}; -use xmpp_parsers::{message::MessageType, Jid}; +use xmpp::{Action, ClientBuilder, ClientFeature, ClientType, Event}; #[tokio::main] async fn main() -> Result<(), Option<()>> { @@ -56,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); @@ -74,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 21b5f0bc..0cf2bb2c 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -74,6 +74,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 { @@ -95,6 +99,20 @@ 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: &'a str, @@ -202,11 +220,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, @@ -227,13 +241,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 @@ -252,6 +260,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