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 (limited to 'src') 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