forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

lib.rs125 lines, 3.4 KiBHistoryRawDownload
1use std::{fs, io::Write, path::PathBuf};
2
3use common::config::Config;
4use tracing::warn;
5
6pub mod accounts;
7pub mod autostart;
8pub mod avatar;
9pub mod embed;
10pub mod emoji;
11pub mod gui;
12pub mod instance;
13pub mod lang;
14pub mod logo;
15pub mod marks;
16pub mod media;
17pub mod net;
18pub mod release;
19pub mod settings;
20pub mod share;
21pub mod tray;
22pub mod update;
23pub use usage;
24pub mod voice;
25
26pub const NAME: &str = "Yumush";
27pub const VERSION: &str = env!("CARGO_PKG_VERSION");
28pub const COMMIT: &str = env!("YUMUSH_COMMIT");
29pub const TARGET: &str = env!("YUMUSH_TARGET");
30
31static EXE: std::sync::LazyLock<Option<PathBuf>> =
32 std::sync::LazyLock::new(|| std::env::current_exe().ok());
33
34pub fn exe() -> Option<&'static std::path::Path> {
35 EXE.as_deref()
36}
37
38pub fn config_dir() -> Option<PathBuf> {
39 std::env::var_os("YUMUSH_CONFIG_DIR")
40 .map(PathBuf::from)
41 .or_else(|| dirs::config_dir().map(|dir| dir.join("yumush")))
42}
43
44fn config_file(name: &str) -> Option<PathBuf> {
45 config_dir().map(|dir| dir.join(name))
46}
47
48pub fn read_config(name: &str) -> Option<String> {
49 fs::read_to_string(config_file(name)?).ok()
50}
51
52pub fn read_config_bytes(name: &str) -> Option<Vec<u8>> {
53 fs::read(config_file(name)?).ok()
54}
55
56pub fn remove_config(name: &str) {
57 if let Some(path) = config_file(name) {
58 let _ = fs::remove_file(path);
59 }
60}
61
62pub fn write_config(name: &str, content: impl AsRef<[u8]>) {
63 let Some(path) = config_file(name) else {
64 return;
65 };
66 let result = path
67 .parent()
68 .map_or(Ok(()), fs::create_dir_all)
69 .and_then(|()| {
70 let mut options = fs::OpenOptions::new();
71 options.write(true).create(true).truncate(true);
72 #[cfg(unix)]
73 std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
74 options.open(&path)?.write_all(content.as_ref())
75 });
76
77 if let Err(error) = result {
78 warn!("save {name} failed: {error}");
79 }
80}
81
82const CLIENT_CONFIG_FILE_CONTENT: &str = include_str!(concat!(
83 env!("CARGO_MANIFEST_DIR"),
84 "/../../configs/client_config.toml"
85));
86
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub struct Server {
89 pub address: String,
90 pub any_cert: bool,
91}
92
93impl Server {
94 pub fn load() -> Self {
95 let saved = read_config("server");
96 let mut lines = saved.as_deref().unwrap_or_default().lines().map(str::trim);
97 let saved_address = lines.next();
98 let address = std::env::var("YUMUSH_SERVER_ADDRESS")
99 .ok()
100 .or_else(|| saved_address.map(str::to_string))
101 .filter(|address| !address.is_empty())
102 .unwrap_or_else(|| {
103 Config::parse(CLIENT_CONFIG_FILE_CONTENT)
104 .and_then(|config| config.get("server_address"))
105 .expect("client config server_address")
106 });
107 let any_cert =
108 std::env::var_os("YUMUSH_ANY_CERT").is_some() || lines.next() == Some("any_cert");
109 Self { address, any_cert }
110 }
111
112 pub fn save(&self) {
113 let flag = if self.any_cert { "any_cert" } else { "verify" };
114 write_config("server", format!("{}\n{flag}\n", self.address));
115 }
116
117 pub fn host(&self) -> &str {
118 match self.address.rsplit_once(':') {
119 Some((host, port)) if !host.ends_with(':') && port.parse::<u16>().is_ok() => {
120 host.trim_start_matches('[').trim_end_matches(']')
121 }
122 _ => &self.address,
123 }
124 }
125}