mirror of
https://github.com/soywod/himalaya.git
synced 2024-09-06 02:41:12 +00:00
add starttls option, rename config keys to kebab-case
This commit is contained in:
parent
54612df215
commit
3281c27d57
|
@ -74,6 +74,7 @@ type Result<T> = result::Result<T, Error>;
|
||||||
// Account
|
// Account
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub struct Account {
|
pub struct Account {
|
||||||
// Override
|
// Override
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
@ -85,16 +86,22 @@ pub struct Account {
|
||||||
|
|
||||||
pub imap_host: String,
|
pub imap_host: String,
|
||||||
pub imap_port: u16,
|
pub imap_port: u16,
|
||||||
|
pub imap_starttls: Option<bool>,
|
||||||
pub imap_login: String,
|
pub imap_login: String,
|
||||||
pub imap_passwd_cmd: String,
|
pub imap_passwd_cmd: String,
|
||||||
|
|
||||||
pub smtp_host: String,
|
pub smtp_host: String,
|
||||||
pub smtp_port: u16,
|
pub smtp_port: u16,
|
||||||
|
pub smtp_starttls: Option<bool>,
|
||||||
pub smtp_login: String,
|
pub smtp_login: String,
|
||||||
pub smtp_passwd_cmd: String,
|
pub smtp_passwd_cmd: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Account {
|
impl Account {
|
||||||
|
pub fn imap_addr(&self) -> (&str, u16) {
|
||||||
|
(&self.imap_host, self.imap_port)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn imap_passwd(&self) -> Result<String> {
|
pub fn imap_passwd(&self) -> Result<String> {
|
||||||
let passwd = run_cmd(&self.imap_passwd_cmd)?;
|
let passwd = run_cmd(&self.imap_passwd_cmd)?;
|
||||||
let passwd = passwd.trim_end_matches("\n").to_owned();
|
let passwd = passwd.trim_end_matches("\n").to_owned();
|
||||||
|
@ -108,15 +115,12 @@ impl Account {
|
||||||
|
|
||||||
Ok(SmtpCredentials::new(self.smtp_login.to_owned(), passwd))
|
Ok(SmtpCredentials::new(self.smtp_login.to_owned(), passwd))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn imap_addr(&self) -> (&str, u16) {
|
|
||||||
(&self.imap_host, self.imap_port)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub downloads_dir: Option<PathBuf>,
|
pub downloads_dir: Option<PathBuf>,
|
||||||
|
|
|
@ -80,7 +80,10 @@ pub struct ImapConnector<'a> {
|
||||||
impl<'a> ImapConnector<'a> {
|
impl<'a> ImapConnector<'a> {
|
||||||
pub fn new(account: &'a Account) -> Result<Self> {
|
pub fn new(account: &'a Account) -> Result<Self> {
|
||||||
let tls = TlsConnector::new()?;
|
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
|
let sess = client
|
||||||
.login(&account.imap_login, &account.imap_passwd()?)
|
.login(&account.imap_login, &account.imap_passwd()?)
|
||||||
.map_err(|res| res.0)?;
|
.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> {
|
pub fn list_msgs(&mut self, mbox: &str, page_size: &u32, page: &u32) -> Result<Msgs> {
|
||||||
let last_seq = self.sess.select(mbox)?.exists;
|
let last_seq = self.sess.select(mbox)?.exists;
|
||||||
let begin = last_seq - (page * page_size);
|
let begin = last_seq - page * page_size;
|
||||||
let end = begin - (page_size - 1);
|
let end = begin - (begin - 1).min(page_size - 1);
|
||||||
let range = format!("{}:{}", begin, end);
|
let range = format!("{}:{}", begin, end);
|
||||||
|
|
||||||
let msgs = self
|
let msgs = self
|
||||||
|
|
Loading…
Reference in a new issue