2022-06-30 12:17:54 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2022-06-29 13:39:54 +00:00
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
2022-06-30 12:17:54 +00:00
|
|
|
use anyhow::{anyhow, bail, Result};
|
2022-06-29 13:39:54 +00:00
|
|
|
use lazy_static::lazy_static;
|
2022-06-30 12:17:54 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-06-30 12:02:57 +00:00
|
|
|
use tokio::sync::watch;
|
2022-06-29 11:16:58 +00:00
|
|
|
|
2022-06-30 12:17:54 +00:00
|
|
|
use crate::cryptoblob::{open_deserialize, seal_serialize};
|
2023-11-23 14:04:47 +00:00
|
|
|
use crate::login::Credentials;
|
2022-06-30 15:40:59 +00:00
|
|
|
use crate::mail::incoming::incoming_mail_watch_process;
|
2022-06-29 11:16:58 +00:00
|
|
|
use crate::mail::mailbox::Mailbox;
|
2022-06-30 11:33:58 +00:00
|
|
|
use crate::mail::uidindex::ImapUidvalidity;
|
2022-06-30 12:17:54 +00:00
|
|
|
use crate::mail::unique_ident::{gen_ident, UniqueIdent};
|
2023-11-02 11:18:43 +00:00
|
|
|
use crate::storage;
|
2023-11-15 14:56:43 +00:00
|
|
|
use crate::timestamp::now_msec;
|
2022-06-30 11:33:58 +00:00
|
|
|
|
2022-07-12 11:19:27 +00:00
|
|
|
pub const MAILBOX_HIERARCHY_DELIMITER: char = '.';
|
2022-06-30 11:33:58 +00:00
|
|
|
|
|
|
|
/// INBOX is the only mailbox that must always exist.
|
|
|
|
/// It is created automatically when the account is created.
|
|
|
|
/// IMAP allows the user to rename INBOX to something else,
|
|
|
|
/// in this case all messages from INBOX are moved to a mailbox
|
|
|
|
/// with the new name and the INBOX mailbox still exists and is empty.
|
|
|
|
/// In our implementation, we indeed move the underlying mailbox
|
|
|
|
/// to the new name (i.e. the new name has the same id as the previous
|
|
|
|
/// INBOX), and we create a new empty mailbox for INBOX.
|
2022-07-12 11:19:27 +00:00
|
|
|
pub const INBOX: &str = "INBOX";
|
2022-06-29 11:16:58 +00:00
|
|
|
|
2024-01-19 15:47:20 +00:00
|
|
|
/// For convenience purpose, we also create some special mailbox
|
|
|
|
/// that are described in RFC6154 SPECIAL-USE
|
|
|
|
/// @FIXME maybe it should be a configuration parameter
|
|
|
|
/// @FIXME maybe we should have a per-mailbox flag mechanism, either an enum or a string, so we
|
|
|
|
/// track which mailbox is used for what.
|
|
|
|
/// @FIXME Junk could be useful but we don't have any antispam solution yet so...
|
|
|
|
/// @FIXME IMAP supports virtual mailbox. \All or \Flagged are intended to be virtual mailboxes.
|
|
|
|
/// \Trash might be one, or not one. I don't know what we should do there.
|
|
|
|
pub const DRAFTS: &str = "Drafts";
|
|
|
|
pub const ARCHIVE: &str = "Archive";
|
|
|
|
pub const SENT: &str = "Sent";
|
|
|
|
pub const TRASH: &str = "Trash";
|
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
const MAILBOX_LIST_PK: &str = "mailboxes";
|
|
|
|
const MAILBOX_LIST_SK: &str = "list";
|
|
|
|
|
2023-11-01 16:18:58 +00:00
|
|
|
pub struct User {
|
2022-06-29 11:16:58 +00:00
|
|
|
pub username: String,
|
2023-11-01 16:18:58 +00:00
|
|
|
pub creds: Credentials,
|
2023-12-18 16:09:44 +00:00
|
|
|
pub storage: storage::Store,
|
2022-06-30 12:02:57 +00:00
|
|
|
pub mailboxes: std::sync::Mutex<HashMap<UniqueIdent, Weak<Mailbox>>>,
|
|
|
|
|
|
|
|
tx_inbox_id: watch::Sender<Option<(UniqueIdent, ImapUidvalidity)>>,
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
2022-06-30 12:02:57 +00:00
|
|
|
pub async fn new(username: String, creds: Credentials) -> Result<Arc<Self>> {
|
2023-12-18 16:09:44 +00:00
|
|
|
let cache_key = (username.clone(), creds.storage.unique());
|
2022-06-30 12:02:57 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let cache = USER_CACHE.lock().unwrap();
|
|
|
|
if let Some(u) = cache.get(&cache_key).and_then(Weak::upgrade) {
|
|
|
|
return Ok(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let user = Self::open(username, creds).await?;
|
|
|
|
|
|
|
|
let mut cache = USER_CACHE.lock().unwrap();
|
|
|
|
if let Some(concurrent_user) = cache.get(&cache_key).and_then(Weak::upgrade) {
|
|
|
|
drop(user);
|
|
|
|
Ok(concurrent_user)
|
|
|
|
} else {
|
|
|
|
cache.insert(cache_key, Arc::downgrade(&user));
|
|
|
|
Ok(user)
|
|
|
|
}
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:41:05 +00:00
|
|
|
/// Lists user's available mailboxes
|
2022-06-30 11:33:58 +00:00
|
|
|
pub async fn list_mailboxes(&self) -> Result<Vec<String>> {
|
|
|
|
let (list, _ct) = self.load_mailbox_list().await?;
|
2022-07-12 13:23:30 +00:00
|
|
|
Ok(list.existing_mailbox_names())
|
2022-06-29 11:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Opens an existing mailbox given its IMAP name.
|
2022-06-29 13:39:54 +00:00
|
|
|
pub async fn open_mailbox(&self, name: &str) -> Result<Option<Arc<Mailbox>>> {
|
2022-06-30 14:04:38 +00:00
|
|
|
let (mut list, ct) = self.load_mailbox_list().await?;
|
2024-01-08 06:52:45 +00:00
|
|
|
|
|
|
|
//@FIXME it could be a trace or an opentelemtry trace thing.
|
|
|
|
// Be careful to not leak sensible data
|
|
|
|
/*
|
2022-07-12 13:59:13 +00:00
|
|
|
eprintln!("List of mailboxes:");
|
|
|
|
for ent in list.0.iter() {
|
|
|
|
eprintln!(" - {:?}", ent);
|
|
|
|
}
|
2024-01-08 06:52:45 +00:00
|
|
|
*/
|
2022-07-12 13:23:30 +00:00
|
|
|
|
|
|
|
if let Some((uidvalidity, Some(mbid))) = list.get_mailbox(name) {
|
2022-07-21 10:50:44 +00:00
|
|
|
let mb = self.open_mailbox_by_id(mbid, uidvalidity).await?;
|
|
|
|
let mb_uidvalidity = mb.current_uid_index().await.uidvalidity;
|
|
|
|
if mb_uidvalidity > uidvalidity {
|
|
|
|
list.update_uidvalidity(name, mb_uidvalidity);
|
2023-12-18 16:09:44 +00:00
|
|
|
self.save_mailbox_list(&list, ct).await?;
|
2022-06-30 14:04:38 +00:00
|
|
|
}
|
2022-07-21 10:50:44 +00:00
|
|
|
Ok(Some(mb))
|
2022-07-12 13:23:30 +00:00
|
|
|
} else {
|
2022-07-21 10:50:44 +00:00
|
|
|
Ok(None)
|
2022-06-30 11:33:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 13:59:13 +00:00
|
|
|
/// Check whether mailbox exists
|
|
|
|
pub async fn has_mailbox(&self, name: &str) -> Result<bool> {
|
|
|
|
let (list, _ct) = self.load_mailbox_list().await?;
|
|
|
|
Ok(list.has_mailbox(name))
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:33:58 +00:00
|
|
|
/// Creates a new mailbox in the user's IMAP namespace.
|
2022-06-30 12:17:54 +00:00
|
|
|
pub async fn create_mailbox(&self, name: &str) -> Result<()> {
|
2022-07-12 13:23:30 +00:00
|
|
|
if name.ends_with(MAILBOX_HIERARCHY_DELIMITER) {
|
|
|
|
bail!("Invalid mailbox name: {}", name);
|
|
|
|
}
|
|
|
|
|
2022-06-30 12:17:54 +00:00
|
|
|
let (mut list, ct) = self.load_mailbox_list().await?;
|
2022-07-12 13:23:30 +00:00
|
|
|
match list.create_mailbox(name) {
|
|
|
|
CreatedMailbox::Created(_, _) => {
|
2023-12-18 16:09:44 +00:00
|
|
|
self.save_mailbox_list(&list, ct).await?;
|
2022-07-12 13:23:30 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-06-30 12:17:54 +00:00
|
|
|
CreatedMailbox::Existed(_, _) => Err(anyhow!("Mailbox {} already exists", name)),
|
|
|
|
}
|
2022-06-30 11:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Deletes a mailbox in the user's IMAP namespace.
|
2022-07-12 13:23:30 +00:00
|
|
|
pub async fn delete_mailbox(&self, name: &str) -> Result<()> {
|
|
|
|
if name == INBOX {
|
|
|
|
bail!("Cannot delete INBOX");
|
|
|
|
}
|
|
|
|
|
|
|
|
let (mut list, ct) = self.load_mailbox_list().await?;
|
|
|
|
if list.has_mailbox(name) {
|
2024-01-19 15:47:20 +00:00
|
|
|
//@TODO: actually delete mailbox contents
|
2022-07-12 13:23:30 +00:00
|
|
|
list.set_mailbox(name, None);
|
2023-12-18 16:09:44 +00:00
|
|
|
self.save_mailbox_list(&list, ct).await?;
|
2022-07-12 13:23:30 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
bail!("Mailbox {} does not exist", name);
|
|
|
|
}
|
2022-06-30 11:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Renames a mailbox in the user's IMAP namespace.
|
2022-07-12 11:19:27 +00:00
|
|
|
pub async fn rename_mailbox(&self, old_name: &str, new_name: &str) -> Result<()> {
|
2022-07-12 13:23:30 +00:00
|
|
|
let (mut list, ct) = self.load_mailbox_list().await?;
|
|
|
|
|
|
|
|
if old_name.ends_with(MAILBOX_HIERARCHY_DELIMITER) {
|
|
|
|
bail!("Invalid mailbox name: {}", old_name);
|
|
|
|
}
|
|
|
|
if new_name.ends_with(MAILBOX_HIERARCHY_DELIMITER) {
|
|
|
|
bail!("Invalid mailbox name: {}", new_name);
|
|
|
|
}
|
|
|
|
|
2022-07-12 11:19:27 +00:00
|
|
|
if old_name == INBOX {
|
2022-07-12 13:23:30 +00:00
|
|
|
list.rename_mailbox(old_name, new_name)?;
|
|
|
|
if !self.ensure_inbox_exists(&mut list, &ct).await? {
|
2023-12-18 16:09:44 +00:00
|
|
|
self.save_mailbox_list(&list, ct).await?;
|
2022-07-12 13:23:30 +00:00
|
|
|
}
|
2022-07-12 11:19:27 +00:00
|
|
|
} else {
|
2022-07-12 13:23:30 +00:00
|
|
|
let names = list.existing_mailbox_names();
|
|
|
|
|
|
|
|
let old_name_w_delim = format!("{}{}", old_name, MAILBOX_HIERARCHY_DELIMITER);
|
|
|
|
let new_name_w_delim = format!("{}{}", new_name, MAILBOX_HIERARCHY_DELIMITER);
|
|
|
|
|
|
|
|
if names
|
|
|
|
.iter()
|
|
|
|
.any(|x| x == new_name || x.starts_with(&new_name_w_delim))
|
|
|
|
{
|
|
|
|
bail!("Mailbox {} already exists", new_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for name in names.iter() {
|
|
|
|
if name == old_name {
|
|
|
|
list.rename_mailbox(name, new_name)?;
|
|
|
|
} else if let Some(tail) = name.strip_prefix(&old_name_w_delim) {
|
|
|
|
let nnew = format!("{}{}", new_name_w_delim, tail);
|
|
|
|
list.rename_mailbox(name, &nnew)?;
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 09:46:13 +00:00
|
|
|
|
2023-12-18 16:09:44 +00:00
|
|
|
self.save_mailbox_list(&list, ct).await?;
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
2022-07-12 13:23:30 +00:00
|
|
|
Ok(())
|
2022-06-30 11:33:58 +00:00
|
|
|
}
|
2022-06-29 13:52:09 +00:00
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
// ---- Internal user & mailbox management ----
|
2022-06-30 12:17:54 +00:00
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
async fn open(username: String, creds: Credentials) -> Result<Arc<Self>> {
|
2023-12-21 20:54:36 +00:00
|
|
|
let storage = creds.storage.build().await?;
|
2022-06-30 11:33:58 +00:00
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
let (tx_inbox_id, rx_inbox_id) = watch::channel(None);
|
2022-06-29 13:52:09 +00:00
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
let user = Arc::new(Self {
|
|
|
|
username,
|
2022-06-30 15:40:59 +00:00
|
|
|
creds: creds.clone(),
|
2023-12-18 16:09:44 +00:00
|
|
|
storage,
|
2022-06-30 12:02:57 +00:00
|
|
|
tx_inbox_id,
|
|
|
|
mailboxes: std::sync::Mutex::new(HashMap::new()),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ensure INBOX exists (done inside load_mailbox_list)
|
|
|
|
user.load_mailbox_list().await?;
|
|
|
|
|
2022-06-30 15:40:59 +00:00
|
|
|
tokio::spawn(incoming_mail_watch_process(
|
|
|
|
Arc::downgrade(&user),
|
|
|
|
user.creds.clone(),
|
|
|
|
rx_inbox_id,
|
|
|
|
));
|
2022-06-30 14:18:08 +00:00
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:40:59 +00:00
|
|
|
pub(super) async fn open_mailbox_by_id(
|
2022-06-30 12:17:54 +00:00
|
|
|
&self,
|
|
|
|
id: UniqueIdent,
|
|
|
|
min_uidvalidity: ImapUidvalidity,
|
2022-07-21 10:50:44 +00:00
|
|
|
) -> Result<Arc<Mailbox>> {
|
2022-06-29 13:39:54 +00:00
|
|
|
{
|
2022-06-30 12:02:57 +00:00
|
|
|
let cache = self.mailboxes.lock().unwrap();
|
|
|
|
if let Some(mb) = cache.get(&id).and_then(Weak::upgrade) {
|
2022-07-21 10:50:44 +00:00
|
|
|
return Ok(mb);
|
2022-06-29 13:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:33:58 +00:00
|
|
|
let mb = Arc::new(Mailbox::open(&self.creds, id, min_uidvalidity).await?);
|
2022-06-29 13:39:54 +00:00
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
let mut cache = self.mailboxes.lock().unwrap();
|
|
|
|
if let Some(concurrent_mb) = cache.get(&id).and_then(Weak::upgrade) {
|
2022-06-29 13:52:09 +00:00
|
|
|
drop(mb); // we worked for nothing but at least we didn't starve someone else
|
2022-07-21 10:50:44 +00:00
|
|
|
Ok(concurrent_mb)
|
2022-06-29 13:39:54 +00:00
|
|
|
} else {
|
2022-06-30 12:02:57 +00:00
|
|
|
cache.insert(id, Arc::downgrade(&mb));
|
2022-07-21 10:50:44 +00:00
|
|
|
Ok(mb)
|
2022-06-29 13:39:54 +00:00
|
|
|
}
|
2022-06-29 11:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 11:33:58 +00:00
|
|
|
// ---- Mailbox list management ----
|
|
|
|
|
2023-11-02 11:58:45 +00:00
|
|
|
async fn load_mailbox_list(&self) -> Result<(MailboxList, Option<storage::RowRef>)> {
|
2023-12-18 16:09:44 +00:00
|
|
|
let row_ref = storage::RowRef::new(MAILBOX_LIST_PK, MAILBOX_LIST_SK);
|
2023-12-27 13:58:28 +00:00
|
|
|
let (mut list, row) = match self
|
|
|
|
.storage
|
|
|
|
.row_fetch(&storage::Selector::Single(&row_ref))
|
|
|
|
.await
|
|
|
|
{
|
2023-11-02 11:58:45 +00:00
|
|
|
Err(storage::StorageError::NotFound) => (MailboxList::new(), None),
|
2022-06-30 11:33:58 +00:00
|
|
|
Err(e) => return Err(e.into()),
|
2023-11-02 11:58:45 +00:00
|
|
|
Ok(rv) => {
|
2022-07-12 13:23:30 +00:00
|
|
|
let mut list = MailboxList::new();
|
2023-12-18 16:09:44 +00:00
|
|
|
let (row_ref, row_vals) = match rv.into_iter().next() {
|
|
|
|
Some(row_val) => (row_val.row_ref, row_val.value),
|
|
|
|
None => (row_ref, vec![]),
|
|
|
|
};
|
|
|
|
|
|
|
|
for v in row_vals {
|
2023-11-02 11:58:45 +00:00
|
|
|
if let storage::Alternative::Value(vbytes) = v {
|
2022-06-30 15:40:59 +00:00
|
|
|
let list2 =
|
|
|
|
open_deserialize::<MailboxList>(&vbytes, &self.creds.keys.master)?;
|
2022-07-12 13:23:30 +00:00
|
|
|
list.merge(list2);
|
2022-06-30 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 16:09:44 +00:00
|
|
|
(list, Some(row_ref))
|
2022-06-30 15:40:59 +00:00
|
|
|
}
|
2022-06-30 11:33:58 +00:00
|
|
|
};
|
|
|
|
|
2024-01-19 16:40:08 +00:00
|
|
|
let is_default_mbx_missing = [DRAFTS, ARCHIVE, SENT, TRASH]
|
2024-01-19 15:47:20 +00:00
|
|
|
.iter()
|
|
|
|
.map(|mbx| list.create_mailbox(mbx))
|
2024-01-19 16:40:08 +00:00
|
|
|
.fold(false, |acc, r| {
|
|
|
|
acc || matches!(r, CreatedMailbox::Created(..))
|
|
|
|
});
|
2024-01-19 15:47:20 +00:00
|
|
|
let is_inbox_missing = self.ensure_inbox_exists(&mut list, &row).await?;
|
|
|
|
if is_default_mbx_missing && !is_inbox_missing {
|
|
|
|
// It's the only case where we created some mailboxes and not saved them
|
|
|
|
// So we save them!
|
|
|
|
self.save_mailbox_list(&list, row.clone()).await?;
|
|
|
|
}
|
2022-07-12 11:19:27 +00:00
|
|
|
|
2023-11-02 11:58:45 +00:00
|
|
|
Ok((list, row))
|
2022-07-12 11:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn ensure_inbox_exists(
|
|
|
|
&self,
|
|
|
|
list: &mut MailboxList,
|
2023-11-02 11:58:45 +00:00
|
|
|
ct: &Option<storage::RowRef>,
|
2022-07-12 13:23:30 +00:00
|
|
|
) -> Result<bool> {
|
2022-06-30 11:33:58 +00:00
|
|
|
// If INBOX doesn't exist, create a new mailbox with that name
|
|
|
|
// and save new mailbox list.
|
2022-06-30 12:02:57 +00:00
|
|
|
// Also, ensure that the mpsc::watch that keeps track of the
|
|
|
|
// inbox id is up-to-date.
|
2022-07-12 13:23:30 +00:00
|
|
|
let saved;
|
|
|
|
let (inbox_id, inbox_uidvalidity) = match list.create_mailbox(INBOX) {
|
|
|
|
CreatedMailbox::Created(i, v) => {
|
2023-12-18 16:09:44 +00:00
|
|
|
self.save_mailbox_list(list, ct.clone()).await?;
|
2022-07-12 13:23:30 +00:00
|
|
|
saved = true;
|
|
|
|
(i, v)
|
|
|
|
}
|
|
|
|
CreatedMailbox::Existed(i, v) => {
|
|
|
|
saved = false;
|
|
|
|
(i, v)
|
|
|
|
}
|
|
|
|
};
|
2022-07-12 11:19:27 +00:00
|
|
|
let inbox_id = Some((inbox_id, inbox_uidvalidity));
|
|
|
|
if *self.tx_inbox_id.borrow() != inbox_id {
|
|
|
|
self.tx_inbox_id.send(inbox_id).unwrap();
|
|
|
|
}
|
2022-06-30 12:17:54 +00:00
|
|
|
|
2022-07-12 13:23:30 +00:00
|
|
|
Ok(saved)
|
2022-06-30 12:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn save_mailbox_list(
|
|
|
|
&self,
|
|
|
|
list: &MailboxList,
|
2023-12-18 16:09:44 +00:00
|
|
|
ct: Option<storage::RowRef>,
|
2022-06-30 12:17:54 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
let list_blob = seal_serialize(list, &self.creds.keys.master)?;
|
2023-12-18 16:09:44 +00:00
|
|
|
let rref = ct.unwrap_or(storage::RowRef::new(MAILBOX_LIST_PK, MAILBOX_LIST_SK));
|
|
|
|
let row_val = storage::RowVal::new(rref, list_blob);
|
|
|
|
self.storage.row_insert(vec![row_val]).await?;
|
2022-06-30 12:17:54 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-06-30 11:33:58 +00:00
|
|
|
}
|
2022-06-29 11:41:05 +00:00
|
|
|
|
2022-06-30 11:33:58 +00:00
|
|
|
// ---- User's mailbox list (serialized in K2V) ----
|
|
|
|
|
2022-07-12 13:23:30 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct MailboxList(BTreeMap<String, MailboxListEntry>);
|
2022-06-30 11:33:58 +00:00
|
|
|
|
2022-06-30 14:18:08 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
|
2022-06-30 11:33:58 +00:00
|
|
|
struct MailboxListEntry {
|
|
|
|
id_lww: (u64, Option<UniqueIdent>),
|
|
|
|
uidvalidity: ImapUidvalidity,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MailboxListEntry {
|
|
|
|
fn merge(&mut self, other: &Self) {
|
|
|
|
// Simple CRDT merge rule
|
|
|
|
if other.id_lww.0 > self.id_lww.0
|
2022-06-30 12:17:54 +00:00
|
|
|
|| (other.id_lww.0 == self.id_lww.0 && other.id_lww.1 > self.id_lww.1)
|
|
|
|
{
|
2022-06-30 11:33:58 +00:00
|
|
|
self.id_lww = other.id_lww;
|
|
|
|
}
|
|
|
|
self.uidvalidity = std::cmp::max(self.uidvalidity, other.uidvalidity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 13:23:30 +00:00
|
|
|
impl MailboxList {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self(BTreeMap::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(&mut self, list2: Self) {
|
|
|
|
for (k, v) in list2.0.into_iter() {
|
|
|
|
if let Some(e) = self.0.get_mut(&k) {
|
|
|
|
e.merge(&v);
|
|
|
|
} else {
|
|
|
|
self.0.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn existing_mailbox_names(&self) -> Vec<String> {
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, v)| v.id_lww.1.is_some())
|
|
|
|
.map(|(k, _)| k.to_string())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_mailbox(&self, name: &str) -> bool {
|
2023-10-12 10:21:59 +00:00
|
|
|
matches!(
|
|
|
|
self.0.get(name),
|
|
|
|
Some(MailboxListEntry {
|
|
|
|
id_lww: (_, Some(_)),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
)
|
2022-07-12 13:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_mailbox(&self, name: &str) -> Option<(ImapUidvalidity, Option<UniqueIdent>)> {
|
2023-10-12 10:21:59 +00:00
|
|
|
self.0.get(name).map(
|
|
|
|
|MailboxListEntry {
|
|
|
|
id_lww: (_, mailbox_id),
|
|
|
|
uidvalidity,
|
|
|
|
}| (*uidvalidity, *mailbox_id),
|
|
|
|
)
|
2022-07-12 13:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensures mailbox `name` maps to id `id`.
|
|
|
|
/// If it already mapped to that, returns None.
|
|
|
|
/// If a change had to be done, returns Some(new uidvalidity in mailbox).
|
|
|
|
fn set_mailbox(&mut self, name: &str, id: Option<UniqueIdent>) -> Option<ImapUidvalidity> {
|
|
|
|
let (ts, id, uidvalidity) = match self.0.get_mut(name) {
|
|
|
|
None => {
|
|
|
|
if id.is_none() {
|
|
|
|
return None;
|
|
|
|
} else {
|
|
|
|
(now_msec(), id, ImapUidvalidity::new(1).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(MailboxListEntry {
|
|
|
|
id_lww,
|
|
|
|
uidvalidity,
|
|
|
|
}) => {
|
|
|
|
if id_lww.1 == id {
|
|
|
|
return None;
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
std::cmp::max(id_lww.0 + 1, now_msec()),
|
|
|
|
id,
|
|
|
|
ImapUidvalidity::new(uidvalidity.get() + 1).unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.0.insert(
|
|
|
|
name.into(),
|
|
|
|
MailboxListEntry {
|
|
|
|
id_lww: (ts, id),
|
|
|
|
uidvalidity,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Some(uidvalidity)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_uidvalidity(&mut self, name: &str, new_uidvalidity: ImapUidvalidity) {
|
|
|
|
match self.0.get_mut(name) {
|
|
|
|
None => {
|
|
|
|
self.0.insert(
|
|
|
|
name.into(),
|
|
|
|
MailboxListEntry {
|
|
|
|
id_lww: (now_msec(), None),
|
|
|
|
uidvalidity: new_uidvalidity,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Some(MailboxListEntry { uidvalidity, .. }) => {
|
|
|
|
*uidvalidity = std::cmp::max(*uidvalidity, new_uidvalidity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_mailbox(&mut self, name: &str) -> CreatedMailbox {
|
|
|
|
if let Some(MailboxListEntry {
|
|
|
|
id_lww: (_, Some(id)),
|
|
|
|
uidvalidity,
|
|
|
|
}) = self.0.get(name)
|
|
|
|
{
|
|
|
|
return CreatedMailbox::Existed(*id, *uidvalidity);
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = gen_ident();
|
|
|
|
let uidvalidity = self.set_mailbox(name, Some(id)).unwrap();
|
|
|
|
CreatedMailbox::Created(id, uidvalidity)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rename_mailbox(&mut self, old_name: &str, new_name: &str) -> Result<()> {
|
|
|
|
if let Some((uidvalidity, Some(mbid))) = self.get_mailbox(old_name) {
|
|
|
|
if self.has_mailbox(new_name) {
|
|
|
|
bail!(
|
|
|
|
"Cannot rename {} into {}: {} already exists",
|
|
|
|
old_name,
|
|
|
|
new_name,
|
|
|
|
new_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.set_mailbox(old_name, None);
|
|
|
|
self.set_mailbox(new_name, Some(mbid));
|
|
|
|
self.update_uidvalidity(new_name, uidvalidity);
|
|
|
|
Ok(())
|
2022-06-30 11:33:58 +00:00
|
|
|
} else {
|
2022-07-12 13:23:30 +00:00
|
|
|
bail!(
|
|
|
|
"Cannot rename {} into {}: {} doesn't exist",
|
|
|
|
old_name,
|
|
|
|
new_name,
|
|
|
|
old_name
|
|
|
|
);
|
2022-06-30 11:33:58 +00:00
|
|
|
}
|
2022-06-29 11:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-29 13:39:54 +00:00
|
|
|
|
2022-06-30 12:17:54 +00:00
|
|
|
enum CreatedMailbox {
|
|
|
|
Created(UniqueIdent, ImapUidvalidity),
|
|
|
|
Existed(UniqueIdent, ImapUidvalidity),
|
|
|
|
}
|
|
|
|
|
2022-06-30 12:02:57 +00:00
|
|
|
// ---- User cache ----
|
2022-06-29 13:39:54 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2023-12-18 16:09:44 +00:00
|
|
|
static ref USER_CACHE: std::sync::Mutex<HashMap<(String, storage::UnicityBuffer), Weak<User>>> =
|
2022-06-30 12:17:54 +00:00
|
|
|
std::sync::Mutex::new(HashMap::new());
|
2022-06-29 13:39:54 +00:00
|
|
|
}
|