//! Channel-chat relays: one query session sitting in each relayed channel. //! //! TeamSpeak only delivers channel chat to clients in that channel, and a //! client can only post to its own channel. A relay session is an invisible //! query client moved into the channel; it forwards what is said there and //! posts messages on behalf of users under their own nickname ([`post_as`]). use std::collections::HashMap; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Instant; use tokio::sync::{Mutex, broadcast, mpsc}; use tokio::task::JoinHandle; use tracing::{debug, info, warn}; use voelin_model::{ChannelId, ChatMessage, ChatTarget, relay_text, split_message}; use voelin_query::{Command, Connect, Notification, QueryClient}; use crate::convert::is_text_message; /// `clientupdate client_nickname=` with a name someone else has. const ERR_NICKNAME_IN_USE: u32 = 513; /// `clientmove` into the channel the client is already in. const ERR_ALREADY_MEMBER: u32 = 770; /// TeamSpeak's limit for one text message. const MAX_MESSAGE_BYTES: usize = 1024; /// TeamSpeak's limits for a nickname, in characters. const MIN_NICKNAME_CHARS: usize = 3; const MAX_NICKNAME_CHARS: usize = 30; #[derive(Clone, Debug)] pub struct RelayConfig { pub connect: Connect, /// Base nickname; the channel id is appended because nicknames are unique. pub nickname: String, /// How posts appear when the relay cannot take the author's nickname /// ([`post_as`]), with `{nick}` and `{text}` placeholders. pub format: String, } impl RelayConfig { pub fn new(connect: Connect) -> Self { Self { connect, nickname: "Chat Relay".into(), format: "[{nick}] {text}".into() } } } #[derive(Clone, Debug, PartialEq)] pub enum RelayEvent { /// Someone wrote in a relayed channel (our own posts are not echoed). Message(ChatMessage), /// A relay stopped (connection lost or closed). Closed { channel: ChannelId, reason: String }, } struct Session { client: QueryClient, opened: Instant, /// The relay's own nickname, taken back after each post. nickname: String, /// One post at a time: each borrows the author's nickname. posting: Arc>, task: JoinHandle<()>, } impl Drop for Session { fn drop(&mut self) { self.task.abort(); } } /// Manages relay sessions, at most one per channel. #[derive(Clone)] pub struct RelayPool { config: Arc, sessions: Arc>>, /// Held while a channel's relay opens or closes, so opening one channel /// (a login and a few commands) holds up nobody in other channels. gates: Arc>>>>, /// Set by [`RelayPool::close_all`]: the pool opens no more relays. closed: Arc, events: broadcast::Sender, } impl RelayPool { pub fn new(config: RelayConfig) -> Self { let (events, _) = broadcast::channel(1024); Self { config: Arc::new(config), sessions: Default::default(), gates: Default::default(), closed: Default::default(), events, } } fn gate(&self, channel: ChannelId) -> Arc> { let mut gates = self.gates.lock().unwrap_or_else(std::sync::PoisonError::into_inner); gates.entry(channel).or_default().clone() } pub fn subscribe(&self) -> broadcast::Receiver { self.events.subscribe() } /// Channels with an open relay. pub async fn channels(&self) -> Vec { self.sessions.lock().await.keys().copied().collect() } /// [`RelayPool::close_all`] was called: opening fails with /// [`voelin_query::Error::Closed`]. pub fn is_closed(&self) -> bool { self.closed.load(Ordering::Relaxed) } /// Start relaying a channel (no-op if already open). pub async fn open(&self, channel: ChannelId) -> voelin_query::Result<()> { let gate = self.gate(channel); let _opening = gate.lock().await; if self.is_closed() { return Err(voelin_query::Error::Closed); } let sessions = self.sessions.lock().await; if sessions.get(&channel).is_some_and(|s| !s.task.is_finished()) { return Ok(()); } drop(sessions); let started = Instant::now(); let session = self.connect(channel).await.inspect_err(|error| { let elapsed_ms = started.elapsed().as_millis() as u64; warn!(channel, elapsed_ms, %error, "relay could not be opened"); })?; self.sessions.lock().await.insert(channel, session); Ok(()) } async fn connect(&self, channel: ChannelId) -> voelin_query::Result { let started = Instant::now(); let mut connect = self.config.connect.clone(); connect.line.label = format!("relay {channel}"); let (client, notifications) = QueryClient::connect(&connect).await?; let Some(notifications) = notifications else { return Err(voelin_query::Error::Unsupported("relays need a transport with events")); }; let own_id = client.own_client_id().await?; let nickname = set_nickname(&client, &format!("{} {channel}", self.config.nickname)).await; match client.send(&Command::new("clientmove").arg("clid", own_id).arg("cid", channel)).await { // Query clients start in the default channel. Err(voelin_query::Error::Query(e)) if e.id == ERR_ALREADY_MEMBER => {} other => { other?; } } client.send(&Command::new("servernotifyregister").arg("event", "textchannel")).await?; let elapsed_ms = started.elapsed().as_millis() as u64; info!(channel, own_id, elapsed_ms, "relay opened"); let task = tokio::spawn(forward(channel, own_id, notifications, self.events.clone())); Ok(Session { client, opened: Instant::now(), nickname, posting: Default::default(), task }) } /// Stop relaying a channel. pub async fn close(&self, channel: ChannelId) { self.close_one(channel).await; } /// Stop relaying a channel; whether it was relayed. async fn close_one(&self, channel: ChannelId) -> bool { // After an open in progress, which would otherwise come back with it. let gate = self.gate(channel); let _closing = gate.lock().await; let Some(session) = self.sessions.lock().await.remove(&channel) else { return false }; // Not a lost connection: no `Closed` event for it. session.task.abort(); session.client.quit().await; info!(channel, open_for_s = session.opened.elapsed().as_secs(), "relay closed"); true } /// Stop all relays, after any opens in progress, and open no more. /// Returns the channels that were relayed. pub async fn close_all(&self) -> Vec { self.closed.store(true, Ordering::Relaxed); // Every channel that has a relay or is opening one has a gate. let channels: Vec = { let gates = self.gates.lock().unwrap_or_else(std::sync::PoisonError::into_inner); gates.keys().copied().collect() }; let mut closed = Vec::new(); for channel in channels { if self.close_one(channel).await { closed.push(channel); } } closed } /// Post `text` in `channel` as `nick` wrote it ([`post_as`]), opening a /// relay if needed. pub async fn send( &self, channel: ChannelId, nick: &str, text: &str, ) -> voelin_query::Result<()> { self.post(channel, nick, text, &self.config.format).await } /// [`RelayPool::send`] with another fallback `format` than the pool's. pub async fn post( &self, channel: ChannelId, nick: &str, text: &str, format: &str, ) -> voelin_query::Result<()> { self.open(channel).await?; let (client, nickname, posting) = match self.sessions.lock().await.get(&channel) { Some(s) => (s.client.clone(), s.nickname.clone(), s.posting.clone()), None => return Err(voelin_query::Error::Closed), }; let _posting = posting.lock().await; post_as(&client, &nickname, nick, text, format, |part| { Command::new("sendtextmessage") .arg("targetmode", 2) .arg("target", channel) .arg("msg", part) }) .await } } /// Post `text` through the query session `client` as if `nick` wrote it: the /// session takes the nickname for the post (or `nick1`, `nick2`, … while /// someone has it, as TeamSpeak names a second client of the same name) and /// goes back to `own_nickname` afterwards, so it never keeps someone's name. /// When it cannot take any (too short, refused), the post goes out under the /// session's own name as `format` (`{nick}`, `{text}`). `command` makes the /// `sendtextmessage` for one part of the text. /// /// Callers serialize posts through one session: each borrows a nickname. pub async fn post_as( client: &QueryClient, own_nickname: &str, nick: &str, text: &str, format: &str, command: impl Fn(String) -> Command, ) -> voelin_query::Result<()> { let taken = take_nickname(client, nick).await; let full = if taken { text.to_owned() } else { relay_text(format, nick, text) }; let mut result = Ok(()); for part in split_message(&full, MAX_MESSAGE_BYTES) { result = client.send(&command(part)).await.map(drop); if result.is_err() { break; } } if taken && !own_nickname.is_empty() { set_nickname(client, own_nickname).await; } result } /// Name the session `nick` or a numbered variant; whether it did. async fn take_nickname(client: &QueryClient, nick: &str) -> bool { for nickname in author_nicknames(nick) { match client.send(&Command::new("clientupdate").arg("client_nickname", &nickname)).await { Ok(_) => return true, Err(voelin_query::Error::Query(e)) if e.id == ERR_NICKNAME_IN_USE => continue, Err(e) => { debug!(%e, "could not take the author's nickname"); return false; } } } false } /// The names a post by `nick` may go out under, in order: the nickname /// itself, then `nick1` to `nick3`, each within TeamSpeak's length limits. fn author_nicknames(nick: &str) -> Vec { let nick = nick.trim(); if nick.chars().count() < MIN_NICKNAME_CHARS { return Vec::new(); } let fit = |suffix: &str| { let head: String = nick.chars().take(MAX_NICKNAME_CHARS - suffix.len()).collect(); format!("{}{suffix}", head.trim_end()) }; std::iter::once(fit("")).chain((1..=3).map(|n| fit(&n.to_string()))).collect() } /// Nicknames are unique per server; try a few numbered variants. The name is /// cosmetic, so failing keeps the server-assigned one. The name it got. async fn set_nickname(client: &QueryClient, base: &str) -> String { for attempt in 1..=5 { let nickname = if attempt == 1 { base.to_string() } else { format!("{base} ({attempt})") }; match client.send(&Command::new("clientupdate").arg("client_nickname", &nickname)).await { Ok(_) => return nickname, Err(voelin_query::Error::Query(e)) if e.id == ERR_NICKNAME_IN_USE => continue, Err(e) => { warn!(%e, %nickname, "could not set relay nickname"); return base.to_owned(); } } } warn!(base, "all relay nicknames in use"); base.to_owned() } async fn forward( channel: ChannelId, own_id: u16, mut notifications: mpsc::UnboundedReceiver, events: broadcast::Sender, ) { let opened = Instant::now(); while let Some(n) = notifications.recv().await { let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_millis() as i64) .unwrap_or_default(); let Some(mut msg) = is_text_message(&n, now) else { continue }; if msg.author_id == Some(own_id) { continue; } if msg.target == ChatTarget::Channel(0) { msg.target = ChatTarget::Channel(channel); } debug!(channel, author = %msg.author_name, "relayed message"); let _ = events.send(RelayEvent::Message(msg)); } // Closing a relay stops this task before it quits, so this is a loss. warn!(channel, open_for_s = opened.elapsed().as_secs(), "relay connection lost"); let _ = events.send(RelayEvent::Closed { channel, reason: "connection closed".into() }); } #[cfg(test)] mod tests { use super::*; #[tokio::test] async fn a_closed_pool_opens_no_relays() { let connect = Connect { transport: voelin_query::Transport::Raw, addr: "127.0.0.1:9".into(), user: "serveradmin".into(), secret: None, server_port: None, server_id: None, line: Default::default(), }; let pool = RelayPool::new(RelayConfig::new(connect)); assert!(!pool.is_closed()); assert!(pool.close_all().await.is_empty()); assert!(pool.is_closed()); // Without trying to connect. assert!(matches!(pool.open(1).await, Err(voelin_query::Error::Closed))); } #[test] fn posts_go_out_under_the_authors_name_then_numbered_variants() { assert_eq!(author_nicknames(" Alice "), ["Alice", "Alice1", "Alice2", "Alice3"]); // Too short for TeamSpeak: the relay's own name and the format. assert!(author_nicknames("Al").is_empty()); let long = "x".repeat(40); let names = author_nicknames(&long); assert_eq!(names[0], "x".repeat(30)); assert_eq!(names[1], format!("{}1", "x".repeat(29))); assert!(names.iter().all(|n| n.chars().count() <= MAX_NICKNAME_CHARS)); // Characters, not bytes. assert_eq!(author_nicknames(&"é".repeat(31))[0].chars().count(), 30); } }