You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

555 lines
16KB

  1. /*
  2. * meli - configuration module.
  3. *
  4. * Copyright 2017 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. extern crate bincode;
  22. extern crate serde;
  23. extern crate toml;
  24. extern crate xdg;
  25. pub mod composing;
  26. pub mod notifications;
  27. pub mod pager;
  28. pub mod pgp;
  29. pub mod shortcuts;
  30. pub mod terminal;
  31. pub mod accounts;
  32. pub use self::accounts::Account;
  33. pub use self::composing::*;
  34. pub use self::pgp::*;
  35. pub use self::shortcuts::*;
  36. use self::default_vals::*;
  37. use self::notifications::NotificationsSettings;
  38. use self::terminal::TerminalSettings;
  39. use crate::pager::PagerSettings;
  40. use melib::backends::SpecialUseMailbox;
  41. use melib::conf::{toggleflag_de, AccountSettings, FolderConf, ToggleFlag};
  42. use melib::error::*;
  43. use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
  44. use std::collections::HashMap;
  45. use std::env;
  46. use std::fs::{File, OpenOptions};
  47. use std::io::{self, BufRead, Read, Write};
  48. use std::path::PathBuf;
  49. #[macro_export]
  50. macro_rules! split_command {
  51. ($cmd:expr) => {{
  52. $cmd.split_whitespace().collect::<Vec<&str>>()
  53. }};
  54. }
  55. #[derive(Default, Debug, Clone, Serialize, Deserialize)]
  56. pub struct MailUIConf {
  57. pub pager: Option<PagerSettings>,
  58. pub notifications: Option<NotificationsSettings>,
  59. pub shortcuts: Option<Shortcuts>,
  60. pub composing: Option<ComposingSettings>,
  61. pub identity: Option<String>,
  62. pub index_style: Option<IndexStyle>,
  63. }
  64. #[serde(default)]
  65. #[derive(Debug, Default, Clone, Serialize, Deserialize)]
  66. pub struct FileFolderConf {
  67. #[serde(flatten)]
  68. pub folder_conf: FolderConf,
  69. #[serde(flatten)]
  70. pub conf_override: MailUIConf,
  71. }
  72. impl FileFolderConf {
  73. pub fn conf_override(&self) -> &MailUIConf {
  74. &self.conf_override
  75. }
  76. pub fn folder_conf(&self) -> &FolderConf {
  77. &self.folder_conf
  78. }
  79. }
  80. use crate::conf::deserializers::extra_settings;
  81. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  82. pub struct FileAccount {
  83. root_folder: String,
  84. format: String,
  85. identity: String,
  86. #[serde(flatten)]
  87. #[serde(deserialize_with = "extra_settings")]
  88. pub extra: HashMap<String, String>, /* use custom deserializer to convert any given value (eg bool, number, etc) to string */
  89. #[serde(default = "none")]
  90. display_name: Option<String>,
  91. index_style: IndexStyle,
  92. #[serde(default = "false_val")]
  93. read_only: bool,
  94. subscribed_folders: Vec<String>,
  95. #[serde(default)]
  96. folders: HashMap<String, FileFolderConf>,
  97. #[serde(default)]
  98. cache_type: CacheType,
  99. }
  100. impl From<FileAccount> for AccountConf {
  101. fn from(x: FileAccount) -> Self {
  102. let format = x.format.to_lowercase();
  103. let root_folder = x.root_folder.clone();
  104. let identity = x.identity.clone();
  105. let display_name = x.display_name.clone();
  106. let folders = x
  107. .folders
  108. .iter()
  109. .map(|(k, v)| (k.clone(), v.folder_conf.clone()))
  110. .collect();
  111. let mut acc = AccountSettings {
  112. name: String::new(),
  113. root_folder,
  114. format,
  115. identity,
  116. read_only: x.read_only,
  117. display_name,
  118. subscribed_folders: x.subscribed_folders.clone(),
  119. folders,
  120. extra: x.extra.clone(),
  121. };
  122. let root_path = PathBuf::from(acc.root_folder.as_str());
  123. let root_tmp = root_path
  124. .components()
  125. .last()
  126. .unwrap()
  127. .as_os_str()
  128. .to_str()
  129. .unwrap()
  130. .to_string();
  131. if !acc.subscribed_folders.contains(&root_tmp) {
  132. acc.subscribed_folders.push(root_tmp);
  133. }
  134. let mut folder_confs = x.folders.clone();
  135. for s in &x.subscribed_folders {
  136. if !folder_confs.contains_key(s) {
  137. folder_confs.insert(
  138. s.to_string(),
  139. FileFolderConf {
  140. folder_conf: FolderConf {
  141. subscribe: ToggleFlag::True,
  142. ..FolderConf::default()
  143. },
  144. ..FileFolderConf::default()
  145. },
  146. );
  147. } else {
  148. if !folder_confs[s].folder_conf().subscribe.is_unset() {
  149. continue;
  150. }
  151. folder_confs.get_mut(s).unwrap().folder_conf.subscribe = ToggleFlag::True;
  152. }
  153. if folder_confs[s].folder_conf().usage.is_none() {
  154. let name = s
  155. .split(if s.contains('/') { '/' } else { '.' })
  156. .last()
  157. .unwrap_or("");
  158. folder_confs.get_mut(s).unwrap().folder_conf.usage =
  159. if name.eq_ignore_ascii_case("inbox") {
  160. Some(SpecialUseMailbox::Inbox)
  161. } else if name.eq_ignore_ascii_case("archive") {
  162. Some(SpecialUseMailbox::Archive)
  163. } else if name.eq_ignore_ascii_case("drafts") {
  164. Some(SpecialUseMailbox::Drafts)
  165. } else if name.eq_ignore_ascii_case("junk") {
  166. Some(SpecialUseMailbox::Junk)
  167. } else if name.eq_ignore_ascii_case("spam") {
  168. Some(SpecialUseMailbox::Junk)
  169. } else if name.eq_ignore_ascii_case("sent") {
  170. Some(SpecialUseMailbox::Sent)
  171. } else if name.eq_ignore_ascii_case("trash") {
  172. Some(SpecialUseMailbox::Trash)
  173. } else {
  174. Some(SpecialUseMailbox::Normal)
  175. };
  176. }
  177. if folder_confs[s].folder_conf().ignore.is_unset() {
  178. use SpecialUseMailbox::*;
  179. if [Junk, Sent, Trash]
  180. .contains(&folder_confs[s].folder_conf().usage.as_ref().unwrap())
  181. {
  182. folder_confs.get_mut(s).unwrap().folder_conf.ignore =
  183. ToggleFlag::InternalVal(true);
  184. }
  185. }
  186. }
  187. AccountConf {
  188. account: acc,
  189. conf: x,
  190. folder_confs,
  191. }
  192. }
  193. }
  194. impl FileAccount {
  195. pub fn folders(&self) -> &HashMap<String, FileFolderConf> {
  196. &self.folders
  197. }
  198. pub fn folder(&self) -> &str {
  199. &self.root_folder
  200. }
  201. pub fn index_style(&self) -> IndexStyle {
  202. self.index_style
  203. }
  204. pub fn cache_type(&self) -> &CacheType {
  205. &self.cache_type
  206. }
  207. }
  208. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  209. pub struct FileSettings {
  210. accounts: HashMap<String, FileAccount>,
  211. #[serde(default)]
  212. pager: PagerSettings,
  213. #[serde(default)]
  214. notifications: NotificationsSettings,
  215. #[serde(default)]
  216. shortcuts: Shortcuts,
  217. composing: ComposingSettings,
  218. #[serde(default)]
  219. pgp: PGPSettings,
  220. #[serde(default)]
  221. terminal: TerminalSettings,
  222. }
  223. #[derive(Debug, Clone, Default)]
  224. pub struct AccountConf {
  225. pub(crate) account: AccountSettings,
  226. pub(crate) conf: FileAccount,
  227. pub(crate) folder_confs: HashMap<String, FileFolderConf>,
  228. }
  229. impl AccountConf {
  230. pub fn account(&self) -> &AccountSettings {
  231. &self.account
  232. }
  233. pub fn conf(&self) -> &FileAccount {
  234. &self.conf
  235. }
  236. pub fn conf_mut(&mut self) -> &mut FileAccount {
  237. &mut self.conf
  238. }
  239. }
  240. #[derive(Debug, Clone, Default)]
  241. pub struct Settings {
  242. pub accounts: HashMap<String, AccountConf>,
  243. pub pager: PagerSettings,
  244. pub notifications: NotificationsSettings,
  245. pub shortcuts: Shortcuts,
  246. pub composing: ComposingSettings,
  247. pub pgp: PGPSettings,
  248. pub terminal: TerminalSettings,
  249. }
  250. impl FileSettings {
  251. pub fn new() -> Result<FileSettings> {
  252. let config_path = match env::var("MELI_CONFIG") {
  253. Ok(path) => PathBuf::from(path),
  254. Err(_) => {
  255. let xdg_dirs = xdg::BaseDirectories::with_prefix("meli").unwrap();
  256. xdg_dirs
  257. .place_config_file("config")
  258. .expect("cannot create configuration directory")
  259. }
  260. };
  261. if !config_path.exists() {
  262. println!(
  263. "No configuration found. Would you like to generate one in {}? [Y/n]",
  264. config_path.display()
  265. );
  266. let mut buffer = String::new();
  267. let stdin = io::stdin();
  268. let mut handle = stdin.lock();
  269. loop {
  270. buffer.clear();
  271. handle
  272. .read_line(&mut buffer)
  273. .expect("Could not read from stdin.");
  274. match buffer.trim() {
  275. "Y" | "y" | "yes" | "YES" | "Yes" => {
  276. let mut file = OpenOptions::new()
  277. .write(true)
  278. .create_new(true)
  279. .open(config_path.as_path())
  280. .expect("Could not create config file.");
  281. file.write_all(include_bytes!("../../sample-config"))
  282. .expect("Could not write to config file.");
  283. println!("Written config to {}", config_path.display());
  284. return Err(MeliError::new(
  285. "Edit the sample configuration and relaunch meli.",
  286. ));
  287. }
  288. "n" | "N" | "no" | "No" | "NO" => {
  289. return Err(MeliError::new("No configuration file found."));
  290. }
  291. _ => {
  292. println!(
  293. "No configuration found. Would you like to generate one in {}? [Y/n]",
  294. config_path.display()
  295. );
  296. }
  297. }
  298. }
  299. }
  300. let mut file = File::open(config_path.to_str().unwrap())?;
  301. let mut contents = String::new();
  302. file.read_to_string(&mut contents)?;
  303. let s = toml::from_str(&contents);
  304. if let Err(e) = s {
  305. return Err(MeliError::new(format!(
  306. "Config file contains errors: {}",
  307. e.to_string()
  308. )));
  309. }
  310. Ok(s.unwrap())
  311. }
  312. pub fn validate(path: &str) -> Result<()> {
  313. let mut file = File::open(path)?;
  314. let mut contents = String::new();
  315. file.read_to_string(&mut contents)?;
  316. let s: std::result::Result<FileSettings, toml::de::Error> = toml::from_str(&contents);
  317. if let Err(e) = s {
  318. return Err(MeliError::new(format!(
  319. "Config file contains errors: {}",
  320. e.to_string()
  321. )));
  322. }
  323. Ok(())
  324. }
  325. }
  326. impl Settings {
  327. pub fn new() -> Result<Settings> {
  328. let fs = FileSettings::new()?;
  329. let mut s: HashMap<String, AccountConf> = HashMap::new();
  330. for (id, x) in fs.accounts {
  331. let mut ac = AccountConf::from(x);
  332. ac.account.set_name(id.clone());
  333. s.insert(id, ac);
  334. }
  335. Ok(Settings {
  336. accounts: s,
  337. pager: fs.pager,
  338. notifications: fs.notifications,
  339. shortcuts: fs.shortcuts,
  340. composing: fs.composing,
  341. pgp: fs.pgp,
  342. terminal: fs.terminal,
  343. })
  344. }
  345. }
  346. #[derive(Copy, Debug, Clone, Hash, PartialEq)]
  347. pub enum IndexStyle {
  348. Plain,
  349. Threaded,
  350. Compact,
  351. Conversations,
  352. }
  353. impl Default for IndexStyle {
  354. fn default() -> Self {
  355. IndexStyle::Compact
  356. }
  357. }
  358. /*
  359. * Deserialize default functions
  360. */
  361. mod default_vals {
  362. pub(in crate::conf) fn false_val() -> bool {
  363. false
  364. }
  365. pub(in crate::conf) fn true_val() -> bool {
  366. true
  367. }
  368. pub(in crate::conf) fn zero_val() -> usize {
  369. 0
  370. }
  371. pub(in crate::conf) fn eighty_percent() -> usize {
  372. 80
  373. }
  374. pub(in crate::conf) fn none<T>() -> Option<T> {
  375. None
  376. }
  377. pub(in crate::conf) fn internal_value_false() -> super::ToggleFlag {
  378. super::ToggleFlag::InternalVal(false)
  379. }
  380. }
  381. mod deserializers {
  382. use serde::{Deserialize, Deserializer};
  383. pub(in crate::conf) fn non_empty_string<'de, D>(
  384. deserializer: D,
  385. ) -> std::result::Result<Option<String>, D::Error>
  386. where
  387. D: Deserializer<'de>,
  388. {
  389. let s = <String>::deserialize(deserializer)?;
  390. if s.is_empty() {
  391. Ok(None)
  392. } else {
  393. Ok(Some(s))
  394. }
  395. }
  396. use toml::Value;
  397. fn any_of<'de, D>(deserializer: D) -> std::result::Result<String, D::Error>
  398. where
  399. D: Deserializer<'de>,
  400. {
  401. let v: Value = Deserialize::deserialize(deserializer)?;
  402. let mut ret = v.to_string();
  403. if ret.starts_with('"') && ret.ends_with('"') {
  404. ret.drain(0..1).count();
  405. ret.drain(ret.len() - 1..).count();
  406. }
  407. Ok(ret)
  408. }
  409. use std::collections::HashMap;
  410. pub(in crate::conf) fn extra_settings<'de, D>(
  411. deserializer: D,
  412. ) -> std::result::Result<HashMap<String, String>, D::Error>
  413. where
  414. D: Deserializer<'de>,
  415. {
  416. /* Why is this needed? If the user gives a configuration value such as key = true, the
  417. * parsing will fail since it expects string values. We want to accept key = true as well
  418. * as key = "true". */
  419. #[derive(Deserialize)]
  420. struct Wrapper(#[serde(deserialize_with = "any_of")] String);
  421. let v = <HashMap<String, Wrapper>>::deserialize(deserializer)?;
  422. Ok(v.into_iter().map(|(k, Wrapper(v))| (k, v)).collect())
  423. }
  424. }
  425. impl<'de> Deserialize<'de> for IndexStyle {
  426. fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
  427. where
  428. D: Deserializer<'de>,
  429. {
  430. let s = <String>::deserialize(deserializer)?;
  431. match s.as_str() {
  432. "Plain" | "plain" => Ok(IndexStyle::Plain),
  433. "Threaded" | "threaded" => Ok(IndexStyle::Threaded),
  434. "Compact" | "compact" => Ok(IndexStyle::Compact),
  435. "Conversations" | "conversations" => Ok(IndexStyle::Conversations),
  436. _ => Err(de::Error::custom("invalid `index_style` value")),
  437. }
  438. }
  439. }
  440. impl Serialize for IndexStyle {
  441. fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
  442. where
  443. S: Serializer,
  444. {
  445. match self {
  446. IndexStyle::Plain => serializer.serialize_str("plain"),
  447. IndexStyle::Threaded => serializer.serialize_str("threaded"),
  448. IndexStyle::Compact => serializer.serialize_str("compact"),
  449. IndexStyle::Conversations => serializer.serialize_str("conversations"),
  450. }
  451. }
  452. }
  453. #[derive(Debug, Clone, PartialEq)]
  454. pub enum CacheType {
  455. None,
  456. #[cfg(feature = "sqlite3")]
  457. Sqlite3,
  458. }
  459. impl Default for CacheType {
  460. fn default() -> Self {
  461. #[cfg(feature = "sqlite3")]
  462. {
  463. CacheType::Sqlite3
  464. }
  465. #[cfg(not(feature = "sqlite3"))]
  466. {
  467. CacheType::None
  468. }
  469. }
  470. }
  471. impl<'de> Deserialize<'de> for CacheType {
  472. fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
  473. where
  474. D: Deserializer<'de>,
  475. {
  476. let s = <String>::deserialize(deserializer)?;
  477. match s.as_str() {
  478. #[cfg(feature = "sqlite3")]
  479. "sqlite3" => Ok(CacheType::Sqlite3),
  480. "nothing" | "none" | "" => Ok(CacheType::None),
  481. _ => Err(de::Error::custom("invalid `index_cache` value")),
  482. }
  483. }
  484. }
  485. impl Serialize for CacheType {
  486. fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
  487. where
  488. S: Serializer,
  489. {
  490. match self {
  491. #[cfg(feature = "sqlite3")]
  492. CacheType::Sqlite3 => serializer.serialize_str("sqlite3"),
  493. CacheType::None => serializer.serialize_str("none"),
  494. }
  495. }
  496. }