1use crate::{
8 jid::{BareJid, Jid},
9 minidom::Element,
10 parsers::message::{Body, Message, MessagePayload, MessageType},
11};
12
13use crate::Agent;
14
15#[derive(Clone, Debug)]
16pub struct RawMessageSettings<'a> {
17 pub recipient: Jid,
18 pub message_type: MessageType,
19 pub message: &'a str,
20 pub lang: Option<&'a str>,
21 pub payloads: Vec<Element>,
22}
23
24impl<'a> RawMessageSettings<'a> {
25 pub fn new(recipient: Jid, message_type: MessageType, message: &'a str) -> Self {
26 Self {
27 recipient,
28 message_type,
29 message,
30 lang: None,
31 payloads: Vec::new(),
32 }
33 }
34
35 pub fn with_lang(mut self, lang: &'a str) -> Self {
36 self.lang = Some(lang);
37 self
38 }
39
40 pub fn with_lang_option(mut self, lang: Option<&'a str>) -> Self {
41 self.lang = lang;
42 self
43 }
44
45 pub fn with_payload(mut self, payload: impl MessagePayload) -> Self {
46 self.payloads.push(payload.into());
47 self
48 }
49}
50
51pub async fn send_raw_message<'a>(agent: &mut Agent, settings: RawMessageSettings<'a>) {
52 let RawMessageSettings {
53 recipient,
54 message_type,
55 message,
56 lang,
57 payloads,
58 } = settings;
59
60 let mut stanza = Message::new(Some(recipient));
61
62 for payload in payloads {
63 stanza.payloads.push(payload);
64 }
65
66 stanza.type_ = message_type;
67 stanza
68 .bodies
69 .insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
70 agent.client.send_stanza(stanza.into()).await.unwrap();
71}
72
73#[derive(Clone, Debug)]
74pub struct MessageSettings<'a> {
75 pub recipient: BareJid,
76 pub message: &'a str,
77 pub lang: Option<&'a str>,
78}
79
80impl<'a> MessageSettings<'a> {
81 pub fn new(recipient: BareJid, message: &'a str) -> Self {
82 Self {
83 recipient,
84 message,
85 lang: None,
86 }
87 }
88
89 pub fn with_lang(mut self, lang: &'a str) -> Self {
90 self.lang = Some(lang);
91 self
92 }
93}
94
95pub async fn send_message<'a>(agent: &mut Agent, settings: MessageSettings<'a>) {
96 let MessageSettings {
97 recipient,
98 message,
99 lang,
100 } = settings;
101
102 agent
104 .send_raw_message(
105 RawMessageSettings::new(recipient.into(), MessageType::Chat, message)
106 .with_lang_option(lang),
107 )
108 .await;
109}