xmpp/message/receive/
chat.rs

1// Copyright (c) 2023 xmpp-rs contributors.
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use tokio_xmpp::{
8    jid::Jid,
9    parsers::{confirm::Confirm, 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 confirm = message.extract_valid_payload::<Confirm>();
24    let is_muc_pm = message.extract_valid_payload::<MucUser>().is_some();
25
26    // First process events that do not require the message body
27    if !is_muc_pm && confirm.is_some() {
28        events.push(Event::AuthConfirm(
29            from.to_bare(),
30            confirm.unwrap(),
31            time_info,
32        ));
33        return;
34    }
35
36    // For other events, a message body is required, so stop here if there isn't one
37    let Some((_lang, body)) = message.get_best_body_cloned(langs) else {
38        debug!("Received normal/chat message without body:\n{:#?}", message);
39        return;
40    };
41
42    let correction = message.extract_valid_payload::<Replace>();
43
44    if is_muc_pm {
45        if from.resource().is_none() {
46            warn!("Received malformed MessageType::Chat in muc#user namespace from a bare JID:\n{:#?}", message);
47        } else {
48            let full_from = from.clone().try_into_full().unwrap();
49
50            let event = if let Some(correction) = correction {
51                Event::RoomPrivateMessageCorrection(
52                    correction.id,
53                    full_from.to_bare(),
54                    RoomNick::from_resource_ref(full_from.resource()),
55                    body.clone(),
56                    time_info,
57                )
58            } else {
59                Event::RoomPrivateMessage(
60                    message.id.clone(),
61                    from.to_bare(),
62                    RoomNick::from_resource_ref(full_from.resource()),
63                    body.clone(),
64                    time_info,
65                )
66            };
67            events.push(event);
68        }
69    } else {
70        let event = if let Some(correction) = correction {
71            // TODO: Check that correction is valid (only for last N minutes or last N messages)
72            Event::ChatMessageCorrection(correction.id, from.to_bare(), body.clone(), time_info)
73        } else {
74            Event::ChatMessage(message.id.clone(), from.to_bare(), body, time_info)
75        };
76        events.push(event);
77    }
78}
79
80pub async fn handle_message_error(
81    _agent: &mut Agent,
82    events: &mut Vec<Event>,
83    from: Jid,
84    message: &mut Message,
85    time_info: StanzaTimeInfo,
86) {
87    let confirm = message.extract_valid_payload::<Confirm>();
88    if let Some(confirm) = confirm {
89        events.push(Event::AuthReject(from.to_bare(), confirm, time_info));
90    }
91}