xmpp/message/receive/
chat.rs1use tokio_xmpp::{
8 jid::Jid,
9 parsers::{message::Message, message_correct::Replace, muc::user::MucUser},
10};
11
12use crate::{delay::StanzaTimeInfo, Agent, Event, RoomNick};
13
14pub async fn handle_message_chat(
15 agent: &mut Agent,
16 events: &mut Vec<Event>,
17 from: Jid,
18 message: &mut Message,
19 time_info: StanzaTimeInfo,
20) {
21 let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect();
22
23 let Some((_lang, body)) = message.get_best_body_cloned(langs) else {
24 debug!("Received normal/chat message without body:\n{:#?}", message);
25 return;
26 };
27
28 let is_muc_pm = message.extract_valid_payload::<MucUser>().is_some();
29 let correction = message.extract_valid_payload::<Replace>();
30
31 if is_muc_pm {
32 if from.resource().is_none() {
33 warn!("Received malformed MessageType::Chat in muc#user namespace from a bare JID:\n{:#?}", message);
34 } else {
35 let full_from = from.clone().try_into_full().unwrap();
36
37 let event = if let Some(correction) = correction {
38 Event::RoomPrivateMessageCorrection(
39 correction.id,
40 full_from.to_bare(),
41 RoomNick::from_resource_ref(full_from.resource()),
42 body.clone(),
43 time_info,
44 )
45 } else {
46 Event::RoomPrivateMessage(
47 message.id.clone(),
48 from.to_bare(),
49 RoomNick::from_resource_ref(full_from.resource()),
50 body.clone(),
51 time_info,
52 )
53 };
54 events.push(event);
55 }
56 } else {
57 let event = if let Some(correction) = correction {
58 Event::ChatMessageCorrection(correction.id, from.to_bare(), body.clone(), time_info)
60 } else {
61 Event::ChatMessage(message.id.clone(), from.to_bare(), body, time_info)
62 };
63 events.push(event);
64 }
65}