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-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;
|
2022-06-29 13:39:54 +00:00
|
|
|
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>,
|
2022-06-29 13:39:54 +00:00
|
|
|
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,
|
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)
|
2022-06-30 09:28:03 +00:00
|
|
|
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,
|
2022-06-30 09:28:03 +00:00
|
|
|
CommandBody::Expunge => ctx.expunge().await,
|
|
|
|
CommandBody::Store {
|
2022-06-30 11:36:21 +00:00
|
|
|
sequence_set,
|
|
|
|
kind,
|
|
|
|
response,
|
|
|
|
flags,
|
|
|
|
uid,
|
2022-06-30 09:28:03 +00:00
|
|
|
} => ctx.store(sequence_set, kind, response, flags, uid).await,
|
|
|
|
CommandBody::Copy {
|
2022-06-22 15:26:52 +00:00
|
|
|
sequence_set,
|
2022-06-30 09:28:03 +00:00
|
|
|
mailbox,
|
2022-06-22 15:26:52 +00:00
|
|
|
uid,
|
2022-06-30 09:28:03 +00:00
|
|
|
} => 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)> {
|
2022-06-30 09:28:03 +00:00
|
|
|
// 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();
|
2022-06-30 09:28:03 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
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-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,
|
2024-01-05 11:40:49 +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-05 11:40:49 +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-05 11:40:49 +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?;
|
2024-01-01 18:25:28 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
))
|
2022-06-30 09:28:03 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
))
|
2022-06-30 09:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-06-30 09:28:03 +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
|
|
|
}
|
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)> {
|
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
2022-06-20 16:09:20 +00:00
|
|
|
}
|