commands now use imap-flow
This commit is contained in:
parent
07eea38765
commit
9a8d4c651e
7 changed files with 468 additions and 391 deletions
|
@ -13,11 +13,11 @@ use crate::mail::user::User;
|
||||||
//--- dispatching
|
//--- dispatching
|
||||||
|
|
||||||
pub struct AnonymousContext<'a> {
|
pub struct AnonymousContext<'a> {
|
||||||
pub req: &'a Command<'static>,
|
pub req: &'a Command<'a>,
|
||||||
pub login_provider: &'a ArcLoginProvider,
|
pub login_provider: &'a ArcLoginProvider,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch(ctx: AnonymousContext<'_>) -> Result<(Response, flow::Transition)> {
|
pub async fn dispatch<'a>(ctx: AnonymousContext<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
match &ctx.req.body {
|
match &ctx.req.body {
|
||||||
// Any State
|
// Any State
|
||||||
CommandBody::Noop => anystate::noop_nothing(ctx.req.tag.clone()),
|
CommandBody::Noop => anystate::noop_nothing(ctx.req.tag.clone()),
|
||||||
|
@ -39,14 +39,14 @@ pub async fn dispatch(ctx: AnonymousContext<'_>) -> Result<(Response, flow::Tran
|
||||||
//--- Command controllers, private
|
//--- Command controllers, private
|
||||||
|
|
||||||
impl<'a> AnonymousContext<'a> {
|
impl<'a> AnonymousContext<'a> {
|
||||||
async fn capability(self) -> Result<(Response, flow::Transition)> {
|
async fn capability(self) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let capabilities: NonEmptyVec<Capability> =
|
let capabilities: NonEmptyVec<Capability> =
|
||||||
(vec![Capability::Imap4Rev1, Capability::Idle]).try_into()?;
|
(vec![Capability::Imap4Rev1, Capability::Idle]).try_into()?;
|
||||||
let res = Response::ok()
|
let res = Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Server capabilities")
|
.message("Server capabilities")
|
||||||
.data(Data::Capability(capabilities))
|
.data(Data::Capability(capabilities))
|
||||||
.build()?;
|
.ok()?;
|
||||||
Ok((res, flow::Transition::None))
|
Ok((res, flow::Transition::None))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ impl<'a> AnonymousContext<'a> {
|
||||||
self,
|
self,
|
||||||
username: &AString<'a>,
|
username: &AString<'a>,
|
||||||
password: &Secret<AString<'a>>,
|
password: &Secret<AString<'a>>,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let (u, p) = (
|
let (u, p) = (
|
||||||
std::str::from_utf8(username.as_ref())?,
|
std::str::from_utf8(username.as_ref())?,
|
||||||
std::str::from_utf8(password.declassify().as_ref())?,
|
std::str::from_utf8(password.declassify().as_ref())?,
|
||||||
|
@ -65,10 +65,10 @@ impl<'a> AnonymousContext<'a> {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::debug!(error=%e, "authentication failed");
|
tracing::debug!(error=%e, "authentication failed");
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Authentication failed")
|
.message("Authentication failed")
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -79,10 +79,10 @@ impl<'a> AnonymousContext<'a> {
|
||||||
|
|
||||||
tracing::info!(username=%u, "connected");
|
tracing::info!(username=%u, "connected");
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Completed")
|
.message("Completed")
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::Authenticate(user),
|
flow::Transition::Authenticate(user),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,45 +5,48 @@ use imap_codec::imap_types::response::{Capability, Data};
|
||||||
use crate::imap::flow;
|
use crate::imap::flow;
|
||||||
use crate::imap::response::Response;
|
use crate::imap::response::Response;
|
||||||
|
|
||||||
pub(crate) fn capability(tag: Tag) -> Result<(Response, flow::Transition)> {
|
pub(crate) fn capability<'a>(tag: Tag<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let capabilities: NonEmptyVec<Capability> =
|
let capabilities: NonEmptyVec<Capability> =
|
||||||
(vec![Capability::Imap4Rev1, Capability::Idle]).try_into()?;
|
(vec![Capability::Imap4Rev1, Capability::Idle]).try_into()?;
|
||||||
let res = Response::ok()
|
let res = Response::build()
|
||||||
.tag(tag)
|
.tag(tag)
|
||||||
.message("Server capabilities")
|
.message("Server capabilities")
|
||||||
.data(Data::Capability(capabilities))
|
.data(Data::Capability(capabilities))
|
||||||
.build()?;
|
.ok()?;
|
||||||
|
|
||||||
Ok((res, flow::Transition::None))
|
Ok((res, flow::Transition::None))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn noop_nothing(tag: Tag) -> Result<(Response, flow::Transition)> {
|
pub(crate) fn noop_nothing<'a>(tag: Tag<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok().tag(tag).message("Noop completed.").build()?,
|
Response::build().tag(tag).message("Noop completed.").ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn logout() -> Result<(Response, flow::Transition)> {
|
pub(crate) fn logout() -> Result<(Response<'static>, flow::Transition)> {
|
||||||
Ok((Response::bye()?, flow::Transition::Logout))
|
Ok((Response::bye()?, flow::Transition::Logout))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn not_implemented(tag: Tag, what: &str) -> Result<(Response, flow::Transition)> {
|
pub(crate) fn not_implemented<'a>(
|
||||||
|
tag: Tag<'a>,
|
||||||
|
what: &str,
|
||||||
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
Ok((
|
Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.tag(tag)
|
.tag(tag)
|
||||||
.message(format!("Command not implemented {}", what))
|
.message(format!("Command not implemented {}", what))
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn wrong_state(tag: Tag) -> Result<(Response, flow::Transition)> {
|
pub(crate) fn wrong_state<'a>(tag: Tag<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
Ok((
|
Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.tag(tag)
|
.tag(tag)
|
||||||
.message("Command not authorized in this state")
|
.message("Command not authorized in this state")
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,14 @@ use crate::mail::uidindex::*;
|
||||||
use crate::mail::user::{User, MAILBOX_HIERARCHY_DELIMITER as MBX_HIER_DELIM_RAW};
|
use crate::mail::user::{User, MAILBOX_HIERARCHY_DELIMITER as MBX_HIER_DELIM_RAW};
|
||||||
use crate::mail::IMF;
|
use crate::mail::IMF;
|
||||||
|
|
||||||
static MAILBOX_HIERARCHY_DELIMITER: QuotedChar = QuotedChar::unvalidated(MBX_HIER_DELIM_RAW);
|
|
||||||
|
|
||||||
pub struct AuthenticatedContext<'a> {
|
pub struct AuthenticatedContext<'a> {
|
||||||
pub req: &'a Command<'static>,
|
pub req: &'a Command<'a>,
|
||||||
pub user: &'a Arc<User>,
|
pub user: &'a Arc<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch(ctx: AuthenticatedContext<'_>) -> Result<(Response, flow::Transition)> {
|
pub async fn dispatch<'a>(
|
||||||
|
ctx: AuthenticatedContext<'a>,
|
||||||
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
match &ctx.req.body {
|
match &ctx.req.body {
|
||||||
// Any state
|
// Any state
|
||||||
CommandBody::Noop => anystate::noop_nothing(ctx.req.tag.clone()),
|
CommandBody::Noop => anystate::noop_nothing(ctx.req.tag.clone()),
|
||||||
|
@ -68,14 +68,14 @@ pub async fn dispatch(ctx: AuthenticatedContext<'_>) -> Result<(Response, flow::
|
||||||
|
|
||||||
// --- PRIVATE ---
|
// --- PRIVATE ---
|
||||||
impl<'a> AuthenticatedContext<'a> {
|
impl<'a> AuthenticatedContext<'a> {
|
||||||
async fn create(self, mailbox: &MailboxCodec<'a>) -> Result<(Response, flow::Transition)> {
|
async fn create(self, mailbox: &MailboxCodec<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name = match mailbox {
|
let name = match mailbox {
|
||||||
MailboxCodec::Inbox => {
|
MailboxCodec::Inbox => {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Cannot create INBOX")
|
.message("Cannot create INBOX")
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -84,38 +84,38 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
|
|
||||||
match self.user.create_mailbox(&name).await {
|
match self.user.create_mailbox(&name).await {
|
||||||
Ok(()) => Ok((
|
Ok(()) => Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("CREATE complete")
|
.message("CREATE complete")
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
Err(e) => Ok((
|
Err(e) => Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(&e.to_string())
|
.message(&e.to_string())
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete(self, mailbox: &MailboxCodec<'a>) -> Result<(Response, flow::Transition)> {
|
async fn delete(self, mailbox: &MailboxCodec<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
|
|
||||||
match self.user.delete_mailbox(&name).await {
|
match self.user.delete_mailbox(&name).await {
|
||||||
Ok(()) => Ok((
|
Ok(()) => Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("DELETE complete")
|
.message("DELETE complete")
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
Err(e) => Ok((
|
Err(e) => Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(e.to_string())
|
.message(e.to_string())
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
@ -125,23 +125,23 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
self,
|
self,
|
||||||
from: &MailboxCodec<'a>,
|
from: &MailboxCodec<'a>,
|
||||||
to: &MailboxCodec<'a>,
|
to: &MailboxCodec<'a>,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(from).try_into()?;
|
let name: &str = MailboxName(from).try_into()?;
|
||||||
let new_name: &str = MailboxName(to).try_into()?;
|
let new_name: &str = MailboxName(to).try_into()?;
|
||||||
|
|
||||||
match self.user.rename_mailbox(&name, &new_name).await {
|
match self.user.rename_mailbox(&name, &new_name).await {
|
||||||
Ok(()) => Ok((
|
Ok(()) => Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("RENAME complete")
|
.message("RENAME complete")
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
Err(e) => Ok((
|
Err(e) => Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(e.to_string())
|
.message(e.to_string())
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
@ -152,14 +152,16 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
reference: &MailboxCodec<'a>,
|
reference: &MailboxCodec<'a>,
|
||||||
mailbox_wildcard: &ListMailbox<'a>,
|
mailbox_wildcard: &ListMailbox<'a>,
|
||||||
is_lsub: bool,
|
is_lsub: bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
|
let mbx_hier_delim: QuotedChar = QuotedChar::unvalidated(MBX_HIER_DELIM_RAW);
|
||||||
|
|
||||||
let reference: &str = MailboxName(reference).try_into()?;
|
let reference: &str = MailboxName(reference).try_into()?;
|
||||||
if !reference.is_empty() {
|
if !reference.is_empty() {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("References not supported")
|
.message("References not supported")
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -172,28 +174,28 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
if wildcard.is_empty() {
|
if wildcard.is_empty() {
|
||||||
if is_lsub {
|
if is_lsub {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("LSUB complete")
|
.message("LSUB complete")
|
||||||
.data(Data::Lsub {
|
.data(Data::Lsub {
|
||||||
items: vec![],
|
items: vec![],
|
||||||
delimiter: Some(MAILBOX_HIERARCHY_DELIMITER),
|
delimiter: Some(mbx_hier_delim),
|
||||||
mailbox: "".try_into().unwrap(),
|
mailbox: "".try_into().unwrap(),
|
||||||
})
|
})
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("LIST complete")
|
.message("LIST complete")
|
||||||
.data(Data::List {
|
.data(Data::List {
|
||||||
items: vec![],
|
items: vec![],
|
||||||
delimiter: Some(MAILBOX_HIERARCHY_DELIMITER),
|
delimiter: Some(mbx_hier_delim),
|
||||||
mailbox: "".try_into().unwrap(),
|
mailbox: "".try_into().unwrap(),
|
||||||
})
|
})
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -227,13 +229,13 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
if is_lsub {
|
if is_lsub {
|
||||||
ret.push(Data::Lsub {
|
ret.push(Data::Lsub {
|
||||||
items,
|
items,
|
||||||
delimiter: Some(MAILBOX_HIERARCHY_DELIMITER),
|
delimiter: Some(mbx_hier_delim),
|
||||||
mailbox,
|
mailbox,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ret.push(Data::List {
|
ret.push(Data::List {
|
||||||
items,
|
items,
|
||||||
delimiter: Some(MAILBOX_HIERARCHY_DELIMITER),
|
delimiter: Some(mbx_hier_delim),
|
||||||
mailbox,
|
mailbox,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -246,11 +248,11 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
"LIST completed"
|
"LIST completed"
|
||||||
};
|
};
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(msg)
|
.message(msg)
|
||||||
.set_data(ret)
|
.many_data(ret)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -259,23 +261,23 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
self,
|
self,
|
||||||
mailbox: &MailboxCodec<'a>,
|
mailbox: &MailboxCodec<'a>,
|
||||||
attributes: &[StatusDataItemName],
|
attributes: &[StatusDataItemName],
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
let mb_opt = self.user.open_mailbox(name).await?;
|
let mb_opt = self.user.open_mailbox(name).await?;
|
||||||
let mb = match mb_opt {
|
let mb = match mb_opt {
|
||||||
Some(mb) => mb,
|
Some(mb) => mb,
|
||||||
None => {
|
None => {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Mailbox does not exist")
|
.message("Mailbox does not exist")
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (view, _data) = MailboxView::new(mb).await?;
|
let view = MailboxView::new(mb).await;
|
||||||
|
|
||||||
let mut ret_attrs = vec![];
|
let mut ret_attrs = vec![];
|
||||||
for attr in attributes.iter() {
|
for attr in attributes.iter() {
|
||||||
|
@ -302,57 +304,63 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("STATUS completed")
|
.message("STATUS completed")
|
||||||
.data(data)
|
.data(data)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn subscribe(self, mailbox: &MailboxCodec<'a>) -> Result<(Response, flow::Transition)> {
|
async fn subscribe(
|
||||||
|
self,
|
||||||
|
mailbox: &MailboxCodec<'a>,
|
||||||
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
|
|
||||||
if self.user.has_mailbox(&name).await? {
|
if self.user.has_mailbox(&name).await? {
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("SUBSCRIBE complete")
|
.message("SUBSCRIBE complete")
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Ok((
|
Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(format!("Mailbox {} does not exist", name))
|
.message(format!("Mailbox {} does not exist", name))
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn unsubscribe(self, mailbox: &MailboxCodec<'a>) -> Result<(Response, flow::Transition)> {
|
async fn unsubscribe(
|
||||||
|
self,
|
||||||
|
mailbox: &MailboxCodec<'a>,
|
||||||
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
|
|
||||||
if self.user.has_mailbox(&name).await? {
|
if self.user.has_mailbox(&name).await? {
|
||||||
Ok((
|
Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(format!(
|
.message(format!(
|
||||||
"Cannot unsubscribe from mailbox {}: not supported by Aerogramme",
|
"Cannot unsubscribe from mailbox {}: not supported by Aerogramme",
|
||||||
name
|
name
|
||||||
))
|
))
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Ok((
|
Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(format!("Mailbox {} does not exist", name))
|
.message(format!("Mailbox {} does not exist", name))
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -391,7 +399,7 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
|
|
||||||
* TRACE END ---
|
* TRACE END ---
|
||||||
*/
|
*/
|
||||||
async fn select(self, mailbox: &MailboxCodec<'a>) -> Result<(Response, flow::Transition)> {
|
async fn select(self, mailbox: &MailboxCodec<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
|
|
||||||
let mb_opt = self.user.open_mailbox(&name).await?;
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
||||||
|
@ -399,29 +407,30 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
Some(mb) => mb,
|
Some(mb) => mb,
|
||||||
None => {
|
None => {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Mailbox does not exist")
|
.message("Mailbox does not exist")
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
tracing::info!(username=%self.user.username, mailbox=%name, "mailbox.selected");
|
tracing::info!(username=%self.user.username, mailbox=%name, "mailbox.selected");
|
||||||
|
|
||||||
let (mb, data) = MailboxView::new(mb).await?;
|
let mb = MailboxView::new(mb).await;
|
||||||
|
let data = mb.summary()?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.message("Select completed")
|
.message("Select completed")
|
||||||
.code(Code::ReadWrite)
|
.code(Code::ReadWrite)
|
||||||
.data(data)
|
.set_body(data)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::Select(mb),
|
flow::Transition::Select(mb),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn examine(self, mailbox: &MailboxCodec<'a>) -> Result<(Response, flow::Transition)> {
|
async fn examine(self, mailbox: &MailboxCodec<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
|
|
||||||
let mb_opt = self.user.open_mailbox(&name).await?;
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
||||||
|
@ -429,25 +438,26 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
Some(mb) => mb,
|
Some(mb) => mb,
|
||||||
None => {
|
None => {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Mailbox does not exist")
|
.message("Mailbox does not exist")
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
tracing::info!(username=%self.user.username, mailbox=%name, "mailbox.examined");
|
tracing::info!(username=%self.user.username, mailbox=%name, "mailbox.examined");
|
||||||
|
|
||||||
let (mb, data) = MailboxView::new(mb).await?;
|
let mb = MailboxView::new(mb).await;
|
||||||
|
let data = mb.summary()?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Examine completed")
|
.message("Examine completed")
|
||||||
.code(Code::ReadOnly)
|
.code(Code::ReadOnly)
|
||||||
.data(data)
|
.set_body(data)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::Examine(mb),
|
flow::Transition::Examine(mb),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -458,24 +468,24 @@ impl<'a> AuthenticatedContext<'a> {
|
||||||
flags: &[Flag<'a>],
|
flags: &[Flag<'a>],
|
||||||
date: &Option<DateTime>,
|
date: &Option<DateTime>,
|
||||||
message: &Literal<'a>,
|
message: &Literal<'a>,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let append_tag = self.req.tag.clone();
|
let append_tag = self.req.tag.clone();
|
||||||
match self.append_internal(mailbox, flags, date, message).await {
|
match self.append_internal(mailbox, flags, date, message).await {
|
||||||
Ok((_mb, uidvalidity, uid)) => Ok((
|
Ok((_mb, uidvalidity, uid)) => Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.tag(append_tag)
|
.tag(append_tag)
|
||||||
.message("APPEND completed")
|
.message("APPEND completed")
|
||||||
.code(Code::Other(CodeOther::unvalidated(
|
.code(Code::Other(CodeOther::unvalidated(
|
||||||
format!("APPENDUID {} {}", uidvalidity, uid).into_bytes(),
|
format!("APPENDUID {} {}", uidvalidity, uid).into_bytes(),
|
||||||
)))
|
)))
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
Err(e) => Ok((
|
Err(e) => Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.tag(append_tag)
|
.tag(append_tag)
|
||||||
.message(e.to_string())
|
.message(e.to_string())
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub struct ExaminedContext<'a> {
|
||||||
pub mailbox: &'a mut MailboxView,
|
pub mailbox: &'a mut MailboxView,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch(ctx: ExaminedContext<'_>) -> Result<(Response, flow::Transition)> {
|
pub async fn dispatch<'a>(ctx: ExaminedContext<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
match &ctx.req.body {
|
match &ctx.req.body {
|
||||||
// Any State
|
// Any State
|
||||||
// noop is specific to this state
|
// noop is specific to this state
|
||||||
|
@ -41,10 +41,10 @@ pub async fn dispatch(ctx: ExaminedContext<'_>) -> Result<(Response, flow::Trans
|
||||||
} => ctx.search(charset, criteria, uid).await,
|
} => ctx.search(charset, criteria, uid).await,
|
||||||
CommandBody::Noop | CommandBody::Check => ctx.noop().await,
|
CommandBody::Noop | CommandBody::Check => ctx.noop().await,
|
||||||
CommandBody::Expunge { .. } | CommandBody::Store { .. } => Ok((
|
CommandBody::Expunge { .. } | CommandBody::Store { .. } => Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(ctx.req)
|
.to_req(ctx.req)
|
||||||
.message("Forbidden command: can't write in read-only mode (EXAMINE)")
|
.message("Forbidden command: can't write in read-only mode (EXAMINE)")
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
|
|
||||||
|
@ -58,12 +58,12 @@ pub async fn dispatch(ctx: ExaminedContext<'_>) -> Result<(Response, flow::Trans
|
||||||
impl<'a> ExaminedContext<'a> {
|
impl<'a> ExaminedContext<'a> {
|
||||||
/// CLOSE in examined state is not the same as in selected state
|
/// CLOSE in examined state is not the same as in selected state
|
||||||
/// (in selected state it also does an EXPUNGE, here it doesn't)
|
/// (in selected state it also does an EXPUNGE, here it doesn't)
|
||||||
async fn close(self) -> Result<(Response, flow::Transition)> {
|
async fn close(self) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("CLOSE completed")
|
.message("CLOSE completed")
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::Unselect,
|
flow::Transition::Unselect,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -71,23 +71,23 @@ impl<'a> ExaminedContext<'a> {
|
||||||
pub async fn fetch(
|
pub async fn fetch(
|
||||||
self,
|
self,
|
||||||
sequence_set: &SequenceSet,
|
sequence_set: &SequenceSet,
|
||||||
attributes: &MacroOrMessageDataItemNames<'a>,
|
attributes: &'a MacroOrMessageDataItemNames<'a>,
|
||||||
uid: &bool,
|
uid: &bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
match self.mailbox.fetch(sequence_set, attributes, uid).await {
|
match self.mailbox.fetch(sequence_set, attributes, uid).await {
|
||||||
Ok(resp) => Ok((
|
Ok(resp) => Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("FETCH completed")
|
.message("FETCH completed")
|
||||||
.set_data(resp)
|
.set_body(resp)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
Err(e) => Ok((
|
Err(e) => Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(e.to_string())
|
.message(e.to_string())
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
@ -98,26 +98,26 @@ impl<'a> ExaminedContext<'a> {
|
||||||
_charset: &Option<Charset<'a>>,
|
_charset: &Option<Charset<'a>>,
|
||||||
_criteria: &SearchKey<'a>,
|
_criteria: &SearchKey<'a>,
|
||||||
_uid: &bool,
|
_uid: &bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
Ok((
|
Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Not implemented")
|
.message("Not implemented")
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn noop(self) -> Result<(Response, flow::Transition)> {
|
pub async fn noop(self) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
self.mailbox.mailbox.force_sync().await?;
|
self.mailbox.mailbox.force_sync().await?;
|
||||||
|
|
||||||
let updates = self.mailbox.update().await?;
|
let updates = self.mailbox.update().await?;
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("NOOP completed.")
|
.message("NOOP completed.")
|
||||||
.set_data(updates)
|
.set_body(updates)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ pub struct SelectedContext<'a> {
|
||||||
pub mailbox: &'a mut MailboxView,
|
pub mailbox: &'a mut MailboxView,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch(ctx: SelectedContext<'_>) -> Result<(Response, flow::Transition)> {
|
pub async fn dispatch<'a>(ctx: SelectedContext<'a>) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
match &ctx.req.body {
|
match &ctx.req.body {
|
||||||
// Any State
|
// Any State
|
||||||
// noop is specific to this state
|
// noop is specific to this state
|
||||||
|
@ -65,13 +65,13 @@ pub async fn dispatch(ctx: SelectedContext<'_>) -> Result<(Response, flow::Trans
|
||||||
// --- PRIVATE ---
|
// --- PRIVATE ---
|
||||||
|
|
||||||
impl<'a> SelectedContext<'a> {
|
impl<'a> SelectedContext<'a> {
|
||||||
async fn close(self) -> Result<(Response, flow::Transition)> {
|
async fn close(self) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
// We expunge messages,
|
// We expunge messages,
|
||||||
// but we don't send the untagged EXPUNGE responses
|
// but we don't send the untagged EXPUNGE responses
|
||||||
let tag = self.req.tag.clone();
|
let tag = self.req.tag.clone();
|
||||||
self.expunge().await?;
|
self.expunge().await?;
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok().tag(tag).message("CLOSE completed").build()?,
|
Response::build().tag(tag).message("CLOSE completed").ok()?,
|
||||||
flow::Transition::Unselect,
|
flow::Transition::Unselect,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -79,23 +79,23 @@ impl<'a> SelectedContext<'a> {
|
||||||
pub async fn fetch(
|
pub async fn fetch(
|
||||||
self,
|
self,
|
||||||
sequence_set: &SequenceSet,
|
sequence_set: &SequenceSet,
|
||||||
attributes: &MacroOrMessageDataItemNames<'a>,
|
attributes: &'a MacroOrMessageDataItemNames<'a>,
|
||||||
uid: &bool,
|
uid: &bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
match self.mailbox.fetch(sequence_set, attributes, uid).await {
|
match self.mailbox.fetch(sequence_set, attributes, uid).await {
|
||||||
Ok(resp) => Ok((
|
Ok(resp) => Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("FETCH completed")
|
.message("FETCH completed")
|
||||||
.set_data(resp)
|
.set_body(resp)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
Err(e) => Ok((
|
Err(e) => Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message(e.to_string())
|
.message(e.to_string())
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
@ -106,40 +106,40 @@ impl<'a> SelectedContext<'a> {
|
||||||
_charset: &Option<Charset<'a>>,
|
_charset: &Option<Charset<'a>>,
|
||||||
_criteria: &SearchKey<'a>,
|
_criteria: &SearchKey<'a>,
|
||||||
_uid: &bool,
|
_uid: &bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
Ok((
|
Ok((
|
||||||
Response::bad()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Not implemented")
|
.message("Not implemented")
|
||||||
.build()?,
|
.bad()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn noop(self) -> Result<(Response, flow::Transition)> {
|
pub async fn noop(self) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
self.mailbox.mailbox.force_sync().await?;
|
self.mailbox.mailbox.force_sync().await?;
|
||||||
|
|
||||||
let updates = self.mailbox.update().await?;
|
let updates = self.mailbox.update().await?;
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("NOOP completed.")
|
.message("NOOP completed.")
|
||||||
.set_data(updates)
|
.set_body(updates)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn expunge(self) -> Result<(Response, flow::Transition)> {
|
async fn expunge(self) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let tag = self.req.tag.clone();
|
let tag = self.req.tag.clone();
|
||||||
let data = self.mailbox.expunge().await?;
|
let data = self.mailbox.expunge().await?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.tag(tag)
|
.tag(tag)
|
||||||
.message("EXPUNGE completed")
|
.message("EXPUNGE completed")
|
||||||
.data(data)
|
.set_body(data)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -151,18 +151,18 @@ impl<'a> SelectedContext<'a> {
|
||||||
response: &StoreResponse,
|
response: &StoreResponse,
|
||||||
flags: &[Flag<'a>],
|
flags: &[Flag<'a>],
|
||||||
uid: &bool,
|
uid: &bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let data = self
|
let data = self
|
||||||
.mailbox
|
.mailbox
|
||||||
.store(sequence_set, kind, response, flags, uid)
|
.store(sequence_set, kind, response, flags, uid)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("STORE completed")
|
.message("STORE completed")
|
||||||
.set_data(data)
|
.set_body(data)
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -172,7 +172,7 @@ impl<'a> SelectedContext<'a> {
|
||||||
sequence_set: &SequenceSet,
|
sequence_set: &SequenceSet,
|
||||||
mailbox: &MailboxCodec<'a>,
|
mailbox: &MailboxCodec<'a>,
|
||||||
uid: &bool,
|
uid: &bool,
|
||||||
) -> Result<(Response, flow::Transition)> {
|
) -> Result<(Response<'a>, flow::Transition)> {
|
||||||
let name: &str = MailboxName(mailbox).try_into()?;
|
let name: &str = MailboxName(mailbox).try_into()?;
|
||||||
|
|
||||||
let mb_opt = self.user.open_mailbox(&name).await?;
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
||||||
|
@ -180,11 +180,11 @@ impl<'a> SelectedContext<'a> {
|
||||||
Some(mb) => mb,
|
Some(mb) => mb,
|
||||||
None => {
|
None => {
|
||||||
return Ok((
|
return Ok((
|
||||||
Response::no()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("Destination mailbox does not exist")
|
.message("Destination mailbox does not exist")
|
||||||
.code(Code::TryCreate)
|
.code(Code::TryCreate)
|
||||||
.build()?,
|
.no()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -208,13 +208,13 @@ impl<'a> SelectedContext<'a> {
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Response::ok()
|
Response::build()
|
||||||
.to_req(self.req)
|
.to_req(self.req)
|
||||||
.message("COPY completed")
|
.message("COPY completed")
|
||||||
.code(Code::Other(CodeOther::unvalidated(
|
.code(Code::Other(CodeOther::unvalidated(
|
||||||
format!("COPYUID {}", copyuid_str).into_bytes(),
|
format!("COPYUID {}", copyuid_str).into_bytes(),
|
||||||
)))
|
)))
|
||||||
.build()?,
|
.ok()?,
|
||||||
flow::Transition::None,
|
flow::Transition::None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,21 +4,19 @@ use std::num::NonZeroU32;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, Error, Result};
|
use anyhow::{anyhow, bail, Error, Result};
|
||||||
use boitalettres::proto::res::body::Data as Body;
|
|
||||||
use chrono::{Offset, TimeZone, Utc};
|
use chrono::{Offset, TimeZone, Utc};
|
||||||
|
|
||||||
use futures::stream::{FuturesOrdered, StreamExt};
|
use futures::stream::{FuturesOrdered, StreamExt};
|
||||||
|
|
||||||
use imap_codec::imap_types::address::Address;
|
|
||||||
use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields};
|
use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields};
|
||||||
use imap_codec::imap_types::core::{AString, Atom, IString, NString};
|
use imap_codec::imap_types::core::{AString, Atom, IString, NString, NonEmptyVec};
|
||||||
use imap_codec::imap_types::datetime::MyDateTime;
|
use imap_codec::imap_types::datetime::DateTime;
|
||||||
use imap_codec::imap_types::envelope::Envelope;
|
use imap_codec::imap_types::envelope::{Address, Envelope};
|
||||||
use imap_codec::imap_types::fetch_attributes::{
|
use imap_codec::imap_types::fetch::{
|
||||||
FetchAttribute, MacroOrFetchAttributes, Section as FetchSection,
|
MacroOrMessageDataItemNames, MessageDataItem, MessageDataItemName, Section as FetchSection,
|
||||||
};
|
};
|
||||||
use imap_codec::imap_types::flag::{Flag, StoreResponse, StoreType};
|
use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType};
|
||||||
use imap_codec::imap_types::response::{Code, Data, MessageAttribute, Status};
|
use imap_codec::imap_types::response::{Code, Data, Status};
|
||||||
use imap_codec::imap_types::sequence::{self, SequenceSet};
|
use imap_codec::imap_types::sequence::{self, SequenceSet};
|
||||||
|
|
||||||
use eml_codec::{
|
use eml_codec::{
|
||||||
|
@ -28,6 +26,7 @@ use eml_codec::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::cryptoblob::Key;
|
use crate::cryptoblob::Key;
|
||||||
|
use crate::imap::response::Body;
|
||||||
use crate::mail::mailbox::{MailMeta, Mailbox};
|
use crate::mail::mailbox::{MailMeta, Mailbox};
|
||||||
use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex};
|
use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex};
|
||||||
use crate::mail::unique_ident::UniqueIdent;
|
use crate::mail::unique_ident::UniqueIdent;
|
||||||
|
@ -76,20 +75,20 @@ impl<'a> FetchedMail<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AttributesProxy {
|
pub struct AttributesProxy<'a> {
|
||||||
attrs: Vec<FetchAttribute>,
|
attrs: Vec<MessageDataItemName<'a>>,
|
||||||
}
|
}
|
||||||
impl AttributesProxy {
|
impl<'a> AttributesProxy<'a> {
|
||||||
fn new(attrs: &MacroOrFetchAttributes, is_uid_fetch: bool) -> Self {
|
fn new(attrs: &'a MacroOrMessageDataItemNames<'a>, is_uid_fetch: bool) -> Self {
|
||||||
// Expand macros
|
// Expand macros
|
||||||
let mut fetch_attrs = match attrs {
|
let mut fetch_attrs = match attrs {
|
||||||
MacroOrFetchAttributes::Macro(m) => m.expand(),
|
MacroOrMessageDataItemNames::Macro(m) => m.expand(),
|
||||||
MacroOrFetchAttributes::FetchAttributes(a) => a.clone(),
|
MacroOrMessageDataItemNames::MessageDataItemNames(a) => a.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle uids
|
// Handle uids
|
||||||
if is_uid_fetch && !fetch_attrs.contains(&FetchAttribute::Uid) {
|
if is_uid_fetch && !fetch_attrs.contains(&MessageDataItemName::Uid) {
|
||||||
fetch_attrs.push(FetchAttribute::Uid);
|
fetch_attrs.push(MessageDataItemName::Uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self { attrs: fetch_attrs }
|
Self { attrs: fetch_attrs }
|
||||||
|
@ -99,11 +98,11 @@ impl AttributesProxy {
|
||||||
self.attrs.iter().any(|x| {
|
self.attrs.iter().any(|x| {
|
||||||
matches!(
|
matches!(
|
||||||
x,
|
x,
|
||||||
FetchAttribute::Body
|
MessageDataItemName::Body
|
||||||
| FetchAttribute::BodyExt { .. }
|
| MessageDataItemName::BodyExt { .. }
|
||||||
| FetchAttribute::Rfc822
|
| MessageDataItemName::Rfc822
|
||||||
| FetchAttribute::Rfc822Text
|
| MessageDataItemName::Rfc822Text
|
||||||
| FetchAttribute::BodyStructure
|
| MessageDataItemName::BodyStructure
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -127,16 +126,20 @@ pub struct MailView<'a> {
|
||||||
meta: &'a MailMeta,
|
meta: &'a MailMeta,
|
||||||
flags: &'a Vec<String>,
|
flags: &'a Vec<String>,
|
||||||
content: FetchedMail<'a>,
|
content: FetchedMail<'a>,
|
||||||
add_seen: bool,
|
}
|
||||||
|
|
||||||
|
enum SeenFlag {
|
||||||
|
DoNothing,
|
||||||
|
MustAdd,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> MailView<'a> {
|
impl<'a> MailView<'a> {
|
||||||
fn uid(&self) -> MessageAttribute {
|
fn uid(&self) -> MessageDataItem<'static> {
|
||||||
MessageAttribute::Uid(self.ids.uid)
|
MessageDataItem::Uid(self.ids.uid.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flags(&self) -> MessageAttribute {
|
fn flags(&self) -> MessageDataItem<'static> {
|
||||||
MessageAttribute::Flags(
|
MessageDataItem::Flags(
|
||||||
self.flags
|
self.flags
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|f| string_to_flag(f))
|
.filter_map(|f| string_to_flag(f))
|
||||||
|
@ -144,12 +147,12 @@ impl<'a> MailView<'a> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rfc_822_size(&self) -> MessageAttribute {
|
fn rfc_822_size(&self) -> MessageDataItem<'static> {
|
||||||
MessageAttribute::Rfc822Size(self.meta.rfc822_size as u32)
|
MessageDataItem::Rfc822Size(self.meta.rfc822_size as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rfc_822_header(&self) -> MessageAttribute {
|
fn rfc_822_header(&self) -> MessageDataItem<'static> {
|
||||||
MessageAttribute::Rfc822Header(NString(
|
MessageDataItem::Rfc822Header(NString(
|
||||||
self.meta
|
self.meta
|
||||||
.headers
|
.headers
|
||||||
.to_vec()
|
.to_vec()
|
||||||
|
@ -159,41 +162,42 @@ impl<'a> MailView<'a> {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rfc_822_text(&self) -> Result<MessageAttribute> {
|
fn rfc_822_text(&self) -> Result<MessageDataItem<'static>> {
|
||||||
Ok(MessageAttribute::Rfc822Text(NString(
|
Ok(MessageDataItem::Rfc822Text(NString(
|
||||||
self.content
|
self.content
|
||||||
.as_full()?
|
.as_full()?
|
||||||
.raw_body
|
.raw_body
|
||||||
|
.to_vec()
|
||||||
.try_into()
|
.try_into()
|
||||||
.ok()
|
.ok()
|
||||||
.map(IString::Literal),
|
.map(IString::Literal),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rfc822(&self) -> Result<MessageAttribute> {
|
fn rfc822(&self) -> Result<MessageDataItem<'static>> {
|
||||||
Ok(MessageAttribute::Rfc822(NString(
|
Ok(MessageDataItem::Rfc822(NString(
|
||||||
self.content
|
self.content
|
||||||
.as_full()?
|
.as_full()?
|
||||||
.raw_part
|
.raw_part
|
||||||
.clone()
|
.to_vec()
|
||||||
.try_into()
|
.try_into()
|
||||||
.ok()
|
.ok()
|
||||||
.map(IString::Literal),
|
.map(IString::Literal),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn envelope(&self) -> MessageAttribute {
|
fn envelope(&self) -> MessageDataItem<'static> {
|
||||||
MessageAttribute::Envelope(message_envelope(self.content.imf()))
|
MessageDataItem::Envelope(message_envelope(self.content.imf().clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn body(&self) -> Result<MessageAttribute> {
|
fn body(&self) -> Result<MessageDataItem<'static>> {
|
||||||
Ok(MessageAttribute::Body(build_imap_email_struct(
|
Ok(MessageDataItem::Body(build_imap_email_struct(
|
||||||
self.content.as_full()?.child.as_ref(),
|
self.content.as_full()?.child.as_ref(),
|
||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn body_structure(&self) -> Result<MessageAttribute> {
|
fn body_structure(&self) -> Result<MessageDataItem<'static>> {
|
||||||
Ok(MessageAttribute::Body(build_imap_email_struct(
|
Ok(MessageDataItem::Body(build_imap_email_struct(
|
||||||
self.content.as_full()?.child.as_ref(),
|
self.content.as_full()?.child.as_ref(),
|
||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
@ -202,12 +206,14 @@ impl<'a> MailView<'a> {
|
||||||
/// peek does not implicitly set the \Seen flag
|
/// peek does not implicitly set the \Seen flag
|
||||||
/// eg. BODY[HEADER.FIELDS (DATE FROM)]
|
/// eg. BODY[HEADER.FIELDS (DATE FROM)]
|
||||||
/// eg. BODY[]<0.2048>
|
/// eg. BODY[]<0.2048>
|
||||||
fn body_ext(
|
fn body_ext<'b>(
|
||||||
&mut self,
|
&self,
|
||||||
section: &Option<FetchSection>,
|
section: &Option<FetchSection<'b>>,
|
||||||
partial: &Option<(u32, NonZeroU32)>,
|
partial: &Option<(u32, NonZeroU32)>,
|
||||||
peek: &bool,
|
peek: &bool,
|
||||||
) -> Result<MessageAttribute> {
|
) -> Result<(MessageDataItem<'b>, SeenFlag)> {
|
||||||
|
let mut seen = SeenFlag::DoNothing;
|
||||||
|
|
||||||
// Extract message section
|
// Extract message section
|
||||||
let text = get_message_section(self.content.as_anypart()?, section)?;
|
let text = get_message_section(self.content.as_anypart()?, section)?;
|
||||||
|
|
||||||
|
@ -215,7 +221,7 @@ impl<'a> MailView<'a> {
|
||||||
if !peek && !self.flags.iter().any(|x| *x == seen_flag) {
|
if !peek && !self.flags.iter().any(|x| *x == seen_flag) {
|
||||||
// Add \Seen flag
|
// Add \Seen flag
|
||||||
//self.mailbox.add_flags(uuid, &[seen_flag]).await?;
|
//self.mailbox.add_flags(uuid, &[seen_flag]).await?;
|
||||||
self.add_seen = true;
|
seen = SeenFlag::MustAdd;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle <<partial>> which cut the message bytes
|
// Handle <<partial>> which cut the message bytes
|
||||||
|
@ -223,49 +229,60 @@ impl<'a> MailView<'a> {
|
||||||
|
|
||||||
let data = NString(text.to_vec().try_into().ok().map(IString::Literal));
|
let data = NString(text.to_vec().try_into().ok().map(IString::Literal));
|
||||||
|
|
||||||
return Ok(MessageAttribute::BodyExt {
|
return Ok((
|
||||||
section: section.clone(),
|
MessageDataItem::BodyExt {
|
||||||
|
section: section.as_ref().map(|fs| fs.clone()),
|
||||||
origin,
|
origin,
|
||||||
data,
|
data,
|
||||||
});
|
},
|
||||||
|
seen,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn internal_date(&self) -> Result<MessageAttribute> {
|
fn internal_date(&self) -> Result<MessageDataItem<'static>> {
|
||||||
let dt = Utc
|
let dt = Utc
|
||||||
.fix()
|
.fix()
|
||||||
.timestamp_opt(i64::try_from(self.meta.internaldate / 1000)?, 0)
|
.timestamp_opt(i64::try_from(self.meta.internaldate / 1000)?, 0)
|
||||||
.earliest()
|
.earliest()
|
||||||
.ok_or(anyhow!("Unable to parse internal date"))?;
|
.ok_or(anyhow!("Unable to parse internal date"))?;
|
||||||
Ok(MessageAttribute::InternalDate(MyDateTime(dt)))
|
Ok(MessageDataItem::InternalDate(DateTime::unvalidated(dt)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter(&mut self, ap: &AttributesProxy) -> Result<Body> {
|
fn filter<'b>(&self, ap: &AttributesProxy<'b>) -> Result<(Body<'b>, SeenFlag)> {
|
||||||
|
let mut seen = SeenFlag::DoNothing;
|
||||||
let res_attrs = ap
|
let res_attrs = ap
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|attr| match attr {
|
.map(|attr| match attr {
|
||||||
FetchAttribute::Uid => Ok(self.uid()),
|
MessageDataItemName::Uid => Ok(self.uid()),
|
||||||
FetchAttribute::Flags => Ok(self.flags()),
|
MessageDataItemName::Flags => Ok(self.flags()),
|
||||||
FetchAttribute::Rfc822Size => Ok(self.rfc_822_size()),
|
MessageDataItemName::Rfc822Size => Ok(self.rfc_822_size()),
|
||||||
FetchAttribute::Rfc822Header => Ok(self.rfc_822_header()),
|
MessageDataItemName::Rfc822Header => Ok(self.rfc_822_header()),
|
||||||
FetchAttribute::Rfc822Text => self.rfc_822_text(),
|
MessageDataItemName::Rfc822Text => self.rfc_822_text(),
|
||||||
FetchAttribute::Rfc822 => self.rfc822(),
|
MessageDataItemName::Rfc822 => self.rfc822(),
|
||||||
FetchAttribute::Envelope => Ok(self.envelope()),
|
MessageDataItemName::Envelope => Ok(self.envelope()),
|
||||||
FetchAttribute::Body => self.body(),
|
MessageDataItemName::Body => self.body(),
|
||||||
FetchAttribute::BodyStructure => self.body_structure(),
|
MessageDataItemName::BodyStructure => self.body_structure(),
|
||||||
FetchAttribute::BodyExt {
|
MessageDataItemName::BodyExt {
|
||||||
section,
|
section,
|
||||||
partial,
|
partial,
|
||||||
peek,
|
peek,
|
||||||
} => self.body_ext(section, partial, peek),
|
} => {
|
||||||
FetchAttribute::InternalDate => self.internal_date(),
|
let (body, has_seen) = self.body_ext(section, partial, peek)?;
|
||||||
|
seen = has_seen;
|
||||||
|
Ok(body)
|
||||||
|
}
|
||||||
|
MessageDataItemName::InternalDate => self.internal_date(),
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
Ok(Body::Data(Data::Fetch {
|
Ok((
|
||||||
seq_or_uid: self.ids.i,
|
Body::Data(Data::Fetch {
|
||||||
attributes: res_attrs,
|
seq: self.ids.i,
|
||||||
}))
|
items: res_attrs.try_into()?,
|
||||||
|
}),
|
||||||
|
seen,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -376,7 +393,6 @@ impl<'a> MailSelectionBuilder<'a> {
|
||||||
meta,
|
meta,
|
||||||
flags,
|
flags,
|
||||||
content,
|
content,
|
||||||
add_seen: false,
|
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
@ -396,35 +412,26 @@ pub struct MailboxView {
|
||||||
|
|
||||||
impl MailboxView {
|
impl MailboxView {
|
||||||
/// Creates a new IMAP view into a mailbox.
|
/// Creates a new IMAP view into a mailbox.
|
||||||
/// Generates the necessary IMAP messages so that the client
|
pub async fn new(mailbox: Arc<Mailbox>) -> Self {
|
||||||
/// has a satisfactory summary of the current mailbox's state.
|
|
||||||
/// These are the messages that are sent in response to a SELECT command.
|
|
||||||
pub async fn new(mailbox: Arc<Mailbox>) -> Result<(Self, Vec<Body>)> {
|
|
||||||
let state = mailbox.current_uid_index().await;
|
let state = mailbox.current_uid_index().await;
|
||||||
|
|
||||||
let new_view = Self {
|
Self {
|
||||||
mailbox,
|
mailbox,
|
||||||
known_state: state,
|
known_state: state,
|
||||||
};
|
}
|
||||||
|
|
||||||
let mut data = Vec::<Body>::new();
|
|
||||||
data.push(new_view.exists_status()?);
|
|
||||||
data.push(new_view.recent_status()?);
|
|
||||||
data.extend(new_view.flags_status()?.into_iter());
|
|
||||||
data.push(new_view.uidvalidity_status()?);
|
|
||||||
data.push(new_view.uidnext_status()?);
|
|
||||||
|
|
||||||
Ok((new_view, data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Produces a set of IMAP responses describing the change between
|
||||||
/// what the client knows and what is actually in the mailbox.
|
/// what the client knows and what is actually in the mailbox.
|
||||||
/// This does NOT trigger a sync, it bases itself on what is currently
|
/// This does NOT trigger a sync, it bases itself on what is currently
|
||||||
/// loaded in RAM by Bayou.
|
/// loaded in RAM by Bayou.
|
||||||
pub async fn update(&mut self) -> Result<Vec<Body>> {
|
pub async fn update(&mut self) -> Result<Vec<Body<'static>>> {
|
||||||
let new_view = MailboxView {
|
let old_view: &mut Self = self;
|
||||||
mailbox: self.mailbox.clone(),
|
let new_view = Self {
|
||||||
known_state: self.mailbox.current_uid_index().await,
|
mailbox: old_view.mailbox.clone(),
|
||||||
|
known_state: old_view.mailbox.current_uid_index().await,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut data = Vec::<Body>::new();
|
let mut data = Vec::<Body>::new();
|
||||||
|
@ -446,7 +453,7 @@ impl MailboxView {
|
||||||
|
|
||||||
// - notify client of expunged mails
|
// - notify client of expunged mails
|
||||||
let mut n_expunge = 0;
|
let mut n_expunge = 0;
|
||||||
for (i, (_uid, uuid)) in self.known_state.idx_by_uid.iter().enumerate() {
|
for (i, (_uid, uuid)) in old_view.known_state.idx_by_uid.iter().enumerate() {
|
||||||
if !new_view.known_state.table.contains_key(uuid) {
|
if !new_view.known_state.table.contains_key(uuid) {
|
||||||
data.push(Body::Data(Data::Expunge(
|
data.push(Body::Data(Data::Expunge(
|
||||||
NonZeroU32::try_from((i + 1 - n_expunge) as u32).unwrap(),
|
NonZeroU32::try_from((i + 1 - n_expunge) as u32).unwrap(),
|
||||||
|
@ -456,49 +463,63 @@ impl MailboxView {
|
||||||
}
|
}
|
||||||
|
|
||||||
// - if new mails arrived, notify client of number of existing mails
|
// - if new mails arrived, notify client of number of existing mails
|
||||||
if new_view.known_state.table.len() != self.known_state.table.len() - n_expunge
|
if new_view.known_state.table.len() != old_view.known_state.table.len() - n_expunge
|
||||||
|| new_view.known_state.uidvalidity != self.known_state.uidvalidity
|
|| new_view.known_state.uidvalidity != old_view.known_state.uidvalidity
|
||||||
{
|
{
|
||||||
data.push(new_view.exists_status()?);
|
data.push(new_view.exists_status()?);
|
||||||
}
|
}
|
||||||
|
|
||||||
if new_view.known_state.uidvalidity != self.known_state.uidvalidity {
|
if new_view.known_state.uidvalidity != old_view.known_state.uidvalidity {
|
||||||
// TODO: do we want to push less/more info than this?
|
// TODO: do we want to push less/more info than this?
|
||||||
data.push(new_view.uidvalidity_status()?);
|
data.push(new_view.uidvalidity_status()?);
|
||||||
data.push(new_view.uidnext_status()?);
|
data.push(new_view.uidnext_status()?);
|
||||||
} else {
|
} else {
|
||||||
// - if flags changed for existing mails, tell client
|
// - if flags changed for existing mails, tell client
|
||||||
for (i, (_uid, uuid)) in new_view.known_state.idx_by_uid.iter().enumerate() {
|
for (i, (_uid, uuid)) in new_view.known_state.idx_by_uid.iter().enumerate() {
|
||||||
let old_mail = self.known_state.table.get(uuid);
|
let old_mail = old_view.known_state.table.get(uuid);
|
||||||
let new_mail = new_view.known_state.table.get(uuid);
|
let new_mail = new_view.known_state.table.get(uuid);
|
||||||
if old_mail.is_some() && old_mail != new_mail {
|
if old_mail.is_some() && old_mail != new_mail {
|
||||||
if let Some((uid, flags)) = new_mail {
|
if let Some((uid, flags)) = new_mail {
|
||||||
data.push(Body::Data(Data::Fetch {
|
data.push(Body::Data(Data::Fetch {
|
||||||
seq_or_uid: NonZeroU32::try_from((i + 1) as u32).unwrap(),
|
seq: NonZeroU32::try_from((i + 1) as u32).unwrap(),
|
||||||
attributes: vec![
|
items: vec![
|
||||||
MessageAttribute::Uid(*uid),
|
MessageDataItem::Uid(*uid),
|
||||||
MessageAttribute::Flags(
|
MessageDataItem::Flags(
|
||||||
flags.iter().filter_map(|f| string_to_flag(f)).collect(),
|
flags.iter().filter_map(|f| string_to_flag(f)).collect(),
|
||||||
),
|
),
|
||||||
],
|
]
|
||||||
|
.try_into()?,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*old_view = new_view;
|
||||||
*self = new_view;
|
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn store(
|
/// 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()?);
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn store<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
sequence_set: &SequenceSet,
|
sequence_set: &SequenceSet,
|
||||||
kind: &StoreType,
|
kind: &StoreType,
|
||||||
_response: &StoreResponse,
|
_response: &StoreResponse,
|
||||||
flags: &[Flag],
|
flags: &[Flag<'a>],
|
||||||
is_uid_store: &bool,
|
is_uid_store: &bool,
|
||||||
) -> Result<Vec<Body>> {
|
) -> Result<Vec<Body<'static>>> {
|
||||||
self.mailbox.opportunistic_sync().await?;
|
self.mailbox.opportunistic_sync().await?;
|
||||||
|
|
||||||
let flags = flags.iter().map(|x| x.to_string()).collect::<Vec<_>>();
|
let flags = flags.iter().map(|x| x.to_string()).collect::<Vec<_>>();
|
||||||
|
@ -522,7 +543,7 @@ impl MailboxView {
|
||||||
self.update().await
|
self.update().await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn expunge(&mut self) -> Result<Vec<Body>> {
|
pub async fn expunge(&mut self) -> Result<Vec<Body<'static>>> {
|
||||||
self.mailbox.opportunistic_sync().await?;
|
self.mailbox.opportunistic_sync().await?;
|
||||||
|
|
||||||
let deleted_flag = Flag::Deleted.to_string();
|
let deleted_flag = Flag::Deleted.to_string();
|
||||||
|
@ -569,12 +590,12 @@ impl MailboxView {
|
||||||
|
|
||||||
/// Looks up state changes in the mailbox and produces a set of IMAP
|
/// Looks up state changes in the mailbox and produces a set of IMAP
|
||||||
/// responses describing the new state.
|
/// responses describing the new state.
|
||||||
pub async fn fetch(
|
pub async fn fetch<'b>(
|
||||||
&self,
|
&self,
|
||||||
sequence_set: &SequenceSet,
|
sequence_set: &SequenceSet,
|
||||||
attributes: &MacroOrFetchAttributes,
|
attributes: &'b MacroOrMessageDataItemNames<'b>,
|
||||||
is_uid_fetch: &bool,
|
is_uid_fetch: &bool,
|
||||||
) -> Result<Vec<Body>> {
|
) -> Result<Vec<Body<'b>>> {
|
||||||
let ap = AttributesProxy::new(attributes, *is_uid_fetch);
|
let ap = AttributesProxy::new(attributes, *is_uid_fetch);
|
||||||
|
|
||||||
// Prepare data
|
// Prepare data
|
||||||
|
@ -619,31 +640,37 @@ impl MailboxView {
|
||||||
selection.with_bodies(bodies.as_slice());
|
selection.with_bodies(bodies.as_slice());
|
||||||
|
|
||||||
// Build mail selection views
|
// Build mail selection views
|
||||||
let mut views = selection.build()?;
|
let views = selection.build()?;
|
||||||
|
|
||||||
// Filter views to build the result
|
// Filter views to build the result
|
||||||
let ret = views
|
// Also identify what must be put as seen
|
||||||
.iter_mut()
|
let filtered_view = views
|
||||||
.filter_map(|mv| mv.filter(&ap).ok())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
// Register seen flags
|
|
||||||
let future_flags = views
|
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|mv| mv.add_seen)
|
.filter_map(|mv| mv.filter(&ap).ok().map(|(body, seen)| (mv, body, seen)))
|
||||||
.map(|mv| async move {
|
.collect::<Vec<_>>();
|
||||||
|
// Register seen flags
|
||||||
|
let future_flags = filtered_view
|
||||||
|
.iter()
|
||||||
|
.filter(|(_mv, _body, seen)| matches!(seen, SeenFlag::MustAdd))
|
||||||
|
.map(|(mv, _body, _seen)| async move {
|
||||||
let seen_flag = Flag::Seen.to_string();
|
let seen_flag = Flag::Seen.to_string();
|
||||||
self.mailbox.add_flags(mv.ids.uuid, &[seen_flag]).await?;
|
self.mailbox.add_flags(mv.ids.uuid, &[seen_flag]).await?;
|
||||||
Ok::<_, anyhow::Error>(())
|
Ok::<_, anyhow::Error>(())
|
||||||
})
|
})
|
||||||
.collect::<FuturesOrdered<_>>();
|
.collect::<FuturesOrdered<_>>();
|
||||||
|
|
||||||
future_flags
|
future_flags
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.await
|
.await
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Result<_, _>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
|
|
||||||
Ok(ret)
|
let command_body = filtered_view
|
||||||
|
.into_iter()
|
||||||
|
.map(|(_mv, body, _seen)| body)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Ok(command_body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
@ -717,7 +744,7 @@ impl MailboxView {
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
/// Produce an OK [UIDVALIDITY _] message corresponding to `known_state`
|
/// Produce an OK [UIDVALIDITY _] message corresponding to `known_state`
|
||||||
fn uidvalidity_status(&self) -> Result<Body> {
|
fn uidvalidity_status(&self) -> Result<Body<'static>> {
|
||||||
let uid_validity = Status::ok(
|
let uid_validity = Status::ok(
|
||||||
None,
|
None,
|
||||||
Some(Code::UidValidity(self.uidvalidity())),
|
Some(Code::UidValidity(self.uidvalidity())),
|
||||||
|
@ -732,7 +759,7 @@ impl MailboxView {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Produce an OK [UIDNEXT _] message corresponding to `known_state`
|
/// Produce an OK [UIDNEXT _] message corresponding to `known_state`
|
||||||
fn uidnext_status(&self) -> Result<Body> {
|
fn uidnext_status(&self) -> Result<Body<'static>> {
|
||||||
let next_uid = Status::ok(
|
let next_uid = Status::ok(
|
||||||
None,
|
None,
|
||||||
Some(Code::UidNext(self.uidnext())),
|
Some(Code::UidNext(self.uidnext())),
|
||||||
|
@ -748,7 +775,7 @@ impl MailboxView {
|
||||||
|
|
||||||
/// Produce an EXISTS message corresponding to the number of mails
|
/// Produce an EXISTS message corresponding to the number of mails
|
||||||
/// in `known_state`
|
/// in `known_state`
|
||||||
fn exists_status(&self) -> Result<Body> {
|
fn exists_status(&self) -> Result<Body<'static>> {
|
||||||
Ok(Body::Data(Data::Exists(self.exists()?)))
|
Ok(Body::Data(Data::Exists(self.exists()?)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -758,7 +785,7 @@ impl MailboxView {
|
||||||
|
|
||||||
/// Produce a RECENT message corresponding to the number of
|
/// Produce a RECENT message corresponding to the number of
|
||||||
/// recent mails in `known_state`
|
/// recent mails in `known_state`
|
||||||
fn recent_status(&self) -> Result<Body> {
|
fn recent_status(&self) -> Result<Body<'static>> {
|
||||||
Ok(Body::Data(Data::Recent(self.recent()?)))
|
Ok(Body::Data(Data::Recent(self.recent()?)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -774,27 +801,48 @@ impl MailboxView {
|
||||||
|
|
||||||
/// Produce a FLAGS and a PERMANENTFLAGS message that indicates
|
/// Produce a FLAGS and a PERMANENTFLAGS message that indicates
|
||||||
/// the flags that are in `known_state` + default flags
|
/// the flags that are in `known_state` + default flags
|
||||||
fn flags_status(&self) -> Result<Vec<Body>> {
|
fn flags_status(&self) -> Result<Vec<Body<'static>>> {
|
||||||
let mut flags: Vec<Flag> = self
|
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
|
.known_state
|
||||||
.idx_by_flag
|
.idx_by_flag
|
||||||
.flags()
|
.flags()
|
||||||
.filter_map(|f| string_to_flag(f))
|
.filter_map(|f| match string_to_flag(f) {
|
||||||
|
Some(FlagFetch::Flag(fl)) => Some(fl),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
// 1.b Merge it with our default flags list
|
||||||
for f in DEFAULT_FLAGS.iter() {
|
for f in DEFAULT_FLAGS.iter() {
|
||||||
if !flags.contains(f) {
|
if !known_flags.contains(f) {
|
||||||
flags.push(f.clone());
|
known_flags.push(f.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut ret = vec![Body::Data(Data::Flags(flags.clone()))];
|
// 1.c Create the IMAP message
|
||||||
|
body.push(Body::Data(Data::Flags(known_flags.clone())));
|
||||||
|
|
||||||
flags.push(Flag::Permanent);
|
// 2. Returning flags that are persisted
|
||||||
let permanent_flags =
|
// 2.a Always advertise our default flags
|
||||||
Status::ok(None, Some(Code::PermanentFlags(flags)), "Flags permitted")
|
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)?;
|
.map_err(Error::msg)?;
|
||||||
ret.push(Body::Status(permanent_flags));
|
body.push(Body::Status(permanent_flags));
|
||||||
|
|
||||||
Ok(ret)
|
// Done!
|
||||||
|
Ok(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn unseen_count(&self) -> usize {
|
pub(crate) fn unseen_count(&self) -> usize {
|
||||||
|
@ -809,21 +857,21 @@ impl MailboxView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_to_flag(f: &str) -> Option<Flag> {
|
fn string_to_flag(f: &str) -> Option<FlagFetch<'static>> {
|
||||||
match f.chars().next() {
|
match f.chars().next() {
|
||||||
Some('\\') => match f {
|
Some('\\') => match f {
|
||||||
"\\Seen" => Some(Flag::Seen),
|
"\\Seen" => Some(FlagFetch::Flag(Flag::Seen)),
|
||||||
"\\Answered" => Some(Flag::Answered),
|
"\\Answered" => Some(FlagFetch::Flag(Flag::Answered)),
|
||||||
"\\Flagged" => Some(Flag::Flagged),
|
"\\Flagged" => Some(FlagFetch::Flag(Flag::Flagged)),
|
||||||
"\\Deleted" => Some(Flag::Deleted),
|
"\\Deleted" => Some(FlagFetch::Flag(Flag::Deleted)),
|
||||||
"\\Draft" => Some(Flag::Draft),
|
"\\Draft" => Some(FlagFetch::Flag(Flag::Draft)),
|
||||||
"\\Recent" => Some(Flag::Recent),
|
"\\Recent" => Some(FlagFetch::Recent),
|
||||||
_ => match Atom::try_from(f.strip_prefix('\\').unwrap().to_string()) {
|
_ => match Atom::try_from(f.strip_prefix('\\').unwrap().to_string()) {
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
tracing::error!(flag=%f, "Unable to encode flag as IMAP atom");
|
tracing::error!(flag=%f, "Unable to encode flag as IMAP atom");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
Ok(a) => Some(Flag::Extension(a)),
|
Ok(a) => Some(FlagFetch::Flag(Flag::system(a))),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Some(_) => match Atom::try_from(f.to_string()) {
|
Some(_) => match Atom::try_from(f.to_string()) {
|
||||||
|
@ -831,7 +879,7 @@ fn string_to_flag(f: &str) -> Option<Flag> {
|
||||||
tracing::error!(flag=%f, "Unable to encode flag as IMAP atom");
|
tracing::error!(flag=%f, "Unable to encode flag as IMAP atom");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
Ok(a) => Some(Flag::Keyword(a)),
|
Ok(a) => Some(FlagFetch::Flag(Flag::keyword(a))),
|
||||||
},
|
},
|
||||||
None => None,
|
None => None,
|
||||||
}
|
}
|
||||||
|
@ -858,7 +906,7 @@ fn string_to_flag(f: &str) -> Option<Flag> {
|
||||||
|
|
||||||
//@FIXME return an error if the envelope is invalid instead of panicking
|
//@FIXME return an error if the envelope is invalid instead of panicking
|
||||||
//@FIXME some fields must be defaulted if there are not set.
|
//@FIXME some fields must be defaulted if there are not set.
|
||||||
fn message_envelope(msg: &imf::Imf) -> Envelope {
|
fn message_envelope(msg: &imf::Imf) -> Envelope<'static> {
|
||||||
let from = msg.from.iter().map(convert_mbx).collect::<Vec<_>>();
|
let from = msg.from.iter().map(convert_mbx).collect::<Vec<_>>();
|
||||||
|
|
||||||
Envelope {
|
Envelope {
|
||||||
|
@ -900,7 +948,7 @@ fn message_envelope(msg: &imf::Imf) -> Envelope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_addresses(addrlist: &Vec<imf::address::AddressRef>) -> Vec<Address> {
|
fn convert_addresses(addrlist: &Vec<imf::address::AddressRef>) -> Vec<Address<'static>> {
|
||||||
let mut acc = vec![];
|
let mut acc = vec![];
|
||||||
for item in addrlist {
|
for item in addrlist {
|
||||||
match item {
|
match item {
|
||||||
|
@ -911,23 +959,23 @@ fn convert_addresses(addrlist: &Vec<imf::address::AddressRef>) -> Vec<Address> {
|
||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address {
|
fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address<'static> {
|
||||||
Address::new(
|
Address {
|
||||||
NString(
|
name: NString(
|
||||||
addr.name
|
addr.name
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|x| IString::try_from(x.to_string()).unwrap()),
|
.map(|x| IString::try_from(x.to_string()).unwrap()),
|
||||||
),
|
),
|
||||||
// SMTP at-domain-list (source route) seems obsolete since at least 1991
|
// SMTP at-domain-list (source route) seems obsolete since at least 1991
|
||||||
// https://www.mhonarc.org/archive/html/ietf-822/1991-06/msg00060.html
|
// https://www.mhonarc.org/archive/html/ietf-822/1991-06/msg00060.html
|
||||||
NString(None),
|
adl: NString(None),
|
||||||
NString(Some(
|
mailbox: NString(Some(
|
||||||
IString::try_from(addr.addrspec.local_part.to_string()).unwrap(),
|
IString::try_from(addr.addrspec.local_part.to_string()).unwrap(),
|
||||||
)),
|
)),
|
||||||
NString(Some(
|
host: NString(Some(
|
||||||
IString::try_from(addr.addrspec.domain.to_string()).unwrap(),
|
IString::try_from(addr.addrspec.domain.to_string()).unwrap(),
|
||||||
)),
|
)),
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -945,19 +993,23 @@ b fetch 29878:29879 (BODY)
|
||||||
b OK Fetch completed (0.001 + 0.000 secs).
|
b OK Fetch completed (0.001 + 0.000 secs).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result<BodyStructure> {
|
fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result<BodyStructure<'static>> {
|
||||||
match part {
|
match part {
|
||||||
AnyPart::Mult(x) => {
|
AnyPart::Mult(x) => {
|
||||||
let itype = &x.mime.interpreted_type;
|
let itype = &x.mime.interpreted_type;
|
||||||
let subtype = IString::try_from(itype.subtype.to_string())
|
let subtype = IString::try_from(itype.subtype.to_string())
|
||||||
.unwrap_or(unchecked_istring("alternative"));
|
.unwrap_or(unchecked_istring("alternative"));
|
||||||
|
|
||||||
Ok(BodyStructure::Multi {
|
let inner_bodies = x
|
||||||
bodies: x
|
|
||||||
.children
|
.children
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|inner| build_imap_email_struct(&inner).ok())
|
.filter_map(|inner| build_imap_email_struct(&inner).ok())
|
||||||
.collect(),
|
.collect::<Vec<_>>();
|
||||||
|
NonEmptyVec::validate(&inner_bodies)?;
|
||||||
|
let bodies = NonEmptyVec::unvalidated(inner_bodies);
|
||||||
|
|
||||||
|
Ok(BodyStructure::Multi {
|
||||||
|
bodies,
|
||||||
subtype,
|
subtype,
|
||||||
extension_data: None,
|
extension_data: None,
|
||||||
/*Some(MultipartExtensionData {
|
/*Some(MultipartExtensionData {
|
||||||
|
@ -996,7 +1048,7 @@ fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result<BodyStructure> {
|
||||||
number_of_lines: nol(x.body),
|
number_of_lines: nol(x.body),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
extension: None,
|
extension_data: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
AnyPart::Bin(x) => {
|
AnyPart::Bin(x) => {
|
||||||
|
@ -1009,7 +1061,8 @@ fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result<BodyStructure> {
|
||||||
};
|
};
|
||||||
let ct = x.mime.fields.ctype.as_ref().unwrap_or(&default);
|
let ct = x.mime.fields.ctype.as_ref().unwrap_or(&default);
|
||||||
|
|
||||||
let type_ = IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err(
|
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"),
|
anyhow!("Unable to build IString from given Content-Type type given"),
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
|
@ -1021,9 +1074,9 @@ fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result<BodyStructure> {
|
||||||
Ok(BodyStructure::Single {
|
Ok(BodyStructure::Single {
|
||||||
body: FetchBody {
|
body: FetchBody {
|
||||||
basic,
|
basic,
|
||||||
specific: SpecificFields::Basic { type_, subtype },
|
specific: SpecificFields::Basic { r#type, subtype },
|
||||||
},
|
},
|
||||||
extension: None,
|
extension_data: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
AnyPart::Msg(x) => {
|
AnyPart::Msg(x) => {
|
||||||
|
@ -1033,12 +1086,12 @@ fn build_imap_email_struct<'a>(part: &AnyPart<'a>) -> Result<BodyStructure> {
|
||||||
body: FetchBody {
|
body: FetchBody {
|
||||||
basic,
|
basic,
|
||||||
specific: SpecificFields::Message {
|
specific: SpecificFields::Message {
|
||||||
envelope: message_envelope(&x.imf),
|
envelope: Box::new(message_envelope(&x.imf)),
|
||||||
body_structure: Box::new(build_imap_email_struct(x.child.as_ref())?),
|
body_structure: Box::new(build_imap_email_struct(x.child.as_ref())?),
|
||||||
number_of_lines: nol(x.raw_part),
|
number_of_lines: nol(x.raw_part),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
extension: None,
|
extension_data: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1059,7 +1112,7 @@ fn unchecked_istring(s: &'static str) -> IString {
|
||||||
IString::try_from(s).expect("this value is expected to be a valid imap-codec::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<BasicFields> {
|
fn basic_fields(m: &mime::NaiveMIME, sz: usize) -> Result<BasicFields<'static>> {
|
||||||
let parameter_list = m
|
let parameter_list = m
|
||||||
.ctype
|
.ctype
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
@ -1136,8 +1189,7 @@ fn get_message_section<'a>(
|
||||||
.ok_or(anyhow!("Part must be a message"))?;
|
.ok_or(anyhow!("Part must be a message"))?;
|
||||||
match section {
|
match section {
|
||||||
Some(FetchSection::Text(None)) => Ok(msg.raw_body.into()),
|
Some(FetchSection::Text(None)) => Ok(msg.raw_body.into()),
|
||||||
Some(FetchSection::Text(Some(part))) => {
|
Some(FetchSection::Text(Some(part))) => map_subpart(parsed, part.0.as_ref(), |part_msg| {
|
||||||
map_subpart(parsed, part.0.as_slice(), |part_msg| {
|
|
||||||
Ok(part_msg
|
Ok(part_msg
|
||||||
.as_message()
|
.as_message()
|
||||||
.ok_or(Error::msg(
|
.ok_or(Error::msg(
|
||||||
|
@ -1145,11 +1197,10 @@ fn get_message_section<'a>(
|
||||||
))?
|
))?
|
||||||
.raw_body
|
.raw_body
|
||||||
.into())
|
.into())
|
||||||
})
|
}),
|
||||||
}
|
|
||||||
Some(FetchSection::Header(part)) => map_subpart(
|
Some(FetchSection::Header(part)) => map_subpart(
|
||||||
parsed,
|
parsed,
|
||||||
part.as_ref().map(|p| p.0.as_slice()).unwrap_or(&[]),
|
part.as_ref().map(|p| p.0.as_ref()).unwrap_or(&[]),
|
||||||
|part_msg| {
|
|part_msg| {
|
||||||
Ok(part_msg
|
Ok(part_msg
|
||||||
.as_message()
|
.as_message()
|
||||||
|
@ -1165,17 +1216,18 @@ fn get_message_section<'a>(
|
||||||
) => {
|
) => {
|
||||||
let invert = matches!(section, Some(FetchSection::HeaderFieldsNot(_, _)));
|
let invert = matches!(section, Some(FetchSection::HeaderFieldsNot(_, _)));
|
||||||
let fields = fields
|
let fields = fields
|
||||||
|
.as_ref()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| match x {
|
.map(|x| match x {
|
||||||
AString::Atom(a) => a.as_bytes(),
|
AString::Atom(a) => a.inner().as_bytes(),
|
||||||
AString::String(IString::Literal(l)) => l.as_slice(),
|
AString::String(IString::Literal(l)) => l.as_ref(),
|
||||||
AString::String(IString::Quoted(q)) => q.as_bytes(),
|
AString::String(IString::Quoted(q)) => q.inner().as_bytes(),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
map_subpart(
|
map_subpart(
|
||||||
parsed,
|
parsed,
|
||||||
part.as_ref().map(|p| p.0.as_slice()).unwrap_or(&[]),
|
part.as_ref().map(|p| p.0.as_ref()).unwrap_or(&[]),
|
||||||
|part_msg| {
|
|part_msg| {
|
||||||
let mut ret = vec![];
|
let mut ret = vec![];
|
||||||
for f in &part_msg.mime().kv {
|
for f in &part_msg.mime().kv {
|
||||||
|
@ -1195,7 +1247,7 @@ fn get_message_section<'a>(
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Some(FetchSection::Part(part)) => map_subpart(parsed, part.0.as_slice(), |part| {
|
Some(FetchSection::Part(part)) => map_subpart(parsed, part.0.as_ref(), |part| {
|
||||||
let bytes = match &part {
|
let bytes = match &part {
|
||||||
AnyPart::Txt(p) => p.body,
|
AnyPart::Txt(p) => p.body,
|
||||||
AnyPart::Bin(p) => p.body,
|
AnyPart::Bin(p) => p.body,
|
||||||
|
@ -1204,7 +1256,7 @@ fn get_message_section<'a>(
|
||||||
};
|
};
|
||||||
Ok(bytes.to_vec().into())
|
Ok(bytes.to_vec().into())
|
||||||
}),
|
}),
|
||||||
Some(FetchSection::Mime(part)) => map_subpart(parsed, part.0.as_slice(), |part| {
|
Some(FetchSection::Mime(part)) => map_subpart(parsed, part.0.as_ref(), |part| {
|
||||||
let bytes = match &part {
|
let bytes = match &part {
|
||||||
AnyPart::Txt(p) => p.mime.fields.raw,
|
AnyPart::Txt(p) => p.mime.fields.raw,
|
||||||
AnyPart::Bin(p) => p.mime.fields.raw,
|
AnyPart::Bin(p) => p.mime.fields.raw,
|
||||||
|
@ -1246,13 +1298,13 @@ mod tests {
|
||||||
use crate::cryptoblob;
|
use crate::cryptoblob;
|
||||||
use crate::mail::unique_ident;
|
use crate::mail::unique_ident;
|
||||||
use imap_codec::codec::Encode;
|
use imap_codec::codec::Encode;
|
||||||
use imap_codec::imap_types::fetch_attributes::Section;
|
use imap_codec::imap_types::fetch::Section;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mailview_body_ext() -> Result<()> {
|
fn mailview_body_ext() -> Result<()> {
|
||||||
let ap = AttributesProxy::new(
|
let ap = AttributesProxy::new(
|
||||||
&MacroOrFetchAttributes::FetchAttributes(vec![FetchAttribute::BodyExt {
|
&MacroOrMessageDataItemNames::FetchAttributes(vec![MessageDataItemName::BodyExt {
|
||||||
section: Some(Section::Header(None)),
|
section: Some(Section::Header(None)),
|
||||||
partial: None,
|
partial: None,
|
||||||
peek: false,
|
peek: false,
|
||||||
|
@ -1281,14 +1333,13 @@ mod tests {
|
||||||
content,
|
content,
|
||||||
meta: &meta,
|
meta: &meta,
|
||||||
flags: &flags,
|
flags: &flags,
|
||||||
add_seen: false,
|
|
||||||
};
|
};
|
||||||
let res_body = mv.filter(&ap)?;
|
let res_body = mv.filter(&ap)?;
|
||||||
|
|
||||||
let fattr = match res_body {
|
let fattr = match res_body {
|
||||||
Body::Data(Data::Fetch {
|
Body::Data(Data::Fetch {
|
||||||
seq_or_uid: _seq,
|
seq: _seq,
|
||||||
attributes: attr,
|
items: attr,
|
||||||
}) => Ok(attr),
|
}) => Ok(attr),
|
||||||
_ => Err(anyhow!("Not a fetch body")),
|
_ => Err(anyhow!("Not a fetch body")),
|
||||||
}?;
|
}?;
|
||||||
|
@ -1296,7 +1347,7 @@ mod tests {
|
||||||
assert_eq!(fattr.len(), 1);
|
assert_eq!(fattr.len(), 1);
|
||||||
|
|
||||||
let (sec, _orig, _data) = match &fattr[0] {
|
let (sec, _orig, _data) = match &fattr[0] {
|
||||||
MessageAttribute::BodyExt {
|
MessageDataItemName::BodyExt {
|
||||||
section,
|
section,
|
||||||
origin,
|
origin,
|
||||||
data,
|
data,
|
||||||
|
@ -1349,7 +1400,7 @@ mod tests {
|
||||||
let message = eml_codec::parse_message(&txt).unwrap().1;
|
let message = eml_codec::parse_message(&txt).unwrap().1;
|
||||||
|
|
||||||
let mut resp = Vec::new();
|
let mut resp = Vec::new();
|
||||||
MessageAttribute::Body(build_imap_email_struct(&message.child)?)
|
MessageDataItemName::Body(build_imap_email_struct(&message.child)?)
|
||||||
.encode(&mut resp)
|
.encode(&mut resp)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
@ -1,34 +1,26 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use imap_codec::imap_types::command::Command;
|
use imap_codec::imap_types::command::Command;
|
||||||
use imap_codec::imap_types::core::Tag;
|
use imap_codec::imap_types::core::Tag;
|
||||||
use imap_codec::imap_types::response::{Code, Data, Status, StatusKind};
|
use imap_codec::imap_types::response::{Code, Data, Status};
|
||||||
|
|
||||||
pub struct ResponseBuilder {
|
pub enum Body<'a> {
|
||||||
status: StatusKind,
|
Data(Data<'a>),
|
||||||
tag: Option<Tag<'static>>,
|
Status(Status<'a>),
|
||||||
code: Option<Code<'static>>,
|
}
|
||||||
|
|
||||||
|
pub struct ResponseBuilder<'a> {
|
||||||
|
tag: Option<Tag<'a>>,
|
||||||
|
code: Option<Code<'a>>,
|
||||||
text: String,
|
text: String,
|
||||||
data: Vec<Data<'static>>,
|
body: Vec<Body<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Default for ResponseBuilder {
|
impl<'a> ResponseBuilder<'a> {
|
||||||
fn default() -> ResponseBuilder {
|
pub fn to_req(mut self, cmd: &Command<'a>) -> Self {
|
||||||
ResponseBuilder {
|
self.tag = Some(cmd.tag.clone());
|
||||||
status: StatusKind::Bad,
|
|
||||||
tag: None,
|
|
||||||
code: None,
|
|
||||||
text: "".to_string(),
|
|
||||||
data: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseBuilder {
|
|
||||||
pub fn to_req(mut self, cmd: &Command) -> Self {
|
|
||||||
self.tag = Some(cmd.tag);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn tag(mut self, tag: Tag) -> Self {
|
pub fn tag(mut self, tag: Tag<'a>) -> Self {
|
||||||
self.tag = Some(tag);
|
self.tag = Some(tag);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -38,60 +30,81 @@ impl ResponseBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn code(mut self, code: Code) -> Self {
|
pub fn code(mut self, code: Code<'a>) -> Self {
|
||||||
self.code = Some(code);
|
self.code = Some(code);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn data(mut self, data: Data) -> Self {
|
pub fn data(mut self, data: Data<'a>) -> Self {
|
||||||
self.data.push(data);
|
self.body.push(Body::Data(data));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_data(mut self, data: Vec<Data>) -> Self {
|
pub fn many_data(mut self, data: Vec<Data<'a>>) -> Self {
|
||||||
self.data = data;
|
for d in data.into_iter() {
|
||||||
|
self = self.data(d);
|
||||||
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<Response> {
|
pub fn info(mut self, status: Status<'a>) -> Self {
|
||||||
|
self.body.push(Body::Status(status));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn many_info(mut self, status: Vec<Status<'a>>) -> Self {
|
||||||
|
for d in status.into_iter() {
|
||||||
|
self = self.info(d);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_body(mut self, body: Vec<Body<'a>>) -> Self {
|
||||||
|
self.body = body;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ok(self) -> Result<Response<'a>> {
|
||||||
Ok(Response {
|
Ok(Response {
|
||||||
status: Status::new(self.tag, self.status, self.code, self.text)?,
|
completion: Status::ok(self.tag, self.code, self.text)?,
|
||||||
data: self.data,
|
body: self.body,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn no(self) -> Result<Response<'a>> {
|
||||||
|
Ok(Response {
|
||||||
|
completion: Status::no(self.tag, self.code, self.text)?,
|
||||||
|
body: self.body,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bad(self) -> Result<Response<'a>> {
|
||||||
|
Ok(Response {
|
||||||
|
completion: Status::bad(self.tag, self.code, self.text)?,
|
||||||
|
body: self.body,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Response {
|
pub struct Response<'a> {
|
||||||
data: Vec<Data<'static>>,
|
body: Vec<Body<'a>>,
|
||||||
status: Status<'static>,
|
completion: Status<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl<'a> Response<'a> {
|
||||||
pub fn ok() -> ResponseBuilder {
|
pub fn build() -> ResponseBuilder<'a> {
|
||||||
ResponseBuilder {
|
ResponseBuilder {
|
||||||
status: StatusKind::Ok,
|
tag: None,
|
||||||
..ResponseBuilder::default()
|
code: None,
|
||||||
|
text: "".to_string(),
|
||||||
|
body: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn no() -> ResponseBuilder {
|
pub fn bye() -> Result<Response<'a>> {
|
||||||
ResponseBuilder {
|
|
||||||
status: StatusKind::No,
|
|
||||||
..ResponseBuilder::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bad() -> ResponseBuilder {
|
|
||||||
ResponseBuilder {
|
|
||||||
status: StatusKind::Bad,
|
|
||||||
..ResponseBuilder::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bye() -> Result<Response> {
|
|
||||||
Ok(Response {
|
Ok(Response {
|
||||||
status: Status::bye(None, "bye")?,
|
completion: Status::bye(None, "bye")?,
|
||||||
data: vec![],
|
body: vec![],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue