2020-01-08 17:04:44 +02:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-11-14 17:55:06 +02:00
|
|
|
use crate::async_workers::{Async, AsyncBuilder, AsyncStatus, WorkContext};
|
2020-02-26 10:54:10 +02:00
|
|
|
use crate::backends::MailboxHash;
|
2019-11-14 17:55:06 +02:00
|
|
|
use crate::backends::{
|
2020-02-26 10:54:10 +02:00
|
|
|
BackendMailbox, BackendOp, MailBackend, Mailbox, MailboxPermissions, RefreshEventConsumer,
|
2019-12-11 00:15:36 +02:00
|
|
|
SpecialUsageMailbox,
|
2019-11-14 17:55:06 +02:00
|
|
|
};
|
|
|
|
use crate::conf::AccountSettings;
|
|
|
|
use crate::email::{Envelope, EnvelopeHash, Flag};
|
|
|
|
use crate::error::{MeliError, Result};
|
|
|
|
use crate::shellexpand::ShellExpandTrait;
|
|
|
|
use fnv::FnvHashMap;
|
2020-01-08 17:04:44 +02:00
|
|
|
use smallvec::SmallVec;
|
2019-11-14 17:55:06 +02:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2019-11-30 17:37:00 +02:00
|
|
|
use std::collections::BTreeMap;
|
2020-05-09 14:27:20 +03:00
|
|
|
use std::error::Error;
|
|
|
|
use std::ffi::{CStr, CString, OsStr};
|
2019-11-14 17:55:06 +02:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
use std::io::Read;
|
2020-05-09 14:27:20 +03:00
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2019-11-14 17:55:06 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-12-17 14:12:41 +02:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2019-11-14 17:55:06 +02:00
|
|
|
|
|
|
|
pub mod bindings;
|
|
|
|
use bindings::*;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-05-07 23:03:43 +03:00
|
|
|
struct DbConnection {
|
|
|
|
lib: Arc<libloading::Library>,
|
2019-11-14 17:55:06 +02:00
|
|
|
inner: Arc<RwLock<*mut notmuch_database_t>>,
|
|
|
|
database_ph: std::marker::PhantomData<&'static mut notmuch_database_t>,
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:03:43 +03:00
|
|
|
unsafe impl Send for DbConnection {}
|
|
|
|
unsafe impl Sync for DbConnection {}
|
2019-11-14 17:55:06 +02:00
|
|
|
|
2020-02-26 14:18:00 +02:00
|
|
|
macro_rules! call {
|
|
|
|
($lib:expr, $func:ty) => {{
|
|
|
|
let func: libloading::Symbol<$func> = $lib.get(stringify!($func).as_bytes()).unwrap();
|
|
|
|
func
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:27:20 +03:00
|
|
|
#[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 Error for NotmuchError {
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_call {
|
|
|
|
($lib:expr, $call:expr) => {{
|
|
|
|
let status = unsafe { $call };
|
|
|
|
if status == _notmuch_status_NOTMUCH_STATUS_SUCCESS {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
let c_str = unsafe { call!($lib, notmuch_status_to_string)(status) };
|
|
|
|
Err(NotmuchError(unsafe {
|
|
|
|
CStr::from_ptr(c_str).to_string_lossy().into_owned()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:03:43 +03:00
|
|
|
impl Drop for DbConnection {
|
2019-11-14 17:55:06 +02:00
|
|
|
fn drop(&mut self) {
|
2020-05-07 23:03:43 +03:00
|
|
|
let inner = self.inner.write().unwrap();
|
2019-11-14 17:55:06 +02:00
|
|
|
unsafe {
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_database_close)(*inner);
|
|
|
|
call!(self.lib, notmuch_database_destroy)(*inner);
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:03:43 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NotmuchDb {
|
|
|
|
lib: Arc<libloading::Library>,
|
2020-05-09 14:27:20 +03:00
|
|
|
revision_uuid: Arc<RwLock<u64>>,
|
2020-05-07 23:03:43 +03:00
|
|
|
mailboxes: Arc<RwLock<FnvHashMap<MailboxHash, NotmuchMailbox>>>,
|
2020-05-09 14:27:20 +03:00
|
|
|
index: Arc<RwLock<FnvHashMap<EnvelopeHash, CString>>>,
|
2020-05-07 23:03:43 +03:00
|
|
|
tag_index: Arc<RwLock<BTreeMap<u64, String>>>,
|
|
|
|
path: PathBuf,
|
|
|
|
save_messages_to: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for NotmuchDb {}
|
|
|
|
unsafe impl Sync for NotmuchDb {}
|
|
|
|
|
2019-11-14 17:55:06 +02:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-02-26 10:54:10 +02:00
|
|
|
struct NotmuchMailbox {
|
|
|
|
hash: MailboxHash,
|
|
|
|
children: Vec<MailboxHash>,
|
|
|
|
parent: Option<MailboxHash>,
|
2019-11-14 17:55:06 +02:00
|
|
|
name: String,
|
|
|
|
path: String,
|
|
|
|
query_str: String,
|
2019-12-17 14:12:41 +02:00
|
|
|
usage: Arc<RwLock<SpecialUsageMailbox>>,
|
|
|
|
|
|
|
|
total: Arc<Mutex<usize>>,
|
|
|
|
unseen: Arc<Mutex<usize>>,
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
impl BackendMailbox for NotmuchMailbox {
|
|
|
|
fn hash(&self) -> MailboxHash {
|
2019-11-14 17:55:06 +02:00
|
|
|
self.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
self.name.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> &str {
|
|
|
|
self.path.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change_name(&mut self, _s: &str) {}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
fn clone(&self) -> Mailbox {
|
2019-11-14 17:55:06 +02:00
|
|
|
Box::new(std::clone::Clone::clone(self))
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
fn children(&self) -> &[MailboxHash] {
|
2019-11-14 17:55:06 +02:00
|
|
|
&self.children
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
fn parent(&self) -> Option<MailboxHash> {
|
2019-11-14 17:55:06 +02:00
|
|
|
self.parent
|
|
|
|
}
|
|
|
|
|
2019-12-11 00:15:36 +02:00
|
|
|
fn special_usage(&self) -> SpecialUsageMailbox {
|
2019-12-17 14:12:41 +02:00
|
|
|
*self.usage.read().unwrap()
|
2019-12-11 00:15:36 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
fn permissions(&self) -> MailboxPermissions {
|
|
|
|
MailboxPermissions::default()
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
2019-12-17 14:12:41 +02:00
|
|
|
|
|
|
|
fn is_subscribed(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_is_subscribed(&mut self, _new_val: bool) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_special_usage(&mut self, new_val: SpecialUsageMailbox) -> Result<()> {
|
|
|
|
*self.usage.write()? = new_val;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn count(&self) -> Result<(usize, usize)> {
|
|
|
|
Ok((*self.unseen.lock()?, *self.total.lock()?))
|
|
|
|
}
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
unsafe impl Send for NotmuchMailbox {}
|
|
|
|
unsafe impl Sync for NotmuchMailbox {}
|
2019-11-14 17:55:06 +02:00
|
|
|
|
|
|
|
impl NotmuchDb {
|
2019-11-16 00:33:22 +02:00
|
|
|
pub fn new(
|
|
|
|
s: &AccountSettings,
|
|
|
|
_is_subscribed: Box<dyn Fn(&str) -> bool>,
|
|
|
|
) -> Result<Box<dyn MailBackend>> {
|
2020-02-26 14:18:00 +02:00
|
|
|
let lib = Arc::new(libloading::Library::new("libnotmuch.so.5")?);
|
2020-02-26 10:54:10 +02:00
|
|
|
let path = Path::new(s.root_mailbox.as_str()).expand().to_path_buf();
|
2019-11-14 17:55:06 +02:00
|
|
|
if !path.exists() {
|
2019-11-30 01:12:14 +02:00
|
|
|
return Err(MeliError::new(format!(
|
2020-02-26 10:54:10 +02:00
|
|
|
"\"root_mailbox\" {} for account {} is not a valid path.",
|
|
|
|
s.root_mailbox.as_str(),
|
2019-11-14 17:55:06 +02:00
|
|
|
s.name()
|
2019-11-27 14:22:53 +02:00
|
|
|
)));
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
let mut mailboxes = FnvHashMap::default();
|
|
|
|
for (k, f) in s.mailboxes.iter() {
|
2019-11-14 17:55:06 +02:00
|
|
|
if let Some(query_str) = f.extra.get("query") {
|
|
|
|
let hash = {
|
|
|
|
let mut h = DefaultHasher::new();
|
|
|
|
k.hash(&mut h);
|
|
|
|
h.finish()
|
|
|
|
};
|
2020-02-26 10:54:10 +02:00
|
|
|
mailboxes.insert(
|
2019-11-14 17:55:06 +02:00
|
|
|
hash,
|
2020-02-26 10:54:10 +02:00
|
|
|
NotmuchMailbox {
|
2019-11-14 17:55:06 +02:00
|
|
|
hash,
|
|
|
|
name: k.to_string(),
|
|
|
|
path: k.to_string(),
|
|
|
|
children: vec![],
|
|
|
|
parent: None,
|
|
|
|
query_str: query_str.to_string(),
|
2019-12-17 14:12:41 +02:00
|
|
|
usage: Arc::new(RwLock::new(SpecialUsageMailbox::Normal)),
|
|
|
|
total: Arc::new(Mutex::new(0)),
|
|
|
|
unseen: Arc::new(Mutex::new(0)),
|
2019-11-14 17:55:06 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
2019-11-16 00:33:22 +02:00
|
|
|
return Err(MeliError::new(format!(
|
2020-02-26 10:54:10 +02:00
|
|
|
"notmuch mailbox configuration entry \"{}\" should have a \"query\" value set.",
|
2019-11-14 17:55:06 +02:00
|
|
|
k
|
2019-11-16 00:33:22 +02:00
|
|
|
)));
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 00:33:22 +02:00
|
|
|
Ok(Box::new(NotmuchDb {
|
2020-02-26 14:18:00 +02:00
|
|
|
lib,
|
2020-05-09 14:27:20 +03:00
|
|
|
revision_uuid: Arc::new(RwLock::new(0)),
|
2019-11-14 17:55:06 +02:00
|
|
|
path,
|
|
|
|
index: Arc::new(RwLock::new(Default::default())),
|
2019-11-30 17:37:00 +02:00
|
|
|
tag_index: Arc::new(RwLock::new(Default::default())),
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
mailboxes: Arc::new(RwLock::new(mailboxes)),
|
2019-11-14 17:55:06 +02:00
|
|
|
save_messages_to: None,
|
2019-11-16 00:33:22 +02:00
|
|
|
}))
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
2019-11-27 14:22:53 +02:00
|
|
|
|
2019-11-30 01:12:14 +02:00
|
|
|
pub fn validate_config(s: &AccountSettings) -> Result<()> {
|
2020-02-26 10:54:10 +02:00
|
|
|
let path = Path::new(s.root_mailbox.as_str()).expand().to_path_buf();
|
2019-11-27 14:22:53 +02:00
|
|
|
if !path.exists() {
|
|
|
|
return Err(MeliError::new(format!(
|
2020-02-26 10:54:10 +02:00
|
|
|
"\"root_mailbox\" {} for account {} is not a valid path.",
|
|
|
|
s.root_mailbox.as_str(),
|
2019-11-27 14:22:53 +02:00
|
|
|
s.name()
|
|
|
|
)));
|
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
for (k, f) in s.mailboxes.iter() {
|
2019-11-27 14:22:53 +02:00
|
|
|
if f.extra.get("query").is_none() {
|
|
|
|
return Err(MeliError::new(format!(
|
2020-02-26 10:54:10 +02:00
|
|
|
"notmuch mailbox configuration entry \"{}\" should have a \"query\" value set.",
|
2019-11-27 14:22:53 +02:00
|
|
|
k
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-30 17:37:00 +02:00
|
|
|
|
2020-01-08 17:04:44 +02:00
|
|
|
pub fn search(&self, query_s: &str) -> Result<SmallVec<[EnvelopeHash; 512]>> {
|
2020-05-09 14:27:20 +03:00
|
|
|
let database = Self::new_connection(self.path.as_path(), self.lib.clone(), false)?;
|
2020-05-07 23:03:43 +03:00
|
|
|
let database_lck = database.inner.read().unwrap();
|
2020-05-09 14:27:20 +03:00
|
|
|
let query: Query = Query::new(self.lib.clone(), &database_lck, query_s)?;
|
2020-01-08 17:04:44 +02:00
|
|
|
let mut ret = SmallVec::new();
|
2020-05-09 14:27:20 +03:00
|
|
|
let iter = query.search()?;
|
2019-11-30 17:37:00 +02:00
|
|
|
for message in iter {
|
2020-05-09 14:27:20 +03:00
|
|
|
let msg_id = unsafe { call!(self.lib, notmuch_message_get_message_id)(message) };
|
|
|
|
let c_str = unsafe { CStr::from_ptr(msg_id) };
|
2019-11-30 17:37:00 +02:00
|
|
|
let env_hash = {
|
|
|
|
let mut hasher = DefaultHasher::default();
|
|
|
|
c_str.hash(&mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
};
|
|
|
|
ret.push(env_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
2020-05-07 23:03:43 +03:00
|
|
|
|
2020-05-09 14:27:20 +03:00
|
|
|
fn new_connection(
|
|
|
|
path: &Path,
|
|
|
|
lib: Arc<libloading::Library>,
|
|
|
|
write: bool,
|
|
|
|
) -> Result<DbConnection> {
|
|
|
|
let path_c = std::ffi::CString::new(path.to_str().unwrap()).unwrap();
|
2020-05-07 23:03:43 +03:00
|
|
|
let path_ptr = path_c.as_ptr();
|
|
|
|
let mut database: *mut notmuch_database_t = std::ptr::null_mut();
|
|
|
|
let status = unsafe {
|
2020-05-09 14:27:20 +03:00
|
|
|
call!(lib, notmuch_database_open)(
|
2020-05-07 23:03:43 +03:00
|
|
|
path_ptr,
|
|
|
|
if write {
|
|
|
|
notmuch_database_mode_t_NOTMUCH_DATABASE_MODE_READ_WRITE
|
|
|
|
} else {
|
|
|
|
notmuch_database_mode_t_NOTMUCH_DATABASE_MODE_READ_ONLY
|
|
|
|
},
|
|
|
|
&mut database as *mut _,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if status != 0 {
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Could not open notmuch database at path {}. notmuch_database_open returned {}.",
|
2020-05-09 14:27:20 +03:00
|
|
|
path.display(),
|
2020-05-07 23:03:43 +03:00
|
|
|
status
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
assert!(!database.is_null());
|
|
|
|
Ok(DbConnection {
|
2020-05-09 14:27:20 +03:00
|
|
|
lib,
|
2020-05-07 23:03:43 +03:00
|
|
|
inner: Arc::new(RwLock::new(database)),
|
|
|
|
database_ph: std::marker::PhantomData,
|
|
|
|
})
|
|
|
|
}
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MailBackend for NotmuchDb {
|
2019-12-14 18:46:12 +02:00
|
|
|
fn is_online(&self) -> Result<()> {
|
|
|
|
Ok(())
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
|
2019-11-14 17:55:06 +02:00
|
|
|
let mut w = AsyncBuilder::new();
|
2020-02-26 10:54:10 +02:00
|
|
|
let mailbox_hash = mailbox.hash();
|
2020-05-09 14:27:20 +03:00
|
|
|
let database = NotmuchDb::new_connection(self.path.as_path(), self.lib.clone(), false);
|
2019-11-14 17:55:06 +02:00
|
|
|
let index = self.index.clone();
|
2019-11-30 17:37:00 +02:00
|
|
|
let tag_index = self.tag_index.clone();
|
2020-02-26 10:54:10 +02:00
|
|
|
let mailboxes = self.mailboxes.clone();
|
2020-02-26 14:18:00 +02:00
|
|
|
let lib = self.lib.clone();
|
2019-11-14 17:55:06 +02:00
|
|
|
let handle = {
|
|
|
|
let tx = w.tx();
|
|
|
|
let closure = move |_work_context| {
|
2020-05-07 23:03:43 +03:00
|
|
|
if let Err(err) = database {
|
|
|
|
tx.send(AsyncStatus::Payload(Err(err))).unwrap();
|
|
|
|
tx.send(AsyncStatus::Finished).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let database = Arc::new(database.unwrap());
|
2019-11-14 17:55:06 +02:00
|
|
|
let mut ret: Vec<Envelope> = Vec::new();
|
|
|
|
let database_lck = database.inner.read().unwrap();
|
2020-02-26 10:54:10 +02:00
|
|
|
let mut mailboxes_lck = mailboxes.write().unwrap();
|
|
|
|
let mailbox = mailboxes_lck.get_mut(&mailbox_hash).unwrap();
|
2020-05-09 14:27:20 +03:00
|
|
|
let query: Query =
|
|
|
|
match Query::new(lib.clone(), &database_lck, mailbox.query_str.as_str()) {
|
|
|
|
Ok(q) => q,
|
|
|
|
Err(err) => {
|
|
|
|
tx.send(AsyncStatus::Payload(Err(err))).unwrap();
|
|
|
|
tx.send(AsyncStatus::Finished).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let iter = match query.search() {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(err) => {
|
|
|
|
tx.send(AsyncStatus::Payload(Err(err))).unwrap();
|
|
|
|
tx.send(AsyncStatus::Finished).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
2020-02-26 14:18:00 +02:00
|
|
|
};
|
2019-11-14 17:55:06 +02:00
|
|
|
for message in iter {
|
|
|
|
let mut response = String::new();
|
2020-02-26 14:18:00 +02:00
|
|
|
let fs_path = unsafe { call!(lib, notmuch_message_get_filename)(message) };
|
2019-11-14 17:55:06 +02:00
|
|
|
let mut f = match std::fs::File::open(unsafe {
|
|
|
|
CStr::from_ptr(fs_path)
|
|
|
|
.to_string_lossy()
|
|
|
|
.into_owned()
|
|
|
|
.as_str()
|
|
|
|
}) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
debug!("could not open fs_path {:?} {}", fs_path, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
response.clear();
|
|
|
|
if let Err(e) = f.read_to_string(&mut response) {
|
|
|
|
debug!("could not read fs_path {:?} {}", fs_path, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let env_hash = {
|
2020-05-09 14:27:20 +03:00
|
|
|
let msg_id = unsafe { call!(lib, notmuch_message_get_message_id)(message) };
|
|
|
|
let c_str = unsafe { CStr::from_ptr(msg_id) };
|
2019-11-14 17:55:06 +02:00
|
|
|
let mut hasher = DefaultHasher::default();
|
|
|
|
c_str.hash(&mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
};
|
2020-05-09 14:27:20 +03:00
|
|
|
let c_str = unsafe { CStr::from_ptr(fs_path) };
|
|
|
|
index.write().unwrap().insert(env_hash, c_str.into());
|
2019-11-14 17:55:06 +02:00
|
|
|
let op = Box::new(NotmuchOp {
|
|
|
|
database: database.clone(),
|
2020-02-26 14:18:00 +02:00
|
|
|
lib: lib.clone(),
|
2019-11-14 17:55:06 +02:00
|
|
|
hash: env_hash,
|
|
|
|
index: index.clone(),
|
|
|
|
bytes: Some(response),
|
2019-12-08 10:57:36 +02:00
|
|
|
tag_index: tag_index.clone(),
|
2019-11-14 17:55:06 +02:00
|
|
|
});
|
2019-11-30 17:37:00 +02:00
|
|
|
if let Some(mut env) = Envelope::from_token(op, env_hash) {
|
|
|
|
let mut tag_lock = tag_index.write().unwrap();
|
2020-05-09 14:27:20 +03:00
|
|
|
for tag in TagIterator::new(lib.clone(), message) {
|
2019-11-30 17:37:00 +02:00
|
|
|
let tag = tag.to_string_lossy().into_owned();
|
|
|
|
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
hasher.write(tag.as_bytes());
|
|
|
|
let num = hasher.finish();
|
|
|
|
if !tag_lock.contains_key(&num) {
|
|
|
|
tag_lock.insert(num, tag);
|
|
|
|
}
|
|
|
|
env.labels_mut().push(num);
|
|
|
|
}
|
2019-11-14 17:55:06 +02:00
|
|
|
ret.push(env);
|
|
|
|
} else {
|
|
|
|
debug!("could not parse path {:?}", c_str);
|
|
|
|
index.write().unwrap().remove(&env_hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.send(AsyncStatus::Payload(Ok(ret))).unwrap();
|
|
|
|
tx.send(AsyncStatus::Finished).unwrap();
|
|
|
|
};
|
|
|
|
Box::new(closure)
|
|
|
|
};
|
|
|
|
w.build(handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn watch(
|
|
|
|
&self,
|
2020-05-09 14:27:20 +03:00
|
|
|
sender: RefreshEventConsumer,
|
2019-11-14 17:55:06 +02:00
|
|
|
_work_context: WorkContext,
|
|
|
|
) -> Result<std::thread::ThreadId> {
|
2020-05-09 14:27:20 +03:00
|
|
|
extern crate notify;
|
|
|
|
use crate::backends::{RefreshEvent, RefreshEventKind::*};
|
|
|
|
use notify::{watcher, RecursiveMode, Watcher};
|
|
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
|
|
let mut watcher = watcher(tx, std::time::Duration::from_secs(2)).unwrap();
|
|
|
|
watcher.watch(&self.path, RecursiveMode::Recursive).unwrap();
|
|
|
|
let path = self.path.clone();
|
|
|
|
let lib = self.lib.clone();
|
|
|
|
{
|
|
|
|
let database = NotmuchDb::new_connection(path.as_path(), lib.clone(), false)?;
|
|
|
|
let mut revision_uuid_lck = self.revision_uuid.write().unwrap();
|
|
|
|
|
|
|
|
*revision_uuid_lck = unsafe {
|
|
|
|
call!(lib, notmuch_database_get_revision)(
|
|
|
|
*database.inner.read().unwrap(),
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
debug!(*revision_uuid_lck);
|
|
|
|
}
|
|
|
|
let revision_uuid = self.revision_uuid.clone();
|
|
|
|
|
2019-11-14 17:55:06 +02:00
|
|
|
let handle = std::thread::Builder::new()
|
|
|
|
.name(format!(
|
|
|
|
"watching {}",
|
|
|
|
self.path.file_name().unwrap().to_str().unwrap()
|
|
|
|
))
|
2020-05-09 14:27:20 +03:00
|
|
|
.spawn(move || {
|
|
|
|
let _watcher = watcher;
|
|
|
|
let c = move || -> std::result::Result<(), MeliError> {
|
|
|
|
loop {
|
|
|
|
let _ = rx.recv().map_err(|err| err.to_string())?;
|
|
|
|
let database =
|
|
|
|
NotmuchDb::new_connection(path.as_path(), lib.clone(), false)?;
|
|
|
|
let database_lck = database.inner.read().unwrap();
|
|
|
|
let mut revision_uuid_lck = revision_uuid.write().unwrap();
|
|
|
|
|
|
|
|
let new_revision = unsafe {
|
|
|
|
call!(lib, notmuch_database_get_revision)(
|
|
|
|
*database_lck,
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if new_revision > *revision_uuid_lck {
|
|
|
|
debug!(new_revision);
|
|
|
|
debug!(*revision_uuid_lck);
|
|
|
|
let query_str =
|
|
|
|
format!("lastmod:{}..{}", *revision_uuid_lck, new_revision);
|
|
|
|
let query: Query = Query::new(lib.clone(), &database_lck, &query_str)?;
|
|
|
|
let iter = query.search()?;
|
|
|
|
*revision_uuid_lck = new_revision;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = c() {
|
|
|
|
sender.send(RefreshEvent {
|
|
|
|
hash: 0,
|
|
|
|
kind: Failure(err.into()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})?;
|
2019-11-14 17:55:06 +02:00
|
|
|
Ok(handle.thread().id())
|
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
fn mailboxes(&self) -> Result<FnvHashMap<MailboxHash, Mailbox>> {
|
2019-11-30 01:12:14 +02:00
|
|
|
Ok(self
|
2020-02-26 10:54:10 +02:00
|
|
|
.mailboxes
|
2019-11-14 17:55:06 +02:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2020-02-26 10:54:10 +02:00
|
|
|
.map(|(k, f)| (*k, BackendMailbox::clone(f)))
|
2019-11-23 17:47:24 +02:00
|
|
|
.collect())
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
fn operation(&self, hash: EnvelopeHash) -> Box<dyn BackendOp> {
|
|
|
|
Box::new(NotmuchOp {
|
2020-05-09 14:27:20 +03:00
|
|
|
database: Arc::new(
|
|
|
|
Self::new_connection(self.path.as_path(), self.lib.clone(), true).unwrap(),
|
|
|
|
),
|
2020-02-26 14:18:00 +02:00
|
|
|
lib: self.lib.clone(),
|
2019-11-14 17:55:06 +02:00
|
|
|
hash,
|
|
|
|
index: self.index.clone(),
|
|
|
|
bytes: None,
|
2019-12-08 10:57:36 +02:00
|
|
|
tag_index: self.tag_index.clone(),
|
2019-11-14 17:55:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
fn save(&self, bytes: &[u8], _mailbox: &str, flags: Option<Flag>) -> Result<()> {
|
2020-03-18 19:22:17 +02:00
|
|
|
let path = self
|
2019-11-14 17:55:06 +02:00
|
|
|
.save_messages_to
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.path)
|
|
|
|
.to_path_buf();
|
2020-02-26 10:54:10 +02:00
|
|
|
crate::backends::MaildirType::save_to_mailbox(path, bytes, flags)
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn::std::any::Any {
|
|
|
|
self
|
|
|
|
}
|
2019-11-30 17:37:00 +02:00
|
|
|
|
|
|
|
fn tags(&self) -> Option<Arc<RwLock<BTreeMap<u64, String>>>> {
|
|
|
|
Some(self.tag_index.clone())
|
|
|
|
}
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct NotmuchOp {
|
|
|
|
hash: EnvelopeHash,
|
2020-05-09 14:27:20 +03:00
|
|
|
index: Arc<RwLock<FnvHashMap<EnvelopeHash, CString>>>,
|
2019-12-08 10:57:36 +02:00
|
|
|
tag_index: Arc<RwLock<BTreeMap<u64, String>>>,
|
2020-05-07 23:03:43 +03:00
|
|
|
database: Arc<DbConnection>,
|
2019-11-14 17:55:06 +02:00
|
|
|
bytes: Option<String>,
|
2020-02-26 14:18:00 +02:00
|
|
|
lib: Arc<libloading::Library>,
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendOp for NotmuchOp {
|
|
|
|
fn description(&self) -> String {
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_bytes(&mut self) -> Result<&[u8]> {
|
|
|
|
let path = &self.index.read().unwrap()[&self.hash];
|
2020-05-09 14:27:20 +03:00
|
|
|
let mut f = std::fs::File::open(&OsStr::from_bytes(path.to_bytes()))?;
|
2019-11-14 17:55:06 +02:00
|
|
|
let mut response = String::new();
|
|
|
|
f.read_to_string(&mut response)?;
|
|
|
|
self.bytes = Some(response);
|
|
|
|
Ok(self.bytes.as_ref().unwrap().as_bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_flags(&self) -> Flag {
|
|
|
|
let mut flag = Flag::default();
|
2020-05-09 14:27:20 +03:00
|
|
|
let path = &self.index.read().unwrap()[&self.hash];
|
|
|
|
for f in path.to_bytes().iter().rev() {
|
2019-11-14 17:55:06 +02:00
|
|
|
match f {
|
2020-05-09 14:27:20 +03:00
|
|
|
b',' => break,
|
|
|
|
b'D' => flag |= Flag::DRAFT,
|
|
|
|
b'F' => flag |= Flag::FLAGGED,
|
|
|
|
b'P' => flag |= Flag::PASSED,
|
|
|
|
b'R' => flag |= Flag::REPLIED,
|
|
|
|
b'S' => flag |= Flag::SEEN,
|
|
|
|
b'T' => flag |= Flag::TRASHED,
|
2019-11-14 17:55:06 +02:00
|
|
|
_ => {
|
2020-05-09 14:27:20 +03:00
|
|
|
debug!(
|
|
|
|
"DEBUG: in fetch_flags, unknown flag '{}' path is {:?}",
|
|
|
|
f, path
|
|
|
|
);
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flag
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_flag(&mut self, _envelope: &mut Envelope, f: Flag, value: bool) -> Result<()> {
|
|
|
|
let mut message: *mut notmuch_message_t = std::ptr::null_mut();
|
|
|
|
let mut index_lck = self.index.write().unwrap();
|
|
|
|
unsafe {
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_database_find_message_by_filename)(
|
2019-11-14 17:55:06 +02:00
|
|
|
*self.database.inner.read().unwrap(),
|
|
|
|
index_lck[&self.hash].as_ptr(),
|
|
|
|
&mut message as *mut _,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if message.is_null() {
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Error, message with path {:?} not found in notmuch database.",
|
|
|
|
index_lck[&self.hash]
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:27:20 +03:00
|
|
|
// TODO: freeze/thaw message.
|
|
|
|
let tags = TagIterator::new(self.lib.clone(), message).collect::<Vec<&CStr>>();
|
2019-11-14 17:55:06 +02:00
|
|
|
debug!(&tags);
|
|
|
|
|
|
|
|
macro_rules! cstr {
|
|
|
|
($l:literal) => {
|
|
|
|
CStr::from_bytes_with_nul_unchecked($l)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
macro_rules! add_tag {
|
2020-05-09 14:27:20 +03:00
|
|
|
($l:literal) => {{
|
|
|
|
if tags.contains(unsafe { &cstr!($l) }) {
|
|
|
|
return Ok(());
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
2020-05-09 14:27:20 +03:00
|
|
|
if let Err(err) = try_call!(
|
|
|
|
self.lib,
|
|
|
|
call!(self.lib, notmuch_message_add_tag)(message, cstr!($l).as_ptr())
|
|
|
|
) {
|
|
|
|
return Err(
|
|
|
|
MeliError::new("Could not set tag.").set_source(Some(Arc::new(err)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}};
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
macro_rules! remove_tag {
|
2020-05-09 14:27:20 +03:00
|
|
|
($l:literal) => {{
|
|
|
|
if !tags.contains(unsafe { &cstr!($l) }) {
|
|
|
|
return Ok(());
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
2020-05-09 14:27:20 +03:00
|
|
|
if let Err(err) = try_call!(
|
|
|
|
self.lib,
|
|
|
|
call!(self.lib, notmuch_message_remove_tag)(message, cstr!($l).as_ptr())
|
|
|
|
) {
|
|
|
|
return Err(
|
|
|
|
MeliError::new("Could not set tag.").set_source(Some(Arc::new(err)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}};
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
match f {
|
|
|
|
Flag::DRAFT if value => add_tag!(b"draft\0"),
|
|
|
|
Flag::DRAFT => remove_tag!(b"draft\0"),
|
|
|
|
Flag::FLAGGED if value => add_tag!(b"flagged\0"),
|
|
|
|
Flag::FLAGGED => remove_tag!(b"flagged\0"),
|
|
|
|
Flag::PASSED if value => add_tag!(b"passed\0"),
|
|
|
|
Flag::PASSED => remove_tag!(b"passed\0"),
|
|
|
|
Flag::REPLIED if value => add_tag!(b"replied\0"),
|
|
|
|
Flag::REPLIED => remove_tag!(b"replied\0"),
|
|
|
|
Flag::SEEN if value => remove_tag!(b"unread\0"),
|
|
|
|
Flag::SEEN => add_tag!(b"unread\0"),
|
|
|
|
Flag::TRASHED if value => add_tag!(b"trashed\0"),
|
|
|
|
Flag::TRASHED => remove_tag!(b"trashed\0"),
|
|
|
|
_ => debug!("flags is {:?} value = {}", f, value),
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update message filesystem path. */
|
2020-05-09 14:27:20 +03:00
|
|
|
if let Err(err) = try_call!(
|
|
|
|
self.lib,
|
|
|
|
call!(self.lib, notmuch_message_tags_to_maildir_flags)(message)
|
|
|
|
) {
|
|
|
|
return Err(MeliError::new("Could not set tag.").set_source(Some(Arc::new(err))));
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 14:18:00 +02:00
|
|
|
let fs_path = unsafe { call!(self.lib, notmuch_message_get_filename)(message) };
|
2019-11-14 17:55:06 +02:00
|
|
|
let c_str = unsafe { CStr::from_ptr(fs_path) };
|
|
|
|
if let Some(p) = index_lck.get_mut(&self.hash) {
|
2020-05-09 14:27:20 +03:00
|
|
|
*p = c_str.into();
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-08 10:57:36 +02:00
|
|
|
|
|
|
|
fn set_tag(&mut self, envelope: &mut Envelope, tag: String, value: bool) -> Result<()> {
|
|
|
|
let mut message: *mut notmuch_message_t = std::ptr::null_mut();
|
|
|
|
let index_lck = self.index.read().unwrap();
|
|
|
|
unsafe {
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_database_find_message_by_filename)(
|
2019-12-08 10:57:36 +02:00
|
|
|
*self.database.inner.read().unwrap(),
|
|
|
|
index_lck[&self.hash].as_ptr(),
|
|
|
|
&mut message as *mut _,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if message.is_null() {
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Error, message with path {:?} not found in notmuch database.",
|
|
|
|
index_lck[&self.hash]
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if value {
|
2020-05-09 14:27:20 +03:00
|
|
|
if let Err(err) = try_call!(
|
|
|
|
self.lib,
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_message_add_tag)(
|
|
|
|
message,
|
|
|
|
CString::new(tag.as_str()).unwrap().as_ptr(),
|
|
|
|
)
|
2020-05-09 14:27:20 +03:00
|
|
|
) {
|
|
|
|
return Err(MeliError::new("Could not set tag.").set_source(Some(Arc::new(err))));
|
2019-12-08 10:57:36 +02:00
|
|
|
}
|
|
|
|
debug!("added tag {}", &tag);
|
|
|
|
} else {
|
2020-05-09 14:27:20 +03:00
|
|
|
if let Err(err) = try_call!(
|
|
|
|
self.lib,
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_message_remove_tag)(
|
|
|
|
message,
|
|
|
|
CString::new(tag.as_str()).unwrap().as_ptr(),
|
|
|
|
)
|
2020-05-09 14:27:20 +03:00
|
|
|
) {
|
|
|
|
return Err(MeliError::new("Could not set tag.").set_source(Some(Arc::new(err))));
|
2019-12-08 10:57:36 +02:00
|
|
|
}
|
|
|
|
debug!("removed tag {}", &tag);
|
|
|
|
}
|
|
|
|
let hash = tag_hash!(tag);
|
|
|
|
if value {
|
|
|
|
self.tag_index.write().unwrap().insert(hash, tag);
|
|
|
|
} else {
|
|
|
|
self.tag_index.write().unwrap().remove(&hash);
|
|
|
|
}
|
|
|
|
if !envelope.labels().iter().any(|&h_| h_ == hash) {
|
|
|
|
if value {
|
|
|
|
envelope.labels_mut().push(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !value {
|
|
|
|
if let Some(pos) = envelope.labels().iter().position(|&h_| h_ == hash) {
|
|
|
|
envelope.labels_mut().remove(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MessageIterator {
|
2020-02-26 14:18:00 +02:00
|
|
|
lib: Arc<libloading::Library>,
|
2019-11-14 17:55:06 +02:00
|
|
|
messages: *mut notmuch_messages_t,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for MessageIterator {
|
|
|
|
type Item = *mut notmuch_message_t;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.messages.is_null() {
|
|
|
|
None
|
2020-02-26 14:18:00 +02:00
|
|
|
} else if unsafe { call!(self.lib, notmuch_messages_valid)(self.messages) } == 1 {
|
|
|
|
let ret = Some(unsafe { call!(self.lib, notmuch_messages_get)(self.messages) });
|
2019-11-14 17:55:06 +02:00
|
|
|
unsafe {
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_messages_move_to_next)(self.messages);
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
self.messages = std::ptr::null_mut();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TagIterator {
|
2020-02-26 14:18:00 +02:00
|
|
|
lib: Arc<libloading::Library>,
|
2019-11-14 17:55:06 +02:00
|
|
|
tags: *mut notmuch_tags_t,
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:27:20 +03:00
|
|
|
impl TagIterator {
|
|
|
|
fn new(lib: Arc<libloading::Library>, message: *mut notmuch_message_t) -> Self {
|
|
|
|
TagIterator {
|
|
|
|
tags: unsafe { call!(lib, notmuch_message_get_tags)(message) },
|
|
|
|
lib,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:55:06 +02:00
|
|
|
impl Iterator for TagIterator {
|
|
|
|
type Item = &'static CStr;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.tags.is_null() {
|
|
|
|
None
|
2020-02-26 14:18:00 +02:00
|
|
|
} else if unsafe { call!(self.lib, notmuch_tags_valid)(self.tags) } == 1 {
|
|
|
|
let ret = Some(unsafe { CStr::from_ptr(call!(self.lib, notmuch_tags_get)(self.tags)) });
|
2019-11-14 17:55:06 +02:00
|
|
|
unsafe {
|
2020-02-26 14:18:00 +02:00
|
|
|
call!(self.lib, notmuch_tags_move_to_next)(self.tags);
|
2019-11-14 17:55:06 +02:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
self.tags = std::ptr::null_mut();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 14:27:20 +03:00
|
|
|
|
|
|
|
pub struct Query<'s> {
|
|
|
|
lib: Arc<libloading::Library>,
|
|
|
|
ptr: *mut notmuch_query_t,
|
|
|
|
query_str: &'s str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> Query<'s> {
|
|
|
|
fn new(
|
|
|
|
lib: Arc<libloading::Library>,
|
|
|
|
database: &*mut notmuch_database_t,
|
|
|
|
query_str: &'s str,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let query_cstr = std::ffi::CString::new(query_str)?;
|
|
|
|
let query: *mut notmuch_query_t =
|
|
|
|
unsafe { call!(lib, notmuch_query_create)(*database, query_cstr.as_ptr()) };
|
|
|
|
if query.is_null() {
|
|
|
|
return Err(MeliError::new("Could not create query. Out of memory?"));
|
|
|
|
}
|
|
|
|
Ok(Query {
|
|
|
|
lib,
|
|
|
|
ptr: query,
|
|
|
|
query_str,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn search(&self) -> Result<MessageIterator> {
|
|
|
|
let mut messages: *mut notmuch_messages_t = std::ptr::null_mut();
|
|
|
|
let status = unsafe {
|
|
|
|
call!(self.lib, notmuch_query_search_messages)(self.ptr, &mut messages as *mut _)
|
|
|
|
};
|
|
|
|
if status != 0 {
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Search for {} returned {}",
|
|
|
|
self.query_str, status,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
assert!(!messages.is_null());
|
|
|
|
Ok(MessageIterator {
|
|
|
|
messages,
|
|
|
|
lib: self.lib.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Query<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
call!(self.lib, notmuch_query_destroy)(self.ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|