aerogramme/src/imap/command/examined.rs

143 lines
4.6 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-03 19:53:07 +00:00
use crate::imap::capability::{ClientCapability, ServerCapability};
2024-01-02 21:32:02 +00:00
use crate::imap::command::{anystate, authenticated};
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-02 19:23:33 +00:00
pub req: &'a Command<'static>,
2022-06-30 12:02:57 +00:00
pub user: &'a Arc<User>,
pub mailbox: &'a mut MailboxView,
2024-01-03 11:29:19 +00:00
pub server_capabilities: &'a ServerCapability,
2024-01-03 19:53:07 +00:00
pub client_capabilities: &'a mut ClientCapability,
}
2024-01-02 19:23:33 +00:00
pub async fn dispatch(ctx: ExaminedContext<'_>) -> Result<(Response<'static>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
match &ctx.req.body {
// Any State
// noop is specific to this state
2024-01-03 14:00:05 +00:00
CommandBody::Capability => {
anystate::capability(ctx.req.tag.clone(), ctx.server_capabilities)
}
2024-01-02 19:23:33 +00:00
CommandBody::Logout => anystate::logout(),
2024-01-01 18:25:28 +00:00
// Specific to the EXAMINE state (specialization of the SELECTED state)
// ~3 commands -> close, fetch, search + NOOP
2024-01-03 08:21:46 +00:00
CommandBody::Close => ctx.close("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-03 08:21:46 +00:00
.no()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
)),
2024-01-03 08:21:46 +00:00
// UNSELECT extension (rfc3691)
CommandBody::Unselect => ctx.close("UNSELECT").await,
2024-01-02 21:32:02 +00:00
// In examined mode, we fallback to authenticated when needed
_ => {
authenticated::dispatch(authenticated::AuthenticatedContext {
req: ctx.req,
2024-01-03 11:29:19 +00:00
server_capabilities: ctx.server_capabilities,
2024-01-03 19:53:07 +00:00
client_capabilities: ctx.client_capabilities,
2024-01-02 21:32:02 +00:00
user: ctx.user,
})
.await
}
}
}
// --- 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-03 08:21:46 +00:00
async fn close(self, kind: &str) -> Result<(Response<'static>, 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)
2024-01-03 08:21:46 +00:00
.message(format!("{} completed", kind))
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 19:23:33 +00:00
attributes: &'a MacroOrMessageDataItemNames<'static>,
uid: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<(Response<'static>, 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-06 17:01:44 +00:00
charset: &Option<Charset<'a>>,
criteria: &SearchKey<'a>,
uid: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<(Response<'static>, flow::Transition)> {
2024-01-06 17:01:44 +00:00
let found = self.mailbox.search(charset, criteria, *uid).await?;
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)
2024-01-06 17:01:44 +00:00
.set_body(found)
.message("SEARCH completed")
.ok()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
))
}
2024-01-02 19:23:33 +00:00
pub async fn noop(self) -> Result<(Response<'static>, flow::Transition)> {
2024-01-09 16:40:23 +00:00
self.mailbox.internal.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,
))
}
}