2022-06-30 12:02:57 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-07-21 10:44:58 +00:00
|
|
|
use anyhow::{bail, Result};
|
2022-06-29 10:50:44 +00:00
|
|
|
use boitalettres::proto::Request;
|
2022-06-20 16:09:20 +00:00
|
|
|
use boitalettres::proto::Response;
|
|
|
|
use imap_codec::types::command::CommandBody;
|
2022-06-30 11:36:21 +00:00
|
|
|
use imap_codec::types::flag::{Flag, StoreResponse, StoreType};
|
|
|
|
use imap_codec::types::mailbox::Mailbox as MailboxCodec;
|
2022-07-21 10:44:58 +00:00
|
|
|
use imap_codec::types::response::Code;
|
2022-06-20 16:09:20 +00:00
|
|
|
use imap_codec::types::sequence::SequenceSet;
|
|
|
|
|
2022-06-30 09:28:03 +00:00
|
|
|
use crate::imap::command::examined;
|
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;
|
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> {
|
|
|
|
pub req: &'a Request,
|
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,
|
2022-06-29 10:50:44 +00:00
|
|
|
}
|
2022-06-22 15:25:04 +00:00
|
|
|
|
2022-06-29 10:50:44 +00:00
|
|
|
pub async fn dispatch<'a>(ctx: SelectedContext<'a>) -> Result<(Response, flow::Transition)> {
|
|
|
|
match &ctx.req.command.body {
|
2022-06-30 09:28:03 +00:00
|
|
|
// Only write commands here, read commands are handled in
|
|
|
|
// `examined.rs`
|
|
|
|
CommandBody::Close => ctx.close().await,
|
|
|
|
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,
|
2022-06-29 10:50:44 +00:00
|
|
|
_ => {
|
2022-06-30 09:28:03 +00:00
|
|
|
let ctx = examined::ExaminedContext {
|
2022-06-29 10:50:44 +00:00
|
|
|
req: ctx.req,
|
|
|
|
user: ctx.user,
|
2022-06-30 09:28:03 +00:00
|
|
|
mailbox: ctx.mailbox,
|
2022-06-29 10:50:44 +00:00
|
|
|
};
|
2022-06-30 09:28:03 +00:00
|
|
|
examined::dispatch(ctx).await
|
2022-06-29 10:50:44 +00:00
|
|
|
}
|
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> {
|
2022-06-30 11:36:21 +00:00
|
|
|
async fn close(self) -> Result<(Response, flow::Transition)> {
|
2022-06-30 09:28:03 +00:00
|
|
|
// We expunge messages,
|
|
|
|
// but we don't send the untagged EXPUNGE responses
|
|
|
|
self.expunge().await?;
|
|
|
|
Ok((Response::ok("CLOSE completed")?, flow::Transition::Unselect))
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:36:21 +00:00
|
|
|
async fn expunge(self) -> Result<(Response, flow::Transition)> {
|
2022-07-13 09:00:35 +00:00
|
|
|
let data = self.mailbox.expunge().await?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Response::ok("EXPUNGE completed")?.with_body(data),
|
|
|
|
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,
|
|
|
|
flags: &[Flag],
|
|
|
|
uid: &bool,
|
2022-06-22 15:26:52 +00:00
|
|
|
) -> Result<(Response, flow::Transition)> {
|
2022-07-12 15:32:57 +00:00
|
|
|
let data = self
|
|
|
|
.mailbox
|
|
|
|
.store(sequence_set, kind, response, flags, uid)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Response::ok("STORE completed")?.with_body(data),
|
|
|
|
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,
|
|
|
|
mailbox: &MailboxCodec,
|
|
|
|
uid: &bool,
|
2022-06-30 09:28:03 +00:00
|
|
|
) -> Result<(Response, flow::Transition)> {
|
2022-07-21 10:44:58 +00:00
|
|
|
let name = String::try_from(mailbox.clone())?;
|
|
|
|
|
|
|
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
|
|
|
let mb = match mb_opt {
|
|
|
|
Some(mb) => mb,
|
|
|
|
None => bail!("Mailbox does not exist"),
|
|
|
|
};
|
|
|
|
|
|
|
|
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((
|
|
|
|
Response::ok("COPY completed")?.with_extra_code(Code::Other(
|
|
|
|
"COPYUID".try_into().unwrap(),
|
|
|
|
Some(copyuid_str),
|
|
|
|
)),
|
|
|
|
flow::Transition::None,
|
|
|
|
))
|
2022-06-29 15:58:31 +00:00
|
|
|
}
|
2022-06-20 16:09:20 +00:00
|
|
|
}
|