2022-08-29 21:28:00 +02:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use std::{fs, path::Path};
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
pub struct Config {
|
|
|
|
pub bind_address: Option<String>,
|
|
|
|
pub bind_port: Option<String>,
|
|
|
|
pub client_storage_path: Option<String>,
|
2022-09-02 14:50:18 +02:00
|
|
|
pub receiver_allowlist: Option<Vec<String>>,
|
2022-08-29 21:28:00 +02:00
|
|
|
pub clients: Vec<ClientConfig>,
|
|
|
|
pub mappings: Vec<Mapping>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
pub struct ClientConfig {
|
|
|
|
pub mxid: String,
|
|
|
|
pub password: String,
|
|
|
|
pub device_id: Option<String>,
|
|
|
|
pub access_token: Option<String>,
|
|
|
|
}
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
pub struct Mapping {
|
|
|
|
pub from: Option<String>,
|
|
|
|
pub to: Option<String>,
|
|
|
|
pub mxid_sender: Option<String>,
|
|
|
|
pub room_id: String,
|
2022-09-01 19:17:23 +02:00
|
|
|
pub room_alias: Option<String>,
|
2022-08-29 21:28:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_config(file_path: &str) -> Config {
|
|
|
|
let yaml_str = fs::read_to_string(file_path).expect("Error while reading config file");
|
|
|
|
let mut cfg: Config = serde_yaml::from_str(&yaml_str).expect("Config file not in expected format");
|
|
|
|
|
|
|
|
// Set default values if necessary
|
|
|
|
if cfg.bind_address.is_none() {
|
|
|
|
cfg.bind_address = Some("127.0.0.1".to_string());
|
|
|
|
}
|
|
|
|
if cfg.bind_port.is_none() {
|
|
|
|
cfg.bind_port = Some("25".to_string());
|
|
|
|
}
|
|
|
|
if cfg.client_storage_path.is_none() {
|
|
|
|
let path = Path::new(file_path).parent().unwrap().join("client_storage");
|
|
|
|
cfg.client_storage_path = Some(path.to_str().unwrap().to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_config(config: &Config, old_config: &Config, file_path: &str) {
|
|
|
|
let new_yaml_str = serde_yaml::to_string(config).expect("Failed to serialize new config.");
|
|
|
|
let old_yaml_str = serde_yaml::to_string(old_config).expect("Failed to serialize old config.");
|
|
|
|
if new_yaml_str != old_yaml_str {
|
|
|
|
rotate_configs(file_path);
|
|
|
|
fs::write(file_path, &new_yaml_str).expect("Error writing new config to file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rotate_configs(base_path: &str) {
|
|
|
|
for i in 9..2 {
|
|
|
|
std::fs::copy(&format!("{}.{}", base_path, i - 1), &format!("{}.{}", base_path, i)).expect("");
|
|
|
|
}
|
|
|
|
std::fs::copy(base_path, &format!("{}.1", base_path)).expect("");
|
|
|
|
}
|