2017-06-14 22:20:31 +02:00
|
|
|
//! JSON configuration files using serde
|
2014-12-23 12:53:30 -05:00
|
|
|
use std::borrow::ToOwned;
|
2014-11-02 18:16:49 -05:00
|
|
|
use std::collections::HashMap;
|
2015-03-02 17:12:16 -05:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
2017-03-15 12:42:15 -04:00
|
|
|
use std::net::{SocketAddr, ToSocketAddrs};
|
2018-01-28 00:52:11 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-06-22 14:14:49 -04:00
|
|
|
|
2017-06-29 00:31:27 -07:00
|
|
|
#[cfg(feature = "json")]
|
2017-06-14 22:20:31 +02:00
|
|
|
use serde_json;
|
2017-06-29 00:31:27 -07:00
|
|
|
#[cfg(feature = "yaml")]
|
|
|
|
use serde_yaml;
|
|
|
|
#[cfg(feature = "toml")]
|
|
|
|
use toml;
|
2017-06-22 14:14:49 -04:00
|
|
|
|
2018-01-28 00:52:11 +01:00
|
|
|
#[cfg(feature = "toml")]
|
|
|
|
use error::TomlError;
|
|
|
|
use error::{ConfigError, Result};
|
|
|
|
use error::IrcError::InvalidConfig;
|
2014-11-02 18:16:49 -05:00
|
|
|
|
2014-11-06 15:23:02 -05:00
|
|
|
/// Configuration data.
|
2017-06-14 22:20:31 +02:00
|
|
|
#[derive(Clone, Deserialize, Serialize, Default, PartialEq, Debug)]
|
2014-11-02 18:16:49 -05:00
|
|
|
pub struct Config {
|
2016-01-15 12:37:16 -05:00
|
|
|
/// A list of the owners of the client by nickname (for bots).
|
2014-12-04 21:04:22 -05:00
|
|
|
pub owners: Option<Vec<String>>,
|
2016-01-15 12:37:16 -05:00
|
|
|
/// The client's nickname.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub nickname: Option<String>,
|
2016-01-15 12:37:16 -05:00
|
|
|
/// The client's NICKSERV password.
|
2014-12-09 17:01:47 -05:00
|
|
|
pub nick_password: Option<String>,
|
2016-01-15 12:37:16 -05:00
|
|
|
/// Alternative nicknames for the client, if the default is taken.
|
2014-12-06 18:49:07 -05:00
|
|
|
pub alt_nicks: Option<Vec<String>>,
|
2016-01-15 12:37:16 -05:00
|
|
|
/// The client's username.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub username: Option<String>,
|
2016-01-15 12:37:16 -05:00
|
|
|
/// The client's real name.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub realname: Option<String>,
|
2014-11-06 15:23:02 -05:00
|
|
|
/// The server to connect to.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub server: Option<String>,
|
2014-11-06 15:23:02 -05:00
|
|
|
/// The port to connect on.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub port: Option<u16>,
|
2014-12-09 16:17:05 -05:00
|
|
|
/// The password to connect to the server.
|
|
|
|
pub password: Option<String>,
|
2014-11-08 17:35:19 -05:00
|
|
|
/// Whether or not to use SSL.
|
2016-01-15 12:37:16 -05:00
|
|
|
/// Clients will automatically panic if this is enabled without SSL support.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub use_ssl: Option<bool>,
|
2017-06-21 23:21:03 -04:00
|
|
|
/// The path to the SSL certificate for this server in DER format.
|
|
|
|
pub cert_path: Option<String>,
|
2014-11-30 01:29:38 -05:00
|
|
|
/// The encoding type used for this connection.
|
|
|
|
/// This is typically UTF-8, but could be something else.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub encoding: Option<String>,
|
2014-11-06 15:23:02 -05:00
|
|
|
/// A list of channels to join on connection.
|
2014-12-04 21:04:22 -05:00
|
|
|
pub channels: Option<Vec<String>>,
|
2017-06-22 13:59:18 -04:00
|
|
|
/// User modes to set on connect. Example: "+RB -x"
|
2015-02-17 13:20:59 -05:00
|
|
|
pub umodes: Option<String>,
|
2014-12-22 16:35:56 -05:00
|
|
|
/// The text that'll be sent in response to CTCP USERINFO requests.
|
|
|
|
pub user_info: Option<String>,
|
2017-03-22 22:06:43 +01:00
|
|
|
/// The text that'll be sent in response to CTCP VERSION requests.
|
|
|
|
pub version: Option<String>,
|
|
|
|
/// The text that'll be sent in response to CTCP SOURCE requests.
|
|
|
|
pub source: Option<String>,
|
2016-01-15 12:37:16 -05:00
|
|
|
/// The amount of inactivity in seconds before the client will ping the server.
|
|
|
|
pub ping_time: Option<u32>,
|
|
|
|
/// The amount of time in seconds for a client to reconnect due to no ping response.
|
|
|
|
pub ping_timeout: Option<u32>,
|
2018-01-01 21:47:28 -05:00
|
|
|
/// The length in seconds of a rolling window for message throttling. If more than
|
|
|
|
/// `max_messages_in_burst` messages are sent within `burst_window_length` seconds, additional
|
|
|
|
/// messages will be delayed automatically as appropriate. In particular, in the past
|
|
|
|
/// `burst_window_length` seconds, there will never be more than `max_messages_in_burst` messages
|
|
|
|
/// sent.
|
2017-06-28 23:07:02 -07:00
|
|
|
pub burst_window_length: Option<u32>,
|
|
|
|
/// The maximum number of messages that can be sent in a burst window before they'll be delayed.
|
2018-01-01 21:47:28 -05:00
|
|
|
/// Messages are automatically delayed as appropriate.
|
2017-06-28 23:07:02 -07:00
|
|
|
pub max_messages_in_burst: Option<u32>,
|
2017-06-21 17:15:30 -04:00
|
|
|
/// Whether the client should use NickServ GHOST to reclaim its primary nickname if it is in
|
|
|
|
/// use. This has no effect if `nick_password` is not set.
|
2016-02-09 18:24:52 +08:00
|
|
|
pub should_ghost: Option<bool>,
|
2017-06-21 17:15:30 -04:00
|
|
|
/// The command(s) that should be sent to NickServ to recover a nickname. The nickname and
|
|
|
|
/// password will be appended in that order after the command.
|
|
|
|
/// E.g. `["RECOVER", "RELEASE"]` means `RECOVER nick pass` and `RELEASE nick pass` will be sent
|
|
|
|
/// in that order.
|
2016-02-09 18:24:52 +08:00
|
|
|
pub ghost_sequence: Option<Vec<String>>,
|
2017-06-21 19:19:58 -04:00
|
|
|
/// Whether or not to use a fake connection for testing purposes. You probably will never want
|
|
|
|
/// to enable this, but it is used in unit testing for the `irc` crate.
|
|
|
|
pub use_mock_connection: Option<bool>,
|
|
|
|
/// The initial value used by the fake connection for testing. You probably will never need to
|
|
|
|
/// set this, but it is used in unit testing for the `irc` crate.
|
|
|
|
pub mock_initial_value: Option<String>,
|
2017-08-04 20:23:07 -04:00
|
|
|
|
|
|
|
/// A mapping of channel names to keys for join-on-connect.
|
|
|
|
pub channel_keys: Option<HashMap<String, String>>,
|
|
|
|
/// A map of additional options to be stored in config.
|
|
|
|
pub options: Option<HashMap<String, String>>,
|
2018-01-28 00:52:11 +01:00
|
|
|
|
|
|
|
/// The path that this configuration was loaded from.
|
|
|
|
///
|
|
|
|
/// This should not be specified in any configuration. It will automatically be handled by the library.
|
|
|
|
pub path: Option<PathBuf>,
|
2014-11-02 18:16:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2018-01-28 00:52:11 +01:00
|
|
|
fn with_path<P: AsRef<Path>>(mut self, path: P) -> Config {
|
|
|
|
self.path = Some(path.as_ref().to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> String {
|
|
|
|
self.path.as_ref().map(|buf| buf.to_string_lossy().into_owned()).unwrap_or_else(|| {
|
|
|
|
"<none>".to_owned()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-29 00:31:27 -07:00
|
|
|
/// Loads a configuration from the desired path. This will use the file extension to detect
|
|
|
|
/// which format to parse the file as (json, toml, or yaml). Using each format requires having
|
|
|
|
/// its respective crate feature enabled. Only json is available by default.
|
2015-06-05 21:37:21 -04:00
|
|
|
pub fn load<P: AsRef<Path>>(path: P) -> Result<Config> {
|
2017-06-29 00:31:27 -07:00
|
|
|
let mut file = File::open(&path)?;
|
2015-03-02 17:12:16 -05:00
|
|
|
let mut data = String::new();
|
2017-06-25 05:06:35 -04:00
|
|
|
file.read_to_string(&mut data)?;
|
2017-06-29 00:31:27 -07:00
|
|
|
|
2018-01-28 00:52:11 +01:00
|
|
|
let res = match path.as_ref().extension().and_then(|s| s.to_str()) {
|
|
|
|
Some("json") => Config::load_json(&path, &data),
|
|
|
|
Some("toml") => Config::load_toml(&path, &data),
|
|
|
|
Some("yaml") | Some("yml") => Config::load_yaml(&path, &data),
|
|
|
|
Some(ext) => Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::UnknownConfigFormat {
|
|
|
|
format: ext.to_owned(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
None => Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::MissingExtension,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
res.map(|config| {
|
|
|
|
config.with_path(path)
|
|
|
|
})
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "json")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_json<P: AsRef<Path>>(path: &P, data: &str) -> Result<Config> {
|
|
|
|
serde_json::from_str(&data[..]).map_err(|e| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::InvalidJson(e),
|
|
|
|
}
|
2017-06-19 13:59:26 -04:00
|
|
|
})
|
2014-11-02 18:16:49 -05:00
|
|
|
}
|
|
|
|
|
2017-06-29 00:31:27 -07:00
|
|
|
#[cfg(not(feature = "json"))]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_json<P: AsRef<Path>>(path: &P, _: &str) -> Result<Config> {
|
|
|
|
Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::ConfigFormatDisabled {
|
|
|
|
format: "JSON"
|
|
|
|
}
|
|
|
|
})
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "toml")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_toml<P: AsRef<Path>>(path: &P, data: &str) -> Result<Config> {
|
|
|
|
toml::from_str(&data[..]).map_err(|e| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::InvalidToml(TomlError::Read(e)),
|
|
|
|
}
|
2017-06-29 00:31:27 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "toml"))]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_toml<P: AsRef<Path>>(path: &P, _: &str) -> Result<Config> {
|
|
|
|
Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::ConfigFormatDisabled {
|
|
|
|
format: "TOML"
|
|
|
|
}
|
|
|
|
})
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "yaml")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_yaml<P: AsRef<Path>>(path: &P, data: &str) -> Result<Config> {
|
|
|
|
serde_yaml::from_str(&data[..]).map_err(|e| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::InvalidYaml(e),
|
|
|
|
}
|
2017-06-29 00:31:27 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "yaml"))]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_yaml<P: AsRef<Path>>(path: &P, _: &str) -> Result<Config> {
|
|
|
|
Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::ConfigFormatDisabled {
|
|
|
|
format: "YAML"
|
|
|
|
}
|
|
|
|
})
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Saves a configuration to the desired path. This will use the file extension to detect
|
|
|
|
/// which format to parse the file as (json, toml, or yaml). Using each format requires having
|
|
|
|
/// its respective crate feature enabled. Only json is available by default.
|
2018-01-28 00:52:11 +01:00
|
|
|
pub fn save<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
|
|
|
|
let _ = self.path.take();
|
2017-06-29 00:31:27 -07:00
|
|
|
let mut file = File::create(&path)?;
|
|
|
|
let data = match path.as_ref().extension().and_then(|s| s.to_str()) {
|
2018-01-28 00:52:11 +01:00
|
|
|
Some("json") => self.save_json(&path)?,
|
|
|
|
Some("toml") => self.save_toml(&path)?,
|
|
|
|
Some("yaml") | Some("yml") => self.save_yaml(&path)?,
|
|
|
|
Some(ext) => return Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::UnknownConfigFormat {
|
|
|
|
format: ext.to_owned(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
None => return Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::MissingExtension,
|
|
|
|
}),
|
2017-06-29 00:31:27 -07:00
|
|
|
};
|
|
|
|
file.write_all(data.as_bytes())?;
|
2018-01-28 00:52:11 +01:00
|
|
|
self.path = Some(path.as_ref().to_owned());
|
2017-06-29 00:31:27 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "json")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn save_json<P: AsRef<Path>>(&self, path: &P) -> Result<String> {
|
|
|
|
serde_json::to_string(self).map_err(|e| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::InvalidJson(e),
|
|
|
|
}
|
2017-06-29 00:31:27 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "json"))]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn save_json<P: AsRef<Path>>(&self, path: &P) -> Result<String> {
|
|
|
|
Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::ConfigFormatDisabled {
|
|
|
|
format: "JSON"
|
|
|
|
}
|
|
|
|
})
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "toml")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn save_toml<P: AsRef<Path>>(&self, path: &P) -> Result<String> {
|
|
|
|
toml::to_string(self).map_err(|e| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::InvalidToml(TomlError::Write(e)),
|
|
|
|
}
|
2017-06-29 00:31:27 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "toml"))]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn save_toml<P: AsRef<Path>>(&self, path: &P) -> Result<String> {
|
|
|
|
Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::ConfigFormatDisabled {
|
|
|
|
format: "TOML"
|
|
|
|
}
|
|
|
|
})
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "yaml")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn save_yaml<P: AsRef<Path>>(&self, path: &P) -> Result<String> {
|
|
|
|
serde_yaml::to_string(self).map_err(|e| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::InvalidYaml(e),
|
|
|
|
}
|
2017-06-29 00:31:27 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "yaml"))]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn save_yaml<P: AsRef<Path>>(&self, path: &P) -> Result<String> {
|
|
|
|
Err(InvalidConfig {
|
|
|
|
path: path.as_ref().to_string_lossy().into_owned(),
|
|
|
|
cause: ConfigError::ConfigFormatDisabled {
|
|
|
|
format: "YAML"
|
|
|
|
}
|
|
|
|
})
|
2015-09-16 12:21:58 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 15:23:02 -05:00
|
|
|
/// Determines whether or not the nickname provided is the owner of the bot.
|
2014-11-02 18:16:49 -05:00
|
|
|
pub fn is_owner(&self, nickname: &str) -> bool {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.owners
|
|
|
|
.as_ref()
|
|
|
|
.map(|o| o.contains(&nickname.to_owned()))
|
|
|
|
.unwrap()
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the nickname specified in the configuration.
|
2018-01-27 20:26:56 +01:00
|
|
|
pub fn nickname(&self) -> Result<&str> {
|
2018-01-28 00:52:11 +01:00
|
|
|
self.nickname.as_ref().map(|s| &s[..]).ok_or_else(|| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: self.path(),
|
|
|
|
cause: ConfigError::NicknameNotSpecified,
|
|
|
|
}
|
|
|
|
})
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:01:47 -05:00
|
|
|
/// Gets the bot's nickserv password specified in the configuration.
|
|
|
|
/// This defaults to an empty string when not specified.
|
|
|
|
pub fn nick_password(&self) -> &str {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.nick_password.as_ref().map_or("", |s| &s[..])
|
2014-12-09 17:01:47 -05:00
|
|
|
}
|
|
|
|
|
2014-12-06 18:49:07 -05:00
|
|
|
/// Gets the alternate nicknames specified in the configuration.
|
|
|
|
/// This defaults to an empty vector when not specified.
|
2016-01-30 11:59:57 -05:00
|
|
|
pub fn alternate_nicknames(&self) -> Vec<&str> {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.alt_nicks.as_ref().map_or(vec![], |v| {
|
|
|
|
v.iter().map(|s| &s[..]).collect()
|
|
|
|
})
|
2014-12-06 18:49:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-04 21:04:22 -05:00
|
|
|
/// Gets the username specified in the configuration.
|
|
|
|
/// This defaults to the user's nickname when not specified.
|
|
|
|
pub fn username(&self) -> &str {
|
2018-01-27 20:26:56 +01:00
|
|
|
self.username.as_ref().map_or(self.nickname().unwrap_or("user"), |s| &s)
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the real name specified in the configuration.
|
|
|
|
/// This defaults to the user's nickname when not specified.
|
|
|
|
pub fn real_name(&self) -> &str {
|
2018-01-27 20:26:56 +01:00
|
|
|
self.realname.as_ref().map_or(self.nickname().unwrap_or("irc"), |s| &s)
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the address of the server specified in the configuration.
|
2018-01-27 20:26:56 +01:00
|
|
|
pub fn server(&self) -> Result<&str> {
|
2018-01-28 00:52:11 +01:00
|
|
|
self.server.as_ref().map(|s| &s[..]).ok_or_else(|| {
|
|
|
|
InvalidConfig {
|
|
|
|
path: self.path(),
|
|
|
|
cause: ConfigError::ServerNotSpecified,
|
|
|
|
}
|
|
|
|
})
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the port of the server specified in the configuration.
|
|
|
|
/// This defaults to 6667 (or 6697 if use_ssl is specified as true) when not specified.
|
|
|
|
pub fn port(&self) -> u16 {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.port.as_ref().cloned().unwrap_or(if self.use_ssl() {
|
2014-12-04 21:04:22 -05:00
|
|
|
6697
|
|
|
|
} else {
|
|
|
|
6667
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-15 17:32:41 -05:00
|
|
|
/// Gets the server and port as a `SocketAddr`.
|
|
|
|
/// This panics when server is not specified or the address is malformed.
|
2017-10-17 00:38:59 +02:00
|
|
|
pub fn socket_addr(&self) -> Result<SocketAddr> {
|
2018-01-27 20:26:56 +01:00
|
|
|
format!("{}:{}", self.server()?, self.port()).to_socket_addrs()
|
2017-10-17 00:38:59 +02:00
|
|
|
.map(|mut i| i.next().unwrap())
|
|
|
|
.map_err(|e| e.into())
|
2017-01-15 17:32:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 16:17:05 -05:00
|
|
|
/// Gets the server password specified in the configuration.
|
|
|
|
/// This defaults to a blank string when not specified.
|
|
|
|
pub fn password(&self) -> &str {
|
2017-06-21 19:19:58 -04:00
|
|
|
self.password.as_ref().map_or("", |s| &s)
|
2014-12-09 16:17:05 -05:00
|
|
|
}
|
|
|
|
|
2014-12-04 21:04:22 -05:00
|
|
|
/// Gets whether or not to use SSL with this connection.
|
|
|
|
/// This defaults to false when not specified.
|
|
|
|
pub fn use_ssl(&self) -> bool {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.use_ssl.as_ref().cloned().unwrap_or(false)
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-21 23:21:03 -04:00
|
|
|
/// Gets the path to the SSL certificate in DER format if specified.
|
|
|
|
pub fn cert_path(&self) -> Option<&str> {
|
|
|
|
self.cert_path.as_ref().map(|s| &s[..])
|
|
|
|
}
|
|
|
|
|
2014-12-04 21:04:22 -05:00
|
|
|
/// Gets the encoding to use for this connection. This requires the encode feature to work.
|
|
|
|
/// This defaults to UTF-8 when not specified.
|
|
|
|
pub fn encoding(&self) -> &str {
|
2017-06-21 19:19:58 -04:00
|
|
|
self.encoding.as_ref().map_or("UTF-8", |s| &s)
|
2014-12-04 21:04:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the channels to join upon connection.
|
|
|
|
/// This defaults to an empty vector if it's not specified.
|
|
|
|
pub fn channels(&self) -> Vec<&str> {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.channels.as_ref().map_or(vec![], |v| {
|
|
|
|
v.iter().map(|s| &s[..]).collect()
|
|
|
|
})
|
2014-11-02 18:16:49 -05:00
|
|
|
}
|
2014-11-29 05:49:20 -05:00
|
|
|
|
2016-07-05 16:21:21 -04:00
|
|
|
|
|
|
|
/// Gets the key for the specified channel if it exists in the configuration.
|
|
|
|
pub fn channel_key(&self, chan: &str) -> Option<&str> {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.channel_keys.as_ref().and_then(|m| {
|
|
|
|
m.get(&chan.to_owned()).map(|s| &s[..])
|
|
|
|
})
|
2016-07-05 16:21:21 -04:00
|
|
|
}
|
|
|
|
|
2015-02-17 13:20:59 -05:00
|
|
|
/// Gets the user modes to set on connect specified in the configuration.
|
|
|
|
/// This defaults to an empty string when not specified.
|
|
|
|
pub fn umodes(&self) -> &str {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.umodes.as_ref().map_or("", |s| &s[..])
|
2015-02-17 13:20:59 -05:00
|
|
|
}
|
|
|
|
|
2014-12-22 16:35:56 -05:00
|
|
|
/// Gets the string to be sent in response to CTCP USERINFO requests.
|
|
|
|
/// This defaults to an empty string when not specified.
|
|
|
|
pub fn user_info(&self) -> &str {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.user_info.as_ref().map_or("", |s| &s[..])
|
2014-12-22 16:35:56 -05:00
|
|
|
}
|
|
|
|
|
2017-03-22 22:06:43 +01:00
|
|
|
/// Gets the string to be sent in response to CTCP VERSION requests.
|
2017-06-22 15:04:27 -04:00
|
|
|
/// This defaults to `irc:version:env` when not specified.
|
|
|
|
/// For example, `irc:0.12.0:Compiled with rustc`
|
2017-03-22 22:06:43 +01:00
|
|
|
pub fn version(&self) -> &str {
|
2017-06-22 15:04:27 -04:00
|
|
|
self.version.as_ref().map_or(::VERSION_STR, |s| &s)
|
2017-03-22 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the string to be sent in response to CTCP SOURCE requests.
|
|
|
|
/// This defaults to `https://github.com/aatxe/irc` when not specified.
|
|
|
|
pub fn source(&self) -> &str {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.source.as_ref().map_or(
|
|
|
|
"https://github.com/aatxe/irc",
|
|
|
|
|s| &s[..],
|
|
|
|
)
|
2017-03-22 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:44:22 -07:00
|
|
|
/// Gets the amount of time in seconds for the interval at which the client pings the server.
|
2016-01-15 12:37:16 -05:00
|
|
|
/// This defaults to 180 seconds when not specified.
|
|
|
|
pub fn ping_time(&self) -> u32 {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.ping_time.as_ref().cloned().unwrap_or(180)
|
2016-01-15 12:37:16 -05:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:44:22 -07:00
|
|
|
/// Gets the amount of time in seconds for the client to disconnect after not receiving a ping
|
|
|
|
/// response.
|
2016-01-15 12:37:16 -05:00
|
|
|
/// This defaults to 10 seconds when not specified.
|
|
|
|
pub fn ping_timeout(&self) -> u32 {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.ping_timeout.as_ref().cloned().unwrap_or(10)
|
2016-01-15 12:37:16 -05:00
|
|
|
}
|
|
|
|
|
2018-01-01 21:47:28 -05:00
|
|
|
/// The amount of time in seconds to consider a window for burst messages. The message throttling
|
|
|
|
/// system maintains the invariant that in the past `burst_window_length` seconds, the maximum
|
|
|
|
/// number of messages sent is `max_messages_in_burst`.
|
2017-06-28 23:07:02 -07:00
|
|
|
/// This defaults to 8 seconds when not specified.
|
|
|
|
pub fn burst_window_length(&self) -> u32 {
|
|
|
|
self.burst_window_length.as_ref().cloned().unwrap_or(8)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The maximum number of messages that can be sent in a burst window before they'll be delayed.
|
2018-01-01 21:47:28 -05:00
|
|
|
/// Messages are automatically delayed until the start of the next window. The message throttling
|
|
|
|
/// system maintains the invariant that in the past `burst_window_length` seconds, the maximum
|
|
|
|
/// number of messages sent is `max_messages_in_burst`.
|
2017-06-28 23:07:02 -07:00
|
|
|
/// This defaults to 15 messages when not specified.
|
|
|
|
pub fn max_messages_in_burst(&self) -> u32 {
|
|
|
|
self.max_messages_in_burst.as_ref().cloned().unwrap_or(15)
|
|
|
|
}
|
|
|
|
|
2016-02-10 00:15:08 -05:00
|
|
|
/// Gets whether or not to attempt nickname reclamation using NickServ GHOST.
|
2016-02-09 18:24:52 +08:00
|
|
|
/// This defaults to false when not specified.
|
|
|
|
pub fn should_ghost(&self) -> bool {
|
2016-03-17 21:35:35 -04:00
|
|
|
self.should_ghost.as_ref().cloned().unwrap_or(false)
|
2016-02-09 18:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the NickServ command sequence to recover a nickname.
|
|
|
|
/// This defaults to `["GHOST"]` when not specified.
|
|
|
|
pub fn ghost_sequence(&self) -> Vec<&str> {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.ghost_sequence.as_ref().map_or(vec!["GHOST"], |v| {
|
|
|
|
v.iter().map(|s| &s[..]).collect()
|
|
|
|
})
|
2016-02-09 18:24:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 05:49:20 -05:00
|
|
|
/// Looks up the specified string in the options map.
|
|
|
|
/// This uses indexing, and thus panics when the string is not present.
|
2014-12-04 21:04:22 -05:00
|
|
|
/// This will also panic if used and there are no options.
|
2014-11-29 05:49:20 -05:00
|
|
|
pub fn get_option(&self, option: &str) -> &str {
|
2017-06-19 13:59:26 -04:00
|
|
|
self.options
|
|
|
|
.as_ref()
|
|
|
|
.map(|o| &o[&option.to_owned()][..])
|
|
|
|
.unwrap()
|
2014-11-29 05:49:20 -05:00
|
|
|
}
|
2017-06-21 19:19:58 -04:00
|
|
|
|
|
|
|
/// Gets whether or not to use a mock connection for testing.
|
|
|
|
/// This defaults to false when not specified.
|
|
|
|
pub fn use_mock_connection(&self) -> bool {
|
|
|
|
self.use_mock_connection.as_ref().cloned().unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the initial value for the mock connection.
|
|
|
|
/// This defaults to false when not specified.
|
|
|
|
/// This has no effect if `use_mock_connection` is not `true`.
|
|
|
|
pub fn mock_initial_value(&self) -> &str {
|
|
|
|
self.mock_initial_value.as_ref().map_or("", |s| &s)
|
|
|
|
}
|
2014-11-02 18:16:49 -05:00
|
|
|
}
|
2014-11-05 01:45:17 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::collections::HashMap;
|
2014-12-05 10:27:58 -05:00
|
|
|
use std::default::Default;
|
2017-06-29 00:31:27 -07:00
|
|
|
#[cfg(feature = "json")]
|
2015-03-07 15:01:34 -05:00
|
|
|
use std::path::Path;
|
2014-11-05 01:45:17 -05:00
|
|
|
|
2017-06-22 14:14:49 -04:00
|
|
|
use super::Config;
|
|
|
|
|
2017-06-29 00:31:27 -07:00
|
|
|
fn test_config() -> Config {
|
|
|
|
Config {
|
2014-12-04 21:04:22 -05:00
|
|
|
owners: Some(vec![format!("test")]),
|
|
|
|
nickname: Some(format!("test")),
|
2014-12-09 17:01:47 -05:00
|
|
|
nick_password: None,
|
2014-12-06 18:49:07 -05:00
|
|
|
alt_nicks: None,
|
2014-12-04 21:04:22 -05:00
|
|
|
username: Some(format!("test")),
|
|
|
|
realname: Some(format!("test")),
|
|
|
|
password: Some(String::new()),
|
2015-02-17 17:56:06 +00:00
|
|
|
umodes: Some(format!("+BR")),
|
2014-12-04 21:04:22 -05:00
|
|
|
server: Some(format!("irc.test.net")),
|
|
|
|
port: Some(6667),
|
|
|
|
use_ssl: Some(false),
|
2017-06-21 23:24:11 -04:00
|
|
|
cert_path: None,
|
2014-12-04 21:04:22 -05:00
|
|
|
encoding: Some(format!("UTF-8")),
|
|
|
|
channels: Some(vec![format!("#test"), format!("#test2")]),
|
2016-07-05 16:21:21 -04:00
|
|
|
channel_keys: None,
|
2014-12-23 12:15:41 -05:00
|
|
|
user_info: None,
|
2017-03-22 22:06:43 +01:00
|
|
|
version: None,
|
|
|
|
source: None,
|
2016-01-16 11:47:25 -05:00
|
|
|
ping_time: None,
|
|
|
|
ping_timeout: None,
|
2017-06-28 23:07:02 -07:00
|
|
|
burst_window_length: None,
|
|
|
|
max_messages_in_burst: None,
|
2016-02-09 18:24:52 +08:00
|
|
|
should_ghost: None,
|
|
|
|
ghost_sequence: None,
|
2014-12-04 21:04:22 -05:00
|
|
|
options: Some(HashMap::new()),
|
2017-06-21 19:19:58 -04:00
|
|
|
use_mock_connection: None,
|
|
|
|
mock_initial_value: None,
|
2017-06-29 00:31:27 -07:00
|
|
|
|
2018-01-28 00:52:11 +01:00
|
|
|
..Default::default()
|
|
|
|
}
|
2014-11-30 03:30:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-06-29 00:31:27 -07:00
|
|
|
#[cfg(feature = "json")]
|
2018-01-28 00:52:11 +01:00
|
|
|
fn load_from_json() {
|
|
|
|
assert_eq!(Config::load("client_config.json").unwrap(), test_config().with_path("client_config.json"));
|
2014-11-30 03:30:10 -05:00
|
|
|
}
|
|
|
|
|
2017-06-29 00:31:27 -07:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "toml")]
|
|
|
|
fn load_from_toml() {
|
2018-01-28 00:52:11 +01:00
|
|
|
assert_eq!(Config::load("client_config.toml").unwrap(), test_config().with_path("client_config.toml"));
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "yaml")]
|
|
|
|
fn load_from_yaml() {
|
2018-01-28 00:52:11 +01:00
|
|
|
assert_eq!(Config::load("client_config.yaml").unwrap(), test_config().with_path("client_config.yaml"));
|
2017-06-29 00:31:27 -07:00
|
|
|
}
|
2014-11-30 03:30:10 -05:00
|
|
|
|
|
|
|
#[test]
|
2014-11-05 01:45:17 -05:00
|
|
|
fn is_owner() {
|
|
|
|
let cfg = Config {
|
2014-12-04 21:04:22 -05:00
|
|
|
owners: Some(vec![format!("test"), format!("test2")]),
|
2017-06-19 13:59:26 -04:00
|
|
|
..Default::default()
|
2014-11-05 01:45:17 -05:00
|
|
|
};
|
|
|
|
assert!(cfg.is_owner("test"));
|
|
|
|
assert!(cfg.is_owner("test2"));
|
|
|
|
assert!(!cfg.is_owner("test3"));
|
|
|
|
}
|
2014-11-29 05:49:20 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_option() {
|
2014-12-04 21:04:22 -05:00
|
|
|
let cfg = Config {
|
2014-11-30 03:30:10 -05:00
|
|
|
options: {
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert(format!("testing"), format!("test"));
|
2014-12-04 21:04:22 -05:00
|
|
|
Some(map)
|
2014-11-30 03:30:10 -05:00
|
|
|
},
|
2017-06-19 13:59:26 -04:00
|
|
|
..Default::default()
|
2014-11-30 03:30:10 -05:00
|
|
|
};
|
|
|
|
assert_eq!(cfg.get_option("testing"), "test");
|
|
|
|
}
|
2014-11-05 01:45:17 -05:00
|
|
|
}
|