Aerogramme refactoring #57

Merged
quentin merged 16 commits from feat/more-imap-qol into main 2024-01-06 10:38:37 +00:00
9 changed files with 129 additions and 120 deletions
Showing only changes of commit ac8fb89d56 - Show all commits

View file

@ -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

View file

@ -95,4 +95,3 @@ pub fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address<'static> {
)),
}
}

View file

@ -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> {
}
}
}

View file

@ -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::flags;
use crate::imap::response::Body;
use crate::imap::attributes::AttributesProxy;
use crate::imap::selectors::MailSelectionBuilder;
use crate::imap::flags;
use crate::imap::mail_view::SeenFlag;
use crate::imap::response::Body;
use crate::imap::selectors::MailSelectionBuilder;
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(())
}
}

View file

@ -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<FetchSection<'a>>,
partial: &'a Option<(u32, NonZeroU32)>
part: &'a AnyPart<'a>,
section: &'a Option<FetchSection<'a>>,
partial: &'a Option<(u32, NonZeroU32)>,
) -> Result<BodySection<'a>> {
let root_mime = NodeMime(part);
let (extractor, path) = SubsettedSection::from(section);
@ -88,12 +83,12 @@ pub fn bodystructure(part: &AnyPart) -> Result<BodyStructure<'static>> {
}
/// 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<SelectedMime<'a>> {
@ -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<AString<'a>>, invert: bool) -> Result<ExtractedFull<'a>> {
fn header_fields(
&self,
fields: &'a NonEmptyVec<AString<'a>>,
invert: bool,
) -> Result<ExtractedFull<'a>> {
// 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::<HashSet<_>>();
.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::<HashSet<_>>();
// 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<ExtractedFull<'a>> {
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<ExtractedFull<'a>> {
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<BodyStructure<'static>> {
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<BodyStructure<'static>> {
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<BodyStructure<'static>> {
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,
}

View file

@ -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;

View file

@ -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())
}
}

View file

@ -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");
}

View file

@ -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<Enable>) -> Result
Some(Enable::Utf8Accept) => {
assert_eq!(srv_msg.lines().count(), 2);
assert!(srv_msg.contains("* ENABLED UTF8=ACCEPT"));
},
}
_ => unimplemented!(),
}