aerogramme/src/imap/command/examined.rs

125 lines
3.9 KiB
Rust
Raw Normal View History

2022-06-30 12:02:57 +00:00
use std::sync::Arc;
use anyhow::Result;
2024-01-01 18:25:28 +00:00
use imap_codec::imap_types::command::{Command, CommandBody};
use imap_codec::imap_types::core::Charset;
use imap_codec::imap_types::fetch::MacroOrMessageDataItemNames;
use imap_codec::imap_types::search::SearchKey;
2024-01-01 08:34:13 +00:00
use imap_codec::imap_types::sequence::SequenceSet;
2024-01-01 18:25:28 +00:00
use crate::imap::command::anystate;
use crate::imap::flow;
use crate::imap::mailbox_view::MailboxView;
2024-01-01 18:25:28 +00:00
use crate::imap::response::Response;
use crate::mail::user::User;
pub struct ExaminedContext<'a> {
2024-01-01 18:25:28 +00:00
pub req: &'a Command<'a>,
2022-06-30 12:02:57 +00:00
pub user: &'a Arc<User>,
pub mailbox: &'a mut MailboxView,
}
2024-01-02 14:35:23 +00:00
pub async fn dispatch<'a>(ctx: ExaminedContext<'a>) -> Result<(Response<'a>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
match &ctx.req.body {
// Any State
// noop is specific to this state
CommandBody::Capability => anystate::capability(ctx.req.tag.clone()),
CommandBody::Logout => Ok((Response::bye()?, flow::Transition::Logout)),
// Specific to the EXAMINE state (specialization of the SELECTED state)
// ~3 commands -> close, fetch, search + NOOP
CommandBody::Close => ctx.close().await,
CommandBody::Fetch {
sequence_set,
2024-01-01 18:25:28 +00:00
macro_or_item_names,
uid,
2024-01-01 18:25:28 +00:00
} => ctx.fetch(sequence_set, macro_or_item_names, uid).await,
CommandBody::Search {
charset,
criteria,
uid,
} => ctx.search(charset, criteria, uid).await,
2024-01-01 18:25:28 +00:00
CommandBody::Noop | CommandBody::Check => ctx.noop().await,
CommandBody::Expunge { .. } | CommandBody::Store { .. } => Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(ctx.req)
.message("Forbidden command: can't write in read-only mode (EXAMINE)")
2024-01-02 14:35:23 +00:00
.bad()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
)),
// The command does not belong to this state
_ => anystate::wrong_state(ctx.req.tag.clone()),
}
}
// --- PRIVATE ---
impl<'a> ExaminedContext<'a> {
2024-01-01 18:25:28 +00:00
/// CLOSE in examined state is not the same as in selected state
/// (in selected state it also does an EXPUNGE, here it doesn't)
2024-01-02 14:35:23 +00:00
async fn close(self) -> Result<(Response<'a>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message("CLOSE completed")
2024-01-02 14:35:23 +00:00
.ok()?,
2024-01-01 18:25:28 +00:00
flow::Transition::Unselect,
))
}
pub async fn fetch(
self,
sequence_set: &SequenceSet,
2024-01-02 14:35:23 +00:00
attributes: &'a MacroOrMessageDataItemNames<'a>,
uid: &bool,
2024-01-02 14:35:23 +00:00
) -> Result<(Response<'a>, flow::Transition)> {
match self.mailbox.fetch(sequence_set, attributes, uid).await {
Ok(resp) => Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message("FETCH completed")
2024-01-02 14:35:23 +00:00
.set_body(resp)
.ok()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
)),
Err(e) => Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message(e.to_string())
2024-01-02 14:35:23 +00:00
.no()?,
flow::Transition::None,
)),
}
}
pub async fn search(
self,
2024-01-01 18:25:28 +00:00
_charset: &Option<Charset<'a>>,
_criteria: &SearchKey<'a>,
2022-07-12 13:31:29 +00:00
_uid: &bool,
2024-01-02 14:35:23 +00:00
) -> Result<(Response<'a>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message("Not implemented")
2024-01-02 14:35:23 +00:00
.bad()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
))
}
2024-01-02 14:35:23 +00:00
pub async fn noop(self) -> Result<(Response<'a>, flow::Transition)> {
self.mailbox.mailbox.force_sync().await?;
let updates = self.mailbox.update().await?;
Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message("NOOP completed.")
2024-01-02 14:35:23 +00:00
.set_body(updates)
.ok()?,
flow::Transition::None,
))
}
}