C12.6: IMAP sync
This commit is contained in:
32 files changed
+2343
-276
No files matched your search
@@ -12,6 +12,7 @@ form_urlencoded = "1"
|
||||
getrandom.workspace = true
|
||||
html5ever.workspace = true
|
||||
imap-proto.workspace = true
|
||||
jiff.workspace = true
|
||||
log.workspace = true
|
||||
mail-builder.workspace = true
|
||||
mail-parser.workspace = true
|
||||
|
||||
+363
-230
@@ -1,29 +1,90 @@
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use imap_proto::{AttributeValue, MailboxDatum, Response, Status};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use imap_proto::{AttributeValue, Capability, MailboxDatum, NameAttribute, Response, ResponseCode};
|
||||
use tokio_rustls::TlsConnector;
|
||||
|
||||
use crate::{
|
||||
Error, Result,
|
||||
net::{self, Login, Secret, Security, Server, Stream, within},
|
||||
wire::{Wire, words},
|
||||
};
|
||||
|
||||
mod conn;
|
||||
mod parts;
|
||||
|
||||
use conn::{Arg, Conn};
|
||||
pub use parts::Node;
|
||||
|
||||
/// A logged-in IMAP session over TLS.
|
||||
pub struct Imap {
|
||||
conn: Conn<Stream>,
|
||||
}
|
||||
|
||||
/// One command argument.
|
||||
enum Arg<'a> {
|
||||
/// Written as is.
|
||||
Atom(&'a str),
|
||||
/// Quoted, or a literal when quotes cannot carry it.
|
||||
Text(&'a str),
|
||||
/// What a folder is for (RFC 6154; INBOX by name).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Role {
|
||||
Inbox,
|
||||
Sent,
|
||||
Drafts,
|
||||
Trash,
|
||||
Junk,
|
||||
Archive,
|
||||
All,
|
||||
Flagged,
|
||||
}
|
||||
|
||||
/// OK, or the words of NO or BAD.
|
||||
type Outcome = std::result::Result<(), String>;
|
||||
impl Role {
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Role::Inbox => "inbox",
|
||||
Role::Sent => "sent",
|
||||
Role::Drafts => "drafts",
|
||||
Role::Trash => "trash",
|
||||
Role::Junk => "junk",
|
||||
Role::Archive => "archive",
|
||||
Role::All => "all",
|
||||
Role::Flagged => "flagged",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_name(name: &str) -> Option<Self> {
|
||||
[
|
||||
Role::Inbox,
|
||||
Role::Sent,
|
||||
Role::Drafts,
|
||||
Role::Trash,
|
||||
Role::Junk,
|
||||
Role::Archive,
|
||||
Role::All,
|
||||
Role::Flagged,
|
||||
]
|
||||
.into_iter()
|
||||
.find(|role| role.name() == name)
|
||||
}
|
||||
}
|
||||
|
||||
/// One listed folder.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Folder {
|
||||
pub name: String,
|
||||
pub role: Option<Role>,
|
||||
/// False for `\Noselect` and `\NonExistent`.
|
||||
pub selectable: bool,
|
||||
}
|
||||
|
||||
/// A folder opened read-only.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Opened {
|
||||
pub uidvalidity: u32,
|
||||
pub exists: u32,
|
||||
}
|
||||
|
||||
/// One fetched message: header, text or whole.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Got {
|
||||
pub uid: u32,
|
||||
pub flags: Vec<String>,
|
||||
pub bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Imap {
|
||||
pub async fn connect(server: &Server, login: &Login, tls: &TlsConnector) -> Result<Self> {
|
||||
@@ -57,52 +118,168 @@ impl Imap {
|
||||
Ok(Self { conn })
|
||||
}
|
||||
|
||||
/// Every mailbox name the server lists.
|
||||
pub async fn mailboxes(&mut self) -> Result<Vec<String>> {
|
||||
let mut names = Vec::new();
|
||||
let args = [Arg::Text(""), Arg::Atom("*")];
|
||||
/// Every folder the server lists, with its role.
|
||||
pub async fn folders(&mut self) -> Result<Vec<Folder>> {
|
||||
let can = self.capabilities().await?;
|
||||
let has = |name: &str| can.iter().any(|it| it.eq_ignore_ascii_case(name));
|
||||
// RFC 6154 §2: roles may come only on request.
|
||||
let extended = has("LIST-EXTENDED") && has("SPECIAL-USE");
|
||||
let mut folders = Vec::new();
|
||||
let all = [
|
||||
Arg::Text(""),
|
||||
Arg::Atom("*"),
|
||||
Arg::Atom("RETURN (SPECIAL-USE)"),
|
||||
];
|
||||
let args = if extended { &all[..] } else { &all[..2] };
|
||||
within(
|
||||
"listing mailboxes",
|
||||
self.conn.run("LIST", &args, |response, raw| {
|
||||
self.conn.run("LIST", args, |response, raw| {
|
||||
if let Response::MailboxData(MailboxDatum::List(list)) = response {
|
||||
names.push(mailbox_name(&list.name, raw));
|
||||
let name = mailbox_name(&list.name, raw);
|
||||
folders.push(folder(name, &list.name_attributes));
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
Ok(names)
|
||||
Ok(folders)
|
||||
}
|
||||
|
||||
async fn capabilities(&mut self) -> Result<Vec<String>> {
|
||||
let mut can = Vec::new();
|
||||
within(
|
||||
"reading capabilities",
|
||||
self.conn.run("CAPABILITY", &[], |response, _| {
|
||||
if let Response::Capabilities(list) = response {
|
||||
can.extend(list.iter().filter_map(|it| match it {
|
||||
Capability::Atom(atom) => Some(atom.to_string()),
|
||||
_ => None,
|
||||
}));
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
Ok(can)
|
||||
}
|
||||
|
||||
pub async fn examine(&mut self, mailbox: &str) -> Result<Opened> {
|
||||
let (mut uidvalidity, mut exists) = (None, None);
|
||||
within(
|
||||
"opening the mailbox",
|
||||
self.conn.run(
|
||||
"EXAMINE",
|
||||
&[Arg::Text(mailbox)],
|
||||
|response, _| match response {
|
||||
Response::Data { outcome, .. } => {
|
||||
if let Some(ResponseCode::UidValidity(value)) = &outcome.code {
|
||||
uidvalidity = Some(*value);
|
||||
}
|
||||
}
|
||||
Response::MailboxData(MailboxDatum::Exists(count)) => exists = Some(*count),
|
||||
_ => {}
|
||||
},
|
||||
),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
let uidvalidity = uidvalidity
|
||||
.ok_or_else(|| Error::Protocol("the mailbox opened without a UIDVALIDITY".into()))?;
|
||||
// RFC 3501 §6.3.1: EXISTS is required.
|
||||
let exists =
|
||||
exists.ok_or_else(|| Error::Protocol("the mailbox opened without EXISTS".into()))?;
|
||||
Ok(Opened {
|
||||
uidvalidity,
|
||||
exists,
|
||||
})
|
||||
}
|
||||
|
||||
/// Every UID in the open mailbox with its flags.
|
||||
pub async fn flags(&mut self) -> Result<Vec<(u32, Vec<String>)>> {
|
||||
let got = self
|
||||
.uid_fetch("1:*", "(UID FLAGS)", "reading flags")
|
||||
.await?;
|
||||
Ok(got.into_iter().map(|(uid, it)| (uid, it.flags)).collect())
|
||||
}
|
||||
|
||||
/// Headers of `uids`; a UID gone meanwhile is absent.
|
||||
pub async fn headers(&mut self, uids: &[u32]) -> Result<Vec<Got>> {
|
||||
let what = "(UID FLAGS BODY.PEEK[HEADER])";
|
||||
let got = self.uid_fetch(&set(uids), what, "fetching headers").await?;
|
||||
asked(got, uids, "HEADER")
|
||||
}
|
||||
|
||||
/// Whole messages of `uids`; a UID gone meanwhile is absent.
|
||||
pub async fn bodies(&mut self, uids: &[u32]) -> Result<Vec<Got>> {
|
||||
let what = "(UID FLAGS BODY.PEEK[])";
|
||||
let got = self
|
||||
.uid_fetch(&set(uids), what, "fetching messages")
|
||||
.await?;
|
||||
asked(got, uids, "")
|
||||
}
|
||||
|
||||
/// Each UID's MIME tree; a UID gone meanwhile is absent.
|
||||
pub async fn structures(&mut self, uids: &[u32]) -> Result<Vec<(u32, Node)>> {
|
||||
let what = "(UID BODYSTRUCTURE)";
|
||||
let mut got = self
|
||||
.uid_fetch(&set(uids), what, "reading structures")
|
||||
.await?;
|
||||
let mut out = Vec::new();
|
||||
for &uid in uids {
|
||||
let Some(it) = got.remove(&uid) else {
|
||||
continue;
|
||||
};
|
||||
let node = it
|
||||
.structure
|
||||
.ok_or_else(|| Error::Protocol(format!("UID {uid} came without a structure")))?;
|
||||
out.push((uid, node?));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The message without its left-out parts; `None` once gone.
|
||||
pub async fn text(&mut self, uid: u32, node: &Node) -> Result<Option<Got>> {
|
||||
let peeks: Vec<String> = node
|
||||
.sections()
|
||||
.iter()
|
||||
.map(|section| format!("BODY.PEEK[{section}]"))
|
||||
.collect();
|
||||
let what = format!("(UID FLAGS {})", peeks.join(" "));
|
||||
let mut got = self
|
||||
.uid_fetch(&uid.to_string(), &what, "fetching message text")
|
||||
.await?;
|
||||
let Some(it) = got.remove(&uid) else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(Got {
|
||||
uid,
|
||||
bytes: node.build(&it.sections)?,
|
||||
flags: it.flags,
|
||||
}))
|
||||
}
|
||||
|
||||
/// UIDs of messages that arrived on or after `day` (`19-Jul-2026`).
|
||||
pub async fn since(&mut self, day: &str) -> Result<Vec<u32>> {
|
||||
let mut uids = Vec::new();
|
||||
let args = [Arg::Atom("SINCE"), Arg::Atom(day)];
|
||||
within(
|
||||
"searching by date",
|
||||
self.conn.run("UID SEARCH", &args, |response, _| {
|
||||
if let Response::MailboxData(MailboxDatum::Search(found)) = response {
|
||||
uids.extend_from_slice(found);
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
uids.sort_unstable();
|
||||
Ok(uids)
|
||||
}
|
||||
|
||||
/// The raw message; `None` once the UID is gone.
|
||||
pub async fn fetch(&mut self, mailbox: &str, uid: u32) -> Result<Option<Vec<u8>>> {
|
||||
let examine = [Arg::Text(mailbox)];
|
||||
within(
|
||||
"opening the mailbox",
|
||||
self.conn.run("EXAMINE", &examine, |_, _| {}),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
let number = uid.to_string();
|
||||
let args = [Arg::Atom(&number), Arg::Atom("BODY.PEEK[]")];
|
||||
let mut found = None;
|
||||
within(
|
||||
"fetching the message",
|
||||
self.conn.run("UID FETCH", &args, |response, _| {
|
||||
if let Response::Fetch(_, values) = response
|
||||
&& values.contains(&AttributeValue::Uid(uid))
|
||||
{
|
||||
found = Some(body(values));
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
match found {
|
||||
None => Ok(None),
|
||||
Some(None) => Err(Error::Protocol(format!("UID {uid} came without a body"))),
|
||||
Some(Some(body)) => Ok(Some(body)),
|
||||
}
|
||||
self.examine(mailbox).await?;
|
||||
Ok(self.bodies(&[uid]).await?.pop().map(|got| got.bytes))
|
||||
}
|
||||
|
||||
pub async fn logout(mut self) -> Result<()> {
|
||||
@@ -110,6 +287,140 @@ impl Imap {
|
||||
.await??
|
||||
.map_err(Error::Server)
|
||||
}
|
||||
|
||||
/// Every FETCH for a UID, merged; flags may come apart.
|
||||
async fn uid_fetch(
|
||||
&mut self,
|
||||
uids: &str,
|
||||
what: &str,
|
||||
step: &'static str,
|
||||
) -> Result<HashMap<u32, Fetch>> {
|
||||
let mut got: HashMap<u32, Fetch> = HashMap::new();
|
||||
let args = [Arg::Atom(uids), Arg::Atom(what)];
|
||||
within(
|
||||
step,
|
||||
self.conn.run("UID FETCH", &args, |response, _| {
|
||||
if let Response::Fetch(_, values) = response {
|
||||
merge(&mut got, values);
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await??
|
||||
.map_err(Error::Server)?;
|
||||
Ok(got)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Fetch {
|
||||
flags: Vec<String>,
|
||||
sections: HashMap<String, Vec<u8>>,
|
||||
structure: Option<Result<Node>>,
|
||||
}
|
||||
|
||||
/// Unsolicited FETCH responses without a UID are skipped.
|
||||
fn merge(got: &mut HashMap<u32, Fetch>, values: &[AttributeValue]) {
|
||||
let Some(uid) = values.iter().find_map(|value| match value {
|
||||
AttributeValue::Uid(uid) => Some(*uid),
|
||||
_ => None,
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
let it = got.entry(uid).or_default();
|
||||
for value in values {
|
||||
match value {
|
||||
AttributeValue::Flags(flags) => {
|
||||
// \Recent belongs to one session.
|
||||
it.flags = flags
|
||||
.iter()
|
||||
.filter(|flag| !flag.eq_ignore_ascii_case(r"\Recent"))
|
||||
.map(|flag| flag.to_string())
|
||||
.collect();
|
||||
it.flags.sort();
|
||||
}
|
||||
AttributeValue::BodySection {
|
||||
section,
|
||||
data: Some(data),
|
||||
..
|
||||
} => {
|
||||
it.sections
|
||||
.insert(parts::section_key(section), data.to_vec());
|
||||
}
|
||||
AttributeValue::BodyStructure(structure) => {
|
||||
it.structure = Some(Node::read(structure));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The asked UIDs in order; each needs `section`.
|
||||
fn asked(mut got: HashMap<u32, Fetch>, uids: &[u32], section: &str) -> Result<Vec<Got>> {
|
||||
let mut out = Vec::new();
|
||||
for &uid in uids {
|
||||
let Some(mut it) = got.remove(&uid) else {
|
||||
continue;
|
||||
};
|
||||
let bytes = it
|
||||
.sections
|
||||
.remove(section)
|
||||
.ok_or_else(|| Error::Protocol(format!("UID {uid} came without {section:?}")))?;
|
||||
out.push(Got {
|
||||
uid,
|
||||
flags: it.flags,
|
||||
bytes,
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `1:3,7` from sorted UIDs.
|
||||
fn set(uids: &[u32]) -> String {
|
||||
let mut out = String::new();
|
||||
let mut at = 0;
|
||||
while at < uids.len() {
|
||||
let start = uids[at];
|
||||
let mut end = start;
|
||||
while at + 1 < uids.len() && uids[at + 1] == end + 1 {
|
||||
at += 1;
|
||||
end = uids[at];
|
||||
}
|
||||
if !out.is_empty() {
|
||||
out.push(',');
|
||||
}
|
||||
match start == end {
|
||||
true => out.push_str(&start.to_string()),
|
||||
false => out.push_str(&format!("{start}:{end}")),
|
||||
}
|
||||
at += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn folder(name: String, attributes: &[NameAttribute]) -> Folder {
|
||||
let mut role = name.eq_ignore_ascii_case("INBOX").then_some(Role::Inbox);
|
||||
let mut selectable = true;
|
||||
for attribute in attributes {
|
||||
match attribute {
|
||||
NameAttribute::NoSelect => selectable = false,
|
||||
NameAttribute::Extension(other) if other.eq_ignore_ascii_case("\\NonExistent") => {
|
||||
selectable = false
|
||||
}
|
||||
NameAttribute::Sent => role = Some(Role::Sent),
|
||||
NameAttribute::Drafts => role = Some(Role::Drafts),
|
||||
NameAttribute::Trash => role = Some(Role::Trash),
|
||||
NameAttribute::Junk => role = Some(Role::Junk),
|
||||
NameAttribute::Archive => role = Some(Role::Archive),
|
||||
NameAttribute::All => role = Some(Role::All),
|
||||
NameAttribute::Flagged => role = Some(Role::Flagged),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Folder {
|
||||
name,
|
||||
role,
|
||||
selectable,
|
||||
}
|
||||
}
|
||||
|
||||
/// imap-proto keeps quoted escapes; a literal is as sent.
|
||||
@@ -137,189 +448,11 @@ fn mailbox_name(name: &str, raw: &[u8]) -> String {
|
||||
plain
|
||||
}
|
||||
|
||||
fn body(values: &[AttributeValue]) -> Option<Vec<u8>> {
|
||||
values.iter().find_map(|value| match value {
|
||||
AttributeValue::BodySection {
|
||||
section: None,
|
||||
data: Some(data),
|
||||
..
|
||||
} => Some(data.to_vec()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
struct Conn<S> {
|
||||
wire: Wire<S>,
|
||||
tags: u32,
|
||||
}
|
||||
|
||||
impl<S: AsyncRead + AsyncWrite + Unpin> Conn<S> {
|
||||
fn new(io: S) -> Self {
|
||||
Self {
|
||||
wire: Wire::new(io),
|
||||
tags: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// `* BYE` ends with the server's words.
|
||||
async fn greeting(&mut self) -> Result<()> {
|
||||
self.wire
|
||||
.next(|response, raw| match response {
|
||||
Response::Data {
|
||||
status: Status::Ok, ..
|
||||
} => Ok(()),
|
||||
Response::Data {
|
||||
status: Status::Bye,
|
||||
..
|
||||
} => Err(Error::Server(words(raw))),
|
||||
Response::Data { status, .. } => Err(Error::Protocol(format!(
|
||||
"the greeting was {status:?}, not OK"
|
||||
))),
|
||||
_ => Err(Error::Protocol("the first line was not a greeting".into())),
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
fn tag(&mut self) -> String {
|
||||
self.tags += 1;
|
||||
format!("A{}", self.tags)
|
||||
}
|
||||
|
||||
/// Sends a command; untagged responses go to `each`.
|
||||
async fn run(
|
||||
&mut self,
|
||||
verb: &str,
|
||||
args: &[Arg<'_>],
|
||||
mut each: impl FnMut(&Response, &[u8]),
|
||||
) -> Result<Outcome> {
|
||||
let tag = self.tag();
|
||||
log::debug!("mail: IMAP {tag} {verb}");
|
||||
let mut line = format!("{tag} {verb}").into_bytes();
|
||||
for arg in args {
|
||||
line.push(b' ');
|
||||
match arg {
|
||||
Arg::Atom(atom) => line.extend_from_slice(atom.as_bytes()),
|
||||
Arg::Text(text) => match quoted(text)? {
|
||||
Some(quoted) => line.extend_from_slice(quoted.as_bytes()),
|
||||
None => {
|
||||
line.extend_from_slice(format!("{{{}}}\r\n", text.len()).as_bytes());
|
||||
self.wire.write(&line).await?;
|
||||
line.clear();
|
||||
if let Err(words) = self.proceed(&tag, &mut each).await? {
|
||||
return Ok(Err(words));
|
||||
}
|
||||
line.extend_from_slice(text.as_bytes());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
line.extend_from_slice(b"\r\n");
|
||||
self.wire.write(&line).await?;
|
||||
loop {
|
||||
match self.step(&tag, &mut each).await? {
|
||||
Step::Done(outcome) => return Ok(outcome),
|
||||
Step::Untagged => {}
|
||||
Step::Continue => {
|
||||
return Err(Error::Protocol(
|
||||
"the server asked for more than was sent".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Waits for `+` before a literal.
|
||||
async fn proceed(
|
||||
&mut self,
|
||||
tag: &str,
|
||||
each: &mut impl FnMut(&Response, &[u8]),
|
||||
) -> Result<Outcome> {
|
||||
loop {
|
||||
match self.step(tag, each).await? {
|
||||
Step::Continue => return Ok(Ok(())),
|
||||
Step::Done(Ok(())) => {
|
||||
return Err(Error::Protocol(
|
||||
"the command ended before its literal".into(),
|
||||
));
|
||||
}
|
||||
Step::Done(refused) => return Ok(refused),
|
||||
Step::Untagged => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Answers the first challenge; empty after, so a refusal ends.
|
||||
async fn xoauth2(&mut self, initial: &str) -> Result<Outcome> {
|
||||
let tag = self.tag();
|
||||
self.wire
|
||||
.write(format!("{tag} AUTHENTICATE XOAUTH2\r\n").as_bytes())
|
||||
.await?;
|
||||
let mut answer = Some(STANDARD.encode(initial));
|
||||
loop {
|
||||
match self.step(&tag, &mut |_, _| {}).await? {
|
||||
Step::Continue => {
|
||||
let line = answer.take().unwrap_or_default() + "\r\n";
|
||||
self.wire.write(line.as_bytes()).await?;
|
||||
}
|
||||
Step::Done(outcome) => return Ok(outcome),
|
||||
Step::Untagged => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn step(&mut self, tag: &str, each: &mut impl FnMut(&Response, &[u8])) -> Result<Step> {
|
||||
self.wire
|
||||
.next(|response, raw| match response {
|
||||
Response::Continue(_) => Ok(Step::Continue),
|
||||
Response::Done {
|
||||
tag: done, status, ..
|
||||
} => {
|
||||
if done.0 != tag {
|
||||
return Err(Error::Protocol(format!(
|
||||
"a reply for {} came while waiting for {tag}",
|
||||
done.0
|
||||
)));
|
||||
}
|
||||
match status {
|
||||
Status::Ok => Ok(Step::Done(Ok(()))),
|
||||
Status::No | Status::Bad => Ok(Step::Done(Err(words(raw)))),
|
||||
other => Err(Error::Protocol(format!("{tag} ended with {other:?}"))),
|
||||
}
|
||||
}
|
||||
untagged => {
|
||||
each(untagged, raw);
|
||||
Ok(Step::Untagged)
|
||||
}
|
||||
})
|
||||
.await?
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn a_uid_set_joins_runs() {
|
||||
assert_eq!(super::set(&[1, 2, 3, 7, 9, 10]), "1:3,7,9:10");
|
||||
assert_eq!(super::set(&[5]), "5");
|
||||
}
|
||||
}
|
||||
|
||||
enum Step {
|
||||
Continue,
|
||||
Untagged,
|
||||
Done(Outcome),
|
||||
}
|
||||
|
||||
/// `None` when only a literal can carry the text.
|
||||
fn quoted(text: &str) -> Result<Option<String>> {
|
||||
if text.contains('\0') {
|
||||
return Err(Error::Invalid("IMAP cannot carry a NUL byte".into()));
|
||||
}
|
||||
if !text
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii() && byte != b'\r' && byte != b'\n')
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
let mut quoted = String::with_capacity(text.len() + 2);
|
||||
quoted.push('"');
|
||||
for char in text.chars() {
|
||||
if char == '"' || char == '\\' {
|
||||
quoted.push('\\');
|
||||
}
|
||||
quoted.push(char);
|
||||
}
|
||||
quoted.push('"');
|
||||
Ok(Some(quoted))
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
//! Tagged commands, literals and replies on one stream.
|
||||
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use imap_proto::{Response, Status};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use crate::{
|
||||
Error, Result,
|
||||
wire::{Wire, words},
|
||||
};
|
||||
|
||||
/// One command argument.
|
||||
pub(super) enum Arg<'a> {
|
||||
/// Written as is.
|
||||
Atom(&'a str),
|
||||
/// Quoted, or a literal when quotes cannot carry it.
|
||||
Text(&'a str),
|
||||
}
|
||||
|
||||
/// OK, or the words of NO or BAD.
|
||||
pub(super) type Outcome = std::result::Result<(), String>;
|
||||
|
||||
pub(super) struct Conn<S> {
|
||||
pub(super) wire: Wire<S>,
|
||||
tags: u32,
|
||||
}
|
||||
|
||||
impl<S: AsyncRead + AsyncWrite + Unpin> Conn<S> {
|
||||
pub(super) fn new(io: S) -> Self {
|
||||
Self {
|
||||
wire: Wire::new(io),
|
||||
tags: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// `* BYE` ends with the server's words.
|
||||
pub(super) async fn greeting(&mut self) -> Result<()> {
|
||||
self.wire
|
||||
.next(|response, raw| match response {
|
||||
Response::Data {
|
||||
status: Status::Ok, ..
|
||||
} => Ok(()),
|
||||
Response::Data {
|
||||
status: Status::Bye,
|
||||
..
|
||||
} => Err(Error::Server(words(raw))),
|
||||
Response::Data { status, .. } => Err(Error::Protocol(format!(
|
||||
"the greeting was {status:?}, not OK"
|
||||
))),
|
||||
_ => Err(Error::Protocol("the first line was not a greeting".into())),
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
fn tag(&mut self) -> String {
|
||||
self.tags += 1;
|
||||
format!("A{}", self.tags)
|
||||
}
|
||||
|
||||
/// Sends a command; untagged responses go to `each`.
|
||||
pub(super) async fn run(
|
||||
&mut self,
|
||||
verb: &str,
|
||||
args: &[Arg<'_>],
|
||||
mut each: impl FnMut(&Response, &[u8]),
|
||||
) -> Result<Outcome> {
|
||||
let tag = self.tag();
|
||||
log::debug!("mail: IMAP {tag} {verb}");
|
||||
let mut line = format!("{tag} {verb}").into_bytes();
|
||||
for arg in args {
|
||||
line.push(b' ');
|
||||
match arg {
|
||||
Arg::Atom(atom) => line.extend_from_slice(atom.as_bytes()),
|
||||
Arg::Text(text) => match quoted(text)? {
|
||||
Some(quoted) => line.extend_from_slice(quoted.as_bytes()),
|
||||
None => {
|
||||
line.extend_from_slice(format!("{{{}}}\r\n", text.len()).as_bytes());
|
||||
self.wire.write(&line).await?;
|
||||
line.clear();
|
||||
if let Err(words) = self.proceed(&tag, &mut each).await? {
|
||||
return Ok(Err(words));
|
||||
}
|
||||
line.extend_from_slice(text.as_bytes());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
line.extend_from_slice(b"\r\n");
|
||||
self.wire.write(&line).await?;
|
||||
loop {
|
||||
match self.step(&tag, &mut each).await? {
|
||||
Step::Done(outcome) => return Ok(outcome),
|
||||
Step::Untagged => {}
|
||||
Step::Continue => {
|
||||
return Err(Error::Protocol(
|
||||
"the server asked for more than was sent".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Waits for `+` before a literal.
|
||||
async fn proceed(
|
||||
&mut self,
|
||||
tag: &str,
|
||||
each: &mut impl FnMut(&Response, &[u8]),
|
||||
) -> Result<Outcome> {
|
||||
loop {
|
||||
match self.step(tag, each).await? {
|
||||
Step::Continue => return Ok(Ok(())),
|
||||
Step::Done(Ok(())) => {
|
||||
return Err(Error::Protocol(
|
||||
"the command ended before its literal".into(),
|
||||
));
|
||||
}
|
||||
Step::Done(refused) => return Ok(refused),
|
||||
Step::Untagged => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Answers the first challenge; empty after, so a refusal ends.
|
||||
pub(super) async fn xoauth2(&mut self, initial: &str) -> Result<Outcome> {
|
||||
let tag = self.tag();
|
||||
self.wire
|
||||
.write(format!("{tag} AUTHENTICATE XOAUTH2\r\n").as_bytes())
|
||||
.await?;
|
||||
let mut answer = Some(STANDARD.encode(initial));
|
||||
loop {
|
||||
match self.step(&tag, &mut |_, _| {}).await? {
|
||||
Step::Continue => {
|
||||
let line = answer.take().unwrap_or_default() + "\r\n";
|
||||
self.wire.write(line.as_bytes()).await?;
|
||||
}
|
||||
Step::Done(outcome) => return Ok(outcome),
|
||||
Step::Untagged => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn step(&mut self, tag: &str, each: &mut impl FnMut(&Response, &[u8])) -> Result<Step> {
|
||||
self.wire
|
||||
.next(|response, raw| match response {
|
||||
Response::Continue(_) => Ok(Step::Continue),
|
||||
Response::Done {
|
||||
tag: done, status, ..
|
||||
} => {
|
||||
if done.0 != tag {
|
||||
return Err(Error::Protocol(format!(
|
||||
"a reply for {} came while waiting for {tag}",
|
||||
done.0
|
||||
)));
|
||||
}
|
||||
match status {
|
||||
Status::Ok => Ok(Step::Done(Ok(()))),
|
||||
Status::No | Status::Bad => Ok(Step::Done(Err(words(raw)))),
|
||||
other => Err(Error::Protocol(format!("{tag} ended with {other:?}"))),
|
||||
}
|
||||
}
|
||||
untagged => {
|
||||
each(untagged, raw);
|
||||
Ok(Step::Untagged)
|
||||
}
|
||||
})
|
||||
.await?
|
||||
}
|
||||
}
|
||||
|
||||
enum Step {
|
||||
Continue,
|
||||
Untagged,
|
||||
Done(Outcome),
|
||||
}
|
||||
|
||||
/// `None` when only a literal can carry the text.
|
||||
fn quoted(text: &str) -> Result<Option<String>> {
|
||||
if text.contains('\0') {
|
||||
return Err(Error::Invalid("IMAP cannot carry a NUL byte".into()));
|
||||
}
|
||||
if !text
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii() && byte != b'\r' && byte != b'\n')
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
let mut quoted = String::with_capacity(text.len() + 2);
|
||||
quoted.push('"');
|
||||
for char in text.chars() {
|
||||
if char == '"' || char == '\\' {
|
||||
quoted.push('\\');
|
||||
}
|
||||
quoted.push(char);
|
||||
}
|
||||
quoted.push('"');
|
||||
Ok(Some(quoted))
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
//! Text without attachments, from BODYSTRUCTURE (§12.3).
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use imap_proto::{BodyParams, BodyStructure, MessageSection, SectionPath};
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
/// A message's MIME tree; paths are IMAP part numbers.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Node {
|
||||
/// `body` parts come down; the rest wait for a request.
|
||||
Leaf { path: Vec<u32>, body: bool },
|
||||
Multi {
|
||||
path: Vec<u32>,
|
||||
boundary: String,
|
||||
children: Vec<Node>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// The reader's rules: `mime/walk.rs`.
|
||||
pub(super) fn read(structure: &BodyStructure) -> Result<Self> {
|
||||
match structure {
|
||||
BodyStructure::Multipart { .. } => body(structure, Vec::new()),
|
||||
single => Ok(Node::Leaf {
|
||||
path: vec![1],
|
||||
body: !attachment(single) && shown(single),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// True when nothing is left out.
|
||||
pub fn complete(&self) -> bool {
|
||||
match self {
|
||||
Node::Leaf { body, .. } => *body,
|
||||
Node::Multi { children, .. } => children.iter().all(Node::complete),
|
||||
}
|
||||
}
|
||||
|
||||
/// Part numbers left out, as `2` or `1.3`.
|
||||
pub fn missing(&self) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
self.walk(&mut |node| {
|
||||
if let Node::Leaf { path, body: false } = node {
|
||||
out.push(number(path));
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// The FETCH sections that rebuild it.
|
||||
pub fn sections(&self) -> Vec<String> {
|
||||
let mut out = vec!["HEADER".to_owned()];
|
||||
if let Node::Leaf { body, .. } = self {
|
||||
if *body {
|
||||
out.push("1".into());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
self.walk(&mut |node| match node {
|
||||
Node::Leaf { path, body } => {
|
||||
out.push(mime(path));
|
||||
if *body {
|
||||
out.push(number(path));
|
||||
}
|
||||
}
|
||||
Node::Multi { path, .. } if !path.is_empty() => out.push(mime(path)),
|
||||
Node::Multi { .. } => {}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// The message with left-out parts empty.
|
||||
pub fn build(&self, got: &HashMap<String, Vec<u8>>) -> Result<Vec<u8>> {
|
||||
let mut out = header(take(got, "HEADER")?);
|
||||
match self {
|
||||
Node::Leaf { body: true, .. } => out.extend_from_slice(take(got, "1")?),
|
||||
Node::Leaf { body: false, .. } => {}
|
||||
multi => content(multi, got, &mut out)?,
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn walk(&self, each: &mut impl FnMut(&Node)) {
|
||||
each(self);
|
||||
if let Node::Multi { children, .. } = self {
|
||||
for child in children {
|
||||
child.walk(each);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The key a FETCH section comes back under.
|
||||
pub(super) fn section_key(section: &Option<SectionPath>) -> String {
|
||||
let named = |part: &MessageSection| match part {
|
||||
MessageSection::Header => "HEADER",
|
||||
MessageSection::Mime => "MIME",
|
||||
MessageSection::Text => "TEXT",
|
||||
};
|
||||
match section {
|
||||
None => String::new(),
|
||||
Some(SectionPath::Full(part)) => named(part).to_owned(),
|
||||
Some(SectionPath::Part(path, None)) => number(path),
|
||||
Some(SectionPath::Part(path, Some(part))) => format!("{}.{}", number(path), named(part)),
|
||||
}
|
||||
}
|
||||
|
||||
/// RFC 2183 §2.8, §2.9: the whole entity waits.
|
||||
fn body(structure: &BodyStructure, path: Vec<u32>) -> Result<Node> {
|
||||
if attachment(structure) {
|
||||
return Ok(Node::Leaf { path, body: false });
|
||||
}
|
||||
inside(structure, path)
|
||||
}
|
||||
|
||||
fn inside(structure: &BodyStructure, path: Vec<u32>) -> Result<Node> {
|
||||
let BodyStructure::Multipart { common, bodies, .. } = structure else {
|
||||
return Ok(Node::Leaf {
|
||||
body: shown(structure),
|
||||
path,
|
||||
});
|
||||
};
|
||||
let boundary = param(&common.ty.params, "boundary")
|
||||
.ok_or_else(|| Error::Protocol("a multipart came without a boundary".into()))?;
|
||||
let related = common.ty.subtype.eq_ignore_ascii_case("related");
|
||||
let root = related.then(|| root(&common.ty.params, bodies));
|
||||
let children = bodies
|
||||
.iter()
|
||||
.zip(1..)
|
||||
.enumerate()
|
||||
.map(|(at, (child, number))| {
|
||||
let mut next = path.clone();
|
||||
next.push(number);
|
||||
match root {
|
||||
None => body(child, next),
|
||||
Some(Root::At(root)) if root == at => inside(child, next),
|
||||
Some(Root::Unseen) if multipart(child) => inside(child, next),
|
||||
// RFC 2387 §4, §6.1: one entity, no disposition.
|
||||
Some(_) => Ok(Node::Leaf {
|
||||
body: id(child).is_some(),
|
||||
path: next,
|
||||
}),
|
||||
}
|
||||
})
|
||||
.collect::<Result<_>>()?;
|
||||
Ok(Node::Multi {
|
||||
path,
|
||||
boundary: boundary.to_owned(),
|
||||
children,
|
||||
})
|
||||
}
|
||||
|
||||
/// The related root, or one hidden in a multipart.
|
||||
#[derive(Clone, Copy)]
|
||||
enum Root {
|
||||
At(usize),
|
||||
Unseen,
|
||||
}
|
||||
|
||||
/// RFC 2387: `start` names the root, else the first.
|
||||
fn root(params: &BodyParams, bodies: &[BodyStructure]) -> Root {
|
||||
let Some(start) = param(params, "start") else {
|
||||
return Root::At(0);
|
||||
};
|
||||
if let Some(at) = bodies
|
||||
.iter()
|
||||
.position(|child| id(child).is_some_and(|id| bare(id) == bare(start)))
|
||||
{
|
||||
return Root::At(at);
|
||||
}
|
||||
if bodies.iter().any(multipart) {
|
||||
return Root::Unseen;
|
||||
}
|
||||
log::warn!("mail: related start names no part; first part is the root");
|
||||
Root::At(0)
|
||||
}
|
||||
|
||||
fn multipart(structure: &BodyStructure) -> bool {
|
||||
matches!(structure, BodyStructure::Multipart { .. })
|
||||
}
|
||||
|
||||
/// Plain and HTML text, and `cid:` targets.
|
||||
fn shown(structure: &BodyStructure) -> bool {
|
||||
match structure {
|
||||
BodyStructure::Text { common, .. }
|
||||
if ["plain", "html"]
|
||||
.iter()
|
||||
.any(|kind| common.ty.subtype.eq_ignore_ascii_case(kind)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
BodyStructure::Text { .. } | BodyStructure::Basic { .. } => id(structure).is_some(),
|
||||
BodyStructure::Message { .. } | BodyStructure::Multipart { .. } => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// RFC 2183 §2.8: unknown dispositions are attachments.
|
||||
fn attachment(structure: &BodyStructure) -> bool {
|
||||
let common = match structure {
|
||||
BodyStructure::Basic { common, .. }
|
||||
| BodyStructure::Text { common, .. }
|
||||
| BodyStructure::Message { common, .. }
|
||||
| BodyStructure::Multipart { common, .. } => common,
|
||||
};
|
||||
common
|
||||
.disposition
|
||||
.as_ref()
|
||||
.is_some_and(|it| !it.ty.eq_ignore_ascii_case("inline"))
|
||||
}
|
||||
|
||||
fn id<'a>(structure: &'a BodyStructure) -> Option<&'a str> {
|
||||
match structure {
|
||||
BodyStructure::Basic { other, .. }
|
||||
| BodyStructure::Text { other, .. }
|
||||
| BodyStructure::Message { other, .. } => other.id.as_deref(),
|
||||
BodyStructure::Multipart { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn bare(id: &str) -> &str {
|
||||
id.trim().trim_start_matches('<').trim_end_matches('>')
|
||||
}
|
||||
|
||||
fn param<'a>(params: &'a BodyParams, name: &str) -> Option<&'a str> {
|
||||
params
|
||||
.iter()
|
||||
.flatten()
|
||||
.find(|(key, _)| key.eq_ignore_ascii_case(name))
|
||||
.map(|(_, value)| value.as_ref())
|
||||
}
|
||||
|
||||
fn content(node: &Node, got: &HashMap<String, Vec<u8>>, out: &mut Vec<u8>) -> Result<()> {
|
||||
match node {
|
||||
Node::Leaf { path, body: true } => out.extend_from_slice(take(got, &number(path))?),
|
||||
Node::Leaf { body: false, .. } => {}
|
||||
Node::Multi {
|
||||
boundary, children, ..
|
||||
} => {
|
||||
for child in children {
|
||||
let path = match child {
|
||||
Node::Leaf { path, .. } | Node::Multi { path, .. } => path,
|
||||
};
|
||||
out.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
|
||||
out.extend(header(take(got, &mime(path))?));
|
||||
content(child, got, out)?;
|
||||
out.extend_from_slice(b"\r\n");
|
||||
}
|
||||
out.extend_from_slice(format!("--{boundary}--\r\n").as_bytes());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A header block ends with a blank line.
|
||||
fn header(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut out = bytes.to_vec();
|
||||
if out.is_empty() {
|
||||
return b"\r\n".to_vec();
|
||||
}
|
||||
while !out.ends_with(b"\r\n\r\n") {
|
||||
out.extend_from_slice(b"\r\n");
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn take<'a>(got: &'a HashMap<String, Vec<u8>>, key: &str) -> Result<&'a [u8]> {
|
||||
got.get(key)
|
||||
.map(Vec::as_slice)
|
||||
.ok_or_else(|| Error::Protocol(format!("section {key} did not come")))
|
||||
}
|
||||
|
||||
/// A whole message's body is `TEXT`.
|
||||
fn number(path: &[u32]) -> String {
|
||||
if path.is_empty() {
|
||||
return "TEXT".into();
|
||||
}
|
||||
path.iter()
|
||||
.map(u32::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join(".")
|
||||
}
|
||||
|
||||
fn mime(path: &[u32]) -> String {
|
||||
format!("{}.MIME", number(path))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,151 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use imap_proto::{AttributeValue, Response};
|
||||
|
||||
use super::Node;
|
||||
|
||||
/// The node of a `* 1 FETCH (BODYSTRUCTURE …)` line.
|
||||
fn node(structure: &str) -> Node {
|
||||
let line = format!("* 1 FETCH (BODYSTRUCTURE {structure})\r\n");
|
||||
let (_, response) = Response::parse(line.as_bytes()).unwrap();
|
||||
let Response::Fetch(_, attributes) = response else {
|
||||
panic!("not a FETCH: {line}");
|
||||
};
|
||||
let [AttributeValue::BodyStructure(structure)] = attributes.as_slice() else {
|
||||
panic!("no structure: {line}");
|
||||
};
|
||||
Node::read(structure).unwrap()
|
||||
}
|
||||
|
||||
/// Reads the rebuilt message; sections not given come empty.
|
||||
fn rebuilt(node: &Node, given: &[(&str, &str)]) -> crate::Mail {
|
||||
let mut got: HashMap<String, Vec<u8>> = given
|
||||
.iter()
|
||||
.map(|(key, text)| ((*key).to_owned(), text.replace('\n', "\r\n").into_bytes()))
|
||||
.collect();
|
||||
for section in node.sections() {
|
||||
got.entry(section).or_default();
|
||||
}
|
||||
crate::read(&node.build(&got).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
const TEXT: &str = "(\"TEXT\" \"PLAIN\" (\"CHARSET\" \"us-ascii\") NIL NIL \"7BIT\" 6 1)";
|
||||
const HTML: &str = "(\"TEXT\" \"HTML\" (\"CHARSET\" \"us-ascii\") NIL NIL \"7BIT\" 9 1)";
|
||||
|
||||
#[test]
|
||||
fn an_attached_multipart_waits_whole() {
|
||||
let structure = format!(
|
||||
"({TEXT}({TEXT}{HTML} \"ALTERNATIVE\" (\"BOUNDARY\" \"b2\") \
|
||||
(\"ATTACHMENT\" (\"FILENAME\" \"note\")) NIL NIL) \"MIXED\" (\"BOUNDARY\" \"b1\") NIL NIL NIL)"
|
||||
);
|
||||
let node = node(&structure);
|
||||
assert!(!node.complete());
|
||||
assert_eq!(node.missing(), ["2"]);
|
||||
assert_eq!(node.sections(), ["HEADER", "1.MIME", "1", "2.MIME"]);
|
||||
let mail = rebuilt(
|
||||
&node,
|
||||
&[
|
||||
(
|
||||
"HEADER",
|
||||
"MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"b1\"\n\n",
|
||||
),
|
||||
("1.MIME", "Content-Type: text/plain\n\n"),
|
||||
("1", "hello\n"),
|
||||
(
|
||||
"2.MIME",
|
||||
"Content-Type: multipart/alternative; boundary=\"b2\"\n\
|
||||
Content-Disposition: attachment; filename=\"note\"\n\n",
|
||||
),
|
||||
],
|
||||
);
|
||||
assert_eq!(mail.text.as_deref().map(str::trim_end), Some("hello"));
|
||||
assert_eq!(mail.attachments.len(), 1);
|
||||
assert_eq!(mail.attachments[0].content_type, "multipart/alternative");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_related_image_marked_attachment_comes_down() {
|
||||
let structure = format!(
|
||||
"({HTML}(\"IMAGE\" \"PNG\" NIL \"<logo>\" NIL \"BASE64\" 12 NIL \
|
||||
(\"ATTACHMENT\" (\"FILENAME\" \"logo.png\")) NIL NIL) \
|
||||
\"RELATED\" (\"BOUNDARY\" \"r1\") NIL NIL NIL)"
|
||||
);
|
||||
let node = node(&structure);
|
||||
assert!(node.complete());
|
||||
assert!(node.missing().is_empty());
|
||||
let mail = rebuilt(
|
||||
&node,
|
||||
&[
|
||||
(
|
||||
"HEADER",
|
||||
"MIME-Version: 1.0\nContent-Type: multipart/related; boundary=\"r1\"\n\n",
|
||||
),
|
||||
("1.MIME", "Content-Type: text/html\n\n"),
|
||||
("1", "<img src=\"cid:logo\">\n"),
|
||||
(
|
||||
"2.MIME",
|
||||
"Content-Type: image/png\nContent-ID: <logo>\n\
|
||||
Content-Disposition: attachment; filename=\"logo.png\"\n\
|
||||
Content-Transfer-Encoding: base64\n\n",
|
||||
),
|
||||
("2", "iVBORw0KGgo=\n"),
|
||||
],
|
||||
);
|
||||
assert_eq!(mail.inline.len(), 1);
|
||||
assert_eq!(mail.inline[0].bytes, b"\x89PNG\r\n\x1a\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_related_start_root_ignores_its_disposition() {
|
||||
let structure = "((\"IMAGE\" \"PNG\" NIL NIL NIL \"BASE64\" 8 NIL NIL NIL NIL)\
|
||||
(\"TEXT\" \"HTML\" NIL \"<root>\" NIL \"7BIT\" 9 1 NIL (\"ATTACHMENT\" NIL) NIL NIL) \
|
||||
\"RELATED\" (\"BOUNDARY\" \"r1\" \"START\" \"<root>\") NIL NIL NIL)";
|
||||
let node = node(structure);
|
||||
assert_eq!(node.missing(), ["1"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_attached_message_root_waits_whole() {
|
||||
let structure =
|
||||
format!("({TEXT}{TEXT} \"MIXED\" (\"BOUNDARY\" \"b1\") (\"ATTACHMENT\" NIL) NIL NIL)");
|
||||
let node = node(&structure);
|
||||
assert_eq!(node.missing(), ["TEXT"]);
|
||||
assert_eq!(node.sections(), ["HEADER"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_related_start_may_name_a_multipart() {
|
||||
let structure = format!(
|
||||
"((\"IMAGE\" \"PNG\" NIL \"<logo>\" NIL \"BASE64\" 12 NIL NIL NIL NIL)\
|
||||
({TEXT}{HTML} \"ALTERNATIVE\" (\"BOUNDARY\" \"b2\") NIL NIL NIL) \
|
||||
\"RELATED\" (\"BOUNDARY\" \"r1\" \"START\" \"<root>\") NIL NIL NIL)"
|
||||
);
|
||||
let node = node(&structure);
|
||||
assert!(node.complete());
|
||||
let mail = rebuilt(
|
||||
&node,
|
||||
&[
|
||||
(
|
||||
"HEADER",
|
||||
"MIME-Version: 1.0\nContent-Type: multipart/related; boundary=\"r1\"; \
|
||||
start=\"<root>\"\n\n",
|
||||
),
|
||||
(
|
||||
"1.MIME",
|
||||
"Content-Type: image/png\nContent-ID: <logo>\n\
|
||||
Content-Transfer-Encoding: base64\n\n",
|
||||
),
|
||||
("1", "iVBORw0KGgo=\n"),
|
||||
(
|
||||
"2.MIME",
|
||||
"Content-Type: multipart/alternative; boundary=\"b2\"\nContent-ID: <root>\n\n",
|
||||
),
|
||||
("2.1.MIME", "Content-Type: text/plain\n\n"),
|
||||
("2.1", "hello\n"),
|
||||
("2.2.MIME", "Content-Type: text/html\n\n"),
|
||||
("2.2", "<p>hello</p>\n"),
|
||||
],
|
||||
);
|
||||
assert_eq!(mail.text.as_deref().map(str::trim_end), Some("hello"));
|
||||
assert!(mail.html.is_some());
|
||||
}
|
||||
@@ -9,16 +9,19 @@ pub mod oauth;
|
||||
mod pop;
|
||||
mod smtp;
|
||||
mod store;
|
||||
pub mod sync;
|
||||
mod vault;
|
||||
mod wire;
|
||||
|
||||
pub use account::{Account, Auth, Checked, Protocol};
|
||||
pub use error::{Error, Result};
|
||||
pub use imap::Imap;
|
||||
pub use imap::{Folder, Got, Imap, Opened, Role};
|
||||
pub use mime::{Built, Draft, Mail, Mailbox, Part, build, read};
|
||||
pub use net::{Login, Secret, Security, Server, system_tls};
|
||||
pub use pop::Pop;
|
||||
pub use smtp::Smtp;
|
||||
pub use store::{AccountId, Cached, Fetched, Key, MailboxId, MessageId, Sizes, Store, Stored};
|
||||
pub use store::{
|
||||
AccountId, Cached, Fetched, Held, Key, MailboxId, MessageId, Sizes, Store, Stored, Synced,
|
||||
};
|
||||
pub use tokio_rustls::TlsConnector;
|
||||
pub use vault::{Backend, Kept, Keyring, Vault};
|
||||
@@ -12,6 +12,7 @@ use crate::{Error, Result};
|
||||
|
||||
mod accounts;
|
||||
mod aside;
|
||||
mod folders;
|
||||
mod html;
|
||||
mod messages;
|
||||
mod schema;
|
||||
@@ -20,6 +21,7 @@ mod search;
|
||||
mod tests;
|
||||
|
||||
pub use accounts::Cached;
|
||||
pub use folders::{Held, Synced};
|
||||
pub use messages::{Fetched, Key, Stored};
|
||||
|
||||
const DB: &str = "mail.db";
|
||||
@@ -68,6 +70,7 @@ impl Store {
|
||||
|
||||
/// The mailbox by name, made on first use.
|
||||
pub fn mailbox(&self, account: AccountId, name: &str) -> Result<MailboxId> {
|
||||
let name = mailbox_name(name);
|
||||
let find = "SELECT id FROM mailboxes WHERE account = ?1 AND name = ?2";
|
||||
if let Some(id) = self
|
||||
.conn
|
||||
@@ -215,3 +218,11 @@ fn folder_size(folder: &Path) -> Result<u64> {
|
||||
fn disk(step: &str, err: io::Error) -> Error {
|
||||
Error::Store(format!("{step}: {err}"))
|
||||
}
|
||||
|
||||
/// RFC 3501 §5.1: INBOX in any case is INBOX.
|
||||
fn mailbox_name(name: &str) -> &str {
|
||||
match name.eq_ignore_ascii_case("INBOX") {
|
||||
true => "INBOX",
|
||||
false => name,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
//! IMAP folders as mailboxes, and how far they are synced.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rusqlite::{Transaction, params};
|
||||
|
||||
use super::{AccountId, MailboxId, MessageId, Store, aside::Aside, mailbox_name};
|
||||
use crate::{Error, Result, Role};
|
||||
|
||||
/// A local IMAP message as sync compares it.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Held {
|
||||
pub id: MessageId,
|
||||
/// The body is here, perhaps without attachments.
|
||||
pub body: bool,
|
||||
/// Sorted.
|
||||
pub flags: Vec<String>,
|
||||
}
|
||||
|
||||
/// An account's sync, as the window shows it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Synced {
|
||||
/// Unix seconds of the last full sync.
|
||||
pub at: Option<i64>,
|
||||
pub messages: u64,
|
||||
/// Messages with only their header here.
|
||||
pub uncached: u64,
|
||||
/// Messages whose attachments wait for a request.
|
||||
pub partial: u64,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
/// Keeps the listed folders; the rest go with their mail.
|
||||
pub fn set_folders(
|
||||
&mut self,
|
||||
account: AccountId,
|
||||
folders: &[(&str, Option<Role>)],
|
||||
) -> Result<Vec<MailboxId>> {
|
||||
self.writable()?;
|
||||
let held: Vec<(i64, String)> = self
|
||||
.conn
|
||||
.prepare("SELECT id, name FROM mailboxes WHERE account = ?1")?
|
||||
.query_map([account.0], |row| Ok((row.get(0)?, row.get(1)?)))?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
for (id, name) in held {
|
||||
if !folders
|
||||
.iter()
|
||||
.any(|(listed, _)| mailbox_name(listed) == name)
|
||||
{
|
||||
self.without_mail(MailboxId(id), None, |tx| {
|
||||
tx.execute("DELETE FROM mailboxes WHERE id = ?1", [id])
|
||||
})?;
|
||||
log::info!("mail: mailbox {id} is no longer listed; removed");
|
||||
}
|
||||
}
|
||||
folders
|
||||
.iter()
|
||||
.map(|(name, role)| {
|
||||
let id = self.mailbox(account, name)?;
|
||||
self.conn.execute(
|
||||
"UPDATE mailboxes SET role = ?1 WHERE id = ?2",
|
||||
params![role.map(Role::name), id.0],
|
||||
)?;
|
||||
Ok(id)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The account's mailboxes in name order.
|
||||
pub fn folders(&self, account: AccountId) -> Result<Vec<(MailboxId, String, Option<Role>)>> {
|
||||
let mut query = self
|
||||
.conn
|
||||
.prepare("SELECT id, name, role FROM mailboxes WHERE account = ?1 ORDER BY name")?;
|
||||
let rows = query.query_map([account.0], |row| {
|
||||
Ok((
|
||||
MailboxId(row.get(0)?),
|
||||
row.get(1)?,
|
||||
row.get::<_, Option<String>>(2)?,
|
||||
))
|
||||
})?;
|
||||
rows.map(|row| {
|
||||
let (id, name, role) = row?;
|
||||
let role = role
|
||||
.map(|role| {
|
||||
Role::from_name(&role)
|
||||
.ok_or_else(|| Error::Store(format!("an unknown folder role {role:?}")))
|
||||
})
|
||||
.transpose()?;
|
||||
Ok((id, name, role))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Held messages by UID; mail under another UIDVALIDITY goes.
|
||||
pub fn open_mailbox(
|
||||
&mut self,
|
||||
mailbox: MailboxId,
|
||||
uidvalidity: u32,
|
||||
) -> Result<HashMap<u32, Held>> {
|
||||
self.writable()?;
|
||||
let known: Option<u32> = self.conn.query_row(
|
||||
"SELECT uidvalidity FROM mailboxes WHERE id = ?1",
|
||||
[mailbox.0],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
if known != Some(uidvalidity) {
|
||||
if known.is_some() {
|
||||
log::warn!("mail: mailbox {} has a new UIDVALIDITY; rebuilt", mailbox.0);
|
||||
}
|
||||
self.without_mail(mailbox, Some(uidvalidity), |tx| {
|
||||
tx.execute(
|
||||
"UPDATE mailboxes SET uidvalidity = ?1 WHERE id = ?2",
|
||||
params![uidvalidity, mailbox.0],
|
||||
)
|
||||
})?;
|
||||
}
|
||||
let mut held: HashMap<u32, Held> = HashMap::new();
|
||||
let mut query = self.conn.prepare(
|
||||
"SELECT id, uid, content IS NOT NULL FROM messages
|
||||
WHERE mailbox = ?1 AND uidvalidity = ?2",
|
||||
)?;
|
||||
let rows = query.query_map(params![mailbox.0, uidvalidity], |row| {
|
||||
Ok((row.get::<_, u32>(1)?, MessageId(row.get(0)?), row.get(2)?))
|
||||
})?;
|
||||
for row in rows {
|
||||
let (uid, id, body) = row?;
|
||||
held.insert(
|
||||
uid,
|
||||
Held {
|
||||
id,
|
||||
body,
|
||||
flags: Vec::new(),
|
||||
},
|
||||
);
|
||||
}
|
||||
let mut query = self.conn.prepare(
|
||||
"SELECT messages.uid, flags.flag FROM flags JOIN messages ON flags.message = messages.id
|
||||
WHERE messages.mailbox = ?1 AND messages.uidvalidity = ?2 ORDER BY flags.flag",
|
||||
)?;
|
||||
let rows = query.query_map(params![mailbox.0, uidvalidity], |row| {
|
||||
Ok((row.get::<_, u32>(0)?, row.get::<_, String>(1)?))
|
||||
})?;
|
||||
for row in rows {
|
||||
let (uid, flag) = row?;
|
||||
held.get_mut(&uid)
|
||||
.ok_or_else(|| Error::Store(format!("a flag for no message {uid}")))?
|
||||
.flags
|
||||
.push(flag);
|
||||
}
|
||||
Ok(held)
|
||||
}
|
||||
|
||||
pub fn set_synced(&self, account: AccountId, at: i64) -> Result<()> {
|
||||
self.writable()?;
|
||||
let changed = self.conn.execute(
|
||||
"UPDATE accounts SET synced = ?1 WHERE id = ?2",
|
||||
params![at, account.0],
|
||||
)?;
|
||||
if changed != 1 {
|
||||
return Err(Error::Store(format!("no account {}", account.0)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn synced(&self, account: AccountId) -> Result<Synced> {
|
||||
let at = self.conn.query_row(
|
||||
"SELECT synced FROM accounts WHERE id = ?1",
|
||||
[account.0],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
let (messages, cached, partial): (i64, i64, i64) = self.conn.query_row(
|
||||
"SELECT count(*), count(content), count(CASE WHEN missing <> '[]' THEN 1 END)
|
||||
FROM messages JOIN mailboxes ON messages.mailbox = mailboxes.id
|
||||
WHERE mailboxes.account = ?1",
|
||||
[account.0],
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
|
||||
)?;
|
||||
let count =
|
||||
|n: i64| u64::try_from(n).map_err(|_| Error::Store(format!("a negative count: {n}")));
|
||||
Ok(Synced {
|
||||
at,
|
||||
messages: count(messages)?,
|
||||
uncached: count(messages - cached)?,
|
||||
partial: count(partial)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Deletes the mailbox's messages not under `keep`, with `change`.
|
||||
fn without_mail(
|
||||
&mut self,
|
||||
mailbox: MailboxId,
|
||||
keep: Option<u32>,
|
||||
change: impl FnOnce(&Transaction) -> rusqlite::Result<usize>,
|
||||
) -> Result<()> {
|
||||
let hashes: Vec<String> = self
|
||||
.conn
|
||||
.prepare(&format!(
|
||||
"SELECT DISTINCT content FROM messages WHERE {STALE} AND content IS NOT NULL"
|
||||
))?
|
||||
.query_map(params![mailbox.0, keep], |row| row.get(0))?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
let tx = self.conn.transaction()?;
|
||||
let mut aside = Aside::new(&self.dir);
|
||||
let done = (|| -> Result<()> {
|
||||
tx.execute(
|
||||
&format!("DELETE FROM messages WHERE {STALE}"),
|
||||
params![mailbox.0, keep],
|
||||
)?;
|
||||
change(&tx)?;
|
||||
for hash in &hashes {
|
||||
aside.drop_unused(&tx, hash)?;
|
||||
}
|
||||
Ok(tx.commit()?)
|
||||
})();
|
||||
aside.settle(done)
|
||||
}
|
||||
}
|
||||
|
||||
const STALE: &str = "mailbox = ?1 AND (?2 IS NULL OR uidvalidity IS NOT ?2)";
|
||||
@@ -22,6 +22,11 @@ pub enum Key {
|
||||
/// What a sync brought down.
|
||||
pub enum Fetched<'a> {
|
||||
Headers(&'a [u8]),
|
||||
/// Left-out parts are empty and named by part number.
|
||||
Text {
|
||||
bytes: &'a [u8],
|
||||
missing: &'a [String],
|
||||
},
|
||||
Whole(&'a [u8]),
|
||||
}
|
||||
|
||||
@@ -40,13 +45,15 @@ pub struct Stored {
|
||||
pub to: Vec<Mailbox>,
|
||||
pub cc: Vec<Mailbox>,
|
||||
pub date: Option<i64>,
|
||||
/// None until the whole message is here.
|
||||
/// None until the body is here.
|
||||
pub attachments: Option<u32>,
|
||||
pub whole: bool,
|
||||
pub body: bool,
|
||||
/// Part numbers not downloaded; their content is empty.
|
||||
pub missing: Vec<String>,
|
||||
}
|
||||
|
||||
const COLUMNS: &str = "id, mailbox, uidvalidity, uid, uidl, message_id, in_reply_to, refs, \
|
||||
subject, sender, recipients, copies, date, attachments, content";
|
||||
subject, sender, recipients, copies, date, attachments, content, missing";
|
||||
|
||||
impl Store {
|
||||
pub fn find(&self, mailbox: MailboxId, key: &Key) -> Result<Option<MessageId>> {
|
||||
@@ -75,7 +82,7 @@ impl Store {
|
||||
) -> Result<MessageId> {
|
||||
self.writable()?;
|
||||
let existing = self.find(mailbox, key)?;
|
||||
let bytes = match (fetched, existing) {
|
||||
let (bytes, missing): (&[u8], &[String]) = match (fetched, existing) {
|
||||
(Fetched::Headers(_), Some(id)) => {
|
||||
self.set_flags(id, flags)?;
|
||||
return Ok(id);
|
||||
@@ -84,11 +91,13 @@ impl Store {
|
||||
let mail = read(bytes)?;
|
||||
return self.write(mailbox, key, flags, &mail, None, None);
|
||||
}
|
||||
(Fetched::Whole(bytes), _) => bytes,
|
||||
(Fetched::Text { bytes, missing }, _) => (bytes, missing),
|
||||
(Fetched::Whole(bytes), _) => (bytes, &[][..]),
|
||||
};
|
||||
let mail = read(bytes)?;
|
||||
let (content, made) = self.keep(bytes)?;
|
||||
let written = self.write(mailbox, key, flags, &mail, Some(&content), existing);
|
||||
let body = Some((content.as_str(), missing));
|
||||
let written = self.write(mailbox, key, flags, &mail, body, existing);
|
||||
if written.is_err() && made {
|
||||
let mut aside = Aside::new(&self.dir);
|
||||
let gone = aside.drop_unused(&self.conn, &content);
|
||||
@@ -105,9 +114,11 @@ impl Store {
|
||||
key: &Key,
|
||||
flags: &[&str],
|
||||
mail: &Mail,
|
||||
content: Option<&str>,
|
||||
body: Option<(&str, &[String])>,
|
||||
existing: Option<MessageId>,
|
||||
) -> Result<MessageId> {
|
||||
let content = body.map(|(content, _)| content);
|
||||
let missing = body.map_or(&[][..], |(_, missing)| missing);
|
||||
let (uidvalidity, uid, uidl) = match key {
|
||||
Key::Imap { uidvalidity, uid } => (Some(*uidvalidity), Some(*uid), None),
|
||||
Key::Pop { uidl } => (None, None, Some(uidl.as_str())),
|
||||
@@ -119,6 +130,7 @@ impl Store {
|
||||
serde_json::to_string(&mail.from)?,
|
||||
serde_json::to_string(&mail.to)?,
|
||||
serde_json::to_string(&mail.cc)?,
|
||||
serde_json::to_string(missing)?,
|
||||
];
|
||||
let mut values: Vec<&dyn ToSql> = vec![
|
||||
&mail.message_id,
|
||||
@@ -131,6 +143,7 @@ impl Store {
|
||||
&mail.date,
|
||||
&attachments,
|
||||
&content,
|
||||
&json[5],
|
||||
];
|
||||
let tx = self.conn.transaction()?;
|
||||
let mut aside = Aside::new(&self.dir);
|
||||
@@ -147,7 +160,7 @@ impl Store {
|
||||
tx.execute(
|
||||
"UPDATE messages SET message_id = ?1, in_reply_to = ?2, refs = ?3,
|
||||
subject = ?4, sender = ?5, recipients = ?6, copies = ?7, date = ?8,
|
||||
attachments = ?9, content = ?10 WHERE id = ?11",
|
||||
attachments = ?9, content = ?10, missing = ?11 WHERE id = ?12",
|
||||
values.as_slice(),
|
||||
)?;
|
||||
*id
|
||||
@@ -156,9 +169,9 @@ impl Store {
|
||||
values.extend::<[&dyn ToSql; 4]>([&mailbox.0, &uidvalidity, &uid, &uidl]);
|
||||
tx.execute(
|
||||
"INSERT INTO messages (message_id, in_reply_to, refs, subject, sender,
|
||||
recipients, copies, date, attachments, content, mailbox, uidvalidity,
|
||||
uid, uidl) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12,
|
||||
?13, ?14)",
|
||||
recipients, copies, date, attachments, content, missing, mailbox,
|
||||
uidvalidity, uid, uidl) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
|
||||
?11, ?12, ?13, ?14, ?15)",
|
||||
values.as_slice(),
|
||||
)?;
|
||||
MessageId(tx.last_insert_rowid())
|
||||
@@ -295,7 +308,8 @@ fn row(row: &Row) -> Result<Stored> {
|
||||
cc: serde_json::from_str(&json(11)?)?,
|
||||
date: row.get(12)?,
|
||||
attachments: row.get(13)?,
|
||||
whole: row.get::<_, Option<String>>(14)?.is_some(),
|
||||
body: row.get::<_, Option<String>>(14)?.is_some(),
|
||||
missing: serde_json::from_str(&json(15)?)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/// Step `n` upgrades version `n`.
|
||||
pub(super) const STEPS: &[&str] = &[V1];
|
||||
pub(super) const STEPS: &[&str] = &[V1, V2];
|
||||
|
||||
const V1: &str = "
|
||||
CREATE TABLE accounts (
|
||||
@@ -50,3 +50,11 @@ CREATE TRIGGER messages_gone AFTER DELETE ON messages BEGIN
|
||||
DELETE FROM search WHERE rowid = old.id;
|
||||
END;
|
||||
";
|
||||
|
||||
/// IMAP sync (C12.6).
|
||||
const V2: &str = "
|
||||
ALTER TABLE accounts ADD COLUMN synced INTEGER;
|
||||
ALTER TABLE mailboxes ADD COLUMN role TEXT;
|
||||
ALTER TABLE mailboxes ADD COLUMN uidvalidity INTEGER;
|
||||
ALTER TABLE messages ADD COLUMN missing TEXT NOT NULL DEFAULT '[]';
|
||||
";
|
||||
@@ -2,8 +2,9 @@ use std::fs;
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
use super::folders::Synced;
|
||||
use super::{BACKUPS, DB, open, schema::STEPS};
|
||||
use crate::{Account, Auth, Error, Protocol, Security, Server};
|
||||
use crate::{Account, Auth, Error, Protocol, Role, Security, Server};
|
||||
|
||||
fn account(address: &str) -> Account {
|
||||
let server = Server {
|
||||
@@ -110,3 +111,44 @@ fn no_backup_means_no_upgrade() {
|
||||
drop(store);
|
||||
assert_eq!(version(&dir.path().join(DB)), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_v1_store_gains_sync_and_keeps_its_mail() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let old = open(dir.path(), &[STEPS[0]]).unwrap();
|
||||
let ann = old.add_account(&account("ann@example.com")).unwrap();
|
||||
old.conn
|
||||
.execute_batch(&format!(
|
||||
"INSERT INTO mailboxes (account, name) VALUES ({}, 'INBOX');
|
||||
INSERT INTO messages (mailbox, uidvalidity, uid, in_reply_to, refs,
|
||||
sender, recipients, copies, content) VALUES
|
||||
(1, 7, 1, '[]', '[]', '[]', '[]', '[]', '{}'),
|
||||
(1, 6, 2, '[]', '[]', '[]', '[]', '[]', NULL);",
|
||||
ann.0,
|
||||
"a".repeat(64)
|
||||
))
|
||||
.unwrap();
|
||||
drop(old);
|
||||
let mut store = open(dir.path(), STEPS).unwrap();
|
||||
assert_eq!(store.read_only(), None);
|
||||
assert_eq!(version(&dir.path().join(DB)), STEPS.len() as i64);
|
||||
assert_eq!(backups(dir.path()).len(), 1);
|
||||
let synced = Synced {
|
||||
at: None,
|
||||
messages: 2,
|
||||
uncached: 1,
|
||||
partial: 0,
|
||||
};
|
||||
assert_eq!(store.synced(ann).unwrap(), synced);
|
||||
store
|
||||
.set_folders(ann, &[("INBOX", Some(Role::Inbox))])
|
||||
.unwrap();
|
||||
let [(inbox, name, role)] = store.folders(ann).unwrap().try_into().unwrap();
|
||||
assert_eq!((name.as_str(), role), ("INBOX", Some(Role::Inbox)));
|
||||
let held = store.open_mailbox(inbox, 7).unwrap();
|
||||
assert_eq!(held.keys().copied().collect::<Vec<_>>(), [1]);
|
||||
assert!(held[&1].body);
|
||||
store.set_synced(ann, 9).unwrap();
|
||||
assert_eq!(store.synced(ann).unwrap().at, Some(9));
|
||||
assert_eq!(store.synced(ann).unwrap().messages, 1);
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
//! IMAP sync: folders, flags, deletions and the 90-day window (§12.3).
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::{Mutex, MutexGuard},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Account, AccountId, Error, Fetched, Got, Held, Imap, Key, MailboxId, Result, Secret, Store,
|
||||
TlsConnector, Vault, oauth,
|
||||
};
|
||||
|
||||
/// Bodies come down for mail this recent.
|
||||
pub const BODY_DAYS: i64 = 90;
|
||||
|
||||
/// UIDs per FETCH.
|
||||
const BATCH: usize = 100;
|
||||
|
||||
/// What one sync changed.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Report {
|
||||
pub folders: usize,
|
||||
pub headers: usize,
|
||||
pub bodies: usize,
|
||||
pub removed: usize,
|
||||
}
|
||||
|
||||
/// The password, or an access token from the refresh token.
|
||||
pub async fn secret(account: &Account, vault: &Vault) -> Result<Secret> {
|
||||
let name = account.secret_name();
|
||||
let kept = vault
|
||||
.get(&name)?
|
||||
.ok_or_else(|| Error::Vault(format!("no saved secret for {name}")))?;
|
||||
let Some(provider) = account.auth.provider() else {
|
||||
return Ok(Secret::Password(kept));
|
||||
};
|
||||
let tokens = oauth::refresh(&provider?, &kept).await?;
|
||||
if let Some(next) = &tokens.refresh {
|
||||
vault.put(&name, next);
|
||||
}
|
||||
Ok(Secret::Token(tokens.access))
|
||||
}
|
||||
|
||||
/// One pass over every folder; `now` is Unix seconds.
|
||||
pub async fn imap(
|
||||
account: &Account,
|
||||
id: AccountId,
|
||||
secret: Secret,
|
||||
tls: &TlsConnector,
|
||||
store: &Mutex<Store>,
|
||||
now: i64,
|
||||
) -> Result<Report> {
|
||||
let mut imap = Imap::connect(&account.incoming, &account.login(secret), tls).await?;
|
||||
let folders: Vec<_> = imap
|
||||
.folders()
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|folder| folder.selectable)
|
||||
.collect();
|
||||
let named: Vec<_> = folders
|
||||
.iter()
|
||||
.map(|folder| (folder.name.as_str(), folder.role))
|
||||
.collect();
|
||||
let boxes = lock(store)?.set_folders(id, &named)?;
|
||||
let day = since(now)?;
|
||||
let mut report = Report {
|
||||
folders: folders.len(),
|
||||
..Report::default()
|
||||
};
|
||||
for (folder, mailbox) in folders.iter().zip(boxes) {
|
||||
one(&mut imap, store, mailbox, &folder.name, &day, &mut report).await?;
|
||||
}
|
||||
imap.logout().await?;
|
||||
lock(store)?.set_synced(id, now)?;
|
||||
log::info!(
|
||||
"mail: account {} synced: {} folders, {} headers, {} bodies, {} removed",
|
||||
id.0,
|
||||
report.folders,
|
||||
report.headers,
|
||||
report.bodies,
|
||||
report.removed
|
||||
);
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
async fn one(
|
||||
imap: &mut Imap,
|
||||
store: &Mutex<Store>,
|
||||
mailbox: MailboxId,
|
||||
name: &str,
|
||||
day: &str,
|
||||
report: &mut Report,
|
||||
) -> Result<()> {
|
||||
let opened = imap.examine(name).await?;
|
||||
let held = lock(store)?.open_mailbox(mailbox, opened.uidvalidity)?;
|
||||
if opened.exists == 0 {
|
||||
return remove(store, held.values(), report);
|
||||
}
|
||||
let server: HashMap<u32, Vec<String>> = imap.flags().await?.into_iter().collect();
|
||||
let gone = held
|
||||
.iter()
|
||||
.filter(|(uid, _)| !server.contains_key(uid))
|
||||
.map(|(_, held)| held);
|
||||
remove(store, gone, report)?;
|
||||
for (uid, held) in &held {
|
||||
match server.get(uid) {
|
||||
Some(flags) if *flags != held.flags => {
|
||||
lock(store)?.set_flags(held.id, &strs(flags))?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let recent: HashSet<u32> = imap.since(day).await?.into_iter().collect();
|
||||
let mut headers = Vec::new();
|
||||
let mut bodies = Vec::new();
|
||||
for uid in server.keys() {
|
||||
match (held.get(uid), recent.contains(uid)) {
|
||||
(None | Some(Held { body: false, .. }), true) => bodies.push(*uid),
|
||||
(None, false) => headers.push(*uid),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
headers.sort_unstable();
|
||||
bodies.sort_unstable();
|
||||
let key = |uid| Key::Imap {
|
||||
uidvalidity: opened.uidvalidity,
|
||||
uid,
|
||||
};
|
||||
for batch in headers.chunks(BATCH) {
|
||||
for got in imap.headers(batch).await? {
|
||||
put(
|
||||
store,
|
||||
mailbox,
|
||||
key(got.uid),
|
||||
&got,
|
||||
Fetched::Headers(&got.bytes),
|
||||
)?;
|
||||
report.headers += 1;
|
||||
}
|
||||
}
|
||||
for batch in bodies.chunks(BATCH) {
|
||||
let (whole, partial): (Vec<_>, Vec<_>) = imap
|
||||
.structures(batch)
|
||||
.await?
|
||||
.into_iter()
|
||||
.partition(|(_, node)| node.complete());
|
||||
let whole: Vec<u32> = whole.into_iter().map(|(uid, _)| uid).collect();
|
||||
if !whole.is_empty() {
|
||||
for got in imap.bodies(&whole).await? {
|
||||
put(
|
||||
store,
|
||||
mailbox,
|
||||
key(got.uid),
|
||||
&got,
|
||||
Fetched::Whole(&got.bytes),
|
||||
)?;
|
||||
report.bodies += 1;
|
||||
}
|
||||
}
|
||||
// §12.3: attachments wait for a request.
|
||||
for (uid, node) in partial {
|
||||
let Some(got) = imap.text(uid, &node).await? else {
|
||||
continue;
|
||||
};
|
||||
let missing = node.missing();
|
||||
let text = Fetched::Text {
|
||||
bytes: &got.bytes,
|
||||
missing: &missing,
|
||||
};
|
||||
put(store, mailbox, key(uid), &got, text)?;
|
||||
report.bodies += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn put(
|
||||
store: &Mutex<Store>,
|
||||
mailbox: MailboxId,
|
||||
key: Key,
|
||||
got: &Got,
|
||||
fetched: Fetched,
|
||||
) -> Result<()> {
|
||||
lock(store)?.put(mailbox, &key, &strs(&got.flags), fetched)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove<'a>(
|
||||
store: &Mutex<Store>,
|
||||
gone: impl Iterator<Item = &'a Held>,
|
||||
report: &mut Report,
|
||||
) -> Result<()> {
|
||||
for held in gone {
|
||||
lock(store)?.remove(held.id)?;
|
||||
report.removed += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn strs(flags: &[String]) -> Vec<&str> {
|
||||
flags.iter().map(String::as_str).collect()
|
||||
}
|
||||
|
||||
/// `SINCE` takes a date in English: `19-Jul-2026`.
|
||||
fn since(now: i64) -> Result<String> {
|
||||
let start = jiff::Timestamp::from_second(now - BODY_DAYS * 86_400)
|
||||
.map_err(|err| Error::Store(format!("a sync time: {err}")))?;
|
||||
Ok(start
|
||||
.to_zoned(jiff::tz::TimeZone::UTC)
|
||||
.strftime("%-d-%b-%Y")
|
||||
.to_string())
|
||||
}
|
||||
|
||||
fn lock(store: &Mutex<Store>) -> Result<MutexGuard<'_, Store>> {
|
||||
store
|
||||
.lock()
|
||||
.map_err(|_| Error::Store("a store task panicked".into()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn the_window_starts_ninety_days_back() {
|
||||
// 2026-10-19 12:00 UTC
|
||||
assert_eq!(super::since(1_792_411_200).unwrap(), "21-Jul-2026");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
mod fake;
|
||||
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use ely_mail_engine::{Error, Imap, Secret, Security, system_tls};
|
||||
use ely_mail_engine::{Error, Imap, Role, Secret, Security, system_tls};
|
||||
use fake::{certs, expect, login, send, serve};
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
@@ -19,13 +19,15 @@ async fn a_password_login_lists_and_fetches() {
|
||||
send("* OK ready\n"),
|
||||
expect("{tag} LOGIN \"ann@example.com\" \"pw 1\""),
|
||||
send("{tag} OK done\n"),
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST (\\HasNoChildren) \"/\" INBOX\n"),
|
||||
send("* LIST (\\HasNoChildren \\Sent) \"/\" \"Sent Mail\"\n"),
|
||||
send("{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK [READ-ONLY] in\n"),
|
||||
expect("{tag} UID FETCH 42 BODY.PEEK[]"),
|
||||
expect("{tag} UID FETCH 42 (UID FLAGS BODY.PEEK[])"),
|
||||
send(&format!(
|
||||
"* 1 FETCH (UID 42 BODY[] {{{}}}\n{lines})\n{{tag}} OK fetched\n",
|
||||
body.len()
|
||||
@@ -42,7 +44,15 @@ async fn a_password_login_lists_and_fetches() {
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(imap.mailboxes().await.unwrap(), ["INBOX", "Sent Mail"]);
|
||||
let folders = imap.folders().await.unwrap();
|
||||
let names: Vec<_> = folders.iter().map(|f| (f.name.as_str(), f.role)).collect();
|
||||
assert_eq!(
|
||||
names,
|
||||
[
|
||||
("INBOX", Some(Role::Inbox)),
|
||||
("Sent Mail", Some(Role::Sent))
|
||||
]
|
||||
);
|
||||
let fetched = imap.fetch("INBOX", 42).await.unwrap().unwrap();
|
||||
assert_eq!(String::from_utf8(fetched).unwrap(), body);
|
||||
imap.logout().await.unwrap();
|
||||
@@ -151,8 +161,8 @@ async fn a_missing_uid_is_none() {
|
||||
expect("{tag} LOGIN \"ann@example.com\" \"pw\""),
|
||||
send("{tag} OK done\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 9 BODY.PEEK[]"),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 9 (UID FLAGS BODY.PEEK[])"),
|
||||
send("{tag} OK nothing\n"),
|
||||
],
|
||||
)
|
||||
@@ -178,6 +188,8 @@ async fn an_unreadable_reply_is_a_protocol_error() {
|
||||
send("* OK ready\n"),
|
||||
expect("{tag} LOGIN \"ann@example.com\" \"pw\""),
|
||||
send("{tag} OK done\n"),
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST (((\n"),
|
||||
],
|
||||
@@ -190,7 +202,7 @@ async fn an_unreadable_reply_is_a_protocol_error() {
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let err = imap.mailboxes().await.unwrap_err();
|
||||
let err = imap.folders().await.unwrap_err();
|
||||
assert!(matches!(err, Error::Protocol(_)), "{err}");
|
||||
script.await.unwrap().unwrap();
|
||||
}
|
||||
@@ -257,18 +269,26 @@ async fn logged_in(steps: Vec<fake::Step>) -> (Imap, JoinHandle<Result<(), Strin
|
||||
#[tokio::test]
|
||||
async fn listed_names_lose_quote_escapes_and_open_as_listed() {
|
||||
let (mut imap, script) = logged_in(vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST () \"/\" \"a\\\"b\"\n"),
|
||||
send("* LIST () \"/\" \"c\\\\d\"\n"),
|
||||
send("* LIST () \"/\" {3}\ne\\\"\n"),
|
||||
send("{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"a\\\"b\""),
|
||||
send("{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 9 BODY.PEEK[]"),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 9 (UID FLAGS BODY.PEEK[])"),
|
||||
send("{tag} OK nothing\n"),
|
||||
])
|
||||
.await;
|
||||
let names = imap.mailboxes().await.unwrap();
|
||||
let names: Vec<_> = imap
|
||||
.folders()
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|f| f.name)
|
||||
.collect();
|
||||
assert_eq!(names, ["a\"b", "c\\d", "e\\\""]);
|
||||
assert_eq!(imap.fetch(&names[0], 9).await.unwrap(), None);
|
||||
script.await.unwrap().unwrap();
|
||||
@@ -277,12 +297,14 @@ async fn listed_names_lose_quote_escapes_and_open_as_listed() {
|
||||
#[tokio::test]
|
||||
async fn a_refused_list_keeps_the_server_words() {
|
||||
let (mut imap, script) = logged_in(vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST () \"/\" INBOX\n"),
|
||||
send("{tag} NO [UNAVAILABLE] Mailbox listing temporarily unavailable\n"),
|
||||
])
|
||||
.await;
|
||||
let err = imap.mailboxes().await.unwrap_err();
|
||||
let err = imap.folders().await.unwrap_err();
|
||||
assert!(
|
||||
matches!(&err, Error::Server(text) if text.contains("listing temporarily unavailable")),
|
||||
"{err}"
|
||||
@@ -294,8 +316,8 @@ async fn a_refused_list_keeps_the_server_words() {
|
||||
async fn a_refused_fetch_is_not_a_missing_uid() {
|
||||
let (mut imap, script) = logged_in(vec![
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 9 BODY.PEEK[]"),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 9 (UID FLAGS BODY.PEEK[])"),
|
||||
send("{tag} NO [UNAVAILABLE] Message store temporarily unavailable\n"),
|
||||
])
|
||||
.await;
|
||||
@@ -335,11 +357,13 @@ async fn a_bye_after_login_keeps_the_server_words() {
|
||||
script.await.unwrap().unwrap();
|
||||
|
||||
let (mut imap, script) = logged_in(vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* BYE Shutting down\n"),
|
||||
])
|
||||
.await;
|
||||
let err = imap.mailboxes().await.unwrap_err();
|
||||
let err = imap.folders().await.unwrap_err();
|
||||
assert!(
|
||||
matches!(&err, Error::Server(text) if text.contains("Shutting down")),
|
||||
"{err}"
|
||||
|
||||
@@ -27,8 +27,8 @@ async fn trace_logs_keep_no_password_token_or_mail() {
|
||||
expect(&format!("{{tag}} LOGIN \"ann@example.com\" \"{PASSWORD}\"")),
|
||||
send("{tag} OK in\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("{tag} OK done\n"),
|
||||
expect("{tag} UID FETCH 7 BODY.PEEK[]"),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK done\n"),
|
||||
expect("{tag} UID FETCH 7 (UID FLAGS BODY.PEEK[])"),
|
||||
send(&format!(
|
||||
"* 1 FETCH (UID 7 BODY[] {{{}}}\n{message})\n{{tag}} OK fetched\n",
|
||||
message.replace('\n', "\r\n").len()
|
||||
|
||||
@@ -71,7 +71,7 @@ fn a_whole_message_is_indexed_and_kept_by_its_hash() {
|
||||
assert_eq!(stored.message_id.as_deref(), Some("m1@example.com"));
|
||||
assert_eq!(stored.flags, ["\\Seen"]);
|
||||
assert_eq!(stored.attachments, Some(1));
|
||||
assert!(stored.whole);
|
||||
assert!(stored.body);
|
||||
assert_eq!(store.original(id).unwrap().as_deref(), Some(raw.as_slice()));
|
||||
assert_eq!(std::fs::read(original_path(dir.path(), &raw)).unwrap(), raw);
|
||||
assert_eq!(found(dir.path(), "numbers"), [id.0]);
|
||||
@@ -92,7 +92,7 @@ fn headers_first_then_the_whole_message() {
|
||||
.unwrap();
|
||||
let stored = store.get(id).unwrap();
|
||||
assert_eq!(stored.subject.as_deref(), Some("Café report"));
|
||||
assert_eq!((stored.whole, stored.attachments), (false, None));
|
||||
assert_eq!((stored.body, stored.attachments), (false, None));
|
||||
assert_eq!(store.original(id).unwrap(), None);
|
||||
assert_eq!(found(dir.path(), "report"), [id.0]);
|
||||
assert!(found(dir.path(), "numbers").is_empty());
|
||||
|
||||
@@ -0,0 +1,409 @@
|
||||
mod fake;
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use ely_mail_engine::{
|
||||
Account, AccountId, Error, Key, MailboxId, Role, Secret, Server, Store, sync,
|
||||
};
|
||||
use fake::{account, certs, expect, send, serve};
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
/// 2026-10-19 12:00 UTC; bodies from 21 July.
|
||||
const NOW: i64 = 1_792_411_200;
|
||||
const SINCE: &str = "{tag} UID SEARCH SINCE 21-Jul-2026";
|
||||
|
||||
const OLD: &str = "Subject: Old\nFrom: a@example.com\n\n";
|
||||
const NEW: &str = "Subject: New\nFrom: b@example.com\n\nfresh\n";
|
||||
|
||||
/// A FETCH line carrying `text` as a literal.
|
||||
fn literal(uid: u32, flags: &str, section: &str, text: &str) -> String {
|
||||
let crlf = text.replace('\n', "\r\n");
|
||||
format!(
|
||||
"* {uid} FETCH (UID {uid} FLAGS ({flags}) {section} {{{}}}\n{text})\n",
|
||||
crlf.len()
|
||||
)
|
||||
}
|
||||
|
||||
/// A one-part text message's structure, and the tag.
|
||||
fn plain(uid: u32) -> String {
|
||||
format!(
|
||||
"* {uid} FETCH (UID {uid} BODYSTRUCTURE (\"TEXT\" \"PLAIN\" NIL NIL NIL \"7BIT\" 7 1))\n\
|
||||
{{tag}} OK structure\n"
|
||||
)
|
||||
}
|
||||
|
||||
async fn server(steps: Vec<fake::Step>) -> (Account, JoinHandle<Result<(), String>>, fake::Certs) {
|
||||
let certs = certs();
|
||||
let mut script = vec![
|
||||
send("* OK ready\n"),
|
||||
expect("{tag} LOGIN \"ann@example.com\" \"pw\""),
|
||||
send("{tag} OK done\n"),
|
||||
];
|
||||
script.extend(steps);
|
||||
script.push(expect("{tag} LOGOUT"));
|
||||
script.push(send("* BYE\n{tag} OK bye\n"));
|
||||
let (incoming, task) = serve(&certs, ely_mail_engine::Security::Tls, script).await;
|
||||
let account = Account {
|
||||
incoming: Server { ..incoming },
|
||||
..account("ann@example.com")
|
||||
};
|
||||
(account, task, certs)
|
||||
}
|
||||
|
||||
async fn run(
|
||||
store: &Mutex<Store>,
|
||||
id: AccountId,
|
||||
steps: Vec<fake::Step>,
|
||||
) -> Result<sync::Report, Error> {
|
||||
let (account, task, certs) = server(steps).await;
|
||||
let secret = Secret::Password("pw".into());
|
||||
let done = sync::imap(&account, id, secret, &certs.trusted, store, NOW).await;
|
||||
if done.is_ok() {
|
||||
task.await.unwrap().unwrap();
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
fn first_sync() -> Vec<fake::Step> {
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST (\\HasNoChildren) \"/\" INBOX\n"),
|
||||
send("* LIST (\\HasNoChildren \\Sent) \"/\" Sent\n"),
|
||||
send("* LIST (\\Noselect) \"/\" Archive\n"),
|
||||
send("{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("* 2 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 1:* (UID FLAGS)"),
|
||||
send("* 1 FETCH (UID 1 FLAGS (\\Seen))\n* 2 FETCH (UID 2 FLAGS (\\Recent))\n"),
|
||||
send("{tag} OK flags\n"),
|
||||
expect(SINCE),
|
||||
send("* SEARCH 2\n{tag} OK found\n"),
|
||||
expect("{tag} UID FETCH 1 (UID FLAGS BODY.PEEK[HEADER])"),
|
||||
send(&literal(1, "\\Seen", "BODY[HEADER]", OLD)),
|
||||
send("{tag} OK headers\n"),
|
||||
expect("{tag} UID FETCH 2 (UID BODYSTRUCTURE)"),
|
||||
send(&plain(2)),
|
||||
expect("{tag} UID FETCH 2 (UID FLAGS BODY.PEEK[])"),
|
||||
send(&literal(2, "\\Recent", "BODY[]", NEW)),
|
||||
send("{tag} OK bodies\n"),
|
||||
expect("{tag} EXAMINE \"Sent\""),
|
||||
send("* 0 EXISTS\n* OK [UIDVALIDITY 3] v\n{tag} OK in\n"),
|
||||
]
|
||||
}
|
||||
|
||||
fn key(uidvalidity: u32, uid: u32) -> Key {
|
||||
Key::Imap { uidvalidity, uid }
|
||||
}
|
||||
|
||||
fn inbox(store: &Store, id: AccountId) -> MailboxId {
|
||||
store.folders(id).unwrap()[0].0
|
||||
}
|
||||
|
||||
fn opened() -> (tempfile::TempDir, Mutex<Store>, AccountId) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let store = Store::open(dir.path()).unwrap();
|
||||
let id = store.add_account(&account("ann@example.com")).unwrap();
|
||||
(dir, Mutex::new(store), id)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_first_sync_takes_headers_and_recent_bodies() {
|
||||
let (_dir, store, id) = opened();
|
||||
let report = run(&store, id, first_sync()).await.unwrap();
|
||||
assert_eq!(
|
||||
report,
|
||||
sync::Report {
|
||||
folders: 2,
|
||||
headers: 1,
|
||||
bodies: 1,
|
||||
removed: 0
|
||||
}
|
||||
);
|
||||
let store = store.lock().unwrap();
|
||||
let folders: Vec<_> = store
|
||||
.folders(id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|(_, name, role)| (name, role))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
folders,
|
||||
[
|
||||
("INBOX".to_owned(), Some(Role::Inbox)),
|
||||
("Sent".to_owned(), Some(Role::Sent))
|
||||
]
|
||||
);
|
||||
let inbox = inbox(&store, id);
|
||||
let old = store
|
||||
.get(store.find(inbox, &key(7, 1)).unwrap().unwrap())
|
||||
.unwrap();
|
||||
assert_eq!((old.body, old.flags), (false, vec!["\\Seen".to_owned()]));
|
||||
assert_eq!(old.subject.as_deref(), Some("Old"));
|
||||
let new = store.find(inbox, &key(7, 2)).unwrap().unwrap();
|
||||
assert_eq!(
|
||||
store.original(new).unwrap().unwrap(),
|
||||
NEW.replace('\n', "\r\n").as_bytes()
|
||||
);
|
||||
assert!(
|
||||
store.get(new).unwrap().flags.is_empty(),
|
||||
"\\Recent is not kept"
|
||||
);
|
||||
let synced = store.synced(id).unwrap();
|
||||
assert_eq!(
|
||||
(synced.at, synced.messages, synced.uncached),
|
||||
(Some(NOW), 2, 1)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn deletions_flags_and_dropped_folders_follow_the_server() {
|
||||
let (_dir, store, id) = opened();
|
||||
run(&store, id, first_sync()).await.unwrap();
|
||||
let report = run(
|
||||
&store,
|
||||
id,
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST () \"/\" INBOX\n{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 1:* (UID FLAGS)"),
|
||||
send("* 1 FETCH (UID 1 FLAGS (\\Seen \\Flagged))\n{tag} OK flags\n"),
|
||||
expect(SINCE),
|
||||
send("* SEARCH\n{tag} OK found\n"),
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.removed, 1);
|
||||
let store = store.lock().unwrap();
|
||||
assert_eq!(store.folders(id).unwrap().len(), 1);
|
||||
let inbox = inbox(&store, id);
|
||||
assert_eq!(store.find(inbox, &key(7, 2)).unwrap(), None);
|
||||
let old = store
|
||||
.get(store.find(inbox, &key(7, 1)).unwrap().unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(old.flags, ["\\Flagged", "\\Seen"]);
|
||||
assert_eq!(store.sizes().unwrap().originals, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_new_uidvalidity_rebuilds_the_mailbox() {
|
||||
let (_dir, store, id) = opened();
|
||||
run(&store, id, first_sync()).await.unwrap();
|
||||
run(
|
||||
&store,
|
||||
id,
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST () \"/\" INBOX\n{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("* 1 EXISTS\n* OK [UIDVALIDITY 8] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 1:* (UID FLAGS)"),
|
||||
send("* 1 FETCH (UID 1 FLAGS ())\n{tag} OK flags\n"),
|
||||
expect(SINCE),
|
||||
send("* SEARCH 1\n{tag} OK found\n"),
|
||||
expect("{tag} UID FETCH 1 (UID BODYSTRUCTURE)"),
|
||||
send(&plain(1)),
|
||||
expect("{tag} UID FETCH 1 (UID FLAGS BODY.PEEK[])"),
|
||||
send(&literal(1, "", "BODY[]", NEW)),
|
||||
send("{tag} OK bodies\n"),
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let store = store.lock().unwrap();
|
||||
let inbox = inbox(&store, id);
|
||||
assert_eq!(store.find(inbox, &key(7, 1)).unwrap(), None);
|
||||
let fresh = store.find(inbox, &key(8, 1)).unwrap().unwrap();
|
||||
assert_eq!(store.get(fresh).unwrap().subject.as_deref(), Some("New"));
|
||||
assert_eq!(store.synced(id).unwrap().messages, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_failed_sync_keeps_the_last_time() {
|
||||
let (_dir, store, id) = opened();
|
||||
let err = run(
|
||||
&store,
|
||||
id,
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("{tag} NO [UNAVAILABLE] Try again later\n"),
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(&err, Error::Server(text) if text.contains("Try again later")),
|
||||
"{err}"
|
||||
);
|
||||
assert_eq!(store.lock().unwrap().synced(id).unwrap().at, None);
|
||||
}
|
||||
|
||||
/// INBOX: UID 1 old, UID 2 recent; up to SEARCH.
|
||||
fn inbox_of_two() -> Vec<fake::Step> {
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST () \"/\" INBOX\n{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("* 2 EXISTS\n* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
expect("{tag} UID FETCH 1:* (UID FLAGS)"),
|
||||
send("* 1 FETCH (UID 1 FLAGS (\\Seen))\n* 2 FETCH (UID 2 FLAGS ())\n"),
|
||||
send("{tag} OK flags\n"),
|
||||
expect(SINCE),
|
||||
send("* SEARCH 2\n{tag} OK found\n"),
|
||||
]
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_examine_without_exists_does_not_delete_mail() {
|
||||
let (_dir, store, id) = opened();
|
||||
run(&store, id, first_sync()).await.unwrap();
|
||||
let err = run(
|
||||
&store,
|
||||
id,
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" *"),
|
||||
send("* LIST () \"/\" INBOX\n* LIST () \"/\" Sent\n{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"INBOX\""),
|
||||
send("* OK [UIDVALIDITY 7] v\n{tag} OK in\n"),
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(&err, Error::Protocol(text) if text.contains("EXISTS")),
|
||||
"{err}"
|
||||
);
|
||||
let store = store.lock().unwrap();
|
||||
let synced = store.synced(id).unwrap();
|
||||
assert_eq!((synced.at, synced.messages), (Some(NOW), 2));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_flags_do_not_make_a_body_fetch_fail() {
|
||||
let (_dir, store, id) = opened();
|
||||
let mut steps = first_sync();
|
||||
let body = steps
|
||||
.iter()
|
||||
.position(|step| matches!(step, fake::Step::Send(text) if text.contains("BODY[]")))
|
||||
.unwrap();
|
||||
steps.insert(body + 1, send("* 2 FETCH (UID 2 FLAGS (\\Seen))\n"));
|
||||
run(&store, id, steps).await.unwrap();
|
||||
let store = store.lock().unwrap();
|
||||
let new = store.find(inbox(&store, id), &key(7, 2)).unwrap().unwrap();
|
||||
let new = store.get(new).unwrap();
|
||||
assert_eq!((new.body, new.flags), (true, vec!["\\Seen".to_owned()]));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn inbox_casing_does_not_destroy_cached_mail() {
|
||||
let (_dir, store, id) = opened();
|
||||
run(&store, id, first_sync()).await.unwrap();
|
||||
let mut store = store.lock().unwrap();
|
||||
let kept = store
|
||||
.set_folders(id, &[("inbox", Some(Role::Inbox))])
|
||||
.unwrap();
|
||||
let folders = store.folders(id).unwrap();
|
||||
assert_eq!(folders.len(), 1);
|
||||
assert_eq!((kept[0], folders[0].1.as_str()), (folders[0].0, "INBOX"));
|
||||
let new = store.find(kept[0], &key(7, 2)).unwrap().unwrap();
|
||||
assert_eq!(
|
||||
store.original(new).unwrap().unwrap(),
|
||||
NEW.replace('\n', "\r\n").as_bytes()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn roles_are_asked_for_when_the_server_can_list_them() {
|
||||
let (_dir, store, id) = opened();
|
||||
run(
|
||||
&store,
|
||||
id,
|
||||
vec![
|
||||
expect("{tag} CAPABILITY"),
|
||||
send("* CAPABILITY IMAP4rev1 LIST-EXTENDED SPECIAL-USE\n{tag} OK caps\n"),
|
||||
expect("{tag} LIST \"\" * RETURN (SPECIAL-USE)"),
|
||||
send("* LIST (\\Sent) \"/\" Outbox\n{tag} OK listed\n"),
|
||||
expect("{tag} EXAMINE \"Outbox\""),
|
||||
send("* 0 EXISTS\n* OK [UIDVALIDITY 3] v\n{tag} OK in\n"),
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let folders = store.lock().unwrap().folders(id).unwrap();
|
||||
assert_eq!(folders[0].2, Some(Role::Sent));
|
||||
}
|
||||
|
||||
/// One FETCH line carrying each section as a literal.
|
||||
fn sections(uid: u32, parts: &[(&str, &str)]) -> String {
|
||||
let mut line = format!("* {uid} FETCH (UID {uid} FLAGS ()");
|
||||
for (name, text) in parts {
|
||||
let crlf = text.replace('\n', "\r\n");
|
||||
line.push_str(&format!(" BODY[{name}] {{{}}}\n{text}", crlf.len()));
|
||||
}
|
||||
line + ")\n{tag} OK text\n"
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_recent_body_sync_leaves_attachments_on_the_server() {
|
||||
let (_dir, store, id) = opened();
|
||||
let header = "Subject: Report\nFrom: b@example.com\nMIME-Version: 1.0\n\
|
||||
Content-Type: multipart/mixed; boundary=\"b1\"\n\n";
|
||||
let structure = "* 2 FETCH (UID 2 BODYSTRUCTURE ((\"TEXT\" \"PLAIN\" (\"CHARSET\" \"us-ascii\") \
|
||||
NIL NIL \"7BIT\" 6 1)(\"APPLICATION\" \"PDF\" (\"NAME\" \"report.pdf\") NIL NIL \
|
||||
\"BASE64\" 12 NIL (\"ATTACHMENT\" (\"FILENAME\" \"report.pdf\")) NIL NIL) \
|
||||
\"MIXED\" (\"BOUNDARY\" \"b1\") NIL NIL NIL))\n{tag} OK structure\n";
|
||||
let mut steps = inbox_of_two();
|
||||
steps.extend([
|
||||
expect("{tag} UID FETCH 1 (UID FLAGS BODY.PEEK[HEADER])"),
|
||||
send(&literal(1, "\\Seen", "BODY[HEADER]", OLD)),
|
||||
send("{tag} OK headers\n"),
|
||||
expect("{tag} UID FETCH 2 (UID BODYSTRUCTURE)"),
|
||||
send(structure),
|
||||
expect(
|
||||
"{tag} UID FETCH 2 (UID FLAGS BODY.PEEK[HEADER] BODY.PEEK[1.MIME] BODY.PEEK[1] \
|
||||
BODY.PEEK[2.MIME])",
|
||||
),
|
||||
send(§ions(
|
||||
2,
|
||||
&[
|
||||
("HEADER", header),
|
||||
("1.MIME", "Content-Type: text/plain; charset=us-ascii\n\n"),
|
||||
("1", "hello\n"),
|
||||
(
|
||||
"2.MIME",
|
||||
"Content-Type: application/pdf; name=\"report.pdf\"\n\
|
||||
Content-Disposition: attachment; filename=\"report.pdf\"\n\
|
||||
Content-Transfer-Encoding: base64\n\n",
|
||||
),
|
||||
],
|
||||
)),
|
||||
]);
|
||||
run(&store, id, steps).await.unwrap();
|
||||
let store = store.lock().unwrap();
|
||||
let report = store.find(inbox(&store, id), &key(7, 2)).unwrap().unwrap();
|
||||
let stored = store.get(report).unwrap();
|
||||
assert_eq!(
|
||||
(stored.body, stored.missing.clone()),
|
||||
(true, vec!["2".to_owned()])
|
||||
);
|
||||
assert_eq!(stored.attachments, Some(1));
|
||||
let mail = ely_mail_engine::read(&store.original(report).unwrap().unwrap()).unwrap();
|
||||
assert_eq!(mail.text.as_deref().map(str::trim_end), Some("hello"));
|
||||
assert_eq!(mail.attachments[0].name.as_deref(), Some("report.pdf"));
|
||||
assert!(mail.attachments[0].bytes.is_empty());
|
||||
assert_eq!(store.synced(id).unwrap().partial, 1);
|
||||
}
|
||||
@@ -111,6 +111,34 @@ pub(super) const EN: &[(&str, &str)] = &[
|
||||
("mail.remove.go", "Remove Account"),
|
||||
("mail.remove.done", "{address} removed."),
|
||||
("mail.remove.failed", "The account was not removed: {error}"),
|
||||
("mail.sync.last", "Last synced"),
|
||||
("mail.sync.running", "Syncing…"),
|
||||
("mail.sync.never", "Not yet"),
|
||||
("mail.sync.range", "Downloaded"),
|
||||
(
|
||||
"mail.sync.range_value",
|
||||
"Headers of all mail; text from the last {days} days; attachments when asked",
|
||||
),
|
||||
("mail.sync.uncached", "Not downloaded"),
|
||||
(
|
||||
"mail.sync.uncached_value",
|
||||
"{n} messages have only their headers here",
|
||||
),
|
||||
(
|
||||
"mail.sync.uncached_value.one",
|
||||
"1 message has only its headers here",
|
||||
),
|
||||
("mail.sync.partial", "Attachments"),
|
||||
(
|
||||
"mail.sync.partial_value",
|
||||
"{n} messages have attachments not downloaded",
|
||||
),
|
||||
(
|
||||
"mail.sync.partial_value.one",
|
||||
"1 message has attachments not downloaded",
|
||||
),
|
||||
("mail.sync.failed", "The last sync failed: {error}"),
|
||||
("mail.sync.unread", "The sync state could not be read: {error}"),
|
||||
];
|
||||
|
||||
pub(super) const ZH_CN: &[(&str, &str)] = &[
|
||||
@@ -212,4 +240,17 @@ pub(super) const ZH_CN: &[(&str, &str)] = &[
|
||||
("mail.remove.go", "删除账号"),
|
||||
("mail.remove.done", "已删除 {address}。"),
|
||||
("mail.remove.failed", "账号没有删除:{error}"),
|
||||
("mail.sync.last", "上次同步"),
|
||||
("mail.sync.running", "正在同步…"),
|
||||
("mail.sync.never", "尚未同步"),
|
||||
("mail.sync.range", "已下载"),
|
||||
("mail.sync.range_value", "全部邮件的邮件头;最近 {days} 天的正文;附件按需下载"),
|
||||
("mail.sync.uncached", "未下载"),
|
||||
("mail.sync.uncached_value", "{n} 封邮件只有邮件头"),
|
||||
("mail.sync.uncached_value.one", "1 封邮件只有邮件头"),
|
||||
("mail.sync.partial", "附件"),
|
||||
("mail.sync.partial_value", "{n} 封邮件的附件未下载"),
|
||||
("mail.sync.partial_value.one", "1 封邮件的附件未下载"),
|
||||
("mail.sync.failed", "上次同步失败:{error}"),
|
||||
("mail.sync.unread", "读不出同步状态:{error}"),
|
||||
];
|
||||
Reference in new issue
Block a user