2022-07-12 11:19:27 +00:00
|
|
|
use std::collections::BTreeMap;
|
2022-06-30 12:02:57 +00:00
|
|
|
use std::sync::Arc;
|
2024-01-20 17:34:37 +00:00
|
|
|
use thiserror::Error;
|
2022-06-30 12:02:57 +00:00
|
|
|
|
2022-07-12 14:35:11 +00:00
|
|
|
use anyhow::{anyhow, bail, Result};
|
2024-01-20 17:34:37 +00:00
|
|
|
use imap_codec::imap_types::command::{
|
|
|
|
Command, CommandBody, ListReturnItem, SelectExamineModifier,
|
|
|
|
};
|
2024-02-22 16:31:03 +00:00
|
|
|
use imap_codec::imap_types::core::{Atom, Literal, QuotedChar, Vec1};
|
2024-01-01 16:54:48 +00:00
|
|
|
use imap_codec::imap_types::datetime::DateTime;
|
2024-01-03 19:53:25 +00:00
|
|
|
use imap_codec::imap_types::extensions::enable::CapabilityEnable;
|
2024-01-01 08:34:13 +00:00
|
|
|
use imap_codec::imap_types::flag::{Flag, FlagNameAttribute};
|
|
|
|
use imap_codec::imap_types::mailbox::{ListMailbox, Mailbox as MailboxCodec};
|
2024-01-01 16:54:48 +00:00
|
|
|
use imap_codec::imap_types::response::{Code, CodeOther, Data};
|
|
|
|
use imap_codec::imap_types::status::{StatusDataItem, StatusDataItemName};
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2024-01-03 19:53:07 +00:00
|
|
|
use crate::imap::capability::{ClientCapability, ServerCapability};
|
2024-01-01 18:25:28 +00:00
|
|
|
use crate::imap::command::{anystate, MailboxName};
|
2022-06-22 14:52:38 +00:00
|
|
|
use crate::imap::flow;
|
2024-02-20 10:42:51 +00:00
|
|
|
use crate::imap::mailbox_view::{MailboxView, UpdateParameters};
|
2024-01-01 16:54:48 +00:00
|
|
|
use crate::imap::response::Response;
|
2024-02-22 16:31:03 +00:00
|
|
|
use crate::imap::Body;
|
2022-06-29 10:52:58 +00:00
|
|
|
|
2022-07-12 14:35:11 +00:00
|
|
|
use crate::mail::uidindex::*;
|
2024-02-27 17:33:49 +00:00
|
|
|
use crate::user::User;
|
2022-07-12 14:35:11 +00:00
|
|
|
use crate::mail::IMF;
|
2024-02-27 17:33:49 +00:00
|
|
|
use crate::mail::namespace::MAILBOX_HIERARCHY_DELIMITER as MBX_HIER_DELIM_RAW;
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2022-06-29 10:50:44 +00:00
|
|
|
pub struct AuthenticatedContext<'a> {
|
2024-01-02 19:23:33 +00:00
|
|
|
pub req: &'a Command<'static>,
|
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-30 12:02:57 +00:00
|
|
|
pub user: &'a Arc<User>,
|
2022-06-29 10:50:44 +00:00
|
|
|
}
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub async fn dispatch<'a>(
|
2024-01-20 17:34:37 +00:00
|
|
|
mut ctx: AuthenticatedContext<'a>,
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
match &ctx.req.body {
|
2024-01-01 18:25:28 +00:00
|
|
|
// Any state
|
|
|
|
CommandBody::Noop => anystate::noop_nothing(ctx.req.tag.clone()),
|
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 (11 commands)
|
2022-06-30 09:28:03 +00:00
|
|
|
CommandBody::Create { mailbox } => ctx.create(mailbox).await,
|
|
|
|
CommandBody::Delete { mailbox } => ctx.delete(mailbox).await,
|
2024-01-01 16:54:48 +00:00
|
|
|
CommandBody::Rename { from, to } => ctx.rename(from, to).await,
|
2022-06-22 15:26:52 +00:00
|
|
|
CommandBody::Lsub {
|
|
|
|
reference,
|
|
|
|
mailbox_wildcard,
|
2024-01-20 17:34:37 +00:00
|
|
|
} => ctx.list(reference, mailbox_wildcard, &[], true).await,
|
2022-06-22 15:26:52 +00:00
|
|
|
CommandBody::List {
|
|
|
|
reference,
|
|
|
|
mailbox_wildcard,
|
2024-01-20 17:34:37 +00:00
|
|
|
r#return,
|
|
|
|
} => ctx.list(reference, mailbox_wildcard, r#return, false).await,
|
2022-06-30 11:36:21 +00:00
|
|
|
CommandBody::Status {
|
|
|
|
mailbox,
|
2024-01-01 16:54:48 +00:00
|
|
|
item_names,
|
|
|
|
} => ctx.status(mailbox, item_names).await,
|
2022-07-12 11:19:27 +00:00
|
|
|
CommandBody::Subscribe { mailbox } => ctx.subscribe(mailbox).await,
|
|
|
|
CommandBody::Unsubscribe { mailbox } => ctx.unsubscribe(mailbox).await,
|
2024-01-11 15:55:37 +00:00
|
|
|
CommandBody::Select { mailbox, modifiers } => ctx.select(mailbox, modifiers).await,
|
|
|
|
CommandBody::Examine { mailbox, modifiers } => ctx.examine(mailbox, modifiers).await,
|
2022-07-12 14:35:11 +00:00
|
|
|
CommandBody::Append {
|
|
|
|
mailbox,
|
|
|
|
flags,
|
|
|
|
date,
|
|
|
|
message,
|
|
|
|
} => ctx.append(mailbox, flags, date, message).await,
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2024-01-03 19:53:07 +00:00
|
|
|
// rfc5161 ENABLE
|
2024-01-03 19:53:25 +00:00
|
|
|
CommandBody::Enable { capabilities } => ctx.enable(capabilities),
|
2024-01-03 19:53:07 +00:00
|
|
|
|
2024-01-01 18:25:28 +00:00
|
|
|
// Collect other commands
|
|
|
|
_ => anystate::wrong_state(ctx.req.tag.clone()),
|
2024-01-01 16:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 18:25:28 +00:00
|
|
|
// --- PRIVATE ---
|
2024-01-01 16:54:48 +00:00
|
|
|
impl<'a> AuthenticatedContext<'a> {
|
2024-01-02 19:23:33 +00:00
|
|
|
async fn create(
|
|
|
|
self,
|
|
|
|
mailbox: &MailboxCodec<'a>,
|
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name = match mailbox {
|
|
|
|
MailboxCodec::Inbox => {
|
|
|
|
return Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("Cannot create INBOX")
|
2024-01-02 14:35:23 +00:00
|
|
|
.bad()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
MailboxCodec::Other(aname) => std::str::from_utf8(aname.as_ref())?,
|
|
|
|
};
|
2022-07-12 11:19:27 +00:00
|
|
|
|
|
|
|
match self.user.create_mailbox(&name).await {
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok(()) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("CREATE complete")
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
|
|
|
Err(e) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(&e.to_string())
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
2022-06-30 09:28:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 19:23:33 +00:00
|
|
|
async fn delete(
|
|
|
|
self,
|
|
|
|
mailbox: &MailboxCodec<'a>,
|
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2022-07-12 11:19:27 +00:00
|
|
|
|
|
|
|
match self.user.delete_mailbox(&name).await {
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok(()) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("DELETE complete")
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
|
|
|
Err(e) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(e.to_string())
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
2022-06-30 09:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 11:36:21 +00:00
|
|
|
async fn rename(
|
|
|
|
self,
|
2024-01-01 16:54:48 +00:00
|
|
|
from: &MailboxCodec<'a>,
|
|
|
|
to: &MailboxCodec<'a>,
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(from).try_into()?;
|
|
|
|
let new_name: &str = MailboxName(to).try_into()?;
|
2022-06-30 09:28:03 +00:00
|
|
|
|
2022-07-12 11:19:27 +00:00
|
|
|
match self.user.rename_mailbox(&name, &new_name).await {
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok(()) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("RENAME complete")
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
|
|
|
Err(e) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(e.to_string())
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 16:09:20 +00:00
|
|
|
async fn list(
|
2024-01-20 17:34:37 +00:00
|
|
|
&mut self,
|
2024-01-01 16:54:48 +00:00
|
|
|
reference: &MailboxCodec<'a>,
|
|
|
|
mailbox_wildcard: &ListMailbox<'a>,
|
2024-01-20 17:34:37 +00:00
|
|
|
must_return: &[ListReturnItem],
|
2022-07-12 11:19:27 +00:00
|
|
|
is_lsub: bool,
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-02 14:35:23 +00:00
|
|
|
let mbx_hier_delim: QuotedChar = QuotedChar::unvalidated(MBX_HIER_DELIM_RAW);
|
|
|
|
|
2024-01-01 16:54:48 +00:00
|
|
|
let reference: &str = MailboxName(reference).try_into()?;
|
2022-07-12 11:19:27 +00:00
|
|
|
if !reference.is_empty() {
|
|
|
|
return Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("References not supported")
|
2024-01-02 14:35:23 +00:00
|
|
|
.bad()?,
|
2022-07-12 11:19:27 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-01-20 17:34:37 +00:00
|
|
|
let status_item_names = must_return.iter().find_map(|m| match m {
|
|
|
|
ListReturnItem::Status(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
2024-01-01 16:54:48 +00:00
|
|
|
// @FIXME would probably need a rewrite to better use the imap_codec library
|
|
|
|
let wildcard = match mailbox_wildcard {
|
|
|
|
ListMailbox::Token(v) => std::str::from_utf8(v.as_ref())?,
|
|
|
|
ListMailbox::String(v) => std::str::from_utf8(v.as_ref())?,
|
|
|
|
};
|
2022-07-13 13:26:00 +00:00
|
|
|
if wildcard.is_empty() {
|
|
|
|
if is_lsub {
|
|
|
|
return Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("LSUB complete")
|
|
|
|
.data(Data::Lsub {
|
|
|
|
items: vec![],
|
2024-01-02 14:35:23 +00:00
|
|
|
delimiter: Some(mbx_hier_delim),
|
2024-01-01 16:54:48 +00:00
|
|
|
mailbox: "".try_into().unwrap(),
|
|
|
|
})
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2022-07-13 13:26:00 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("LIST complete")
|
|
|
|
.data(Data::List {
|
|
|
|
items: vec![],
|
2024-01-02 14:35:23 +00:00
|
|
|
delimiter: Some(mbx_hier_delim),
|
2024-01-01 16:54:48 +00:00
|
|
|
mailbox: "".try_into().unwrap(),
|
|
|
|
})
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2022-07-13 13:26:00 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 11:19:27 +00:00
|
|
|
let mailboxes = self.user.list_mailboxes().await?;
|
|
|
|
let mut vmailboxes = BTreeMap::new();
|
|
|
|
for mb in mailboxes.iter() {
|
2024-01-01 16:54:48 +00:00
|
|
|
for (i, _) in mb.match_indices(MBX_HIER_DELIM_RAW) {
|
2022-07-12 11:19:27 +00:00
|
|
|
if i > 0 {
|
|
|
|
let smb = &mb[..i];
|
2023-05-15 16:23:23 +00:00
|
|
|
vmailboxes.entry(smb).or_insert(false);
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vmailboxes.insert(mb, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ret = vec![];
|
|
|
|
for (mb, is_real) in vmailboxes.iter() {
|
2023-05-15 16:23:23 +00:00
|
|
|
if matches_wildcard(&wildcard, mb) {
|
2024-01-20 17:34:37 +00:00
|
|
|
let mailbox: MailboxCodec = mb
|
2022-07-12 11:19:27 +00:00
|
|
|
.to_string()
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_| anyhow!("invalid mailbox name"))?;
|
2024-01-19 16:40:08 +00:00
|
|
|
let mut items = vec![FlagNameAttribute::from(Atom::unvalidated("Subscribed"))];
|
2024-01-20 17:34:37 +00:00
|
|
|
|
|
|
|
// Decoration
|
2022-07-12 11:19:27 +00:00
|
|
|
if !*is_real {
|
|
|
|
items.push(FlagNameAttribute::Noselect);
|
2024-01-19 15:47:20 +00:00
|
|
|
} else {
|
|
|
|
match *mb {
|
|
|
|
"Drafts" => items.push(Atom::unvalidated("Drafts").into()),
|
|
|
|
"Archive" => items.push(Atom::unvalidated("Archive").into()),
|
|
|
|
"Sent" => items.push(Atom::unvalidated("Sent").into()),
|
|
|
|
"Trash" => items.push(Atom::unvalidated("Trash").into()),
|
|
|
|
_ => (),
|
|
|
|
};
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
2024-01-20 17:34:37 +00:00
|
|
|
|
|
|
|
// Result type
|
2022-07-12 11:19:27 +00:00
|
|
|
if is_lsub {
|
|
|
|
ret.push(Data::Lsub {
|
|
|
|
items,
|
2024-01-02 14:35:23 +00:00
|
|
|
delimiter: Some(mbx_hier_delim),
|
2024-01-20 17:34:37 +00:00
|
|
|
mailbox: mailbox.clone(),
|
2022-07-12 11:19:27 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ret.push(Data::List {
|
|
|
|
items,
|
2024-01-02 14:35:23 +00:00
|
|
|
delimiter: Some(mbx_hier_delim),
|
2024-01-20 17:34:37 +00:00
|
|
|
mailbox: mailbox.clone(),
|
2022-07-12 11:19:27 +00:00
|
|
|
});
|
|
|
|
}
|
2024-01-20 17:34:37 +00:00
|
|
|
|
|
|
|
// Also collect status
|
|
|
|
if let Some(sin) = status_item_names {
|
|
|
|
let ret_attrs = match self.status_items(mb, sin).await {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(err=?e, mailbox=%mb, "Unable to fetch status for mailbox");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = Data::Status {
|
|
|
|
mailbox,
|
|
|
|
items: ret_attrs.into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.push(data);
|
|
|
|
}
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:32:47 +00:00
|
|
|
let msg = if is_lsub {
|
|
|
|
"LSUB completed"
|
|
|
|
} else {
|
|
|
|
"LIST completed"
|
|
|
|
};
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(msg)
|
2024-01-02 14:35:23 +00:00
|
|
|
.many_data(ret)
|
|
|
|
.ok()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 09:28:03 +00:00
|
|
|
async fn status(
|
2024-01-20 17:34:37 +00:00
|
|
|
&mut self,
|
2024-01-02 19:23:33 +00:00
|
|
|
mailbox: &MailboxCodec<'static>,
|
2024-01-01 16:54:48 +00:00
|
|
|
attributes: &[StatusDataItemName],
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2024-01-20 17:34:37 +00:00
|
|
|
|
|
|
|
let ret_attrs = match self.status_items(name, attributes).await {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => match e.downcast_ref::<CommandError>() {
|
|
|
|
Some(CommandError::MailboxNotFound) => {
|
|
|
|
return Ok((
|
|
|
|
Response::build()
|
|
|
|
.to_req(self.req)
|
|
|
|
.message("Mailbox does not exist")
|
|
|
|
.no()?,
|
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => return Err(e.into()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = Data::Status {
|
|
|
|
mailbox: mailbox.clone(),
|
|
|
|
items: ret_attrs.into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Response::build()
|
|
|
|
.to_req(self.req)
|
|
|
|
.message("STATUS completed")
|
|
|
|
.data(data)
|
|
|
|
.ok()?,
|
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn status_items(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
attributes: &[StatusDataItemName],
|
|
|
|
) -> Result<Vec<StatusDataItem>> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let mb_opt = self.user.open_mailbox(name).await?;
|
2022-07-12 13:59:13 +00:00
|
|
|
let mb = match mb_opt {
|
|
|
|
Some(mb) => mb,
|
2024-01-20 17:34:37 +00:00
|
|
|
None => return Err(CommandError::MailboxNotFound.into()),
|
2022-07-12 13:59:13 +00:00
|
|
|
};
|
|
|
|
|
2024-01-09 16:40:23 +00:00
|
|
|
let view = MailboxView::new(mb, self.client_capabilities.condstore.is_enabled()).await;
|
2022-07-12 11:19:27 +00:00
|
|
|
|
2022-07-12 13:59:13 +00:00
|
|
|
let mut ret_attrs = vec![];
|
|
|
|
for attr in attributes.iter() {
|
|
|
|
ret_attrs.push(match attr {
|
2024-01-01 16:54:48 +00:00
|
|
|
StatusDataItemName::Messages => StatusDataItem::Messages(view.exists()?),
|
|
|
|
StatusDataItemName::Unseen => StatusDataItem::Unseen(view.unseen_count() as u32),
|
|
|
|
StatusDataItemName::Recent => StatusDataItem::Recent(view.recent()?),
|
|
|
|
StatusDataItemName::UidNext => StatusDataItem::UidNext(view.uidnext()),
|
|
|
|
StatusDataItemName::UidValidity => {
|
|
|
|
StatusDataItem::UidValidity(view.uidvalidity())
|
2022-07-12 13:59:13 +00:00
|
|
|
}
|
2024-01-01 16:54:48 +00:00
|
|
|
StatusDataItemName::Deleted => {
|
|
|
|
bail!("quota not implemented, can't return deleted elements waiting for EXPUNGE");
|
|
|
|
},
|
|
|
|
StatusDataItemName::DeletedStorage => {
|
|
|
|
bail!("quota not implemented, can't return freed storage after EXPUNGE will be run");
|
|
|
|
},
|
2024-01-03 15:52:31 +00:00
|
|
|
StatusDataItemName::HighestModSeq => {
|
2024-01-10 16:07:07 +00:00
|
|
|
self.client_capabilities.enable_condstore();
|
|
|
|
StatusDataItem::HighestModSeq(view.highestmodseq().get())
|
|
|
|
},
|
2022-07-12 13:59:13 +00:00
|
|
|
});
|
|
|
|
}
|
2024-01-20 17:34:37 +00:00
|
|
|
Ok(ret_attrs)
|
2022-06-30 09:28:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
async fn subscribe(
|
|
|
|
self,
|
|
|
|
mailbox: &MailboxCodec<'a>,
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2022-07-12 11:19:27 +00:00
|
|
|
|
2022-07-12 13:59:13 +00:00
|
|
|
if self.user.has_mailbox(&name).await? {
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("SUBSCRIBE complete")
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
2022-07-12 13:59:13 +00:00
|
|
|
} else {
|
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(format!("Mailbox {} does not exist", name))
|
2024-01-02 14:35:23 +00:00
|
|
|
.bad()?,
|
2022-07-12 13:59:13 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
async fn unsubscribe(
|
|
|
|
self,
|
|
|
|
mailbox: &MailboxCodec<'a>,
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2022-07-12 11:19:27 +00:00
|
|
|
|
2022-07-12 13:59:13 +00:00
|
|
|
if self.user.has_mailbox(&name).await? {
|
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(format!(
|
|
|
|
"Cannot unsubscribe from mailbox {}: not supported by Aerogramme",
|
|
|
|
name
|
|
|
|
))
|
2024-01-02 14:35:23 +00:00
|
|
|
.bad()?,
|
2022-07-12 13:59:13 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message(format!("Mailbox {} does not exist", name))
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2022-07-12 13:59:13 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 16:39:36 +00:00
|
|
|
/*
|
2022-06-22 15:26:52 +00:00
|
|
|
* TRACE BEGIN ---
|
2022-06-20 16:09:20 +00:00
|
|
|
|
|
|
|
|
2022-06-22 15:26:52 +00:00
|
|
|
Example: C: A142 SELECT INBOX
|
|
|
|
S: * 172 EXISTS
|
|
|
|
S: * 1 RECENT
|
|
|
|
S: * OK [UNSEEN 12] Message 12 is first unseen
|
|
|
|
S: * OK [UIDVALIDITY 3857529045] UIDs valid
|
|
|
|
S: * OK [UIDNEXT 4392] Predicted next UID
|
|
|
|
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
|
|
|
|
S: * OK [PERMANENTFLAGS (\Deleted \Seen \*)] Limited
|
|
|
|
S: A142 OK [READ-WRITE] SELECT completed
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2022-06-23 12:41:10 +00:00
|
|
|
--- a mailbox with no unseen message -> no unseen entry
|
2022-07-13 13:39:52 +00:00
|
|
|
NOTES:
|
|
|
|
RFC3501 (imap4rev1) says if there is no OK [UNSEEN] response, client must make no assumption,
|
|
|
|
it is therefore correct to not return it even if there are unseen messages
|
|
|
|
RFC9051 (imap4rev2) says that OK [UNSEEN] responses are deprecated after SELECT and EXAMINE
|
|
|
|
For Aerogramme, we just don't send the OK [UNSEEN], it's correct to do in both specifications.
|
2024-01-18 17:03:21 +00:00
|
|
|
|
2022-06-23 12:41:10 +00:00
|
|
|
|
|
|
|
20 select "INBOX.achats"
|
|
|
|
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded JUNK $label1)
|
|
|
|
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded JUNK $label1 \*)] Flags permitted.
|
|
|
|
* 88 EXISTS
|
|
|
|
* 0 RECENT
|
|
|
|
* OK [UIDVALIDITY 1347986788] UIDs valid
|
|
|
|
* OK [UIDNEXT 91] Predicted next UID
|
|
|
|
* OK [HIGHESTMODSEQ 72] Highest
|
|
|
|
20 OK [READ-WRITE] Select completed (0.001 + 0.000 secs).
|
|
|
|
|
2022-06-22 15:26:52 +00:00
|
|
|
* TRACE END ---
|
|
|
|
*/
|
2024-01-02 19:23:33 +00:00
|
|
|
async fn select(
|
|
|
|
self,
|
|
|
|
mailbox: &MailboxCodec<'a>,
|
2024-01-11 15:55:37 +00:00
|
|
|
modifiers: &[SelectExamineModifier],
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-11 15:55:37 +00:00
|
|
|
self.client_capabilities.select_enable(modifiers);
|
2024-01-09 18:16:55 +00:00
|
|
|
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2022-06-17 16:39:36 +00:00
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
|
|
|
let mb = match mb_opt {
|
|
|
|
Some(mb) => mb,
|
|
|
|
None => {
|
|
|
|
return Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("Mailbox does not exist")
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2022-06-29 13:39:54 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-06-29 11:16:58 +00:00
|
|
|
tracing::info!(username=%self.user.username, mailbox=%name, "mailbox.selected");
|
2022-06-17 16:39:36 +00:00
|
|
|
|
2024-01-09 16:40:23 +00:00
|
|
|
let mb = MailboxView::new(mb, self.client_capabilities.condstore.is_enabled()).await;
|
2024-01-02 14:35:23 +00:00
|
|
|
let data = mb.summary()?;
|
2022-06-24 08:27:26 +00:00
|
|
|
|
2022-06-28 07:34:24 +00:00
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.message("Select completed")
|
2024-01-02 21:32:02 +00:00
|
|
|
.to_req(self.req)
|
2024-01-01 16:54:48 +00:00
|
|
|
.code(Code::ReadWrite)
|
2024-01-02 14:35:23 +00:00
|
|
|
.set_body(data)
|
|
|
|
.ok()?,
|
2024-01-17 07:22:15 +00:00
|
|
|
flow::Transition::Select(mb, flow::MailboxPerm::ReadWrite),
|
2022-06-28 07:34:24 +00:00
|
|
|
))
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
2022-06-30 09:28:03 +00:00
|
|
|
|
2024-01-02 19:23:33 +00:00
|
|
|
async fn examine(
|
|
|
|
self,
|
|
|
|
mailbox: &MailboxCodec<'a>,
|
2024-01-11 15:55:37 +00:00
|
|
|
modifiers: &[SelectExamineModifier],
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-11 15:55:37 +00:00
|
|
|
self.client_capabilities.select_enable(modifiers);
|
2024-01-09 18:16:55 +00:00
|
|
|
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2022-06-30 09:28:03 +00:00
|
|
|
|
|
|
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
|
|
|
let mb = match mb_opt {
|
|
|
|
Some(mb) => mb,
|
|
|
|
None => {
|
|
|
|
return Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("Mailbox does not exist")
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2022-06-30 09:28:03 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
tracing::info!(username=%self.user.username, mailbox=%name, "mailbox.examined");
|
|
|
|
|
2024-01-09 16:40:23 +00:00
|
|
|
let mb = MailboxView::new(mb, self.client_capabilities.condstore.is_enabled()).await;
|
2024-01-02 14:35:23 +00:00
|
|
|
let data = mb.summary()?;
|
2022-06-30 09:28:03 +00:00
|
|
|
|
|
|
|
Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.to_req(self.req)
|
|
|
|
.message("Examine completed")
|
|
|
|
.code(Code::ReadOnly)
|
2024-01-02 14:35:23 +00:00
|
|
|
.set_body(data)
|
|
|
|
.ok()?,
|
2024-01-17 07:22:15 +00:00
|
|
|
flow::Transition::Select(mb, flow::MailboxPerm::ReadOnly),
|
2022-06-30 09:28:03 +00:00
|
|
|
))
|
|
|
|
}
|
2022-07-12 14:35:11 +00:00
|
|
|
|
2024-02-20 12:24:42 +00:00
|
|
|
//@FIXME we should write a specific version for the "selected" state
|
|
|
|
//that returns some unsollicited responses
|
2022-07-12 14:35:11 +00:00
|
|
|
async fn append(
|
|
|
|
self,
|
2024-01-01 16:54:48 +00:00
|
|
|
mailbox: &MailboxCodec<'a>,
|
|
|
|
flags: &[Flag<'a>],
|
|
|
|
date: &Option<DateTime>,
|
|
|
|
message: &Literal<'a>,
|
2024-01-02 19:23:33 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let append_tag = self.req.tag.clone();
|
2022-07-12 14:35:11 +00:00
|
|
|
match self.append_internal(mailbox, flags, date, message).await {
|
2024-02-20 12:24:42 +00:00
|
|
|
Ok((_mb_view, uidvalidity, uid, _modseq)) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.tag(append_tag)
|
|
|
|
.message("APPEND completed")
|
|
|
|
.code(Code::Other(CodeOther::unvalidated(
|
|
|
|
format!("APPENDUID {} {}", uidvalidity, uid).into_bytes(),
|
|
|
|
)))
|
2024-01-02 14:35:23 +00:00
|
|
|
.ok()?,
|
2024-01-01 16:54:48 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
|
|
|
Err(e) => Ok((
|
2024-01-02 14:35:23 +00:00
|
|
|
Response::build()
|
2024-01-01 16:54:48 +00:00
|
|
|
.tag(append_tag)
|
|
|
|
.message(e.to_string())
|
2024-01-02 14:35:23 +00:00
|
|
|
.no()?,
|
2022-07-12 14:35:11 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 19:53:25 +00:00
|
|
|
fn enable(
|
|
|
|
self,
|
2024-02-22 16:30:40 +00:00
|
|
|
cap_enable: &Vec1<CapabilityEnable<'static>>,
|
2024-01-03 19:53:25 +00:00
|
|
|
) -> Result<(Response<'static>, flow::Transition)> {
|
2024-01-03 19:53:07 +00:00
|
|
|
let mut response_builder = Response::build().to_req(self.req);
|
|
|
|
let capabilities = self.client_capabilities.try_enable(cap_enable.as_ref());
|
|
|
|
if capabilities.len() > 0 {
|
|
|
|
response_builder = response_builder.data(Data::Enabled { capabilities });
|
|
|
|
}
|
|
|
|
Ok((
|
2024-01-03 19:53:25 +00:00
|
|
|
response_builder.message("ENABLE completed").ok()?,
|
2024-01-03 19:53:07 +00:00
|
|
|
flow::Transition::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:42:51 +00:00
|
|
|
//@FIXME should be refactored and integrated to the mailbox view
|
2022-07-12 14:35:11 +00:00
|
|
|
pub(crate) async fn append_internal(
|
|
|
|
self,
|
2024-01-01 16:54:48 +00:00
|
|
|
mailbox: &MailboxCodec<'a>,
|
|
|
|
flags: &[Flag<'a>],
|
|
|
|
date: &Option<DateTime>,
|
|
|
|
message: &Literal<'a>,
|
2024-02-20 12:24:42 +00:00
|
|
|
) -> Result<(MailboxView, ImapUidvalidity, ImapUid, ModSeq)> {
|
2024-01-01 16:54:48 +00:00
|
|
|
let name: &str = MailboxName(mailbox).try_into()?;
|
2022-07-12 14:35:11 +00:00
|
|
|
|
|
|
|
let mb_opt = self.user.open_mailbox(&name).await?;
|
|
|
|
let mb = match mb_opt {
|
|
|
|
Some(mb) => mb,
|
|
|
|
None => bail!("Mailbox does not exist"),
|
|
|
|
};
|
2024-02-20 10:42:51 +00:00
|
|
|
let mut view = MailboxView::new(mb, self.client_capabilities.condstore.is_enabled()).await;
|
2022-07-12 14:35:11 +00:00
|
|
|
|
|
|
|
if date.is_some() {
|
2024-01-03 15:52:31 +00:00
|
|
|
tracing::warn!("Cannot set date when appending message");
|
2022-07-12 14:35:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:54:48 +00:00
|
|
|
let msg =
|
|
|
|
IMF::try_from(message.data()).map_err(|_| anyhow!("Could not parse e-mail message"))?;
|
2022-07-12 14:35:11 +00:00
|
|
|
let flags = flags.iter().map(|x| x.to_string()).collect::<Vec<_>>();
|
|
|
|
// TODO: filter allowed flags? ping @Quentin
|
|
|
|
|
2024-02-22 16:31:03 +00:00
|
|
|
let (uidvalidity, uid, modseq) =
|
|
|
|
view.internal.mailbox.append(msg, None, &flags[..]).await?;
|
|
|
|
//let unsollicited = view.update(UpdateParameters::default()).await?;
|
2022-07-12 14:35:11 +00:00
|
|
|
|
2024-02-20 12:24:42 +00:00
|
|
|
Ok((view, uidvalidity, uid, modseq))
|
2022-07-12 14:35:11 +00:00
|
|
|
}
|
2022-06-20 16:09:20 +00:00
|
|
|
}
|
2022-07-13 13:26:00 +00:00
|
|
|
|
|
|
|
fn matches_wildcard(wildcard: &str, name: &str) -> bool {
|
|
|
|
let wildcard = wildcard.chars().collect::<Vec<char>>();
|
|
|
|
let name = name.chars().collect::<Vec<char>>();
|
|
|
|
|
|
|
|
let mut matches = vec![vec![false; wildcard.len() + 1]; name.len() + 1];
|
|
|
|
|
|
|
|
for i in 0..=name.len() {
|
|
|
|
for j in 0..=wildcard.len() {
|
|
|
|
matches[i][j] = (i == 0 && j == 0)
|
|
|
|
|| (j > 0
|
|
|
|
&& matches[i][j - 1]
|
|
|
|
&& (wildcard[j - 1] == '%' || wildcard[j - 1] == '*'))
|
|
|
|
|| (i > 0
|
|
|
|
&& j > 0
|
|
|
|
&& matches[i - 1][j - 1]
|
|
|
|
&& wildcard[j - 1] == name[i - 1]
|
|
|
|
&& wildcard[j - 1] != '%'
|
|
|
|
&& wildcard[j - 1] != '*')
|
|
|
|
|| (i > 0
|
|
|
|
&& j > 0
|
|
|
|
&& matches[i - 1][j]
|
|
|
|
&& (wildcard[j - 1] == '*'
|
2024-01-01 16:54:48 +00:00
|
|
|
|| (wildcard[j - 1] == '%' && name[i - 1] != MBX_HIER_DELIM_RAW)));
|
2022-07-13 13:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matches[name.len()][wildcard.len()]
|
|
|
|
}
|
|
|
|
|
2024-01-20 17:34:37 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum CommandError {
|
|
|
|
#[error("Mailbox not found")]
|
|
|
|
MailboxNotFound,
|
|
|
|
}
|
|
|
|
|
2022-07-13 13:26:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wildcard_matches() {
|
|
|
|
assert!(matches_wildcard("INBOX", "INBOX"));
|
|
|
|
assert!(matches_wildcard("*", "INBOX"));
|
|
|
|
assert!(matches_wildcard("%", "INBOX"));
|
|
|
|
assert!(!matches_wildcard("%", "Test.Azerty"));
|
|
|
|
assert!(!matches_wildcard("INBOX.*", "INBOX"));
|
|
|
|
assert!(matches_wildcard("Sent.*", "Sent.A"));
|
|
|
|
assert!(matches_wildcard("Sent.*", "Sent.A.B"));
|
|
|
|
assert!(!matches_wildcard("Sent.%", "Sent.A.B"));
|
|
|
|
}
|
|
|
|
}
|