mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-05 09:05:13 +00:00
merge back account with config
This commit is contained in:
parent
94f7dd5b05
commit
8fbbb324da
|
@ -1,10 +1,17 @@
|
|||
use clap::Arg;
|
||||
|
||||
/// Config argument.
|
||||
pub fn path_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("config")
|
||||
.long("config")
|
||||
.short("c")
|
||||
.help("Forces a specific config path")
|
||||
.value_name("PATH")
|
||||
/// Config arguments.
|
||||
pub fn args<'a>() -> Vec<Arg<'a, 'a>> {
|
||||
vec![
|
||||
Arg::with_name("config")
|
||||
.long("config")
|
||||
.short("c")
|
||||
.help("Forces a specific config path")
|
||||
.value_name("PATH"),
|
||||
Arg::with_name("account")
|
||||
.long("account")
|
||||
.short("a")
|
||||
.help("Selects a specific account")
|
||||
.value_name("NAME"),
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use lettre::transport::smtp::authentication::Credentials as SmtpCredentials;
|
||||
use log::{debug, trace};
|
||||
use serde::Deserialize;
|
||||
use shellexpand;
|
||||
|
@ -8,34 +9,7 @@ use toml;
|
|||
use crate::output::utils::run_cmd;
|
||||
|
||||
const DEFAULT_PAGE_SIZE: usize = 10;
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct Account {
|
||||
// TODO: rename with `from`
|
||||
pub name: Option<String>,
|
||||
pub downloads_dir: Option<PathBuf>,
|
||||
pub signature_delimiter: Option<String>,
|
||||
pub signature: Option<String>,
|
||||
pub default_page_size: Option<usize>,
|
||||
pub watch_cmds: Option<Vec<String>>,
|
||||
pub default: Option<bool>,
|
||||
pub email: String,
|
||||
pub imap_host: String,
|
||||
pub imap_port: u16,
|
||||
pub imap_starttls: Option<bool>,
|
||||
pub imap_insecure: Option<bool>,
|
||||
pub imap_login: String,
|
||||
pub imap_passwd_cmd: String,
|
||||
pub smtp_host: String,
|
||||
pub smtp_port: u16,
|
||||
pub smtp_starttls: Option<bool>,
|
||||
pub smtp_insecure: Option<bool>,
|
||||
pub smtp_login: String,
|
||||
pub smtp_passwd_cmd: String,
|
||||
}
|
||||
|
||||
pub type AccountsMap = HashMap<String, Account>;
|
||||
const DEFAULT_SIG_DELIM: &str = "-- \n";
|
||||
|
||||
/// Represents the whole config file.
|
||||
#[derive(Debug, Default, Clone, Deserialize)]
|
||||
|
@ -51,7 +25,7 @@ pub struct Config {
|
|||
pub default_page_size: Option<usize>,
|
||||
pub watch_cmds: Option<Vec<String>>,
|
||||
#[serde(flatten)]
|
||||
pub accounts: HashMap<String, Account>,
|
||||
pub accounts: ConfigAccountsMap,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
@ -105,7 +79,7 @@ impl Config {
|
|||
|
||||
/// Returns the account by the given name.
|
||||
/// If `name` is `None`, then the default account is returned.
|
||||
pub fn find_account_by_name(&self, name: Option<&str>) -> Result<&Account> {
|
||||
pub fn find_account_by_name(&self, name: Option<&str>) -> Result<&ConfigAccountEntry> {
|
||||
match name {
|
||||
Some("") | None => self
|
||||
.accounts
|
||||
|
@ -125,7 +99,7 @@ impl Config {
|
|||
/// ```skip
|
||||
/// Account-specifique-download-dir-path + Attachment-Filename
|
||||
/// ```
|
||||
pub fn downloads_filepath(&self, account: &Account, filename: &str) -> PathBuf {
|
||||
pub fn downloads_filepath(&self, account: &ConfigAccountEntry, filename: &str) -> PathBuf {
|
||||
account
|
||||
.downloads_dir
|
||||
.as_ref()
|
||||
|
@ -179,7 +153,7 @@ impl Config {
|
|||
/// assert_eq!(config.address(&special_account), "\"TL;DR\" <acc2@mail.com>");
|
||||
/// }
|
||||
/// ```
|
||||
pub fn address(&self, account: &Account) -> String {
|
||||
pub fn address(&self, account: &ConfigAccountEntry) -> String {
|
||||
let name = account.name.as_ref().unwrap_or(&self.name);
|
||||
let has_special_chars = "()<>[]:;@.,".contains(|special_char| name.contains(special_char));
|
||||
|
||||
|
@ -235,7 +209,7 @@ impl Config {
|
|||
/// assert_eq!(config_no_global.signature(&account2), None);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn signature(&self, account: &Account) -> Option<String> {
|
||||
pub fn signature(&self, account: &ConfigAccountEntry) -> Option<String> {
|
||||
let default_sig_delim = String::from("-- \n");
|
||||
let sig_delim = account
|
||||
.signature_delimiter
|
||||
|
@ -253,7 +227,7 @@ impl Config {
|
|||
.map(|sig| format!("\n{}{}", sig_delim, sig))
|
||||
}
|
||||
|
||||
pub fn default_page_size(&self, account: &Account) -> usize {
|
||||
pub fn default_page_size(&self, account: &ConfigAccountEntry) -> usize {
|
||||
account
|
||||
.default_page_size
|
||||
.as_ref()
|
||||
|
@ -263,7 +237,7 @@ impl Config {
|
|||
.to_owned()
|
||||
}
|
||||
|
||||
pub fn exec_watch_cmds(&self, account: &Account) -> Result<()> {
|
||||
pub fn exec_watch_cmds(&self, account: &ConfigAccountEntry) -> Result<()> {
|
||||
let cmds = account
|
||||
.watch_cmds
|
||||
.as_ref()
|
||||
|
@ -297,6 +271,324 @@ impl TryFrom<Option<&str>> for Config {
|
|||
}
|
||||
}
|
||||
|
||||
pub type ConfigAccountsMap = HashMap<String, ConfigAccountEntry>;
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct ConfigAccountEntry {
|
||||
// TODO: rename with `from`
|
||||
pub name: Option<String>,
|
||||
pub downloads_dir: Option<PathBuf>,
|
||||
pub signature_delimiter: Option<String>,
|
||||
pub signature: Option<String>,
|
||||
pub default_page_size: Option<usize>,
|
||||
pub watch_cmds: Option<Vec<String>>,
|
||||
pub default: Option<bool>,
|
||||
pub email: String,
|
||||
pub imap_host: String,
|
||||
pub imap_port: u16,
|
||||
pub imap_starttls: Option<bool>,
|
||||
pub imap_insecure: Option<bool>,
|
||||
pub imap_login: String,
|
||||
pub imap_passwd_cmd: String,
|
||||
pub smtp_host: String,
|
||||
pub smtp_port: u16,
|
||||
pub smtp_starttls: Option<bool>,
|
||||
pub smtp_insecure: Option<bool>,
|
||||
pub smtp_login: String,
|
||||
pub smtp_passwd_cmd: String,
|
||||
}
|
||||
|
||||
/// Representation of a user account.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Account {
|
||||
pub name: String,
|
||||
pub from: String,
|
||||
pub downloads_dir: PathBuf,
|
||||
pub signature: String,
|
||||
pub default_page_size: usize,
|
||||
pub watch_cmds: Vec<String>,
|
||||
|
||||
pub default: bool,
|
||||
pub email: String,
|
||||
|
||||
pub imap_host: String,
|
||||
pub imap_port: u16,
|
||||
pub imap_starttls: bool,
|
||||
pub imap_insecure: bool,
|
||||
pub imap_login: String,
|
||||
pub imap_passwd_cmd: String,
|
||||
|
||||
pub smtp_host: String,
|
||||
pub smtp_port: u16,
|
||||
pub smtp_starttls: bool,
|
||||
pub smtp_insecure: bool,
|
||||
pub smtp_login: String,
|
||||
pub smtp_passwd_cmd: String,
|
||||
}
|
||||
|
||||
impl Account {
|
||||
/// This is a little helper-function like which uses the the name and email
|
||||
/// of the account to create a valid address for the header of the headers
|
||||
/// of a msg.
|
||||
///
|
||||
/// # Hint
|
||||
/// If the name includes some special characters like a whitespace, comma or semicolon, then
|
||||
/// the name will be automatically wrapped between two `"`.
|
||||
///
|
||||
/// # Exapmle
|
||||
/// ```
|
||||
/// use himalaya::config::model::{Account, Config};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let config = Config::default();
|
||||
///
|
||||
/// let normal_account = Account::new(Some("Acc1"), "acc1@mail.com");
|
||||
/// // notice the semicolon in the name!
|
||||
/// let special_account = Account::new(Some("TL;DR"), "acc2@mail.com");
|
||||
///
|
||||
/// // -- Expeced outputs --
|
||||
/// let expected_normal = Account {
|
||||
/// name: Some("Acc1".to_string()),
|
||||
/// email: "acc1@mail.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let expected_special = Account {
|
||||
/// name: Some("\"TL;DR\"".to_string()),
|
||||
/// email: "acc2@mail.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(config.address(&normal_account), "Acc1 <acc1@mail.com>");
|
||||
/// assert_eq!(config.address(&special_account), "\"TL;DR\" <acc2@mail.com>");
|
||||
/// }
|
||||
/// ```
|
||||
pub fn address(&self) -> String {
|
||||
let name = &self.from;
|
||||
let has_special_chars = "()<>[]:;@.,".contains(|special_char| name.contains(special_char));
|
||||
|
||||
if name.is_empty() {
|
||||
format!("{}", self.email)
|
||||
} else if has_special_chars {
|
||||
// so the name has special characters => Wrap it with '"'
|
||||
format!("\"{}\" <{}>", name, self.email)
|
||||
} else {
|
||||
format!("{} <{}>", name, self.email)
|
||||
}
|
||||
}
|
||||
/// Returns the imap-host address + the port usage of the account
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use himalaya::config::model::Account;
|
||||
/// fn main () {
|
||||
/// let account = Account {
|
||||
/// imap_host: String::from("hostExample"),
|
||||
/// imap_port: 42,
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let expected_output = ("hostExample", 42);
|
||||
///
|
||||
/// assert_eq!(account.imap_addr(), expected_output);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn imap_addr(&self) -> (&str, u16) {
|
||||
debug!("host: {}", self.imap_host);
|
||||
debug!("port: {}", self.imap_port);
|
||||
(&self.imap_host, self.imap_port)
|
||||
}
|
||||
|
||||
/// Runs the given command in your password string and returns it.
|
||||
pub fn imap_passwd(&self) -> Result<String> {
|
||||
let passwd = run_cmd(&self.imap_passwd_cmd).context("cannot run IMAP passwd cmd")?;
|
||||
let passwd = passwd
|
||||
.trim_end_matches(|c| c == '\r' || c == '\n')
|
||||
.to_owned();
|
||||
|
||||
Ok(passwd)
|
||||
}
|
||||
|
||||
pub fn smtp_creds(&self) -> Result<SmtpCredentials> {
|
||||
let passwd = run_cmd(&self.smtp_passwd_cmd).context("cannot run SMTP passwd cmd")?;
|
||||
let passwd = passwd
|
||||
.trim_end_matches(|c| c == '\r' || c == '\n')
|
||||
.to_owned();
|
||||
|
||||
Ok(SmtpCredentials::new(self.smtp_login.to_owned(), passwd))
|
||||
}
|
||||
|
||||
/// Creates a new account with the given values and returns it. All other attributes of the
|
||||
/// account are gonna be empty/None.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use himalaya::config::model::Account;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let account1 = Account::new(Some("Name1"), "email@address.com");
|
||||
/// let account2 = Account::new(None, "email@address.com");
|
||||
///
|
||||
/// let expected1 = Account {
|
||||
/// name: Some("Name1".to_string()),
|
||||
/// email: "email@address.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let expected2 = Account {
|
||||
/// email: "email@address.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(account1, expected1);
|
||||
/// assert_eq!(account2, expected2);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new<S: ToString + Default>(name: Option<S>, email_addr: S) -> Self {
|
||||
Self {
|
||||
name: name.unwrap_or_default().to_string(),
|
||||
email: email_addr.to_string(),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new account with a custom signature. Passing `None` to `signature` sets the
|
||||
/// signature to `Account Signature`.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use himalaya::config::model::Account;
|
||||
///
|
||||
/// fn main() {
|
||||
///
|
||||
/// // the testing accounts
|
||||
/// let account_with_custom_signature = Account::new_with_signature(
|
||||
/// Some("Email name"), "some@mail.com", Some("Custom signature! :)"));
|
||||
/// let account_with_default_signature = Account::new_with_signature(
|
||||
/// Some("Email name"), "some@mail.com", None);
|
||||
///
|
||||
/// // How they should look like
|
||||
/// let account_cmp1 = Account {
|
||||
/// name: Some("Email name".to_string()),
|
||||
/// email: "some@mail.com".to_string(),
|
||||
/// signature: Some("Custom signature! :)".to_string()),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let account_cmp2 = Account {
|
||||
/// name: Some("Email name".to_string()),
|
||||
/// email: "some@mail.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(account_with_custom_signature, account_cmp1);
|
||||
/// assert_eq!(account_with_default_signature, account_cmp2);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new_with_signature<S: AsRef<str> + ToString + Default>(
|
||||
name: Option<S>,
|
||||
email_addr: S,
|
||||
signature: Option<S>,
|
||||
) -> Self {
|
||||
let mut account = Account::new(name, email_addr);
|
||||
account.signature = signature.unwrap_or_default().to_string();
|
||||
account
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<(&'a Config, Option<&str>)> for Account {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from((config, account_name): (&'a Config, Option<&str>)) -> Result<Self, Self::Error> {
|
||||
debug!("init account `{}`", account_name.unwrap_or("default"));
|
||||
let (name, account) = match account_name {
|
||||
Some("") | None => config
|
||||
.accounts
|
||||
.iter()
|
||||
.find(|(_, account)| account.default.unwrap_or(false))
|
||||
.map(|(name, account)| (name.to_owned(), account))
|
||||
.ok_or_else(|| anyhow!("cannot find default account")),
|
||||
Some(name) => config
|
||||
.accounts
|
||||
.get(name)
|
||||
.map(|account| (name.to_owned(), account))
|
||||
.ok_or_else(|| anyhow!(format!("cannot find account `{}`", name))),
|
||||
}?;
|
||||
|
||||
let downloads_dir = account
|
||||
.downloads_dir
|
||||
.as_ref()
|
||||
.and_then(|dir| dir.to_str())
|
||||
.and_then(|dir| shellexpand::full(dir).ok())
|
||||
.map(|dir| PathBuf::from(dir.to_string()))
|
||||
.or_else(|| {
|
||||
config
|
||||
.downloads_dir
|
||||
.as_ref()
|
||||
.and_then(|dir| dir.to_str())
|
||||
.and_then(|dir| shellexpand::full(dir).ok())
|
||||
.map(|dir| PathBuf::from(dir.to_string()))
|
||||
})
|
||||
.unwrap_or_else(|| env::temp_dir());
|
||||
|
||||
let default_page_size = account
|
||||
.default_page_size
|
||||
.as_ref()
|
||||
.or_else(|| config.default_page_size.as_ref())
|
||||
.unwrap_or(&DEFAULT_PAGE_SIZE)
|
||||
.to_owned();
|
||||
|
||||
let default_sig_delim = DEFAULT_SIG_DELIM.to_string();
|
||||
let signature_delim = account
|
||||
.signature_delimiter
|
||||
.as_ref()
|
||||
.or_else(|| config.signature_delimiter.as_ref())
|
||||
.unwrap_or(&default_sig_delim);
|
||||
let signature = account
|
||||
.signature
|
||||
.as_ref()
|
||||
.or_else(|| config.signature.as_ref());
|
||||
let signature = signature
|
||||
.and_then(|sig| shellexpand::full(sig).ok())
|
||||
.map(|sig| sig.to_string())
|
||||
.and_then(|sig| fs::read_to_string(sig).ok())
|
||||
.or_else(|| signature.map(|sig| sig.to_owned()))
|
||||
.map(|sig| format!("\n{}{}", signature_delim, sig))
|
||||
.unwrap_or_default();
|
||||
|
||||
let account = Account {
|
||||
name,
|
||||
from: account.name.as_ref().unwrap_or(&config.name).to_owned(),
|
||||
downloads_dir,
|
||||
signature,
|
||||
default_page_size,
|
||||
watch_cmds: account
|
||||
.watch_cmds
|
||||
.as_ref()
|
||||
.or_else(|| config.watch_cmds.as_ref())
|
||||
.unwrap_or(&vec![])
|
||||
.to_owned(),
|
||||
default: account.default.unwrap_or(false),
|
||||
email: account.email.to_owned(),
|
||||
imap_host: account.imap_host.to_owned(),
|
||||
imap_port: account.imap_port,
|
||||
imap_starttls: account.imap_starttls.unwrap_or_default(),
|
||||
imap_insecure: account.imap_insecure.unwrap_or_default(),
|
||||
imap_login: account.imap_login.to_owned(),
|
||||
imap_passwd_cmd: account.imap_passwd_cmd.to_owned(),
|
||||
smtp_host: account.smtp_host.to_owned(),
|
||||
smtp_port: account.smtp_port,
|
||||
smtp_starttls: account.smtp_starttls.unwrap_or_default(),
|
||||
smtp_insecure: account.smtp_insecure.unwrap_or_default(),
|
||||
smtp_login: account.smtp_login.to_owned(),
|
||||
smtp_passwd_cmd: account.smtp_passwd_cmd.to_owned(),
|
||||
};
|
||||
|
||||
trace!("{:#?}", account);
|
||||
Ok(account)
|
||||
}
|
||||
}
|
||||
// FIXME: tests
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
use clap::Arg;
|
||||
|
||||
/// Account argument.
|
||||
pub fn name_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("account")
|
||||
.long("account")
|
||||
.short("a")
|
||||
.help("Selects a specific account")
|
||||
.value_name("NAME")
|
||||
}
|
|
@ -1,300 +0,0 @@
|
|||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use lettre::transport::smtp::authentication::Credentials as SmtpCredentials;
|
||||
use log::{debug, trace};
|
||||
use std::{convert::TryFrom, env, fs, path::PathBuf};
|
||||
|
||||
use crate::{config::entity::Config, output::utils::run_cmd};
|
||||
|
||||
const DEFAULT_PAGE_SIZE: usize = 10;
|
||||
const DEFAULT_SIG_DELIM: &str = "-- \n";
|
||||
|
||||
/// Representation of a user account.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Account {
|
||||
pub name: String,
|
||||
pub from: String,
|
||||
pub downloads_dir: PathBuf,
|
||||
pub signature: String,
|
||||
pub default_page_size: usize,
|
||||
pub watch_cmds: Vec<String>,
|
||||
|
||||
pub default: bool,
|
||||
pub email: String,
|
||||
|
||||
pub imap_host: String,
|
||||
pub imap_port: u16,
|
||||
pub imap_starttls: bool,
|
||||
pub imap_insecure: bool,
|
||||
pub imap_login: String,
|
||||
pub imap_passwd_cmd: String,
|
||||
|
||||
pub smtp_host: String,
|
||||
pub smtp_port: u16,
|
||||
pub smtp_starttls: bool,
|
||||
pub smtp_insecure: bool,
|
||||
pub smtp_login: String,
|
||||
pub smtp_passwd_cmd: String,
|
||||
}
|
||||
|
||||
impl Account {
|
||||
/// This is a little helper-function like which uses the the name and email
|
||||
/// of the account to create a valid address for the header of the headers
|
||||
/// of a msg.
|
||||
///
|
||||
/// # Hint
|
||||
/// If the name includes some special characters like a whitespace, comma or semicolon, then
|
||||
/// the name will be automatically wrapped between two `"`.
|
||||
///
|
||||
/// # Exapmle
|
||||
/// ```
|
||||
/// use himalaya::config::model::{Account, Config};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let config = Config::default();
|
||||
///
|
||||
/// let normal_account = Account::new(Some("Acc1"), "acc1@mail.com");
|
||||
/// // notice the semicolon in the name!
|
||||
/// let special_account = Account::new(Some("TL;DR"), "acc2@mail.com");
|
||||
///
|
||||
/// // -- Expeced outputs --
|
||||
/// let expected_normal = Account {
|
||||
/// name: Some("Acc1".to_string()),
|
||||
/// email: "acc1@mail.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let expected_special = Account {
|
||||
/// name: Some("\"TL;DR\"".to_string()),
|
||||
/// email: "acc2@mail.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(config.address(&normal_account), "Acc1 <acc1@mail.com>");
|
||||
/// assert_eq!(config.address(&special_account), "\"TL;DR\" <acc2@mail.com>");
|
||||
/// }
|
||||
/// ```
|
||||
pub fn address(&self) -> String {
|
||||
let name = &self.from;
|
||||
let has_special_chars = "()<>[]:;@.,".contains(|special_char| name.contains(special_char));
|
||||
|
||||
if name.is_empty() {
|
||||
format!("{}", self.email)
|
||||
} else if has_special_chars {
|
||||
// so the name has special characters => Wrap it with '"'
|
||||
format!("\"{}\" <{}>", name, self.email)
|
||||
} else {
|
||||
format!("{} <{}>", name, self.email)
|
||||
}
|
||||
}
|
||||
/// Returns the imap-host address + the port usage of the account
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use himalaya::config::model::Account;
|
||||
/// fn main () {
|
||||
/// let account = Account {
|
||||
/// imap_host: String::from("hostExample"),
|
||||
/// imap_port: 42,
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let expected_output = ("hostExample", 42);
|
||||
///
|
||||
/// assert_eq!(account.imap_addr(), expected_output);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn imap_addr(&self) -> (&str, u16) {
|
||||
debug!("host: {}", self.imap_host);
|
||||
debug!("port: {}", self.imap_port);
|
||||
(&self.imap_host, self.imap_port)
|
||||
}
|
||||
|
||||
/// Runs the given command in your password string and returns it.
|
||||
pub fn imap_passwd(&self) -> Result<String> {
|
||||
let passwd = run_cmd(&self.imap_passwd_cmd).context("cannot run IMAP passwd cmd")?;
|
||||
let passwd = passwd
|
||||
.trim_end_matches(|c| c == '\r' || c == '\n')
|
||||
.to_owned();
|
||||
|
||||
Ok(passwd)
|
||||
}
|
||||
|
||||
pub fn smtp_creds(&self) -> Result<SmtpCredentials> {
|
||||
let passwd = run_cmd(&self.smtp_passwd_cmd).context("cannot run SMTP passwd cmd")?;
|
||||
let passwd = passwd
|
||||
.trim_end_matches(|c| c == '\r' || c == '\n')
|
||||
.to_owned();
|
||||
|
||||
Ok(SmtpCredentials::new(self.smtp_login.to_owned(), passwd))
|
||||
}
|
||||
|
||||
/// Creates a new account with the given values and returns it. All other attributes of the
|
||||
/// account are gonna be empty/None.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use himalaya::config::model::Account;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let account1 = Account::new(Some("Name1"), "email@address.com");
|
||||
/// let account2 = Account::new(None, "email@address.com");
|
||||
///
|
||||
/// let expected1 = Account {
|
||||
/// name: Some("Name1".to_string()),
|
||||
/// email: "email@address.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let expected2 = Account {
|
||||
/// email: "email@address.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(account1, expected1);
|
||||
/// assert_eq!(account2, expected2);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new<S: ToString + Default>(name: Option<S>, email_addr: S) -> Self {
|
||||
Self {
|
||||
name: name.unwrap_or_default().to_string(),
|
||||
email: email_addr.to_string(),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new account with a custom signature. Passing `None` to `signature` sets the
|
||||
/// signature to `Account Signature`.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use himalaya::config::model::Account;
|
||||
///
|
||||
/// fn main() {
|
||||
///
|
||||
/// // the testing accounts
|
||||
/// let account_with_custom_signature = Account::new_with_signature(
|
||||
/// Some("Email name"), "some@mail.com", Some("Custom signature! :)"));
|
||||
/// let account_with_default_signature = Account::new_with_signature(
|
||||
/// Some("Email name"), "some@mail.com", None);
|
||||
///
|
||||
/// // How they should look like
|
||||
/// let account_cmp1 = Account {
|
||||
/// name: Some("Email name".to_string()),
|
||||
/// email: "some@mail.com".to_string(),
|
||||
/// signature: Some("Custom signature! :)".to_string()),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// let account_cmp2 = Account {
|
||||
/// name: Some("Email name".to_string()),
|
||||
/// email: "some@mail.com".to_string(),
|
||||
/// .. Account::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(account_with_custom_signature, account_cmp1);
|
||||
/// assert_eq!(account_with_default_signature, account_cmp2);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new_with_signature<S: AsRef<str> + ToString + Default>(
|
||||
name: Option<S>,
|
||||
email_addr: S,
|
||||
signature: Option<S>,
|
||||
) -> Self {
|
||||
let mut account = Account::new(name, email_addr);
|
||||
account.signature = signature.unwrap_or_default().to_string();
|
||||
account
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<(&'a Config, Option<&str>)> for Account {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from((config, account_name): (&'a Config, Option<&str>)) -> Result<Self, Self::Error> {
|
||||
debug!("init account `{}`", account_name.unwrap_or("default"));
|
||||
let (name, account) = match account_name {
|
||||
Some("") | None => config
|
||||
.accounts
|
||||
.iter()
|
||||
.find(|(_, account)| account.default.unwrap_or(false))
|
||||
.map(|(name, account)| (name.to_owned(), account))
|
||||
.ok_or_else(|| anyhow!("cannot find default account")),
|
||||
Some(name) => config
|
||||
.accounts
|
||||
.get(name)
|
||||
.map(|account| (name.to_owned(), account))
|
||||
.ok_or_else(|| anyhow!(format!("cannot find account `{}`", name))),
|
||||
}?;
|
||||
|
||||
let downloads_dir = account
|
||||
.downloads_dir
|
||||
.as_ref()
|
||||
.and_then(|dir| dir.to_str())
|
||||
.and_then(|dir| shellexpand::full(dir).ok())
|
||||
.map(|dir| PathBuf::from(dir.to_string()))
|
||||
.or_else(|| {
|
||||
config
|
||||
.downloads_dir
|
||||
.as_ref()
|
||||
.and_then(|dir| dir.to_str())
|
||||
.and_then(|dir| shellexpand::full(dir).ok())
|
||||
.map(|dir| PathBuf::from(dir.to_string()))
|
||||
})
|
||||
.unwrap_or_else(|| env::temp_dir());
|
||||
|
||||
let default_page_size = account
|
||||
.default_page_size
|
||||
.as_ref()
|
||||
.or_else(|| config.default_page_size.as_ref())
|
||||
.unwrap_or(&DEFAULT_PAGE_SIZE)
|
||||
.to_owned();
|
||||
|
||||
let default_sig_delim = DEFAULT_SIG_DELIM.to_string();
|
||||
let signature_delim = account
|
||||
.signature_delimiter
|
||||
.as_ref()
|
||||
.or_else(|| config.signature_delimiter.as_ref())
|
||||
.unwrap_or(&default_sig_delim);
|
||||
let signature = account
|
||||
.signature
|
||||
.as_ref()
|
||||
.or_else(|| config.signature.as_ref());
|
||||
let signature = signature
|
||||
.and_then(|sig| shellexpand::full(sig).ok())
|
||||
.map(|sig| sig.to_string())
|
||||
.and_then(|sig| fs::read_to_string(sig).ok())
|
||||
.or_else(|| signature.map(|sig| sig.to_owned()))
|
||||
.map(|sig| format!("\n{}{}", signature_delim, sig))
|
||||
.unwrap_or_default();
|
||||
|
||||
let account = Account {
|
||||
name,
|
||||
from: account.name.as_ref().unwrap_or(&config.name).to_owned(),
|
||||
downloads_dir,
|
||||
signature,
|
||||
default_page_size,
|
||||
watch_cmds: account
|
||||
.watch_cmds
|
||||
.as_ref()
|
||||
.or_else(|| config.watch_cmds.as_ref())
|
||||
.unwrap_or(&vec![])
|
||||
.to_owned(),
|
||||
default: account.default.unwrap_or(false),
|
||||
email: account.email.to_owned(),
|
||||
imap_host: account.imap_host.to_owned(),
|
||||
imap_port: account.imap_port,
|
||||
imap_starttls: account.imap_starttls.unwrap_or_default(),
|
||||
imap_insecure: account.imap_insecure.unwrap_or_default(),
|
||||
imap_login: account.imap_login.to_owned(),
|
||||
imap_passwd_cmd: account.imap_passwd_cmd.to_owned(),
|
||||
smtp_host: account.smtp_host.to_owned(),
|
||||
smtp_port: account.smtp_port,
|
||||
smtp_starttls: account.smtp_starttls.unwrap_or_default(),
|
||||
smtp_insecure: account.smtp_insecure.unwrap_or_default(),
|
||||
smtp_login: account.smtp_login.to_owned(),
|
||||
smtp_passwd_cmd: account.smtp_passwd_cmd.to_owned(),
|
||||
};
|
||||
|
||||
trace!("{:#?}", account);
|
||||
Ok(account)
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
//! Modules related to the user's accounts.
|
||||
|
||||
pub mod arg;
|
||||
pub mod entity;
|
|
@ -9,9 +9,8 @@ use native_tls::{self, TlsConnector, TlsStream};
|
|||
use std::{collections::HashSet, convert::TryFrom, iter::FromIterator, net::TcpStream};
|
||||
|
||||
use crate::{
|
||||
config::entity::Config,
|
||||
config::entity::{Account, Config},
|
||||
domain::{
|
||||
account::entity::Account,
|
||||
mbox::entity::Mbox,
|
||||
msg::{entity::Msg, flag::entity::Flags},
|
||||
},
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
//! Domain-specific modules.
|
||||
|
||||
pub mod account;
|
||||
pub mod imap;
|
||||
pub mod mbox;
|
||||
pub mod msg;
|
||||
|
|
|
@ -5,7 +5,8 @@ use mailparse;
|
|||
|
||||
use super::{attachment::Attachment, body::Body, headers::Headers};
|
||||
use crate::{
|
||||
domain::{account::entity::Account, msg::flag::entity::Flags},
|
||||
config::entity::Account,
|
||||
domain::msg::flag::entity::Flags,
|
||||
ui::table::{Cell, Row, Table},
|
||||
};
|
||||
|
||||
|
|
|
@ -12,8 +12,8 @@ use std::{
|
|||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
config::entity::Account,
|
||||
domain::{
|
||||
account::entity::Account,
|
||||
imap::service::ImapServiceInterface,
|
||||
mbox::entity::Mbox,
|
||||
msg::{
|
||||
|
|
|
@ -4,8 +4,8 @@ use anyhow::Result;
|
|||
use log::trace;
|
||||
|
||||
use crate::{
|
||||
config::entity::Account,
|
||||
domain::{
|
||||
account::entity::Account,
|
||||
imap::service::ImapServiceInterface,
|
||||
msg::entity::{Msg, MsgSerialized},
|
||||
},
|
||||
|
|
|
@ -9,7 +9,7 @@ use lettre::{
|
|||
};
|
||||
use log::debug;
|
||||
|
||||
use crate::domain::account::entity::Account;
|
||||
use crate::config::entity::Account;
|
||||
|
||||
pub trait SmtpServiceInterface {
|
||||
fn send(&mut self, msg: &lettre::Message) -> Result<()>;
|
||||
|
|
|
@ -6,9 +6,11 @@ use url::Url;
|
|||
|
||||
use himalaya::{
|
||||
compl,
|
||||
config::{self, entity::Config},
|
||||
config::{
|
||||
self,
|
||||
entity::{Account, Config},
|
||||
},
|
||||
domain::{
|
||||
account::{self, entity::Account},
|
||||
imap::{self, service::ImapService},
|
||||
mbox::{self, entity::Mbox},
|
||||
msg,
|
||||
|
@ -23,8 +25,7 @@ fn create_app<'a>() -> clap::App<'a, 'a> {
|
|||
.about(env!("CARGO_PKG_DESCRIPTION"))
|
||||
.author(env!("CARGO_PKG_AUTHORS"))
|
||||
.args(&output_args())
|
||||
.arg(config::arg::path_arg())
|
||||
.arg(account::arg::name_arg())
|
||||
.args(&config::arg::args())
|
||||
.arg(mbox::arg::source_arg())
|
||||
.subcommands(compl::arg::subcmds())
|
||||
.subcommands(imap::arg::subcmds())
|
||||
|
|
Loading…
Reference in a new issue