2022-06-29 11:16:58 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use k2v_client::K2vClient;
|
|
|
|
use rusoto_s3::S3Client;
|
2022-06-29 13:39:54 +00:00
|
|
|
use tokio::sync::RwLock;
|
2022-06-29 11:16:58 +00:00
|
|
|
|
|
|
|
use crate::bayou::Bayou;
|
|
|
|
use crate::cryptoblob::Key;
|
|
|
|
use crate::login::Credentials;
|
|
|
|
use crate::mail::uidindex::*;
|
2022-06-29 13:52:09 +00:00
|
|
|
use crate::mail::unique_ident::*;
|
2022-06-29 11:16:58 +00:00
|
|
|
use crate::mail::IMF;
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
pub struct Mailbox {
|
|
|
|
id: UniqueIdent,
|
|
|
|
mbox: RwLock<MailboxInternal>,
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
2022-06-29 11:41:05 +00:00
|
|
|
|
2022-06-29 11:16:58 +00:00
|
|
|
impl Mailbox {
|
2022-06-29 13:52:09 +00:00
|
|
|
pub(super) async fn open(creds: &Credentials, id: UniqueIdent) -> Result<Self> {
|
|
|
|
let index_path = format!("index/{}", id);
|
|
|
|
let mail_path = format!("mail/{}", id);
|
2022-06-29 11:16:58 +00:00
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
let mut uid_index = Bayou::<UidIndex>::new(creds, index_path)?;
|
|
|
|
uid_index.sync().await?;
|
|
|
|
|
2022-06-29 13:52:09 +00:00
|
|
|
let mbox = RwLock::new(MailboxInternal {
|
|
|
|
id,
|
2022-06-29 11:16:58 +00:00
|
|
|
bucket: creds.bucket().to_string(),
|
2022-06-29 13:52:09 +00:00
|
|
|
encryption_key: creds.keys.master.clone(),
|
2022-06-29 11:16:58 +00:00
|
|
|
k2v: creds.k2v_client()?,
|
|
|
|
s3: creds.s3_client()?,
|
|
|
|
uid_index,
|
|
|
|
mail_path,
|
2022-06-29 13:52:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(Self { id, mbox })
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
/// Get a clone of the current UID Index of this mailbox
|
|
|
|
/// (cloning is cheap so don't hesitate to use this)
|
|
|
|
pub async fn current_uid_index(&self) -> UidIndex {
|
2022-06-29 13:52:09 +00:00
|
|
|
self.mbox.read().await.uid_index.state().clone()
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:41:05 +00:00
|
|
|
/// Insert an email in the mailbox
|
2022-06-29 13:39:54 +00:00
|
|
|
pub async fn append<'a>(&self, _msg: IMF<'a>) -> Result<()> {
|
2022-06-29 11:41:05 +00:00
|
|
|
unimplemented!()
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:41:05 +00:00
|
|
|
/// Copy an email from an other Mailbox to this mailbox
|
|
|
|
/// (use this when possible, as it allows for a certain number of storage optimizations)
|
2022-06-29 13:39:54 +00:00
|
|
|
pub async fn copy(&self, _from: &Mailbox, _uid: ImapUid) -> Result<()> {
|
2022-06-29 11:41:05 +00:00
|
|
|
unimplemented!()
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:41:05 +00:00
|
|
|
/// Delete all emails with the \Delete flag in the mailbox
|
|
|
|
/// Can be called by CLOSE and EXPUNGE
|
|
|
|
/// @FIXME do we want to implement this feature or a simpler "delete" command
|
|
|
|
/// The controller could then "fetch \Delete" and call delete on each email?
|
2022-06-29 13:39:54 +00:00
|
|
|
pub async fn expunge(&self) -> Result<()> {
|
2022-06-29 11:41:05 +00:00
|
|
|
unimplemented!()
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:41:05 +00:00
|
|
|
/// Update flags of a range of emails
|
2022-06-29 13:39:54 +00:00
|
|
|
pub async fn store(&self) -> Result<()> {
|
2022-06-29 11:41:05 +00:00
|
|
|
unimplemented!()
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
pub async fn fetch(&self) -> Result<()> {
|
2022-06-29 11:41:05 +00:00
|
|
|
unimplemented!()
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
2022-06-29 13:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
|
|
|
|
// Non standard but common flags:
|
|
|
|
// https://www.iana.org/assignments/imap-jmap-keywords/imap-jmap-keywords.xhtml
|
|
|
|
struct MailboxInternal {
|
2022-06-29 13:52:09 +00:00
|
|
|
id: UniqueIdent,
|
2022-06-29 13:39:54 +00:00
|
|
|
bucket: String,
|
2022-06-29 13:52:09 +00:00
|
|
|
mail_path: String,
|
|
|
|
encryption_key: Key,
|
2022-06-29 11:16:58 +00:00
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
k2v: K2vClient,
|
|
|
|
s3: S3Client,
|
|
|
|
|
|
|
|
uid_index: Bayou<UidIndex>,
|
|
|
|
}
|
2022-06-29 11:41:05 +00:00
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
impl MailboxInternal {
|
2022-06-29 11:16:58 +00:00
|
|
|
pub async fn test(&mut self) -> Result<()> {
|
|
|
|
self.uid_index.sync().await?;
|
|
|
|
|
|
|
|
dump(&self.uid_index);
|
|
|
|
|
|
|
|
let add_mail_op = self
|
|
|
|
.uid_index
|
|
|
|
.state()
|
|
|
|
.op_mail_add(gen_ident(), vec!["\\Unseen".into()]);
|
|
|
|
self.uid_index.push(add_mail_op).await?;
|
|
|
|
|
|
|
|
dump(&self.uid_index);
|
|
|
|
|
|
|
|
if self.uid_index.state().idx_by_uid.len() > 6 {
|
|
|
|
for i in 0..2 {
|
|
|
|
let (_, ident) = self
|
|
|
|
.uid_index
|
|
|
|
.state()
|
|
|
|
.idx_by_uid
|
|
|
|
.iter()
|
|
|
|
.skip(3 + i)
|
|
|
|
.next()
|
|
|
|
.unwrap();
|
|
|
|
let del_mail_op = self.uid_index.state().op_mail_del(*ident);
|
|
|
|
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);
|
|
|
|
for (uid, ident) in s.idx_by_uid.iter() {
|
|
|
|
println!(
|
|
|
|
"{} {} {}",
|
|
|
|
uid,
|
|
|
|
hex::encode(ident.0),
|
|
|
|
s.table.get(ident).cloned().unwrap().1.join(", ")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|