aerogramme/src/imap/command/selected.rs

137 lines
3.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;
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;
use crate::imap::command::examined;
2022-06-22 15:25:04 +00:00
use crate::imap::flow;
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>,
pub mailbox: &'a mut MailboxView,
2022-06-29 10:50:44 +00:00
}
2022-06-22 15:25:04 +00:00
2023-05-15 16:23:23 +00:00
pub async fn dispatch(ctx: SelectedContext<'_>) -> Result<(Response, flow::Transition)> {
2022-06-29 10:50:44 +00:00
match &ctx.req.command.body {
// 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,
} => 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,
2022-06-29 10:50:44 +00:00
_ => {
let ctx = examined::ExaminedContext {
2022-06-29 10:50:44 +00:00
req: ctx.req,
user: ctx.user,
mailbox: ctx.mailbox,
2022-06-29 10:50:44 +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)> {
// 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,
))
}
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
async fn copy(
self,
2022-07-21 10:44:58 +00:00
sequence_set: &SequenceSet,
mailbox: &MailboxCodec,
uid: &bool,
) -> 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,
2022-07-21 10:50:44 +00:00
None => {
return Ok((
Response::no("Destination mailbox does not exist")?
.with_extra_code(Code::TryCreate),
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((
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
}