2020-09-28 21:35:05 +00:00
|
|
|
use serde::{self, Deserialize, Serialize};
|
|
|
|
|
|
|
|
/// The "matrix" section of the config, which gives home server, login information, and etc.
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2020-10-03 20:31:42 +00:00
|
|
|
struct MatrixConfig {
|
2020-09-28 21:35:05 +00:00
|
|
|
/// Your homeserver of choice, as an FQDN without scheme or path
|
2020-10-03 20:31:42 +00:00
|
|
|
home_server: String,
|
2020-09-28 21:35:05 +00:00
|
|
|
|
|
|
|
/// Username to login as. Only the localpart.
|
2020-10-03 20:31:42 +00:00
|
|
|
username: String,
|
2020-09-28 21:35:05 +00:00
|
|
|
|
|
|
|
/// Bot account password.
|
2020-10-03 20:31:42 +00:00
|
|
|
password: String,
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_OLDEST_MESSAGE_AGE: u64 = 15 * 60;
|
|
|
|
|
|
|
|
/// The "bot" section of the config file, for bot settings.
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2020-10-03 20:31:42 +00:00
|
|
|
struct BotConfig {
|
2020-09-28 21:35:05 +00:00
|
|
|
/// How far back from current time should we process a message?
|
|
|
|
oldest_message_age: Option<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BotConfig {
|
|
|
|
/// Determine the oldest allowable message age, in seconds. If the
|
|
|
|
/// setting is defined, use that value. If it is not defined, fall
|
|
|
|
/// back to DEFAULT_OLDEST_MESSAGE_AGE (15 minutes).
|
2020-10-03 20:31:42 +00:00
|
|
|
fn oldest_message_age(&self) -> u64 {
|
2020-09-28 21:35:05 +00:00
|
|
|
match self.oldest_message_age {
|
|
|
|
Some(seconds) => seconds,
|
|
|
|
None => DEFAULT_OLDEST_MESSAGE_AGE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:31:42 +00:00
|
|
|
/// Represents the toml config file for the dicebot. The sections of
|
|
|
|
/// the config are not directly accessible; instead the config
|
|
|
|
/// provides friendly methods that handle default values, etc.
|
2020-09-28 21:35:05 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
|
|
pub struct Config {
|
2020-10-03 20:31:42 +00:00
|
|
|
matrix: MatrixConfig,
|
|
|
|
bot: Option<BotConfig>,
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2020-10-03 20:31:42 +00:00
|
|
|
/// The matrix homeserver URL.
|
|
|
|
pub fn matrix_homeserver(&self) -> &str {
|
|
|
|
&self.matrix.home_server
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The username used to connect to the matrix server.
|
|
|
|
pub fn matrix_username(&self) -> &str {
|
|
|
|
&self.matrix.username
|
|
|
|
}
|
2020-09-28 21:35:05 +00:00
|
|
|
|
2020-10-03 20:31:42 +00:00
|
|
|
/// The password used to connect to the matrix server.
|
|
|
|
pub fn matrix_password(&self) -> &str {
|
|
|
|
&self.matrix.password
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Figure out the allowed oldest message age, in seconds. This will
|
|
|
|
/// be the defined oldest message age in the bot config, if the bot
|
|
|
|
/// configuration and associated "oldest_message_age" setting are
|
|
|
|
/// defined. If the bot config or the message setting are not defined,
|
|
|
|
/// it will defualt to 15 minutes.
|
2020-10-03 20:31:42 +00:00
|
|
|
pub fn oldest_message_age(&self) -> u64 {
|
2020-09-28 21:35:05 +00:00
|
|
|
self.bot
|
|
|
|
.as_ref()
|
2020-10-03 20:31:42 +00:00
|
|
|
.map(|bc| bc.oldest_message_age())
|
|
|
|
.unwrap_or(DEFAULT_OLDEST_MESSAGE_AGE)
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn oldest_message_default_no_setting_test() {
|
|
|
|
let cfg = Config {
|
|
|
|
matrix: MatrixConfig {
|
|
|
|
home_server: "".to_owned(),
|
|
|
|
username: "".to_owned(),
|
|
|
|
password: "".to_owned(),
|
|
|
|
},
|
|
|
|
bot: Some(BotConfig {
|
|
|
|
oldest_message_age: None,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2020-10-03 20:31:42 +00:00
|
|
|
assert_eq!(15 * 60, cfg.oldest_message_age());
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn oldest_message_default_no_bot_config_test() {
|
|
|
|
let cfg = Config {
|
|
|
|
matrix: MatrixConfig {
|
|
|
|
home_server: "".to_owned(),
|
|
|
|
username: "".to_owned(),
|
|
|
|
password: "".to_owned(),
|
|
|
|
},
|
|
|
|
bot: None,
|
|
|
|
};
|
|
|
|
|
2020-10-03 20:31:42 +00:00
|
|
|
assert_eq!(15 * 60, cfg.oldest_message_age());
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
}
|