From 1404a9801c290e114d2ccff83eefb3238a9ee97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 26 Feb 2023 23:58:02 +0100 Subject: [PATCH] xmpp: Allow MUC join to be configured MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- xmpp/examples/hello_bot.rs | 19 +++++++------- xmpp/src/lib.rs | 54 ++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/xmpp/examples/hello_bot.rs b/xmpp/examples/hello_bot.rs index 791e33f8..1d8ce5c7 100644 --- a/xmpp/examples/hello_bot.rs +++ b/xmpp/examples/hello_bot.rs @@ -6,7 +6,7 @@ use env_logger; use std::env::args; -use xmpp::{ClientBuilder, ClientFeature, ClientType, Event}; +use xmpp::{ClientBuilder, ClientFeature, ClientType, Event, RoomJoinConfig}; use xmpp_parsers::{message::MessageType, Jid}; #[tokio::main] @@ -56,15 +56,14 @@ async fn main() -> Result<(), Option<()>> { } Event::JoinRoom(jid, conference) => { println!("Joining room {} ({:?})…", jid, conference.name); - client - .join_room( - jid, - conference.nick, - conference.password, - "en", - "Yet another bot!", - ) - .await; + let config = RoomJoinConfig { + nick: conference.nick, + password: conference.password, + lang: "en", + status: "Yet another bot!", + ..RoomJoinConfig::default() + }; + client.join_room(jid, config).await; } Event::LeaveRoom(jid) => { println!("Leaving room {}…", jid); diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index 034db7fc..670ac510 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -20,12 +20,14 @@ use tokio_xmpp::{AsyncClient as TokioXmppClient, Event as TokioXmppEvent}; use xmpp_parsers::{ bookmarks2::Conference, caps::{compute_disco, hash_caps, Caps}, + date::DateTime, disco::{DiscoInfoQuery, DiscoInfoResult, Feature, Identity}, hashes::Algo, http_upload::{Header as HttpUploadHeader, SlotRequest, SlotResult}, iq::{Iq, IqType}, message::{Body, Message, MessageType}, muc::{ + muc::History as MucHistory, user::{MucUser, Status}, Muc, }, @@ -192,6 +194,33 @@ impl ClientBuilder<'_> { } } +pub enum RoomJoinHistory { + MaxChars(u32), + MaxStanzas(u32), + MaxSeconds(u32), + Since(DateTime), +} + +pub struct RoomJoinConfig<'a> { + pub nick: Option, + pub password: Option, + pub history: Option, + pub lang: &'a str, + pub status: &'a str, +} + +impl<'a> Default for RoomJoinConfig<'a> { + fn default() -> Self { + Self { + nick: None, + password: None, + history: None, + lang: "en", + status: "", + } + } +} + pub struct Agent { client: TokioXmppClient, default_nick: Rc>, @@ -206,24 +235,27 @@ impl Agent { self.client.send_end().await } - pub async fn join_room( - &mut self, - room: BareJid, - nick: Option, - password: Option, - lang: &str, - status: &str, - ) { + pub async fn join_room(&mut self, room: BareJid, config: RoomJoinConfig<'_>) { let mut muc = Muc::new(); - if let Some(password) = password { + if let Some(password) = config.password { muc = muc.with_password(password); } + if let Some(history) = config.history { + muc.history = Some(match history { + RoomJoinHistory::MaxChars(max) => MucHistory::new().with_maxchars(max), + RoomJoinHistory::MaxStanzas(max) => MucHistory::new().with_maxstanzas(max), + RoomJoinHistory::MaxSeconds(max) => MucHistory::new().with_seconds(max), + RoomJoinHistory::Since(since) => MucHistory::new().with_since(since), + }); + }; - let nick = nick.unwrap_or_else(|| self.default_nick.borrow().clone()); + let nick = config + .nick + .unwrap_or_else(|| self.default_nick.borrow().clone()); let room_jid = room.with_resource(nick); let mut presence = Presence::new(PresenceType::None).with_to(Jid::Full(room_jid)); presence.add_payload(muc); - presence.set_status(String::from(lang), String::from(status)); + presence.set_status(String::from(config.lang), String::from(config.status)); let _ = self.client.send_stanza(presence.into()).await; }