mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Expose Actions instead of methods on Agent
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
76b68e932a
commit
53053eab1e
2 changed files with 61 additions and 27 deletions
|
@ -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);
|
||||
|
|
|
@ -73,6 +73,10 @@ pub enum ClientFeature {
|
|||
|
||||
pub type Id = Option<String>;
|
||||
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<RoomNick>,
|
||||
Option<Password<'a>>,
|
||||
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<String>,
|
||||
|
@ -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<Event> {
|
||||
let mut events = vec![];
|
||||
let from = iq
|
||||
|
|
Loading…
Reference in a new issue