aerogramme/src/imap/command/selected.rs

230 lines
6.8 KiB
Rust
Raw Normal View History

2022-06-30 12:02:57 +00:00
use std::sync::Arc;
2022-07-21 10:50:44 +00:00
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;
2024-01-01 08:34:13 +00:00
use imap_codec::imap_types::flag::{Flag, StoreResponse, StoreType};
use imap_codec::imap_types::mailbox::Mailbox as MailboxCodec;
2024-01-01 18:25:28 +00:00
use imap_codec::imap_types::response::{Code, CodeOther};
use imap_codec::imap_types::search::SearchKey;
2024-01-01 08:34:13 +00:00
use imap_codec::imap_types::sequence::SequenceSet;
2022-06-20 16:09:20 +00:00
2024-01-02 21:32:02 +00:00
use crate::imap::command::{anystate, authenticated, MailboxName};
2022-06-22 15:25:04 +00:00
use crate::imap::flow;
use crate::imap::mailbox_view::MailboxView;
2024-01-01 18:25:28 +00:00
use crate::imap::response::Response;
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> {
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,
2022-06-29 10:50:44 +00:00
}
2022-06-22 15:25:04 +00:00
2024-01-02 19:23:33 +00:00
pub async fn dispatch<'a>(
ctx: SelectedContext<'a>,
) -> 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
CommandBody::Capability => anystate::capability(ctx.req.tag.clone()),
2024-01-02 19:23:33 +00:00
CommandBody::Logout => anystate::logout(),
2024-01-01 18:25:28 +00:00
// Specific to this state (7 commands + NOOP)
CommandBody::Close => ctx.close().await,
2024-01-01 18:25:28 +00:00
CommandBody::Noop | CommandBody::Check => ctx.noop().await,
CommandBody::Fetch {
sequence_set,
macro_or_item_names,
uid,
} => ctx.fetch(sequence_set, macro_or_item_names, uid).await,
CommandBody::Search {
charset,
criteria,
uid,
} => ctx.search(charset, criteria, uid).await,
CommandBody::Expunge => ctx.expunge().await,
CommandBody::Store {
2022-06-30 11:36:21 +00:00
sequence_set,
kind,
response,
flags,
uid,
} => ctx.store(sequence_set, kind, response, flags, uid).await,
CommandBody::Copy {
2022-06-22 15:26:52 +00:00
sequence_set,
mailbox,
2022-06-22 15:26:52 +00:00
uid,
} => ctx.copy(sequence_set, mailbox, uid).await,
2024-01-01 18:25:28 +00:00
2024-01-02 21:32:02 +00:00
// In selected mode, we fallback to authenticated when needed
_ => {
authenticated::dispatch(authenticated::AuthenticatedContext {
req: ctx.req,
user: ctx.user,
})
.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> {
2024-01-02 19:23:33 +00:00
async fn close(self) -> Result<(Response<'static>, flow::Transition)> {
// We expunge messages,
// but we don't send the untagged EXPUNGE responses
2024-01-01 18:25:28 +00:00
let tag = self.req.tag.clone();
self.expunge().await?;
2024-01-01 18:25:28 +00:00
Ok((
2024-01-02 14:35:23 +00:00
Response::build().tag(tag).message("CLOSE completed").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>,
2024-01-01 18:25:28 +00:00
uid: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<(Response<'static>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
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()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
)),
}
}
pub async fn search(
self,
_charset: &Option<Charset<'a>>,
_criteria: &SearchKey<'a>,
_uid: &bool,
2024-01-02 19:23:33 +00:00
) -> 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)
.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 19:23:33 +00:00
pub async fn noop(self) -> Result<(Response<'static>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
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()?,
2024-01-01 18:25:28 +00:00
flow::Transition::None,
))
}
2024-01-02 19:23:33 +00:00
async fn expunge(self) -> Result<(Response<'static>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
let tag = self.req.tag.clone();
2022-07-13 09:00:35 +00:00
let data = self.mailbox.expunge().await?;
Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.tag(tag)
.message("EXPUNGE completed")
2024-01-02 14:35:23 +00:00
.set_body(data)
.ok()?,
2022-07-13 09:00:35 +00:00
flow::Transition::None,
))
}
async fn store(
2022-06-29 10:50:44 +00:00
self,
2022-07-12 15:32:57 +00:00
sequence_set: &SequenceSet,
kind: &StoreType,
response: &StoreResponse,
2024-01-01 18:25:28 +00:00
flags: &[Flag<'a>],
2022-07-12 15:32:57 +00:00
uid: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<(Response<'static>, flow::Transition)> {
2022-07-12 15:32:57 +00:00
let data = self
.mailbox
.store(sequence_set, kind, response, flags, uid)
.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("STORE completed")
2024-01-02 14:35:23 +00:00
.set_body(data)
.ok()?,
2022-07-12 15:32:57 +00:00
flow::Transition::None,
))
2022-06-17 16:39:36 +00:00
}
2022-06-29 15:58:31 +00:00
async fn copy(
self,
2022-07-21 10:44:58 +00:00
sequence_set: &SequenceSet,
2024-01-01 18:25:28 +00:00
mailbox: &MailboxCodec<'a>,
2022-07-21 10:44:58 +00:00
uid: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<(Response<'static>, flow::Transition)> {
2024-01-01 18:25:28 +00:00
let name: &str = MailboxName(mailbox).try_into()?;
2022-07-21 10:44:58 +00:00
let mb_opt = self.user.open_mailbox(&name).await?;
let mb = match mb_opt {
Some(mb) => mb,
2022-07-21 10:50:44 +00:00
None => {
return Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message("Destination mailbox does not exist")
.code(Code::TryCreate)
2024-01-02 14:35:23 +00:00
.no()?,
2022-07-21 10:50:44 +00:00
flow::Transition::None,
))
}
2022-07-21 10:44:58 +00:00
};
let (uidval, uid_map) = self.mailbox.copy(sequence_set, mb, uid).await?;
let copyuid_str = format!(
"{} {} {}",
uidval,
uid_map
.iter()
.map(|(sid, _)| format!("{}", sid))
.collect::<Vec<_>>()
.join(","),
uid_map
.iter()
.map(|(_, tuid)| format!("{}", tuid))
.collect::<Vec<_>>()
.join(",")
);
Ok((
2024-01-02 14:35:23 +00:00
Response::build()
2024-01-01 18:25:28 +00:00
.to_req(self.req)
.message("COPY completed")
.code(Code::Other(CodeOther::unvalidated(
format!("COPYUID {}", copyuid_str).into_bytes(),
)))
2024-01-02 14:35:23 +00:00
.ok()?,
2022-07-21 10:44:58 +00:00
flow::Transition::None,
))
2022-06-29 15:58:31 +00:00
}
2022-06-20 16:09:20 +00:00
}