aerogramme/src/imap/command/selected.rs

425 lines
13 KiB
Rust
Raw Normal View History

2024-01-11 22:02:03 +00:00
use std::num::NonZeroU64;
2024-01-18 17:03:21 +00:00
use std::sync::Arc;
2022-06-30 12:02:57 +00:00
2022-07-21 10:50:44 +00:00
use anyhow::Result;
2024-01-11 15:55:37 +00:00
use imap_codec::imap_types::command::{Command, CommandBody, FetchModifier, StoreModifier};
2024-02-22 16:31:03 +00:00
use imap_codec::imap_types::core::{Charset, Vec1};
2024-01-01 18:25:28 +00:00
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-18 17:03:21 +00:00
use crate::imap::attributes::AttributesProxy;
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, MailboxName};
2022-06-22 15:25:04 +00:00
use crate::imap::flow;
2024-01-11 22:02:03 +00:00
use crate::imap::mailbox_view::{MailboxView, UpdateParameters};
2024-01-01 18:25:28 +00:00
use crate::imap::response::Response;
2024-02-27 17:33:49 +00:00
use crate::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,
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-17 07:22:15 +00:00
pub perm: &'a flow::MailboxPerm,
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
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 this state (7 commands + NOOP)
2024-01-17 07:22:15 +00:00
CommandBody::Close => match ctx.perm {
flow::MailboxPerm::ReadWrite => ctx.close().await,
flow::MailboxPerm::ReadOnly => ctx.examine_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,
2024-01-11 15:55:37 +00:00
modifiers,
2024-01-01 18:25:28 +00:00
uid,
2024-01-18 17:03:21 +00:00
} => {
ctx.fetch(sequence_set, macro_or_item_names, modifiers, uid)
.await
}
2024-02-22 16:30:40 +00:00
//@FIXME SearchKey::And is a legacy hack, should be refactored
2024-01-01 18:25:28 +00:00
CommandBody::Search {
charset,
criteria,
uid,
2024-02-22 16:31:03 +00:00
} => {
ctx.search(charset, &SearchKey::And(criteria.clone()), uid)
.await
}
2024-01-19 16:39:55 +00:00
CommandBody::Expunge {
// UIDPLUS (rfc4315)
uid_sequence_set,
} => ctx.expunge(uid_sequence_set).await,
CommandBody::Store {
2022-06-30 11:36:21 +00:00
sequence_set,
kind,
response,
flags,
modifiers,
2022-06-30 11:36:21 +00:00
uid,
2024-01-18 17:03:21 +00:00
} => {
ctx.store(sequence_set, kind, response, flags, modifiers, 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-03 14:00:05 +00:00
CommandBody::Move {
sequence_set,
mailbox,
uid,
} => ctx.r#move(sequence_set, mailbox, uid).await,
2024-01-01 18:25:28 +00:00
2024-01-03 08:21:46 +00:00
// UNSELECT extension (rfc3691)
CommandBody::Unselect => ctx.unselect().await,
2024-01-02 21:32:02 +00:00
// In selected 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
}
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();
2024-01-19 16:39:55 +00:00
self.expunge(&None).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,
))
}
2024-01-17 07:22:15 +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)
async fn examine_close(self) -> Result<(Response<'static>, flow::Transition)> {
Ok((
Response::build()
.to_req(self.req)
.message("CLOSE completed")
.ok()?,
flow::Transition::Unselect,
))
}
2024-01-03 08:21:46 +00:00
async fn unselect(self) -> Result<(Response<'static>, flow::Transition)> {
Ok((
Response::build()
.to_req(self.req)
.message("UNSELECT completed")
.ok()?,
flow::Transition::Unselect,
))
}
2024-01-01 18:25:28 +00:00
pub async fn fetch(
self,
sequence_set: &SequenceSet,
2024-01-02 19:23:33 +00:00
attributes: &'a MacroOrMessageDataItemNames<'static>,
2024-01-11 15:55:37 +00:00
modifiers: &[FetchModifier],
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-11 22:02:03 +00:00
let ap = AttributesProxy::new(attributes, modifiers, *uid);
let mut changed_since: Option<NonZeroU64> = None;
modifiers.iter().for_each(|m| match m {
FetchModifier::ChangedSince(val) => {
changed_since = Some(*val);
2024-01-18 17:03:21 +00:00
}
2024-01-11 22:02:03 +00:00
});
2024-01-18 17:03:21 +00:00
match self
.mailbox
.fetch(sequence_set, &ap, changed_since, uid)
.await
{
Ok(resp) => {
// Capabilities enabling logic only on successful command
// (according to my understanding of the spec)
self.client_capabilities.attributes_enable(&ap);
self.client_capabilities.fetch_modifiers_enable(modifiers);
// Response to the client
Ok((
Response::build()
.to_req(self.req)
.message("FETCH completed")
.set_body(resp)
.ok()?,
flow::Transition::None,
))
2024-01-18 17:03:21 +00:00
}
2024-01-01 18:25:28 +00:00
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)> {
let (found, enable_condstore) = self.mailbox.search(charset, criteria, *uid).await?;
if enable_condstore {
self.client_capabilities.enable_condstore();
}
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)
.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?;
2024-01-01 18:25:28 +00:00
2024-01-11 22:02:03 +00:00
let updates = self.mailbox.update(UpdateParameters::default()).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)
.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-19 16:40:08 +00:00
async fn expunge(
self,
uid_sequence_set: &Option<SequenceSet>,
) -> Result<(Response<'static>, flow::Transition)> {
2024-01-17 07:22:15 +00:00
if let Some(failed) = self.fail_read_only() {
2024-01-18 17:03:21 +00:00
return Ok((failed, flow::Transition::None));
2024-01-17 07:22:15 +00:00
}
2024-01-01 18:25:28 +00:00
let tag = self.req.tag.clone();
2024-01-19 16:39:55 +00:00
let data = self.mailbox.expunge(uid_sequence_set).await?;
2022-07-13 09:00:35 +00:00
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>],
2024-01-11 15:55:37 +00:00
modifiers: &[StoreModifier],
2022-07-12 15:32:57 +00:00
uid: &bool,
2024-01-02 19:23:33 +00:00
) -> Result<(Response<'static>, flow::Transition)> {
2024-01-17 07:22:15 +00:00
if let Some(failed) = self.fail_read_only() {
2024-01-18 17:03:21 +00:00
return Ok((failed, flow::Transition::None));
2024-01-17 07:22:15 +00:00
}
2024-01-11 22:02:03 +00:00
let mut unchanged_since: Option<NonZeroU64> = None;
modifiers.iter().for_each(|m| match m {
StoreModifier::UnchangedSince(val) => {
unchanged_since = Some(*val);
2024-01-18 17:03:21 +00:00
}
2024-01-11 22:02:03 +00:00
});
let (data, modified) = self
2022-07-12 15:32:57 +00:00
.mailbox
2024-01-11 22:02:03 +00:00
.store(sequence_set, kind, response, flags, unchanged_since, uid)
2022-07-12 15:32:57 +00:00
.await?;
2024-01-12 12:01:22 +00:00
let mut ok_resp = Response::build()
2024-01-18 17:03:21 +00:00
.to_req(self.req)
.message("STORE completed")
.set_body(data);
2024-01-12 12:01:22 +00:00
match modified[..] {
[] => (),
[_head, ..] => {
2024-01-18 17:03:21 +00:00
let modified_str = format!(
"MODIFIED {}",
modified
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",")
);
ok_resp = ok_resp.code(Code::Other(CodeOther::unvalidated(
modified_str.into_bytes(),
)));
}
2024-01-12 12:01:22 +00:00
};
self.client_capabilities.store_modifiers_enable(modifiers);
2024-01-18 17:03:21 +00:00
Ok((ok_resp.ok()?, 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-17 07:22:15 +00:00
//@FIXME Could copy be valid in EXAMINE mode?
if let Some(failed) = self.fail_read_only() {
2024-01-18 17:03:21 +00:00
return Ok((failed, flow::Transition::None));
2024-01-17 07:22:15 +00:00
}
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
}
2024-01-03 14:00:05 +00:00
async fn r#move(
self,
sequence_set: &SequenceSet,
mailbox: &MailboxCodec<'a>,
uid: &bool,
) -> Result<(Response<'static>, flow::Transition)> {
2024-01-17 07:22:15 +00:00
if let Some(failed) = self.fail_read_only() {
2024-01-18 17:03:21 +00:00
return Ok((failed, flow::Transition::None));
2024-01-17 07:22:15 +00:00
}
2024-01-03 14:00:05 +00:00
let name: &str = MailboxName(mailbox).try_into()?;
let mb_opt = self.user.open_mailbox(&name).await?;
let mb = match mb_opt {
Some(mb) => mb,
None => {
return Ok((
Response::build()
.to_req(self.req)
.message("Destination mailbox does not exist")
.code(Code::TryCreate)
.no()?,
flow::Transition::None,
))
}
};
let (uidval, uid_map, data) = self.mailbox.r#move(sequence_set, mb, uid).await?;
// compute code
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((
Response::build()
.to_req(self.req)
.message("COPY completed")
.code(Code::Other(CodeOther::unvalidated(
format!("COPYUID {}", copyuid_str).into_bytes(),
)))
.set_body(data)
.ok()?,
flow::Transition::None,
))
}
2024-01-17 07:22:15 +00:00
fn fail_read_only(&self) -> Option<Response<'static>> {
match self.perm {
flow::MailboxPerm::ReadWrite => None,
2024-01-18 17:03:21 +00:00
flow::MailboxPerm::ReadOnly => Some(
Response::build()
.to_req(self.req)
.message("Write command are forbidden while exmining mailbox")
.no()
.unwrap(),
),
2024-01-17 07:22:15 +00:00
}
}
2022-06-20 16:09:20 +00:00
}