xmpp: Add Agent::send_raw_message

This commit is contained in:
xmppftw xmppftw 2024-12-17 20:03:57 +01:00
commit b4d3252da4
3 changed files with 62 additions and 3 deletions

View file

@ -4,13 +4,67 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::{
jid::BareJid,
parsers::message::{Body, Message, MessageType},
use crate::{
minidom::Element,
parsers::message::{Body, Message, MessagePayload, MessageType},
tokio_xmpp::jid::{BareJid, Jid},
};
use crate::Agent;
#[derive(Clone, Debug)]
pub struct RawMessageSettings<'a> {
pub recipient: Jid,
pub message_type: MessageType,
pub message: &'a str,
pub lang: Option<&'a str>,
pub payloads: Vec<Element>,
}
impl<'a> RawMessageSettings<'a> {
pub fn new(recipient: Jid, message_type: MessageType, message: &'a str) -> Self {
Self {
recipient,
message_type,
message,
lang: None,
payloads: Vec::new(),
}
}
pub fn with_lang(mut self, lang: &'a str) -> Self {
self.lang = Some(lang);
self
}
pub fn with_payload(mut self, payload: impl MessagePayload) -> Self {
self.payloads.push(payload.into());
self
}
}
pub async fn send_raw_message<'a>(agent: &mut Agent, settings: RawMessageSettings<'a>) {
let RawMessageSettings {
recipient,
message_type,
message,
lang,
payloads,
} = settings;
let mut stanza = Message::new(Some(recipient));
for payload in payloads {
stanza.payloads.push(payload);
}
stanza.type_ = message_type;
stanza
.bodies
.insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
agent.client.send_stanza(stanza.into()).await.unwrap();
}
#[derive(Clone, Debug)]
pub struct MessageSettings<'a> {
pub recipient: BareJid,