1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-01 22:09:20 +02:00
xmpp-rs/xmpp/src/message/receive/chat.rs
Jonas Schäfer 7fce1146e0 Offer {Resource,Node,Domain}Ref on Jid API
This provides a non-copying API, which is generally favourable. The
other accessors were removed, because the intent was to provide this
"most sensible" API via the "default" (i.e. shortest, most concisely
named) functions.
2024-03-10 10:51:01 +01:00

60 lines
2.1 KiB
Rust

// Copyright (c) 2023 xmpp-rs contributors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// 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::connect::ServerConnector;
use tokio_xmpp::{
parsers::{message::Message, muc::user::MucUser},
Jid,
};
use crate::{delay::StanzaTimeInfo, Agent, Event};
pub async fn handle_message_chat<C: ServerConnector>(
agent: &mut Agent<C>,
events: &mut Vec<Event>,
from: Jid,
message: &Message,
time_info: StanzaTimeInfo,
) {
let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect();
if let Some((_lang, body)) = message.get_best_body(langs) {
let mut found_special_message = false;
for payload in &message.payloads {
if let Ok(_) = MucUser::try_from(payload.clone()) {
let event = match from.clone() {
Jid::Bare(bare) => {
// TODO: Can a service message be of type Chat/Normal and not Groupchat?
warn!("Received misformed MessageType::Chat in muc#user namespace from a bare JID.");
Event::ServiceMessage(
message.id.clone(),
bare,
body.clone(),
time_info.clone(),
)
}
Jid::Full(full) => Event::RoomPrivateMessage(
message.id.clone(),
full.to_bare(),
full.resource().to_string(),
body.clone(),
time_info.clone(),
),
};
found_special_message = true;
events.push(event);
}
}
if !found_special_message {
let event =
Event::ChatMessage(message.id.clone(), from.to_bare(), body.clone(), time_info);
events.push(event);
}
}
}