aerogramme/src/imap/mailbox_view.rs

670 lines
23 KiB
Rust
Raw Normal View History

2022-06-29 15:58:31 +00:00
use std::num::NonZeroU32;
use std::sync::Arc;
2022-06-29 17:24:21 +00:00
use anyhow::{anyhow, bail, Error, Result};
2023-10-09 10:00:16 +00:00
2022-06-29 17:24:21 +00:00
use futures::stream::{FuturesOrdered, StreamExt};
2023-10-09 10:00:16 +00:00
2024-01-05 09:05:30 +00:00
use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItem};
2024-01-02 14:35:23 +00:00
use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType};
use imap_codec::imap_types::response::{Code, Data, Status};
2024-01-01 08:34:13 +00:00
use imap_codec::imap_types::sequence::{self, SequenceSet};
2023-10-09 10:00:16 +00:00
2024-01-05 09:05:30 +00:00
use crate::imap::attributes::AttributesProxy;
2024-01-04 19:54:21 +00:00
use crate::imap::flags;
2024-01-05 09:05:30 +00:00
use crate::imap::mail_view::SeenFlag;
2024-01-02 14:35:23 +00:00
use crate::imap::response::Body;
2024-01-04 19:54:21 +00:00
use crate::imap::selectors::MailSelectionBuilder;
use crate::mail::mailbox::Mailbox;
2022-07-12 13:59:13 +00:00
use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex};
2022-07-12 15:32:57 +00:00
use crate::mail::unique_ident::UniqueIdent;
const DEFAULT_FLAGS: [Flag; 5] = [
Flag::Seen,
Flag::Answered,
Flag::Flagged,
Flag::Deleted,
Flag::Draft,
];
/// 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
/// of the mailbox has been changed.
/// 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 {
2022-07-12 14:35:11 +00:00
pub(crate) mailbox: Arc<Mailbox>,
known_state: UidIndex,
}
impl MailboxView {
/// Creates a new IMAP view into a mailbox.
2024-01-02 14:35:23 +00:00
pub async fn new(mailbox: Arc<Mailbox>) -> Self {
let state = mailbox.current_uid_index().await;
2024-01-02 14:35:23 +00:00
Self {
mailbox,
known_state: state,
2024-01-02 14:35:23 +00:00
}
}
2024-01-02 14:35:23 +00:00
/// Create an updated view, useful to make a diff
/// between what the client knows and new stuff
/// Produces a set of IMAP responses describing the change between
/// what the client knows and what is actually in the mailbox.
/// This does NOT trigger a sync, it bases itself on what is currently
/// loaded in RAM by Bayou.
2024-01-02 14:35:23 +00:00
pub async fn update(&mut self) -> Result<Vec<Body<'static>>> {
let old_view: &mut Self = self;
let new_view = Self {
mailbox: old_view.mailbox.clone(),
known_state: old_view.mailbox.current_uid_index().await,
2022-06-29 15:58:31 +00:00
};
let mut data = Vec::<Body>::new();
2022-07-13 09:39:13 +00:00
// Calculate diff between two mailbox states
// See example in IMAP RFC in section on NOOP command:
// we want to produce something like this:
// C: a047 NOOP
// S: * 22 EXPUNGE
// S: * 23 EXISTS
// S: * 14 FETCH (UID 1305 FLAGS (\Seen \Deleted))
// S: a047 OK Noop completed
// In other words:
// - notify client of expunged mails
// - if new mails arrived, notify client of number of existing mails
// - if flags changed for existing mails, tell client
// (for this last step: if uidvalidity changed, do nothing,
// just notify of new uidvalidity and they will resync)
// - notify client of expunged mails
let mut n_expunge = 0;
2024-01-02 14:35:23 +00:00
for (i, (_uid, uuid)) in old_view.known_state.idx_by_uid.iter().enumerate() {
2022-07-13 09:39:13 +00:00
if !new_view.known_state.table.contains_key(uuid) {
data.push(Body::Data(Data::Expunge(
NonZeroU32::try_from((i + 1 - n_expunge) as u32).unwrap(),
)));
n_expunge += 1;
}
}
// - if new mails arrived, notify client of number of existing mails
2024-01-02 14:35:23 +00:00
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
2022-07-13 09:39:13 +00:00
{
data.push(new_view.exists_status()?);
}
2024-01-02 14:35:23 +00:00
if new_view.known_state.uidvalidity != old_view.known_state.uidvalidity {
2022-06-29 15:58:31 +00:00
// TODO: do we want to push less/more info than this?
2022-07-12 13:59:13 +00:00
data.push(new_view.uidvalidity_status()?);
data.push(new_view.uidnext_status()?);
2022-06-29 15:58:31 +00:00
} else {
// - if flags changed for existing mails, tell client
2022-06-29 17:27:32 +00:00
for (i, (_uid, uuid)) in new_view.known_state.idx_by_uid.iter().enumerate() {
2024-01-02 14:35:23 +00:00
let old_mail = old_view.known_state.table.get(uuid);
2022-06-29 15:58:31 +00:00
let new_mail = new_view.known_state.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 {
2024-01-02 14:35:23 +00:00
seq: NonZeroU32::try_from((i + 1) as u32).unwrap(),
items: vec![
MessageDataItem::Uid(*uid),
MessageDataItem::Flags(
2024-01-04 19:54:21 +00:00
flags.iter().filter_map(|f| flags::from_str(f)).collect(),
2022-06-29 15:58:31 +00:00
),
2024-01-02 14:35:23 +00:00
]
.try_into()?,
2022-06-29 15:58:31 +00:00
}));
}
}
}
}
2024-01-02 14:35:23 +00:00
*old_view = new_view;
Ok(data)
}
/// Generates the necessary IMAP messages so that the client
/// has a satisfactory summary of the current mailbox's state.
/// These are the messages that are sent in response to a SELECT command.
pub fn summary(&self) -> Result<Vec<Body<'static>>> {
let mut data = Vec::<Body>::new();
data.push(self.exists_status()?);
data.push(self.recent_status()?);
data.extend(self.flags_status()?.into_iter());
data.push(self.uidvalidity_status()?);
data.push(self.uidnext_status()?);
2022-06-29 15:58:31 +00:00
Ok(data)
}
2024-01-02 14:35:23 +00:00
pub async fn store<'a>(
2022-07-12 15:32:57 +00:00
&mut self,
sequence_set: &SequenceSet,
kind: &StoreType,
_response: &StoreResponse,
2024-01-02 14:35:23 +00:00
flags: &[Flag<'a>],
is_uid_store: &bool,
2024-01-02 14:35:23 +00:00
) -> Result<Vec<Body<'static>>> {
self.mailbox.opportunistic_sync().await?;
2022-07-12 15:32:57 +00:00
let flags = flags.iter().map(|x| x.to_string()).collect::<Vec<_>>();
let mails = self.get_mail_ids(sequence_set, *is_uid_store)?;
2023-10-10 15:59:34 +00:00
for mi in mails.iter() {
2022-07-12 15:32:57 +00:00
match kind {
StoreType::Add => {
2023-10-10 15:59:34 +00:00
self.mailbox.add_flags(mi.uuid, &flags[..]).await?;
2022-07-12 15:32:57 +00:00
}
StoreType::Remove => {
2023-10-10 15:59:34 +00:00
self.mailbox.del_flags(mi.uuid, &flags[..]).await?;
2022-07-12 15:32:57 +00:00
}
StoreType::Replace => {
2023-10-10 15:59:34 +00:00
self.mailbox.set_flags(mi.uuid, &flags[..]).await?;
2022-07-12 15:32:57 +00:00
}
}
}
2022-07-21 10:44:58 +00:00
// @TODO: handle _response
2022-07-12 15:32:57 +00:00
self.update().await
}
2024-01-02 14:35:23 +00:00
pub async fn expunge(&mut self) -> Result<Vec<Body<'static>>> {
self.mailbox.opportunistic_sync().await?;
2022-07-13 09:19:08 +00:00
let deleted_flag = Flag::Deleted.to_string();
let state = self.mailbox.current_uid_index().await;
let msgs = state
2022-07-13 09:19:08 +00:00
.table
.iter()
.filter(|(_uuid, (_uid, flags))| flags.iter().any(|x| *x == deleted_flag))
.map(|(uuid, _)| *uuid);
for msg in msgs {
self.mailbox.delete(msg).await?;
}
self.update().await
2022-07-13 09:00:35 +00:00
}
2022-07-21 10:44:58 +00:00
pub async fn copy(
&self,
sequence_set: &SequenceSet,
to: Arc<Mailbox>,
is_uid_copy: &bool,
) -> Result<(ImapUidvalidity, Vec<(ImapUid, ImapUid)>)> {
let mails = self.get_mail_ids(sequence_set, *is_uid_copy)?;
let mut new_uuids = vec![];
2023-10-10 15:59:34 +00:00
for mi in mails.iter() {
new_uuids.push(to.copy_from(&self.mailbox, mi.uuid).await?);
2022-07-21 10:44:58 +00:00
}
let mut ret = vec![];
let to_state = to.current_uid_index().await;
2023-10-10 15:59:34 +00:00
for (mi, new_uuid) in mails.iter().zip(new_uuids.iter()) {
2022-07-21 10:44:58 +00:00
let dest_uid = to_state
.table
.get(new_uuid)
.ok_or(anyhow!("copied mail not in destination mailbox"))?
.0;
2023-10-10 15:59:34 +00:00
ret.push((mi.uid, dest_uid));
2022-07-21 10:44:58 +00:00
}
Ok((to_state.uidvalidity, ret))
}
2024-01-03 14:00:05 +00:00
pub async fn r#move(
&mut self,
sequence_set: &SequenceSet,
to: Arc<Mailbox>,
is_uid_copy: &bool,
) -> Result<(ImapUidvalidity, Vec<(ImapUid, ImapUid)>, Vec<Body<'static>>)> {
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?
}
let mut ret = vec![];
let to_state = to.current_uid_index().await;
for (mi, new_uuid) in mails.iter().zip(new_uuids.iter()) {
let dest_uid = to_state
.table
.get(new_uuid)
.ok_or(anyhow!("moved mail not in destination mailbox"))?
.0;
ret.push((mi.uid, dest_uid));
}
let update = self.update().await?;
Ok((to_state.uidvalidity, ret, update))
}
2022-06-29 17:24:21 +00:00
/// Looks up state changes in the mailbox and produces a set of IMAP
/// responses describing the new state.
2024-01-02 14:35:23 +00:00
pub async fn fetch<'b>(
2022-06-29 17:24:21 +00:00
&self,
sequence_set: &SequenceSet,
2024-01-02 19:23:33 +00:00
attributes: &'b MacroOrMessageDataItemNames<'static>,
is_uid_fetch: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<Vec<Body<'static>>> {
2023-10-09 10:00:16 +00:00
let ap = AttributesProxy::new(attributes, *is_uid_fetch);
2022-06-29 17:24:21 +00:00
2023-10-10 15:59:34 +00:00
// Prepare data
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?;
2023-10-12 10:21:59 +00:00
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::<Result<Vec<_>, _>>()?;
2023-10-10 15:59:34 +00:00
// Start filling data to build the view
let mut selection = MailSelectionBuilder::new(ap.need_body(), mail_count);
selection
2023-10-12 10:21:59 +00:00
.with_mail_identifiers(&mids.0)
.with_metadata(&meta)
.with_flags(&flags);
2023-10-09 10:00:16 +00:00
// Asynchronously fetch full bodies (if needed)
2023-10-10 15:59:34 +00:00
let btc = selection.bodies_to_collect();
2023-10-12 10:21:59 +00:00
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::<FuturesOrdered<_>>();
let bodies = future_bodies
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
2023-10-09 10:00:16 +00:00
2023-10-10 15:59:34 +00:00
// Add bodies
selection.with_bodies(bodies.as_slice());
2023-10-09 10:00:16 +00:00
// Build mail selection views
2024-01-02 14:35:23 +00:00
let views = selection.build()?;
2023-10-09 10:00:16 +00:00
// Filter views to build the result
2024-01-02 14:35:23 +00:00
// Also identify what must be put as seen
let filtered_view = views
.iter()
.filter_map(|mv| mv.filter(&ap).ok().map(|(body, seen)| (mv, body, seen)))
2023-10-12 10:21:59 +00:00
.collect::<Vec<_>>();
2023-10-09 10:00:16 +00:00
// Register seen flags
2024-01-02 14:35:23 +00:00
let future_flags = filtered_view
2023-10-12 10:21:59 +00:00
.iter()
2024-01-02 14:35:23 +00:00
.filter(|(_mv, _body, seen)| matches!(seen, SeenFlag::MustAdd))
.map(|(mv, _body, _seen)| async move {
2023-10-12 10:21:59 +00:00
let seen_flag = Flag::Seen.to_string();
self.mailbox.add_flags(mv.ids.uuid, &[seen_flag]).await?;
Ok::<_, anyhow::Error>(())
})
.collect::<FuturesOrdered<_>>();
2024-01-02 14:35:23 +00:00
2023-10-12 10:21:59 +00:00
future_flags
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<_, _>>()?;
2023-10-09 10:00:16 +00:00
2024-01-02 14:35:23 +00:00
let command_body = filtered_view
.into_iter()
.map(|(_mv, body, _seen)| body)
.collect::<Vec<_>>();
Ok(command_body)
2022-06-29 17:24:21 +00:00
}
// ----
2023-09-28 09:57:46 +00:00
// Gets the IMAP ID, the IMAP UIDs and, the Aerogramme UUIDs of mails identified by a SequenceSet of
// sequence numbers (~ IMAP selector)
2022-07-12 15:32:57 +00:00
fn get_mail_ids(
&self,
sequence_set: &SequenceSet,
by_uid: bool,
2023-10-09 10:00:16 +00:00
) -> Result<Vec<MailIdentifiers>> {
2022-07-12 15:32:57 +00:00
let mail_vec = self
.known_state
.idx_by_uid
.iter()
.map(|(uid, uuid)| (*uid, *uuid))
.collect::<Vec<_>>();
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 {
2023-10-12 10:21:59 +00:00
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) {
2023-10-12 10:21:59 +00:00
mails.push(MailIdentifiers {
i,
uid: mail.0,
uuid: mail.1,
});
} else {
bail!("No such mail: {}", i);
}
2022-07-12 15:32:57 +00:00
}
}
Ok(mails)
}
// ----
/// Produce an OK [UIDVALIDITY _] message corresponding to `known_state`
2024-01-02 14:35:23 +00:00
fn uidvalidity_status(&self) -> Result<Body<'static>> {
let uid_validity = Status::ok(
None,
2022-07-12 13:59:13 +00:00
Some(Code::UidValidity(self.uidvalidity())),
"UIDs valid",
)
.map_err(Error::msg)?;
Ok(Body::Status(uid_validity))
}
2022-07-12 13:59:13 +00:00
pub(crate) fn uidvalidity(&self) -> ImapUidvalidity {
self.known_state.uidvalidity
}
/// Produce an OK [UIDNEXT _] message corresponding to `known_state`
2024-01-02 14:35:23 +00:00
fn uidnext_status(&self) -> Result<Body<'static>> {
let next_uid = Status::ok(
None,
2022-07-12 13:59:13 +00:00
Some(Code::UidNext(self.uidnext())),
"Predict next UID",
)
.map_err(Error::msg)?;
Ok(Body::Status(next_uid))
}
2022-07-12 13:59:13 +00:00
pub(crate) fn uidnext(&self) -> ImapUid {
self.known_state.uidnext
}
/// Produce an EXISTS message corresponding to the number of mails
/// in `known_state`
2024-01-02 14:35:23 +00:00
fn exists_status(&self) -> Result<Body<'static>> {
2022-07-12 13:59:13 +00:00
Ok(Body::Data(Data::Exists(self.exists()?)))
}
pub(crate) fn exists(&self) -> Result<u32> {
Ok(u32::try_from(self.known_state.idx_by_uid.len())?)
}
/// Produce a RECENT message corresponding to the number of
/// recent mails in `known_state`
2024-01-02 14:35:23 +00:00
fn recent_status(&self) -> Result<Body<'static>> {
2022-07-12 13:59:13 +00:00
Ok(Body::Data(Data::Recent(self.recent()?)))
}
pub(crate) fn recent(&self) -> Result<u32> {
let recent = self
.known_state
.idx_by_flag
.get(&"\\Recent".to_string())
.map(|os| os.len())
.unwrap_or(0);
2022-07-12 13:59:13 +00:00
Ok(u32::try_from(recent)?)
}
/// Produce a FLAGS and a PERMANENTFLAGS message that indicates
/// the flags that are in `known_state` + default flags
2024-01-02 14:35:23 +00:00
fn flags_status(&self) -> Result<Vec<Body<'static>>> {
let mut body = vec![];
// 1. Collecting all the possible flags in the mailbox
// 1.a Fetch them from our index
let mut known_flags: Vec<Flag> = self
.known_state
.idx_by_flag
.flags()
2024-01-04 19:54:21 +00:00
.filter_map(|f| match flags::from_str(f) {
2024-01-02 14:35:23 +00:00
Some(FlagFetch::Flag(fl)) => Some(fl),
_ => None,
})
.collect();
2024-01-02 14:35:23 +00:00
// 1.b Merge it with our default flags list
for f in DEFAULT_FLAGS.iter() {
2024-01-02 14:35:23 +00:00
if !known_flags.contains(f) {
known_flags.push(f.clone());
}
}
2024-01-02 14:35:23 +00:00
// 1.c Create the IMAP message
body.push(Body::Data(Data::Flags(known_flags.clone())));
2024-01-02 14:35:23 +00:00
// 2. Returning flags that are persisted
// 2.a Always advertise our default flags
let mut permanent = DEFAULT_FLAGS
.iter()
.map(|f| FlagPerm::Flag(f.clone()))
.collect::<Vec<_>>();
// 2.b Say that we support any keyword flag
permanent.push(FlagPerm::Asterisk);
// 2.c Create the IMAP message
let permanent_flags = Status::ok(
None,
Some(Code::PermanentFlags(permanent)),
"Flags permitted",
)
.map_err(Error::msg)?;
body.push(Body::Status(permanent_flags));
2024-01-02 14:35:23 +00:00
// Done!
Ok(body)
}
pub(crate) fn unseen_count(&self) -> usize {
let total = self.known_state.table.len();
let seen = self
.known_state
.idx_by_flag
.get(&Flag::Seen.to_string())
.map(|x| x.len())
.unwrap_or(0);
total - seen
}
}
2022-06-29 15:58:31 +00:00
2024-01-04 19:54:21 +00:00
pub struct MailIdentifiers {
pub i: NonZeroU32,
pub uid: ImapUid,
pub uuid: UniqueIdent,
2022-07-15 14:15:48 +00:00
}
2024-01-04 19:54:21 +00:00
pub struct MailIdentifiersList(Vec<MailIdentifiers>);
2022-07-15 14:15:48 +00:00
2024-01-04 19:54:21 +00:00
impl MailIdentifiersList {
fn uuids(&self) -> Vec<UniqueIdent> {
self.0.iter().map(|mi| mi.uuid).collect()
2022-07-15 14:15:48 +00:00
}
}
#[cfg(test)]
mod tests {
use super::*;
2024-01-02 21:09:45 +00:00
use imap_codec::encode::Encoder;
2024-01-04 19:54:21 +00:00
use imap_codec::imap_types::core::NonEmptyVec;
2024-01-02 14:35:23 +00:00
use imap_codec::imap_types::fetch::Section;
2024-01-05 09:05:30 +00:00
use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItemName};
2024-01-02 21:09:45 +00:00
use imap_codec::imap_types::response::Response;
use imap_codec::ResponseCodec;
2022-07-05 13:21:14 +00:00
use std::fs;
2024-01-04 19:54:21 +00:00
use crate::cryptoblob;
2024-01-05 09:05:30 +00:00
use crate::imap::mail_view::{FetchedMail, MailView};
2024-01-04 19:54:21 +00:00
use crate::imap::mime_view;
2024-01-05 09:05:30 +00:00
use crate::mail::mailbox::MailMeta;
use crate::mail::unique_ident;
2024-01-04 19:54:21 +00:00
2023-10-12 10:21:59 +00:00
#[test]
fn mailview_body_ext() -> Result<()> {
let ap = AttributesProxy::new(
2024-01-02 21:09:45 +00:00
&MacroOrMessageDataItemNames::MessageDataItemNames(vec![
MessageDataItemName::BodyExt {
section: Some(Section::Header(None)),
partial: None,
peek: false,
},
]),
2023-10-12 10:21:59 +00:00
false,
);
let flags = vec![];
let key = cryptoblob::gen_key();
let meta = MailMeta {
internaldate: 0u64,
headers: vec![],
message_key: key,
rfc822_size: 8usize,
};
let ids = MailIdentifiers {
i: NonZeroU32::MIN,
uid: NonZeroU32::MIN,
uuid: unique_ident::gen_ident(),
};
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);
2024-01-02 21:09:45 +00:00
let mv = MailView {
2023-10-12 10:21:59 +00:00
ids: &ids,
content,
meta: &meta,
flags: &flags,
};
2024-01-02 21:09:45 +00:00
let (res_body, _seen) = mv.filter(&ap)?;
2023-10-12 10:21:59 +00:00
let fattr = match res_body {
Body::Data(Data::Fetch {
2024-01-02 14:35:23 +00:00
seq: _seq,
items: attr,
2023-10-12 10:21:59 +00:00
}) => Ok(attr),
_ => Err(anyhow!("Not a fetch body")),
}?;
2024-01-02 21:09:45 +00:00
assert_eq!(fattr.as_ref().len(), 1);
2023-10-12 10:21:59 +00:00
2024-01-02 21:09:45 +00:00
let (sec, _orig, _data) = match &fattr.as_ref()[0] {
MessageDataItem::BodyExt {
2023-10-12 10:21:59 +00:00
section,
origin,
data,
} => Ok((section, origin, data)),
_ => Err(anyhow!("not a body ext message attribute")),
}?;
assert_eq!(sec.as_ref().unwrap(), &Section::Header(None));
Ok(())
}
2022-07-05 16:27:49 +00:00
/// Future automated test. We use lossy utf8 conversion + lowercase everything,
2022-07-05 13:21:14 +00:00
/// so this test might allow invalid results. But at least it allows us to quickly test a
/// large variety of emails.
/// Keep in mind that special cases must still be tested manually!
#[test]
2022-07-05 13:21:14 +00:00
fn fetch_body() -> Result<()> {
2022-07-05 15:48:10 +00:00
let prefixes = [
2023-07-25 17:08:48 +00:00
/* *** MY OWN DATASET *** */
2022-07-05 15:48:10 +00:00
"tests/emails/dxflrs/0001_simple",
"tests/emails/dxflrs/0002_mime",
2022-07-05 16:27:49 +00:00
"tests/emails/dxflrs/0003_mime-in-mime",
2022-07-20 13:14:34 +00:00
"tests/emails/dxflrs/0004_msg-in-msg",
2023-07-25 17:08:48 +00:00
// eml_codec do not support continuation for the moment
2022-07-20 11:58:24 +00:00
//"tests/emails/dxflrs/0005_mail-parser-readme",
2023-03-09 10:30:44 +00:00
"tests/emails/dxflrs/0006_single-mime",
2023-07-25 17:08:48 +00:00
"tests/emails/dxflrs/0007_raw_msg_in_rfc822",
/* *** (STRANGE) RFC *** */
//"tests/emails/rfc/000", // must return text/enriched, we return text/plain
//"tests/emails/rfc/001", // does not recognize the multipart/external-body, breaks the
// whole parsing
//"tests/emails/rfc/002", // wrong date in email
//"tests/emails/rfc/003", // dovecot fixes \r\r: the bytes number is wrong + text/enriched
/* *** THIRD PARTY *** */
//"tests/emails/thirdparty/000", // dovecot fixes \r\r: the bytes number is wrong
//"tests/emails/thirdparty/001", // same
"tests/emails/thirdparty/002", // same
2023-10-12 10:21:59 +00:00
/* *** LEGACY *** */
//"tests/emails/legacy/000", // same issue with \r\r
2022-07-05 15:48:10 +00:00
];
2022-07-05 15:48:10 +00:00
for pref in prefixes.iter() {
println!("{}", pref);
let txt = fs::read(format!("{}.eml", pref))?;
2024-01-02 21:09:45 +00:00
let oracle = fs::read(format!("{}.dovecot.body", pref))?;
2023-07-25 17:08:48 +00:00
let message = eml_codec::parse_message(&txt).unwrap().1;
2024-01-02 21:09:45 +00:00
let test_repr = Response::Data(Data::Fetch {
seq: NonZeroU32::new(1).unwrap(),
2024-01-04 19:54:21 +00:00
items: NonEmptyVec::from(MessageDataItem::Body(mime_view::bodystructure(
2024-01-02 21:09:45 +00:00
&message.child,
)?)),
});
let test_bytes = ResponseCodec::new().encode(&test_repr).dump();
let test_str = String::from_utf8_lossy(&test_bytes).to_lowercase();
2022-07-05 13:21:14 +00:00
2024-01-02 21:09:45 +00:00
let oracle_str =
format!("* 1 FETCH {}\r\n", String::from_utf8_lossy(&oracle)).to_lowercase();
2022-07-05 15:48:10 +00:00
2024-01-02 21:09:45 +00:00
println!("aerogramme: {}\n\ndovecot: {}\n\n", test_str, oracle_str);
2022-07-08 08:23:07 +00:00
//println!("\n\n {} \n\n", String::from_utf8_lossy(&resp));
2024-01-02 21:09:45 +00:00
assert_eq!(test_str, oracle_str);
2022-07-05 15:48:10 +00:00
}
Ok(())
}
}