socket-bot/ica-rs/src/config.rs

122 lines
3.4 KiB
Rust

use std::env;
use std::fs;
use serde::Deserialize;
use toml::from_str;
use tracing::warn;
/// Icalingua bot 的配置
#[derive(Debug, Clone, Deserialize)]
pub struct IcaConfig {
/// icalingua 私钥
pub private_key: String,
/// icalingua 服务器地址
pub host: String,
/// bot 的 qq
pub self_id: u64,
/// 提醒的房间
pub notice_room: Vec<i64>,
/// 是否提醒
pub notice_start: bool,
/// 管理员列表
pub admin_list: Vec<i64>,
/// 过滤列表
pub filter_list: Vec<i64>,
}
/// Matrix 配置
#[derive(Debug, Clone, Deserialize)]
pub struct MatrixConfig {
/// home server
pub home_server: String,
/// bot_id
pub bot_id: String,
/// bot password
pub bot_password: String,
/// 提醒的房间
pub notice_room: Vec<String>,
/// 是否提醒
pub notice_start: bool,
}
/// 主配置
#[derive(Debug, Clone, Deserialize)]
pub struct BotConfig {
/// 是否启用 icalingua
pub enable_ica: Option<bool>,
/// Ica 配置
pub ica: Option<IcaConfig>,
/// 是否启用 Matrix
pub enable_matrix: Option<bool>,
/// Matrix 配置
pub matrix: Option<MatrixConfig>,
/// Python 插件路径
pub py_plugin_path: Option<String>,
/// Python 配置文件路径
pub py_config_path: Option<String>,
}
impl BotConfig {
pub fn new_from_path(config_file_path: String) -> Self {
// try read config from file
let config = fs::read_to_string(&config_file_path).expect("Failed to read config file");
let ret: Self = from_str(&config)
.expect(format!("Failed to parse config file {}", &config_file_path).as_str());
ret
}
pub fn new_from_cli() -> Self {
let config_file_path = env::args().nth(1).expect("No config path given");
Self::new_from_path(config_file_path)
}
/// 检查是否启用 ica
pub fn check_ica(&self) -> bool {
match self.enable_ica {
Some(enable) => {
if enable {
if let None = self.ica {
warn!("enable_ica 为 true 但未填写 [ica] 配置\n将不启用 ica");
false
} else {
true
}
} else {
false
}
}
None => {
if let Some(_) = self.ica {
warn!("未填写 enable_ica 但填写了 [ica] 配置\n将不启用 ica");
}
false
}
}
}
/// 检查是否启用 Matrix
pub fn check_matrix(&self) -> bool {
match self.enable_matrix {
Some(enable) => {
if enable {
if let None = self.matrix {
warn!("enable_matrix 为 true 但未填写 [matrix] 配置\n将不启用 Matrix");
false
} else {
true
}
} else {
false
}
}
None => {
if let Some(_) = self.matrix {
warn!("未填写 enable_matrix 但填写了 [matrix] 配置\n将不启用 Matrix");
}
false
}
}
}
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
}