use std::{fs, future::Future, io::Write, path::PathBuf}; use tracing::warn; pub mod accounts; pub mod autostart; pub mod avatar; pub mod ci; #[cfg(target_os = "linux")] pub mod cli; pub mod embed; pub mod emoji; pub mod gui; pub mod instance; pub mod lang; pub mod logo; pub mod marks; pub mod media; pub mod net; pub mod settings; pub mod share; pub mod storage; pub mod tray; pub mod update; pub use usage; pub mod voice; pub mod worker; pub const NAME: &str = "Yumush"; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const COMMIT: &str = env!("YUMUSH_COMMIT"); pub const TARGET: &str = env!("YUMUSH_TARGET"); const DEFAULT_SERVER: &str = "127.0.0.1:2026"; static EXE: std::sync::LazyLock> = std::sync::LazyLock::new(|| std::env::current_exe().ok()); pub fn exe() -> Option<&'static std::path::Path> { EXE.as_deref() } pub fn config_dir() -> Option { std::env::var_os("YUMUSH_CONFIG_DIR") .map(PathBuf::from) .or_else(|| dirs::config_dir().map(|dir| dir.join("yumush"))) } fn config_file(name: &str) -> Option { config_dir().map(|dir| dir.join(name)) } pub fn read_config(name: &str) -> Option { fs::read_to_string(config_file(name)?).ok() } pub fn read_config_bytes(name: &str) -> Option> { fs::read(config_file(name)?).ok() } pub fn remove_config(name: &str) { if let Some(path) = config_file(name) { let _ = fs::remove_file(path); } } pub fn write_config(name: &str, content: impl AsRef<[u8]>) { let Some(path) = config_file(name) else { return; }; let temp = path.with_file_name(format!("{name}.tmp")); let result = path .parent() .map_or(Ok(()), fs::create_dir_all) .and_then(|()| { let _ = fs::remove_file(&temp); let mut options = fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); let mut file = options.open(&temp)?; file.write_all(content.as_ref())?; file.sync_all()?; fs::rename(&temp, &path) }); if let Err(error) = result { warn!("save {name} failed: {error}"); } } pub fn spawn_runtime>( name: &str, task: impl FnOnce() -> F + Send + 'static, ) { let name = name.to_string(); let _ = std::thread::Builder::new() .name(name.clone()) .spawn(move || { match tokio::runtime::Builder::new_current_thread() .enable_all() .build() { Ok(runtime) => runtime.block_on(task()), Err(error) => warn!("{name} runtime: {error}"), } }); } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Server { pub address: String, pub any_cert: bool, } impl Server { pub fn load() -> Self { let saved = read_config("server"); let mut lines = saved.as_deref().unwrap_or_default().lines().map(str::trim); let saved_address = lines.next(); let address = std::env::var("YUMUSH_SERVER_ADDRESS") .ok() .or_else(|| saved_address.map(str::to_string)) .filter(|address| !address.is_empty()) .unwrap_or_else(|| DEFAULT_SERVER.to_string()); let any_cert = std::env::var_os("YUMUSH_ANY_CERT").is_some() || lines.next() == Some("any_cert"); Self { address, any_cert } } pub fn save(&self) { let flag = if self.any_cert { "any_cert" } else { "verify" }; write_config("server", format!("{}\n{flag}\n", self.address)); } pub fn host(&self) -> &str { if let Some(bracketed) = self.address.strip_prefix('[') { return bracketed.split(']').next().unwrap_or(bracketed); } match self.address.rsplit_once(':') { Some((host, port)) if !host.contains(':') && port.parse::().is_ok() => host, _ => &self.address, } } }