2022-06-24 08:27:26 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2022-05-19 10:14:06 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use k2v_client::K2vClient;
|
|
|
|
use rusoto_s3::S3Client;
|
2022-05-19 10:10:48 +00:00
|
|
|
|
|
|
|
use crate::bayou::Bayou;
|
2022-05-19 10:14:06 +00:00
|
|
|
use crate::cryptoblob::Key;
|
2022-05-19 10:10:48 +00:00
|
|
|
use crate::login::Credentials;
|
2022-06-15 16:40:39 +00:00
|
|
|
use crate::mail_ident::*;
|
2022-05-19 10:10:48 +00:00
|
|
|
use crate::uidindex::*;
|
|
|
|
|
2022-06-24 08:27:26 +00:00
|
|
|
pub struct Summary<'a> {
|
2022-06-14 08:19:24 +00:00
|
|
|
pub validity: ImapUidvalidity,
|
|
|
|
pub next: ImapUid,
|
2022-06-24 08:27:26 +00:00
|
|
|
pub exists: u32,
|
|
|
|
pub recent: u32,
|
2022-06-27 09:40:45 +00:00
|
|
|
pub flags: FlagIter<'a>,
|
2022-06-24 08:27:26 +00:00
|
|
|
pub unseen: Option<&'a ImapUid>,
|
2022-06-13 16:01:07 +00:00
|
|
|
}
|
2022-06-24 08:27:26 +00:00
|
|
|
impl std::fmt::Display for Summary<'_> {
|
2022-06-13 16:01:07 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2022-06-14 08:19:24 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"uidvalidity: {}, uidnext: {}, exists: {}",
|
|
|
|
self.validity, self.next, self.exists
|
|
|
|
)
|
2022-06-13 16:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 08:27:26 +00:00
|
|
|
|
|
|
|
// Non standard but common flags:
|
|
|
|
// https://www.iana.org/assignments/imap-jmap-keywords/imap-jmap-keywords.xhtml
|
2022-05-19 10:10:48 +00:00
|
|
|
pub struct Mailbox {
|
|
|
|
bucket: String,
|
2022-06-13 10:25:19 +00:00
|
|
|
pub name: String,
|
2022-05-19 10:10:48 +00:00
|
|
|
key: Key,
|
|
|
|
|
|
|
|
k2v: K2vClient,
|
|
|
|
s3: S3Client,
|
|
|
|
|
|
|
|
uid_index: Bayou<UidIndex>,
|
|
|
|
}
|
|
|
|
|
2022-06-24 08:27:26 +00:00
|
|
|
// IDEA: We store a specific flag named $unseen.
|
|
|
|
// If it is not present, we add the virtual flag \Seen
|
2022-05-19 10:10:48 +00:00
|
|
|
impl Mailbox {
|
2022-06-07 11:46:13 +00:00
|
|
|
pub fn new(creds: &Credentials, name: String) -> Result<Self> {
|
2022-05-19 12:33:49 +00:00
|
|
|
let uid_index = Bayou::<UidIndex>::new(creds, name.clone())?;
|
2022-05-19 10:10:48 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
2022-05-19 12:33:49 +00:00
|
|
|
bucket: creds.bucket().to_string(),
|
2022-05-19 10:10:48 +00:00
|
|
|
name,
|
2022-05-19 12:33:49 +00:00
|
|
|
key: creds.keys.master.clone(),
|
|
|
|
k2v: creds.k2v_client()?,
|
|
|
|
s3: creds.s3_client()?,
|
2022-05-19 10:10:48 +00:00
|
|
|
uid_index,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
pub async fn summary(&mut self) -> Result<Summary> {
|
|
|
|
self.uid_index.sync().await?;
|
|
|
|
let state = self.uid_index.state();
|
|
|
|
|
2022-06-24 08:27:26 +00:00
|
|
|
let unseen = state.idx_by_flag.get(&"$unseen".to_string()).and_then(|os| os.get_min());
|
|
|
|
let recent = state.idx_by_flag.get(&"\\Recent".to_string()).map(|os| os.len()).unwrap_or(0);
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
return Ok(Summary {
|
2022-06-14 08:19:24 +00:00
|
|
|
validity: state.uidvalidity,
|
|
|
|
next: state.uidnext,
|
2022-06-24 08:27:26 +00:00
|
|
|
exists: u32::try_from(state.idx_by_uid.len())?,
|
|
|
|
recent: u32::try_from(recent)?,
|
2022-06-27 09:40:45 +00:00
|
|
|
flags: state.idx_by_flag.flags(),
|
2022-06-24 08:27:26 +00:00
|
|
|
unseen,
|
2022-06-14 08:19:24 +00:00
|
|
|
});
|
2022-06-13 16:01:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 10:10:48 +00:00
|
|
|
pub async fn test(&mut self) -> Result<()> {
|
|
|
|
self.uid_index.sync().await?;
|
|
|
|
|
|
|
|
dump(&self.uid_index);
|
|
|
|
|
2022-05-19 10:14:06 +00:00
|
|
|
let add_mail_op = self
|
|
|
|
.uid_index
|
2022-05-19 10:10:48 +00:00
|
|
|
.state()
|
2022-06-15 16:40:39 +00:00
|
|
|
.op_mail_add(gen_ident(), vec!["\\Unseen".into()]);
|
2022-05-19 10:10:48 +00:00
|
|
|
self.uid_index.push(add_mail_op).await?;
|
|
|
|
|
|
|
|
dump(&self.uid_index);
|
|
|
|
|
2022-06-14 16:06:42 +00:00
|
|
|
if self.uid_index.state().idx_by_uid.len() > 6 {
|
2022-05-19 10:10:48 +00:00
|
|
|
for i in 0..2 {
|
2022-06-15 16:40:39 +00:00
|
|
|
let (_, ident) = self
|
2022-05-19 10:14:06 +00:00
|
|
|
.uid_index
|
2022-05-19 10:10:48 +00:00
|
|
|
.state()
|
2022-06-14 16:06:42 +00:00
|
|
|
.idx_by_uid
|
2022-05-19 10:10:48 +00:00
|
|
|
.iter()
|
|
|
|
.skip(3 + i)
|
|
|
|
.next()
|
|
|
|
.unwrap();
|
2022-06-15 16:40:39 +00:00
|
|
|
let del_mail_op = self.uid_index.state().op_mail_del(*ident);
|
2022-05-19 10:10:48 +00:00
|
|
|
self.uid_index.push(del_mail_op).await?;
|
|
|
|
|
|
|
|
dump(&self.uid_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dump(uid_index: &Bayou<UidIndex>) {
|
|
|
|
let s = uid_index.state();
|
|
|
|
println!("---- MAILBOX STATE ----");
|
|
|
|
println!("UIDVALIDITY {}", s.uidvalidity);
|
|
|
|
println!("UIDNEXT {}", s.uidnext);
|
|
|
|
println!("INTERNALSEQ {}", s.internalseq);
|
2022-06-14 16:06:42 +00:00
|
|
|
for (uid, ident) in s.idx_by_uid.iter() {
|
2022-05-19 10:10:48 +00:00
|
|
|
println!(
|
|
|
|
"{} {} {}",
|
|
|
|
uid,
|
2022-06-14 16:06:42 +00:00
|
|
|
hex::encode(ident.0),
|
2022-06-23 12:41:10 +00:00
|
|
|
s.table.get(ident).cloned().unwrap().1.join(", ")
|
2022-05-19 10:10:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|