Compare commits
1 commit
main
...
xmpp-actio
Author | SHA1 | Date | |
---|---|---|---|
bd47931ae8 |
2 changed files with 60 additions and 26 deletions
|
@ -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);
|
||||
|
|
|
@ -74,6 +74,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 {
|
||||
|
@ -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<RoomNick>,
|
||||
Option<Password<'a>>,
|
||||
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<String>,
|
||||
|
@ -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<Event> {
|
||||
let mut events = vec![];
|
||||
let from = iq
|
||||
|
|
Loading…
Reference in a new issue