aerogramme/src/imap/command/selected.rs

66 lines
1.8 KiB
Rust
Raw Normal View History

2022-06-29 11:41:05 +00:00
use anyhow::Result;
2022-06-29 10:50:44 +00:00
use boitalettres::proto::Request;
2022-06-20 16:09:20 +00:00
use boitalettres::proto::Response;
use imap_codec::types::command::CommandBody;
2022-06-29 10:52:58 +00:00
2022-06-20 16:09:20 +00:00
use imap_codec::types::fetch_attributes::MacroOrFetchAttributes;
2022-06-29 10:52:58 +00:00
2022-06-20 16:09:20 +00:00
use imap_codec::types::sequence::SequenceSet;
use crate::imap::command::authenticated;
2022-06-22 15:25:04 +00:00
use crate::imap::flow;
use crate::imap::mailbox_view::MailboxView;
2022-06-29 10:52:58 +00:00
2022-06-29 11:16:58 +00:00
use crate::mail::user::User;
2022-06-20 16:09:20 +00:00
2022-06-29 10:50:44 +00:00
pub struct SelectedContext<'a> {
pub req: &'a Request,
2022-06-29 11:16:58 +00:00
pub user: &'a User,
pub mailbox: &'a mut MailboxView,
2022-06-29 10:50:44 +00:00
}
2022-06-22 15:25:04 +00:00
2022-06-29 10:50:44 +00:00
pub async fn dispatch<'a>(ctx: SelectedContext<'a>) -> Result<(Response, flow::Transition)> {
match &ctx.req.command.body {
2022-06-29 15:58:31 +00:00
CommandBody::Noop => ctx.noop().await,
2022-06-22 15:26:52 +00:00
CommandBody::Fetch {
sequence_set,
attributes,
uid,
} => ctx.fetch(sequence_set, attributes, uid).await,
2022-06-29 10:50:44 +00:00
_ => {
let ctx = authenticated::AuthenticatedContext {
req: ctx.req,
user: ctx.user,
};
authenticated::dispatch(ctx).await
}
2022-06-20 16:09:20 +00:00
}
}
2022-06-22 15:25:04 +00:00
2022-06-20 16:09:20 +00:00
// --- PRIVATE ---
2022-06-29 10:50:44 +00:00
impl<'a> SelectedContext<'a> {
2022-06-17 16:39:36 +00:00
pub async fn fetch(
2022-06-29 10:50:44 +00:00
self,
2022-06-29 17:24:21 +00:00
sequence_set: &SequenceSet,
attributes: &MacroOrFetchAttributes,
uid: &bool,
2022-06-22 15:26:52 +00:00
) -> Result<(Response, flow::Transition)> {
2022-06-29 18:00:38 +00:00
match self.mailbox.fetch(sequence_set, attributes, uid).await {
Ok(resp) => Ok((
Response::ok("FETCH completed")?.with_body(resp),
flow::Transition::None,
)),
Err(e) => Ok((Response::no(&e.to_string())?, flow::Transition::None)),
}
2022-06-17 16:39:36 +00:00
}
2022-06-29 15:58:31 +00:00
pub async fn noop(self) -> Result<(Response, flow::Transition)> {
let updates = self.mailbox.update().await?;
Ok((
2022-06-29 17:24:21 +00:00
Response::ok("NOOP completed.")?.with_body(updates),
2022-06-29 15:58:31 +00:00
flow::Transition::None,
))
}
2022-06-20 16:09:20 +00:00
}