mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-08 18:45:13 +00:00
improve mailbox alias management
This commit is contained in:
parent
1f01202262
commit
811ea45610
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use log::{debug, info, trace};
|
use log::{debug, info, trace};
|
||||||
use std::{convert::TryInto, fs, path::PathBuf};
|
use std::{convert::TryInto, env, fs, path::PathBuf};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
backends::{Backend, IdMapper, MaildirEnvelopes, MaildirFlags, MaildirMboxes},
|
backends::{Backend, IdMapper, MaildirEnvelopes, MaildirFlags, MaildirMboxes},
|
||||||
|
@ -41,33 +41,32 @@ impl<'a> MaildirBackend<'a> {
|
||||||
|
|
||||||
/// Creates a maildir instance from a string slice.
|
/// Creates a maildir instance from a string slice.
|
||||||
pub fn get_mdir_from_dir(&self, dir: &str) -> Result<maildir::Maildir> {
|
pub fn get_mdir_from_dir(&self, dir: &str) -> Result<maildir::Maildir> {
|
||||||
|
let dir = self.account_config.get_mbox_alias(dir)?;
|
||||||
|
|
||||||
// If the dir points to the inbox folder, creates a maildir
|
// If the dir points to the inbox folder, creates a maildir
|
||||||
// instance from the root folder.
|
// instance from the root folder.
|
||||||
if dir.to_lowercase() == "inbox" {
|
if &dir == "inbox" {
|
||||||
self.validate_mdir_path(self.mdir.path().to_owned())
|
return self
|
||||||
.map(maildir::Maildir::from)
|
.validate_mdir_path(self.mdir.path().to_owned())
|
||||||
} else {
|
.map(maildir::Maildir::from);
|
||||||
// If the dir is a valid maildir path, creates a maildir
|
|
||||||
// instance from it. Checks for absolute path first,
|
|
||||||
self.validate_mdir_path(dir.into())
|
|
||||||
// then for relative path,
|
|
||||||
.or_else(|_| self.validate_mdir_path(self.mdir.path().join(dir)))
|
|
||||||
.or_else(|_| {
|
|
||||||
// otherwise creates a maildir instance from a
|
|
||||||
// maildir subdirectory by adding a "." in front
|
|
||||||
// of the name as described in the spec:
|
|
||||||
// https://cr.yp.to/proto/maildir.html.
|
|
||||||
let dir = self
|
|
||||||
.account_config
|
|
||||||
.mailboxes
|
|
||||||
.get(dir)
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or(dir);
|
|
||||||
let path = self.mdir.path().join(format!(".{}", dir));
|
|
||||||
self.validate_mdir_path(path)
|
|
||||||
})
|
|
||||||
.map(maildir::Maildir::from)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the dir is a valid maildir path, creates a maildir
|
||||||
|
// instance from it. First checks for absolute path,
|
||||||
|
self.validate_mdir_path((&dir).into())
|
||||||
|
// then for relative path to `maildir-dir`,
|
||||||
|
.or_else(|_| self.validate_mdir_path(self.mdir.path().join(&dir)))
|
||||||
|
// and finally for relative path to the current directory.
|
||||||
|
.or_else(|_| self.validate_mdir_path(env::current_dir()?.join(&dir)))
|
||||||
|
.or_else(|_| {
|
||||||
|
// Otherwise creates a maildir instance from a maildir
|
||||||
|
// subdirectory by adding a "." in front of the name
|
||||||
|
// as described in the [spec].
|
||||||
|
//
|
||||||
|
// [spec]: http://www.courier-mta.org/imap/README.maildirquota.html
|
||||||
|
self.validate_mdir_path(self.mdir.path().join(format!(".{}", dir)))
|
||||||
|
})
|
||||||
|
.map(maildir::Maildir::from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -317,6 +317,19 @@ impl<'a> AccountConfig {
|
||||||
run_cmd(&cmd).context("cannot run notify cmd")?;
|
run_cmd(&cmd).context("cannot run notify cmd")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the mailbox alias if exists, otherwise returns the
|
||||||
|
/// mailbox. Also tries to expand shell variables.
|
||||||
|
pub fn get_mbox_alias(&self, mbox: &str) -> Result<String> {
|
||||||
|
let mbox = self
|
||||||
|
.mailboxes
|
||||||
|
.get(&mbox.trim().to_lowercase())
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.unwrap_or(mbox);
|
||||||
|
shellexpand::full(mbox)
|
||||||
|
.map(String::from)
|
||||||
|
.with_context(|| format!("cannot expand mailbox path {:?}", mbox))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents all existing kind of account (backend).
|
/// Represents all existing kind of account (backend).
|
||||||
|
|
Loading…
Reference in a new issue