diff options
-rw-r--r-- | bin/domain/imag-mail/Cargo.toml | 3 | ||||
-rw-r--r-- | bin/domain/imag-mail/src/lib.rs | 92 | ||||
-rw-r--r-- | bin/domain/imag-mail/src/ui.rs | 14 | ||||
-rw-r--r-- | bin/domain/imag-mail/src/util.rs | 87 | ||||
-rw-r--r-- | imagrc.toml | 1 | ||||
-rw-r--r-- | lib/core/libimagstore/src/store.rs | 12 | ||||
-rw-r--r-- | lib/domain/libimagmail/src/lib.rs | 1 | ||||
-rw-r--r-- | lib/domain/libimagmail/src/mail.rs | 69 | ||||
-rw-r--r-- | lib/domain/libimagmail/src/mailflags.rs | 93 |
9 files changed, 285 insertions, 87 deletions
diff --git a/bin/domain/imag-mail/Cargo.toml b/bin/domain/imag-mail/Cargo.toml index 68e90552..fd871089 100644 --- a/bin/domain/imag-mail/Cargo.toml +++ b/bin/domain/imag-mail/Cargo.toml @@ -22,8 +22,8 @@ maintenance = { status = "actively-developed" } [dependencies] log = "0.4.6" failure = "0.1.5" -indoc = "0.3.3" resiter = "0.4" +handlebars = "2" walkdir = "2" rayon = "1" @@ -33,6 +33,7 @@ libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" libimagmail = { version = "0.10.0", path = "../../../lib/domain/libimagmail" } libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" } libimagentryref = { version = "0.10.0", path = "../../../lib/entry/libimagentryref" } +libimaginteraction = { version = "0.10.0", path = "../../../lib/etc/libimaginteraction" } [dependencies.clap] version = "2.33.0" diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs index 0240042a..4bb217ff 100644 --- a/bin/domain/imag-mail/src/lib.rs +++ b/bin/domain/imag-mail/src/lib.rs @@ -38,8 +38,8 @@ extern crate clap; #[macro_use] extern crate log; #[macro_use] extern crate failure; extern crate toml_query; -#[macro_use] extern crate indoc; extern crate resiter; +extern crate handlebars; extern crate walkdir; extern crate rayon; @@ -49,6 +49,7 @@ extern crate libimagerror; extern crate libimagstore; extern crate libimagutil; extern crate libimagentryref; +extern crate libimaginteraction; use std::io::Write; use std::path::PathBuf; @@ -64,20 +65,17 @@ use resiter::IterInnerOkOrElse; use resiter::Map; use rayon::prelude::*; -use libimagmail::mail::Mail; use libimagmail::store::MailStore; -use libimagmail::util; -use libimagentryref::reference::{Ref, RefFassade}; use libimagentryref::util::get_ref_config; use libimagentryref::reference::Config as RefConfig; use libimagrt::runtime::Runtime; use libimagrt::application::ImagApplication; use libimagutil::info_result::*; -use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreIdIterator; use libimagstore::iter::get::StoreIdGetIteratorExtension; mod ui; +mod util; /// Marker enum for implementing ImagApplication on /// @@ -206,77 +204,10 @@ fn import_mail(rt: &Runtime) -> Result<()> { } fn list(rt: &Runtime) -> Result<()> { - let refconfig = get_ref_config(rt, "imag-mail")?; - let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe via clap - let print_content = scmd.is_present("list-read"); - - if print_content { - // TODO: Check whether workaround with "{}" is still necessary when updating "indoc" - warn!("{}", indoc!(r#"You requested to print the content of the mail as well. - We use the 'mailparse' crate underneath, but its implementation is nonoptimal. - Thus, the content might be printed as empty (no text in the email) - This is not reliable and might be wrong."#)); - - // TODO: Fix above. - } - - // TODO: Implement lister type in libimagmail for this - // - // Optimization: Pass refconfig here instead of call get_ref_config() in lister function. This - // way we do not call get_ref_config() multiple times. - fn list_mail<'a>(rt: &Runtime, - refconfig: &::libimagentryref::reference::Config, - m: &FileLockEntry<'a>, - print_content: bool) -> Result<()> { - - let id = match m.get_message_id(&refconfig)? { - Some(f) => f.into(), - None => "<no id>".to_owned(), - }; - - let from = match m.get_from(&refconfig)? { - Some(f) => f, - None => "<no from>".to_owned(), - }; - - let to = match m.get_to(&refconfig)? { - Some(f) => f, - None => "<no to>".to_owned(), - }; - - let subject = match m.get_subject(&refconfig)? { - Some(f) => f, - None => "<no subject>".to_owned(), - }; - - if print_content { - use libimagmail::hasher::MailHasher; - - let content = m.as_ref_with_hasher::<MailHasher>() - .get_path(&refconfig) - .and_then(util::get_mail_text_content)?; - - writeln!(rt.stdout(), - "Mail: {id}\nFrom: {from}\nTo: {to}\n{subj}\n---\n{content}\n---\n", - from = from, - id = id, - subj = subject, - to = to, - content = content - )?; - } else { - writeln!(rt.stdout(), - "Mail: {id}\nFrom: {from}\nTo: {to}\n{subj}\n", - from = from, - id = id, - subj = subject, - to = to - )?; - } - - rt.report_touched(m.get_location())?; - Ok(()) - } + let refconfig = get_ref_config(rt, "imag-mail")?; + let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe via clap + let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?; + let mut i = 0; if rt.ids_from_stdin() { let iter = rt @@ -294,7 +225,14 @@ fn list(rt: &Runtime) -> Result<()> { .inspect(|id| debug!("Found: {:?}", id)) .into_get_iter(rt.store()) .map_inner_ok_or_else(|| err_msg("Did not find one entry")) - .and_then_ok(|m| list_mail(&rt, &refconfig, &m, print_content)) + .and_then_ok(|m| { + let data = util::build_data_object_for_handlebars(i, &m, &refconfig)?; + let s = list_format.render("format", &data)?; + writeln!(rt.stdout(), "{}", s)?; + rt.report_touched(m.get_location())?; + i += 1; + Ok(()) + }) .collect::<Result<Vec<_>>>() .map(|_| ()) } diff --git a/bin/domain/imag-mail/src/ui.rs b/bin/domain/imag-mail/src/ui.rs index 0af3e880..2462e15e 100644 --- a/bin/domain/imag-mail/src/ui.rs +++ b/bin/domain/imag-mail/src/ui.rs @@ -78,14 +78,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .about("List all stored references to mails") .version("0.1") - .arg(Arg::with_name("list-read") - .long("read") - .short("r") - .takes_value(false) - .required(false) - .multiple(false) - .help("Print the textual content of the mail itself as well")) - .arg(Arg::with_name("list-id") .index(1) .takes_value(true) @@ -93,6 +85,12 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .multiple(true) .help("The ids of the mails to list information for")) + .arg(Arg::with_name("format") + .long("format") + .takes_value(true) + .required(false) + .multiple(false) + .help("The format to list the mails with")) ) .subcommand(SubCommand::with_name("mail-store") diff --git a/bin/domain/imag-mail/src/util.rs b/bin/domain/imag-mail/src/util.rs new file mode 100644 index 00000000..77d6b5a5 --- /dev/null +++ b/bin/domain/imag-mail/src/util.rs @@ -0,0 +1,87 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +use std::collections::BTreeMap; + +use clap::ArgMatches; +use failure::Error; +use failure::Fallible as Result; +use failure::err_msg; +use handlebars::Handlebars; +use toml_query::read::TomlValueReadTypeExt; + +use libimagmail::mail::Mail; +use libimagrt::runtime::Runtime; +use libimagstore::store::FileLockEntry; +use libimagentryref::reference::Config as RefConfig; + +pub fn get_mail_print_format(config_value_path: &'static str, rt: &Runtime, scmd: &ArgMatches) -> Result<Handlebars> { + let fmt = match scmd.value_of("format").map(String::from) { + Some(s) => Ok(s), + None => { + rt.config() + .ok_or_else(|| err_msg("No configuration file"))? + .read_string(config_value_path) + .map_err(Error::from)? + .ok_or_else(|| format_err!("Configuration '{}' does not exist", config_value_path)) + } + }?; + + let mut hb = Handlebars::new(); + hb.register_template_string("format", fmt)?; + + hb.register_escape_fn(::handlebars::no_escape); + ::libimaginteraction::format::register_all_color_helpers(&mut hb); + ::libimaginteraction::format::register_all_format_helpers(&mut hb); + Ok(hb) +} + +pub fn build_data_object_for_handlebars(i: usize, m: &FileLockEntry, refconfig: &RefConfig) -> Result<BTreeMap<&'static str, String>> { + let mut data = BTreeMap::new(); + + data.insert("i", format!("{}", i)); + + let id = match m.get_message_id(refconfig)? { + Some(f) => f.into(), + None => "<no id>".to_owned(), + }; + + let from = match m.get_from(refconfig)? { + Some(f) => f, + None => "<no from>".to_owned(), + }; + + let to = match m.get_to(refconfig)? { + Some(f) => f, + None => "<no to>".to_owned(), + }; + + let subject = match m.get_subject(refconfig)? { + Some(f) => f, + None => "<no subject>".to_owned(), + }; + + data.insert("id" , id); + data.insert("from" , from); + data.insert("to" , to); + data.insert("subject" , subject); + + Ok(data) +} + diff --git a/imagrc.toml b/imagrc.toml index 40a3ac49..33b7195b 100644 --- a/imagrc.toml +++ b/imagrc.toml @@ -374,6 +374,7 @@ calendars = "/home/user/calendars" [mail] # The name of the mail reference collection ref_collection_name = "mail" +list_format = "{{i}} - {{id}} - {{from}} -> {{to}}\t: {{subject}}" [todo] show_format = """ diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs index 26612965..5137b517 100644 --- a/lib/core/libimagstore/src/store.rs +++ b/lib/core/libimagstore/src/store.rs @@ -216,6 +216,18 @@ impl Store { Ok(store) } + pub fn is_borrowed<S: IntoStoreId>(&self, id: S) -> Result<bool> { + let id = id.into_storeid()?; + debug!("Checking whether id is borrowed: '{}'", id); + + self.entries + .read() + .map_err(|_| Error::from(EM::LockError)) + .context(format_err!("Error while checking whether {} is borrowed", id)) + .map(|cache| cache.get(&id).map(|e| e.is_borrowed()).unwrap_or(false)) + .map_err(Error::from) + } + /// Creates the Entry at the given location (inside the entry) /// /// # Return value diff --git a/lib/domain/libimagmail/src/lib.rs b/lib/domain/libimagmail/src/lib.rs index b3d5785b..2334a74f 100644 --- a/lib/domain/libimagmail/src/lib.rs +++ b/lib/domain/libimagmail/src/lib.rs @@ -56,6 +56,7 @@ module_entry_path_mod!("mail"); pub mod config; pub mod hasher; pub mod mail; +pub mod mailflags; pub mod mid; pub mod store; pub mod util; diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index 1d2f5a2d..db75b5d3 100644 --- a/lib/domain/libimagmail/src/mail.rs +++ b/lib/domain/libimagmail/src/mail.rs @@ -17,6 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::str::FromStr; + use failure::Fallible as Result; use failure::ResultExt; use failure::Error; @@ -29,6 +31,8 @@ use libimagentryref::reference::Config as RefConfig; use libimagentryref::reference::{Ref, RefFassade}; use crate::mid::MessageId; +use crate::mailflags::MailFlag; +use crate::hasher::MailHasher; provide_kindflag_path!(pub IsMail, "mail.is_mail"); @@ -40,6 +44,14 @@ pub trait Mail : RefFassade { fn get_subject(&self, refconfig: &RefConfig) -> Result<Option<String>>; fn get_message_id(&self, refconfig: &RefConfig) -> Result<Option<MessageId>>; fn get_in_reply_to(&self, refconfig: &RefConfig) -> Result<Option<MessageId>>; + + fn flags(&self, refconfig: &RefConfig) -> Result<Vec<MailFlag>>; + fn is_passed(&self, refconfig: &RefConfig) -> Result<bool>; + fn is_replied(&self, refconfig: &RefConfig) -> Result<bool>; + fn is_seen(&self, refconfig: &RefConfig) -> Result<bool>; + fn is_trashed(&self, refconfig: &RefConfig) -> Result<bool>; + fn is_draft(&self, refconfig: &RefConfig) -> Result<bool>; + fn is_flagged(&self, refconfig: &RefConfig) -> Result<bool>; } impl Mail for Entry { @@ -51,7 +63,6 @@ impl Mail for Entry { /// Get a value of a single field of the mail file fn get_field(&self, refconfig: &RefConfig, field: &str) -> Result<Option<String>> { use std::fs::read_to_string; - use crate::hasher::MailHasher; debug!("Getting field in mail: {:?}", field); let mail_file_location = self.as_ref_with_hasher::<MailHasher>().get_path(refconfig)?; @@ -134,6 +145,62 @@ impl Mail for Entry { .map(|o| o.map(crate::util::strip_message_delimiters).map(MessageId::from)) } + /// Get the flags of the message + fn flags(&self, refconfig: &RefConfig) -> Result<Vec<MailFlag>> { + let path = self.as_ref_with_hasher::<MailHasher>().get_path(refconfig)?; + + if !path.exists() { + return Err(format_err!("Path {} does not exist", path.display())) + } + + { + // Now parse mail flags + path.to_str() + .ok_or_else(|| format_err!("Path is not UTF-8: {}", path.display()))? + .split("2,") + .map(String::from) + .collect::<Vec<String>>() + .split_last() + .ok_or_else(|| format_err!("Splitting path into prefix and flags failed: {}", path.display()))? + .0 + .chars() + .map(|c| c.to_string()) + .map(|c| MailFlag::from_str(&c)) + .collect::<Result<Vec<_>>>() + } + } + + /// Check whether the mail is passed + fn is_passed(&self, refconfig: &RefConfig) -> Result<bool> { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Passed == f)) + } + + /// Check whether the mail is replied + fn is_replied(&self, refconfig: &RefConfig) -> Result<bool> { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Replied == f)) + } + + /// Check whether the mail is seen + fn is_seen(&self, refconfig: &RefConfig) -> Result<bool> { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Seen == f)) + } + + /// Check whether the mail is trashed + fn is_trashed(&self, refconfig: &RefConfig) -> Result<bool> { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Trashed == f)) + } + + /// Check whether the mail is draft + fn is_draft(&self, refconfig: &RefConfig) -> Result<bool> { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Draft == f)) + } + + /// Check whether the mail is flagged + fn is_flagged(&self, refconfig: &RefConfig) -> Result<bool> { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Flagged == f)) + } + + } #[derive(Debug)] diff --git a/lib/domain/libimagmail/src/mailflags.rs b/lib/domain/libimagmail/src/mailflags.rs new file mode 100644 index 00000000..5fd52761 --- /dev/null +++ b/lib/domain/libimagmail/src/mailflags.rs @@ -0,0 +1,93 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +use std::fmt::{Display, Result as FmtResult, Formatter}; +use std::str::FromStr; + +use failure::Fallible as Result; +use failure::Error; + +/// Message flags +/// +/// As defined by https://cr.yp.to/proto/maildir.html with strong typing +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum MailFlag { + /// Flag "P" (passed): the user has resent/forwarded/bounced this message to someone else. + Passed, + + /// Flag "R" (replied): the user has replied to this message. + Replied, + + /// Flag "S" (seen): the user has viewed this message, though perhaps he didn't read all the way through it. + Seen, + + /// Flag "T" (trashed): the user has moved this message to the trash; the trash will be emptied by a later user action. + Trashed, + + /// Flag "D" (draft): the user considers this message a draft; toggled at user discretion. + Draft, + + /// Flag "F" (flagged): user-defined flag; toggled at user discretion. + Flagged, +} + +impl MailFlag { + pub fn as_char(self) -> char { + match self { + MailFlag::Passed => 'P', + MailFlag::Replied => 'R', + MailFlag::Seen => 'S', + MailFlag::Trashed => 'T', + MailFlag::Draft => 'D', + MailFlag::Flagged => 'F', + } + } +} + +impl FromStr for MailFlag { + type Err = Error; + + fn from_str(s: &str) -> Result<MailFlag> { + match s { + "P" => Ok(MailFlag::Passed), + "R" => Ok(MailFlag::Replied), + "S" => Ok(MailFlag::Seen), + "T" => Ok(MailFlag::Trashed), + "D" => Ok(MailFlag::Draft), + "F" => Ok(MailFlag::Flagged), + _ => Err(format_err!("Unknown message flag: '{}'", s)), + } + } +} + +impl Display for MailFlag { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + let s = match self { + MailFlag::Passed => "Passed", + MailFlag::Replied => "Replied", + MailFlag::Seen => "Seen", + MailFlag::Trashed => "Trashed", + MailFlag::Draft => "Draft", + MailFlag::Flagged => "Flagged", + }; + + write!(f, "{}", s) + } +} + |