xmpp: Agent::send_message is only for normal messages
This commit is contained in:
parent
d98ed615a5
commit
e19a7988ed
5 changed files with 51 additions and 30 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
Version NEXT
|
Version NEXT
|
||||||
XXXX-YY-ZZ [ RELEASER <admin@localhost> ]
|
XXXX-YY-ZZ [ RELEASER <admin@localhost> ]
|
||||||
* Breaking:
|
* Breaking:
|
||||||
|
- Agent::send_message now only sends normal messages to other users (!487)
|
||||||
- Event::LeaveRoom, Event::LeaveAllRooms, and Event::JoinRooms have been removed.
|
- Event::LeaveRoom, Event::LeaveAllRooms, and Event::JoinRooms have been removed.
|
||||||
Agent now handles MUC connection states internally. (!481)
|
Agent now handles MUC connection states internally. (!481)
|
||||||
- Agent::leave_room now takes LeaveRoomSettings argument (!483)
|
- Agent::leave_room now takes LeaveRoomSettings argument (!483)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
pub use tokio_xmpp::parsers;
|
pub use tokio_xmpp::parsers;
|
||||||
use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
|
use tokio_xmpp::parsers::disco::DiscoInfoResult;
|
||||||
pub use tokio_xmpp::{
|
pub use tokio_xmpp::{
|
||||||
jid::{BareJid, FullJid, Jid, ResourcePart},
|
jid::{BareJid, FullJid, Jid, ResourcePart},
|
||||||
minidom::Element,
|
minidom::Element,
|
||||||
|
|
@ -49,14 +49,8 @@ impl Agent {
|
||||||
muc::room::leave_room(self, settings).await
|
muc::room::leave_room(self, settings).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_message(
|
pub async fn send_message<'a>(&mut self, settings: message::send::MessageSettings<'a>) {
|
||||||
&mut self,
|
message::send::send_message(self, settings).await
|
||||||
recipient: Jid,
|
|
||||||
type_: MessageType,
|
|
||||||
lang: &str,
|
|
||||||
text: &str,
|
|
||||||
) {
|
|
||||||
message::send::send_message(self, recipient, type_, lang, text).await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_room_message<'a>(&mut self, settings: muc::room::RoomMessageSettings<'a>) {
|
pub async fn send_room_message<'a>(&mut self, settings: muc::room::RoomMessageSettings<'a>) {
|
||||||
|
|
|
||||||
|
|
@ -5,23 +5,47 @@
|
||||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
jid::Jid,
|
jid::BareJid,
|
||||||
parsers::message::{Body, Message, MessageType},
|
parsers::message::{Body, Message, MessageType},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::Agent;
|
use crate::Agent;
|
||||||
|
|
||||||
pub async fn send_message(
|
#[derive(Clone, Debug)]
|
||||||
agent: &mut Agent,
|
pub struct MessageSettings<'a> {
|
||||||
recipient: Jid,
|
pub recipient: BareJid,
|
||||||
type_: MessageType,
|
pub message: &'a str,
|
||||||
lang: &str,
|
pub lang: Option<&'a str>,
|
||||||
text: &str,
|
}
|
||||||
) {
|
|
||||||
let mut message = Message::new(Some(recipient));
|
impl<'a> MessageSettings<'a> {
|
||||||
message.type_ = type_;
|
pub fn new(recipient: BareJid, message: &'a str) -> Self {
|
||||||
message
|
Self {
|
||||||
.bodies
|
recipient,
|
||||||
.insert(String::from(lang), Body(String::from(text)));
|
message,
|
||||||
let _ = agent.client.send_stanza(message.into()).await;
|
lang: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_lang(mut self, lang: &'a str) -> Self {
|
||||||
|
self.lang = Some(lang);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_message<'a>(agent: &mut Agent, settings: MessageSettings<'a>) {
|
||||||
|
let MessageSettings {
|
||||||
|
recipient,
|
||||||
|
message,
|
||||||
|
lang,
|
||||||
|
} = settings;
|
||||||
|
|
||||||
|
// TODO: check that recipient may receive normal chat message, eg is not a MUC chatroom
|
||||||
|
|
||||||
|
let mut stanza = Message::new(Some(recipient.into()));
|
||||||
|
stanza.type_ = MessageType::Chat;
|
||||||
|
stanza
|
||||||
|
.bodies
|
||||||
|
.insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
|
||||||
|
agent.client.send_stanza(stanza.into()).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ pub async fn send_room_private_message<'a>(
|
||||||
lang,
|
lang,
|
||||||
} = settings;
|
} = settings;
|
||||||
|
|
||||||
|
// TODO: check that room is in agent.joined_rooms
|
||||||
let recipient: Jid = room.with_resource(recipient.as_ref()).into();
|
let recipient: Jid = room.with_resource(recipient.as_ref()).into();
|
||||||
let mut stanza = Message::new(recipient).with_payload(MucUser::new());
|
let mut stanza = Message::new(recipient).with_payload(MucUser::new());
|
||||||
stanza.type_ = MessageType::Chat;
|
stanza.type_ = MessageType::Chat;
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use crate::parsers::message::MessageType;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
jid::{BareJid, ResourcePart, ResourceRef},
|
jid::{BareJid, ResourcePart, ResourceRef},
|
||||||
parsers::{
|
parsers::{
|
||||||
|
message::{Body, Message},
|
||||||
muc::Muc,
|
muc::Muc,
|
||||||
presence::{Presence, Type as PresenceType},
|
presence::{Presence, Type as PresenceType},
|
||||||
},
|
},
|
||||||
|
|
@ -188,12 +189,12 @@ pub async fn send_room_message<'a>(agent: &mut Agent, settings: RoomMessageSetti
|
||||||
lang,
|
lang,
|
||||||
} = settings;
|
} = settings;
|
||||||
|
|
||||||
agent
|
// TODO: check that room is in agent.joined_rooms
|
||||||
.send_message(
|
|
||||||
room.into(),
|
let mut stanza = Message::new(Some(room.into()));
|
||||||
MessageType::Groupchat,
|
stanza.type_ = MessageType::Groupchat;
|
||||||
lang.unwrap_or(""),
|
stanza
|
||||||
message,
|
.bodies
|
||||||
)
|
.insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
|
||||||
.await;
|
agent.client.send_stanza(stanza.into()).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue