From b22df840dbda699957ae48eca019e3ecfa493bd9 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 4 Jan 2024 17:55:16 +0100 Subject: WIP refactor of the different views --- src/imap/mime_view.rs | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 src/imap/mime_view.rs diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs new file mode 100644 index 0000000..ba324a2 --- /dev/null +++ b/src/imap/mime_view.rs @@ -0,0 +1,292 @@ +use std::borrow::Cow; +use std::num::NonZeroU32; +use std::collections::HashSet; + +use anyhow::{anyhow, bail, Result}; + +use imap_codec::imap_types::core::{AString, IString, NonEmptyVec}; +use imap_codec::imap_types::fetch::{ + Section as FetchSection, Part as FetchPart +}; + +use eml_codec::{ + header, part::AnyPart, +}; + + +pub enum BodySection<'a> { + Full(Cow<'a, [u8]>), + Slice { + body: Cow<'a, [u8]>, + origin_octet: u32, + }, +} + +/// Logic for BODY[
]<> +/// Works in 3 times: +/// 1. Find the section (RootMime::subset) +/// 2. Apply the extraction logic (SelectedMime::extract), like TEXT, HEADERS, etc. +/// 3. Keep only the given subset provided by partial +pub fn body_ext<'a>( + part: &'a AnyPart<'a>, + section: &Option, + partial: &Option<(u32, NonZeroU32)> +) -> Result> { + let root_mime = RootMime(part); + let (extractor, path) = SubsettedSection::from(section); + let selected_mime = root_mime.subset(path); + unimplemented!(); + } + +/// RootMime +/// +/// Example of message sections: +/// +/// ``` +/// HEADER ([RFC-2822] header of the message) +/// TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED +/// 1 TEXT/PLAIN +/// 2 APPLICATION/OCTET-STREAM +/// 3 MESSAGE/RFC822 +/// 3.HEADER ([RFC-2822] header of the message) +/// 3.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED +/// 3.1 TEXT/PLAIN +/// 3.2 APPLICATION/OCTET-STREAM +/// 4 MULTIPART/MIXED +/// 4.1 IMAGE/GIF +/// 4.1.MIME ([MIME-IMB] header for the IMAGE/GIF) +/// 4.2 MESSAGE/RFC822 +/// 4.2.HEADER ([RFC-2822] header of the message) +/// 4.2.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED +/// 4.2.1 TEXT/PLAIN +/// 4.2.2 MULTIPART/ALTERNATIVE +/// 4.2.2.1 TEXT/PLAIN +/// 4.2.2.2 TEXT/RICHTEXT +/// ``` + + +struct RootMime<'a>(&'a AnyPart<'a>); +impl<'a> RootMime<'a> { + + + /// A MIME object is a tree of elements. + /// The path indicates which element must be picked. + /// This function returns the picked element as the new view + fn subset(&self, path: Option<&FetchPart>) -> Result { + match path { + None => Ok(SelectedMime(self.0)), + Some(v) => self.rec_subset(v.0.as_ref()), + } + } + + fn rec_subset(self, path: &[NonZeroU32]) -> Result { + if path.is_empty() { + Ok(SelectedMime(self.0)) + } else { + match self.0 { + AnyPart::Mult(x) => { + let next = Self(x.children + .get(path[0].get() as usize - 1) + .ok_or(anyhow!("Unable to resolve subpath {:?}, current multipart has only {} elements", path, x.children.len()))?); + next.rec_subset(&path[1..]) + }, + AnyPart::Msg(x) => { + let next = Self(x.child.as_ref()); + next.rec_subset(path) + }, + _ => bail!("You tried to access a subpart on an atomic part (text or binary). Unresolved subpath {:?}", path), + } + } + } +} + +//---------------------------------------------------------- + +/// A FetchSection must be handled in 2 times: +/// - First we must extract the MIME part +/// - Then we must process it as desired +/// The given struct mixes both work, so +/// we separate this work here. +enum SubsettedSection<'a> { + Part, + Header, + HeaderFields(&'a NonEmptyVec>), + HeaderFieldsNot(&'a NonEmptyVec>), + Text, + Mime, +} +impl<'a> SubsettedSection<'a> { + fn from(section: &'a Option) -> (Self, Option<&'a FetchPart>) { + match section { + Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), + Some(FetchSection::Header(maybe_part)) => (Self::Header, maybe_part.as_ref()), + Some(FetchSection::HeaderFields(maybe_part, fields)) => (Self::HeaderFields(fields), maybe_part.as_ref()), + Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => (Self::HeaderFieldsNot(fields), maybe_part.as_ref()), + Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), + Some(FetchSection::Mime(part)) => (Self::Mime, Some(part)), + Some(FetchSection::Part(part)) => (Self::Part, Some(part)), + None => (Self::Part, None), + } + } +} + +struct SelectedMime<'a>(&'a AnyPart<'a>); +impl<'a> SelectedMime<'a> { + /// The subsetted fetch section basically tells us the + /// extraction logic to apply on our selected MIME. + /// This function acts as a router for these logic. + fn extract(&self, extractor: &SubsettedSection) -> Result> { + match extractor { + SubsettedSection::Text => self.text(), + SubsettedSection::Header => self.header(), + SubsettedSection::HeaderFields(fields) => self.header_fields(fields, false), + SubsettedSection::HeaderFieldsNot(fields) => self.header_fields(fields, true), + SubsettedSection::Part => self.part(), + SubsettedSection::Mime => self.mime(), + } + } + + fn mime(&self) -> Result> { + let bytes = match &self.0 { + AnyPart::Txt(p) => p.mime.fields.raw, + AnyPart::Bin(p) => p.mime.fields.raw, + AnyPart::Msg(p) => p.child.mime().raw, + AnyPart::Mult(p) => p.mime.fields.raw, + }; + Ok(ExtractedFull(bytes.to_vec().into())) + } + + fn part(&self) -> Result> { + let bytes = match &self.0 { + AnyPart::Txt(p) => p.body, + AnyPart::Bin(p) => p.body, + AnyPart::Msg(p) => p.raw_part, + AnyPart::Mult(_) => bail!("Multipart part has no body"), + }; + Ok(ExtractedFull(bytes.to_vec().into())) + } + + /// The [...] HEADER.FIELDS, and HEADER.FIELDS.NOT part + /// specifiers refer to the [RFC-2822] header of the message or of + /// an encapsulated [MIME-IMT] MESSAGE/RFC822 message. + /// HEADER.FIELDS and HEADER.FIELDS.NOT are followed by a list of + /// field-name (as defined in [RFC-2822]) names, and return a + /// subset of the header. The subset returned by HEADER.FIELDS + /// contains only those header fields with a field-name that + /// matches one of the names in the list; similarly, the subset + /// returned by HEADER.FIELDS.NOT contains only the header fields + /// with a non-matching field-name. The field-matching is + /// case-insensitive but otherwise exact. + fn header_fields(&self, fields: &'a NonEmptyVec>, invert: bool) -> Result> { + // Build a lowercase ascii hashset with the fields to fetch + let index = fields + .as_ref() + .iter() + .map(|x| match x { + AString::Atom(a) => a.inner().as_bytes(), + AString::String(IString::Literal(l)) => l.as_ref(), + AString::String(IString::Quoted(q)) => q.inner().as_bytes(), + }.to_ascii_lowercase()) + .collect::>(); + + // Extract MIME headers + let mime = match &self.0 { + AnyPart::Msg(msg) => msg.child.mime(), + other => other.mime(), + }; + + // Filter our MIME headers based on the field index + // 1. Keep only the correctly formatted headers + // 2. Keep only based on the index presence or absence + // 3. Reduce as a byte vector + let buffer = mime.kv.iter() + .filter_map(|field| match field { + header::Field::Good(header::Kv2(k, v)) => Some((k, v)), + _ => None, + }) + .filter(|(k, _)| index.contains(&k.to_ascii_lowercase()) ^ invert) + .fold(vec![], |mut acc, (k, v)| { + acc.extend(*k); + acc.extend(b": "); + acc.extend(*v); + acc.extend(b"\r\n"); + acc + }); + + Ok(ExtractedFull(buffer.into())) + } + + /// The HEADER [...] part specifiers refer to the [RFC-2822] header of the message or of + /// an encapsulated [MIME-IMT] MESSAGE/RFC822 message. + /// ```raw + /// HEADER ([RFC-2822] header of the message) + /// ``` + fn header(&self) -> Result> { + let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + Ok(ExtractedFull(msg.raw_headers.into())) + } + + /// The TEXT part specifier refers to the text body of the message, omitting the [RFC-2822] header. + fn text(&self) -> Result> { + let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + Ok(ExtractedFull(msg.raw_body.into())) + } +} + +// --------------------------- + +struct ExtractedFull<'a>(Cow<'a, [u8]>); +impl<'a> ExtractedFull<'a> { + /// It is possible to fetch a substring of the designated text. + /// This is done by appending an open angle bracket ("<"), the + /// octet position of the first desired octet, a period, the + /// maximum number of octets desired, and a close angle bracket + /// (">") to the part specifier. If the starting octet is beyond + /// the end of the text, an empty string is returned. + /// + /// Any partial fetch that attempts to read beyond the end of the + /// text is truncated as appropriate. A partial fetch that starts + /// at octet 0 is returned as a partial fetch, even if this + /// truncation happened. + /// + /// Note: This means that BODY[]<0.2048> of a 1500-octet message + /// will return BODY[]<0> with a literal of size 1500, not + /// BODY[]. + /// + /// Note: A substring fetch of a HEADER.FIELDS or + /// HEADER.FIELDS.NOT part specifier is calculated after + /// subsetting the header. + fn to_body_section(&self, partial: &'_ Option<(u32, NonZeroU32)>) -> BodySection<'a> { + match partial { + Some((begin, len)) => self.partialize(*begin, *len), + None => BodySection::Full(self.0), + } + } + + fn partialize(&self, begin: u32, len: NonZeroU32) -> BodySection<'a> { + // Asked range is starting after the end of the content, + // returning an empty buffer + if begin as usize > self.0.len() { + return BodySection::Slice { + body: Cow::Borrowed(&[][..]), + origin_octet: begin, + } + } + + // Asked range is ending after the end of the content, + // slice only the beginning of the buffer + if (begin + len.get()) as usize >= self.0.len() { + return BodySection::Slice { + body: Cow::Borrowed(&self.0[begin as usize..]), + origin_octet: begin, + } + } + + // Range is included inside the considered content, + // this is the "happy case" + BodySection::Slice { + body: Cow::Borrowed(&self.0[begin as usize..(begin + len.get()) as usize]), + origin_octet: begin, + } + } +} -- cgit v1.2.3 From 2a9ae1297bf5c4a4b9552eeabb00246d76004f63 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 4 Jan 2024 20:54:21 +0100 Subject: bcp commit --- src/imap/attributes.rs | 51 +++ src/imap/flags.rs | 30 ++ src/imap/imf_view.rs | 129 +++++++ src/imap/mail_view.rs | 232 +++++++++++++ src/imap/mailbox_view.rs | 854 ++--------------------------------------------- src/imap/mime_view.rs | 292 ---------------- src/imap/mod.rs | 6 + src/imap/selectors.rs | 103 ++++++ 8 files changed, 580 insertions(+), 1117 deletions(-) create mode 100644 src/imap/attributes.rs create mode 100644 src/imap/flags.rs create mode 100644 src/imap/imf_view.rs create mode 100644 src/imap/mail_view.rs create mode 100644 src/imap/selectors.rs diff --git a/src/imap/attributes.rs b/src/imap/attributes.rs new file mode 100644 index 0000000..66b078e --- /dev/null +++ b/src/imap/attributes.rs @@ -0,0 +1,51 @@ +use imap_codec::imap_types::fetch::{ + MacroOrMessageDataItemNames, MessageDataItemName, +}; + +/// Internal decisions based on fetched attributes +/// passed by the client + +pub struct AttributesProxy { + pub attrs: Vec>, +} +impl AttributesProxy { + pub fn new(attrs: &MacroOrMessageDataItemNames<'static>, is_uid_fetch: bool) -> Self { + // Expand macros + let mut fetch_attrs = match attrs { + MacroOrMessageDataItemNames::Macro(m) => { + use imap_codec::imap_types::fetch::Macro; + use MessageDataItemName::*; + match m { + Macro::All => vec![Flags, InternalDate, Rfc822Size, Envelope], + Macro::Fast => vec![Flags, InternalDate, Rfc822Size], + Macro::Full => vec![Flags, InternalDate, Rfc822Size, Envelope, Body], + _ => { + tracing::error!("unimplemented macro"); + vec![] + } + } + } + MacroOrMessageDataItemNames::MessageDataItemNames(a) => a.clone(), + }; + + // Handle uids + if is_uid_fetch && !fetch_attrs.contains(&MessageDataItemName::Uid) { + fetch_attrs.push(MessageDataItemName::Uid); + } + + Self { attrs: fetch_attrs } + } + + pub fn need_body(&self) -> bool { + self.attrs.iter().any(|x| { + matches!( + x, + MessageDataItemName::Body + | MessageDataItemName::BodyExt { .. } + | MessageDataItemName::Rfc822 + | MessageDataItemName::Rfc822Text + | MessageDataItemName::BodyStructure + ) + }) + } +} diff --git a/src/imap/flags.rs b/src/imap/flags.rs new file mode 100644 index 0000000..0f6ec64 --- /dev/null +++ b/src/imap/flags.rs @@ -0,0 +1,30 @@ +use imap_codec::imap_types::core::Atom; +use imap_codec::imap_types::flag::{Flag, FlagFetch}; + +pub fn from_str(f: &str) -> Option> { + match f.chars().next() { + Some('\\') => match f { + "\\Seen" => Some(FlagFetch::Flag(Flag::Seen)), + "\\Answered" => Some(FlagFetch::Flag(Flag::Answered)), + "\\Flagged" => Some(FlagFetch::Flag(Flag::Flagged)), + "\\Deleted" => Some(FlagFetch::Flag(Flag::Deleted)), + "\\Draft" => Some(FlagFetch::Flag(Flag::Draft)), + "\\Recent" => Some(FlagFetch::Recent), + _ => match Atom::try_from(f.strip_prefix('\\').unwrap().to_string()) { + Err(_) => { + tracing::error!(flag=%f, "Unable to encode flag as IMAP atom"); + None + } + Ok(a) => Some(FlagFetch::Flag(Flag::system(a))), + }, + }, + Some(_) => match Atom::try_from(f.to_string()) { + Err(_) => { + tracing::error!(flag=%f, "Unable to encode flag as IMAP atom"); + None + } + Ok(a) => Some(FlagFetch::Flag(Flag::keyword(a))), + }, + None => None, + } +} diff --git a/src/imap/imf_view.rs b/src/imap/imf_view.rs new file mode 100644 index 0000000..dce53e6 --- /dev/null +++ b/src/imap/imf_view.rs @@ -0,0 +1,129 @@ +use std::borrow::Cow; +use std::iter::zip; +use std::num::NonZeroU32; +use std::sync::Arc; + +use anyhow::{anyhow, bail, Error, Result}; +use chrono::{Offset, TimeZone, Utc}; + +use futures::stream::{FuturesOrdered, StreamExt}; + +use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; +use imap_codec::imap_types::core::{AString, Atom, IString, NString, NonEmptyVec}; +use imap_codec::imap_types::datetime::DateTime; +use imap_codec::imap_types::envelope::{Address, Envelope}; +use imap_codec::imap_types::fetch::{ + MacroOrMessageDataItemNames, MessageDataItem, MessageDataItemName, Section as FetchSection, +}; +use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; +use imap_codec::imap_types::response::{Code, Data, Status}; +use imap_codec::imap_types::sequence::{self, SequenceSet}; + +use eml_codec::{ + header, imf, mime, + mime::r#type::Deductible, + part::{composite::Message, AnyPart}, +}; + +use crate::cryptoblob::Key; +use crate::imap::response::Body; +use crate::mail::mailbox::{MailMeta, Mailbox}; +use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; +use crate::mail::unique_ident::UniqueIdent; + + + + +/// Envelope rules are defined in RFC 3501, section 7.4.2 +/// https://datatracker.ietf.org/doc/html/rfc3501#section-7.4.2 +/// +/// Some important notes: +/// +/// If the Sender or Reply-To lines are absent in the [RFC-2822] +/// header, or are present but empty, the server sets the +/// corresponding member of the envelope to be the same value as +/// the from member (the client is not expected to know to do +/// this). Note: [RFC-2822] requires that all messages have a valid +/// From header. Therefore, the from, sender, and reply-to +/// members in the envelope can not be NIL. +/// +/// If the Date, Subject, In-Reply-To, and Message-ID header lines +/// are absent in the [RFC-2822] header, the corresponding member +/// of the envelope is NIL; if these header lines are present but +/// empty the corresponding member of the envelope is the empty +/// string. + +//@FIXME return an error if the envelope is invalid instead of panicking +//@FIXME some fields must be defaulted if there are not set. +pub fn message_envelope(msg: &imf::Imf) -> Envelope<'static> { + let from = msg.from.iter().map(convert_mbx).collect::>(); + + Envelope { + date: NString( + msg.date + .as_ref() + .map(|d| IString::try_from(d.to_rfc3339()).unwrap()), + ), + subject: NString( + msg.subject + .as_ref() + .map(|d| IString::try_from(d.to_string()).unwrap()), + ), + sender: msg + .sender + .as_ref() + .map(|v| vec![convert_mbx(v)]) + .unwrap_or(from.clone()), + reply_to: if msg.reply_to.is_empty() { + from.clone() + } else { + convert_addresses(&msg.reply_to) + }, + from, + to: convert_addresses(&msg.to), + cc: convert_addresses(&msg.cc), + bcc: convert_addresses(&msg.bcc), + in_reply_to: NString( + msg.in_reply_to + .iter() + .next() + .map(|d| IString::try_from(d.to_string()).unwrap()), + ), + message_id: NString( + msg.msg_id + .as_ref() + .map(|d| IString::try_from(d.to_string()).unwrap()), + ), + } +} + +pub fn convert_addresses(addrlist: &Vec) -> Vec> { + let mut acc = vec![]; + for item in addrlist { + match item { + imf::address::AddressRef::Single(a) => acc.push(convert_mbx(a)), + imf::address::AddressRef::Many(l) => acc.extend(l.participants.iter().map(convert_mbx)), + } + } + return acc; +} + +pub fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address<'static> { + Address { + name: NString( + addr.name + .as_ref() + .map(|x| IString::try_from(x.to_string()).unwrap()), + ), + // SMTP at-domain-list (source route) seems obsolete since at least 1991 + // https://www.mhonarc.org/archive/html/ietf-822/1991-06/msg00060.html + adl: NString(None), + mailbox: NString(Some( + IString::try_from(addr.addrspec.local_part.to_string()).unwrap(), + )), + host: NString(Some( + IString::try_from(addr.addrspec.domain.to_string()).unwrap(), + )), + } +} + diff --git a/src/imap/mail_view.rs b/src/imap/mail_view.rs new file mode 100644 index 0000000..d1f8a58 --- /dev/null +++ b/src/imap/mail_view.rs @@ -0,0 +1,232 @@ +use std::num::NonZeroU32; + +use anyhow::{anyhow, bail, Result}; +use chrono::{Offset, TimeZone, Utc}; + + +use imap_codec::imap_types::core::{IString, NString}; +use imap_codec::imap_types::datetime::DateTime; +use imap_codec::imap_types::fetch::{ + MessageDataItem, MessageDataItemName, Section as FetchSection, +}; +use imap_codec::imap_types::flag::Flag; +use imap_codec::imap_types::response::Data; + +use eml_codec::{ + imf, + part::{composite::Message, AnyPart}, +}; + +use crate::imap::response::Body; +use crate::imap::mime_view; +use crate::imap::flags; +use crate::imap::attributes::AttributesProxy; +use crate::mail::mailbox::MailMeta; +use crate::imap::mailbox_view::MailIdentifiers; +use crate::imap::imf_view::message_envelope; + +pub struct MailView<'a> { + pub ids: &'a MailIdentifiers, + pub meta: &'a MailMeta, + pub flags: &'a Vec, + pub content: FetchedMail<'a>, +} + +impl<'a> MailView<'a> { + fn uid(&self) -> MessageDataItem<'static> { + MessageDataItem::Uid(self.ids.uid.clone()) + } + + fn flags(&self) -> MessageDataItem<'static> { + MessageDataItem::Flags( + self.flags + .iter() + .filter_map(|f| flags::from_str(f)) + .collect(), + ) + } + + fn rfc_822_size(&self) -> MessageDataItem<'static> { + MessageDataItem::Rfc822Size(self.meta.rfc822_size as u32) + } + + fn rfc_822_header(&self) -> MessageDataItem<'static> { + MessageDataItem::Rfc822Header(NString( + self.meta + .headers + .to_vec() + .try_into() + .ok() + .map(IString::Literal), + )) + } + + fn rfc_822_text(&self) -> Result> { + Ok(MessageDataItem::Rfc822Text(NString( + self.content + .as_full()? + .raw_body + .to_vec() + .try_into() + .ok() + .map(IString::Literal), + ))) + } + + fn rfc822(&self) -> Result> { + Ok(MessageDataItem::Rfc822(NString( + self.content + .as_full()? + .raw_part + .to_vec() + .try_into() + .ok() + .map(IString::Literal), + ))) + } + + fn envelope(&self) -> MessageDataItem<'static> { + MessageDataItem::Envelope(message_envelope(self.content.imf().clone())) + } + + fn body(&self) -> Result> { + Ok(MessageDataItem::Body(mime_view::bodystructure( + self.content.as_full()?.child.as_ref(), + )?)) + } + + fn body_structure(&self) -> Result> { + Ok(MessageDataItem::Body(mime_view::bodystructure( + self.content.as_full()?.child.as_ref(), + )?)) + } + + /// maps to BODY[
]<> and BODY.PEEK[
]<> + /// peek does not implicitly set the \Seen flag + /// eg. BODY[HEADER.FIELDS (DATE FROM)] + /// eg. BODY[]<0.2048> + fn body_ext<'b>( + &self, + section: &Option>, + partial: &Option<(u32, NonZeroU32)>, + peek: &bool, + ) -> Result<(MessageDataItem<'b>, SeenFlag)> { + // Manage Seen flag + let mut seen = SeenFlag::DoNothing; + let seen_flag = Flag::Seen.to_string(); + if !peek && !self.flags.iter().any(|x| *x == seen_flag) { + // Add \Seen flag + //self.mailbox.add_flags(uuid, &[seen_flag]).await?; + seen = SeenFlag::MustAdd; + } + + // Process message + let (text, origin) = match mime_view::body_ext(self.content.as_anypart()?, section, partial)? { + mime_view::BodySection::Full(body) => (body, None), + mime_view::BodySection::Slice { body, origin_octet } => (body, Some(origin_octet)), + }; + + let data = NString(text.to_vec().try_into().ok().map(IString::Literal)); + + return Ok(( + MessageDataItem::BodyExt { + section: section.as_ref().map(|fs| fs.clone()), + origin, + data, + }, + seen, + )); + } + + fn internal_date(&self) -> Result> { + let dt = Utc + .fix() + .timestamp_opt(i64::try_from(self.meta.internaldate / 1000)?, 0) + .earliest() + .ok_or(anyhow!("Unable to parse internal date"))?; + Ok(MessageDataItem::InternalDate(DateTime::unvalidated(dt))) + } + + pub fn filter<'b>(&self, ap: &AttributesProxy) -> Result<(Body<'static>, SeenFlag)> { + let mut seen = SeenFlag::DoNothing; + let res_attrs = ap + .attrs + .iter() + .map(|attr| match attr { + MessageDataItemName::Uid => Ok(self.uid()), + MessageDataItemName::Flags => Ok(self.flags()), + MessageDataItemName::Rfc822Size => Ok(self.rfc_822_size()), + MessageDataItemName::Rfc822Header => Ok(self.rfc_822_header()), + MessageDataItemName::Rfc822Text => self.rfc_822_text(), + MessageDataItemName::Rfc822 => self.rfc822(), + MessageDataItemName::Envelope => Ok(self.envelope()), + MessageDataItemName::Body => self.body(), + MessageDataItemName::BodyStructure => self.body_structure(), + MessageDataItemName::BodyExt { + section, + partial, + peek, + } => { + let (body, has_seen) = self.body_ext(section, partial, peek)?; + seen = has_seen; + Ok(body) + } + MessageDataItemName::InternalDate => self.internal_date(), + }) + .collect::, _>>()?; + + Ok(( + Body::Data(Data::Fetch { + seq: self.ids.i, + items: res_attrs.try_into()?, + }), + seen, + )) + } +} + + +pub enum SeenFlag { + DoNothing, + MustAdd, +} + + +// ------------------- + +pub enum FetchedMail<'a> { + Partial(imf::Imf<'a>), + Full(AnyPart<'a>), +} +impl<'a> FetchedMail<'a> { + pub fn new_from_message(msg: Message<'a>) -> Self { + FetchedMail::Full(AnyPart::Msg(msg)) + } + + /*fn new_from_header(hdr: imf::Imf<'a>) -> Self { + FetchedMail::Partial(hdr) + }*/ + + fn as_anypart(&self) -> Result<&AnyPart<'a>> { + match self { + FetchedMail::Full(x) => Ok(&x), + _ => bail!("The full message must be fetched, not only its headers"), + } + } + + fn as_full(&self) -> Result<&Message<'a>> { + match self { + FetchedMail::Full(AnyPart::Msg(x)) => Ok(&x), + _ => bail!("The full message must be fetched, not only its headers AND it must be an AnyPart::Msg."), + } + } + + fn imf(&self) -> &imf::Imf<'a> { + match self { + FetchedMail::Full(AnyPart::Msg(x)) => &x.imf, + FetchedMail::Partial(x) => &x, + _ => panic!("Can't contain AnyPart that is not a message"), + } + } +} + diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 861d27c..04253d0 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -1,33 +1,23 @@ -use std::borrow::Cow; -use std::iter::zip; use std::num::NonZeroU32; use std::sync::Arc; use anyhow::{anyhow, bail, Error, Result}; -use chrono::{Offset, TimeZone, Utc}; use futures::stream::{FuturesOrdered, StreamExt}; -use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; -use imap_codec::imap_types::core::{AString, Atom, IString, NString, NonEmptyVec}; -use imap_codec::imap_types::datetime::DateTime; -use imap_codec::imap_types::envelope::{Address, Envelope}; use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItem, MessageDataItemName, Section as FetchSection, + MacroOrMessageDataItemNames, MessageDataItem }; use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; use imap_codec::imap_types::response::{Code, Data, Status}; use imap_codec::imap_types::sequence::{self, SequenceSet}; -use eml_codec::{ - header, imf, mime, - mime::r#type::Deductible, - part::{composite::Message, AnyPart}, -}; - -use crate::cryptoblob::Key; +use crate::imap::flags; use crate::imap::response::Body; -use crate::mail::mailbox::{MailMeta, Mailbox}; +use crate::imap::attributes::AttributesProxy; +use crate::imap::selectors::MailSelectionBuilder; +use crate::imap::mail_view::SeenFlag; +use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; use crate::mail::unique_ident::UniqueIdent; @@ -39,377 +29,6 @@ const DEFAULT_FLAGS: [Flag; 5] = [ Flag::Draft, ]; -enum FetchedMail<'a> { - Partial(imf::Imf<'a>), - Full(AnyPart<'a>), -} -impl<'a> FetchedMail<'a> { - fn new_from_message(msg: Message<'a>) -> Self { - FetchedMail::Full(AnyPart::Msg(msg)) - } - - /*fn new_from_header(hdr: imf::Imf<'a>) -> Self { - FetchedMail::Partial(hdr) - }*/ - - fn as_anypart(&self) -> Result<&AnyPart<'a>> { - match self { - FetchedMail::Full(x) => Ok(&x), - _ => bail!("The full message must be fetched, not only its headers"), - } - } - - fn as_full(&self) -> Result<&Message<'a>> { - match self { - FetchedMail::Full(AnyPart::Msg(x)) => Ok(&x), - _ => bail!("The full message must be fetched, not only its headers AND it must be an AnyPart::Msg."), - } - } - - fn imf(&self) -> &imf::Imf<'a> { - match self { - FetchedMail::Full(AnyPart::Msg(x)) => &x.imf, - FetchedMail::Partial(x) => &x, - _ => panic!("Can't contain AnyPart that is not a message"), - } - } -} - -pub struct AttributesProxy { - attrs: Vec>, -} -impl AttributesProxy { - fn new(attrs: &MacroOrMessageDataItemNames<'static>, is_uid_fetch: bool) -> Self { - // Expand macros - let mut fetch_attrs = match attrs { - MacroOrMessageDataItemNames::Macro(m) => { - use imap_codec::imap_types::fetch::Macro; - use MessageDataItemName::*; - match m { - Macro::All => vec![Flags, InternalDate, Rfc822Size, Envelope], - Macro::Fast => vec![Flags, InternalDate, Rfc822Size], - Macro::Full => vec![Flags, InternalDate, Rfc822Size, Envelope, Body], - _ => { - tracing::error!("unimplemented macro"); - vec![] - } - } - } - MacroOrMessageDataItemNames::MessageDataItemNames(a) => a.clone(), - }; - - // Handle uids - if is_uid_fetch && !fetch_attrs.contains(&MessageDataItemName::Uid) { - fetch_attrs.push(MessageDataItemName::Uid); - } - - Self { attrs: fetch_attrs } - } - - fn need_body(&self) -> bool { - self.attrs.iter().any(|x| { - matches!( - x, - MessageDataItemName::Body - | MessageDataItemName::BodyExt { .. } - | MessageDataItemName::Rfc822 - | MessageDataItemName::Rfc822Text - | MessageDataItemName::BodyStructure - ) - }) - } -} - -pub struct MailIdentifiers { - i: NonZeroU32, - uid: ImapUid, - uuid: UniqueIdent, -} -struct MailIdentifiersList(Vec); - -impl MailIdentifiersList { - fn uuids(&self) -> Vec { - self.0.iter().map(|mi| mi.uuid).collect() - } -} - -pub struct MailView<'a> { - ids: &'a MailIdentifiers, - meta: &'a MailMeta, - flags: &'a Vec, - content: FetchedMail<'a>, -} - -enum SeenFlag { - DoNothing, - MustAdd, -} - -impl<'a> MailView<'a> { - fn uid(&self) -> MessageDataItem<'static> { - MessageDataItem::Uid(self.ids.uid.clone()) - } - - fn flags(&self) -> MessageDataItem<'static> { - MessageDataItem::Flags( - self.flags - .iter() - .filter_map(|f| string_to_flag(f)) - .collect(), - ) - } - - fn rfc_822_size(&self) -> MessageDataItem<'static> { - MessageDataItem::Rfc822Size(self.meta.rfc822_size as u32) - } - - fn rfc_822_header(&self) -> MessageDataItem<'static> { - MessageDataItem::Rfc822Header(NString( - self.meta - .headers - .to_vec() - .try_into() - .ok() - .map(IString::Literal), - )) - } - - fn rfc_822_text(&self) -> Result> { - Ok(MessageDataItem::Rfc822Text(NString( - self.content - .as_full()? - .raw_body - .to_vec() - .try_into() - .ok() - .map(IString::Literal), - ))) - } - - fn rfc822(&self) -> Result> { - Ok(MessageDataItem::Rfc822(NString( - self.content - .as_full()? - .raw_part - .to_vec() - .try_into() - .ok() - .map(IString::Literal), - ))) - } - - fn envelope(&self) -> MessageDataItem<'static> { - MessageDataItem::Envelope(message_envelope(self.content.imf().clone())) - } - - fn body(&self) -> Result> { - Ok(MessageDataItem::Body(build_imap_email_struct( - self.content.as_full()?.child.as_ref(), - )?)) - } - - fn body_structure(&self) -> Result> { - Ok(MessageDataItem::Body(build_imap_email_struct( - self.content.as_full()?.child.as_ref(), - )?)) - } - - /// maps to BODY[
]<> and BODY.PEEK[
]<> - /// peek does not implicitly set the \Seen flag - /// eg. BODY[HEADER.FIELDS (DATE FROM)] - /// eg. BODY[]<0.2048> - fn body_ext<'b>( - &self, - section: &Option>, - partial: &Option<(u32, NonZeroU32)>, - peek: &bool, - ) -> Result<(MessageDataItem<'b>, SeenFlag)> { - let mut seen = SeenFlag::DoNothing; - - // Extract message section - let text = get_message_section(self.content.as_anypart()?, section)?; - - let seen_flag = Flag::Seen.to_string(); - if !peek && !self.flags.iter().any(|x| *x == seen_flag) { - // Add \Seen flag - //self.mailbox.add_flags(uuid, &[seen_flag]).await?; - seen = SeenFlag::MustAdd; - } - - // Handle <> which cut the message bytes - let (text, origin) = apply_partial(partial, &text); - - let data = NString(text.to_vec().try_into().ok().map(IString::Literal)); - - return Ok(( - MessageDataItem::BodyExt { - section: section.as_ref().map(|fs| fs.clone()), - origin, - data, - }, - seen, - )); - } - - fn internal_date(&self) -> Result> { - let dt = Utc - .fix() - .timestamp_opt(i64::try_from(self.meta.internaldate / 1000)?, 0) - .earliest() - .ok_or(anyhow!("Unable to parse internal date"))?; - Ok(MessageDataItem::InternalDate(DateTime::unvalidated(dt))) - } - - fn filter<'b>(&self, ap: &AttributesProxy) -> Result<(Body<'static>, SeenFlag)> { - let mut seen = SeenFlag::DoNothing; - let res_attrs = ap - .attrs - .iter() - .map(|attr| match attr { - MessageDataItemName::Uid => Ok(self.uid()), - MessageDataItemName::Flags => Ok(self.flags()), - MessageDataItemName::Rfc822Size => Ok(self.rfc_822_size()), - MessageDataItemName::Rfc822Header => Ok(self.rfc_822_header()), - MessageDataItemName::Rfc822Text => self.rfc_822_text(), - MessageDataItemName::Rfc822 => self.rfc822(), - MessageDataItemName::Envelope => Ok(self.envelope()), - MessageDataItemName::Body => self.body(), - MessageDataItemName::BodyStructure => self.body_structure(), - MessageDataItemName::BodyExt { - section, - partial, - peek, - } => { - let (body, has_seen) = self.body_ext(section, partial, peek)?; - seen = has_seen; - Ok(body) - } - MessageDataItemName::InternalDate => self.internal_date(), - }) - .collect::, _>>()?; - - Ok(( - Body::Data(Data::Fetch { - seq: self.ids.i, - items: res_attrs.try_into()?, - }), - seen, - )) - } -} - -fn apply_partial<'a>( - partial: &'_ Option<(u32, NonZeroU32)>, - text: &'a [u8], -) -> (&'a [u8], Option) { - match partial { - Some((begin, len)) => { - if *begin as usize > text.len() { - (&[][..], Some(*begin)) - } else if (begin + len.get()) as usize >= text.len() { - (&text[*begin as usize..], Some(*begin)) - } else { - ( - &text[*begin as usize..(begin + len.get()) as usize], - Some(*begin), - ) - } - } - None => (&text[..], None), - } -} - -pub struct BodyIdentifier<'a> { - msg_uuid: &'a UniqueIdent, - msg_key: &'a Key, -} - -#[derive(Default)] -pub struct MailSelectionBuilder<'a> { - //attrs: AttributeProxy, - mail_count: usize, - need_body: bool, - mi: &'a [MailIdentifiers], - meta: &'a [MailMeta], - flags: &'a [&'a Vec], - bodies: &'a [Vec], -} - -impl<'a> MailSelectionBuilder<'a> { - fn new(need_body: bool, mail_count: usize) -> Self { - Self { - mail_count, - need_body, - ..MailSelectionBuilder::default() - } - } - - fn with_mail_identifiers(&mut self, mi: &'a [MailIdentifiers]) -> &mut Self { - self.mi = mi; - self - } - - fn with_metadata(&mut self, meta: &'a [MailMeta]) -> &mut Self { - self.meta = meta; - self - } - - fn with_flags(&mut self, flags: &'a [&'a Vec]) -> &mut Self { - self.flags = flags; - self - } - - fn bodies_to_collect(&self) -> Vec { - if !self.need_body { - return vec![]; - } - zip(self.mi, self.meta) - .map(|(mi, meta)| BodyIdentifier { - msg_uuid: &mi.uuid, - msg_key: &meta.message_key, - }) - .collect::>() - } - - fn with_bodies(&mut self, rbodies: &'a [Vec]) -> &mut Self { - self.bodies = rbodies; - self - } - - fn build(&self) -> Result>> { - let mut bodies = vec![]; - - if !self.need_body { - for m in self.meta.iter() { - let (_, hdrs) = - eml_codec::parse_imf(&m.headers).or(Err(anyhow!("Invalid mail headers")))?; - bodies.push(FetchedMail::Partial(hdrs)); - } - } else { - for rb in self.bodies.iter() { - let (_, p) = eml_codec::parse_message(&rb).or(Err(anyhow!("Invalid mail body")))?; - bodies.push(FetchedMail::new_from_message(p)); - } - } - - if self.mi.len() != self.mail_count && self.meta.len() != self.mail_count - || self.flags.len() != self.mail_count - || bodies.len() != self.mail_count - { - return Err(anyhow!("Can't build a mail view selection as parts were not correctly registered into the builder.")); - } - - Ok(zip(self.mi, zip(self.meta, zip(self.flags, bodies))) - .map(|(ids, (meta, (flags, content)))| MailView { - ids, - meta, - flags, - content, - }) - .collect()) - } -} - /// A MailboxView is responsible for giving the client the information /// it needs about a mailbox, such as an initial summary of the mailbox's /// content and continuous updates indicating when the content @@ -497,7 +116,7 @@ impl MailboxView { items: vec![ MessageDataItem::Uid(*uid), MessageDataItem::Flags( - flags.iter().filter_map(|f| string_to_flag(f)).collect(), + flags.iter().filter_map(|f| flags::from_str(f)).collect(), ), ] .try_into()?, @@ -853,7 +472,7 @@ impl MailboxView { .known_state .idx_by_flag .flags() - .filter_map(|f| match string_to_flag(f) { + .filter_map(|f| match flags::from_str(f) { Some(FlagFetch::Flag(fl)) => Some(fl), _ => None, }) @@ -900,456 +519,40 @@ impl MailboxView { } } -fn string_to_flag(f: &str) -> Option> { - match f.chars().next() { - Some('\\') => match f { - "\\Seen" => Some(FlagFetch::Flag(Flag::Seen)), - "\\Answered" => Some(FlagFetch::Flag(Flag::Answered)), - "\\Flagged" => Some(FlagFetch::Flag(Flag::Flagged)), - "\\Deleted" => Some(FlagFetch::Flag(Flag::Deleted)), - "\\Draft" => Some(FlagFetch::Flag(Flag::Draft)), - "\\Recent" => Some(FlagFetch::Recent), - _ => match Atom::try_from(f.strip_prefix('\\').unwrap().to_string()) { - Err(_) => { - tracing::error!(flag=%f, "Unable to encode flag as IMAP atom"); - None - } - Ok(a) => Some(FlagFetch::Flag(Flag::system(a))), - }, - }, - Some(_) => match Atom::try_from(f.to_string()) { - Err(_) => { - tracing::error!(flag=%f, "Unable to encode flag as IMAP atom"); - None - } - Ok(a) => Some(FlagFetch::Flag(Flag::keyword(a))), - }, - None => None, - } -} - -/// Envelope rules are defined in RFC 3501, section 7.4.2 -/// https://datatracker.ietf.org/doc/html/rfc3501#section-7.4.2 -/// -/// Some important notes: -/// -/// If the Sender or Reply-To lines are absent in the [RFC-2822] -/// header, or are present but empty, the server sets the -/// corresponding member of the envelope to be the same value as -/// the from member (the client is not expected to know to do -/// this). Note: [RFC-2822] requires that all messages have a valid -/// From header. Therefore, the from, sender, and reply-to -/// members in the envelope can not be NIL. -/// -/// If the Date, Subject, In-Reply-To, and Message-ID header lines -/// are absent in the [RFC-2822] header, the corresponding member -/// of the envelope is NIL; if these header lines are present but -/// empty the corresponding member of the envelope is the empty -/// string. - -//@FIXME return an error if the envelope is invalid instead of panicking -//@FIXME some fields must be defaulted if there are not set. -fn message_envelope(msg: &imf::Imf) -> Envelope<'static> { - let from = msg.from.iter().map(convert_mbx).collect::>(); - - Envelope { - date: NString( - msg.date - .as_ref() - .map(|d| IString::try_from(d.to_rfc3339()).unwrap()), - ), - subject: NString( - msg.subject - .as_ref() - .map(|d| IString::try_from(d.to_string()).unwrap()), - ), - sender: msg - .sender - .as_ref() - .map(|v| vec![convert_mbx(v)]) - .unwrap_or(from.clone()), - reply_to: if msg.reply_to.is_empty() { - from.clone() - } else { - convert_addresses(&msg.reply_to) - }, - from, - to: convert_addresses(&msg.to), - cc: convert_addresses(&msg.cc), - bcc: convert_addresses(&msg.bcc), - in_reply_to: NString( - msg.in_reply_to - .iter() - .next() - .map(|d| IString::try_from(d.to_string()).unwrap()), - ), - message_id: NString( - msg.msg_id - .as_ref() - .map(|d| IString::try_from(d.to_string()).unwrap()), - ), - } -} - -fn convert_addresses(addrlist: &Vec) -> Vec> { - let mut acc = vec![]; - for item in addrlist { - match item { - imf::address::AddressRef::Single(a) => acc.push(convert_mbx(a)), - imf::address::AddressRef::Many(l) => acc.extend(l.participants.iter().map(convert_mbx)), - } - } - return acc; -} - -fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address<'static> { - Address { - name: NString( - addr.name - .as_ref() - .map(|x| IString::try_from(x.to_string()).unwrap()), - ), - // SMTP at-domain-list (source route) seems obsolete since at least 1991 - // https://www.mhonarc.org/archive/html/ietf-822/1991-06/msg00060.html - adl: NString(None), - mailbox: NString(Some( - IString::try_from(addr.addrspec.local_part.to_string()).unwrap(), - )), - host: NString(Some( - IString::try_from(addr.addrspec.domain.to_string()).unwrap(), - )), - } -} - -/* ---CAPTURE-- -b fetch 29878:29879 (BODY) -* 29878 FETCH (BODY (("text" "plain" ("charset" "utf-8") NIL NIL "quoted-printable" 3264 82)("text" "html" ("charset" "utf-8") NIL NIL "quoted-printable" 31834 643) "alternative")) -* 29879 FETCH (BODY ("text" "html" ("charset" "us-ascii") NIL NIL "7bit" 4107 131)) - ^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^ ^^^^^^ ^^^^ ^^^ - | | | | | | number of lines - | | | | | size - | | | | content transfer encoding - | | | description - | | id - | parameter list -b OK Fetch completed (0.001 + 0.000 secs). -*/ - -fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result> { - match part { - AnyPart::Mult(x) => { - let itype = &x.mime.interpreted_type; - let subtype = IString::try_from(itype.subtype.to_string()) - .unwrap_or(unchecked_istring("alternative")); - - let inner_bodies = x - .children - .iter() - .filter_map(|inner| build_imap_email_struct(&inner).ok()) - .collect::>(); - NonEmptyVec::validate(&inner_bodies)?; - let bodies = NonEmptyVec::unvalidated(inner_bodies); - - Ok(BodyStructure::Multi { - bodies, - subtype, - extension_data: None, - /*Some(MultipartExtensionData { - parameter_list: vec![], - disposition: None, - language: None, - location: None, - extension: vec![], - })*/ - }) - } - AnyPart::Txt(x) => { - let mut basic = basic_fields(&x.mime.fields, x.body.len())?; - - // Get the interpreted content type, set it - let itype = match &x.mime.interpreted_type { - Deductible::Inferred(v) | Deductible::Explicit(v) => v, - }; - let subtype = - IString::try_from(itype.subtype.to_string()).unwrap_or(unchecked_istring("plain")); - - // Add charset to the list of parameters if we know it has been inferred as it will be - // missing from the parsed content. - if let Deductible::Inferred(charset) = &itype.charset { - basic.parameter_list.push(( - unchecked_istring("charset"), - IString::try_from(charset.to_string()).unwrap_or(unchecked_istring("us-ascii")), - )); - } - - Ok(BodyStructure::Single { - body: FetchBody { - basic, - specific: SpecificFields::Text { - subtype, - number_of_lines: nol(x.body), - }, - }, - extension_data: None, - }) - } - AnyPart::Bin(x) => { - let basic = basic_fields(&x.mime.fields, x.body.len())?; - - let default = mime::r#type::NaiveType { - main: &b"application"[..], - sub: &b"octet-stream"[..], - params: vec![], - }; - let ct = x.mime.fields.ctype.as_ref().unwrap_or(&default); - - let r#type = - IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err( - anyhow!("Unable to build IString from given Content-Type type given"), - ))?; - - let subtype = - IString::try_from(String::from_utf8_lossy(ct.sub).to_string()).or(Err(anyhow!( - "Unable to build IString from given Content-Type subtype given" - )))?; - - Ok(BodyStructure::Single { - body: FetchBody { - basic, - specific: SpecificFields::Basic { r#type, subtype }, - }, - extension_data: None, - }) - } - AnyPart::Msg(x) => { - let basic = basic_fields(&x.mime.fields, x.raw_part.len())?; - - Ok(BodyStructure::Single { - body: FetchBody { - basic, - specific: SpecificFields::Message { - envelope: Box::new(message_envelope(&x.imf)), - body_structure: Box::new(build_imap_email_struct(x.child.as_ref())?), - number_of_lines: nol(x.raw_part), - }, - }, - extension_data: None, - }) - } - } -} - -fn nol(input: &[u8]) -> u32 { - input - .iter() - .filter(|x| **x == b'\n') - .count() - .try_into() - .unwrap_or(0) -} -/// s is set to static to ensure that only compile time values -/// checked by developpers are passed. -fn unchecked_istring(s: &'static str) -> IString { - IString::try_from(s).expect("this value is expected to be a valid imap-codec::IString") -} - -fn basic_fields(m: &mime::NaiveMIME, sz: usize) -> Result> { - let parameter_list = m - .ctype - .as_ref() - .map(|x| { - x.params - .iter() - .map(|p| { - ( - IString::try_from(String::from_utf8_lossy(p.name).to_string()), - IString::try_from(p.value.to_string()), - ) - }) - .filter(|(k, v)| k.is_ok() && v.is_ok()) - .map(|(k, v)| (k.unwrap(), v.unwrap())) - .collect() - }) - .unwrap_or(vec![]); - - Ok(BasicFields { - parameter_list, - id: NString( - m.id.as_ref() - .and_then(|ci| IString::try_from(ci.to_string()).ok()), - ), - description: NString( - m.description - .as_ref() - .and_then(|cd| IString::try_from(cd.to_string()).ok()), - ), - content_transfer_encoding: match m.transfer_encoding { - mime::mechanism::Mechanism::_8Bit => unchecked_istring("8bit"), - mime::mechanism::Mechanism::Binary => unchecked_istring("binary"), - mime::mechanism::Mechanism::QuotedPrintable => unchecked_istring("quoted-printable"), - mime::mechanism::Mechanism::Base64 => unchecked_istring("base64"), - _ => unchecked_istring("7bit"), - }, - // @FIXME we can't compute the size of the message currently... - size: u32::try_from(sz)?, - }) -} - -/// Extract message section for section identifier passed by the FETCH BODY[
]<> -/// request -/// -/// Example of message sections: -/// -/// ``` -/// HEADER ([RFC-2822] header of the message) -/// TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED -/// 1 TEXT/PLAIN -/// 2 APPLICATION/OCTET-STREAM -/// 3 MESSAGE/RFC822 -/// 3.HEADER ([RFC-2822] header of the message) -/// 3.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED -/// 3.1 TEXT/PLAIN -/// 3.2 APPLICATION/OCTET-STREAM -/// 4 MULTIPART/MIXED -/// 4.1 IMAGE/GIF -/// 4.1.MIME ([MIME-IMB] header for the IMAGE/GIF) -/// 4.2 MESSAGE/RFC822 -/// 4.2.HEADER ([RFC-2822] header of the message) -/// 4.2.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED -/// 4.2.1 TEXT/PLAIN -/// 4.2.2 MULTIPART/ALTERNATIVE -/// 4.2.2.1 TEXT/PLAIN -/// 4.2.2.2 TEXT/RICHTEXT -/// ``` -fn get_message_section<'a>( - parsed: &'a AnyPart<'a>, - section: &Option, -) -> Result> { - let msg = parsed - .as_message() - .ok_or(anyhow!("Part must be a message"))?; - match section { - Some(FetchSection::Text(None)) => Ok(msg.raw_body.into()), - Some(FetchSection::Text(Some(part))) => map_subpart(parsed, part.0.as_ref(), |part_msg| { - Ok(part_msg - .as_message() - .ok_or(Error::msg( - "Not a message/rfc822 part while expected by request (TEXT)", - ))? - .raw_body - .into()) - }), - Some(FetchSection::Header(part)) => map_subpart( - parsed, - part.as_ref().map(|p| p.0.as_ref()).unwrap_or(&[]), - |part_msg| { - Ok(part_msg - .as_message() - .ok_or(Error::msg( - "Not a message/rfc822 part while expected by request (HEADER)", - ))? - .raw_headers - .into()) - }, - ), - Some( - FetchSection::HeaderFields(part, fields) | FetchSection::HeaderFieldsNot(part, fields), - ) => { - let invert = matches!(section, Some(FetchSection::HeaderFieldsNot(_, _))); - let fields = fields - .as_ref() - .iter() - .map(|x| match x { - AString::Atom(a) => a.inner().as_bytes(), - AString::String(IString::Literal(l)) => l.as_ref(), - AString::String(IString::Quoted(q)) => q.inner().as_bytes(), - }) - .collect::>(); - - map_subpart( - parsed, - part.as_ref().map(|p| p.0.as_ref()).unwrap_or(&[]), - |part_msg| { - let mut ret = vec![]; - let mime = match &part_msg { - AnyPart::Msg(msg) => msg.child.mime(), - other => other.mime(), - }; - for f in mime.kv.iter() { - let (k, v) = match f { - header::Field::Good(header::Kv2(k, v)) => (k, v), - _ => continue, - }; - if fields.as_slice().iter().any(|x| (x == k) ^ invert) { - ret.extend(*k); - ret.extend(b": "); - ret.extend(*v); - ret.extend(b"\r\n"); - } - } - ret.extend(b"\r\n"); - Ok(ret.into()) - }, - ) - } - Some(FetchSection::Part(part)) => map_subpart(parsed, part.0.as_ref(), |part| { - let bytes = match &part { - AnyPart::Txt(p) => p.body, - AnyPart::Bin(p) => p.body, - AnyPart::Msg(p) => p.raw_part, - AnyPart::Mult(_) => bail!("Multipart part has no body"), - }; - Ok(bytes.to_vec().into()) - }), - Some(FetchSection::Mime(part)) => map_subpart(parsed, part.0.as_ref(), |part| { - let bytes = match &part { - AnyPart::Txt(p) => p.mime.fields.raw, - AnyPart::Bin(p) => p.mime.fields.raw, - AnyPart::Msg(p) => p.child.mime().raw, - AnyPart::Mult(p) => p.mime.fields.raw, - }; - Ok(bytes.to_vec().into()) - }), - None => Ok(msg.raw_part.into()), - } +pub struct MailIdentifiers { + pub i: NonZeroU32, + pub uid: ImapUid, + pub uuid: UniqueIdent, } +pub struct MailIdentifiersList(Vec); -/// Fetch a MIME SubPart -/// -/// eg. FETCH BODY[4.2.2.1] -> [4, 2, 2, 1] -fn map_subpart<'a, F, R>(part: &AnyPart<'a>, path: &[NonZeroU32], f: F) -> Result -where - F: FnOnce(&AnyPart<'a>) -> Result, -{ - if path.is_empty() { - f(part) - } else { - match part { - AnyPart::Mult(x) => map_subpart( - x.children - .get(path[0].get() as usize - 1) - .ok_or(anyhow!("Unable to resolve subpath {:?}, current multipart has only {} elements", path, x.children.len()))?, - &path[1..], - f), - AnyPart::Msg(x) => map_subpart(x.child.as_ref(), path, f), - _ => bail!("You tried to access a subpart on an atomic part (text or binary). Unresolved subpath {:?}", path), - } +impl MailIdentifiersList { + fn uuids(&self) -> Vec { + self.0.iter().map(|mi| mi.uuid).collect() } } #[cfg(test)] mod tests { use super::*; - use crate::cryptoblob; - use crate::mail::unique_ident; + use imap_codec::imap_types::fetch::{ + MacroOrMessageDataItemNames, MessageDataItemName, + }; use imap_codec::encode::Encoder; + use imap_codec::imap_types::core::NonEmptyVec; use imap_codec::imap_types::fetch::Section; use imap_codec::imap_types::response::Response; use imap_codec::ResponseCodec; use std::fs; + + use crate::cryptoblob; + use crate::mail::unique_ident; + use crate::mail::mailbox::MailMeta; + use crate::imap::mail_view::{MailView, FetchedMail}; + use crate::imap::mime_view; + #[test] fn mailview_body_ext() -> Result<()> { let ap = AttributesProxy::new( @@ -1452,7 +655,7 @@ mod tests { let test_repr = Response::Data(Data::Fetch { seq: NonZeroU32::new(1).unwrap(), - items: NonEmptyVec::from(MessageDataItem::Body(build_imap_email_struct( + items: NonEmptyVec::from(MessageDataItem::Body(mime_view::bodystructure( &message.child, )?)), }); @@ -1470,3 +673,4 @@ mod tests { Ok(()) } } + diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs index ba324a2..e69de29 100644 --- a/src/imap/mime_view.rs +++ b/src/imap/mime_view.rs @@ -1,292 +0,0 @@ -use std::borrow::Cow; -use std::num::NonZeroU32; -use std::collections::HashSet; - -use anyhow::{anyhow, bail, Result}; - -use imap_codec::imap_types::core::{AString, IString, NonEmptyVec}; -use imap_codec::imap_types::fetch::{ - Section as FetchSection, Part as FetchPart -}; - -use eml_codec::{ - header, part::AnyPart, -}; - - -pub enum BodySection<'a> { - Full(Cow<'a, [u8]>), - Slice { - body: Cow<'a, [u8]>, - origin_octet: u32, - }, -} - -/// Logic for BODY[
]<> -/// Works in 3 times: -/// 1. Find the section (RootMime::subset) -/// 2. Apply the extraction logic (SelectedMime::extract), like TEXT, HEADERS, etc. -/// 3. Keep only the given subset provided by partial -pub fn body_ext<'a>( - part: &'a AnyPart<'a>, - section: &Option, - partial: &Option<(u32, NonZeroU32)> -) -> Result> { - let root_mime = RootMime(part); - let (extractor, path) = SubsettedSection::from(section); - let selected_mime = root_mime.subset(path); - unimplemented!(); - } - -/// RootMime -/// -/// Example of message sections: -/// -/// ``` -/// HEADER ([RFC-2822] header of the message) -/// TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED -/// 1 TEXT/PLAIN -/// 2 APPLICATION/OCTET-STREAM -/// 3 MESSAGE/RFC822 -/// 3.HEADER ([RFC-2822] header of the message) -/// 3.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED -/// 3.1 TEXT/PLAIN -/// 3.2 APPLICATION/OCTET-STREAM -/// 4 MULTIPART/MIXED -/// 4.1 IMAGE/GIF -/// 4.1.MIME ([MIME-IMB] header for the IMAGE/GIF) -/// 4.2 MESSAGE/RFC822 -/// 4.2.HEADER ([RFC-2822] header of the message) -/// 4.2.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED -/// 4.2.1 TEXT/PLAIN -/// 4.2.2 MULTIPART/ALTERNATIVE -/// 4.2.2.1 TEXT/PLAIN -/// 4.2.2.2 TEXT/RICHTEXT -/// ``` - - -struct RootMime<'a>(&'a AnyPart<'a>); -impl<'a> RootMime<'a> { - - - /// A MIME object is a tree of elements. - /// The path indicates which element must be picked. - /// This function returns the picked element as the new view - fn subset(&self, path: Option<&FetchPart>) -> Result { - match path { - None => Ok(SelectedMime(self.0)), - Some(v) => self.rec_subset(v.0.as_ref()), - } - } - - fn rec_subset(self, path: &[NonZeroU32]) -> Result { - if path.is_empty() { - Ok(SelectedMime(self.0)) - } else { - match self.0 { - AnyPart::Mult(x) => { - let next = Self(x.children - .get(path[0].get() as usize - 1) - .ok_or(anyhow!("Unable to resolve subpath {:?}, current multipart has only {} elements", path, x.children.len()))?); - next.rec_subset(&path[1..]) - }, - AnyPart::Msg(x) => { - let next = Self(x.child.as_ref()); - next.rec_subset(path) - }, - _ => bail!("You tried to access a subpart on an atomic part (text or binary). Unresolved subpath {:?}", path), - } - } - } -} - -//---------------------------------------------------------- - -/// A FetchSection must be handled in 2 times: -/// - First we must extract the MIME part -/// - Then we must process it as desired -/// The given struct mixes both work, so -/// we separate this work here. -enum SubsettedSection<'a> { - Part, - Header, - HeaderFields(&'a NonEmptyVec>), - HeaderFieldsNot(&'a NonEmptyVec>), - Text, - Mime, -} -impl<'a> SubsettedSection<'a> { - fn from(section: &'a Option) -> (Self, Option<&'a FetchPart>) { - match section { - Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), - Some(FetchSection::Header(maybe_part)) => (Self::Header, maybe_part.as_ref()), - Some(FetchSection::HeaderFields(maybe_part, fields)) => (Self::HeaderFields(fields), maybe_part.as_ref()), - Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => (Self::HeaderFieldsNot(fields), maybe_part.as_ref()), - Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), - Some(FetchSection::Mime(part)) => (Self::Mime, Some(part)), - Some(FetchSection::Part(part)) => (Self::Part, Some(part)), - None => (Self::Part, None), - } - } -} - -struct SelectedMime<'a>(&'a AnyPart<'a>); -impl<'a> SelectedMime<'a> { - /// The subsetted fetch section basically tells us the - /// extraction logic to apply on our selected MIME. - /// This function acts as a router for these logic. - fn extract(&self, extractor: &SubsettedSection) -> Result> { - match extractor { - SubsettedSection::Text => self.text(), - SubsettedSection::Header => self.header(), - SubsettedSection::HeaderFields(fields) => self.header_fields(fields, false), - SubsettedSection::HeaderFieldsNot(fields) => self.header_fields(fields, true), - SubsettedSection::Part => self.part(), - SubsettedSection::Mime => self.mime(), - } - } - - fn mime(&self) -> Result> { - let bytes = match &self.0 { - AnyPart::Txt(p) => p.mime.fields.raw, - AnyPart::Bin(p) => p.mime.fields.raw, - AnyPart::Msg(p) => p.child.mime().raw, - AnyPart::Mult(p) => p.mime.fields.raw, - }; - Ok(ExtractedFull(bytes.to_vec().into())) - } - - fn part(&self) -> Result> { - let bytes = match &self.0 { - AnyPart::Txt(p) => p.body, - AnyPart::Bin(p) => p.body, - AnyPart::Msg(p) => p.raw_part, - AnyPart::Mult(_) => bail!("Multipart part has no body"), - }; - Ok(ExtractedFull(bytes.to_vec().into())) - } - - /// The [...] HEADER.FIELDS, and HEADER.FIELDS.NOT part - /// specifiers refer to the [RFC-2822] header of the message or of - /// an encapsulated [MIME-IMT] MESSAGE/RFC822 message. - /// HEADER.FIELDS and HEADER.FIELDS.NOT are followed by a list of - /// field-name (as defined in [RFC-2822]) names, and return a - /// subset of the header. The subset returned by HEADER.FIELDS - /// contains only those header fields with a field-name that - /// matches one of the names in the list; similarly, the subset - /// returned by HEADER.FIELDS.NOT contains only the header fields - /// with a non-matching field-name. The field-matching is - /// case-insensitive but otherwise exact. - fn header_fields(&self, fields: &'a NonEmptyVec>, invert: bool) -> Result> { - // Build a lowercase ascii hashset with the fields to fetch - let index = fields - .as_ref() - .iter() - .map(|x| match x { - AString::Atom(a) => a.inner().as_bytes(), - AString::String(IString::Literal(l)) => l.as_ref(), - AString::String(IString::Quoted(q)) => q.inner().as_bytes(), - }.to_ascii_lowercase()) - .collect::>(); - - // Extract MIME headers - let mime = match &self.0 { - AnyPart::Msg(msg) => msg.child.mime(), - other => other.mime(), - }; - - // Filter our MIME headers based on the field index - // 1. Keep only the correctly formatted headers - // 2. Keep only based on the index presence or absence - // 3. Reduce as a byte vector - let buffer = mime.kv.iter() - .filter_map(|field| match field { - header::Field::Good(header::Kv2(k, v)) => Some((k, v)), - _ => None, - }) - .filter(|(k, _)| index.contains(&k.to_ascii_lowercase()) ^ invert) - .fold(vec![], |mut acc, (k, v)| { - acc.extend(*k); - acc.extend(b": "); - acc.extend(*v); - acc.extend(b"\r\n"); - acc - }); - - Ok(ExtractedFull(buffer.into())) - } - - /// The HEADER [...] part specifiers refer to the [RFC-2822] header of the message or of - /// an encapsulated [MIME-IMT] MESSAGE/RFC822 message. - /// ```raw - /// HEADER ([RFC-2822] header of the message) - /// ``` - fn header(&self) -> Result> { - let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; - Ok(ExtractedFull(msg.raw_headers.into())) - } - - /// The TEXT part specifier refers to the text body of the message, omitting the [RFC-2822] header. - fn text(&self) -> Result> { - let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; - Ok(ExtractedFull(msg.raw_body.into())) - } -} - -// --------------------------- - -struct ExtractedFull<'a>(Cow<'a, [u8]>); -impl<'a> ExtractedFull<'a> { - /// It is possible to fetch a substring of the designated text. - /// This is done by appending an open angle bracket ("<"), the - /// octet position of the first desired octet, a period, the - /// maximum number of octets desired, and a close angle bracket - /// (">") to the part specifier. If the starting octet is beyond - /// the end of the text, an empty string is returned. - /// - /// Any partial fetch that attempts to read beyond the end of the - /// text is truncated as appropriate. A partial fetch that starts - /// at octet 0 is returned as a partial fetch, even if this - /// truncation happened. - /// - /// Note: This means that BODY[]<0.2048> of a 1500-octet message - /// will return BODY[]<0> with a literal of size 1500, not - /// BODY[]. - /// - /// Note: A substring fetch of a HEADER.FIELDS or - /// HEADER.FIELDS.NOT part specifier is calculated after - /// subsetting the header. - fn to_body_section(&self, partial: &'_ Option<(u32, NonZeroU32)>) -> BodySection<'a> { - match partial { - Some((begin, len)) => self.partialize(*begin, *len), - None => BodySection::Full(self.0), - } - } - - fn partialize(&self, begin: u32, len: NonZeroU32) -> BodySection<'a> { - // Asked range is starting after the end of the content, - // returning an empty buffer - if begin as usize > self.0.len() { - return BodySection::Slice { - body: Cow::Borrowed(&[][..]), - origin_octet: begin, - } - } - - // Asked range is ending after the end of the content, - // slice only the beginning of the buffer - if (begin + len.get()) as usize >= self.0.len() { - return BodySection::Slice { - body: Cow::Borrowed(&self.0[begin as usize..]), - origin_octet: begin, - } - } - - // Range is included inside the considered content, - // this is the "happy case" - BodySection::Slice { - body: Cow::Borrowed(&self.0[begin as usize..(begin + len.get()) as usize]), - origin_octet: begin, - } - } -} diff --git a/src/imap/mod.rs b/src/imap/mod.rs index aac1fd3..d3cf966 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -1,7 +1,13 @@ mod capability; mod command; mod flow; +mod attributes; +mod mail_view; mod mailbox_view; +mod mime_view; +mod imf_view; +mod selectors; +mod flags; mod response; mod session; diff --git a/src/imap/selectors.rs b/src/imap/selectors.rs new file mode 100644 index 0000000..6b5372a --- /dev/null +++ b/src/imap/selectors.rs @@ -0,0 +1,103 @@ +use std::iter::zip; + +use anyhow::{anyhow, Result}; + + +use crate::cryptoblob::Key; +use crate::imap::mail_view::{MailView, FetchedMail}; +use crate::imap::mailbox_view::MailIdentifiers; +use crate::mail::mailbox::MailMeta; +use crate::mail::unique_ident::UniqueIdent; + +pub struct BodyIdentifier<'a> { + pub msg_uuid: &'a UniqueIdent, + pub msg_key: &'a Key, +} + + +#[derive(Default)] +pub struct MailSelectionBuilder<'a> { + //attrs: AttributeProxy, + mail_count: usize, + need_body: bool, + mi: &'a [MailIdentifiers], + meta: &'a [MailMeta], + flags: &'a [&'a Vec], + bodies: &'a [Vec], +} + +impl<'a> MailSelectionBuilder<'a> { + pub fn new(need_body: bool, mail_count: usize) -> Self { + Self { + mail_count, + need_body, + ..MailSelectionBuilder::default() + } + } + + pub fn with_mail_identifiers(&mut self, mi: &'a [MailIdentifiers]) -> &mut Self { + self.mi = mi; + self + } + + pub fn with_metadata(&mut self, meta: &'a [MailMeta]) -> &mut Self { + self.meta = meta; + self + } + + pub fn with_flags(&mut self, flags: &'a [&'a Vec]) -> &mut Self { + self.flags = flags; + self + } + + pub fn bodies_to_collect(&self) -> Vec { + if !self.need_body { + return vec![]; + } + zip(self.mi, self.meta) + .map(|(mi, meta)| BodyIdentifier { + msg_uuid: &mi.uuid, + msg_key: &meta.message_key, + }) + .collect::>() + } + + pub fn with_bodies(&mut self, rbodies: &'a [Vec]) -> &mut Self { + self.bodies = rbodies; + self + } + + pub fn build(&self) -> Result>> { + let mut bodies = vec![]; + + if !self.need_body { + for m in self.meta.iter() { + let (_, hdrs) = + eml_codec::parse_imf(&m.headers).or(Err(anyhow!("Invalid mail headers")))?; + bodies.push(FetchedMail::Partial(hdrs)); + } + } else { + for rb in self.bodies.iter() { + let (_, p) = eml_codec::parse_message(&rb).or(Err(anyhow!("Invalid mail body")))?; + bodies.push(FetchedMail::new_from_message(p)); + } + } + + if self.mi.len() != self.mail_count && self.meta.len() != self.mail_count + || self.flags.len() != self.mail_count + || bodies.len() != self.mail_count + { + return Err(anyhow!("Can't build a mail view selection as parts were not correctly registered into the builder.")); + } + + Ok(zip(self.mi, zip(self.meta, zip(self.flags, bodies))) + .map(|(ids, (meta, (flags, content)))| MailView { + ids, + meta, + flags, + content, + }) + .collect()) + } +} + -- cgit v1.2.3 From e25576e363baf24153c543456a445ea548b9f599 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 09:26:54 +0100 Subject: bodyext --- src/imap/mime_view.rs | 331 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 331 insertions(+) diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs index e69de29..74bb6ad 100644 --- a/src/imap/mime_view.rs +++ b/src/imap/mime_view.rs @@ -0,0 +1,331 @@ +use std::borrow::Cow; +use std::num::NonZeroU32; +use std::collections::HashSet; + +use anyhow::{anyhow, bail, Result}; + +use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; +use imap_codec::imap_types::core::{AString, IString, NonEmptyVec}; +use imap_codec::imap_types::fetch::{ + Section as FetchSection, Part as FetchPart +}; + +use eml_codec::{ + header, part::AnyPart, part::composite, part::discrete, +}; + + +pub enum BodySection<'a> { + Full(Cow<'a, [u8]>), + Slice { + body: Cow<'a, [u8]>, + origin_octet: u32, + }, +} + +/// Logic for BODY[
]<> +/// Works in 3 times: +/// 1. Find the section (RootMime::subset) +/// 2. Apply the extraction logic (SelectedMime::extract), like TEXT, HEADERS, etc. +/// 3. Keep only the given subset provided by partial +/// +/// Example of message sections: +/// +/// ``` +/// HEADER ([RFC-2822] header of the message) +/// TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED +/// 1 TEXT/PLAIN +/// 2 APPLICATION/OCTET-STREAM +/// 3 MESSAGE/RFC822 +/// 3.HEADER ([RFC-2822] header of the message) +/// 3.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED +/// 3.1 TEXT/PLAIN +/// 3.2 APPLICATION/OCTET-STREAM +/// 4 MULTIPART/MIXED +/// 4.1 IMAGE/GIF +/// 4.1.MIME ([MIME-IMB] header for the IMAGE/GIF) +/// 4.2 MESSAGE/RFC822 +/// 4.2.HEADER ([RFC-2822] header of the message) +/// 4.2.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED +/// 4.2.1 TEXT/PLAIN +/// 4.2.2 MULTIPART/ALTERNATIVE +/// 4.2.2.1 TEXT/PLAIN +/// 4.2.2.2 TEXT/RICHTEXT +/// ``` +pub fn body_ext<'a>( + part: &'a AnyPart<'a>, + section: &'a Option>, + partial: &'a Option<(u32, NonZeroU32)> +) -> Result> { + let root_mime = NodeMime(part); + let (extractor, path) = SubsettedSection::from(section); + let selected_mime = root_mime.subset(path)?; + let extracted_full = selected_mime.extract(&extractor)?; + Ok(extracted_full.to_body_section(partial)) +} + +/// Logic for BODY and BODYSTRUCTURE +/// +/// ```raw +/// b fetch 29878:29879 (BODY) +/// * 29878 FETCH (BODY (("text" "plain" ("charset" "utf-8") NIL NIL "quoted-printable" 3264 82)("text" "html" ("charset" "utf-8") NIL NIL "quoted-printable" 31834 643) "alternative")) +/// * 29879 FETCH (BODY ("text" "html" ("charset" "us-ascii") NIL NIL "7bit" 4107 131)) +/// ^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^ ^^^^^^ ^^^^ ^^^ +/// | | | | | | number of lines +/// | | | | | size +/// | | | | content transfer encoding +/// | | | description +/// | | id +/// | parameter list +/// b OK Fetch completed (0.001 + 0.000 secs). +/// ``` +pub fn bodystructure(part: &AnyPart) -> Result> { + unimplemented!(); +} + +/// NodeMime +/// + + + +struct NodeMime<'a>(&'a AnyPart<'a>); +impl<'a> NodeMime<'a> { + /// A MIME object is a tree of elements. + /// The path indicates which element must be picked. + /// This function returns the picked element as the new view + fn subset(self, path: Option<&'a FetchPart>) -> Result> { + match path { + None => Ok(SelectedMime(self.0)), + Some(v) => self.rec_subset(v.0.as_ref()), + } + } + + fn rec_subset(self, path: &'a [NonZeroU32]) -> Result { + if path.is_empty() { + Ok(SelectedMime(self.0)) + } else { + match self.0 { + AnyPart::Mult(x) => { + let next = Self(x.children + .get(path[0].get() as usize - 1) + .ok_or(anyhow!("Unable to resolve subpath {:?}, current multipart has only {} elements", path, x.children.len()))?); + next.rec_subset(&path[1..]) + }, + AnyPart::Msg(x) => { + let next = Self(x.child.as_ref()); + next.rec_subset(path) + }, + _ => bail!("You tried to access a subpart on an atomic part (text or binary). Unresolved subpath {:?}", path), + } + } + } +} + +//---------------------------------------------------------- + +/// A FetchSection must be handled in 2 times: +/// - First we must extract the MIME part +/// - Then we must process it as desired +/// The given struct mixes both work, so +/// we separate this work here. +enum SubsettedSection<'a> { + Part, + Header, + HeaderFields(&'a NonEmptyVec>), + HeaderFieldsNot(&'a NonEmptyVec>), + Text, + Mime, +} +impl<'a> SubsettedSection<'a> { + fn from(section: &'a Option) -> (Self, Option<&'a FetchPart>) { + match section { + Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), + Some(FetchSection::Header(maybe_part)) => (Self::Header, maybe_part.as_ref()), + Some(FetchSection::HeaderFields(maybe_part, fields)) => (Self::HeaderFields(fields), maybe_part.as_ref()), + Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => (Self::HeaderFieldsNot(fields), maybe_part.as_ref()), + Some(FetchSection::Mime(part)) => (Self::Mime, Some(part)), + Some(FetchSection::Part(part)) => (Self::Part, Some(part)), + None => (Self::Part, None), + } + } +} + +struct SelectedMime<'a>(&'a AnyPart<'a>); +impl<'a> SelectedMime<'a> { + /// The subsetted fetch section basically tells us the + /// extraction logic to apply on our selected MIME. + /// This function acts as a router for these logic. + fn extract(&self, extractor: &SubsettedSection<'a>) -> Result> { + match extractor { + SubsettedSection::Text => self.text(), + SubsettedSection::Header => self.header(), + SubsettedSection::HeaderFields(fields) => self.header_fields(fields, false), + SubsettedSection::HeaderFieldsNot(fields) => self.header_fields(fields, true), + SubsettedSection::Part => self.part(), + SubsettedSection::Mime => self.mime(), + } + } + + fn mime(&self) -> Result> { + let bytes = match &self.0 { + AnyPart::Txt(p) => p.mime.fields.raw, + AnyPart::Bin(p) => p.mime.fields.raw, + AnyPart::Msg(p) => p.child.mime().raw, + AnyPart::Mult(p) => p.mime.fields.raw, + }; + Ok(ExtractedFull(bytes.into())) + } + + fn part(&self) -> Result> { + let bytes = match &self.0 { + AnyPart::Txt(p) => p.body, + AnyPart::Bin(p) => p.body, + AnyPart::Msg(p) => p.raw_part, + AnyPart::Mult(_) => bail!("Multipart part has no body"), + }; + Ok(ExtractedFull(bytes.to_vec().into())) + } + + /// The [...] HEADER.FIELDS, and HEADER.FIELDS.NOT part + /// specifiers refer to the [RFC-2822] header of the message or of + /// an encapsulated [MIME-IMT] MESSAGE/RFC822 message. + /// HEADER.FIELDS and HEADER.FIELDS.NOT are followed by a list of + /// field-name (as defined in [RFC-2822]) names, and return a + /// subset of the header. The subset returned by HEADER.FIELDS + /// contains only those header fields with a field-name that + /// matches one of the names in the list; similarly, the subset + /// returned by HEADER.FIELDS.NOT contains only the header fields + /// with a non-matching field-name. The field-matching is + /// case-insensitive but otherwise exact. + fn header_fields(&self, fields: &'a NonEmptyVec>, invert: bool) -> Result> { + // Build a lowercase ascii hashset with the fields to fetch + let index = fields + .as_ref() + .iter() + .map(|x| match x { + AString::Atom(a) => a.inner().as_bytes(), + AString::String(IString::Literal(l)) => l.as_ref(), + AString::String(IString::Quoted(q)) => q.inner().as_bytes(), + }.to_ascii_lowercase()) + .collect::>(); + + // Extract MIME headers + let mime = match &self.0 { + AnyPart::Msg(msg) => msg.child.mime(), + other => other.mime(), + }; + + // Filter our MIME headers based on the field index + // 1. Keep only the correctly formatted headers + // 2. Keep only based on the index presence or absence + // 3. Reduce as a byte vector + let buffer = mime.kv.iter() + .filter_map(|field| match field { + header::Field::Good(header::Kv2(k, v)) => Some((k, v)), + _ => None, + }) + .filter(|(k, _)| index.contains(&k.to_ascii_lowercase()) ^ invert) + .fold(vec![], |mut acc, (k, v)| { + acc.extend(*k); + acc.extend(b": "); + acc.extend(*v); + acc.extend(b"\r\n"); + acc + }); + + Ok(ExtractedFull(buffer.into())) + } + + /// The HEADER [...] part specifiers refer to the [RFC-2822] header of the message or of + /// an encapsulated [MIME-IMT] MESSAGE/RFC822 message. + /// ```raw + /// HEADER ([RFC-2822] header of the message) + /// ``` + fn header(&self) -> Result> { + let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + Ok(ExtractedFull(msg.raw_headers.into())) + } + + /// The TEXT part specifier refers to the text body of the message, omitting the [RFC-2822] header. + fn text(&self) -> Result> { + let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + Ok(ExtractedFull(msg.raw_body.into())) + } + + // ------------ + + /// Returns the structure of the message + fn structure(&self) -> Result> { + unimplemented!(); + } +} + +// --------------------------- +struct SelectedMsg<'a>(&'a SelectedMime<'a>, &'a composite::Message<'a>); +struct SelectedMult<'a>(&'a SelectedMime<'a>, &'a composite::Multipart<'a>); +struct SelectedTxt<'a>(&'a SelectedMime<'a>, &'a discrete::Text<'a>); +struct SelectedBin<'a>(&'a SelectedMime<'a>, &'a discrete::Binary<'a>); + +// --------------------------- + +struct ExtractedFull<'a>(Cow<'a, [u8]>); +impl<'a> ExtractedFull<'a> { + /// It is possible to fetch a substring of the designated text. + /// This is done by appending an open angle bracket ("<"), the + /// octet position of the first desired octet, a period, the + /// maximum number of octets desired, and a close angle bracket + /// (">") to the part specifier. If the starting octet is beyond + /// the end of the text, an empty string is returned. + /// + /// Any partial fetch that attempts to read beyond the end of the + /// text is truncated as appropriate. A partial fetch that starts + /// at octet 0 is returned as a partial fetch, even if this + /// truncation happened. + /// + /// Note: This means that BODY[]<0.2048> of a 1500-octet message + /// will return BODY[]<0> with a literal of size 1500, not + /// BODY[]. + /// + /// Note: A substring fetch of a HEADER.FIELDS or + /// HEADER.FIELDS.NOT part specifier is calculated after + /// subsetting the header. + fn to_body_section(self, partial: &'_ Option<(u32, NonZeroU32)>) -> BodySection<'a> { + match partial { + Some((begin, len)) => self.partialize(*begin, *len), + None => BodySection::Full(self.0), + } + } + + fn partialize(self, begin: u32, len: NonZeroU32) -> BodySection<'a> { + // Asked range is starting after the end of the content, + // returning an empty buffer + if begin as usize > self.0.len() { + return BodySection::Slice { + body: Cow::Borrowed(&[][..]), + origin_octet: begin, + } + } + + // Asked range is ending after the end of the content, + // slice only the beginning of the buffer + if (begin + len.get()) as usize >= self.0.len() { + return BodySection::Slice { + body: match self.0 { + Cow::Borrowed(body) => Cow::Borrowed(&body[begin as usize..]), + Cow::Owned(body) => Cow::Owned(body[begin as usize..].to_vec()), + }, + origin_octet: begin, + } + } + + // Range is included inside the considered content, + // this is the "happy case" + BodySection::Slice { + body: match self.0 { + Cow::Borrowed(body) => Cow::Borrowed(&body[begin as usize..(begin + len.get()) as usize]), + Cow::Owned(body) => Cow::Owned(body[begin as usize..(begin + len.get()) as usize].to_vec()), + }, + origin_octet: begin, + } + } +} -- cgit v1.2.3 From 0e7595d65a7f2d8badc324bd20bad65f891a0f15 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 09:45:47 +0100 Subject: message structure msg --- src/imap/mime_view.rs | 156 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 140 insertions(+), 16 deletions(-) diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs index 74bb6ad..f13b989 100644 --- a/src/imap/mime_view.rs +++ b/src/imap/mime_view.rs @@ -5,15 +5,18 @@ use std::collections::HashSet; use anyhow::{anyhow, bail, Result}; use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; -use imap_codec::imap_types::core::{AString, IString, NonEmptyVec}; +use imap_codec::imap_types::core::{AString, IString, NString, NonEmptyVec}; use imap_codec::imap_types::fetch::{ Section as FetchSection, Part as FetchPart }; use eml_codec::{ - header, part::AnyPart, part::composite, part::discrete, + header, mime, + part::AnyPart, part::composite, part::discrete, }; +use crate::imap::imf_view::message_envelope; + pub enum BodySection<'a> { Full(Cow<'a, [u8]>), @@ -80,14 +83,13 @@ pub fn body_ext<'a>( /// b OK Fetch completed (0.001 + 0.000 secs). /// ``` pub fn bodystructure(part: &AnyPart) -> Result> { - unimplemented!(); + NodeMime(part).structure() } /// NodeMime -/// - - - +/// +/// Used for recursive logic on MIME. +/// See SelectedMime for inspection. struct NodeMime<'a>(&'a AnyPart<'a>); impl<'a> NodeMime<'a> { /// A MIME object is a tree of elements. @@ -119,6 +121,15 @@ impl<'a> NodeMime<'a> { } } } + + fn structure(&self) -> Result> { + match self.0 { + AnyPart::Txt(x) => NodeTxt(self, x).structure(), + AnyPart::Bin(x) => NodeBin(self, x).structure(), + AnyPart::Mult(x) => NodeMult(self, x).structure(), + AnyPart::Msg(x) => NodeMsg(self, x).structure(), + } + } } //---------------------------------------------------------- @@ -150,6 +161,9 @@ impl<'a> SubsettedSection<'a> { } } +/// Used for current MIME inspection +/// +/// See NodeMime for recursive logic struct SelectedMime<'a>(&'a AnyPart<'a>); impl<'a> SelectedMime<'a> { /// The subsetted fetch section basically tells us the @@ -253,18 +267,110 @@ impl<'a> SelectedMime<'a> { } // ------------ - - /// Returns the structure of the message - fn structure(&self) -> Result> { - unimplemented!(); - } + + /// Basic field of a MIME part that is + /// common to all parts + fn basic_fields(&self) -> Result> { + let sz = match self.0 { + AnyPart::Txt(x) => x.body.len(), + AnyPart::Bin(x) => x.body.len(), + AnyPart::Msg(x) => x.raw_part.len(), + AnyPart::Mult(x) => 0 + }; + let m = self.0.mime(); + let parameter_list = m + .ctype + .as_ref() + .map(|x| { + x.params + .iter() + .map(|p| { + ( + IString::try_from(String::from_utf8_lossy(p.name).to_string()), + IString::try_from(p.value.to_string()), + ) + }) + .filter(|(k, v)| k.is_ok() && v.is_ok()) + .map(|(k, v)| (k.unwrap(), v.unwrap())) + .collect() + }) + .unwrap_or(vec![]); + + Ok(BasicFields { + parameter_list, + id: NString( + m.id.as_ref() + .and_then(|ci| IString::try_from(ci.to_string()).ok()), + ), + description: NString( + m.description + .as_ref() + .and_then(|cd| IString::try_from(cd.to_string()).ok()), + ), + content_transfer_encoding: match m.transfer_encoding { + mime::mechanism::Mechanism::_8Bit => unchecked_istring("8bit"), + mime::mechanism::Mechanism::Binary => unchecked_istring("binary"), + mime::mechanism::Mechanism::QuotedPrintable => unchecked_istring("quoted-printable"), + mime::mechanism::Mechanism::Base64 => unchecked_istring("base64"), + _ => unchecked_istring("7bit"), + }, + // @FIXME we can't compute the size of the message currently... + size: u32::try_from(sz)?, + }) + } } // --------------------------- -struct SelectedMsg<'a>(&'a SelectedMime<'a>, &'a composite::Message<'a>); -struct SelectedMult<'a>(&'a SelectedMime<'a>, &'a composite::Multipart<'a>); -struct SelectedTxt<'a>(&'a SelectedMime<'a>, &'a discrete::Text<'a>); -struct SelectedBin<'a>(&'a SelectedMime<'a>, &'a discrete::Binary<'a>); +struct NodeMsg<'a>(&'a NodeMime<'a>, &'a composite::Message<'a>); +impl<'a> NodeMsg<'a> { + fn structure(&self) -> Result> { + let basic = SelectedMime(self.0.0).basic_fields()?; + + Ok(BodyStructure::Single { + body: FetchBody { + basic, + specific: SpecificFields::Message { + envelope: Box::new(message_envelope(&self.1.imf)), + body_structure: Box::new(NodeMime(&self.1.child).structure()?), + number_of_lines: nol(self.1.raw_part), + }, + }, + extension_data: None, + }) + } +} +struct NodeMult<'a>(&'a NodeMime<'a>, &'a composite::Multipart<'a>); +impl<'a> NodeMult<'a> { + fn structure(&self) -> Result> { + let itype = &self.1.mime.interpreted_type; + let subtype = IString::try_from(itype.subtype.to_string()) + .unwrap_or(unchecked_istring("alternative")); + + let inner_bodies = self.1 + .children + .iter() + .filter_map(|inner| NodeMime(&inner).structure().ok()) + .collect::>(); + + NonEmptyVec::validate(&inner_bodies)?; + let bodies = NonEmptyVec::unvalidated(inner_bodies); + + Ok(BodyStructure::Multi { + bodies, + subtype, + extension_data: None, + /*Some(MultipartExtensionData { + parameter_list: vec![], + disposition: None, + language: None, + location: None, + extension: vec![], + })*/ + }) + } +} +struct NodeTxt<'a>(&'a NodeMime<'a>, &'a discrete::Text<'a>); +struct NodeBin<'a>(&'a NodeMime<'a>, &'a discrete::Binary<'a>); // --------------------------- @@ -329,3 +435,21 @@ impl<'a> ExtractedFull<'a> { } } } + +/// ---- LEGACY + +/// s is set to static to ensure that only compile time values +/// checked by developpers are passed. +fn unchecked_istring(s: &'static str) -> IString { + IString::try_from(s).expect("this value is expected to be a valid imap-codec::IString") +} + +// Number Of Lines +fn nol(input: &[u8]) -> u32 { + input + .iter() + .filter(|x| **x == b'\n') + .count() + .try_into() + .unwrap_or(0) +} -- cgit v1.2.3 From 271ec2ef5153ef537ce9cf9f5c57cbdf5a328ad9 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 10:00:41 +0100 Subject: mime view should be complete --- src/imap/mime_view.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs index f13b989..b9e21f9 100644 --- a/src/imap/mime_view.rs +++ b/src/imap/mime_view.rs @@ -12,6 +12,7 @@ use imap_codec::imap_types::fetch::{ use eml_codec::{ header, mime, + mime::r#type::Deductible, part::AnyPart, part::composite, part::discrete, }; @@ -275,7 +276,7 @@ impl<'a> SelectedMime<'a> { AnyPart::Txt(x) => x.body.len(), AnyPart::Bin(x) => x.body.len(), AnyPart::Msg(x) => x.raw_part.len(), - AnyPart::Mult(x) => 0 + AnyPart::Mult(_) => 0, }; let m = self.0.mime(); let parameter_list = m @@ -370,7 +371,70 @@ impl<'a> NodeMult<'a> { } } struct NodeTxt<'a>(&'a NodeMime<'a>, &'a discrete::Text<'a>); +impl<'a> NodeTxt<'a> { + fn structure(&self) -> Result> { + let mut basic = SelectedMime(self.0.0).basic_fields()?; + + // Get the interpreted content type, set it + let itype = match &self.1.mime.interpreted_type { + Deductible::Inferred(v) | Deductible::Explicit(v) => v, + }; + let subtype = + IString::try_from(itype.subtype.to_string()).unwrap_or(unchecked_istring("plain")); + + // Add charset to the list of parameters if we know it has been inferred as it will be + // missing from the parsed content. + if let Deductible::Inferred(charset) = &itype.charset { + basic.parameter_list.push(( + unchecked_istring("charset"), + IString::try_from(charset.to_string()).unwrap_or(unchecked_istring("us-ascii")), + )); + } + + Ok(BodyStructure::Single { + body: FetchBody { + basic, + specific: SpecificFields::Text { + subtype, + number_of_lines: nol(self.1.body), + }, + }, + extension_data: None, + }) + } +} + struct NodeBin<'a>(&'a NodeMime<'a>, &'a discrete::Binary<'a>); +impl<'a> NodeBin<'a> { + fn structure(&self) -> Result> { + let basic = SelectedMime(self.0.0).basic_fields()?; + + let default = mime::r#type::NaiveType { + main: &b"application"[..], + sub: &b"octet-stream"[..], + params: vec![], + }; + let ct = self.1.mime.fields.ctype.as_ref().unwrap_or(&default); + + let r#type = + IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err( + anyhow!("Unable to build IString from given Content-Type type given"), + ))?; + + let subtype = + IString::try_from(String::from_utf8_lossy(ct.sub).to_string()).or(Err(anyhow!( + "Unable to build IString from given Content-Type subtype given" + )))?; + + Ok(BodyStructure::Single { + body: FetchBody { + basic, + specific: SpecificFields::Basic { r#type, subtype }, + }, + extension_data: None, + }) + } +} // --------------------------- -- cgit v1.2.3 From cd74ae5e638a03e2656fb54aa09a976e6939e1e3 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 10:05:09 +0100 Subject: clean imf view --- src/imap/imf_view.rs | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/src/imap/imf_view.rs b/src/imap/imf_view.rs index dce53e6..c18b05b 100644 --- a/src/imap/imf_view.rs +++ b/src/imap/imf_view.rs @@ -1,38 +1,7 @@ -use std::borrow::Cow; -use std::iter::zip; -use std::num::NonZeroU32; -use std::sync::Arc; - -use anyhow::{anyhow, bail, Error, Result}; -use chrono::{Offset, TimeZone, Utc}; - -use futures::stream::{FuturesOrdered, StreamExt}; - -use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; -use imap_codec::imap_types::core::{AString, Atom, IString, NString, NonEmptyVec}; -use imap_codec::imap_types::datetime::DateTime; +use imap_codec::imap_types::core::{IString, NString}; use imap_codec::imap_types::envelope::{Address, Envelope}; -use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItem, MessageDataItemName, Section as FetchSection, -}; -use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; -use imap_codec::imap_types::response::{Code, Data, Status}; -use imap_codec::imap_types::sequence::{self, SequenceSet}; - -use eml_codec::{ - header, imf, mime, - mime::r#type::Deductible, - part::{composite::Message, AnyPart}, -}; - -use crate::cryptoblob::Key; -use crate::imap::response::Body; -use crate::mail::mailbox::{MailMeta, Mailbox}; -use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; -use crate::mail::unique_ident::UniqueIdent; - - +use eml_codec::imf; /// Envelope rules are defined in RFC 3501, section 7.4.2 /// https://datatracker.ietf.org/doc/html/rfc3501#section-7.4.2 -- cgit v1.2.3 From ac8fb89d56351fbc0017b8a7a8a4ddf53217ab60 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 10:05:30 +0100 Subject: reformat cargo --- src/imap/attributes.rs | 4 +- src/imap/imf_view.rs | 1 - src/imap/mail_view.rs | 25 ++++---- src/imap/mailbox_view.rs | 21 +++---- src/imap/mime_view.rs | 153 ++++++++++++++++++++++++++-------------------- src/imap/mod.rs | 8 +-- src/imap/selectors.rs | 5 +- tests/behavior.rs | 24 +++++--- tests/common/fragments.rs | 6 +- 9 files changed, 128 insertions(+), 119 deletions(-) diff --git a/src/imap/attributes.rs b/src/imap/attributes.rs index 66b078e..7a55632 100644 --- a/src/imap/attributes.rs +++ b/src/imap/attributes.rs @@ -1,6 +1,4 @@ -use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItemName, -}; +use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItemName}; /// Internal decisions based on fetched attributes /// passed by the client diff --git a/src/imap/imf_view.rs b/src/imap/imf_view.rs index c18b05b..4297769 100644 --- a/src/imap/imf_view.rs +++ b/src/imap/imf_view.rs @@ -95,4 +95,3 @@ pub fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address<'static> { )), } } - diff --git a/src/imap/mail_view.rs b/src/imap/mail_view.rs index d1f8a58..c95c733 100644 --- a/src/imap/mail_view.rs +++ b/src/imap/mail_view.rs @@ -3,7 +3,6 @@ use std::num::NonZeroU32; use anyhow::{anyhow, bail, Result}; use chrono::{Offset, TimeZone, Utc}; - use imap_codec::imap_types::core::{IString, NString}; use imap_codec::imap_types::datetime::DateTime; use imap_codec::imap_types::fetch::{ @@ -17,13 +16,13 @@ use eml_codec::{ part::{composite::Message, AnyPart}, }; -use crate::imap::response::Body; -use crate::imap::mime_view; -use crate::imap::flags; use crate::imap::attributes::AttributesProxy; -use crate::mail::mailbox::MailMeta; -use crate::imap::mailbox_view::MailIdentifiers; +use crate::imap::flags; use crate::imap::imf_view::message_envelope; +use crate::imap::mailbox_view::MailIdentifiers; +use crate::imap::mime_view; +use crate::imap::response::Body; +use crate::mail::mailbox::MailMeta; pub struct MailView<'a> { pub ids: &'a MailIdentifiers, @@ -121,10 +120,11 @@ impl<'a> MailView<'a> { } // Process message - let (text, origin) = match mime_view::body_ext(self.content.as_anypart()?, section, partial)? { - mime_view::BodySection::Full(body) => (body, None), - mime_view::BodySection::Slice { body, origin_octet } => (body, Some(origin_octet)), - }; + let (text, origin) = + match mime_view::body_ext(self.content.as_anypart()?, section, partial)? { + mime_view::BodySection::Full(body) => (body, None), + mime_view::BodySection::Slice { body, origin_octet } => (body, Some(origin_octet)), + }; let data = NString(text.to_vec().try_into().ok().map(IString::Literal)); @@ -185,14 +185,12 @@ impl<'a> MailView<'a> { } } - pub enum SeenFlag { DoNothing, MustAdd, } - -// ------------------- +// ------------------- pub enum FetchedMail<'a> { Partial(imf::Imf<'a>), @@ -229,4 +227,3 @@ impl<'a> FetchedMail<'a> { } } } - diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 04253d0..f5cf394 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -5,18 +5,16 @@ use anyhow::{anyhow, bail, Error, Result}; use futures::stream::{FuturesOrdered, StreamExt}; -use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItem -}; +use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItem}; use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; use imap_codec::imap_types::response::{Code, Data, Status}; use imap_codec::imap_types::sequence::{self, SequenceSet}; +use crate::imap::attributes::AttributesProxy; use crate::imap::flags; +use crate::imap::mail_view::SeenFlag; use crate::imap::response::Body; -use crate::imap::attributes::AttributesProxy; use crate::imap::selectors::MailSelectionBuilder; -use crate::imap::mail_view::SeenFlag; use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; use crate::mail::unique_ident::UniqueIdent; @@ -519,7 +517,6 @@ impl MailboxView { } } - pub struct MailIdentifiers { pub i: NonZeroU32, pub uid: ImapUid, @@ -536,22 +533,19 @@ impl MailIdentifiersList { #[cfg(test)] mod tests { use super::*; - use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItemName, - }; use imap_codec::encode::Encoder; use imap_codec::imap_types::core::NonEmptyVec; use imap_codec::imap_types::fetch::Section; + use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItemName}; use imap_codec::imap_types::response::Response; use imap_codec::ResponseCodec; use std::fs; - use crate::cryptoblob; - use crate::mail::unique_ident; - use crate::mail::mailbox::MailMeta; - use crate::imap::mail_view::{MailView, FetchedMail}; + use crate::imap::mail_view::{FetchedMail, MailView}; use crate::imap::mime_view; + use crate::mail::mailbox::MailMeta; + use crate::mail::unique_ident; #[test] fn mailview_body_ext() -> Result<()> { @@ -673,4 +667,3 @@ mod tests { Ok(()) } } - diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs index b9e21f9..1f36c47 100644 --- a/src/imap/mime_view.rs +++ b/src/imap/mime_view.rs @@ -1,28 +1,23 @@ use std::borrow::Cow; -use std::num::NonZeroU32; use std::collections::HashSet; +use std::num::NonZeroU32; use anyhow::{anyhow, bail, Result}; use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; use imap_codec::imap_types::core::{AString, IString, NString, NonEmptyVec}; -use imap_codec::imap_types::fetch::{ - Section as FetchSection, Part as FetchPart -}; +use imap_codec::imap_types::fetch::{Part as FetchPart, Section as FetchSection}; use eml_codec::{ - header, mime, - mime::r#type::Deductible, - part::AnyPart, part::composite, part::discrete, + header, mime, mime::r#type::Deductible, part::composite, part::discrete, part::AnyPart, }; use crate::imap::imf_view::message_envelope; - pub enum BodySection<'a> { Full(Cow<'a, [u8]>), Slice { - body: Cow<'a, [u8]>, + body: Cow<'a, [u8]>, origin_octet: u32, }, } @@ -57,9 +52,9 @@ pub enum BodySection<'a> { /// 4.2.2.2 TEXT/RICHTEXT /// ``` pub fn body_ext<'a>( - part: &'a AnyPart<'a>, - section: &'a Option>, - partial: &'a Option<(u32, NonZeroU32)> + part: &'a AnyPart<'a>, + section: &'a Option>, + partial: &'a Option<(u32, NonZeroU32)>, ) -> Result> { let root_mime = NodeMime(part); let (extractor, path) = SubsettedSection::from(section); @@ -88,12 +83,12 @@ pub fn bodystructure(part: &AnyPart) -> Result> { } /// NodeMime -/// +/// /// Used for recursive logic on MIME. /// See SelectedMime for inspection. struct NodeMime<'a>(&'a AnyPart<'a>); impl<'a> NodeMime<'a> { - /// A MIME object is a tree of elements. + /// A MIME object is a tree of elements. /// The path indicates which element must be picked. /// This function returns the picked element as the new view fn subset(self, path: Option<&'a FetchPart>) -> Result> { @@ -138,7 +133,7 @@ impl<'a> NodeMime<'a> { /// A FetchSection must be handled in 2 times: /// - First we must extract the MIME part /// - Then we must process it as desired -/// The given struct mixes both work, so +/// The given struct mixes both work, so /// we separate this work here. enum SubsettedSection<'a> { Part, @@ -153,8 +148,12 @@ impl<'a> SubsettedSection<'a> { match section { Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), Some(FetchSection::Header(maybe_part)) => (Self::Header, maybe_part.as_ref()), - Some(FetchSection::HeaderFields(maybe_part, fields)) => (Self::HeaderFields(fields), maybe_part.as_ref()), - Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => (Self::HeaderFieldsNot(fields), maybe_part.as_ref()), + Some(FetchSection::HeaderFields(maybe_part, fields)) => { + (Self::HeaderFields(fields), maybe_part.as_ref()) + } + Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => { + (Self::HeaderFieldsNot(fields), maybe_part.as_ref()) + } Some(FetchSection::Mime(part)) => (Self::Mime, Some(part)), Some(FetchSection::Part(part)) => (Self::Part, Some(part)), None => (Self::Part, None), @@ -212,17 +211,24 @@ impl<'a> SelectedMime<'a> { /// returned by HEADER.FIELDS.NOT contains only the header fields /// with a non-matching field-name. The field-matching is /// case-insensitive but otherwise exact. - fn header_fields(&self, fields: &'a NonEmptyVec>, invert: bool) -> Result> { + fn header_fields( + &self, + fields: &'a NonEmptyVec>, + invert: bool, + ) -> Result> { // Build a lowercase ascii hashset with the fields to fetch let index = fields .as_ref() .iter() - .map(|x| match x { - AString::Atom(a) => a.inner().as_bytes(), - AString::String(IString::Literal(l)) => l.as_ref(), - AString::String(IString::Quoted(q)) => q.inner().as_bytes(), - }.to_ascii_lowercase()) - .collect::>(); + .map(|x| { + match x { + AString::Atom(a) => a.inner().as_bytes(), + AString::String(IString::Literal(l)) => l.as_ref(), + AString::String(IString::Quoted(q)) => q.inner().as_bytes(), + } + .to_ascii_lowercase() + }) + .collect::>(); // Extract MIME headers let mime = match &self.0 { @@ -234,7 +240,9 @@ impl<'a> SelectedMime<'a> { // 1. Keep only the correctly formatted headers // 2. Keep only based on the index presence or absence // 3. Reduce as a byte vector - let buffer = mime.kv.iter() + let buffer = mime + .kv + .iter() .filter_map(|field| match field { header::Field::Good(header::Kv2(k, v)) => Some((k, v)), _ => None, @@ -257,13 +265,19 @@ impl<'a> SelectedMime<'a> { /// HEADER ([RFC-2822] header of the message) /// ``` fn header(&self) -> Result> { - let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + let msg = self + .0 + .as_message() + .ok_or(anyhow!("Selected part must be a message/rfc822"))?; Ok(ExtractedFull(msg.raw_headers.into())) } /// The TEXT part specifier refers to the text body of the message, omitting the [RFC-2822] header. fn text(&self) -> Result> { - let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + let msg = self + .0 + .as_message() + .ok_or(anyhow!("Selected part must be a message/rfc822"))?; Ok(ExtractedFull(msg.raw_body.into())) } @@ -289,34 +303,36 @@ impl<'a> SelectedMime<'a> { ( IString::try_from(String::from_utf8_lossy(p.name).to_string()), IString::try_from(p.value.to_string()), - ) + ) }) - .filter(|(k, v)| k.is_ok() && v.is_ok()) + .filter(|(k, v)| k.is_ok() && v.is_ok()) .map(|(k, v)| (k.unwrap(), v.unwrap())) .collect() }) - .unwrap_or(vec![]); + .unwrap_or(vec![]); Ok(BasicFields { parameter_list, id: NString( m.id.as_ref() - .and_then(|ci| IString::try_from(ci.to_string()).ok()), - ), - description: NString( - m.description + .and_then(|ci| IString::try_from(ci.to_string()).ok()), + ), + description: NString( + m.description .as_ref() .and_then(|cd| IString::try_from(cd.to_string()).ok()), - ), - content_transfer_encoding: match m.transfer_encoding { - mime::mechanism::Mechanism::_8Bit => unchecked_istring("8bit"), - mime::mechanism::Mechanism::Binary => unchecked_istring("binary"), - mime::mechanism::Mechanism::QuotedPrintable => unchecked_istring("quoted-printable"), - mime::mechanism::Mechanism::Base64 => unchecked_istring("base64"), - _ => unchecked_istring("7bit"), - }, - // @FIXME we can't compute the size of the message currently... - size: u32::try_from(sz)?, + ), + content_transfer_encoding: match m.transfer_encoding { + mime::mechanism::Mechanism::_8Bit => unchecked_istring("8bit"), + mime::mechanism::Mechanism::Binary => unchecked_istring("binary"), + mime::mechanism::Mechanism::QuotedPrintable => { + unchecked_istring("quoted-printable") + } + mime::mechanism::Mechanism::Base64 => unchecked_istring("base64"), + _ => unchecked_istring("7bit"), + }, + // @FIXME we can't compute the size of the message currently... + size: u32::try_from(sz)?, }) } } @@ -325,7 +341,7 @@ impl<'a> SelectedMime<'a> { struct NodeMsg<'a>(&'a NodeMime<'a>, &'a composite::Message<'a>); impl<'a> NodeMsg<'a> { fn structure(&self) -> Result> { - let basic = SelectedMime(self.0.0).basic_fields()?; + let basic = SelectedMime(self.0 .0).basic_fields()?; Ok(BodyStructure::Single { body: FetchBody { @@ -347,7 +363,8 @@ impl<'a> NodeMult<'a> { let subtype = IString::try_from(itype.subtype.to_string()) .unwrap_or(unchecked_istring("alternative")); - let inner_bodies = self.1 + let inner_bodies = self + .1 .children .iter() .filter_map(|inner| NodeMime(&inner).structure().ok()) @@ -361,19 +378,19 @@ impl<'a> NodeMult<'a> { subtype, extension_data: None, /*Some(MultipartExtensionData { - parameter_list: vec![], - disposition: None, - language: None, - location: None, - extension: vec![], - })*/ + parameter_list: vec![], + disposition: None, + language: None, + location: None, + extension: vec![], + })*/ }) } } struct NodeTxt<'a>(&'a NodeMime<'a>, &'a discrete::Text<'a>); impl<'a> NodeTxt<'a> { fn structure(&self) -> Result> { - let mut basic = SelectedMime(self.0.0).basic_fields()?; + let mut basic = SelectedMime(self.0 .0).basic_fields()?; // Get the interpreted content type, set it let itype = match &self.1.mime.interpreted_type { @@ -407,7 +424,7 @@ impl<'a> NodeTxt<'a> { struct NodeBin<'a>(&'a NodeMime<'a>, &'a discrete::Binary<'a>); impl<'a> NodeBin<'a> { fn structure(&self) -> Result> { - let basic = SelectedMime(self.0.0).basic_fields()?; + let basic = SelectedMime(self.0 .0).basic_fields()?; let default = mime::r#type::NaiveType { main: &b"application"[..], @@ -416,15 +433,13 @@ impl<'a> NodeBin<'a> { }; let ct = self.1.mime.fields.ctype.as_ref().unwrap_or(&default); - let r#type = - IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err( - anyhow!("Unable to build IString from given Content-Type type given"), - ))?; + let r#type = IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err( + anyhow!("Unable to build IString from given Content-Type type given"), + ))?; - let subtype = - IString::try_from(String::from_utf8_lossy(ct.sub).to_string()).or(Err(anyhow!( - "Unable to build IString from given Content-Type subtype given" - )))?; + let subtype = IString::try_from(String::from_utf8_lossy(ct.sub).to_string()).or(Err( + anyhow!("Unable to build IString from given Content-Type subtype given"), + ))?; Ok(BodyStructure::Single { body: FetchBody { @@ -473,8 +488,8 @@ impl<'a> ExtractedFull<'a> { return BodySection::Slice { body: Cow::Borrowed(&[][..]), origin_octet: begin, - } - } + }; + } // Asked range is ending after the end of the content, // slice only the beginning of the buffer @@ -485,15 +500,19 @@ impl<'a> ExtractedFull<'a> { Cow::Owned(body) => Cow::Owned(body[begin as usize..].to_vec()), }, origin_octet: begin, - } + }; } // Range is included inside the considered content, // this is the "happy case" BodySection::Slice { body: match self.0 { - Cow::Borrowed(body) => Cow::Borrowed(&body[begin as usize..(begin + len.get()) as usize]), - Cow::Owned(body) => Cow::Owned(body[begin as usize..(begin + len.get()) as usize].to_vec()), + Cow::Borrowed(body) => { + Cow::Borrowed(&body[begin as usize..(begin + len.get()) as usize]) + } + Cow::Owned(body) => { + Cow::Owned(body[begin as usize..(begin + len.get()) as usize].to_vec()) + } }, origin_octet: begin, } diff --git a/src/imap/mod.rs b/src/imap/mod.rs index d3cf966..4f33bfe 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -1,14 +1,14 @@ +mod attributes; mod capability; mod command; +mod flags; mod flow; -mod attributes; +mod imf_view; mod mail_view; mod mailbox_view; mod mime_view; -mod imf_view; -mod selectors; -mod flags; mod response; +mod selectors; mod session; use std::net::SocketAddr; diff --git a/src/imap/selectors.rs b/src/imap/selectors.rs index 6b5372a..09320c3 100644 --- a/src/imap/selectors.rs +++ b/src/imap/selectors.rs @@ -2,9 +2,8 @@ use std::iter::zip; use anyhow::{anyhow, Result}; - use crate::cryptoblob::Key; -use crate::imap::mail_view::{MailView, FetchedMail}; +use crate::imap::mail_view::{FetchedMail, MailView}; use crate::imap::mailbox_view::MailIdentifiers; use crate::mail::mailbox::MailMeta; use crate::mail::unique_ident::UniqueIdent; @@ -14,7 +13,6 @@ pub struct BodyIdentifier<'a> { pub msg_key: &'a Key, } - #[derive(Default)] pub struct MailSelectionBuilder<'a> { //attrs: AttributeProxy, @@ -100,4 +98,3 @@ impl<'a> MailSelectionBuilder<'a> { .collect()) } } - diff --git a/tests/behavior.rs b/tests/behavior.rs index 82fdc53..17f3a72 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -26,15 +26,18 @@ fn rfc3501_imap4rev1_base() { lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Multipart).context("fetch rfc822 message, should be our first message")?; - copy(imap_socket, Selection::FirstId, Mailbox::Archive).context("copy message to the archive mailbox")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Multipart) + .context("fetch rfc822 message, should be our first message")?; + copy(imap_socket, Selection::FirstId, Mailbox::Archive) + .context("copy message to the archive mailbox")?; append_email(imap_socket, Email::Basic).context("insert email in INBOX")?; // SEARCH IS NOT IMPLEMENTED YET //search(imap_socket).expect("search should return something"); add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) .context("should add delete flag to the email")?; expunge(imap_socket).context("expunge emails")?; - rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts).context("Archive mailbox is renamed Drafts")?; + rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts) + .context("Archive mailbox is renamed Drafts")?; delete_mailbox(imap_socket, Mailbox::Drafts).context("Drafts mailbox is deleted")?; Ok(()) }) @@ -53,13 +56,16 @@ fn rfc3691_imapext_unselect() { login(imap_socket, Account::Alice).context("login test")?; select(imap_socket, Mailbox::Inbox, None).context("select inbox")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted).context("add delete flags to the email")?; + add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) + .context("add delete flags to the email")?; unselect(imap_socket) .context("unselect inbox while preserving email with the \\Delete flag")?; select(imap_socket, Mailbox::Inbox, Some(1)).context("select inbox again")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic).context("message is still present")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic) + .context("message is still present")?; close(imap_socket).context("close inbox and expunge message")?; - select(imap_socket, Mailbox::Inbox, Some(0)).context("select inbox again and check it's empty")?; + select(imap_socket, Mailbox::Inbox, Some(0)) + .context("select inbox again and check it's empty")?; Ok(()) }) @@ -94,7 +100,8 @@ fn rfc6851_imapext_move() { lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - r#move(imap_socket, Selection::FirstId, Mailbox::Archive).context("message from inbox moved to archive")?; + r#move(imap_socket, Selection::FirstId, Mailbox::Archive) + .context("message from inbox moved to archive")?; unselect(imap_socket) .context("unselect inbox while preserving email with the \\Delete flag")?; @@ -116,5 +123,6 @@ fn rfc7888_imapext_literal() { login_with_literal(imap_socket, Account::Alice).context("use literal to connect Alice")?; Ok(()) - }).expect("test fully run"); + }) + .expect("test fully run"); } diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index 3ed14cc..2e2fbd4 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -52,7 +52,7 @@ pub enum Mailbox { pub enum Flag { Deleted, - Important + Important, } pub enum Email { @@ -287,8 +287,6 @@ pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> { Ok(()) } - - pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) -> Result<()> { let mut buffer: [u8; 1500] = [0; 1500]; assert!(matches!(selection, Selection::FirstId)); @@ -390,7 +388,7 @@ pub fn enable(imap: &mut TcpStream, ask: Enable, done: Option) -> Result Some(Enable::Utf8Accept) => { assert_eq!(srv_msg.lines().count(), 2); assert!(srv_msg.contains("* ENABLED UTF8=ACCEPT")); - }, + } _ => unimplemented!(), } -- cgit v1.2.3 From 35591ff0608096b32d7bab22d719a6ceb8574c2c Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 12:40:49 +0100 Subject: search first ultra minimal implementation --- src/imap/command/selected.rs | 12 +++-- src/imap/mailbox_view.rs | 26 +++++++++++ src/imap/mod.rs | 1 + src/imap/search.rs | 107 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 src/imap/search.rs diff --git a/src/imap/command/selected.rs b/src/imap/command/selected.rs index c8cc680..933f397 100644 --- a/src/imap/command/selected.rs +++ b/src/imap/command/selected.rs @@ -136,15 +136,17 @@ impl<'a> SelectedContext<'a> { pub async fn search( self, - _charset: &Option>, - _criteria: &SearchKey<'a>, - _uid: &bool, + charset: &Option>, + criteria: &SearchKey<'a>, + uid: &bool, ) -> Result<(Response<'static>, flow::Transition)> { + let found = self.mailbox.search(charset, criteria, *uid).await?; Ok(( Response::build() .to_req(self.req) - .message("Not implemented") - .bad()?, + .set_body(found) + .message("SEARCH completed") + .ok()?, flow::Transition::None, )) } diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index f5cf394..5311635 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -5,15 +5,18 @@ use anyhow::{anyhow, bail, Error, Result}; use futures::stream::{FuturesOrdered, StreamExt}; +use imap_codec::imap_types::core::Charset; use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItem}; use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; use imap_codec::imap_types::response::{Code, Data, Status}; +use imap_codec::imap_types::search::SearchKey; use imap_codec::imap_types::sequence::{self, SequenceSet}; use crate::imap::attributes::AttributesProxy; use crate::imap::flags; use crate::imap::mail_view::SeenFlag; use crate::imap::response::Body; +use crate::imap::search; use crate::imap::selectors::MailSelectionBuilder; use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; @@ -308,6 +311,7 @@ impl MailboxView { .iter() .filter_map(|mv| mv.filter(&ap).ok().map(|(body, seen)| (mv, body, seen))) .collect::>(); + // Register seen flags let future_flags = filtered_view .iter() @@ -333,6 +337,22 @@ impl MailboxView { Ok(command_body) } + /// A very naive search implementation... + pub async fn search<'a>( + &self, + _charset: &Option>, + search_key: &SearchKey<'a>, + uid: bool, + ) -> Result>> { + let (seq_set, seq_type) = search::Criteria(search_key).to_sequence_set(); + let mailids = MailIdentifiersList(self.get_mail_ids(&seq_set, seq_type.is_uid())?); + let mail_u32 = match uid { + true => mailids.uids(), + _ => mailids.ids(), + }; + Ok(vec![Body::Data(Data::Search(mail_u32))]) + } + // ---- // Gets the IMAP ID, the IMAP UIDs and, the Aerogramme UUIDs of mails identified by a SequenceSet of @@ -525,6 +545,12 @@ pub struct MailIdentifiers { pub struct MailIdentifiersList(Vec); impl MailIdentifiersList { + fn ids(&self) -> Vec { + self.0.iter().map(|mi| mi.i).collect() + } + fn uids(&self) -> Vec { + self.0.iter().map(|mi| mi.uid).collect() + } fn uuids(&self) -> Vec { self.0.iter().map(|mi| mi.uuid).collect() } diff --git a/src/imap/mod.rs b/src/imap/mod.rs index 4f33bfe..ea34629 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -8,6 +8,7 @@ mod mail_view; mod mailbox_view; mod mime_view; mod response; +mod search; mod selectors; mod session; diff --git a/src/imap/search.rs b/src/imap/search.rs new file mode 100644 index 0000000..bf1d30e --- /dev/null +++ b/src/imap/search.rs @@ -0,0 +1,107 @@ +use imap_codec::imap_types::core::NonEmptyVec; +use imap_codec::imap_types::search::SearchKey; +use imap_codec::imap_types::sequence::{SeqOrUid, Sequence, SequenceSet}; +use std::num::NonZeroU32; + +pub enum SeqType { + Undefined, + NonUid, + Uid, +} +impl SeqType { + pub fn is_uid(&self) -> bool { + matches!(self, Self::Uid) + } +} + +pub struct Criteria<'a>(pub &'a SearchKey<'a>); +impl<'a> Criteria<'a> { + /// Returns a set of email identifiers that is greater or equal + /// to the set of emails to return + pub fn to_sequence_set(&self) -> (SequenceSet, SeqType) { + match self.0 { + SearchKey::All => (sequence_set_all(), SeqType::Undefined), + SearchKey::SequenceSet(seq_set) => (seq_set.clone(), SeqType::NonUid), + SearchKey::Uid(seq_set) => (seq_set.clone(), SeqType::Uid), + SearchKey::Not(_inner) => { + tracing::debug!( + "using NOT in a search request is slow: it selects all identifiers" + ); + (sequence_set_all(), SeqType::Undefined) + } + SearchKey::Or(left, right) => { + tracing::debug!("using OR in a search request is slow: no deduplication is done"); + let (base, base_seqtype) = Self(&left).to_sequence_set(); + let (ext, ext_seqtype) = Self(&right).to_sequence_set(); + + // Check if we have a UID/ID conflict in fetching: now we don't know how to handle them + match (base_seqtype, ext_seqtype) { + (SeqType::Uid, SeqType::NonUid) | (SeqType::NonUid, SeqType::Uid) => { + (sequence_set_all(), SeqType::Undefined) + } + (SeqType::Undefined, x) | (x, _) => { + let mut new_vec = base.0.into_inner(); + new_vec.extend_from_slice(ext.0.as_ref()); + let seq = SequenceSet( + NonEmptyVec::try_from(new_vec) + .expect("merging non empty vec lead to non empty vec"), + ); + (seq, x) + } + } + } + SearchKey::And(search_list) => { + tracing::debug!( + "using AND in a search request is slow: no intersection is performed" + ); + search_list + .as_ref() + .iter() + .map(|crit| Self(&crit).to_sequence_set()) + .min_by(|(x, _), (y, _)| { + let x_size = approx_sequence_set_size(x); + let y_size = approx_sequence_set_size(y); + x_size.cmp(&y_size) + }) + .unwrap_or((sequence_set_all(), SeqType::Undefined)) + } + _ => (sequence_set_all(), SeqType::Undefined), + } + } + + fn need_meta(&self) { + unimplemented!(); + } + + fn need_body(&self) { + unimplemented!(); + } +} + +fn sequence_set_all() -> SequenceSet { + SequenceSet::from(Sequence::Range( + SeqOrUid::Value(NonZeroU32::MIN), + SeqOrUid::Asterisk, + )) +} + +// This is wrong as sequences can overlap +fn approx_sequence_set_size(seq_set: &SequenceSet) -> u64 { + seq_set.0.as_ref().iter().fold(0u64, |acc, seq| { + acc.saturating_add(approx_sequence_size(seq)) + }) +} + +// This is wrong as sequence UID can have holes, +// as we don't know the number of messages in the mailbox also +fn approx_sequence_size(seq: &Sequence) -> u64 { + match seq { + Sequence::Single(_) => 1, + Sequence::Range(SeqOrUid::Asterisk, _) | Sequence::Range(_, SeqOrUid::Asterisk) => u64::MAX, + Sequence::Range(SeqOrUid::Value(x1), SeqOrUid::Value(x2)) => { + let x2 = x2.get() as i64; + let x1 = x1.get() as i64; + (x2 - x1).abs().try_into().unwrap_or(1) + } + } +} -- cgit v1.2.3 From d3c156a087f3c767fc0d2376abd7c1d304161d47 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 15:26:57 +0100 Subject: Select what to fecth for search --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/imap/mailbox_view.rs | 9 ++++++++- src/imap/search.rs | 30 ++++++++++++++++++++++++++---- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6a01b9..2c264f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1723,7 +1723,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite 0.2.13", - "socket2 0.5.5", + "socket2 0.4.10", "tokio", "tower-service", "tracing", diff --git a/Cargo.toml b/Cargo.toml index b110a6a..68a46e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ aws-sdk-s3 = "1.9.0" eml-codec = { git = "https://git.deuxfleurs.fr/Deuxfleurs/eml-codec.git", branch = "main" } smtp-message = { git = "http://github.com/Alexis211/kannader", branch = "feature/lmtp" } smtp-server = { git = "http://github.com/Alexis211/kannader", branch = "feature/lmtp" } -imap-codec = { version = "1.0.0", features = ["quirk_crlf_relaxed", "bounded-static", "ext_condstore_qresync"] } +imap-codec = { version = "1.0.0", features = ["bounded-static", "ext_condstore_qresync"] } imap-flow = { git = "https://github.com/duesee/imap-flow.git", rev = "e45ce7bb6ab6bda3c71a0c7b05e9b558a5902e90" } [dev-dependencies] diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 5311635..4d0858f 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -344,12 +344,19 @@ impl MailboxView { search_key: &SearchKey<'a>, uid: bool, ) -> Result>> { - let (seq_set, seq_type) = search::Criteria(search_key).to_sequence_set(); + // 1. Compute the subset of sequence identifiers we need to fetch + let query = search::Criteria(search_key); + let (seq_set, seq_type) = query.to_sequence_set(); let mailids = MailIdentifiersList(self.get_mail_ids(&seq_set, seq_type.is_uid())?); let mail_u32 = match uid { true => mailids.uids(), _ => mailids.ids(), }; + + // 2. Compute wether we will need to fetch the mail meta and/or the body + let _need_meta = query.need_meta(); + let _need_body = query.need_body(); + Ok(vec![Body::Data(Data::Search(mail_u32))]) } diff --git a/src/imap/search.rs b/src/imap/search.rs index bf1d30e..ef89288 100644 --- a/src/imap/search.rs +++ b/src/imap/search.rs @@ -69,12 +69,34 @@ impl<'a> Criteria<'a> { } } - fn need_meta(&self) { - unimplemented!(); + /// Not really clever as we can have cases where we filter out + /// the email before needing to inspect its meta. + /// But for now we are seeking the most basic/stupid algorithm. + pub fn need_meta(&self) -> bool { + use SearchKey::*; + match self.0 { + // IMF Headers + Bcc(_) | Cc(_) | From(_) | Header(..) | SentBefore(_) | SentOn(_) | SentSince(_) | Subject(_) | To(_) => true, + // Internal Date is also stored in MailMeta + Before(_) | On(_) | Since(_) => true, + // Message size is also stored in MailMeta + Larger(_) | Smaller(_) => true, + And(and_list) => and_list.as_ref().iter().any(|sk| Criteria(sk).need_meta()), + Not(inner) => Criteria(inner).need_meta(), + Or(left, right) => Criteria(left).need_meta() || Criteria(right).need_meta(), + _ => false, + } } - fn need_body(&self) { - unimplemented!(); + pub fn need_body(&self) -> bool { + use SearchKey::*; + match self.0 { + Text(_) | Body(_) => true, + And(and_list) => and_list.as_ref().iter().any(|sk| Criteria(sk).need_body()), + Not(inner) => Criteria(inner).need_body(), + Or(left, right) => Criteria(left).need_body() || Criteria(right).need_body(), + _ => false, + } } } -- cgit v1.2.3 From 335750a29a83edba9bce2fb7e1452001e4962d1f Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 15:36:40 +0100 Subject: MOVE command is optimized --- src/imap/mailbox_view.rs | 9 +++------ src/mail/mailbox.rs | 3 --- src/mail/mod.rs | 2 ++ src/mail/query.rs | 0 src/mail/snapshot.rs | 11 +++++++++++ 5 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 src/mail/query.rs create mode 100644 src/mail/snapshot.rs diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 4d0858f..6db1bd2 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -228,19 +228,16 @@ impl MailboxView { ) -> Result<(ImapUidvalidity, Vec<(ImapUid, ImapUid)>, Vec>)> { let mails = self.get_mail_ids(sequence_set, *is_uid_copy)?; - let mut new_uuids = vec![]; for mi in mails.iter() { - let copy_action = to.copy_from(&self.mailbox, mi.uuid).await?; - new_uuids.push(copy_action); - self.mailbox.delete(mi.uuid).await? + to.move_from(&self.mailbox, mi.uuid).await?; } let mut ret = vec![]; let to_state = to.current_uid_index().await; - for (mi, new_uuid) in mails.iter().zip(new_uuids.iter()) { + for mi in mails.iter() { let dest_uid = to_state .table - .get(new_uuid) + .get(&mi.uuid) .ok_or(anyhow!("moved mail not in destination mailbox"))? .0; ret.push((mi.uid, dest_uid)); diff --git a/src/mail/mailbox.rs b/src/mail/mailbox.rs index e424ba3..b011110 100644 --- a/src/mail/mailbox.rs +++ b/src/mail/mailbox.rs @@ -149,7 +149,6 @@ impl Mailbox { /// Move an email from an other Mailbox to this mailbox /// (use this when possible, as it allows for a certain number of storage optimizations) - #[allow(dead_code)] pub async fn move_from(&self, from: &Mailbox, uuid: UniqueIdent) -> Result<()> { if self.id == from.id { bail!("Cannot copy move same mailbox"); @@ -403,8 +402,6 @@ impl MailboxInternal { Ok(new_id) } - #[allow(dead_code)] - // 2023-05-15 will probably be used later async fn move_from(&mut self, from: &mut MailboxInternal, id: UniqueIdent) -> Result<()> { self.copy_internal(from, id, id).await?; from.delete(id).await?; diff --git a/src/mail/mod.rs b/src/mail/mod.rs index bbe4033..7371b53 100644 --- a/src/mail/mod.rs +++ b/src/mail/mod.rs @@ -3,6 +3,8 @@ use std::io::Write; pub mod incoming; pub mod mailbox; +pub mod snapshot; +pub mod query; pub mod uidindex; pub mod unique_ident; pub mod user; diff --git a/src/mail/query.rs b/src/mail/query.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/mail/snapshot.rs b/src/mail/snapshot.rs new file mode 100644 index 0000000..7256d50 --- /dev/null +++ b/src/mail/snapshot.rs @@ -0,0 +1,11 @@ +use std::sync::Arc; +use super::mailbox::Mailbox; +use super::uidindex::UidIndex; + +pub struct Snapshot { + pub mailbox: Arc, + pub snapshot: UidIndex, +} + +impl Snapshot { +} -- cgit v1.2.3 From adf4d33f226a745330a3bb802fe9b96f263a0895 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 17:46:16 +0100 Subject: added some utility structures --- src/mail/mailbox.rs | 4 +++ src/mail/query.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mail/snapshot.rs | 45 +++++++++++++++++++++++++++-- src/mail/uidindex.rs | 3 +- 4 files changed, 130 insertions(+), 3 deletions(-) diff --git a/src/mail/mailbox.rs b/src/mail/mailbox.rs index b011110..306fd7d 100644 --- a/src/mail/mailbox.rs +++ b/src/mail/mailbox.rs @@ -82,6 +82,10 @@ impl Mailbox { self.mbox.read().await.fetch_full(id, message_key).await } + async fn frozen(self: &std::sync::Arc) -> super::snapshot::FrozenMailbox { + super::snapshot::FrozenMailbox::new(self.clone()).await + } + // ---- Functions for changing the mailbox ---- /// Add flags to message diff --git a/src/mail/query.rs b/src/mail/query.rs index e69de29..631ad56 100644 --- a/src/mail/query.rs +++ b/src/mail/query.rs @@ -0,0 +1,81 @@ +use anyhow::{Result, anyhow}; +use super::mailbox::MailMeta; +use super::snapshot::FrozenMailbox; +use super::unique_ident::UniqueIdent; +use super::uidindex::IndexEntry; +use futures::stream::{FuturesUnordered, StreamExt}; + +/// Query is in charge of fetching efficiently +/// requested data for a list of emails +pub struct Query<'a,'b> { + pub frozen: &'a FrozenMailbox, + pub emails: &'b [UniqueIdent], +} + +impl<'a,'b> Query<'a,'b> { + pub fn index(&self) -> Result> { + self + .emails + .iter() + .map(|uuid| { + self + .frozen + .snapshot + .table + .get(uuid) + .map(|index| IndexResult { uuid: *uuid, index }) + .ok_or(anyhow!("missing email in index")) + }) + .collect::, _>>() + } + + pub async fn partial(&self) -> Result> { + let meta = self.frozen.mailbox.fetch_meta(self.emails).await?; + let result = meta + .into_iter() + .zip(self.index()?) + .map(|(metadata, index)| PartialResult { uuid: index.uuid, index: index.index, metadata }) + .collect::>(); + Ok(result) + } + + /// @FIXME WARNING: THIS CAN ALLOCATE A LOT OF MEMORY + /// AND GENERATE SO MUCH NETWORK TRAFFIC. + /// THIS FUNCTION SHOULD BE REWRITTEN, FOR EXAMPLE WITH + /// SOMETHING LIKE AN ITERATOR + pub async fn full(&self) -> Result> { + let meta_list = self.partial().await?; + meta_list + .into_iter() + .map(|meta| async move { + let content = self.frozen.mailbox.fetch_full(meta.uuid, &meta.metadata.message_key).await?; + Ok(FullResult { + uuid: meta.uuid, + index: meta.index, + metadata: meta.metadata, + content, + }) + }) + .collect::>() + .collect::>() + .await + .into_iter() + .collect::, _>>() + } +} + +pub struct IndexResult<'a> { + pub uuid: UniqueIdent, + pub index: &'a IndexEntry, +} +pub struct PartialResult<'a> { + pub uuid: UniqueIdent, + pub index: &'a IndexEntry, + pub metadata: MailMeta, +} +pub struct FullResult<'a> { + pub uuid: UniqueIdent, + pub index: &'a IndexEntry, + pub metadata: MailMeta, + pub content: Vec, +} diff --git a/src/mail/snapshot.rs b/src/mail/snapshot.rs index 7256d50..54bec64 100644 --- a/src/mail/snapshot.rs +++ b/src/mail/snapshot.rs @@ -1,11 +1,52 @@ use std::sync::Arc; + +use anyhow::Result; + use super::mailbox::Mailbox; use super::uidindex::UidIndex; -pub struct Snapshot { +/// A Frozen Mailbox has a snapshot of the current mailbox +/// state that is desynchronized with the real mailbox state. +/// It's up to the user to choose when their snapshot must be updated +/// to give useful information to their clients +/// +/// +pub struct FrozenMailbox { pub mailbox: Arc, pub snapshot: UidIndex, } -impl Snapshot { +impl FrozenMailbox { + /// Create a snapshot from a mailbox, the mailbox + the snapshot + /// becomes the "Frozen Mailbox". + pub async fn new(mailbox: Arc) -> Self { + let state = mailbox.current_uid_index().await; + + Self { + mailbox, + snapshot: state, + } + } + + /// Force the synchronization of the inner mailbox + /// but do not update the local snapshot + pub async fn sync(&self) -> Result<()> { + self.mailbox.opportunistic_sync().await + } + + /// Peek snapshot without updating the frozen mailbox + /// Can be useful if you want to plan some writes + /// while sending a diff to the client later + pub async fn peek(&self) -> UidIndex { + self.mailbox.current_uid_index().await + } + + /// Update the FrozenMailbox local snapshot. + /// Returns the old snapshot, so you can build a diff + pub async fn update(&mut self) -> UidIndex { + let old_snapshot = self.snapshot.clone(); + self.snapshot = self.mailbox.current_uid_index().await; + + old_snapshot + } } diff --git a/src/mail/uidindex.rs b/src/mail/uidindex.rs index 956b194..01f8c9c 100644 --- a/src/mail/uidindex.rs +++ b/src/mail/uidindex.rs @@ -9,6 +9,7 @@ use crate::mail::unique_ident::UniqueIdent; pub type ImapUid = NonZeroU32; pub type ImapUidvalidity = NonZeroU32; pub type Flag = String; +pub type IndexEntry = (ImapUid, Vec); /// A UidIndex handles the mutable part of a mailbox /// It is built by running the event log on it @@ -18,7 +19,7 @@ pub type Flag = String; #[derive(Clone)] pub struct UidIndex { // Source of trust - pub table: OrdMap)>, + pub table: OrdMap, // Indexes optimized for queries pub idx_by_uid: OrdMap, -- cgit v1.2.3 From 4806f7ff84c595ec6647744577388fe4fab33736 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 18:59:19 +0100 Subject: WIP rewrite with a query manager --- src/imap/command/examined.rs | 2 +- src/imap/command/selected.rs | 2 +- src/imap/mail_view.rs | 26 +++++-- src/imap/mailbox_view.rs | 180 +++++++++++++++++-------------------------- src/mail/mailbox.rs | 2 +- src/mail/query.rs | 114 +++++++++++++++++++++------ src/mail/snapshot.rs | 10 +++ 7 files changed, 193 insertions(+), 143 deletions(-) diff --git a/src/imap/command/examined.rs b/src/imap/command/examined.rs index 0d688c0..ec16973 100644 --- a/src/imap/command/examined.rs +++ b/src/imap/command/examined.rs @@ -125,7 +125,7 @@ impl<'a> ExaminedContext<'a> { } pub async fn noop(self) -> Result<(Response<'static>, flow::Transition)> { - self.mailbox.mailbox.force_sync().await?; + self.mailbox.0.mailbox.force_sync().await?; let updates = self.mailbox.update().await?; Ok(( diff --git a/src/imap/command/selected.rs b/src/imap/command/selected.rs index 933f397..35c3eb4 100644 --- a/src/imap/command/selected.rs +++ b/src/imap/command/selected.rs @@ -152,7 +152,7 @@ impl<'a> SelectedContext<'a> { } pub async fn noop(self) -> Result<(Response<'static>, flow::Transition)> { - self.mailbox.mailbox.force_sync().await?; + self.mailbox.0.mailbox.force_sync().await?; let updates = self.mailbox.update().await?; Ok(( diff --git a/src/imap/mail_view.rs b/src/imap/mail_view.rs index c95c733..94215dc 100644 --- a/src/imap/mail_view.rs +++ b/src/imap/mail_view.rs @@ -1,6 +1,6 @@ use std::num::NonZeroU32; -use anyhow::{anyhow, bail, Result}; +use anyhow::{anyhow, bail, Result, Context}; use chrono::{Offset, TimeZone, Utc}; use imap_codec::imap_types::core::{IString, NString}; @@ -22,16 +22,31 @@ use crate::imap::imf_view::message_envelope; use crate::imap::mailbox_view::MailIdentifiers; use crate::imap::mime_view; use crate::imap::response::Body; -use crate::mail::mailbox::MailMeta; +use crate::mail::query::QueryResult; pub struct MailView<'a> { - pub ids: &'a MailIdentifiers, - pub meta: &'a MailMeta, - pub flags: &'a Vec, + pub query_result: &'a QueryResult<'a>, pub content: FetchedMail<'a>, } impl<'a> MailView<'a> { + pub fn new(query_result: &'a QueryResult<'a>) -> Result { + Ok(Self { + query_result, + content: match query_result { + QueryResult::FullResult { content, .. } => { + let (_, parsed) = eml_codec::parse_message(content).context("Invalid mail body")?; + FetchedMail::new_from_message(parsed) + }, + QueryResult::PartialResult { metadata, .. } => { + let (_, parsed) = eml_codec::parse_imf(&metadata.headers).context("Invalid mail headers")?; + FetchedMail::Partial(parsed) + } + QueryResult::IndexResult { .. } => FetchedMail::None, + } + }) + } + fn uid(&self) -> MessageDataItem<'static> { MessageDataItem::Uid(self.ids.uid.clone()) } @@ -193,6 +208,7 @@ pub enum SeenFlag { // ------------------- pub enum FetchedMail<'a> { + None, Partial(imf::Imf<'a>), Full(AnyPart<'a>), } diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 6db1bd2..9cc72c1 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -12,15 +12,18 @@ use imap_codec::imap_types::response::{Code, Data, Status}; use imap_codec::imap_types::search::SearchKey; use imap_codec::imap_types::sequence::{self, SequenceSet}; +use crate::mail::mailbox::Mailbox; +use crate::mail::snapshot::FrozenMailbox; +use crate::mail::query::QueryScope; +use crate::mail::uidindex::{ImapUid, ImapUidvalidity}; +use crate::mail::unique_ident::UniqueIdent; + use crate::imap::attributes::AttributesProxy; use crate::imap::flags; -use crate::imap::mail_view::SeenFlag; +use crate::imap::mail_view::{MailView, SeenFlag}; use crate::imap::response::Body; use crate::imap::search; -use crate::imap::selectors::MailSelectionBuilder; -use crate::mail::mailbox::Mailbox; -use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; -use crate::mail::unique_ident::UniqueIdent; + const DEFAULT_FLAGS: [Flag; 5] = [ Flag::Seen, @@ -37,20 +40,12 @@ const DEFAULT_FLAGS: [Flag; 5] = [ /// To do this, it keeps a variable `known_state` that corresponds to /// what the client knows, and produces IMAP messages to be sent to the /// client that go along updates to `known_state`. -pub struct MailboxView { - pub(crate) mailbox: Arc, - known_state: UidIndex, -} +pub struct MailboxView (pub FrozenMailbox); impl MailboxView { /// Creates a new IMAP view into a mailbox. pub async fn new(mailbox: Arc) -> Self { - let state = mailbox.current_uid_index().await; - - Self { - mailbox, - known_state: state, - } + Self(mailbox.frozen().await) } /// Create an updated view, useful to make a diff @@ -60,11 +55,8 @@ impl MailboxView { /// This does NOT trigger a sync, it bases itself on what is currently /// loaded in RAM by Bayou. pub async fn update(&mut self) -> Result>> { - let old_view: &mut Self = self; - let new_view = Self { - mailbox: old_view.mailbox.clone(), - known_state: old_view.mailbox.current_uid_index().await, - }; + let old_snapshot = self.0.update().await; + let new_snapshot = &self.0.snapshot; let mut data = Vec::::new(); @@ -85,8 +77,8 @@ impl MailboxView { // - notify client of expunged mails let mut n_expunge = 0; - for (i, (_uid, uuid)) in old_view.known_state.idx_by_uid.iter().enumerate() { - if !new_view.known_state.table.contains_key(uuid) { + for (i, (_uid, uuid)) in old_snapshot.idx_by_uid.iter().enumerate() { + if !new_snapshot.table.contains_key(uuid) { data.push(Body::Data(Data::Expunge( NonZeroU32::try_from((i + 1 - n_expunge) as u32).unwrap(), ))); @@ -95,21 +87,21 @@ impl MailboxView { } // - if new mails arrived, notify client of number of existing mails - if new_view.known_state.table.len() != old_view.known_state.table.len() - n_expunge - || new_view.known_state.uidvalidity != old_view.known_state.uidvalidity + if new_snapshot.table.len() != old_snapshot.table.len() - n_expunge + || new_snapshot.uidvalidity != old_snapshot.uidvalidity { - data.push(new_view.exists_status()?); + data.push(self.exists_status()?); } - if new_view.known_state.uidvalidity != old_view.known_state.uidvalidity { + if new_snapshot.uidvalidity != old_snapshot.uidvalidity { // TODO: do we want to push less/more info than this? - data.push(new_view.uidvalidity_status()?); - data.push(new_view.uidnext_status()?); + data.push(self.uidvalidity_status()?); + data.push(self.uidnext_status()?); } else { // - if flags changed for existing mails, tell client - for (i, (_uid, uuid)) in new_view.known_state.idx_by_uid.iter().enumerate() { - let old_mail = old_view.known_state.table.get(uuid); - let new_mail = new_view.known_state.table.get(uuid); + for (i, (_uid, uuid)) in new_snapshot.idx_by_uid.iter().enumerate() { + let old_mail = old_snapshot.table.get(uuid); + let new_mail = new_snapshot.table.get(uuid); if old_mail.is_some() && old_mail != new_mail { if let Some((uid, flags)) = new_mail { data.push(Body::Data(Data::Fetch { @@ -126,7 +118,6 @@ impl MailboxView { } } } - *old_view = new_view; Ok(data) } @@ -152,7 +143,7 @@ impl MailboxView { flags: &[Flag<'a>], is_uid_store: &bool, ) -> Result>> { - self.mailbox.opportunistic_sync().await?; + self.0.sync().await?; let flags = flags.iter().map(|x| x.to_string()).collect::>(); @@ -160,13 +151,13 @@ impl MailboxView { for mi in mails.iter() { match kind { StoreType::Add => { - self.mailbox.add_flags(mi.uuid, &flags[..]).await?; + self.0.mailbox.add_flags(mi.uuid, &flags[..]).await?; } StoreType::Remove => { - self.mailbox.del_flags(mi.uuid, &flags[..]).await?; + self.0.mailbox.del_flags(mi.uuid, &flags[..]).await?; } StoreType::Replace => { - self.mailbox.set_flags(mi.uuid, &flags[..]).await?; + self.0.mailbox.set_flags(mi.uuid, &flags[..]).await?; } } } @@ -176,10 +167,10 @@ impl MailboxView { } pub async fn expunge(&mut self) -> Result>> { - self.mailbox.opportunistic_sync().await?; + self.0.sync().await?; + let state = self.0.peek().await; let deleted_flag = Flag::Deleted.to_string(); - let state = self.mailbox.current_uid_index().await; let msgs = state .table .iter() @@ -187,7 +178,7 @@ impl MailboxView { .map(|(uuid, _)| *uuid); for msg in msgs { - self.mailbox.delete(msg).await?; + self.0.mailbox.delete(msg).await?; } self.update().await @@ -203,7 +194,7 @@ impl MailboxView { let mut new_uuids = vec![]; for mi in mails.iter() { - new_uuids.push(to.copy_from(&self.mailbox, mi.uuid).await?); + new_uuids.push(to.copy_from(&self.0.mailbox, mi.uuid).await?); } let mut ret = vec![]; @@ -229,7 +220,7 @@ impl MailboxView { let mails = self.get_mail_ids(sequence_set, *is_uid_copy)?; for mi in mails.iter() { - to.move_from(&self.mailbox, mi.uuid).await?; + to.move_from(&self.0.mailbox, mi.uuid).await?; } let mut ret = vec![]; @@ -256,82 +247,49 @@ impl MailboxView { attributes: &'b MacroOrMessageDataItemNames<'static>, is_uid_fetch: &bool, ) -> Result>> { + // [1/6] Pre-compute data + // a. what are the uuids of the emails we want? + // b. do we need to fetch the full body? let ap = AttributesProxy::new(attributes, *is_uid_fetch); - - // Prepare data + let query_scope = match ap.need_body() { + true => QueryScope::Full, + _ => QueryScope::Partial, + }; let mids = MailIdentifiersList(self.get_mail_ids(sequence_set, *is_uid_fetch)?); - let mail_count = mids.0.len(); let uuids = mids.uuids(); - let meta = self.mailbox.fetch_meta(&uuids).await?; - let flags = uuids - .iter() - .map(|uuid| { - self.known_state - .table - .get(uuid) - .map(|(_uuid, f)| f) - .ok_or(anyhow!("missing email from the flag table")) - }) - .collect::, _>>()?; - - // Start filling data to build the view - let mut selection = MailSelectionBuilder::new(ap.need_body(), mail_count); - selection - .with_mail_identifiers(&mids.0) - .with_metadata(&meta) - .with_flags(&flags); - // Asynchronously fetch full bodies (if needed) - let btc = selection.bodies_to_collect(); - let future_bodies = btc - .iter() - .map(|bi| async move { - let body = self.mailbox.fetch_full(*bi.msg_uuid, bi.msg_key).await?; - Ok::<_, anyhow::Error>(body) - }) - .collect::>(); - let bodies = future_bodies - .collect::>() - .await - .into_iter() + // [2/6] Fetch the emails + let query = self.0.query(&uuids, query_scope); + let query_result = query.fetch().await?; + + // [3/6] Derive an IMAP-specific view from the results, apply the filters + let views = query_result.iter() + .map(MailView::new) .collect::, _>>()?; - // Add bodies - selection.with_bodies(bodies.as_slice()); - - // Build mail selection views - let views = selection.build()?; - - // Filter views to build the result - // Also identify what must be put as seen - let filtered_view = views + // [4/6] Apply the IMAP transformation to keep only relevant fields + let (flag_mgmt, imap_ret): (Vec<_>, Vec<_>) = views .iter() - .filter_map(|mv| mv.filter(&ap).ok().map(|(body, seen)| (mv, body, seen))) - .collect::>(); + .filter_map(|mv| mv.filter(&ap).ok().map(|(body, seen)| ((mv, seen), body))) + .unzip(); - // Register seen flags - let future_flags = filtered_view + // [5/6] Register seen flags + flag_mgmt .iter() - .filter(|(_mv, _body, seen)| matches!(seen, SeenFlag::MustAdd)) - .map(|(mv, _body, _seen)| async move { + .filter(|(_mv, seen)| matches!(seen, SeenFlag::MustAdd)) + .map(|(mv, _seen)| async move { let seen_flag = Flag::Seen.to_string(); - self.mailbox.add_flags(mv.ids.uuid, &[seen_flag]).await?; + self.0.mailbox.add_flags(*mv.query_result.uuid(), &[seen_flag]).await?; Ok::<_, anyhow::Error>(()) }) - .collect::>(); - - future_flags + .collect::>() .collect::>() .await .into_iter() .collect::>()?; - let command_body = filtered_view - .into_iter() - .map(|(_mv, body, _seen)| body) - .collect::>(); - - Ok(command_body) + // [6/6] Build the final result that will be sent to the client. + Ok(imap_ret) } /// A very naive search implementation... @@ -367,7 +325,8 @@ impl MailboxView { by_uid: bool, ) -> Result> { let mail_vec = self - .known_state + .0 + .snapshot .idx_by_uid .iter() .map(|(uid, uuid)| (*uid, *uuid)) @@ -439,7 +398,7 @@ impl MailboxView { } pub(crate) fn uidvalidity(&self) -> ImapUidvalidity { - self.known_state.uidvalidity + self.0.snapshot.uidvalidity } /// Produce an OK [UIDNEXT _] message corresponding to `known_state` @@ -454,7 +413,7 @@ impl MailboxView { } pub(crate) fn uidnext(&self) -> ImapUid { - self.known_state.uidnext + self.0.snapshot.uidnext } /// Produce an EXISTS message corresponding to the number of mails @@ -464,7 +423,7 @@ impl MailboxView { } pub(crate) fn exists(&self) -> Result { - Ok(u32::try_from(self.known_state.idx_by_uid.len())?) + Ok(u32::try_from(self.0.snapshot.idx_by_uid.len())?) } /// Produce a RECENT message corresponding to the number of @@ -475,7 +434,8 @@ impl MailboxView { pub(crate) fn recent(&self) -> Result { let recent = self - .known_state + .0 + .snapshot .idx_by_flag .get(&"\\Recent".to_string()) .map(|os| os.len()) @@ -490,8 +450,8 @@ impl MailboxView { // 1. Collecting all the possible flags in the mailbox // 1.a Fetch them from our index - let mut known_flags: Vec = self - .known_state + let mut known_flags: Vec = self.0 + .snapshot .idx_by_flag .flags() .filter_map(|f| match flags::from_str(f) { @@ -530,9 +490,9 @@ impl MailboxView { } pub(crate) fn unseen_count(&self) -> usize { - let total = self.known_state.table.len(); - let seen = self - .known_state + let total = self.0.snapshot.table.len(); + let seen = self.0 + .snapshot .idx_by_flag .get(&Flag::Seen.to_string()) .map(|x| x.len()) diff --git a/src/mail/mailbox.rs b/src/mail/mailbox.rs index 306fd7d..2a0a24a 100644 --- a/src/mail/mailbox.rs +++ b/src/mail/mailbox.rs @@ -82,7 +82,7 @@ impl Mailbox { self.mbox.read().await.fetch_full(id, message_key).await } - async fn frozen(self: &std::sync::Arc) -> super::snapshot::FrozenMailbox { + pub async fn frozen(self: &std::sync::Arc) -> super::snapshot::FrozenMailbox { super::snapshot::FrozenMailbox::new(self.clone()).await } diff --git a/src/mail/query.rs b/src/mail/query.rs index 631ad56..5beff37 100644 --- a/src/mail/query.rs +++ b/src/mail/query.rs @@ -10,10 +10,27 @@ use futures::stream::{FuturesUnordered, StreamExt}; pub struct Query<'a,'b> { pub frozen: &'a FrozenMailbox, pub emails: &'b [UniqueIdent], + pub scope: QueryScope, +} + +pub enum QueryScope { + Index, + Partial, + Full, } impl<'a,'b> Query<'a,'b> { - pub fn index(&self) -> Result> { + pub async fn fetch(&self) -> Result> { + match self.scope { + QueryScope::Index => self.index(), + QueryScope::Partial => self.partial().await, + QueryScope::Full => self.full().await, + } + } + + // --- functions below are private *for reasons* + + fn index(&self) -> Result> { self .emails .iter() @@ -23,18 +40,18 @@ impl<'a,'b> Query<'a,'b> { .snapshot .table .get(uuid) - .map(|index| IndexResult { uuid: *uuid, index }) + .map(|index| QueryResult::IndexResult { uuid: *uuid, index }) .ok_or(anyhow!("missing email in index")) }) .collect::, _>>() } - pub async fn partial(&self) -> Result> { + async fn partial(&self) -> Result> { let meta = self.frozen.mailbox.fetch_meta(self.emails).await?; let result = meta .into_iter() .zip(self.index()?) - .map(|(metadata, index)| PartialResult { uuid: index.uuid, index: index.index, metadata }) + .map(|(metadata, index)| index.into_partial(metadata).expect("index to be IndexResult")) .collect::>(); Ok(result) } @@ -43,18 +60,17 @@ impl<'a,'b> Query<'a,'b> { /// AND GENERATE SO MUCH NETWORK TRAFFIC. /// THIS FUNCTION SHOULD BE REWRITTEN, FOR EXAMPLE WITH /// SOMETHING LIKE AN ITERATOR - pub async fn full(&self) -> Result> { + async fn full(&self) -> Result> { let meta_list = self.partial().await?; meta_list .into_iter() .map(|meta| async move { - let content = self.frozen.mailbox.fetch_full(meta.uuid, &meta.metadata.message_key).await?; - Ok(FullResult { - uuid: meta.uuid, - index: meta.index, - metadata: meta.metadata, - content, - }) + let content = self.frozen.mailbox.fetch_full( + *meta.uuid(), + &meta.metadata().expect("meta to be PartialResult").message_key + ).await?; + + Ok(meta.into_full(content).expect("meta to be PartialResult")) }) .collect::>() .collect::>() @@ -64,18 +80,66 @@ impl<'a,'b> Query<'a,'b> { } } -pub struct IndexResult<'a> { - pub uuid: UniqueIdent, - pub index: &'a IndexEntry, -} -pub struct PartialResult<'a> { - pub uuid: UniqueIdent, - pub index: &'a IndexEntry, - pub metadata: MailMeta, +pub enum QueryResult<'a> { + IndexResult { + uuid: UniqueIdent, + index: &'a IndexEntry, + }, + PartialResult { + uuid: UniqueIdent, + index: &'a IndexEntry, + metadata: MailMeta, + }, + FullResult { + uuid: UniqueIdent, + index: &'a IndexEntry, + metadata: MailMeta, + content: Vec, + } } -pub struct FullResult<'a> { - pub uuid: UniqueIdent, - pub index: &'a IndexEntry, - pub metadata: MailMeta, - pub content: Vec, +impl<'a> QueryResult<'a> { + pub fn uuid(&self) -> &UniqueIdent { + match self { + Self::IndexResult { uuid, .. } => uuid, + Self::PartialResult { uuid, .. } => uuid, + Self::FullResult { uuid, .. } => uuid, + } + } + + pub fn index(&self) -> &IndexEntry { + match self { + Self::IndexResult { index, .. } => index, + Self::PartialResult { index, .. } => index, + Self::FullResult { index, .. } => index, + } + } + + pub fn metadata(&self) -> Option<&MailMeta> { + match self { + Self::IndexResult { .. } => None, + Self::PartialResult { metadata, .. } => Some(metadata), + Self::FullResult { metadata, .. } => Some(metadata), + } + } + + pub fn content(&self) -> Option<&[u8]> { + match self { + Self::FullResult { content, .. } => Some(content), + _ => None, + } + } + + fn into_partial(self, metadata: MailMeta) -> Option { + match self { + Self::IndexResult { uuid, index } => Some(Self::PartialResult { uuid, index, metadata }), + _ => None, + } + } + + fn into_full(self, content: Vec) -> Option { + match self { + Self::PartialResult { uuid, index, metadata } => Some(Self::FullResult { uuid, index, metadata, content }), + _ => None, + } + } } diff --git a/src/mail/snapshot.rs b/src/mail/snapshot.rs index 54bec64..c3145b4 100644 --- a/src/mail/snapshot.rs +++ b/src/mail/snapshot.rs @@ -4,6 +4,8 @@ use anyhow::Result; use super::mailbox::Mailbox; use super::uidindex::UidIndex; +use super::unique_ident::UniqueIdent; +use super::query::{Query, QueryScope}; /// A Frozen Mailbox has a snapshot of the current mailbox /// state that is desynchronized with the real mailbox state. @@ -49,4 +51,12 @@ impl FrozenMailbox { old_snapshot } + + pub fn query<'a, 'b>(&'a self, uuids: &'b [UniqueIdent], scope: QueryScope) -> Query<'a, 'b> { + Query { + frozen: self, + emails: uuids, + scope, + } + } } -- cgit v1.2.3 From a84ba4d42fcdb38be514178eb9fced777ba76055 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 6 Jan 2024 11:07:53 +0100 Subject: Mailbox View made more readable --- src/imap/index.rs | 80 +++++++++++++++++++++++++++++++ src/imap/mail_view.rs | 94 +++++++++++++++---------------------- src/imap/mailbox_view.rs | 119 ++++++++--------------------------------------- src/imap/mod.rs | 2 +- src/imap/selectors.rs | 100 --------------------------------------- src/mail/query.rs | 7 ++- 6 files changed, 143 insertions(+), 259 deletions(-) create mode 100644 src/imap/index.rs delete mode 100644 src/imap/selectors.rs diff --git a/src/imap/index.rs b/src/imap/index.rs new file mode 100644 index 0000000..347222c --- /dev/null +++ b/src/imap/index.rs @@ -0,0 +1,80 @@ +use std::num::NonZeroU32; + +use anyhow::{anyhow, bail, Result}; +use imap_codec::imap_types::sequence::{self, SequenceSet}; + +use crate::mail::uidindex::{ImapUid, UidIndex}; +use crate::mail::unique_ident::UniqueIdent; + +pub struct Index<'a>(pub &'a UidIndex); +impl<'a> Index<'a> { + pub fn fetch(self: &Index<'a>, sequence_set: &SequenceSet, by_uid: bool) -> Result>> { + let mail_vec = self + .0 + .idx_by_uid + .iter() + .map(|(uid, uuid)| (*uid, *uuid)) + .collect::>(); + + let mut mails = vec![]; + + if by_uid { + if mail_vec.is_empty() { + return Ok(vec![]); + } + let iter_strat = sequence::Strategy::Naive { + largest: mail_vec.last().unwrap().0, + }; + + let mut i = 0; + for uid in sequence_set.iter(iter_strat) { + while mail_vec.get(i).map(|mail| mail.0 < uid).unwrap_or(false) { + i += 1; + } + if let Some(mail) = mail_vec.get(i) { + if mail.0 == uid { + mails.push(MailIndex { + i: NonZeroU32::try_from(i as u32 + 1).unwrap(), + uid: mail.0, + uuid: mail.1, + flags: self.0.table.get(&mail.1).ok_or(anyhow!("mail is missing from index"))?.1.as_ref(), + }); + } + } else { + break; + } + } + } else { + if mail_vec.is_empty() { + bail!("No such message (mailbox is empty)"); + } + + let iter_strat = sequence::Strategy::Naive { + largest: NonZeroU32::try_from((mail_vec.len()) as u32).unwrap(), + }; + + for i in sequence_set.iter(iter_strat) { + if let Some(mail) = mail_vec.get(i.get() as usize - 1) { + mails.push(MailIndex { + i, + uid: mail.0, + uuid: mail.1, + flags: self.0.table.get(&mail.1).ok_or(anyhow!("mail is missing from index"))?.1.as_ref(), + }); + } else { + bail!("No such mail: {}", i); + } + } + } + + Ok(mails) + + } +} + +pub struct MailIndex<'a> { + pub i: NonZeroU32, + pub uid: ImapUid, + pub uuid: UniqueIdent, + pub flags: &'a Vec +} diff --git a/src/imap/mail_view.rs b/src/imap/mail_view.rs index 94215dc..1f87f02 100644 --- a/src/imap/mail_view.rs +++ b/src/imap/mail_view.rs @@ -1,9 +1,9 @@ use std::num::NonZeroU32; -use anyhow::{anyhow, bail, Result, Context}; +use anyhow::{anyhow, bail, Result}; use chrono::{Offset, TimeZone, Utc}; -use imap_codec::imap_types::core::{IString, NString}; +use imap_codec::imap_types::core::NString; use imap_codec::imap_types::datetime::DateTime; use imap_codec::imap_types::fetch::{ MessageDataItem, MessageDataItemName, Section as FetchSection, @@ -16,87 +16,73 @@ use eml_codec::{ part::{composite::Message, AnyPart}, }; + +use crate::mail::query::QueryResult; + use crate::imap::attributes::AttributesProxy; use crate::imap::flags; use crate::imap::imf_view::message_envelope; -use crate::imap::mailbox_view::MailIdentifiers; use crate::imap::mime_view; use crate::imap::response::Body; -use crate::mail::query::QueryResult; +use crate::imap::index::MailIndex; pub struct MailView<'a> { + pub in_idx: MailIndex<'a>, pub query_result: &'a QueryResult<'a>, pub content: FetchedMail<'a>, } impl<'a> MailView<'a> { - pub fn new(query_result: &'a QueryResult<'a>) -> Result { + pub fn new(query_result: &'a QueryResult<'a>, in_idx: MailIndex<'a>) -> Result> { Ok(Self { + in_idx, query_result, content: match query_result { QueryResult::FullResult { content, .. } => { - let (_, parsed) = eml_codec::parse_message(content).context("Invalid mail body")?; + let (_, parsed) = eml_codec::parse_message(&content).or(Err(anyhow!("Invalid mail body")))?; FetchedMail::new_from_message(parsed) }, QueryResult::PartialResult { metadata, .. } => { - let (_, parsed) = eml_codec::parse_imf(&metadata.headers).context("Invalid mail headers")?; + let (_, parsed) = eml_codec::parse_imf(&metadata.headers).or(Err(anyhow!("unable to parse email headers")))?; FetchedMail::Partial(parsed) } - QueryResult::IndexResult { .. } => FetchedMail::None, + QueryResult::IndexResult { .. } => FetchedMail::IndexOnly, } }) } - + fn uid(&self) -> MessageDataItem<'static> { - MessageDataItem::Uid(self.ids.uid.clone()) + MessageDataItem::Uid(self.in_idx.uid.clone()) } fn flags(&self) -> MessageDataItem<'static> { MessageDataItem::Flags( - self.flags + self.in_idx + .flags .iter() .filter_map(|f| flags::from_str(f)) .collect(), ) } - fn rfc_822_size(&self) -> MessageDataItem<'static> { - MessageDataItem::Rfc822Size(self.meta.rfc822_size as u32) + fn rfc_822_size(&self) -> Result> { + let sz = self.query_result.metadata().ok_or(anyhow!("mail metadata are required"))?.rfc822_size; + Ok(MessageDataItem::Rfc822Size(sz as u32)) } - fn rfc_822_header(&self) -> MessageDataItem<'static> { - MessageDataItem::Rfc822Header(NString( - self.meta - .headers - .to_vec() - .try_into() - .ok() - .map(IString::Literal), - )) + fn rfc_822_header(&self) -> Result> { + let hdrs: NString = self.query_result.metadata().ok_or(anyhow!("mail metadata are required"))?.headers.to_vec().try_into()?; + Ok(MessageDataItem::Rfc822Header(hdrs)) } fn rfc_822_text(&self) -> Result> { - Ok(MessageDataItem::Rfc822Text(NString( - self.content - .as_full()? - .raw_body - .to_vec() - .try_into() - .ok() - .map(IString::Literal), - ))) + let txt: NString = self.content.as_full()?.raw_body.to_vec().try_into()?; + Ok(MessageDataItem::Rfc822Text(txt)) } fn rfc822(&self) -> Result> { - Ok(MessageDataItem::Rfc822(NString( - self.content - .as_full()? - .raw_part - .to_vec() - .try_into() - .ok() - .map(IString::Literal), - ))) + let full: NString = self.content.as_full()?.raw_part.to_vec().try_into()?; + Ok(MessageDataItem::Rfc822(full)) } fn envelope(&self) -> MessageDataItem<'static> { @@ -119,16 +105,16 @@ impl<'a> MailView<'a> { /// peek does not implicitly set the \Seen flag /// eg. BODY[HEADER.FIELDS (DATE FROM)] /// eg. BODY[]<0.2048> - fn body_ext<'b>( + fn body_ext( &self, - section: &Option>, + section: &Option>, partial: &Option<(u32, NonZeroU32)>, peek: &bool, - ) -> Result<(MessageDataItem<'b>, SeenFlag)> { + ) -> Result<(MessageDataItem<'static>, SeenFlag)> { // Manage Seen flag let mut seen = SeenFlag::DoNothing; let seen_flag = Flag::Seen.to_string(); - if !peek && !self.flags.iter().any(|x| *x == seen_flag) { + if !peek && !self.in_idx.flags.iter().any(|x| *x == seen_flag) { // Add \Seen flag //self.mailbox.add_flags(uuid, &[seen_flag]).await?; seen = SeenFlag::MustAdd; @@ -141,7 +127,7 @@ impl<'a> MailView<'a> { mime_view::BodySection::Slice { body, origin_octet } => (body, Some(origin_octet)), }; - let data = NString(text.to_vec().try_into().ok().map(IString::Literal)); + let data: NString = text.to_vec().try_into()?; return Ok(( MessageDataItem::BodyExt { @@ -156,13 +142,13 @@ impl<'a> MailView<'a> { fn internal_date(&self) -> Result> { let dt = Utc .fix() - .timestamp_opt(i64::try_from(self.meta.internaldate / 1000)?, 0) + .timestamp_opt(i64::try_from(self.query_result.metadata().ok_or(anyhow!("mail metadata were not fetched"))?.internaldate / 1000)?, 0) .earliest() .ok_or(anyhow!("Unable to parse internal date"))?; Ok(MessageDataItem::InternalDate(DateTime::unvalidated(dt))) } - pub fn filter<'b>(&self, ap: &AttributesProxy) -> Result<(Body<'static>, SeenFlag)> { + pub fn filter(&self, ap: &AttributesProxy) -> Result<(Body<'static>, SeenFlag)> { let mut seen = SeenFlag::DoNothing; let res_attrs = ap .attrs @@ -170,8 +156,8 @@ impl<'a> MailView<'a> { .map(|attr| match attr { MessageDataItemName::Uid => Ok(self.uid()), MessageDataItemName::Flags => Ok(self.flags()), - MessageDataItemName::Rfc822Size => Ok(self.rfc_822_size()), - MessageDataItemName::Rfc822Header => Ok(self.rfc_822_header()), + MessageDataItemName::Rfc822Size => self.rfc_822_size(), + MessageDataItemName::Rfc822Header => self.rfc_822_header(), MessageDataItemName::Rfc822Text => self.rfc_822_text(), MessageDataItemName::Rfc822 => self.rfc822(), MessageDataItemName::Envelope => Ok(self.envelope()), @@ -192,7 +178,7 @@ impl<'a> MailView<'a> { Ok(( Body::Data(Data::Fetch { - seq: self.ids.i, + seq: self.in_idx.i, items: res_attrs.try_into()?, }), seen, @@ -208,19 +194,15 @@ pub enum SeenFlag { // ------------------- pub enum FetchedMail<'a> { - None, + IndexOnly, Partial(imf::Imf<'a>), Full(AnyPart<'a>), } impl<'a> FetchedMail<'a> { pub fn new_from_message(msg: Message<'a>) -> Self { - FetchedMail::Full(AnyPart::Msg(msg)) + Self::Full(AnyPart::Msg(msg)) } - /*fn new_from_header(hdr: imf::Imf<'a>) -> Self { - FetchedMail::Partial(hdr) - }*/ - fn as_anypart(&self) -> Result<&AnyPart<'a>> { match self { FetchedMail::Full(x) => Ok(&x), diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 9cc72c1..5bc6f87 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -1,7 +1,7 @@ use std::num::NonZeroU32; use std::sync::Arc; -use anyhow::{anyhow, bail, Error, Result}; +use anyhow::{anyhow, Error, Result}; use futures::stream::{FuturesOrdered, StreamExt}; @@ -10,19 +10,19 @@ use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItem use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; use imap_codec::imap_types::response::{Code, Data, Status}; use imap_codec::imap_types::search::SearchKey; -use imap_codec::imap_types::sequence::{self, SequenceSet}; +use imap_codec::imap_types::sequence::SequenceSet; use crate::mail::mailbox::Mailbox; use crate::mail::snapshot::FrozenMailbox; use crate::mail::query::QueryScope; use crate::mail::uidindex::{ImapUid, ImapUidvalidity}; -use crate::mail::unique_ident::UniqueIdent; use crate::imap::attributes::AttributesProxy; use crate::imap::flags; use crate::imap::mail_view::{MailView, SeenFlag}; use crate::imap::response::Body; -use crate::imap::search; +//use crate::imap::search; +use crate::imap::index::Index; const DEFAULT_FLAGS: [Flag; 5] = [ @@ -147,7 +147,7 @@ impl MailboxView { let flags = flags.iter().map(|x| x.to_string()).collect::>(); - let mails = self.get_mail_ids(sequence_set, *is_uid_store)?; + let mails = self.index().fetch(sequence_set, *is_uid_store)?; for mi in mails.iter() { match kind { StoreType::Add => { @@ -190,7 +190,7 @@ impl MailboxView { to: Arc, is_uid_copy: &bool, ) -> Result<(ImapUidvalidity, Vec<(ImapUid, ImapUid)>)> { - let mails = self.get_mail_ids(sequence_set, *is_uid_copy)?; + let mails = self.index().fetch(sequence_set, *is_uid_copy)?; let mut new_uuids = vec![]; for mi in mails.iter() { @@ -217,7 +217,7 @@ impl MailboxView { to: Arc, is_uid_copy: &bool, ) -> Result<(ImapUidvalidity, Vec<(ImapUid, ImapUid)>, Vec>)> { - let mails = self.get_mail_ids(sequence_set, *is_uid_copy)?; + let mails = self.index().fetch(sequence_set, *is_uid_copy)?; for mi in mails.iter() { to.move_from(&self.0.mailbox, mi.uuid).await?; @@ -255,16 +255,17 @@ impl MailboxView { true => QueryScope::Full, _ => QueryScope::Partial, }; - let mids = MailIdentifiersList(self.get_mail_ids(sequence_set, *is_uid_fetch)?); - let uuids = mids.uuids(); + let mail_idx_list = self.index().fetch(sequence_set, *is_uid_fetch)?; // [2/6] Fetch the emails + let uuids = mail_idx_list.iter().map(|midx| midx.uuid).collect::>(); let query = self.0.query(&uuids, query_scope); let query_result = query.fetch().await?; // [3/6] Derive an IMAP-specific view from the results, apply the filters let views = query_result.iter() - .map(MailView::new) + .zip(mail_idx_list.into_iter()) + .map(|(qr, midx)| MailView::new(qr, midx)) .collect::, _>>()?; // [4/6] Apply the IMAP transformation to keep only relevant fields @@ -296,9 +297,10 @@ impl MailboxView { pub async fn search<'a>( &self, _charset: &Option>, - search_key: &SearchKey<'a>, - uid: bool, + _search_key: &SearchKey<'a>, + _uid: bool, ) -> Result>> { + /* // 1. Compute the subset of sequence identifiers we need to fetch let query = search::Criteria(search_key); let (seq_set, seq_type) = query.to_sequence_set(); @@ -313,79 +315,15 @@ impl MailboxView { let _need_body = query.need_body(); Ok(vec![Body::Data(Data::Search(mail_u32))]) + */ + unimplemented!() } // ---- - - // Gets the IMAP ID, the IMAP UIDs and, the Aerogramme UUIDs of mails identified by a SequenceSet of - // sequence numbers (~ IMAP selector) - fn get_mail_ids( - &self, - sequence_set: &SequenceSet, - by_uid: bool, - ) -> Result> { - let mail_vec = self - .0 - .snapshot - .idx_by_uid - .iter() - .map(|(uid, uuid)| (*uid, *uuid)) - .collect::>(); - - let mut mails = vec![]; - - if by_uid { - if mail_vec.is_empty() { - return Ok(vec![]); - } - let iter_strat = sequence::Strategy::Naive { - largest: mail_vec.last().unwrap().0, - }; - - let mut i = 0; - for uid in sequence_set.iter(iter_strat) { - while mail_vec.get(i).map(|mail| mail.0 < uid).unwrap_or(false) { - i += 1; - } - if let Some(mail) = mail_vec.get(i) { - if mail.0 == uid { - mails.push(MailIdentifiers { - i: NonZeroU32::try_from(i as u32 + 1).unwrap(), - uid: mail.0, - uuid: mail.1, - }); - } - } else { - break; - } - } - } else { - if mail_vec.is_empty() { - bail!("No such message (mailbox is empty)"); - } - - let iter_strat = sequence::Strategy::Naive { - largest: NonZeroU32::try_from((mail_vec.len()) as u32).unwrap(), - }; - - for i in sequence_set.iter(iter_strat) { - if let Some(mail) = mail_vec.get(i.get() as usize - 1) { - mails.push(MailIdentifiers { - i, - uid: mail.0, - uuid: mail.1, - }); - } else { - bail!("No such mail: {}", i); - } - } - } - - Ok(mails) + fn index<'a>(&'a self) -> Index<'a> { + Index(&self.0.snapshot) } - // ---- - /// Produce an OK [UIDVALIDITY _] message corresponding to `known_state` fn uidvalidity_status(&self) -> Result> { let uid_validity = Status::ok( @@ -501,25 +439,6 @@ impl MailboxView { } } -pub struct MailIdentifiers { - pub i: NonZeroU32, - pub uid: ImapUid, - pub uuid: UniqueIdent, -} -pub struct MailIdentifiersList(Vec); - -impl MailIdentifiersList { - fn ids(&self) -> Vec { - self.0.iter().map(|mi| mi.i).collect() - } - fn uids(&self) -> Vec { - self.0.iter().map(|mi| mi.uid).collect() - } - fn uuids(&self) -> Vec { - self.0.iter().map(|mi| mi.uuid).collect() - } -} - #[cfg(test)] mod tests { use super::*; @@ -558,7 +477,7 @@ mod tests { message_key: key, rfc822_size: 8usize, }; - let ids = MailIdentifiers { + let ids = MailIndex { i: NonZeroU32::MIN, uid: NonZeroU32::MIN, uuid: unique_ident::gen_ident(), diff --git a/src/imap/mod.rs b/src/imap/mod.rs index ea34629..4142ef9 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -4,12 +4,12 @@ mod command; mod flags; mod flow; mod imf_view; +mod index; mod mail_view; mod mailbox_view; mod mime_view; mod response; mod search; -mod selectors; mod session; use std::net::SocketAddr; diff --git a/src/imap/selectors.rs b/src/imap/selectors.rs deleted file mode 100644 index 09320c3..0000000 --- a/src/imap/selectors.rs +++ /dev/null @@ -1,100 +0,0 @@ -use std::iter::zip; - -use anyhow::{anyhow, Result}; - -use crate::cryptoblob::Key; -use crate::imap::mail_view::{FetchedMail, MailView}; -use crate::imap::mailbox_view::MailIdentifiers; -use crate::mail::mailbox::MailMeta; -use crate::mail::unique_ident::UniqueIdent; - -pub struct BodyIdentifier<'a> { - pub msg_uuid: &'a UniqueIdent, - pub msg_key: &'a Key, -} - -#[derive(Default)] -pub struct MailSelectionBuilder<'a> { - //attrs: AttributeProxy, - mail_count: usize, - need_body: bool, - mi: &'a [MailIdentifiers], - meta: &'a [MailMeta], - flags: &'a [&'a Vec], - bodies: &'a [Vec], -} - -impl<'a> MailSelectionBuilder<'a> { - pub fn new(need_body: bool, mail_count: usize) -> Self { - Self { - mail_count, - need_body, - ..MailSelectionBuilder::default() - } - } - - pub fn with_mail_identifiers(&mut self, mi: &'a [MailIdentifiers]) -> &mut Self { - self.mi = mi; - self - } - - pub fn with_metadata(&mut self, meta: &'a [MailMeta]) -> &mut Self { - self.meta = meta; - self - } - - pub fn with_flags(&mut self, flags: &'a [&'a Vec]) -> &mut Self { - self.flags = flags; - self - } - - pub fn bodies_to_collect(&self) -> Vec { - if !self.need_body { - return vec![]; - } - zip(self.mi, self.meta) - .map(|(mi, meta)| BodyIdentifier { - msg_uuid: &mi.uuid, - msg_key: &meta.message_key, - }) - .collect::>() - } - - pub fn with_bodies(&mut self, rbodies: &'a [Vec]) -> &mut Self { - self.bodies = rbodies; - self - } - - pub fn build(&self) -> Result>> { - let mut bodies = vec![]; - - if !self.need_body { - for m in self.meta.iter() { - let (_, hdrs) = - eml_codec::parse_imf(&m.headers).or(Err(anyhow!("Invalid mail headers")))?; - bodies.push(FetchedMail::Partial(hdrs)); - } - } else { - for rb in self.bodies.iter() { - let (_, p) = eml_codec::parse_message(&rb).or(Err(anyhow!("Invalid mail body")))?; - bodies.push(FetchedMail::new_from_message(p)); - } - } - - if self.mi.len() != self.mail_count && self.meta.len() != self.mail_count - || self.flags.len() != self.mail_count - || bodies.len() != self.mail_count - { - return Err(anyhow!("Can't build a mail view selection as parts were not correctly registered into the builder.")); - } - - Ok(zip(self.mi, zip(self.meta, zip(self.flags, bodies))) - .map(|(ids, (meta, (flags, content)))| MailView { - ids, - meta, - flags, - content, - }) - .collect()) - } -} diff --git a/src/mail/query.rs b/src/mail/query.rs index 5beff37..70feb89 100644 --- a/src/mail/query.rs +++ b/src/mail/query.rs @@ -13,6 +13,7 @@ pub struct Query<'a,'b> { pub scope: QueryScope, } +#[allow(dead_code)] pub enum QueryScope { Index, Partial, @@ -106,6 +107,7 @@ impl<'a> QueryResult<'a> { } } + #[allow(dead_code)] pub fn index(&self) -> &IndexEntry { match self { Self::IndexResult { index, .. } => index, @@ -114,7 +116,7 @@ impl<'a> QueryResult<'a> { } } - pub fn metadata(&self) -> Option<&MailMeta> { + pub fn metadata(&'a self) -> Option<&'a MailMeta> { match self { Self::IndexResult { .. } => None, Self::PartialResult { metadata, .. } => Some(metadata), @@ -122,7 +124,8 @@ impl<'a> QueryResult<'a> { } } - pub fn content(&self) -> Option<&[u8]> { + #[allow(dead_code)] + pub fn content(&'a self) -> Option<&'a [u8]> { match self { Self::FullResult { content, .. } => Some(content), _ => None, -- cgit v1.2.3 From 1b64867ea3156424b55262f4a683cde0618e45f7 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 6 Jan 2024 11:14:55 +0100 Subject: Tests are fixed --- src/imap/mailbox_view.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 5bc6f87..d83fa3e 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -451,10 +451,12 @@ mod tests { use std::fs; use crate::cryptoblob; - use crate::imap::mail_view::{FetchedMail, MailView}; + use crate::imap::mail_view::MailView; use crate::imap::mime_view; + use crate::imap::index::MailIndex; use crate::mail::mailbox::MailMeta; use crate::mail::unique_ident; + use crate::mail::query::QueryResult; #[test] fn mailview_body_ext() -> Result<()> { @@ -469,7 +471,6 @@ mod tests { false, ); - let flags = vec![]; let key = cryptoblob::gen_key(); let meta = MailMeta { internaldate: 0u64, @@ -477,20 +478,23 @@ mod tests { message_key: key, rfc822_size: 8usize, }; - let ids = MailIndex { + + let index_entry = (NonZeroU32::MIN, vec![]); + let mail_in_idx = MailIndex { i: NonZeroU32::MIN, - uid: NonZeroU32::MIN, + uid: index_entry.0, uuid: unique_ident::gen_ident(), + flags: &index_entry.1, }; let rfc822 = b"Subject: hello\r\nFrom: a@a.a\r\nTo: b@b.b\r\nDate: Thu, 12 Oct 2023 08:45:28 +0000\r\n\r\nhello world"; - let content = FetchedMail::new_from_message(eml_codec::parse_message(rfc822)?.1); - - let mv = MailView { - ids: &ids, - content, - meta: &meta, - flags: &flags, + let qr = QueryResult::FullResult { + uuid: mail_in_idx.uuid.clone(), + index: &index_entry, + metadata: meta, + content: rfc822.to_vec(), }; + + let mv = MailView::new(&qr, mail_in_idx)?; let (res_body, _seen) = mv.filter(&ap)?; let fattr = match res_body { -- cgit v1.2.3 From 1ca6cd5de0656910213425e1d8f05256af820f21 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 6 Jan 2024 11:33:40 +0100 Subject: search is re-enabled --- src/imap/mailbox_view.rs | 57 +++++++++++++++++++++++++++++------------------- src/mail/query.rs | 8 +++---- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index d83fa3e..6a4724d 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -21,7 +21,7 @@ use crate::imap::attributes::AttributesProxy; use crate::imap::flags; use crate::imap::mail_view::{MailView, SeenFlag}; use crate::imap::response::Body; -//use crate::imap::search; +use crate::imap::search; use crate::imap::index::Index; @@ -259,8 +259,7 @@ impl MailboxView { // [2/6] Fetch the emails let uuids = mail_idx_list.iter().map(|midx| midx.uuid).collect::>(); - let query = self.0.query(&uuids, query_scope); - let query_result = query.fetch().await?; + let query_result = self.0.query(&uuids, query_scope).fetch().await?; // [3/6] Derive an IMAP-specific view from the results, apply the filters let views = query_result.iter() @@ -268,13 +267,18 @@ impl MailboxView { .map(|(qr, midx)| MailView::new(qr, midx)) .collect::, _>>()?; - // [4/6] Apply the IMAP transformation to keep only relevant fields + // [4/6] Apply the IMAP transformation, bubble up any error + // We get 2 results: + // - The one we send to the client + // - The \Seen flags we must set internally let (flag_mgmt, imap_ret): (Vec<_>, Vec<_>) = views .iter() - .filter_map(|mv| mv.filter(&ap).ok().map(|(body, seen)| ((mv, seen), body))) + .map(|mv| mv.filter(&ap).map(|(body, seen)| ((mv, seen), body))) + .collect::, _>>()? + .into_iter() .unzip(); - // [5/6] Register seen flags + // [5/6] Register the \Seen flags flag_mgmt .iter() .filter(|(_mv, seen)| matches!(seen, SeenFlag::MustAdd)) @@ -293,30 +297,37 @@ impl MailboxView { Ok(imap_ret) } - /// A very naive search implementation... + /// A naive search implementation... pub async fn search<'a>( &self, _charset: &Option>, - _search_key: &SearchKey<'a>, - _uid: bool, + search_key: &SearchKey<'a>, + uid: bool, ) -> Result>> { - /* // 1. Compute the subset of sequence identifiers we need to fetch - let query = search::Criteria(search_key); - let (seq_set, seq_type) = query.to_sequence_set(); - let mailids = MailIdentifiersList(self.get_mail_ids(&seq_set, seq_type.is_uid())?); - let mail_u32 = match uid { - true => mailids.uids(), - _ => mailids.ids(), - }; + // based on the search query + let crit = search::Criteria(search_key); + let (seq_set, seq_type) = crit.to_sequence_set(); + + // 2. Get the selection + let selection = self.index().fetch(&seq_set, seq_type.is_uid())?; + + // 3. Filter the selection based on the ID / UID / Flags - // 2. Compute wether we will need to fetch the mail meta and/or the body - let _need_meta = query.need_meta(); - let _need_body = query.need_body(); + // 4. If needed, filter the selection based on the metadata + let _need_meta = crit.need_meta(); + + // 5. If needed, filter the selection based on the body + let _need_body = crit.need_body(); + + // 6. Format the result according to the client's taste: + // either return UID or ID. + let selection_fmt = match uid { + true => selection.into_iter().map(|in_idx| in_idx.uid).collect(), + _ => selection.into_iter().map(|in_idx| in_idx.i).collect(), + }; - Ok(vec![Body::Data(Data::Search(mail_u32))]) - */ - unimplemented!() + Ok(vec![Body::Data(Data::Search(selection_fmt))]) } // ---- diff --git a/src/mail/query.rs b/src/mail/query.rs index 70feb89..7b26cb9 100644 --- a/src/mail/query.rs +++ b/src/mail/query.rs @@ -21,7 +21,7 @@ pub enum QueryScope { } impl<'a,'b> Query<'a,'b> { - pub async fn fetch(&self) -> Result> { + pub async fn fetch(&self) -> Result>> { match self.scope { QueryScope::Index => self.index(), QueryScope::Partial => self.partial().await, @@ -31,7 +31,7 @@ impl<'a,'b> Query<'a,'b> { // --- functions below are private *for reasons* - fn index(&self) -> Result> { + fn index(&self) -> Result>> { self .emails .iter() @@ -47,7 +47,7 @@ impl<'a,'b> Query<'a,'b> { .collect::, _>>() } - async fn partial(&self) -> Result> { + async fn partial(&self) -> Result>> { let meta = self.frozen.mailbox.fetch_meta(self.emails).await?; let result = meta .into_iter() @@ -61,7 +61,7 @@ impl<'a,'b> Query<'a,'b> { /// AND GENERATE SO MUCH NETWORK TRAFFIC. /// THIS FUNCTION SHOULD BE REWRITTEN, FOR EXAMPLE WITH /// SOMETHING LIKE AN ITERATOR - async fn full(&self) -> Result> { + async fn full(&self) -> Result>> { let meta_list = self.partial().await?; meta_list .into_iter() -- cgit v1.2.3 From 53dbf82cbce3cb17cbcffd09558677faf8702f54 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 6 Jan 2024 11:33:56 +0100 Subject: Format code again --- src/imap/index.rs | 25 ++++++++++++++++----- src/imap/mail_view.rs | 40 +++++++++++++++++++++++++--------- src/imap/mailbox_view.rs | 32 ++++++++++++++++----------- src/imap/search.rs | 3 ++- src/mail/mod.rs | 2 +- src/mail/query.rs | 56 +++++++++++++++++++++++++++++++++--------------- src/mail/snapshot.rs | 16 +++++++------- 7 files changed, 120 insertions(+), 54 deletions(-) diff --git a/src/imap/index.rs b/src/imap/index.rs index 347222c..01dd2ef 100644 --- a/src/imap/index.rs +++ b/src/imap/index.rs @@ -8,7 +8,11 @@ use crate::mail::unique_ident::UniqueIdent; pub struct Index<'a>(pub &'a UidIndex); impl<'a> Index<'a> { - pub fn fetch(self: &Index<'a>, sequence_set: &SequenceSet, by_uid: bool) -> Result>> { + pub fn fetch( + self: &Index<'a>, + sequence_set: &SequenceSet, + by_uid: bool, + ) -> Result>> { let mail_vec = self .0 .idx_by_uid @@ -37,7 +41,13 @@ impl<'a> Index<'a> { i: NonZeroU32::try_from(i as u32 + 1).unwrap(), uid: mail.0, uuid: mail.1, - flags: self.0.table.get(&mail.1).ok_or(anyhow!("mail is missing from index"))?.1.as_ref(), + flags: self + .0 + .table + .get(&mail.1) + .ok_or(anyhow!("mail is missing from index"))? + .1 + .as_ref(), }); } } else { @@ -59,7 +69,13 @@ impl<'a> Index<'a> { i, uid: mail.0, uuid: mail.1, - flags: self.0.table.get(&mail.1).ok_or(anyhow!("mail is missing from index"))?.1.as_ref(), + flags: self + .0 + .table + .get(&mail.1) + .ok_or(anyhow!("mail is missing from index"))? + .1 + .as_ref(), }); } else { bail!("No such mail: {}", i); @@ -68,7 +84,6 @@ impl<'a> Index<'a> { } Ok(mails) - } } @@ -76,5 +91,5 @@ pub struct MailIndex<'a> { pub i: NonZeroU32, pub uid: ImapUid, pub uuid: UniqueIdent, - pub flags: &'a Vec + pub flags: &'a Vec, } diff --git a/src/imap/mail_view.rs b/src/imap/mail_view.rs index 1f87f02..de9bfe3 100644 --- a/src/imap/mail_view.rs +++ b/src/imap/mail_view.rs @@ -16,15 +16,14 @@ use eml_codec::{ part::{composite::Message, AnyPart}, }; - use crate::mail::query::QueryResult; use crate::imap::attributes::AttributesProxy; use crate::imap::flags; use crate::imap::imf_view::message_envelope; +use crate::imap::index::MailIndex; use crate::imap::mime_view; use crate::imap::response::Body; -use crate::imap::index::MailIndex; pub struct MailView<'a> { pub in_idx: MailIndex<'a>, @@ -39,18 +38,20 @@ impl<'a> MailView<'a> { query_result, content: match query_result { QueryResult::FullResult { content, .. } => { - let (_, parsed) = eml_codec::parse_message(&content).or(Err(anyhow!("Invalid mail body")))?; + let (_, parsed) = + eml_codec::parse_message(&content).or(Err(anyhow!("Invalid mail body")))?; FetchedMail::new_from_message(parsed) - }, + } QueryResult::PartialResult { metadata, .. } => { - let (_, parsed) = eml_codec::parse_imf(&metadata.headers).or(Err(anyhow!("unable to parse email headers")))?; + let (_, parsed) = eml_codec::parse_imf(&metadata.headers) + .or(Err(anyhow!("unable to parse email headers")))?; FetchedMail::Partial(parsed) } QueryResult::IndexResult { .. } => FetchedMail::IndexOnly, - } + }, }) } - + fn uid(&self) -> MessageDataItem<'static> { MessageDataItem::Uid(self.in_idx.uid.clone()) } @@ -66,12 +67,22 @@ impl<'a> MailView<'a> { } fn rfc_822_size(&self) -> Result> { - let sz = self.query_result.metadata().ok_or(anyhow!("mail metadata are required"))?.rfc822_size; + let sz = self + .query_result + .metadata() + .ok_or(anyhow!("mail metadata are required"))? + .rfc822_size; Ok(MessageDataItem::Rfc822Size(sz as u32)) } fn rfc_822_header(&self) -> Result> { - let hdrs: NString = self.query_result.metadata().ok_or(anyhow!("mail metadata are required"))?.headers.to_vec().try_into()?; + let hdrs: NString = self + .query_result + .metadata() + .ok_or(anyhow!("mail metadata are required"))? + .headers + .to_vec() + .try_into()?; Ok(MessageDataItem::Rfc822Header(hdrs)) } @@ -142,7 +153,16 @@ impl<'a> MailView<'a> { fn internal_date(&self) -> Result> { let dt = Utc .fix() - .timestamp_opt(i64::try_from(self.query_result.metadata().ok_or(anyhow!("mail metadata were not fetched"))?.internaldate / 1000)?, 0) + .timestamp_opt( + i64::try_from( + self.query_result + .metadata() + .ok_or(anyhow!("mail metadata were not fetched"))? + .internaldate + / 1000, + )?, + 0, + ) .earliest() .ok_or(anyhow!("Unable to parse internal date"))?; Ok(MessageDataItem::InternalDate(DateTime::unvalidated(dt))) diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 6a4724d..e4ffdcd 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -13,17 +13,16 @@ use imap_codec::imap_types::search::SearchKey; use imap_codec::imap_types::sequence::SequenceSet; use crate::mail::mailbox::Mailbox; -use crate::mail::snapshot::FrozenMailbox; use crate::mail::query::QueryScope; +use crate::mail::snapshot::FrozenMailbox; use crate::mail::uidindex::{ImapUid, ImapUidvalidity}; use crate::imap::attributes::AttributesProxy; use crate::imap::flags; +use crate::imap::index::Index; use crate::imap::mail_view::{MailView, SeenFlag}; use crate::imap::response::Body; use crate::imap::search; -use crate::imap::index::Index; - const DEFAULT_FLAGS: [Flag; 5] = [ Flag::Seen, @@ -40,7 +39,7 @@ const DEFAULT_FLAGS: [Flag; 5] = [ /// To do this, it keeps a variable `known_state` that corresponds to /// what the client knows, and produces IMAP messages to be sent to the /// client that go along updates to `known_state`. -pub struct MailboxView (pub FrozenMailbox); +pub struct MailboxView(pub FrozenMailbox); impl MailboxView { /// Creates a new IMAP view into a mailbox. @@ -258,11 +257,15 @@ impl MailboxView { let mail_idx_list = self.index().fetch(sequence_set, *is_uid_fetch)?; // [2/6] Fetch the emails - let uuids = mail_idx_list.iter().map(|midx| midx.uuid).collect::>(); + let uuids = mail_idx_list + .iter() + .map(|midx| midx.uuid) + .collect::>(); let query_result = self.0.query(&uuids, query_scope).fetch().await?; - + // [3/6] Derive an IMAP-specific view from the results, apply the filters - let views = query_result.iter() + let views = query_result + .iter() .zip(mail_idx_list.into_iter()) .map(|(qr, midx)| MailView::new(qr, midx)) .collect::, _>>()?; @@ -284,7 +287,10 @@ impl MailboxView { .filter(|(_mv, seen)| matches!(seen, SeenFlag::MustAdd)) .map(|(mv, _seen)| async move { let seen_flag = Flag::Seen.to_string(); - self.0.mailbox.add_flags(*mv.query_result.uuid(), &[seen_flag]).await?; + self.0 + .mailbox + .add_flags(*mv.query_result.uuid(), &[seen_flag]) + .await?; Ok::<_, anyhow::Error>(()) }) .collect::>() @@ -399,7 +405,8 @@ impl MailboxView { // 1. Collecting all the possible flags in the mailbox // 1.a Fetch them from our index - let mut known_flags: Vec = self.0 + let mut known_flags: Vec = self + .0 .snapshot .idx_by_flag .flags() @@ -440,7 +447,8 @@ impl MailboxView { pub(crate) fn unseen_count(&self) -> usize { let total = self.0.snapshot.table.len(); - let seen = self.0 + let seen = self + .0 .snapshot .idx_by_flag .get(&Flag::Seen.to_string()) @@ -462,12 +470,12 @@ mod tests { use std::fs; use crate::cryptoblob; + use crate::imap::index::MailIndex; use crate::imap::mail_view::MailView; use crate::imap::mime_view; - use crate::imap::index::MailIndex; use crate::mail::mailbox::MailMeta; - use crate::mail::unique_ident; use crate::mail::query::QueryResult; + use crate::mail::unique_ident; #[test] fn mailview_body_ext() -> Result<()> { diff --git a/src/imap/search.rs b/src/imap/search.rs index ef89288..b3c6b05 100644 --- a/src/imap/search.rs +++ b/src/imap/search.rs @@ -76,7 +76,8 @@ impl<'a> Criteria<'a> { use SearchKey::*; match self.0 { // IMF Headers - Bcc(_) | Cc(_) | From(_) | Header(..) | SentBefore(_) | SentOn(_) | SentSince(_) | Subject(_) | To(_) => true, + Bcc(_) | Cc(_) | From(_) | Header(..) | SentBefore(_) | SentOn(_) | SentSince(_) + | Subject(_) | To(_) => true, // Internal Date is also stored in MailMeta Before(_) | On(_) | Since(_) => true, // Message size is also stored in MailMeta diff --git a/src/mail/mod.rs b/src/mail/mod.rs index 7371b53..1836052 100644 --- a/src/mail/mod.rs +++ b/src/mail/mod.rs @@ -3,8 +3,8 @@ use std::io::Write; pub mod incoming; pub mod mailbox; -pub mod snapshot; pub mod query; +pub mod snapshot; pub mod uidindex; pub mod unique_ident; pub mod user; diff --git a/src/mail/query.rs b/src/mail/query.rs index 7b26cb9..8de73e6 100644 --- a/src/mail/query.rs +++ b/src/mail/query.rs @@ -1,13 +1,13 @@ -use anyhow::{Result, anyhow}; use super::mailbox::MailMeta; use super::snapshot::FrozenMailbox; -use super::unique_ident::UniqueIdent; use super::uidindex::IndexEntry; +use super::unique_ident::UniqueIdent; +use anyhow::{anyhow, Result}; use futures::stream::{FuturesUnordered, StreamExt}; /// Query is in charge of fetching efficiently /// requested data for a list of emails -pub struct Query<'a,'b> { +pub struct Query<'a, 'b> { pub frozen: &'a FrozenMailbox, pub emails: &'b [UniqueIdent], pub scope: QueryScope, @@ -20,7 +20,7 @@ pub enum QueryScope { Full, } -impl<'a,'b> Query<'a,'b> { +impl<'a, 'b> Query<'a, 'b> { pub async fn fetch(&self) -> Result>> { match self.scope { QueryScope::Index => self.index(), @@ -32,12 +32,10 @@ impl<'a,'b> Query<'a,'b> { // --- functions below are private *for reasons* fn index(&self) -> Result>> { - self - .emails + self.emails .iter() .map(|uuid| { - self - .frozen + self.frozen .snapshot .table .get(uuid) @@ -52,7 +50,11 @@ impl<'a,'b> Query<'a,'b> { let result = meta .into_iter() .zip(self.index()?) - .map(|(metadata, index)| index.into_partial(metadata).expect("index to be IndexResult")) + .map(|(metadata, index)| { + index + .into_partial(metadata) + .expect("index to be IndexResult") + }) .collect::>(); Ok(result) } @@ -65,11 +67,18 @@ impl<'a,'b> Query<'a,'b> { let meta_list = self.partial().await?; meta_list .into_iter() - .map(|meta| async move { - let content = self.frozen.mailbox.fetch_full( - *meta.uuid(), - &meta.metadata().expect("meta to be PartialResult").message_key - ).await?; + .map(|meta| async move { + let content = self + .frozen + .mailbox + .fetch_full( + *meta.uuid(), + &meta + .metadata() + .expect("meta to be PartialResult") + .message_key, + ) + .await?; Ok(meta.into_full(content).expect("meta to be PartialResult")) }) @@ -96,7 +105,7 @@ pub enum QueryResult<'a> { index: &'a IndexEntry, metadata: MailMeta, content: Vec, - } + }, } impl<'a> QueryResult<'a> { pub fn uuid(&self) -> &UniqueIdent { @@ -134,14 +143,27 @@ impl<'a> QueryResult<'a> { fn into_partial(self, metadata: MailMeta) -> Option { match self { - Self::IndexResult { uuid, index } => Some(Self::PartialResult { uuid, index, metadata }), + Self::IndexResult { uuid, index } => Some(Self::PartialResult { + uuid, + index, + metadata, + }), _ => None, } } fn into_full(self, content: Vec) -> Option { match self { - Self::PartialResult { uuid, index, metadata } => Some(Self::FullResult { uuid, index, metadata, content }), + Self::PartialResult { + uuid, + index, + metadata, + } => Some(Self::FullResult { + uuid, + index, + metadata, + content, + }), _ => None, } } diff --git a/src/mail/snapshot.rs b/src/mail/snapshot.rs index c3145b4..0834f09 100644 --- a/src/mail/snapshot.rs +++ b/src/mail/snapshot.rs @@ -3,16 +3,16 @@ use std::sync::Arc; use anyhow::Result; use super::mailbox::Mailbox; +use super::query::{Query, QueryScope}; use super::uidindex::UidIndex; use super::unique_ident::UniqueIdent; -use super::query::{Query, QueryScope}; /// A Frozen Mailbox has a snapshot of the current mailbox /// state that is desynchronized with the real mailbox state. /// It's up to the user to choose when their snapshot must be updated /// to give useful information to their clients /// -/// +/// pub struct FrozenMailbox { pub mailbox: Arc, pub snapshot: UidIndex, @@ -46,17 +46,17 @@ impl FrozenMailbox { /// Update the FrozenMailbox local snapshot. /// Returns the old snapshot, so you can build a diff pub async fn update(&mut self) -> UidIndex { - let old_snapshot = self.snapshot.clone(); - self.snapshot = self.mailbox.current_uid_index().await; + let old_snapshot = self.snapshot.clone(); + self.snapshot = self.mailbox.current_uid_index().await; - old_snapshot + old_snapshot } pub fn query<'a, 'b>(&'a self, uuids: &'b [UniqueIdent], scope: QueryScope) -> Query<'a, 'b> { Query { - frozen: self, - emails: uuids, - scope, + frozen: self, + emails: uuids, + scope, } } } -- cgit v1.2.3