add starttls option, rename config keys to kebab-case

This commit is contained in:
Clément DOUIN 2021-03-05 13:59:50 +01:00
parent 54612df215
commit 3281c27d57
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
2 changed files with 14 additions and 7 deletions

View file

@ -74,6 +74,7 @@ type Result<T> = result::Result<T, Error>;
// Account
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Account {
// Override
pub name: Option<String>,
@ -85,16 +86,22 @@ pub struct Account {
pub imap_host: String,
pub imap_port: u16,
pub imap_starttls: 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_login: String,
pub smtp_passwd_cmd: String,
}
impl Account {
pub fn imap_addr(&self) -> (&str, u16) {
(&self.imap_host, self.imap_port)
}
pub fn imap_passwd(&self) -> Result<String> {
let passwd = run_cmd(&self.imap_passwd_cmd)?;
let passwd = passwd.trim_end_matches("\n").to_owned();
@ -108,15 +115,12 @@ impl Account {
Ok(SmtpCredentials::new(self.smtp_login.to_owned(), passwd))
}
pub fn imap_addr(&self) -> (&str, u16) {
(&self.imap_host, self.imap_port)
}
}
// Config
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub name: String,
pub downloads_dir: Option<PathBuf>,

View file

@ -80,7 +80,10 @@ pub struct ImapConnector<'a> {
impl<'a> ImapConnector<'a> {
pub fn new(account: &'a Account) -> Result<Self> {
let tls = TlsConnector::new()?;
let client = imap::connect(account.imap_addr(), &account.imap_host, &tls)?;
let client = match account.imap_starttls {
Some(true) => imap::connect_starttls(account.imap_addr(), &account.imap_host, &tls),
_ => imap::connect(account.imap_addr(), &account.imap_host, &tls),
}?;
let sess = client
.login(&account.imap_login, &account.imap_passwd()?)
.map_err(|res| res.0)?;
@ -107,8 +110,8 @@ impl<'a> ImapConnector<'a> {
pub fn list_msgs(&mut self, mbox: &str, page_size: &u32, page: &u32) -> Result<Msgs> {
let last_seq = self.sess.select(mbox)?.exists;
let begin = last_seq - (page * page_size);
let end = begin - (page_size - 1);
let begin = last_seq - page * page_size;
let end = begin - (begin - 1).min(page_size - 1);
let range = format!("{}:{}", begin, end);
let msgs = self