/*
* meli - configuration module.
*
* Copyright 2017 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see .
*/
/*! Configuration logic and `config.toml` interfaces. */
extern crate bincode;
extern crate serde;
extern crate toml;
extern crate xdg;
pub mod composing;
pub mod notifications;
pub mod pager;
pub mod pgp;
pub mod tags;
#[macro_use]
pub mod shortcuts;
mod listing;
pub mod terminal;
mod themes;
pub use themes::*;
pub mod accounts;
pub use self::accounts::Account;
pub use self::composing::*;
pub use self::pgp::*;
pub use self::shortcuts::*;
pub use self::tags::*;
use self::default_vals::*;
use self::listing::{ListingSettings, ListingSettingsOverride};
use self::notifications::{NotificationsSettings, NotificationsSettingsOverride};
use self::terminal::TerminalSettings;
use crate::pager::{PagerSettings, PagerSettingsOverride};
use crate::plugins::Plugin;
use melib::conf::{AccountSettings, MailboxConf, ToggleFlag};
use melib::error::*;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::env;
use std::fs::OpenOptions;
use std::io::{self, BufRead, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
#[macro_export]
macro_rules! split_command {
($cmd:expr) => {{
$cmd.split_whitespace().collect::>()
}};
}
#[macro_export]
macro_rules! mailbox_acc_settings {
($context:ident[$account_idx:expr].$setting:ident.$field:ident) => {{
$context.accounts[$account_idx]
.settings
.conf_override
.$setting
.$field
.as_ref()
.unwrap_or(&$context.settings.$setting.$field)
}};
}
#[macro_export]
macro_rules! mailbox_settings {
($context:ident[$account_idx:expr][$mailbox_path:expr].$setting:ident.$field:ident) => {{
$context.accounts[$account_idx][$mailbox_path]
.conf
.conf_override
.$setting
.$field
.as_ref()
.or($context.accounts[$account_idx]
.settings
.conf_override
.$setting
.$field
.as_ref())
.unwrap_or(&$context.settings.$setting.$field)
}};
}
#[macro_export]
macro_rules! override_def {
($override_name:ident,
$(#[$outer:meta])*
pub struct $name:ident { $( $(#[$fouter:meta])* $fname:ident : $ft:ty),*,
}) => {
$(#[$outer])*
pub struct $name {
$(
$(#[$fouter])*
pub $fname : $ft
),*
}
$(#[$outer])*
pub struct $override_name {
$(
$(#[$fouter])*
pub $fname : Option<$ft>
),*
}
impl Default for $override_name {
fn default() -> Self {
$override_name {
$(
$fname : None
),*
}
}
}
}
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct MailUIConf {
#[serde(default)]
pub pager: PagerSettingsOverride,
#[serde(default)]
pub listing: ListingSettingsOverride,
#[serde(default)]
pub notifications: NotificationsSettingsOverride,
#[serde(default)]
pub shortcuts: ShortcutsOverride,
#[serde(default)]
pub composing: ComposingSettingsOverride,
#[serde(default)]
pub identity: Option,
#[serde(default)]
pub tags: TagsSettingsOverride,
#[serde(default)]
pub themes: Option,
#[serde(default)]
pub pgp: PGPSettingsOverride,
}
#[serde(default)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct FileMailboxConf {
#[serde(flatten)]
pub conf_override: MailUIConf,
#[serde(flatten)]
pub mailbox_conf: MailboxConf,
}
impl FileMailboxConf {
pub fn conf_override(&self) -> &MailUIConf {
&self.conf_override
}
pub fn mailbox_conf(&self) -> &MailboxConf {
&self.mailbox_conf
}
}
use crate::conf::deserializers::extra_settings;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FileAccount {
root_mailbox: String,
format: String,
identity: String,
#[serde(default = "none")]
display_name: Option,
#[serde(default = "false_val")]
read_only: bool,
#[serde(default)]
subscribed_mailboxes: Vec,
#[serde(default)]
mailboxes: HashMap,
#[serde(default)]
cache_type: CacheType,
#[serde(default = "false_val")]
pub manual_refresh: bool,
#[serde(default = "none")]
pub refresh_command: Option,
#[serde(flatten)]
pub conf_override: MailUIConf,
#[serde(flatten)]
#[serde(deserialize_with = "extra_settings")]
pub extra: HashMap, /* use custom deserializer to convert any given value (eg bool, number, etc) to string */
}
impl From for AccountConf {
fn from(x: FileAccount) -> Self {
let format = x.format.to_lowercase();
let root_mailbox = x.root_mailbox.clone();
let identity = x.identity.clone();
let display_name = x.display_name.clone();
let mailboxes = x
.mailboxes
.iter()
.map(|(k, v)| (k.clone(), v.mailbox_conf.clone()))
.collect();
let acc = AccountSettings {
name: String::new(),
root_mailbox,
format,
identity,
read_only: x.read_only,
display_name,
subscribed_mailboxes: x.subscribed_mailboxes.clone(),
mailboxes,
manual_refresh: x.manual_refresh,
extra: x.extra.clone(),
};
let mailbox_confs = x.mailboxes.clone();
AccountConf {
account: acc,
conf_override: x.conf_override.clone(),
conf: x,
mailbox_confs,
}
}
}
impl FileAccount {
pub fn mailboxes(&self) -> &HashMap {
&self.mailboxes
}
pub fn mailbox(&self) -> &str {
&self.root_mailbox
}
pub fn cache_type(&self) -> &CacheType {
&self.cache_type
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FileSettings {
pub accounts: HashMap,
#[serde(default)]
pub pager: PagerSettings,
#[serde(default)]
pub listing: ListingSettings,
#[serde(default)]
pub notifications: NotificationsSettings,
#[serde(default)]
pub shortcuts: Shortcuts,
pub composing: ComposingSettings,
#[serde(default)]
pub tags: TagsSettings,
#[serde(default)]
pub pgp: PGPSettings,
#[serde(default)]
pub terminal: TerminalSettings,
#[serde(default)]
pub plugins: HashMap,
#[serde(default)]
pub log: LogSettings,
}
#[derive(Debug, Clone, Default)]
pub struct AccountConf {
pub(crate) account: AccountSettings,
pub(crate) conf: FileAccount,
pub conf_override: MailUIConf,
pub(crate) mailbox_confs: HashMap,
}
impl AccountConf {
pub fn account(&self) -> &AccountSettings {
&self.account
}
pub fn account_mut(&mut self) -> &mut AccountSettings {
&mut self.account
}
pub fn conf(&self) -> &FileAccount {
&self.conf
}
pub fn conf_mut(&mut self) -> &mut FileAccount {
&mut self.conf
}
}
#[derive(Debug, Clone, Default)]
pub struct Settings {
pub accounts: HashMap,
pub pager: PagerSettings,
pub listing: ListingSettings,
pub notifications: NotificationsSettings,
pub shortcuts: Shortcuts,
pub tags: TagsSettings,
pub composing: ComposingSettings,
pub pgp: PGPSettings,
pub terminal: TerminalSettings,
pub plugins: HashMap,
pub log: LogSettings,
}
impl FileSettings {
pub fn new() -> Result {
let xdg_dirs = xdg::BaseDirectories::with_prefix("meli");
let config_path = match env::var("MELI_CONFIG") {
Ok(path) => PathBuf::from(path),
Err(_) => xdg_dirs
.as_ref()
.unwrap()
.place_config_file("config.toml")
.expect("cannot create configuration directory"),
};
if !config_path.exists() {
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
loop {
buffer.clear();
handle
.read_line(&mut buffer)
.expect("Could not read from stdin.");
match buffer.trim() {
"Y" | "y" | "yes" | "YES" | "Yes" => {
create_config_file(&config_path)?;
return Err(MeliError::new(
"Edit the sample configuration and relaunch meli.",
));
}
"n" | "N" | "no" | "No" | "NO" => {
return Err(MeliError::new("No configuration file found."));
}
_ => {
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
}
}
}
}
let path = config_path
.to_str()
.expect("Configuration file path was not valid UTF-8");
FileSettings::validate(path)
}
pub fn validate(path: &str) -> Result {
let s = pp::pp(path)?;
let mut s: FileSettings = toml::from_str(&s).map_err(|e| {
MeliError::new(format!(
"{}:\nConfig file contains errors: {}",
path,
e.to_string()
))
})?;
let mut backends = melib::backends::Backends::new();
let plugin_manager = crate::plugins::PluginManager::new();
for (_, p) in s.plugins.clone() {
if crate::plugins::PluginKind::Backend == p.kind() {
crate::plugins::backend::PluginBackend::register(
plugin_manager.listener(),
p.clone(),
&mut backends,
);
}
}
let Themes {
light: default_light,
dark: default_dark,
..
} = Themes::default();
for (k, v) in default_light.into_iter() {
if !s.terminal.themes.light.contains_key(&k) {
s.terminal.themes.light.insert(k, v);
}
}
for theme in s.terminal.themes.other_themes.values_mut() {
for (k, v) in default_dark.clone().into_iter() {
if !theme.contains_key(&k) {
theme.insert(k, v);
}
}
}
for (k, v) in default_dark.into_iter() {
if !s.terminal.themes.dark.contains_key(&k) {
s.terminal.themes.dark.insert(k, v);
}
}
match s.terminal.theme.as_str() {
"dark" | "light" => {}
t if s.terminal.themes.other_themes.contains_key(t) => {}
t => {
return Err(MeliError::new(format!("Theme `{}` was not found.", t)));
}
}
s.terminal.themes.validate()?;
for (name, acc) in &s.accounts {
let FileAccount {
root_mailbox,
format,
identity,
read_only,
display_name,
subscribed_mailboxes,
mailboxes,
extra,
manual_refresh,
refresh_command: _,
cache_type: _,
conf_override: _,
} = acc.clone();
let lowercase_format = format.to_lowercase();
let s = AccountSettings {
name: name.to_string(),
root_mailbox,
format: format.clone(),
identity,
read_only,
display_name,
subscribed_mailboxes,
manual_refresh,
mailboxes: mailboxes
.into_iter()
.map(|(k, v)| (k, v.mailbox_conf))
.collect(),
extra,
};
backends.validate_config(&lowercase_format, &s)?;
}
Ok(s)
}
}
impl Settings {
pub fn new() -> Result {
let fs = FileSettings::new()?;
let mut s: HashMap = HashMap::new();
for (id, x) in fs.accounts {
let mut ac = AccountConf::from(x);
ac.account.set_name(id.clone());
s.insert(id, ac);
}
if let Some(ref log_path) = fs.log.log_file {
melib::change_log_dest(log_path.into());
}
if fs.log.maximum_level != melib::LoggingLevel::default() {
melib::change_log_level(fs.log.maximum_level);
}
Ok(Settings {
accounts: s,
pager: fs.pager,
listing: fs.listing,
notifications: fs.notifications,
shortcuts: fs.shortcuts,
tags: fs.tags,
composing: fs.composing,
pgp: fs.pgp,
terminal: fs.terminal,
plugins: fs.plugins,
log: fs.log,
})
}
}
#[derive(Copy, Debug, Clone, Hash, PartialEq)]
pub enum IndexStyle {
Plain,
Threaded,
Compact,
Conversations,
}
impl Default for IndexStyle {
fn default() -> Self {
IndexStyle::Compact
}
}
/*
* Deserialize default functions
*/
mod default_vals {
pub(in crate::conf) fn false_val>() -> T {
false.into()
}
pub(in crate::conf) fn true_val>() -> T {
true.into()
}
pub(in crate::conf) fn zero_val>() -> T {
0.into()
}
pub(in crate::conf) fn eighty_val>() -> T {
80.into()
}
pub(in crate::conf) fn none() -> Option {
None
}
pub(in crate::conf) fn internal_value_false>() -> T {
super::ToggleFlag::InternalVal(false).into()
}
pub(in crate::conf) fn internal_value_true>() -> T {
super::ToggleFlag::InternalVal(true).into()
}
}
mod deserializers {
use serde::{Deserialize, Deserializer};
pub(in crate::conf) fn non_empty_string<'de, D, T: std::convert::From