1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-18 13:45:57 +02:00
xmpp-rs/xmpp/src/muc/private_message.rs

50 lines
1.7 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::{Body, Message, MessageType},
muc::user::MucUser,
},
BareJid, Error, Jid,
};
use crate::{Agent, RoomNick};
/// Send a private message to a given MUC room member.
///
/// Returns the generated message ID.
///
/// # Arguments
///
/// * `agent`: The agent to use to send the message.
/// * `room`: The JID of the room.
/// * `recipient`: The nickname of the recipient within the room.
/// * `type_`: The type of message to send.
/// For a message to a MUC room, this should be `MessageType::Groupchat`.
/// For a private message to a MUC room member or regular contact,
/// this should be `MessageType::Chat`.
/// * `lang`: The language of the message. (See IETF RFC 5646)
/// * `text`: The text of the message.
///
/// Returns the generated message ID, or an error if the message could not be sent.
pub async fn send_room_private_message<C: ServerConnector>(
agent: &mut Agent<C>,
room: BareJid,
recipient: RoomNick,
lang: &str,
text: &str,
) -> Result<String, Error> {
let recipient: Jid = room.with_resource_str(&recipient).unwrap().into();
let mut message = Message::new(recipient).with_payload(MucUser::new());
message.type_ = MessageType::Chat;
message
.bodies
.insert(String::from(lang), Body(String::from(text)));
agent.client.send_stanza(message.into()).await
}