From b44778a6c835c86a8c1ba7c373aaac08db4557a2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 30 Dec 2019 12:41:08 +0100 Subject: Add flags module for flags representation as strong types Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/lib.rs | 1 + lib/domain/libimagmail/src/mailflags.rs | 80 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 lib/domain/libimagmail/src/mailflags.rs 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/mailflags.rs b/lib/domain/libimagmail/src/mailflags.rs new file mode 100644 index 00000000..ad8d6c52 --- /dev/null +++ b/lib/domain/libimagmail/src/mailflags.rs @@ -0,0 +1,80 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2020 Matthias Beyer 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 FromStr for MailFlag { + type Err = Error; + + fn from_str(s: &str) -> Result { + 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) + } +} + -- cgit v1.2.3 From d5cbf655a3edf86594bd5aac539b527c596675b2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 30 Dec 2019 12:41:16 +0100 Subject: Add function to get flags Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/mail.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index 1d2f5a2d..6e5f2876 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,8 @@ pub trait Mail : RefFassade { fn get_subject(&self, refconfig: &RefConfig) -> Result>; fn get_message_id(&self, refconfig: &RefConfig) -> Result>; fn get_in_reply_to(&self, refconfig: &RefConfig) -> Result>; + + fn flags(&self, refconfig: &RefConfig) -> Result>; } impl Mail for Entry { @@ -51,7 +57,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> { 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::().get_path(refconfig)?; @@ -134,6 +139,31 @@ 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> { + let path = self.as_ref_with_hasher::().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::>() + .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::>>() + } + } + } #[derive(Debug)] -- cgit v1.2.3 From 2e0fc1e4f0f03b7d7677bf7c0ff2076e2e2599aa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 31 Dec 2019 13:21:24 +0100 Subject: Add function to get char representation of flag Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/mailflags.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/domain/libimagmail/src/mailflags.rs b/lib/domain/libimagmail/src/mailflags.rs index ad8d6c52..5fd52761 100644 --- a/lib/domain/libimagmail/src/mailflags.rs +++ b/lib/domain/libimagmail/src/mailflags.rs @@ -47,6 +47,19 @@ pub enum MailFlag { 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; -- cgit v1.2.3 From 7b4556c190656537e473deb435124d030948298b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 3 Jan 2020 12:59:27 +0100 Subject: Add helper functions to check whether a mail is passed, replied,... Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/mail.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index 6e5f2876..db75b5d3 100644 --- a/lib/domain/libimagmail/src/mail.rs +++ b/lib/domain/libimagmail/src/mail.rs @@ -46,6 +46,12 @@ pub trait Mail : RefFassade { fn get_in_reply_to(&self, refconfig: &RefConfig) -> Result>; fn flags(&self, refconfig: &RefConfig) -> Result>; + fn is_passed(&self, refconfig: &RefConfig) -> Result; + fn is_replied(&self, refconfig: &RefConfig) -> Result; + fn is_seen(&self, refconfig: &RefConfig) -> Result; + fn is_trashed(&self, refconfig: &RefConfig) -> Result; + fn is_draft(&self, refconfig: &RefConfig) -> Result; + fn is_flagged(&self, refconfig: &RefConfig) -> Result; } impl Mail for Entry { @@ -164,6 +170,37 @@ impl Mail for Entry { } } + /// Check whether the mail is passed + fn is_passed(&self, refconfig: &RefConfig) -> Result { + 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 { + 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 { + 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 { + 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 { + 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 { + self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Flagged == f)) + } + + } #[derive(Debug)] -- cgit v1.2.3