use crate::config::AccountConfig;
use secret::Secret;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MailProvider {
Gmail,
ICloud,
Outlook,
Yahoo,
Fastmail,
Custom,
}
impl MailProvider {
pub fn all() -> &'static [(&'static str, MailProvider)] {
&[
("Gmail", MailProvider::Gmail),
("iCloud", MailProvider::ICloud),
("Outlook", MailProvider::Outlook),
("Yahoo", MailProvider::Yahoo),
("Fastmail", MailProvider::Fastmail),
("Custom", MailProvider::Custom),
]
}
pub fn from_name(name: &str) -> Option<MailProvider> {
match name.to_lowercase().as_str() {
"gmail" | "google" => Some(MailProvider::Gmail),
"icloud" | "apple" => Some(MailProvider::ICloud),
"outlook" | "microsoft" | "office365" | "hotmail" | "live" => {
Some(MailProvider::Outlook)
}
"yahoo" => Some(MailProvider::Yahoo),
"fastmail" => Some(MailProvider::Fastmail),
"custom" => Some(MailProvider::Custom),
_ => None,
}
}
pub fn name(&self) -> &'static str {
match self {
MailProvider::Gmail => "Gmail",
MailProvider::ICloud => "iCloud",
MailProvider::Outlook => "Outlook",
MailProvider::Yahoo => "Yahoo",
MailProvider::Fastmail => "Fastmail",
MailProvider::Custom => "Custom",
}
}
pub fn host(&self) -> &'static str {
match self {
MailProvider::Gmail => "imap.gmail.com",
MailProvider::ICloud => "imap.mail.me.com",
MailProvider::Outlook => "outlook.office365.com",
MailProvider::Yahoo => "imap.mail.yahoo.com",
MailProvider::Fastmail => "imap.fastmail.com",
MailProvider::Custom => "",
}
}
pub fn port(&self) -> u16 {
993
}
pub fn trash_mailbox(&self) -> &'static str {
match self {
MailProvider::Gmail => "[Gmail]/Trash",
MailProvider::ICloud => "Deleted Messages",
MailProvider::Outlook => "Deleted",
MailProvider::Yahoo => "Trash",
MailProvider::Fastmail => "Trash",
MailProvider::Custom => "Trash",
}
}
pub fn drafts_mailbox(&self) -> &'static str {
match self {
MailProvider::Gmail => "[Gmail]/Drafts",
MailProvider::Yahoo => "Draft",
_ => "Drafts",
}
}
pub fn default_config(&self, username: &str) -> AccountConfig {
let password = secret::keyring::KeyringEntry::try_new(username)
.ok()
.map(Secret::new_keyring_entry);
AccountConfig {
host: self.host().to_string(),
port: self.port(),
username: username.to_string(),
password,
tls: true,
trash_mailbox: Some(self.trash_mailbox().to_string()),
drafts_mailbox: Some(self.drafts_mailbox().to_string()),
max_connections: None,
}
}
}