xmpp: Rewrite other messages with Agent::send_raw_message

This commit is contained in:
xmppftw xmppftw 2024-12-17 21:23:51 +01:00
commit 0ec9103b28
3 changed files with 28 additions and 26 deletions

View file

@ -37,6 +37,11 @@ impl<'a> RawMessageSettings<'a> {
self self
} }
pub fn with_lang_option(mut self, lang: Option<&'a str>) -> Self {
self.lang = lang;
self
}
pub fn with_payload(mut self, payload: impl MessagePayload) -> Self { pub fn with_payload(mut self, payload: impl MessagePayload) -> Self {
self.payloads.push(payload.into()); self.payloads.push(payload.into());
self self
@ -94,12 +99,11 @@ pub async fn send_message<'a>(agent: &mut Agent, settings: MessageSettings<'a>)
lang, lang,
} = settings; } = settings;
// TODO: check that recipient may receive normal chat message, eg is not a MUC chatroom // TODO: check that recipient is not in agent.joined_rooms
agent
let mut stanza = Message::new(Some(recipient.into())); .send_raw_message(
stanza.type_ = MessageType::Chat; RawMessageSettings::new(recipient.into(), MessageType::Chat, message)
stanza .with_lang_option(lang),
.bodies )
.insert(lang.unwrap_or("").to_string(), Body(String::from(message))); .await;
agent.client.send_stanza(stanza.into()).await.unwrap();
} }

View file

@ -5,10 +5,8 @@
// 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 crate::{ use crate::{
parsers::{ message::send::RawMessageSettings,
message::{Body, Message, MessageType}, parsers::{message::MessageType, muc::user::MucUser},
muc::user::MucUser,
},
tokio_xmpp::jid::{BareJid, Jid}, tokio_xmpp::jid::{BareJid, Jid},
Agent, RoomNick, Agent, RoomNick,
}; };
@ -50,10 +48,11 @@ pub async fn send_room_private_message<'a>(
// TODO: check that room is in agent.joined_rooms // 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()); agent
stanza.type_ = MessageType::Chat; .send_raw_message(
stanza RawMessageSettings::new(recipient, MessageType::Chat, message)
.bodies .with_payload(MucUser::new())
.insert(lang.unwrap_or("").to_string(), Body(String::from(message))); .with_lang_option(lang),
agent.client.send_stanza(stanza.into()).await.unwrap(); )
.await;
} }

View file

@ -8,12 +8,12 @@ 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},
}, },
}; };
use crate::message::send::RawMessageSettings;
use crate::Agent; use crate::Agent;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -190,11 +190,10 @@ pub async fn send_room_message<'a>(agent: &mut Agent, settings: RoomMessageSetti
} = settings; } = settings;
// TODO: check that room is in agent.joined_rooms // TODO: check that room is in agent.joined_rooms
agent
let mut stanza = Message::new(Some(room.into())); .send_raw_message(
stanza.type_ = MessageType::Groupchat; RawMessageSettings::new(room.into(), MessageType::Groupchat, message)
stanza .with_lang_option(lang),
.bodies )
.insert(lang.unwrap_or("").to_string(), Body(String::from(message))); .await;
agent.client.send_stanza(stanza.into()).await.unwrap();
} }