/*
* meli - notmuch backend
*
* Copyright 2019 - 2020 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see .
*/
use std::{
borrow::Cow,
collections::{hash_map::HashMap, BTreeMap, BTreeSet},
ffi::{CStr, CString, OsStr},
io::Read,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
pin::Pin,
ptr::NonNull,
sync::{Arc, Mutex, RwLock},
};
use futures::Stream;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use smallvec::SmallVec;
use crate::{
backends::*,
conf::AccountSettings,
email::{Envelope, EnvelopeHash, Flag},
error::{Error, ErrorKind, IntoError, Result},
utils::shellexpand::ShellExpandTrait,
Collection,
};
macro_rules! call {
($lib:expr, $func:ty) => {{
#[cfg(debug_assertions)]
debug_assert!(
!stringify!($func).starts_with("ffi::"),
"{} must be a valid FFI symbol.",
stringify!($func)
);
let func: libloading::Symbol<$func> = $lib.inner.get(stringify!($func).as_bytes()).unwrap();
func
}};
}
macro_rules! try_call {
($lib:expr, $call:expr) => {{
let status = $call;
if status == $crate::notmuch::ffi::NOTMUCH_STATUS_SUCCESS {
Ok(())
} else {
let c_str = call!($lib, notmuch_status_to_string)(status);
Err($crate::notmuch::NotmuchError(
std::ffi::CStr::from_ptr(c_str)
.to_string_lossy()
.into_owned(),
))
}
}};
}
pub mod query;
use query::{MelibQueryToNotmuchQuery, Query};
pub mod mailbox;
use mailbox::NotmuchMailbox;
pub mod ffi;
use ffi::{
notmuch_database_close, notmuch_database_destroy, notmuch_database_get_revision,
notmuch_database_open, notmuch_status_to_string,
};
mod message;
pub use message::*;
mod tags;
pub use tags::*;
mod thread;
pub use thread::*;
#[derive(Debug)]
#[repr(transparent)]
struct DbPointer(NonNull);
unsafe impl Send for DbPointer {}
unsafe impl Sync for DbPointer {}
impl DbPointer {
#[inline]
pub(self) fn as_mut(&mut self) -> *mut ffi::notmuch_database_t {
unsafe { self.0.as_mut() }
}
}
#[derive(Debug)]
pub struct NotmuchLibrary {
pub inner: libloading::Library,
pub dlpath: Cow<'static, str>,
}
#[derive(Debug)]
pub struct DbConnection {
pub lib: Arc,
inner: Arc>,
pub revision_uuid: Arc>,
}
impl DbConnection {
pub fn get_revision_uuid(&self) -> u64 {
unsafe {
call!(self.lib, notmuch_database_get_revision)(
self.inner.lock().unwrap().as_mut(),
std::ptr::null_mut(),
)
}
}
#[allow(clippy::too_many_arguments)] // Don't judge me clippy.
fn refresh(
&mut self,
mailboxes: Arc>>,
index: Arc>>,
mailbox_index: Arc>>>,
tag_index: Arc>>,
account_hash: AccountHash,
event_consumer: BackendEventConsumer,
new_revision_uuid: u64,
) -> Result<()> {
use RefreshEventKind::*;
let query_str = format!(
"lastmod:{}..{}",
*self.revision_uuid.read().unwrap(),
new_revision_uuid
);
let query: Query = Query::new(self, &query_str)?;
let iter = query.search()?;
let mailbox_index_lck = mailbox_index.write().unwrap();
let mailboxes_lck = mailboxes.read().unwrap();
for message in iter {
let env_hash = message.env_hash();
if let Some(mailbox_hashes) = mailbox_index_lck.get(&env_hash) {
let tags: (Flag, Vec) = message.tags().collect_flags_and_tags();
let mut tag_lock = tag_index.write().unwrap();
for tag in tags.1.iter() {
let num = TagHash::from_bytes(tag.as_bytes());
tag_lock.entry(num).or_insert_with(|| tag.clone());
}
for &mailbox_hash in mailbox_hashes {
(event_consumer)(
account_hash,
BackendEvent::Refresh(RefreshEvent {
account_hash,
mailbox_hash,
kind: NewFlags(env_hash, tags.clone()),
}),
);
}
} else {
let message_id = message.msg_id_cstr().to_string_lossy().to_string();
let env = message.into_envelope(&index, &tag_index);
for (&mailbox_hash, m) in mailboxes_lck.iter() {
let query_str = format!("{} id:{}", m.query_str.as_str(), &message_id);
let query: Query = Query::new(self, &query_str)?;
if query.count().unwrap_or(0) > 0 {
let mut total_lck = m.total.lock().unwrap();
let mut unseen_lck = m.unseen.lock().unwrap();
*total_lck += 1;
if !env.is_seen() {
*unseen_lck += 1;
}
(event_consumer)(
account_hash,
BackendEvent::Refresh(RefreshEvent {
account_hash,
mailbox_hash,
kind: Create(Box::new(env.clone())),
}),
);
}
}
}
}
drop(query);
index.write().unwrap().retain(|&env_hash, msg_id| {
if Message::find_message(self, msg_id).is_err() {
if let Some(mailbox_hashes) = mailbox_index_lck.get(&env_hash) {
for &mailbox_hash in mailbox_hashes {
let m = &mailboxes_lck[&mailbox_hash];
let mut total_lck = m.total.lock().unwrap();
*total_lck = total_lck.saturating_sub(1);
(event_consumer)(
account_hash,
BackendEvent::Refresh(RefreshEvent {
account_hash,
mailbox_hash,
kind: Remove(env_hash),
}),
);
}
}
return false;
}
true
});
Ok(())
}
}
#[derive(Debug)]
pub struct NotmuchError(String);
impl std::fmt::Display for NotmuchError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl std::error::Error for NotmuchError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl Drop for DbConnection {
fn drop(&mut self) {
let mut inner = self.inner.lock().unwrap();
unsafe {
if let Err(err) = try_call!(
self.lib,
call!(self.lib, notmuch_database_close)(inner.as_mut())
) {
log::error!("Could not call C notmuch_database_close: {}", err);
return;
}
if let Err(err) = try_call!(
self.lib,
call!(self.lib, notmuch_database_destroy)(inner.as_mut())
) {
log::error!("Could not call C notmuch_database_destroy: {}", err);
}
}
}
}
#[derive(Debug)]
pub struct NotmuchDb {
#[allow(dead_code)]
lib: Arc,
revision_uuid: Arc>,
mailboxes: Arc>>,
index: Arc>>,
mailbox_index: Arc>>>,
collection: Collection,
path: PathBuf,
_account_name: Arc,
account_hash: AccountHash,
event_consumer: BackendEventConsumer,
save_messages_to: Option,
}
impl NotmuchDb {
#[allow(clippy::new_ret_no_self)]
pub fn new(
s: &AccountSettings,
_is_subscribed: Box bool>,
event_consumer: BackendEventConsumer,
) -> Result> {
#[cfg(target_os = "linux")]
let mut dlpath = Cow::Borrowed("libnotmuch.so.5");
#[cfg(target_os = "macos")]
let mut dlpath = Cow::Borrowed("libnotmuch.5.dylib");
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
let mut dlpath = Cow::Borrowed("libnotmuch.so");
let mut custom_dlpath = false;
if let Some(lib_path) = s.extra.get("library_file_path") {
dlpath = Cow::Owned(lib_path.to_string());
custom_dlpath = true;
}
let lib = Arc::new(NotmuchLibrary {
inner: unsafe {
match libloading::Library::new(dlpath.as_ref()) {
Ok(l) => l,
Err(err) => {
if custom_dlpath {
return Err(Error::new(format!(
"Notmuch `library_file_path` setting value `{}` for account {} \
does not exist or is a directory or not a valid library file.",
dlpath, s.name
))
.set_kind(ErrorKind::Configuration)
.set_source(Some(Arc::new(err))));
} else {
return Err(Error::new("Could not load libnotmuch!")
.set_details(super::NOTMUCH_ERROR_DETAILS)
.set_source(Some(Arc::new(err))));
}
}
}
},
dlpath,
});
let mut path = Path::new(s.root_mailbox.as_str()).expand();
if !path.exists() {
return Err(Error::new(format!(
"Notmuch `root_mailbox` {} for account {} does not exist.",
s.root_mailbox.as_str(),
s.name
))
.set_kind(ErrorKind::Configuration));
}
if !path.is_dir() {
return Err(Error::new(format!(
"Notmuch `root_mailbox` {} for account {} is not a directory.",
s.root_mailbox.as_str(),
s.name
))
.set_kind(ErrorKind::Configuration));
}
path.push(".notmuch");
if !path.exists() || !path.is_dir() {
return Err(Error::new(format!(
"Notmuch `root_mailbox` {} for account {} does not contain a `.notmuch` \
subdirectory.",
s.root_mailbox.as_str(),
s.name
))
.set_kind(ErrorKind::Configuration));
}
path.pop();
let mut mailboxes = HashMap::with_capacity(s.mailboxes.len());
let mut parents: Vec<(MailboxHash, &str)> = Vec::with_capacity(s.mailboxes.len());
for (k, f) in s.mailboxes.iter() {
if let Some(query_str) = f.extra.get("query") {
let hash = MailboxHash::from_bytes(k.as_bytes());
if let Some(parent) = f.extra.get("parent") {
parents.push((hash, parent));
}
mailboxes.insert(
hash,
NotmuchMailbox {
hash,
name: k.to_string(),
path: k.to_string(),
children: vec![],
parent: None,
query_str: query_str.to_string(),
usage: Arc::new(RwLock::new(SpecialUsageMailbox::Normal)),
total: Arc::new(Mutex::new(0)),
unseen: Arc::new(Mutex::new(0)),
},
);
} else {
return Err(Error::new(format!(
"notmuch mailbox configuration entry `{}` for account {} should have a \
`query` value set.",
k, s.name,
))
.set_kind(ErrorKind::Configuration));
}
}
for (hash, parent) in parents {
if let Some(&parent_hash) = mailboxes
.iter()
.find(|(_, v)| v.name == parent)
.map(|(k, _)| k)
{
mailboxes
.entry(parent_hash)
.or_default()
.children
.push(hash);
mailboxes.entry(hash).or_default().parent = Some(parent_hash);
} else {
return Err(Error::new(format!(
"Mailbox configuration for `{}` defines its parent mailbox as `{}` but no \
mailbox exists with this exact name.",
mailboxes[&hash].name(),
parent
))
.set_kind(ErrorKind::Configuration));
}
}
let account_hash = AccountHash::from_bytes(s.name.as_bytes());
Ok(Box::new(Self {
lib,
revision_uuid: Arc::new(RwLock::new(0)),
path,
index: Arc::new(RwLock::new(Default::default())),
mailbox_index: Arc::new(RwLock::new(Default::default())),
collection: Collection::default(),
mailboxes: Arc::new(RwLock::new(mailboxes)),
save_messages_to: None,
_account_name: s.name.to_string().into(),
account_hash,
event_consumer,
}))
}
pub fn validate_config(s: &mut AccountSettings) -> Result<()> {
let mut path = Path::new(s.root_mailbox.as_str()).expand();
if !path.exists() {
return Err(Error::new(format!(
"Notmuch `root_mailbox` {} for account {} does not exist.",
s.root_mailbox.as_str(),
s.name
))
.set_kind(ErrorKind::Configuration));
}
if !path.is_dir() {
return Err(Error::new(format!(
"Notmuch `root_mailbox` {} for account {} is not a directory.",
s.root_mailbox.as_str(),
s.name
))
.set_kind(ErrorKind::Configuration));
}
path.push(".notmuch");
if !path.exists() || !path.is_dir() {
return Err(Error::new(format!(
"Notmuch `root_mailbox` {} for account {} does not contain a `.notmuch` \
subdirectory.",
s.root_mailbox.as_str(),
s.name
))
.set_kind(ErrorKind::Configuration));
}
path.pop();
let account_name = s.name.to_string();
if let Some(lib_path) = s.extra.remove("library_file_path") {
if !Path::new(&lib_path).exists() || Path::new(&lib_path).is_dir() {
return Err(Error::new(format!(
"Notmuch `library_file_path` setting value `{}` for account {} does not exist \
or is a directory.",
&lib_path, s.name
))
.set_kind(ErrorKind::Configuration));
}
}
let mut parents: Vec<(String, String)> = Vec::with_capacity(s.mailboxes.len());
for (k, f) in s.mailboxes.iter_mut() {
if f.extra.remove("query").is_none() {
return Err(Error::new(format!(
"notmuch mailbox configuration entry `{}` for account {} should have a \
`query` value set.",
k, account_name,
))
.set_kind(ErrorKind::Configuration));
}
if let Some(parent) = f.extra.remove("parent") {
parents.push((k.clone(), parent));
}
}
let mut path = Vec::with_capacity(8);
for (mbox, parent) in parents.iter() {
if !s.mailboxes.contains_key(parent) {
return Err(Error::new(format!(
"Mailbox configuration for `{}` defines its parent mailbox as `{}` but no \
mailbox exists with this exact name.",
mbox, parent
))
.set_kind(ErrorKind::Configuration));
}
path.clear();
path.push(mbox.as_str());
let mut iter = parent.as_str();
while let Some((k, v)) = parents.iter().find(|(k, _v)| k == iter) {
if k == mbox {
return Err(Error::new(format!(
"Found cycle in mailbox hierarchy: {}",
path.join("->")
))
.set_kind(ErrorKind::Configuration));
}
path.push(k.as_str());
iter = v.as_str();
}
}
Ok(())
}
fn new_connection(
path: &Path,
revision_uuid: Arc>,
lib: Arc,
write: bool,
) -> Result {
let path_c = CString::new(path.to_str().unwrap()).unwrap();
let path_ptr = path_c.as_ptr();
let mut database: *mut ffi::notmuch_database_t = std::ptr::null_mut();
let status = unsafe {
call!(lib, notmuch_database_open)(
path_ptr,
if write {
ffi::NOTMUCH_DATABASE_MODE_READ_WRITE
} else {
ffi::NOTMUCH_DATABASE_MODE_READ_ONLY
},
std::ptr::addr_of_mut!(database),
)
};
if status != 0 {
return Err(Error::new(format!(
"Could not open notmuch database at path {}. notmuch_database_open returned {}.",
path.display(),
status
)));
}
#[cfg(debug_assertions)]
let database = DbPointer(
NonNull::new(database)
.expect("notmuch_database_open returned a NULL pointer and status = 0"),
);
#[cfg(not(debug_assertions))]
let database = NonNull::new(database).map(DbPointer).ok_or_else(|| {
Error::new("notmuch_database_open returned a NULL pointer and status = 0")
.set_kind(ErrorKind::External)
.set_details(
"libnotmuch exhibited an unexpected and unrecoverable error. Make sure your \
libnotmuch version is compatible with this release.",
)
})?;
let ret = DbConnection {
lib,
revision_uuid,
inner: Arc::new(Mutex::new(database)),
};
if *ret.revision_uuid.read().unwrap() == 0 {
let new = ret.get_revision_uuid();
*ret.revision_uuid.write().unwrap() = new;
}
Ok(ret)
}
}
impl MailBackend for NotmuchDb {
fn capabilities(&self) -> MailBackendCapabilities {
const CAPABILITIES: MailBackendCapabilities = MailBackendCapabilities {
is_async: false,
is_remote: false,
supports_search: true,
extensions: None,
supports_tags: true,
supports_submission: false,
extra_submission_headers: &[],
};
CAPABILITIES
}
fn is_online(&self) -> ResultFuture<()> {
Ok(Box::pin(async { Ok(()) }))
}
fn fetch(
&mut self,
mailbox_hash: MailboxHash,
) -> Result>> + Send + 'static>>> {
struct FetchState {
mailbox_hash: MailboxHash,
database: Arc,
index: Arc>>,
mailbox_index: Arc>>>,
mailboxes: Arc>>,
tag_index: Arc>>,
iter: std::vec::IntoIter,
}
impl FetchState {
async fn fetch(&mut self) -> Result