2017-09-01 12:24:32 +00:00
|
|
|
/*
|
|
|
|
* meli - configuration module.
|
|
|
|
*
|
|
|
|
* Copyright 2017 Manos Pitsidianakis
|
2017-09-07 20:00:08 +00:00
|
|
|
*
|
2017-09-01 12:24:32 +00:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-09-09 11:24:30 +00:00
|
|
|
|
|
|
|
//! Basic mail account configuration to use with [`backends`](./backends/index.html)
|
2019-12-10 22:15:36 +00:00
|
|
|
use crate::backends::SpecialUsageMailbox;
|
2023-03-05 18:53:00 +00:00
|
|
|
use crate::error::{Error, Result};
|
2022-07-19 12:27:38 +00:00
|
|
|
pub use crate::{SortField, SortOrder};
|
2019-11-15 17:51:42 +00:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2019-12-01 10:05:14 +00:00
|
|
|
use std::collections::HashMap;
|
2017-09-01 12:24:32 +00:00
|
|
|
|
2019-05-14 18:47:47 +00:00
|
|
|
#[derive(Debug, Serialize, Default, Clone)]
|
2017-09-14 15:08:14 +00:00
|
|
|
pub struct AccountSettings {
|
2018-08-19 10:12:48 +00:00
|
|
|
pub name: String,
|
2020-02-26 08:54:10 +00:00
|
|
|
pub root_mailbox: String,
|
2018-08-19 10:12:48 +00:00
|
|
|
pub format: String,
|
2018-08-30 12:54:30 +00:00
|
|
|
pub identity: String,
|
2022-03-21 18:53:37 +00:00
|
|
|
pub extra_identities: Vec<String>,
|
2019-07-18 17:16:51 +00:00
|
|
|
pub read_only: bool,
|
2018-08-30 12:54:30 +00:00
|
|
|
pub display_name: Option<String>,
|
2022-07-19 12:27:38 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub order: (SortField, SortOrder),
|
2020-02-26 08:54:10 +00:00
|
|
|
pub subscribed_mailboxes: Vec<String>,
|
2019-11-15 17:51:42 +00:00
|
|
|
#[serde(default)]
|
2020-02-26 08:54:10 +00:00
|
|
|
pub mailboxes: HashMap<String, MailboxConf>,
|
2019-12-14 16:55:46 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub manual_refresh: bool,
|
2019-06-28 16:34:40 +00:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub extra: HashMap<String, String>,
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2017-09-30 17:02:22 +00:00
|
|
|
|
|
|
|
impl AccountSettings {
|
2018-07-20 09:44:04 +00:00
|
|
|
pub fn format(&self) -> &str {
|
2017-09-30 17:02:22 +00:00
|
|
|
&self.format
|
|
|
|
}
|
2018-07-20 09:44:04 +00:00
|
|
|
pub fn name(&self) -> &str {
|
2018-07-11 20:12:25 +00:00
|
|
|
&self.name
|
|
|
|
}
|
2018-08-30 12:54:30 +00:00
|
|
|
pub fn set_name(&mut self, s: String) {
|
|
|
|
self.name = s;
|
|
|
|
}
|
2020-02-26 08:54:10 +00:00
|
|
|
pub fn root_mailbox(&self) -> &str {
|
|
|
|
&self.root_mailbox
|
2018-08-11 15:00:21 +00:00
|
|
|
}
|
2018-08-30 12:54:30 +00:00
|
|
|
pub fn identity(&self) -> &str {
|
|
|
|
&self.identity
|
|
|
|
}
|
2019-07-18 17:16:51 +00:00
|
|
|
pub fn read_only(&self) -> bool {
|
|
|
|
self.read_only
|
|
|
|
}
|
2018-08-30 12:54:30 +00:00
|
|
|
pub fn display_name(&self) -> Option<&String> {
|
|
|
|
self.display_name.as_ref()
|
|
|
|
}
|
2022-07-19 12:27:38 +00:00
|
|
|
pub fn order(&self) -> Option<(SortField, SortOrder)> {
|
|
|
|
Some(self.order)
|
|
|
|
}
|
2019-08-23 18:32:32 +00:00
|
|
|
|
2020-02-26 08:54:10 +00:00
|
|
|
pub fn subscribed_mailboxes(&self) -> &Vec<String> {
|
|
|
|
&self.subscribed_mailboxes
|
2019-08-23 18:32:32 +00:00
|
|
|
}
|
2019-11-26 23:39:06 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "vcard")]
|
|
|
|
pub fn vcard_folder(&self) -> Option<&str> {
|
|
|
|
self.extra.get("vcard_folder").map(String::as_str)
|
|
|
|
}
|
2023-03-05 18:53:00 +00:00
|
|
|
|
|
|
|
/// Get the server password, either directly from the `server_password`
|
|
|
|
/// settings value, or by running the `server_password_command` and reading
|
|
|
|
/// the output.
|
|
|
|
pub fn server_password(&self) -> Result<String> {
|
|
|
|
if let Some(cmd) = self.extra.get("server_password_command") {
|
|
|
|
let output = std::process::Command::new("sh")
|
|
|
|
.args(&["-c", cmd])
|
|
|
|
.stdin(std::process::Stdio::piped())
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
if output.status.success() {
|
|
|
|
Ok(std::str::from_utf8(&output.stdout)?.trim_end().to_string())
|
|
|
|
} else {
|
|
|
|
Err(Error::new(format!(
|
|
|
|
"({}) server_password_command `{}` returned {}: {}",
|
|
|
|
self.name,
|
|
|
|
cmd,
|
|
|
|
output.status,
|
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
} else if let Some(pass) = self.extra.get("server_password") {
|
|
|
|
Ok(pass.to_owned())
|
|
|
|
} else {
|
|
|
|
Err(Error::new(format!("Configuration error: connection requires either server_password or server_password_command")))
|
|
|
|
}
|
|
|
|
}
|
2017-09-30 17:02:22 +00:00
|
|
|
}
|
2019-11-15 17:51:42 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-09-04 13:52:17 +00:00
|
|
|
#[serde(default)]
|
2020-02-26 08:54:10 +00:00
|
|
|
pub struct MailboxConf {
|
2020-07-29 11:33:09 +00:00
|
|
|
#[serde(alias = "rename")]
|
2019-12-18 13:40:57 +00:00
|
|
|
pub alias: Option<String>,
|
2020-06-22 08:31:43 +00:00
|
|
|
#[serde(default = "false_val")]
|
2019-11-15 17:51:42 +00:00
|
|
|
pub autoload: bool,
|
2020-01-01 22:07:19 +00:00
|
|
|
#[serde(default)]
|
2019-11-15 17:51:42 +00:00
|
|
|
pub subscribe: ToggleFlag,
|
2020-01-01 22:07:19 +00:00
|
|
|
#[serde(default)]
|
2019-11-15 17:51:42 +00:00
|
|
|
pub ignore: ToggleFlag,
|
|
|
|
#[serde(default = "none")]
|
2019-12-10 22:15:36 +00:00
|
|
|
pub usage: Option<SpecialUsageMailbox>,
|
2021-10-31 16:15:11 +00:00
|
|
|
#[serde(default = "none")]
|
|
|
|
pub sort_order: Option<usize>,
|
2019-11-15 17:51:42 +00:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub extra: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:54:10 +00:00
|
|
|
impl Default for MailboxConf {
|
2019-11-15 17:51:42 +00:00
|
|
|
fn default() -> Self {
|
2020-02-26 08:54:10 +00:00
|
|
|
MailboxConf {
|
2019-12-18 13:40:57 +00:00
|
|
|
alias: None,
|
2020-06-22 08:31:43 +00:00
|
|
|
autoload: false,
|
2019-11-15 17:51:42 +00:00
|
|
|
subscribe: ToggleFlag::Unset,
|
|
|
|
ignore: ToggleFlag::Unset,
|
|
|
|
usage: None,
|
2021-10-31 16:15:11 +00:00
|
|
|
sort_order: None,
|
2019-11-15 17:51:42 +00:00
|
|
|
extra: HashMap::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:54:10 +00:00
|
|
|
impl MailboxConf {
|
2019-12-18 13:40:57 +00:00
|
|
|
pub fn alias(&self) -> Option<&str> {
|
2020-08-25 13:39:12 +00:00
|
|
|
self.alias.as_deref()
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:03:03 +00:00
|
|
|
pub fn true_val() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn false_val() -> bool {
|
2020-06-22 08:31:43 +00:00
|
|
|
false
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 12:03:03 +00:00
|
|
|
pub fn none<T>() -> Option<T> {
|
2019-11-15 17:51:42 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-09-04 17:03:33 +00:00
|
|
|
macro_rules! named_unit_variant {
|
|
|
|
($variant:ident) => {
|
|
|
|
pub mod $variant {
|
|
|
|
/*
|
|
|
|
pub fn serialize<S>(serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(stringify!($variant))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<(), D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
struct V;
|
|
|
|
impl<'de> serde::de::Visitor<'de> for V {
|
|
|
|
type Value = ();
|
|
|
|
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
f.write_str(concat!("\"", stringify!($variant), "\""))
|
|
|
|
}
|
|
|
|
fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
|
|
|
|
if value == stringify!($variant) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(E::invalid_value(serde::de::Unexpected::Str(value), &self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deserializer.deserialize_str(V)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mod strings {
|
|
|
|
named_unit_variant!(ask);
|
|
|
|
}
|
|
|
|
|
2022-11-14 17:14:19 +00:00
|
|
|
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
|
2019-11-15 17:51:42 +00:00
|
|
|
pub enum ToggleFlag {
|
|
|
|
Unset,
|
|
|
|
InternalVal(bool),
|
|
|
|
False,
|
|
|
|
True,
|
2020-10-10 21:32:55 +00:00
|
|
|
Ask,
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bool> for ToggleFlag {
|
|
|
|
fn from(val: bool) -> Self {
|
|
|
|
if val {
|
|
|
|
ToggleFlag::True
|
|
|
|
} else {
|
|
|
|
ToggleFlag::False
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ToggleFlag {
|
|
|
|
fn default() -> Self {
|
|
|
|
ToggleFlag::Unset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToggleFlag {
|
|
|
|
pub fn is_unset(&self) -> bool {
|
|
|
|
ToggleFlag::Unset == *self
|
|
|
|
}
|
|
|
|
pub fn is_internal(&self) -> bool {
|
2022-09-17 21:09:23 +00:00
|
|
|
matches!(self, ToggleFlag::InternalVal(_))
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
2020-10-10 21:32:55 +00:00
|
|
|
|
|
|
|
pub fn is_ask(&self) -> bool {
|
2022-09-17 21:09:23 +00:00
|
|
|
matches!(self, ToggleFlag::Ask)
|
2020-10-10 21:32:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 17:51:42 +00:00
|
|
|
pub fn is_false(&self) -> bool {
|
2022-09-17 21:09:23 +00:00
|
|
|
matches!(self, ToggleFlag::False | ToggleFlag::InternalVal(false))
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
2020-10-10 21:32:55 +00:00
|
|
|
|
2019-11-15 17:51:42 +00:00
|
|
|
pub fn is_true(&self) -> bool {
|
2022-09-17 21:09:23 +00:00
|
|
|
matches!(self, ToggleFlag::True | ToggleFlag::InternalVal(true))
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for ToggleFlag {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
ToggleFlag::Unset | ToggleFlag::InternalVal(_) => serializer.serialize_none(),
|
|
|
|
ToggleFlag::False => serializer.serialize_bool(false),
|
|
|
|
ToggleFlag::True => serializer.serialize_bool(true),
|
2020-10-10 21:32:55 +00:00
|
|
|
ToggleFlag::Ask => serializer.serialize_str("ask"),
|
2019-11-15 17:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 22:07:19 +00:00
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for ToggleFlag {
|
|
|
|
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
2021-09-04 17:03:33 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum InnerToggleFlag {
|
|
|
|
Bool(bool),
|
|
|
|
#[serde(with = "strings::ask")]
|
|
|
|
Ask,
|
|
|
|
}
|
|
|
|
let s = <InnerToggleFlag>::deserialize(deserializer);
|
|
|
|
Ok(
|
|
|
|
match s.map_err(|err| {
|
|
|
|
serde::de::Error::custom(format!(
|
2020-10-10 21:32:55 +00:00
|
|
|
r#"expected one of "true", "false", "ask", found `{}`"#,
|
2021-09-04 17:03:33 +00:00
|
|
|
err
|
|
|
|
))
|
|
|
|
})? {
|
|
|
|
InnerToggleFlag::Bool(true) => ToggleFlag::True,
|
|
|
|
InnerToggleFlag::Bool(false) => ToggleFlag::False,
|
|
|
|
InnerToggleFlag::Ask => ToggleFlag::Ask,
|
|
|
|
},
|
|
|
|
)
|
2020-01-01 22:07:19 +00:00
|
|
|
}
|
|
|
|
}
|