2017-09-14 18:08:14 +03:00
|
|
|
/*
|
|
|
|
* meli - accounts module.
|
|
|
|
*
|
|
|
|
* Copyright 2017 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-08-06 22:20:34 +03:00
|
|
|
/*!
|
|
|
|
* Account management from user configuration.
|
|
|
|
*/
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
use super::{AccountConf, FileMailboxConf};
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
use crate::jobs::{JobChannel, JobExecutor, JobId, JoinHandle};
|
2020-08-17 15:31:30 +03:00
|
|
|
use indexmap::IndexMap;
|
2020-07-25 17:53:04 +03:00
|
|
|
use melib::backends::*;
|
2020-02-10 02:11:07 +02:00
|
|
|
use melib::email::*;
|
2019-07-18 20:16:51 +03:00
|
|
|
use melib::error::{MeliError, Result};
|
2020-02-04 17:26:25 +02:00
|
|
|
use melib::text_processing::GlobMatch;
|
2020-07-23 13:39:58 +03:00
|
|
|
use melib::thread::{SortField, SortOrder, Threads};
|
2019-02-15 09:06:42 +02:00
|
|
|
use melib::AddressBook;
|
2020-02-10 02:11:07 +02:00
|
|
|
use melib::Collection;
|
2020-01-08 17:04:44 +02:00
|
|
|
use smallvec::SmallVec;
|
2020-07-24 21:48:29 +03:00
|
|
|
use std::collections::BTreeMap;
|
2020-05-19 15:00:26 +03:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-02-15 09:06:42 +02:00
|
|
|
|
2019-06-18 21:13:58 +03:00
|
|
|
use crate::types::UIEvent::{self, EnvelopeRemove, EnvelopeRename, EnvelopeUpdate, Notification};
|
2019-12-12 01:01:11 +02:00
|
|
|
use crate::{StatusEvent, ThreadEvent};
|
2019-09-11 17:57:55 +03:00
|
|
|
use crossbeam::Sender;
|
2020-06-27 21:40:46 +03:00
|
|
|
use futures::channel::oneshot;
|
2020-06-30 11:40:26 +03:00
|
|
|
use futures::future::FutureExt;
|
2020-06-26 18:31:37 +03:00
|
|
|
pub use futures::stream::Stream;
|
2020-06-28 15:39:33 +03:00
|
|
|
use futures::stream::StreamExt;
|
2020-07-24 22:09:35 +03:00
|
|
|
use std::borrow::Cow;
|
2019-05-26 02:41:29 +03:00
|
|
|
use std::collections::VecDeque;
|
2020-08-06 19:45:08 +03:00
|
|
|
use std::convert::TryFrom;
|
2019-02-15 09:06:42 +02:00
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
2017-09-16 15:05:28 +03:00
|
|
|
use std::ops::{Index, IndexMut};
|
2019-11-24 17:00:55 +02:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2020-06-28 15:39:33 +03:00
|
|
|
use std::pin::Pin;
|
2018-08-03 13:46:08 +03:00
|
|
|
use std::result;
|
2019-11-03 13:17:11 +02:00
|
|
|
use std::sync::{Arc, RwLock};
|
2018-08-03 13:46:08 +03:00
|
|
|
|
2020-07-05 13:22:48 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! try_recv_timeout {
|
|
|
|
($oneshot:expr) => {{
|
|
|
|
const _3_MS: std::time::Duration = std::time::Duration::from_millis(95);
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
let mut res = Ok(None);
|
|
|
|
while now + _3_MS >= std::time::Instant::now() {
|
|
|
|
res = $oneshot.try_recv().map_err(|_| MeliError::new("canceled"));
|
|
|
|
if res.as_ref().map(|r| r.is_some()).unwrap_or(false) || res.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:29:32 +03:00
|
|
|
#[derive(Debug)]
|
2020-02-10 02:11:07 +02:00
|
|
|
pub enum MailboxStatus {
|
|
|
|
Available,
|
2019-07-28 18:52:45 +03:00
|
|
|
Failed(MeliError),
|
|
|
|
/// first argument is done work, and second is total work
|
2020-02-10 02:11:07 +02:00
|
|
|
Parsing(usize, usize),
|
2020-02-08 23:42:31 +02:00
|
|
|
None,
|
2019-09-08 11:05:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 02:11:07 +02:00
|
|
|
impl Default for MailboxStatus {
|
2019-09-08 11:05:25 +03:00
|
|
|
fn default() -> Self {
|
2020-03-02 12:06:19 +02:00
|
|
|
MailboxStatus::None
|
2019-09-08 11:05:25 +03:00
|
|
|
}
|
2019-07-28 18:52:45 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 02:11:07 +02:00
|
|
|
impl MailboxStatus {
|
2019-07-28 18:52:45 +03:00
|
|
|
pub fn is_available(&self) -> bool {
|
2020-02-10 02:11:07 +02:00
|
|
|
if let MailboxStatus::Available = self {
|
2019-07-28 18:52:45 +03:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_parsing(&self) -> bool {
|
2020-02-10 02:11:07 +02:00
|
|
|
if let MailboxStatus::Parsing(_, _) = self {
|
2019-07-28 18:52:45 +03:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 02:11:07 +02:00
|
|
|
}
|
2019-10-07 16:42:44 +03:00
|
|
|
|
2020-02-10 02:11:07 +02:00
|
|
|
#[derive(Debug)]
|
2020-02-26 10:54:10 +02:00
|
|
|
pub struct MailboxEntry {
|
2020-02-10 02:11:07 +02:00
|
|
|
pub status: MailboxStatus,
|
|
|
|
pub name: String,
|
2020-02-26 10:54:10 +02:00
|
|
|
pub ref_mailbox: Mailbox,
|
|
|
|
pub conf: FileMailboxConf,
|
2020-02-10 02:11:07 +02:00
|
|
|
}
|
2019-10-07 16:42:44 +03:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
impl MailboxEntry {
|
2020-02-10 02:11:07 +02:00
|
|
|
pub fn status(&self) -> String {
|
|
|
|
match self.status {
|
2020-08-07 00:39:17 +03:00
|
|
|
MailboxStatus::Available => format!(
|
|
|
|
"{} [{} messages]",
|
|
|
|
self.name(),
|
|
|
|
self.ref_mailbox.count().ok().unwrap_or((0, 0)).1
|
|
|
|
),
|
2020-02-10 02:11:07 +02:00
|
|
|
MailboxStatus::Failed(ref e) => e.to_string(),
|
2020-08-07 00:39:17 +03:00
|
|
|
MailboxStatus::None => "Retrieving mailbox.".to_string(),
|
2020-02-10 02:11:07 +02:00
|
|
|
MailboxStatus::Parsing(done, total) => {
|
|
|
|
format!("Parsing messages. [{}/{}]", done, total)
|
|
|
|
}
|
2019-07-28 18:52:45 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:42:44 +03:00
|
|
|
|
2020-02-10 02:11:07 +02:00
|
|
|
pub fn name(&self) -> &str {
|
2020-07-24 22:10:39 +03:00
|
|
|
if let Some(name) = self.conf.mailbox_conf.alias.as_ref() {
|
|
|
|
name
|
|
|
|
} else {
|
|
|
|
&self.ref_mailbox.name()
|
|
|
|
}
|
2019-07-28 18:52:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 18:08:14 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Account {
|
|
|
|
name: String,
|
2020-06-08 22:08:10 +03:00
|
|
|
hash: AccountHash,
|
2020-07-08 00:26:40 +03:00
|
|
|
pub is_online: Result<()>,
|
2020-08-17 15:31:30 +03:00
|
|
|
pub(crate) mailbox_entries: IndexMap<MailboxHash, MailboxEntry>,
|
2020-02-26 10:54:10 +02:00
|
|
|
pub(crate) mailboxes_order: Vec<MailboxHash>,
|
|
|
|
tree: Vec<MailboxNode>,
|
|
|
|
sent_mailbox: Option<MailboxHash>,
|
2019-05-26 02:34:03 +03:00
|
|
|
pub(crate) collection: Collection,
|
2019-02-15 09:06:42 +02:00
|
|
|
pub(crate) address_book: AddressBook,
|
|
|
|
pub(crate) settings: AccountConf,
|
2019-11-03 13:17:11 +02:00
|
|
|
pub(crate) backend: Arc<RwLock<Box<dyn MailBackend>>>,
|
2019-05-26 02:41:29 +03:00
|
|
|
|
2020-06-29 18:41:47 +03:00
|
|
|
pub job_executor: Arc<JobExecutor>,
|
2020-06-30 11:40:26 +03:00
|
|
|
pub active_jobs: HashMap<JobId, JobRequest>,
|
2020-07-24 21:48:29 +03:00
|
|
|
pub active_job_instants: BTreeMap<std::time::Instant, JobId>,
|
2019-12-12 01:01:11 +02:00
|
|
|
sender: Sender<ThreadEvent>,
|
2020-02-26 10:54:10 +02:00
|
|
|
event_queue: VecDeque<(MailboxHash, RefreshEvent)>,
|
2020-07-25 17:53:04 +03:00
|
|
|
pub backend_capabilities: MailBackendCapabilities,
|
2017-09-14 18:08:14 +03:00
|
|
|
}
|
|
|
|
|
2020-06-30 11:40:26 +03:00
|
|
|
pub enum JobRequest {
|
2020-07-15 11:02:53 +03:00
|
|
|
Mailboxes(
|
|
|
|
JoinHandle,
|
|
|
|
oneshot::Receiver<Result<HashMap<MailboxHash, Mailbox>>>,
|
|
|
|
),
|
2020-07-17 23:30:02 +03:00
|
|
|
Fetch(
|
2020-06-28 15:39:33 +03:00
|
|
|
MailboxHash,
|
2020-07-15 11:02:53 +03:00
|
|
|
JoinHandle,
|
2020-06-28 15:39:33 +03:00
|
|
|
oneshot::Receiver<(
|
|
|
|
Option<Result<Vec<Envelope>>>,
|
|
|
|
Pin<Box<dyn Stream<Item = Result<Vec<Envelope>>> + Send + 'static>>,
|
|
|
|
)>,
|
|
|
|
),
|
2020-07-16 22:54:50 +03:00
|
|
|
Generic {
|
2020-07-24 22:09:35 +03:00
|
|
|
name: Cow<'static, str>,
|
2020-08-26 19:15:31 +03:00
|
|
|
logging_level: melib::LoggingLevel,
|
2020-07-16 22:54:50 +03:00
|
|
|
handle: JoinHandle,
|
|
|
|
channel: JobChannel<()>,
|
2020-08-25 15:39:43 +03:00
|
|
|
on_finish: Option<crate::types::CallbackFn>,
|
2020-07-16 22:54:50 +03:00
|
|
|
},
|
2020-07-15 11:02:53 +03:00
|
|
|
IsOnline(JoinHandle, oneshot::Receiver<Result<()>>),
|
|
|
|
Refresh(MailboxHash, JoinHandle, oneshot::Receiver<Result<()>>),
|
2020-07-24 20:17:06 +03:00
|
|
|
SetFlags(EnvelopeHashBatch, JoinHandle, oneshot::Receiver<Result<()>>),
|
2020-08-26 20:00:25 +03:00
|
|
|
SaveMessage {
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
mailbox_hash: MailboxHash,
|
|
|
|
handle: JoinHandle,
|
|
|
|
channel: oneshot::Receiver<Result<()>>,
|
|
|
|
},
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
SendMessage,
|
|
|
|
SendMessageBackground(JoinHandle, JobChannel<()>),
|
2020-07-15 11:02:53 +03:00
|
|
|
CopyTo(MailboxHash, JoinHandle, oneshot::Receiver<Result<Vec<u8>>>),
|
2020-07-24 20:17:06 +03:00
|
|
|
DeleteMessages(EnvelopeHashBatch, JoinHandle, oneshot::Receiver<Result<()>>),
|
2020-08-25 22:56:45 +03:00
|
|
|
CreateMailbox {
|
|
|
|
path: String,
|
|
|
|
handle: JoinHandle,
|
|
|
|
channel: JobChannel<(MailboxHash, HashMap<MailboxHash, Mailbox>)>,
|
|
|
|
},
|
|
|
|
DeleteMailbox {
|
|
|
|
mailbox_hash: MailboxHash,
|
|
|
|
handle: JoinHandle,
|
|
|
|
channel: JobChannel<HashMap<MailboxHash, Mailbox>>,
|
|
|
|
},
|
2020-06-29 00:18:24 +03:00
|
|
|
//RenameMailbox,
|
2020-07-15 11:02:53 +03:00
|
|
|
Search(JoinHandle),
|
|
|
|
AsBytes(JoinHandle),
|
|
|
|
SetMailboxPermissions(MailboxHash, JoinHandle, oneshot::Receiver<Result<()>>),
|
|
|
|
SetMailboxSubscription(MailboxHash, JoinHandle, oneshot::Receiver<Result<()>>),
|
2020-08-25 21:12:28 +03:00
|
|
|
Watch {
|
|
|
|
channel: oneshot::Receiver<Result<()>>,
|
|
|
|
handle: JoinHandle,
|
|
|
|
},
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:27:43 +03:00
|
|
|
impl Drop for JobRequest {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self {
|
|
|
|
JobRequest::Generic { handle, .. } => handle.0.cancel(),
|
|
|
|
JobRequest::Mailboxes(h, _) => h.0.cancel(),
|
|
|
|
JobRequest::Fetch(_, h, _) => h.0.cancel(),
|
|
|
|
JobRequest::IsOnline(h, _) => h.0.cancel(),
|
|
|
|
JobRequest::Refresh(_, h, _) => h.0.cancel(),
|
|
|
|
JobRequest::SetFlags(_, h, _) => h.0.cancel(),
|
2020-08-26 20:00:25 +03:00
|
|
|
JobRequest::SaveMessage { handle, .. } => handle.0.cancel(),
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::CopyTo(_, h, _) => h.0.cancel(),
|
|
|
|
JobRequest::DeleteMessages(_, h, _) => h.0.cancel(),
|
2020-08-25 22:56:45 +03:00
|
|
|
JobRequest::CreateMailbox { handle, .. } => handle.0.cancel(),
|
|
|
|
JobRequest::DeleteMailbox { handle, .. } => handle.0.cancel(),
|
2020-07-29 14:27:43 +03:00
|
|
|
//JobRequest::RenameMailbox,
|
|
|
|
JobRequest::Search(h) => h.0.cancel(),
|
|
|
|
JobRequest::AsBytes(h) => h.0.cancel(),
|
|
|
|
JobRequest::SetMailboxPermissions(_, h, _) => {
|
|
|
|
h.0.cancel();
|
|
|
|
}
|
|
|
|
JobRequest::SetMailboxSubscription(_, h, _) => {
|
|
|
|
h.0.cancel();
|
|
|
|
}
|
2020-08-25 21:12:28 +03:00
|
|
|
JobRequest::Watch { handle, .. } => handle.0.cancel(),
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::SendMessage => {}
|
|
|
|
JobRequest::SendMessageBackground(h, _) => {
|
|
|
|
h.0.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 15:39:33 +03:00
|
|
|
impl core::fmt::Debug for JobRequest {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
|
|
|
match self {
|
2020-07-16 22:54:50 +03:00
|
|
|
JobRequest::Generic { name, .. } => write!(f, "JobRequest::Generic({})", name),
|
2020-07-15 11:02:53 +03:00
|
|
|
JobRequest::Mailboxes(_, _) => write!(f, "JobRequest::Mailboxes"),
|
2020-07-17 23:30:02 +03:00
|
|
|
JobRequest::Fetch(hash, _, _) => write!(f, "JobRequest::Fetch({})", hash),
|
2020-07-15 11:02:53 +03:00
|
|
|
JobRequest::IsOnline(_, _) => write!(f, "JobRequest::IsOnline"),
|
|
|
|
JobRequest::Refresh(_, _, _) => write!(f, "JobRequest::Refresh"),
|
|
|
|
JobRequest::SetFlags(_, _, _) => write!(f, "JobRequest::SetFlags"),
|
2020-08-26 20:00:25 +03:00
|
|
|
JobRequest::SaveMessage { .. } => write!(f, "JobRequest::SaveMessage"),
|
2020-07-15 11:02:53 +03:00
|
|
|
JobRequest::CopyTo(_, _, _) => write!(f, "JobRequest::CopyTo"),
|
2020-07-24 20:17:06 +03:00
|
|
|
JobRequest::DeleteMessages(_, _, _) => write!(f, "JobRequest::DeleteMessages"),
|
2020-08-25 22:56:45 +03:00
|
|
|
JobRequest::CreateMailbox { .. } => write!(f, "JobRequest::CreateMailbox"),
|
|
|
|
JobRequest::DeleteMailbox { mailbox_hash, .. } => {
|
|
|
|
write!(f, "JobRequest::DeleteMailbox({})", mailbox_hash)
|
|
|
|
}
|
2020-06-29 00:18:24 +03:00
|
|
|
//JobRequest::RenameMailbox,
|
2020-07-15 11:02:53 +03:00
|
|
|
JobRequest::Search(_) => write!(f, "JobRequest::Search"),
|
|
|
|
JobRequest::AsBytes(_) => write!(f, "JobRequest::AsBytes"),
|
|
|
|
JobRequest::SetMailboxPermissions(_, _, _) => {
|
2020-07-05 15:28:55 +03:00
|
|
|
write!(f, "JobRequest::SetMailboxPermissions")
|
2020-06-29 00:18:24 +03:00
|
|
|
}
|
2020-07-15 11:02:53 +03:00
|
|
|
JobRequest::SetMailboxSubscription(_, _, _) => {
|
2020-07-05 15:28:55 +03:00
|
|
|
write!(f, "JobRequest::SetMailboxSubscription")
|
2020-06-29 00:18:24 +03:00
|
|
|
}
|
2020-08-25 21:12:28 +03:00
|
|
|
JobRequest::Watch { .. } => write!(f, "JobRequest::Watch"),
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
JobRequest::SendMessage => write!(f, "JobRequest::SendMessage"),
|
|
|
|
JobRequest::SendMessageBackground(_, _) => {
|
|
|
|
write!(f, "JobRequest::SendMessageBackground")
|
|
|
|
}
|
2020-06-28 15:39:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:17:03 +03:00
|
|
|
impl core::fmt::Display for JobRequest {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
|
|
|
match self {
|
|
|
|
JobRequest::Generic { name, .. } => write!(f, "{}", name),
|
|
|
|
JobRequest::Mailboxes(_, _) => write!(f, "Get mailbox list"),
|
|
|
|
JobRequest::Fetch(_, _, _) => write!(f, "Mailbox fetch"),
|
|
|
|
JobRequest::IsOnline(_, _) => write!(f, "Online status check"),
|
|
|
|
JobRequest::Refresh(_, _, _) => write!(f, "Refresh mailbox"),
|
|
|
|
JobRequest::SetFlags(batch, _, _) => write!(
|
|
|
|
f,
|
|
|
|
"Set flags for {} message{}",
|
|
|
|
batch.len(),
|
|
|
|
if batch.len() == 1 { "" } else { "s" }
|
|
|
|
),
|
|
|
|
JobRequest::SaveMessage { .. } => write!(f, "Save message"),
|
|
|
|
JobRequest::CopyTo(_, _, _) => write!(f, "Copy message."),
|
|
|
|
JobRequest::DeleteMessages(batch, _, _) => write!(
|
|
|
|
f,
|
|
|
|
"Delete {} message{}",
|
|
|
|
batch.len(),
|
|
|
|
if batch.len() == 1 { "" } else { "s" }
|
|
|
|
),
|
|
|
|
JobRequest::CreateMailbox { path, .. } => write!(f, "Create mailbox {}", path),
|
|
|
|
JobRequest::DeleteMailbox { .. } => write!(f, "Delete mailbox"),
|
|
|
|
//JobRequest::RenameMailbox,
|
|
|
|
JobRequest::Search(_) => write!(f, "Search"),
|
|
|
|
JobRequest::AsBytes(_) => write!(f, "Message body fetch"),
|
|
|
|
JobRequest::SetMailboxPermissions(_, _, _) => write!(f, "Set mailbox permissions"),
|
|
|
|
JobRequest::SetMailboxSubscription(_, _, _) => write!(f, "Set mailbox subscription"),
|
|
|
|
JobRequest::Watch { .. } => write!(f, "Background watch"),
|
|
|
|
JobRequest::SendMessageBackground(_, _) | JobRequest::SendMessage => {
|
|
|
|
write!(f, "Sending message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 11:40:26 +03:00
|
|
|
impl JobRequest {
|
2020-09-16 13:17:03 +03:00
|
|
|
pub fn is_watch(&self) -> bool {
|
2020-07-05 13:22:48 +03:00
|
|
|
match self {
|
2020-08-25 21:12:28 +03:00
|
|
|
JobRequest::Watch { .. } => true,
|
2020-07-05 13:22:48 +03:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:17:03 +03:00
|
|
|
pub fn is_fetch(&self, mailbox_hash: MailboxHash) -> bool {
|
2020-06-30 11:40:26 +03:00
|
|
|
match self {
|
2020-07-17 23:30:02 +03:00
|
|
|
JobRequest::Fetch(h, _, _) if *h == mailbox_hash => true,
|
2020-06-30 11:40:26 +03:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:17:03 +03:00
|
|
|
pub fn is_online(&self) -> bool {
|
2020-06-30 11:40:26 +03:00
|
|
|
match self {
|
2020-07-15 11:02:53 +03:00
|
|
|
JobRequest::IsOnline(_, _) => true,
|
2020-06-30 11:40:26 +03:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 09:06:42 +02:00
|
|
|
impl Drop for Account {
|
|
|
|
fn drop(&mut self) {
|
2019-11-22 13:59:00 +02:00
|
|
|
if let Ok(data_dir) = xdg::BaseDirectories::with_profile("meli", &self.name) {
|
|
|
|
if let Ok(data) = data_dir.place_data_file("addressbook") {
|
|
|
|
/* place result in cache directory */
|
|
|
|
let f = match fs::File::create(data) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2019-11-24 17:00:55 +02:00
|
|
|
let metadata = f.metadata().unwrap();
|
|
|
|
let mut permissions = metadata.permissions();
|
|
|
|
|
|
|
|
permissions.set_mode(0o600); // Read/write for owner only.
|
|
|
|
f.set_permissions(permissions).unwrap();
|
2019-11-22 13:59:00 +02:00
|
|
|
let writer = io::BufWriter::new(f);
|
|
|
|
if let Err(err) = serde_json::to_writer(writer, &self.address_book) {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
return;
|
|
|
|
};
|
2019-02-15 09:06:42 +02:00
|
|
|
};
|
2020-02-28 09:16:50 +02:00
|
|
|
/*
|
2019-11-22 13:59:00 +02:00
|
|
|
if let Ok(data) = data_dir.place_data_file("mailbox") {
|
|
|
|
/* place result in cache directory */
|
|
|
|
let f = match fs::File::create(data) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2019-11-24 17:00:55 +02:00
|
|
|
let metadata = f.metadata().unwrap();
|
|
|
|
let mut permissions = metadata.permissions();
|
|
|
|
|
|
|
|
permissions.set_mode(0o600); // Read/write for owner only.
|
|
|
|
f.set_permissions(permissions).unwrap();
|
2019-11-22 13:59:00 +02:00
|
|
|
let writer = io::BufWriter::new(f);
|
2020-02-10 02:11:07 +02:00
|
|
|
if let Err(err) = bincode::serialize_into(writer, &self.collection) {
|
2019-11-22 13:59:00 +02:00
|
|
|
eprintln!("{}", err);
|
|
|
|
};
|
2019-05-14 21:47:47 +03:00
|
|
|
};
|
2020-02-28 09:16:50 +02:00
|
|
|
*/
|
2019-11-22 13:59:00 +02:00
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 23:42:31 +02:00
|
|
|
#[derive(Serialize, Debug, Clone, Default)]
|
2020-02-26 10:54:10 +02:00
|
|
|
pub struct MailboxNode {
|
|
|
|
pub hash: MailboxHash,
|
2020-02-08 23:42:31 +02:00
|
|
|
pub depth: usize,
|
2020-02-26 10:54:10 +02:00
|
|
|
pub children: Vec<MailboxNode>,
|
2019-05-07 21:52:11 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 18:08:14 +03:00
|
|
|
impl Account {
|
2019-09-20 09:10:33 +03:00
|
|
|
pub fn new(
|
2020-06-08 22:08:10 +03:00
|
|
|
hash: AccountHash,
|
2019-09-20 09:10:33 +03:00
|
|
|
name: String,
|
2019-11-11 18:01:01 +02:00
|
|
|
mut settings: AccountConf,
|
2019-09-20 09:10:33 +03:00
|
|
|
map: &Backends,
|
2020-06-27 21:40:46 +03:00
|
|
|
job_executor: Arc<JobExecutor>,
|
2019-12-12 01:01:11 +02:00
|
|
|
sender: Sender<ThreadEvent>,
|
2020-08-20 01:55:24 +03:00
|
|
|
event_consumer: BackendEventConsumer,
|
2019-11-16 00:33:22 +02:00
|
|
|
) -> Result<Self> {
|
2019-09-06 12:48:17 +03:00
|
|
|
let s = settings.clone();
|
2019-10-24 20:30:17 +03:00
|
|
|
let backend = map.get(settings.account().format())(
|
2019-09-06 12:48:17 +03:00
|
|
|
settings.account(),
|
|
|
|
Box::new(move |path: &str| {
|
2020-02-26 10:54:10 +02:00
|
|
|
s.account.subscribed_mailboxes.is_empty()
|
|
|
|
|| (s.mailbox_confs.contains_key(path)
|
|
|
|
&& s.mailbox_confs[path].mailbox_conf().subscribe.is_true())
|
2019-11-23 17:56:38 +02:00
|
|
|
|| s.account
|
2020-02-26 10:54:10 +02:00
|
|
|
.subscribed_mailboxes
|
2019-11-23 17:56:38 +02:00
|
|
|
.iter()
|
|
|
|
.any(|m| path.matches_glob(m))
|
2019-09-06 12:48:17 +03:00
|
|
|
}),
|
2020-08-20 01:55:24 +03:00
|
|
|
event_consumer,
|
2019-11-16 00:33:22 +02:00
|
|
|
)?;
|
2019-10-24 20:30:17 +03:00
|
|
|
|
|
|
|
let data_dir = xdg::BaseDirectories::with_profile("meli", &name).unwrap();
|
2019-11-27 01:39:06 +02:00
|
|
|
let mut address_book = AddressBook::with_account(&settings.account());
|
|
|
|
|
|
|
|
if let Ok(data) = data_dir.place_data_file("addressbook") {
|
2019-10-24 20:30:17 +03:00
|
|
|
if data.exists() {
|
|
|
|
let reader = io::BufReader::new(fs::File::open(data).unwrap());
|
|
|
|
let result: result::Result<AddressBook, _> = serde_json::from_reader(reader);
|
|
|
|
if let Ok(data_t) = result {
|
2019-11-27 01:39:06 +02:00
|
|
|
for (id, c) in data_t.cards {
|
|
|
|
if !address_book.card_exists(id) && !c.external_resource() {
|
|
|
|
address_book.add_card(c);
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 20:30:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-11-27 01:39:06 +02:00
|
|
|
|
2020-08-06 19:45:08 +03:00
|
|
|
if ["imap", "jmap", "notmuch"].contains(&settings.account().format()) {
|
2020-07-16 23:57:00 +03:00
|
|
|
settings.conf.search_backend = crate::conf::SearchBackend::None;
|
2019-11-11 18:01:01 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 21:40:46 +03:00
|
|
|
let mut active_jobs = HashMap::default();
|
2020-07-24 21:48:29 +03:00
|
|
|
let mut active_job_instants = BTreeMap::default();
|
2020-08-20 17:37:19 +03:00
|
|
|
if let Ok(mailboxes_job) = backend.mailboxes() {
|
|
|
|
if let Ok(online_job) = backend.is_online() {
|
|
|
|
let (rcvr, handle, job_id) = if backend.capabilities().is_async {
|
|
|
|
job_executor.spawn_specialized(online_job.then(|_| mailboxes_job))
|
|
|
|
} else {
|
|
|
|
job_executor.spawn_blocking(online_job.then(|_| mailboxes_job))
|
|
|
|
};
|
|
|
|
active_jobs.insert(job_id, JobRequest::Mailboxes(handle, rcvr));
|
|
|
|
active_job_instants.insert(std::time::Instant::now(), job_id);
|
2020-09-10 21:01:40 +03:00
|
|
|
sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::NewJob(job_id),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
2020-06-30 11:40:26 +03:00
|
|
|
}
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
2020-08-20 17:37:19 +03:00
|
|
|
Ok(Account {
|
2020-06-08 22:08:10 +03:00
|
|
|
hash,
|
2019-10-24 20:30:17 +03:00
|
|
|
name,
|
2020-07-25 17:53:04 +03:00
|
|
|
is_online: if !backend.capabilities().is_remote {
|
2020-07-08 00:26:40 +03:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(MeliError::new("Attempting connection."))
|
|
|
|
},
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_entries: Default::default(),
|
|
|
|
mailboxes_order: Default::default(),
|
2019-10-24 20:30:17 +03:00
|
|
|
tree: Default::default(),
|
|
|
|
address_book,
|
2020-02-26 10:54:10 +02:00
|
|
|
sent_mailbox: Default::default(),
|
2019-10-24 20:30:17 +03:00
|
|
|
collection: Default::default(),
|
|
|
|
settings,
|
2019-12-12 01:01:11 +02:00
|
|
|
sender,
|
2020-06-27 21:40:46 +03:00
|
|
|
job_executor,
|
|
|
|
active_jobs,
|
2020-07-24 21:48:29 +03:00
|
|
|
active_job_instants,
|
2019-10-24 20:30:17 +03:00
|
|
|
event_queue: VecDeque::with_capacity(8),
|
2020-07-25 17:53:04 +03:00
|
|
|
backend_capabilities: backend.capabilities(),
|
2020-07-05 19:17:28 +03:00
|
|
|
backend: Arc::new(RwLock::new(backend)),
|
2020-08-20 17:37:19 +03:00
|
|
|
})
|
2019-10-24 20:30:17 +03:00
|
|
|
}
|
2019-11-12 22:14:44 +02:00
|
|
|
|
2020-08-20 17:37:19 +03:00
|
|
|
fn init(&mut self, mut ref_mailboxes: HashMap<MailboxHash, Mailbox>) -> Result<()> {
|
2020-08-01 12:36:47 +03:00
|
|
|
self.backend_capabilities = self.backend.read().unwrap().capabilities();
|
2020-08-17 15:31:30 +03:00
|
|
|
let mut mailbox_entries: IndexMap<MailboxHash, MailboxEntry> =
|
|
|
|
IndexMap::with_capacity_and_hasher(ref_mailboxes.len(), Default::default());
|
2020-02-26 10:54:10 +02:00
|
|
|
let mut mailboxes_order: Vec<MailboxHash> = Vec::with_capacity(ref_mailboxes.len());
|
|
|
|
|
|
|
|
let mut sent_mailbox = None;
|
2020-05-19 15:00:26 +03:00
|
|
|
|
|
|
|
/* Keep track of which mailbox config values we encounter in the actual mailboxes returned
|
|
|
|
* by the backend. For each of the actual mailboxes, delete the key from the hash set. If
|
|
|
|
* any are left, they are misconfigurations (eg misspelling) and a warning is shown to the
|
|
|
|
* user */
|
|
|
|
let mut mailbox_conf_hash_set = self
|
|
|
|
.settings
|
|
|
|
.mailbox_confs
|
|
|
|
.keys()
|
|
|
|
.cloned()
|
|
|
|
.collect::<HashSet<String>>();
|
2020-02-26 10:54:10 +02:00
|
|
|
for f in ref_mailboxes.values_mut() {
|
|
|
|
if let Some(conf) = self.settings.mailbox_confs.get_mut(f.path()) {
|
2020-05-19 15:00:26 +03:00
|
|
|
mailbox_conf_hash_set.remove(f.path());
|
2020-02-26 10:54:10 +02:00
|
|
|
conf.mailbox_conf.usage = if f.special_usage() != SpecialUsageMailbox::Normal {
|
2019-12-17 14:12:41 +02:00
|
|
|
Some(f.special_usage())
|
|
|
|
} else {
|
|
|
|
let tmp = SpecialUsageMailbox::detect_usage(f.name());
|
|
|
|
if tmp != Some(SpecialUsageMailbox::Normal) && tmp != None {
|
|
|
|
let _ = f.set_special_usage(tmp.unwrap());
|
|
|
|
}
|
|
|
|
tmp
|
|
|
|
};
|
2020-02-26 10:54:10 +02:00
|
|
|
match conf.mailbox_conf.usage {
|
2019-12-11 00:15:36 +02:00
|
|
|
Some(SpecialUsageMailbox::Sent) => {
|
2020-02-26 10:54:10 +02:00
|
|
|
sent_mailbox = Some(f.hash());
|
2019-11-23 17:56:38 +02:00
|
|
|
}
|
2019-12-17 14:12:41 +02:00
|
|
|
None => {
|
|
|
|
if f.special_usage() == SpecialUsageMailbox::Sent {
|
2020-02-26 10:54:10 +02:00
|
|
|
sent_mailbox = Some(f.hash());
|
2019-12-17 14:12:41 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 17:56:38 +02:00
|
|
|
_ => {}
|
2019-08-23 21:58:41 +03:00
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_entries.insert(
|
2020-02-10 02:11:07 +02:00
|
|
|
f.hash(),
|
2020-02-26 10:54:10 +02:00
|
|
|
MailboxEntry {
|
|
|
|
ref_mailbox: f.clone(),
|
2020-02-10 02:11:07 +02:00
|
|
|
name: f.path().to_string(),
|
|
|
|
status: MailboxStatus::None,
|
|
|
|
conf: conf.clone(),
|
|
|
|
},
|
|
|
|
);
|
2019-11-23 17:56:38 +02:00
|
|
|
} else {
|
2020-02-26 10:54:10 +02:00
|
|
|
let mut new = FileMailboxConf::default();
|
|
|
|
new.mailbox_conf.usage = if f.special_usage() != SpecialUsageMailbox::Normal {
|
2019-12-17 14:12:41 +02:00
|
|
|
Some(f.special_usage())
|
|
|
|
} else {
|
|
|
|
let tmp = SpecialUsageMailbox::detect_usage(f.name());
|
|
|
|
if tmp != Some(SpecialUsageMailbox::Normal) && tmp != None {
|
|
|
|
let _ = f.set_special_usage(tmp.unwrap());
|
|
|
|
}
|
|
|
|
tmp
|
|
|
|
};
|
2020-02-26 10:54:10 +02:00
|
|
|
if new.mailbox_conf.usage == Some(SpecialUsageMailbox::Sent) {
|
|
|
|
sent_mailbox = Some(f.hash());
|
2020-02-12 18:56:05 +02:00
|
|
|
}
|
2019-12-17 14:12:41 +02:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_entries.insert(
|
2020-02-10 02:11:07 +02:00
|
|
|
f.hash(),
|
2020-02-26 10:54:10 +02:00
|
|
|
MailboxEntry {
|
|
|
|
ref_mailbox: f.clone(),
|
2020-02-10 02:11:07 +02:00
|
|
|
name: f.path().to_string(),
|
|
|
|
status: MailboxStatus::None,
|
|
|
|
conf: new,
|
|
|
|
},
|
|
|
|
);
|
2019-08-23 21:58:41 +03:00
|
|
|
}
|
2019-04-10 18:58:09 +03:00
|
|
|
}
|
2019-05-13 21:08:18 +03:00
|
|
|
|
2020-05-19 15:00:26 +03:00
|
|
|
for missing_mailbox in &mailbox_conf_hash_set {
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Account `{}` mailbox `{}` configured but not present in account's mailboxes. Is it misspelled?",
|
|
|
|
&self.name, missing_mailbox,
|
|
|
|
),
|
|
|
|
melib::WARN,
|
|
|
|
);
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::DisplayMessage(format!(
|
|
|
|
"Account `{}` mailbox `{}` configured but not present in account's mailboxes. Is it misspelled?",
|
|
|
|
&self.name, missing_mailbox,
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
if !mailbox_conf_hash_set.is_empty() {
|
|
|
|
let mut mailbox_comma_sep_list_string = mailbox_entries
|
|
|
|
.values()
|
|
|
|
.map(|e| e.name.as_str())
|
|
|
|
.fold(String::new(), |mut acc, el| {
|
|
|
|
acc.push('`');
|
|
|
|
acc.push_str(el);
|
|
|
|
acc.push('`');
|
|
|
|
acc.push_str(", ");
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
mailbox_comma_sep_list_string.drain(mailbox_comma_sep_list_string.len() - 2..);
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Account `{}` has the following mailboxes: [{}]",
|
|
|
|
&self.name, mailbox_comma_sep_list_string,
|
|
|
|
),
|
|
|
|
melib::WARN,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
let mut tree: Vec<MailboxNode> = Vec::new();
|
|
|
|
for (h, f) in ref_mailboxes.iter() {
|
2020-02-08 23:45:49 +02:00
|
|
|
if !f.is_subscribed() {
|
2020-02-26 10:54:10 +02:00
|
|
|
/* Skip unsubscribed mailbox */
|
2019-08-23 21:32:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_entries.entry(*h).and_modify(|entry| {
|
2020-07-24 22:07:43 +03:00
|
|
|
if entry.conf.mailbox_conf.autoload
|
|
|
|
|| entry.ref_mailbox.special_usage() == SpecialUsageMailbox::Inbox
|
|
|
|
{
|
2020-08-07 00:39:17 +03:00
|
|
|
let total = entry.ref_mailbox.count().ok().unwrap_or((0, 0)).1;
|
|
|
|
entry.status = MailboxStatus::Parsing(0, total);
|
2020-08-20 17:37:19 +03:00
|
|
|
if let Ok(mailbox_job) = self.backend.write().unwrap().fetch(*h) {
|
|
|
|
let mailbox_job = mailbox_job.into_future();
|
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(mailbox_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(mailbox_job)
|
|
|
|
};
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::NewJob(job_id),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
self.active_jobs
|
|
|
|
.insert(job_id, JobRequest::Fetch(*h, handle, rcvr));
|
|
|
|
self.active_job_instants
|
|
|
|
.insert(std::time::Instant::now(), job_id);
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
2020-03-02 12:06:19 +02:00
|
|
|
}
|
2020-02-10 02:11:07 +02:00
|
|
|
});
|
2020-03-12 09:47:39 +02:00
|
|
|
self.collection.new_mailbox(*h);
|
2017-09-16 15:05:28 +03:00
|
|
|
}
|
2019-08-23 21:32:32 +03:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
build_mailboxes_order(&mut tree, &mailbox_entries, &mut mailboxes_order);
|
|
|
|
self.mailboxes_order = mailboxes_order;
|
|
|
|
self.mailbox_entries = mailbox_entries;
|
2019-10-24 20:30:17 +03:00
|
|
|
self.tree = tree;
|
2020-02-26 10:54:10 +02:00
|
|
|
self.sent_mailbox = sent_mailbox;
|
2020-06-22 17:32:51 +03:00
|
|
|
Ok(())
|
2017-09-14 18:08:14 +03:00
|
|
|
}
|
2019-10-24 20:30:17 +03:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
pub fn reload(&mut self, event: RefreshEvent, mailbox_hash: MailboxHash) -> Option<UIEvent> {
|
2020-09-11 16:58:56 +03:00
|
|
|
if !self.mailbox_entries[&mailbox_hash].status.is_available()
|
|
|
|
&& !self.mailbox_entries[&mailbox_hash].status.is_parsing()
|
|
|
|
{
|
2020-02-26 10:54:10 +02:00
|
|
|
self.event_queue.push_back((mailbox_hash, event));
|
2019-05-26 02:41:29 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-09-23 19:55:29 +03:00
|
|
|
{
|
2020-02-26 10:54:10 +02:00
|
|
|
//let mailbox: &mut Mailbox = self.mailboxes[idx].as_mut().unwrap().as_mut().unwrap();
|
2020-08-26 19:13:18 +03:00
|
|
|
match event.kind {
|
2018-09-23 19:55:29 +03:00
|
|
|
RefreshEventKind::Update(old_hash, envelope) => {
|
2020-08-26 19:13:18 +03:00
|
|
|
if !self.collection.contains_key(&old_hash) {
|
|
|
|
return self.reload(
|
|
|
|
RefreshEvent {
|
|
|
|
account_hash: event.account_hash,
|
|
|
|
mailbox_hash: event.mailbox_hash,
|
|
|
|
kind: RefreshEventKind::Create(envelope),
|
|
|
|
},
|
|
|
|
mailbox_hash,
|
|
|
|
);
|
|
|
|
}
|
2019-11-12 13:09:43 +02:00
|
|
|
#[cfg(feature = "sqlite3")]
|
|
|
|
{
|
2020-08-26 00:00:38 +03:00
|
|
|
match crate::sqlite3::remove(old_hash).map(|_| {
|
|
|
|
crate::sqlite3::insert(
|
|
|
|
(*envelope).clone(),
|
|
|
|
self.backend.clone(),
|
|
|
|
self.name.clone(),
|
|
|
|
)
|
2019-11-12 13:09:43 +02:00
|
|
|
}) {
|
2020-08-26 00:00:38 +03:00
|
|
|
Err(err) => {
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Failed to update envelope {} in cache: {}",
|
|
|
|
envelope.message_id_display(),
|
|
|
|
err.to_string()
|
|
|
|
),
|
|
|
|
melib::ERROR,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(job) => {
|
|
|
|
let (channel, handle, job_id) =
|
|
|
|
self.job_executor.spawn_blocking(job);
|
|
|
|
self.insert_job(
|
|
|
|
job_id,
|
|
|
|
JobRequest::Generic {
|
|
|
|
name: format!(
|
|
|
|
"Update envelope {} in sqlite3 cache",
|
|
|
|
envelope.message_id_display()
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
handle,
|
|
|
|
channel,
|
2020-08-26 19:15:31 +03:00
|
|
|
logging_level: melib::LoggingLevel::TRACE,
|
2020-08-26 00:00:38 +03:00
|
|
|
on_finish: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-11-12 13:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
self.collection.update(old_hash, *envelope, mailbox_hash);
|
2019-04-01 23:53:06 +03:00
|
|
|
return Some(EnvelopeUpdate(old_hash));
|
2018-10-14 19:49:16 +03:00
|
|
|
}
|
2020-02-28 09:09:43 +02:00
|
|
|
RefreshEventKind::NewFlags(env_hash, (flags, tags)) => {
|
2020-08-26 19:13:18 +03:00
|
|
|
if !self.collection.contains_key(&env_hash) {
|
|
|
|
return None;
|
|
|
|
}
|
2020-08-26 00:00:38 +03:00
|
|
|
self.collection
|
|
|
|
.envelopes
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(env_hash)
|
|
|
|
.and_modify(|entry| {
|
|
|
|
entry.labels_mut().clear();
|
|
|
|
entry
|
|
|
|
.labels_mut()
|
|
|
|
.extend(tags.into_iter().map(|h| tag_hash!(h)));
|
|
|
|
entry.set_flags(flags);
|
|
|
|
});
|
2020-02-28 09:09:43 +02:00
|
|
|
#[cfg(feature = "sqlite3")]
|
|
|
|
{
|
2020-08-26 00:00:38 +03:00
|
|
|
match crate::sqlite3::remove(env_hash).map(|_| {
|
|
|
|
crate::sqlite3::insert(
|
|
|
|
self.collection.envelopes.read().unwrap()[&env_hash].clone(),
|
|
|
|
self.backend.clone(),
|
|
|
|
self.name.clone(),
|
|
|
|
)
|
2020-02-28 09:09:43 +02:00
|
|
|
}) {
|
2020-08-26 00:00:38 +03:00
|
|
|
Ok(job) => {
|
|
|
|
let (channel, handle, job_id) =
|
|
|
|
self.job_executor.spawn_blocking(job);
|
|
|
|
self.insert_job(
|
|
|
|
job_id,
|
|
|
|
JobRequest::Generic {
|
|
|
|
name: format!(
|
|
|
|
"Update envelope {} in sqlite3 cache",
|
|
|
|
self.collection.envelopes.read().unwrap()[&env_hash]
|
|
|
|
.message_id_display()
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
handle,
|
|
|
|
channel,
|
2020-08-26 19:15:31 +03:00
|
|
|
logging_level: melib::LoggingLevel::TRACE,
|
2020-08-26 00:00:38 +03:00
|
|
|
on_finish: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Failed to update envelope {} in cache: {}",
|
|
|
|
self.collection.envelopes.read().unwrap()[&env_hash]
|
|
|
|
.message_id_display(),
|
|
|
|
err.to_string()
|
|
|
|
),
|
|
|
|
melib::ERROR,
|
|
|
|
);
|
|
|
|
}
|
2020-02-28 09:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 12:27:10 +03:00
|
|
|
self.collection.update_flags(env_hash, mailbox_hash);
|
2020-02-28 09:09:43 +02:00
|
|
|
return Some(EnvelopeUpdate(env_hash));
|
|
|
|
}
|
2018-10-14 19:49:16 +03:00
|
|
|
RefreshEventKind::Rename(old_hash, new_hash) => {
|
2019-05-01 19:20:33 +03:00
|
|
|
debug!("rename {} to {}", old_hash, new_hash);
|
2020-06-09 15:38:13 +03:00
|
|
|
if !self.collection.rename(old_hash, new_hash, mailbox_hash) {
|
|
|
|
return Some(EnvelopeRename(old_hash, new_hash));
|
|
|
|
}
|
2019-11-12 13:09:43 +02:00
|
|
|
#[cfg(feature = "sqlite3")]
|
|
|
|
{
|
2020-08-26 00:00:38 +03:00
|
|
|
match crate::sqlite3::remove(old_hash).map(|_| {
|
|
|
|
crate::sqlite3::insert(
|
|
|
|
self.collection.envelopes.read().unwrap()[&new_hash].clone(),
|
|
|
|
self.backend.clone(),
|
|
|
|
self.name.clone(),
|
|
|
|
)
|
2019-11-12 13:09:43 +02:00
|
|
|
}) {
|
2020-08-26 00:00:38 +03:00
|
|
|
Err(err) => {
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Failed to update envelope {} in cache: {}",
|
|
|
|
&self.collection.envelopes.read().unwrap()[&new_hash]
|
|
|
|
.message_id_display(),
|
|
|
|
err.to_string()
|
|
|
|
),
|
|
|
|
melib::ERROR,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(job) => {
|
|
|
|
let (channel, handle, job_id) =
|
|
|
|
self.job_executor.spawn_blocking(job);
|
|
|
|
self.insert_job(
|
|
|
|
job_id,
|
|
|
|
JobRequest::Generic {
|
|
|
|
name: format!(
|
|
|
|
"Update envelope {} in sqlite3 cache",
|
|
|
|
self.collection.envelopes.read().unwrap()[&new_hash]
|
|
|
|
.message_id_display()
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
handle,
|
|
|
|
channel,
|
2020-08-26 19:15:31 +03:00
|
|
|
logging_level: melib::LoggingLevel::TRACE,
|
2020-08-26 00:00:38 +03:00
|
|
|
on_finish: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-11-12 13:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 00:27:40 +03:00
|
|
|
return Some(EnvelopeRename(old_hash, new_hash));
|
2018-09-23 19:55:29 +03:00
|
|
|
}
|
|
|
|
RefreshEventKind::Create(envelope) => {
|
2019-05-26 02:34:03 +03:00
|
|
|
let env_hash = envelope.hash();
|
2019-11-02 12:14:31 +02:00
|
|
|
if self.collection.contains_key(&env_hash)
|
2020-07-21 07:53:38 +03:00
|
|
|
&& self
|
|
|
|
.collection
|
|
|
|
.get_mailbox(mailbox_hash)
|
|
|
|
.contains(&env_hash)
|
2019-09-11 17:57:55 +03:00
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
2019-11-02 12:14:31 +02:00
|
|
|
let (is_seen, is_draft) =
|
|
|
|
{ (envelope.is_seen(), envelope.flags().contains(Flag::DRAFT)) };
|
|
|
|
let (subject, from) = {
|
|
|
|
(
|
|
|
|
envelope.subject().into_owned(),
|
|
|
|
envelope.field_from_to_string(),
|
|
|
|
)
|
|
|
|
};
|
2019-11-05 16:42:55 +02:00
|
|
|
#[cfg(feature = "sqlite3")]
|
|
|
|
{
|
2020-08-26 00:00:38 +03:00
|
|
|
let (channel, handle, job_id) =
|
|
|
|
self.job_executor.spawn_blocking(crate::sqlite3::insert(
|
|
|
|
(*envelope).clone(),
|
|
|
|
self.backend.clone(),
|
|
|
|
self.name.clone(),
|
|
|
|
));
|
|
|
|
self.insert_job(
|
|
|
|
job_id,
|
|
|
|
JobRequest::Generic {
|
|
|
|
name: format!(
|
|
|
|
"Update envelope {} in sqlite3 cache",
|
|
|
|
envelope.message_id_display()
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
handle,
|
|
|
|
channel,
|
2020-08-26 19:15:31 +03:00
|
|
|
logging_level: melib::LoggingLevel::TRACE,
|
2020-08-26 00:00:38 +03:00
|
|
|
on_finish: None,
|
|
|
|
},
|
|
|
|
);
|
2019-11-05 16:42:55 +02:00
|
|
|
}
|
2020-03-12 09:47:39 +02:00
|
|
|
|
|
|
|
if self.collection.insert(*envelope, mailbox_hash) {
|
|
|
|
/* is a duplicate */
|
|
|
|
return None;
|
|
|
|
}
|
2019-05-26 02:34:03 +03:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
if self.mailbox_entries[&mailbox_hash]
|
2020-02-10 02:11:07 +02:00
|
|
|
.conf
|
2020-02-26 10:54:10 +02:00
|
|
|
.mailbox_conf
|
2020-02-10 02:11:07 +02:00
|
|
|
.ignore
|
|
|
|
.is_true()
|
|
|
|
{
|
2020-08-17 15:31:30 +03:00
|
|
|
return Some(UIEvent::MailboxUpdate((self.hash, mailbox_hash)));
|
2019-05-13 21:08:18 +03:00
|
|
|
}
|
2019-11-02 12:14:31 +02:00
|
|
|
|
2020-01-18 01:48:29 +02:00
|
|
|
let thread = {
|
|
|
|
let thread_hash = self.collection.get_env(env_hash).thread();
|
2020-07-21 07:53:38 +03:00
|
|
|
self.collection.get_threads(mailbox_hash).find_group(
|
|
|
|
self.collection.get_threads(mailbox_hash)[&thread_hash].group,
|
|
|
|
)
|
2019-11-02 12:14:31 +02:00
|
|
|
};
|
2020-07-21 07:53:38 +03:00
|
|
|
if self
|
|
|
|
.collection
|
|
|
|
.get_threads(mailbox_hash)
|
2020-01-18 02:46:59 +02:00
|
|
|
.thread_ref(thread)
|
|
|
|
.snoozed()
|
|
|
|
{
|
2020-08-17 15:31:30 +03:00
|
|
|
return Some(UIEvent::MailboxUpdate((self.hash, mailbox_hash)));
|
2019-05-15 20:58:59 +03:00
|
|
|
}
|
2019-11-02 12:14:31 +02:00
|
|
|
if is_seen || is_draft {
|
2020-08-17 15:31:30 +03:00
|
|
|
return Some(UIEvent::MailboxUpdate((self.hash, mailbox_hash)));
|
2019-09-27 13:29:42 +03:00
|
|
|
}
|
|
|
|
|
2018-09-23 19:55:29 +03:00
|
|
|
return Some(Notification(
|
2019-11-02 12:14:31 +02:00
|
|
|
Some(format!("new e-mail from: {}", from)),
|
2018-09-23 19:55:29 +03:00
|
|
|
format!(
|
2019-09-27 13:29:42 +03:00
|
|
|
"{}\n{} {}",
|
2019-11-02 12:14:31 +02:00
|
|
|
subject,
|
2019-03-02 21:14:03 +02:00
|
|
|
self.name,
|
2020-02-26 10:54:10 +02:00
|
|
|
self.mailbox_entries[&mailbox_hash].name()
|
2018-09-23 19:55:29 +03:00
|
|
|
),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::NewMail),
|
2018-09-23 19:55:29 +03:00
|
|
|
));
|
|
|
|
}
|
2020-06-23 17:23:42 +03:00
|
|
|
RefreshEventKind::Remove(env_hash) => {
|
2020-08-26 19:13:18 +03:00
|
|
|
if !self.collection.contains_key(&env_hash) {
|
|
|
|
return None;
|
|
|
|
}
|
2020-06-23 17:23:42 +03:00
|
|
|
let thread_hash = {
|
|
|
|
let thread_hash = self.collection.get_env(env_hash).thread();
|
2020-07-21 07:53:38 +03:00
|
|
|
self.collection.get_threads(mailbox_hash).find_group(
|
|
|
|
self.collection.get_threads(mailbox_hash)[&thread_hash].group,
|
|
|
|
)
|
2020-06-23 17:23:42 +03:00
|
|
|
};
|
2019-11-12 13:09:43 +02:00
|
|
|
#[cfg(feature = "sqlite3")]
|
|
|
|
{
|
|
|
|
let envelopes = self.collection.envelopes.read();
|
|
|
|
let envelopes = envelopes.unwrap();
|
2020-06-23 17:23:42 +03:00
|
|
|
if let Err(err) = crate::sqlite3::remove(env_hash) {
|
2019-11-12 13:09:43 +02:00
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Failed to remove envelope {} [{}] in cache: {}",
|
2020-06-23 17:23:42 +03:00
|
|
|
&envelopes[&env_hash].message_id_display(),
|
|
|
|
env_hash,
|
2019-11-12 13:09:43 +02:00
|
|
|
err.to_string()
|
|
|
|
),
|
|
|
|
melib::ERROR,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 17:23:42 +03:00
|
|
|
self.collection.remove(env_hash, mailbox_hash);
|
|
|
|
return Some(EnvelopeRemove(env_hash, thread_hash));
|
2018-09-23 19:55:29 +03:00
|
|
|
}
|
|
|
|
RefreshEventKind::Rescan => {
|
2020-08-20 17:37:19 +03:00
|
|
|
self.watch();
|
2018-09-23 19:55:29 +03:00
|
|
|
}
|
2020-07-05 17:22:06 +03:00
|
|
|
RefreshEventKind::Failure(err) => {
|
|
|
|
debug!("RefreshEvent Failure: {}", err.to_string());
|
2020-09-13 00:03:12 +03:00
|
|
|
while let Some((job_id, _)) =
|
|
|
|
self.active_jobs.iter().find(|(_, j)| j.is_watch())
|
|
|
|
{
|
|
|
|
let job_id = *job_id;
|
|
|
|
let j = self.active_jobs.remove(&job_id);
|
|
|
|
drop(j);
|
|
|
|
}
|
2019-12-12 01:01:11 +02:00
|
|
|
/*
|
2019-11-23 17:50:22 +02:00
|
|
|
context
|
|
|
|
.1
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{} watcher exited with error", &self.name)),
|
|
|
|
e.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2019-11-23 17:50:22 +02:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
2019-12-12 01:01:11 +02:00
|
|
|
*/
|
|
|
|
self.watch();
|
2020-07-05 17:22:06 +03:00
|
|
|
return Some(Notification(
|
2020-09-13 00:03:12 +03:00
|
|
|
Some("Account watch failed".into()),
|
2020-07-05 17:22:06 +03:00
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-07-05 17:22:06 +03:00
|
|
|
));
|
2019-08-14 22:59:46 +03:00
|
|
|
}
|
2018-09-05 16:08:11 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 15:10:19 +03:00
|
|
|
None
|
2018-08-11 19:19:30 +03:00
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
pub fn refresh(&mut self, mailbox_hash: MailboxHash) -> Result<()> {
|
2020-01-08 21:41:57 +02:00
|
|
|
if let Some(ref refresh_command) = self.settings.conf().refresh_command {
|
2020-05-28 16:27:02 +03:00
|
|
|
let child = std::process::Command::new("sh")
|
|
|
|
.args(&["-c", refresh_command])
|
2020-01-08 21:41:57 +02:00
|
|
|
.stdin(std::process::Stdio::null())
|
|
|
|
.stdout(std::process::Stdio::null())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()?;
|
2020-02-28 09:16:19 +02:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::DisplayMessage(format!("Running command {}", refresh_command)),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
2020-03-01 17:56:58 +02:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Fork(
|
|
|
|
crate::ForkType::Generic(child),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
2020-01-08 21:41:57 +02:00
|
|
|
return Ok(());
|
|
|
|
}
|
2020-09-10 21:01:40 +03:00
|
|
|
let refresh_job = self.backend.write().unwrap().refresh(mailbox_hash);
|
|
|
|
if let Ok(refresh_job) = refresh_job {
|
2020-08-20 17:37:19 +03:00
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(refresh_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(refresh_job)
|
|
|
|
};
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(job_id, JobRequest::Refresh(mailbox_hash, handle, rcvr));
|
2020-06-29 00:18:24 +03:00
|
|
|
}
|
2019-12-15 08:52:22 +02:00
|
|
|
Ok(())
|
2019-12-14 18:55:46 +02:00
|
|
|
}
|
2020-08-20 17:37:19 +03:00
|
|
|
|
2020-07-05 13:22:48 +03:00
|
|
|
pub fn watch(&mut self) {
|
2019-12-14 18:55:46 +02:00
|
|
|
if self.settings.account().manual_refresh {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-20 17:37:19 +03:00
|
|
|
if !self.active_jobs.values().any(|j| j.is_watch()) {
|
|
|
|
match self.backend.read().unwrap().watch() {
|
|
|
|
Ok(fut) => {
|
2020-08-25 21:12:28 +03:00
|
|
|
let (channel, handle, job_id) = if self.backend_capabilities.is_async {
|
2020-08-20 17:37:19 +03:00
|
|
|
self.job_executor.spawn_specialized(fut)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(fut)
|
|
|
|
};
|
2020-08-25 21:12:28 +03:00
|
|
|
self.active_jobs
|
|
|
|
.insert(job_id, JobRequest::Watch { channel, handle });
|
2020-07-05 13:22:48 +03:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::DisplayMessage(e.to_string()),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2019-09-11 17:57:55 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 18:06:35 +03:00
|
|
|
}
|
2019-07-28 18:52:45 +03:00
|
|
|
|
2018-07-11 18:58:57 +03:00
|
|
|
pub fn len(&self) -> usize {
|
2020-02-10 02:11:07 +02:00
|
|
|
self.tree.len()
|
2018-07-11 18:58:57 +03:00
|
|
|
}
|
2018-08-23 15:36:52 +03:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2020-02-10 02:11:07 +02:00
|
|
|
self.tree.is_empty()
|
2020-02-08 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
pub fn list_mailboxes(&self) -> Vec<MailboxNode> {
|
|
|
|
let mut ret = Vec::with_capacity(self.mailbox_entries.len());
|
|
|
|
fn rec(node: &MailboxNode, ret: &mut Vec<MailboxNode>) {
|
2020-02-08 23:42:31 +02:00
|
|
|
ret.push(node.clone());
|
|
|
|
for c in node.children.iter() {
|
|
|
|
rec(c, ret);
|
2018-08-19 14:54:32 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:42:31 +02:00
|
|
|
for node in &self.tree {
|
|
|
|
rec(node, &mut ret);
|
2019-11-23 17:56:38 +02:00
|
|
|
}
|
2020-02-08 23:42:31 +02:00
|
|
|
ret
|
2019-04-26 11:04:30 +03:00
|
|
|
}
|
2020-02-08 23:42:31 +02:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
pub fn mailboxes_order(&self) -> &Vec<MailboxHash> {
|
|
|
|
&self.mailboxes_order
|
2018-07-11 18:58:57 +03:00
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
pub fn name(&self) -> &str {
|
2018-07-11 23:12:25 +03:00
|
|
|
&self.name
|
|
|
|
}
|
2018-08-23 15:36:52 +03:00
|
|
|
|
2020-07-24 22:08:09 +03:00
|
|
|
pub fn hash(&self) -> AccountHash {
|
|
|
|
self.hash
|
|
|
|
}
|
|
|
|
|
2020-03-02 12:06:19 +02:00
|
|
|
pub fn load(&mut self, mailbox_hash: MailboxHash) -> result::Result<(), usize> {
|
2020-02-26 10:54:10 +02:00
|
|
|
if mailbox_hash == 0 {
|
2020-02-08 23:42:31 +02:00
|
|
|
return Err(0);
|
|
|
|
}
|
2020-08-20 17:37:19 +03:00
|
|
|
match self.mailbox_entries[&mailbox_hash].status {
|
|
|
|
MailboxStatus::Available | MailboxStatus::Parsing(_, _)
|
|
|
|
if self
|
|
|
|
.collection
|
|
|
|
.mailboxes
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.contains_key(&mailbox_hash) =>
|
2020-02-10 02:11:07 +02:00
|
|
|
{
|
2020-08-20 17:37:19 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
MailboxStatus::None => {
|
|
|
|
if !self.active_jobs.values().any(|j| j.is_fetch(mailbox_hash)) {
|
|
|
|
let mailbox_job = self.backend.write().unwrap().fetch(mailbox_hash);
|
|
|
|
match mailbox_job {
|
|
|
|
Ok(mailbox_job) => {
|
|
|
|
let mailbox_job = mailbox_job.into_future();
|
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(mailbox_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(mailbox_job)
|
|
|
|
};
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(job_id, JobRequest::Fetch(mailbox_hash, handle, rcvr));
|
2020-03-02 12:06:19 +02:00
|
|
|
}
|
2020-08-20 17:37:19 +03:00
|
|
|
Err(err) => {
|
2020-03-02 12:06:19 +02:00
|
|
|
self.mailbox_entries
|
|
|
|
.entry(mailbox_hash)
|
|
|
|
.and_modify(|entry| {
|
2020-07-13 18:49:27 +03:00
|
|
|
entry.status = MailboxStatus::Failed(err);
|
2020-03-02 12:06:19 +02:00
|
|
|
});
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StartupCheck(mailbox_hash)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2019-12-14 19:56:43 +02:00
|
|
|
}
|
2020-08-20 17:37:19 +03:00
|
|
|
}
|
|
|
|
self.collection.new_mailbox(mailbox_hash);
|
|
|
|
Err(0)
|
|
|
|
}
|
|
|
|
_ => Err(0),
|
2019-08-14 23:01:57 +03:00
|
|
|
}
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
2019-02-16 16:37:14 +02:00
|
|
|
|
2019-12-03 13:27:35 +02:00
|
|
|
pub fn save_special(
|
2020-06-30 11:40:26 +03:00
|
|
|
&mut self,
|
2019-12-03 13:27:35 +02:00
|
|
|
bytes: &[u8],
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_type: SpecialUsageMailbox,
|
2019-12-03 13:27:35 +02:00
|
|
|
flags: Flag,
|
2020-06-23 19:25:01 +03:00
|
|
|
) -> Result<MailboxHash> {
|
|
|
|
let mut saved_at: Option<MailboxHash> = None;
|
2020-02-26 10:54:10 +02:00
|
|
|
for mailbox in &[
|
|
|
|
self.special_use_mailbox(mailbox_type),
|
|
|
|
self.special_use_mailbox(SpecialUsageMailbox::Inbox),
|
|
|
|
self.special_use_mailbox(SpecialUsageMailbox::Normal),
|
2019-12-03 13:27:35 +02:00
|
|
|
] {
|
2020-02-26 10:54:10 +02:00
|
|
|
if mailbox.is_none() {
|
2019-12-03 13:27:35 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
let mailbox = mailbox.unwrap();
|
|
|
|
if let Err(e) = self.save(bytes, mailbox, Some(flags)) {
|
2019-12-03 13:27:35 +02:00
|
|
|
debug!("{:?} could not save msg", e);
|
|
|
|
melib::log(
|
2020-02-26 10:54:10 +02:00
|
|
|
format!(
|
|
|
|
"Could not save in '{}' mailbox: {}.",
|
|
|
|
mailbox,
|
|
|
|
e.to_string()
|
|
|
|
),
|
2019-12-03 13:27:35 +02:00
|
|
|
melib::ERROR,
|
|
|
|
);
|
|
|
|
} else {
|
2020-06-23 19:25:01 +03:00
|
|
|
saved_at = Some(mailbox);
|
2019-12-03 13:27:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:25:01 +03:00
|
|
|
if let Some(mailbox_hash) = saved_at {
|
|
|
|
Ok(mailbox_hash)
|
|
|
|
} else {
|
2019-12-03 13:27:35 +02:00
|
|
|
let file = crate::types::create_temp_file(bytes, None, None, false);
|
|
|
|
debug!("message saved in {}", file.path.display());
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Message was stored in {} so that you can restore it manually.",
|
|
|
|
file.path.display()
|
|
|
|
),
|
|
|
|
melib::INFO,
|
|
|
|
);
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Message was stored in {} so that you can restore it manually.",
|
|
|
|
file.path.display()
|
|
|
|
))
|
2020-02-26 10:54:10 +02:00
|
|
|
.set_summary("Could not save in any mailbox"));
|
2019-12-03 13:27:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 11:40:26 +03:00
|
|
|
pub fn save(
|
|
|
|
&mut self,
|
|
|
|
bytes: &[u8],
|
|
|
|
mailbox_hash: MailboxHash,
|
|
|
|
flags: Option<Flag>,
|
|
|
|
) -> Result<()> {
|
2019-07-18 20:16:51 +03:00
|
|
|
if self.settings.account.read_only() {
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Account {} is read-only.",
|
|
|
|
self.name.as_str()
|
|
|
|
)));
|
|
|
|
}
|
2020-06-30 11:40:26 +03:00
|
|
|
let job = self
|
|
|
|
.backend
|
2020-06-08 22:08:10 +03:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
2020-06-30 11:40:26 +03:00
|
|
|
.save(bytes.to_vec(), mailbox_hash, flags)?;
|
2020-09-16 13:17:26 +03:00
|
|
|
|
|
|
|
let (channel, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(job)
|
|
|
|
};
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(
|
2020-08-26 20:00:25 +03:00
|
|
|
job_id,
|
|
|
|
JobRequest::SaveMessage {
|
|
|
|
bytes: bytes.to_vec(),
|
|
|
|
mailbox_hash,
|
|
|
|
handle,
|
|
|
|
channel,
|
|
|
|
},
|
|
|
|
);
|
2020-06-30 11:40:26 +03:00
|
|
|
Ok(())
|
2019-04-10 18:57:09 +03:00
|
|
|
}
|
2019-05-26 02:34:03 +03:00
|
|
|
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
pub fn send(
|
|
|
|
&mut self,
|
|
|
|
message: String,
|
|
|
|
send_mail: crate::conf::composing::SendMail,
|
|
|
|
complete_in_background: bool,
|
|
|
|
) -> Result<Option<(JobId, JoinHandle, JobChannel<()>)>> {
|
|
|
|
use crate::conf::composing::SendMail;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
debug!(&send_mail);
|
|
|
|
match send_mail {
|
|
|
|
SendMail::ShellCommand(ref command) => {
|
|
|
|
if command.is_empty() {
|
|
|
|
return Err(MeliError::new(
|
|
|
|
"send_mail shell command configuration value is empty",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
let mut msmtp = Command::new("sh")
|
|
|
|
.args(&["-c", command])
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("Failed to start mailer command");
|
|
|
|
{
|
|
|
|
let stdin = msmtp.stdin.as_mut().expect("failed to open stdin");
|
|
|
|
stdin
|
|
|
|
.write_all(message.as_bytes())
|
|
|
|
.expect("Failed to write to stdin");
|
|
|
|
}
|
|
|
|
let output = msmtp.wait().expect("Failed to wait on mailer");
|
|
|
|
if output.success() {
|
|
|
|
melib::log("Message sent.", melib::LoggingLevel::TRACE);
|
|
|
|
} else {
|
|
|
|
let error_message = if let Some(exit_code) = output.code() {
|
|
|
|
format!(
|
|
|
|
"Could not send e-mail using `{}`: Process exited with {}",
|
|
|
|
command, exit_code
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"Could not send e-mail using `{}`: Process was killed by signal",
|
|
|
|
command
|
|
|
|
)
|
|
|
|
};
|
|
|
|
melib::log(&error_message, melib::LoggingLevel::ERROR);
|
|
|
|
return Err(
|
|
|
|
MeliError::new(error_message.clone()).set_summary("Message not sent.")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
#[cfg(feature = "smtp")]
|
|
|
|
SendMail::Smtp(conf) => {
|
|
|
|
let (chan, handle, job_id) = self.job_executor.spawn_specialized(async move {
|
|
|
|
let mut smtp_connection =
|
|
|
|
melib::smtp::SmtpConnection::new_connection(conf).await?;
|
2020-08-16 15:16:27 +03:00
|
|
|
smtp_connection.mail_transaction(&message, None).await
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
});
|
|
|
|
if complete_in_background {
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(job_id, JobRequest::SendMessageBackground(handle, chan));
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
return Ok(None);
|
|
|
|
} else {
|
2020-07-24 21:48:29 +03:00
|
|
|
self.insert_job(job_id, JobRequest::SendMessage);
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
}
|
|
|
|
Ok(Some((job_id, handle, chan)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 13:22:48 +03:00
|
|
|
pub fn delete(
|
|
|
|
&mut self,
|
|
|
|
env_hash: EnvelopeHash,
|
|
|
|
mailbox_hash: MailboxHash,
|
|
|
|
) -> ResultFuture<()> {
|
2020-03-01 17:47:23 +02:00
|
|
|
if self.settings.account.read_only() {
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
"Account {} is read-only.",
|
|
|
|
self.name.as_str()
|
|
|
|
)));
|
|
|
|
}
|
2020-07-05 13:22:48 +03:00
|
|
|
self.backend.write().unwrap().delete(env_hash, mailbox_hash)
|
2020-03-01 17:47:23 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:13:58 +03:00
|
|
|
pub fn contains_key(&self, h: EnvelopeHash) -> bool {
|
|
|
|
self.collection.contains_key(&h)
|
2019-05-26 02:34:03 +03:00
|
|
|
}
|
2020-06-23 17:21:50 +03:00
|
|
|
pub fn operation(&self, h: EnvelopeHash) -> Result<Box<dyn BackendOp>> {
|
|
|
|
let operation = self.backend.read().unwrap().operation(h)?;
|
|
|
|
Ok(if self.settings.account.read_only() {
|
2019-11-03 13:12:28 +02:00
|
|
|
ReadOnlyOp::new(operation)
|
|
|
|
} else {
|
|
|
|
operation
|
2020-06-23 17:21:50 +03:00
|
|
|
})
|
2019-05-26 02:34:03 +03:00
|
|
|
}
|
2019-11-02 12:14:31 +02:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
pub fn mailbox_operation(
|
2020-02-06 01:49:18 +02:00
|
|
|
&mut self,
|
2020-07-25 13:08:36 +03:00
|
|
|
op: crate::command::actions::MailboxOperation,
|
2020-08-25 22:56:45 +03:00
|
|
|
) -> Result<()> {
|
2020-07-25 13:08:36 +03:00
|
|
|
use crate::command::actions::MailboxOperation;
|
2020-02-06 01:49:18 +02:00
|
|
|
if self.settings.account.read_only() {
|
|
|
|
return Err(MeliError::new("Account is read-only."));
|
|
|
|
}
|
2020-01-15 12:31:49 +02:00
|
|
|
match op {
|
2020-02-26 10:54:10 +02:00
|
|
|
MailboxOperation::Create(path) => {
|
2020-08-25 22:56:45 +03:00
|
|
|
let job = self
|
|
|
|
.backend
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.create_mailbox(path.to_string())?;
|
|
|
|
let (channel, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(job)
|
2020-02-26 10:54:10 +02:00
|
|
|
} else {
|
2020-08-25 22:56:45 +03:00
|
|
|
self.job_executor.spawn_blocking(job)
|
2020-02-26 10:54:10 +02:00
|
|
|
};
|
2020-08-25 22:56:45 +03:00
|
|
|
self.insert_job(
|
|
|
|
job_id,
|
|
|
|
JobRequest::CreateMailbox {
|
|
|
|
path,
|
|
|
|
handle,
|
|
|
|
channel,
|
2020-02-10 02:11:07 +02:00
|
|
|
},
|
2020-02-06 01:49:18 +02:00
|
|
|
);
|
2020-08-25 22:56:45 +03:00
|
|
|
Ok(())
|
2020-02-06 01:49:18 +02:00
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
MailboxOperation::Delete(path) => {
|
|
|
|
if self.mailbox_entries.len() == 1 {
|
2020-02-06 01:49:18 +02:00
|
|
|
return Err(MeliError::new("Cannot delete only mailbox."));
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
2020-08-25 22:56:45 +03:00
|
|
|
|
2020-06-08 22:08:10 +03:00
|
|
|
let mailbox_hash = self.mailbox_by_path(&path)?;
|
2020-08-25 22:56:45 +03:00
|
|
|
let job = self.backend.write().unwrap().delete_mailbox(mailbox_hash)?;
|
|
|
|
let (channel, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(job)
|
|
|
|
};
|
|
|
|
self.insert_job(
|
|
|
|
job_id,
|
|
|
|
JobRequest::DeleteMailbox {
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_hash,
|
2020-08-25 22:56:45 +03:00
|
|
|
handle,
|
|
|
|
channel,
|
|
|
|
},
|
2020-02-22 10:57:59 +02:00
|
|
|
);
|
2020-08-25 22:56:45 +03:00
|
|
|
Ok(())
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
2020-03-24 21:01:06 +02:00
|
|
|
MailboxOperation::Subscribe(path) => {
|
2020-06-08 22:08:10 +03:00
|
|
|
let mailbox_hash = self.mailbox_by_path(&path)?;
|
2020-03-24 21:01:06 +02:00
|
|
|
self.backend
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.set_mailbox_subscription(mailbox_hash, true)?;
|
|
|
|
self.mailbox_entries.entry(mailbox_hash).and_modify(|m| {
|
|
|
|
m.conf.mailbox_conf.subscribe = super::ToggleFlag::True;
|
|
|
|
let _ = m.ref_mailbox.set_is_subscribed(true);
|
|
|
|
});
|
|
|
|
|
2020-08-25 22:56:45 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
None,
|
|
|
|
format!("'`{}` has been subscribed.", &path),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-08-25 22:56:45 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
Ok(())
|
2020-03-24 21:01:06 +02:00
|
|
|
}
|
|
|
|
MailboxOperation::Unsubscribe(path) => {
|
2020-06-08 22:08:10 +03:00
|
|
|
let mailbox_hash = self.mailbox_by_path(&path)?;
|
2020-03-24 21:01:06 +02:00
|
|
|
self.backend
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.set_mailbox_subscription(mailbox_hash, false)?;
|
|
|
|
self.mailbox_entries.entry(mailbox_hash).and_modify(|m| {
|
|
|
|
m.conf.mailbox_conf.subscribe = super::ToggleFlag::False;
|
|
|
|
let _ = m.ref_mailbox.set_is_subscribed(false);
|
|
|
|
});
|
|
|
|
|
2020-08-25 22:56:45 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
None,
|
|
|
|
format!("'`{}` has been unsubscribed.", &path),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-08-25 22:56:45 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
Ok(())
|
2020-03-24 21:01:06 +02:00
|
|
|
}
|
2020-02-26 10:54:10 +02:00
|
|
|
MailboxOperation::Rename(_, _) => Err(MeliError::new("Not implemented.")),
|
|
|
|
MailboxOperation::SetPermissions(_) => Err(MeliError::new("Not implemented.")),
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
2019-08-26 19:44:05 +03:00
|
|
|
}
|
2019-09-09 21:38:37 +03:00
|
|
|
|
2020-06-08 22:08:10 +03:00
|
|
|
pub fn special_use_mailbox(&self, special_use: SpecialUsageMailbox) -> Option<MailboxHash> {
|
2019-09-23 09:30:23 +03:00
|
|
|
let ret = self
|
2020-02-26 10:54:10 +02:00
|
|
|
.mailbox_entries
|
2019-09-23 09:30:23 +03:00
|
|
|
.iter()
|
2020-02-26 10:54:10 +02:00
|
|
|
.find(|(_, f)| f.conf.mailbox_conf().usage == Some(special_use));
|
2019-11-23 17:56:38 +02:00
|
|
|
if let Some(ret) = ret.as_ref() {
|
2020-06-08 22:08:10 +03:00
|
|
|
Some(ret.1.ref_mailbox.hash())
|
2019-11-23 17:56:38 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-09-23 09:30:23 +03:00
|
|
|
}
|
2019-10-24 20:30:17 +03:00
|
|
|
|
2019-11-12 13:07:00 +02:00
|
|
|
/* Call only in Context::is_online, since only Context can launch the watcher threads if an
|
|
|
|
* account goes from offline to online. */
|
2019-12-14 18:46:12 +02:00
|
|
|
pub fn is_online(&mut self) -> Result<()> {
|
2020-07-25 17:53:04 +03:00
|
|
|
if !self.backend_capabilities.is_remote && !self.backend_capabilities.is_async {
|
2020-07-05 19:17:28 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-08-02 00:46:37 +03:00
|
|
|
if self.is_online.is_err()
|
|
|
|
&& self
|
|
|
|
.is_online
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_err()
|
|
|
|
.kind
|
|
|
|
.is_authentication()
|
|
|
|
{
|
|
|
|
return self.is_online.clone();
|
|
|
|
}
|
2020-09-16 13:17:56 +03:00
|
|
|
if self.is_online.is_ok() {
|
2020-08-20 17:37:19 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if !self.active_jobs.values().any(JobRequest::is_online) {
|
|
|
|
let online_job = self.backend.read().unwrap().is_online();
|
|
|
|
if let Ok(online_job) = online_job {
|
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(online_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(online_job)
|
|
|
|
};
|
|
|
|
self.insert_job(job_id, JobRequest::IsOnline(handle, rcvr));
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
2020-02-28 09:16:19 +02:00
|
|
|
}
|
2020-08-20 17:37:19 +03:00
|
|
|
return self.is_online.clone();
|
2019-10-24 20:30:17 +03:00
|
|
|
}
|
2019-10-30 21:09:48 +02:00
|
|
|
|
|
|
|
pub fn search(
|
|
|
|
&self,
|
|
|
|
search_term: &str,
|
2020-07-05 13:22:48 +03:00
|
|
|
_sort: (SortField, SortOrder),
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_hash: MailboxHash,
|
2020-06-30 11:40:26 +03:00
|
|
|
) -> ResultFuture<SmallVec<[EnvelopeHash; 512]>> {
|
2020-08-06 19:45:08 +03:00
|
|
|
let query = melib::search::Query::try_from(search_term)?;
|
2020-07-16 23:58:46 +03:00
|
|
|
match self.settings.conf.search_backend {
|
|
|
|
#[cfg(feature = "sqlite3")]
|
2020-07-25 18:06:42 +03:00
|
|
|
crate::conf::SearchBackend::Sqlite3 => crate::sqlite3::search(&query, _sort),
|
2020-07-16 23:58:46 +03:00
|
|
|
crate::conf::SearchBackend::None => {
|
2020-07-25 17:53:04 +03:00
|
|
|
if self.backend_capabilities.supports_search {
|
2020-07-16 23:58:46 +03:00
|
|
|
self.backend
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.search(query, Some(mailbox_hash))
|
2020-06-23 17:21:50 +03:00
|
|
|
} else {
|
2020-08-06 19:45:08 +03:00
|
|
|
use melib::search::QueryTrait;
|
2020-07-16 23:58:46 +03:00
|
|
|
let mut ret = SmallVec::new();
|
|
|
|
let envelopes = self.collection.envelopes.read().unwrap();
|
2020-07-21 07:53:38 +03:00
|
|
|
for &env_hash in self.collection.get_mailbox(mailbox_hash).iter() {
|
2020-07-25 18:06:42 +03:00
|
|
|
if let Some(envelope) = envelopes.get(&env_hash) {
|
|
|
|
if envelope.is_match(&query) {
|
|
|
|
ret.push(env_hash);
|
|
|
|
}
|
2020-07-16 23:58:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Box::pin(async { Ok(ret) }))
|
2019-10-30 21:09:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 22:08:10 +03:00
|
|
|
|
|
|
|
pub fn mailbox_by_path(&self, path: &str) -> Result<MailboxHash> {
|
|
|
|
if let Some((mailbox_hash, _)) = self
|
|
|
|
.mailbox_entries
|
|
|
|
.iter()
|
|
|
|
.find(|(_, f)| f.ref_mailbox.path() == path)
|
|
|
|
{
|
|
|
|
Ok(*mailbox_hash)
|
|
|
|
} else {
|
|
|
|
Err(MeliError::new("Mailbox with that path not found."))
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 21:40:46 +03:00
|
|
|
|
|
|
|
pub fn process_event(&mut self, job_id: &JobId) -> bool {
|
2020-09-10 21:01:40 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::JobFinished(*job_id),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
|
2020-07-04 15:59:09 +03:00
|
|
|
if self.active_jobs.contains_key(job_id) {
|
|
|
|
match self.active_jobs.remove(job_id).unwrap() {
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::Mailboxes(_, ref mut chan) => {
|
2020-07-04 15:59:09 +03:00
|
|
|
if let Some(mailboxes) = chan.try_recv().unwrap() {
|
2020-08-26 19:59:27 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::AccountStatusChange(
|
|
|
|
self.hash,
|
|
|
|
)))
|
|
|
|
.unwrap();
|
2020-08-20 17:37:19 +03:00
|
|
|
if let Err(err) = mailboxes.and_then(|mailboxes| self.init(mailboxes)) {
|
2020-08-02 00:46:37 +03:00
|
|
|
if err.kind.is_authentication() {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: authentication error", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-08-02 00:46:37 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
self.is_online = Err(err);
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-10 21:01:40 +03:00
|
|
|
let mailboxes_job = self.backend.read().unwrap().mailboxes();
|
|
|
|
if let Ok(mailboxes_job) = mailboxes_job {
|
2020-09-16 13:17:26 +03:00
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(mailboxes_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(mailboxes_job)
|
|
|
|
};
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(job_id, JobRequest::Mailboxes(handle, rcvr));
|
|
|
|
};
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::Fetch(mailbox_hash, _, ref mut chan) => {
|
2020-06-28 15:39:33 +03:00
|
|
|
let (payload, rest): (Option<Result<Vec<Envelope>>>, _) =
|
|
|
|
chan.try_recv().unwrap().unwrap();
|
2020-06-27 21:40:46 +03:00
|
|
|
debug!("got payload in status for {}", mailbox_hash);
|
2020-06-28 15:39:33 +03:00
|
|
|
if payload.is_none() {
|
|
|
|
debug!("finished in status for {}", mailbox_hash);
|
|
|
|
self.mailbox_entries
|
|
|
|
.entry(mailbox_hash)
|
|
|
|
.and_modify(|entry| {
|
|
|
|
entry.status = MailboxStatus::Available;
|
|
|
|
});
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::MailboxUpdate((
|
2020-08-17 15:31:30 +03:00
|
|
|
self.hash,
|
2020-06-28 15:39:33 +03:00
|
|
|
mailbox_hash,
|
|
|
|
))))
|
|
|
|
.unwrap();
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-16 13:17:26 +03:00
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(rest.into_future())
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(rest.into_future())
|
|
|
|
};
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(job_id, JobRequest::Fetch(mailbox_hash, handle, rcvr));
|
2020-06-28 15:39:33 +03:00
|
|
|
let payload = payload.unwrap();
|
2020-07-13 18:49:27 +03:00
|
|
|
if let Err(err) = payload {
|
2020-08-02 00:46:37 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: could not fetch mailbox", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-08-02 00:46:37 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
2020-06-27 21:40:46 +03:00
|
|
|
self.mailbox_entries
|
|
|
|
.entry(mailbox_hash)
|
|
|
|
.and_modify(|entry| {
|
2020-07-13 18:49:27 +03:00
|
|
|
entry.status = MailboxStatus::Failed(err);
|
2020-06-27 21:40:46 +03:00
|
|
|
});
|
|
|
|
self.sender
|
2020-06-28 15:39:33 +03:00
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::MailboxUpdate((
|
2020-08-17 15:31:30 +03:00
|
|
|
self.hash,
|
2020-06-28 15:39:33 +03:00
|
|
|
mailbox_hash,
|
|
|
|
))))
|
2020-06-27 21:40:46 +03:00
|
|
|
.unwrap();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let envelopes = payload
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| (e.hash(), e))
|
|
|
|
.collect::<HashMap<EnvelopeHash, Envelope>>();
|
|
|
|
if let Some(updated_mailboxes) =
|
|
|
|
self.collection
|
|
|
|
.merge(envelopes, mailbox_hash, self.sent_mailbox)
|
|
|
|
{
|
|
|
|
for f in updated_mailboxes {
|
|
|
|
self.sender
|
2020-08-17 15:31:30 +03:00
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::MailboxUpdate((self.hash, f))))
|
2020-06-27 21:40:46 +03:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::MailboxUpdate((
|
2020-08-17 15:31:30 +03:00
|
|
|
self.hash,
|
2020-06-27 21:40:46 +03:00
|
|
|
mailbox_hash,
|
|
|
|
))))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::IsOnline(_, ref mut chan) => {
|
2020-07-04 15:59:09 +03:00
|
|
|
let is_online = chan.try_recv().unwrap();
|
2020-07-08 00:26:40 +03:00
|
|
|
if let Some(is_online) = is_online {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::AccountStatusChange(
|
2020-08-17 15:31:30 +03:00
|
|
|
self.hash,
|
2020-07-08 00:26:40 +03:00
|
|
|
)))
|
|
|
|
.unwrap();
|
2020-06-28 15:39:33 +03:00
|
|
|
if is_online.is_ok() {
|
2020-08-02 00:46:37 +03:00
|
|
|
if self.is_online.is_err()
|
|
|
|
&& !self
|
|
|
|
.is_online
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_err()
|
|
|
|
.kind
|
|
|
|
.is_authentication()
|
|
|
|
{
|
2020-07-05 20:11:47 +03:00
|
|
|
self.watch();
|
|
|
|
}
|
2020-07-08 00:26:40 +03:00
|
|
|
self.is_online = Ok(());
|
2020-06-28 15:39:33 +03:00
|
|
|
return true;
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
2020-07-08 00:26:40 +03:00
|
|
|
self.is_online = is_online;
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
2020-09-10 21:01:40 +03:00
|
|
|
let online_job = self.backend.read().unwrap().is_online();
|
|
|
|
if let Ok(online_job) = online_job {
|
2020-09-16 13:17:26 +03:00
|
|
|
let (rcvr, handle, job_id) = if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(online_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(online_job)
|
|
|
|
};
|
2020-09-10 21:01:40 +03:00
|
|
|
self.insert_job(job_id, JobRequest::IsOnline(handle, rcvr));
|
|
|
|
};
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::Refresh(_mailbox_hash, _, ref mut chan) => {
|
2020-07-04 15:59:09 +03:00
|
|
|
let r = chan.try_recv().unwrap();
|
2020-09-13 00:03:12 +03:00
|
|
|
match r {
|
|
|
|
Some(Ok(())) => {
|
2020-08-02 00:46:37 +03:00
|
|
|
if self.is_online.is_err()
|
|
|
|
&& !self
|
|
|
|
.is_online
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_err()
|
|
|
|
.kind
|
|
|
|
.is_authentication()
|
|
|
|
{
|
2020-07-08 00:26:40 +03:00
|
|
|
self.watch();
|
|
|
|
}
|
2020-09-13 00:03:12 +03:00
|
|
|
if !(self.is_online.is_err()
|
|
|
|
&& self
|
|
|
|
.is_online
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_err()
|
|
|
|
.kind
|
|
|
|
.is_authentication())
|
|
|
|
{
|
|
|
|
self.is_online = Ok(());
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::AccountStatusChange(
|
|
|
|
self.hash,
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-07-05 20:11:47 +03:00
|
|
|
}
|
2020-09-13 00:03:12 +03:00
|
|
|
Some(Err(err)) => {
|
|
|
|
if !err.kind.is_authentication() {
|
|
|
|
let online_job = self.backend.read().unwrap().is_online();
|
|
|
|
if let Ok(online_job) = online_job {
|
|
|
|
let (rcvr, handle, job_id) =
|
2020-09-16 13:17:26 +03:00
|
|
|
if self.backend_capabilities.is_async {
|
|
|
|
self.job_executor.spawn_specialized(online_job)
|
|
|
|
} else {
|
|
|
|
self.job_executor.spawn_blocking(online_job)
|
|
|
|
};
|
2020-09-13 00:03:12 +03:00
|
|
|
self.insert_job(job_id, JobRequest::IsOnline(handle, rcvr));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self.is_online = Err(err);
|
2020-08-02 00:46:37 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::AccountStatusChange(
|
2020-08-17 15:31:30 +03:00
|
|
|
self.hash,
|
2020-08-02 00:46:37 +03:00
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-09-13 00:03:12 +03:00
|
|
|
None => {}
|
2020-06-29 00:18:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::SetFlags(_, _, ref mut chan) => {
|
2020-06-29 00:18:24 +03:00
|
|
|
let r = chan.try_recv().unwrap();
|
|
|
|
if let Some(Err(err)) = r {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: could not set flag", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 20:00:25 +03:00
|
|
|
JobRequest::SaveMessage {
|
|
|
|
ref mut channel,
|
|
|
|
ref bytes,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let r = channel.try_recv().unwrap();
|
2020-06-29 00:18:24 +03:00
|
|
|
if let Some(Err(err)) = r {
|
2020-08-26 20:00:25 +03:00
|
|
|
melib::log(format!("Could not save message: {}", err), melib::ERROR);
|
|
|
|
let file = crate::types::create_temp_file(bytes, None, None, false);
|
|
|
|
debug!("message saved in {}", file.path.display());
|
|
|
|
melib::log(
|
|
|
|
format!(
|
|
|
|
"Message was stored in {} so that you can restore it manually.",
|
|
|
|
file.path.display()
|
|
|
|
),
|
|
|
|
melib::INFO,
|
|
|
|
);
|
2020-06-29 00:18:24 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: could not save message", &self.name)),
|
2020-08-26 20:00:25 +03:00
|
|
|
format!(
|
|
|
|
"Message was stored in {} so that you can restore it manually.",
|
|
|
|
file.path.display()
|
|
|
|
),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 21:01:40 +03:00
|
|
|
JobRequest::SendMessage => {}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::SendMessageBackground(_, ref mut chan) => {
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
let r = chan.try_recv().unwrap();
|
|
|
|
if let Some(Err(err)) = r {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some("Could not send message".to_string()),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 14:38:43 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::CopyTo(mailbox_hash, _, ref mut chan) => {
|
2020-07-05 13:22:48 +03:00
|
|
|
if let Err(err) = chan
|
|
|
|
.try_recv()
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.and_then(|bytes| self.save(&bytes, mailbox_hash, None))
|
|
|
|
{
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: could not save message", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-07-05 13:22:48 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::DeleteMessages(_, _, ref mut chan) => {
|
2020-06-29 00:18:24 +03:00
|
|
|
let r = chan.try_recv().unwrap();
|
|
|
|
if let Some(Err(err)) = r {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: could not delete message", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 22:56:45 +03:00
|
|
|
JobRequest::CreateMailbox {
|
|
|
|
ref path,
|
|
|
|
ref mut channel,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let r = channel.try_recv().unwrap();
|
2020-06-29 00:18:24 +03:00
|
|
|
if let Some(r) = r {
|
2020-08-25 22:56:45 +03:00
|
|
|
match r {
|
|
|
|
Err(err) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!(
|
|
|
|
"{}: could not create mailbox {}",
|
|
|
|
&self.name, path
|
|
|
|
)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-08-25 22:56:45 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
Ok((mailbox_hash, mut mailboxes)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::MailboxCreate((
|
|
|
|
self.hash,
|
|
|
|
mailbox_hash,
|
|
|
|
))))
|
|
|
|
.unwrap();
|
|
|
|
let mut new = FileMailboxConf::default();
|
|
|
|
new.mailbox_conf.subscribe = super::ToggleFlag::InternalVal(true);
|
|
|
|
new.mailbox_conf.usage = if mailboxes[&mailbox_hash].special_usage()
|
|
|
|
!= SpecialUsageMailbox::Normal
|
|
|
|
{
|
|
|
|
Some(mailboxes[&mailbox_hash].special_usage())
|
|
|
|
} else {
|
|
|
|
let tmp = SpecialUsageMailbox::detect_usage(
|
|
|
|
mailboxes[&mailbox_hash].name(),
|
|
|
|
);
|
|
|
|
if tmp != Some(SpecialUsageMailbox::Normal) && tmp != None {
|
|
|
|
mailboxes.entry(mailbox_hash).and_modify(|entry| {
|
|
|
|
let _ = entry.set_special_usage(tmp.unwrap());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
tmp
|
|
|
|
};
|
|
|
|
/* if new mailbox has parent, we need to update its children field */
|
|
|
|
if let Some(parent_hash) = mailboxes[&mailbox_hash].parent() {
|
|
|
|
self.mailbox_entries
|
|
|
|
.entry(parent_hash)
|
|
|
|
.and_modify(|parent| {
|
|
|
|
parent.ref_mailbox =
|
|
|
|
mailboxes.remove(&parent_hash).unwrap();
|
|
|
|
});
|
|
|
|
}
|
2020-08-27 17:29:27 +03:00
|
|
|
let status = MailboxStatus::default();
|
2020-08-25 22:56:45 +03:00
|
|
|
|
|
|
|
self.mailbox_entries.insert(
|
|
|
|
mailbox_hash,
|
|
|
|
MailboxEntry {
|
|
|
|
name: mailboxes[&mailbox_hash].path().to_string(),
|
|
|
|
status,
|
|
|
|
conf: new,
|
|
|
|
ref_mailbox: mailboxes.remove(&mailbox_hash).unwrap(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
self.collection
|
|
|
|
.threads
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.insert(mailbox_hash, Threads::default());
|
|
|
|
self.collection
|
|
|
|
.mailboxes
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.insert(mailbox_hash, Default::default());
|
|
|
|
build_mailboxes_order(
|
|
|
|
&mut self.tree,
|
|
|
|
&self.mailbox_entries,
|
|
|
|
&mut self.mailboxes_order,
|
|
|
|
);
|
|
|
|
//Ok(format!("`{}` successfully created.", &path))
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 00:18:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 22:56:45 +03:00
|
|
|
JobRequest::DeleteMailbox {
|
|
|
|
mailbox_hash,
|
|
|
|
ref mut channel,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let r = channel.try_recv().unwrap();
|
2020-06-29 00:18:24 +03:00
|
|
|
match r {
|
|
|
|
Some(Err(err)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: could not delete mailbox", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
2020-08-25 22:56:45 +03:00
|
|
|
Some(Ok(mut mailboxes)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::MailboxDelete((
|
|
|
|
self.hash,
|
|
|
|
mailbox_hash,
|
|
|
|
))))
|
|
|
|
.unwrap();
|
|
|
|
if let Some(pos) =
|
|
|
|
self.mailboxes_order.iter().position(|&h| h == mailbox_hash)
|
|
|
|
{
|
|
|
|
self.mailboxes_order.remove(pos);
|
|
|
|
}
|
|
|
|
if let Some(pos) = self.tree.iter().position(|n| n.hash == mailbox_hash)
|
|
|
|
{
|
|
|
|
self.tree.remove(pos);
|
|
|
|
}
|
|
|
|
if self.sent_mailbox == Some(mailbox_hash) {
|
|
|
|
self.sent_mailbox = None;
|
|
|
|
}
|
|
|
|
self.collection
|
|
|
|
.threads
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.remove(&mailbox_hash);
|
|
|
|
let deleted_mailbox =
|
|
|
|
self.mailbox_entries.remove(&mailbox_hash).unwrap();
|
|
|
|
/* if deleted mailbox had parent, we need to update its children field */
|
|
|
|
if let Some(parent_hash) = deleted_mailbox.ref_mailbox.parent() {
|
|
|
|
self.mailbox_entries
|
|
|
|
.entry(parent_hash)
|
|
|
|
.and_modify(|parent| {
|
|
|
|
parent.ref_mailbox =
|
|
|
|
mailboxes.remove(&parent_hash).unwrap();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
self.collection
|
|
|
|
.mailboxes
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.remove(&mailbox_hash);
|
|
|
|
build_mailboxes_order(
|
|
|
|
&mut self.tree,
|
|
|
|
&self.mailbox_entries,
|
|
|
|
&mut self.mailboxes_order,
|
|
|
|
);
|
|
|
|
// FIXME remove from settings as well
|
|
|
|
|
2020-06-29 00:18:24 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: mailbox deleted successfully", &self.name)),
|
|
|
|
String::new(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//JobRequest::RenameMailbox,
|
2020-09-10 21:01:40 +03:00
|
|
|
JobRequest::Search(_) | JobRequest::AsBytes(_) => {}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::SetMailboxPermissions(_, _, ref mut chan) => {
|
2020-06-29 00:18:24 +03:00
|
|
|
let r = chan.try_recv().unwrap();
|
|
|
|
match r {
|
|
|
|
Some(Err(err)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!(
|
|
|
|
"{}: could not set mailbox permissions",
|
|
|
|
&self.name
|
|
|
|
)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
Some(Ok(_)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!(
|
|
|
|
"{}: mailbox permissions set successfully",
|
|
|
|
&self.name
|
|
|
|
)),
|
|
|
|
String::new(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 14:27:43 +03:00
|
|
|
JobRequest::SetMailboxSubscription(_, _, ref mut chan) => {
|
2020-06-29 00:18:24 +03:00
|
|
|
let r = chan.try_recv().unwrap();
|
|
|
|
match r {
|
|
|
|
Some(Err(err)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!(
|
|
|
|
"{}: could not set mailbox subscription",
|
|
|
|
&self.name
|
|
|
|
)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
Some(Ok(_)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!(
|
|
|
|
"{}: mailbox subscription set successfully",
|
|
|
|
&self.name
|
|
|
|
)),
|
|
|
|
String::new(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-06-29 00:18:24 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 21:12:28 +03:00
|
|
|
JobRequest::Watch {
|
|
|
|
ref mut channel,
|
|
|
|
handle: _,
|
|
|
|
} => {
|
2020-07-05 13:22:48 +03:00
|
|
|
debug!("JobRequest::Watch finished??? ");
|
2020-08-25 21:12:28 +03:00
|
|
|
let r = channel.try_recv().unwrap();
|
|
|
|
debug!("JobRequest::Watch {:?}", r);
|
|
|
|
if let Some(Err(err)) = r {
|
2020-08-26 20:01:39 +03:00
|
|
|
if err.kind.is_timeout() {
|
|
|
|
self.watch();
|
|
|
|
} else {
|
|
|
|
//TODO: relaunch watch job with ratelimit for failure
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: watch thread failed", &self.name)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-08-26 20:01:39 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
2020-08-25 21:12:28 +03:00
|
|
|
}
|
2020-07-05 13:22:48 +03:00
|
|
|
}
|
2020-07-16 22:54:50 +03:00
|
|
|
JobRequest::Generic {
|
2020-07-29 14:27:43 +03:00
|
|
|
ref name,
|
|
|
|
ref mut channel,
|
2020-07-16 22:54:50 +03:00
|
|
|
handle: _,
|
2020-08-25 15:39:43 +03:00
|
|
|
ref mut on_finish,
|
2020-08-26 19:15:31 +03:00
|
|
|
logging_level,
|
2020-07-16 22:54:50 +03:00
|
|
|
} => {
|
|
|
|
let r = channel.try_recv().unwrap();
|
|
|
|
match r {
|
|
|
|
Some(Err(err)) => {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: {} failed", &self.name, name,)),
|
|
|
|
err.to_string(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Error(err.kind)),
|
2020-07-16 22:54:50 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
2020-08-26 19:15:31 +03:00
|
|
|
Some(Ok(())) if on_finish.is_none() => {
|
|
|
|
if logging_level <= melib::LoggingLevel::INFO {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Notification(
|
|
|
|
Some(format!("{}: {} succeeded", &self.name, name,)),
|
|
|
|
String::new(),
|
2020-09-13 15:23:14 +03:00
|
|
|
Some(crate::types::NotificationType::Info),
|
2020-08-26 19:15:31 +03:00
|
|
|
)))
|
|
|
|
.expect("Could not send event on main channel");
|
|
|
|
}
|
2020-07-16 22:54:50 +03:00
|
|
|
}
|
2020-08-26 19:15:31 +03:00
|
|
|
Some(Ok(())) | None => {}
|
2020-07-16 22:54:50 +03:00
|
|
|
}
|
2020-08-25 15:39:43 +03:00
|
|
|
if on_finish.is_some() {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::Callback(
|
|
|
|
on_finish.take().unwrap(),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-07-16 22:54:50 +03:00
|
|
|
}
|
2020-06-27 21:40:46 +03:00
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:48:29 +03:00
|
|
|
|
|
|
|
pub fn insert_job(&mut self, job_id: JobId, job: JobRequest) {
|
|
|
|
self.active_jobs.insert(job_id, job);
|
|
|
|
self.active_job_instants
|
|
|
|
.insert(std::time::Instant::now(), job_id);
|
2020-09-10 21:01:40 +03:00
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::NewJob(job_id),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
2020-07-24 21:48:29 +03:00
|
|
|
}
|
2020-09-14 19:32:43 +03:00
|
|
|
|
|
|
|
pub fn cancel_job(&mut self, job_id: JobId) -> Option<JobRequest> {
|
|
|
|
if let Some(req) = self.active_jobs.remove(&job_id) {
|
|
|
|
self.sender
|
|
|
|
.send(ThreadEvent::UIEvent(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::JobCanceled(job_id),
|
|
|
|
)))
|
|
|
|
.unwrap();
|
|
|
|
Some(req)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
2019-05-26 02:34:03 +03:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
impl Index<&MailboxHash> for Account {
|
|
|
|
type Output = MailboxEntry;
|
|
|
|
fn index(&self, index: &MailboxHash) -> &MailboxEntry {
|
|
|
|
&self.mailbox_entries[index]
|
2019-04-14 17:24:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
impl IndexMut<&MailboxHash> for Account {
|
|
|
|
fn index_mut(&mut self, index: &MailboxHash) -> &mut MailboxEntry {
|
|
|
|
self.mailbox_entries.get_mut(index).unwrap()
|
2019-04-14 17:24:01 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 13:46:08 +03:00
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
fn build_mailboxes_order(
|
|
|
|
tree: &mut Vec<MailboxNode>,
|
2020-08-17 15:31:30 +03:00
|
|
|
mailbox_entries: &IndexMap<MailboxHash, MailboxEntry>,
|
2020-02-26 10:54:10 +02:00
|
|
|
mailboxes_order: &mut Vec<MailboxHash>,
|
2020-01-15 12:31:49 +02:00
|
|
|
) {
|
|
|
|
tree.clear();
|
2020-02-26 10:54:10 +02:00
|
|
|
mailboxes_order.clear();
|
|
|
|
for (h, f) in mailbox_entries.iter() {
|
|
|
|
if f.ref_mailbox.parent().is_none() {
|
2020-02-08 23:42:31 +02:00
|
|
|
fn rec(
|
2020-02-26 10:54:10 +02:00
|
|
|
h: MailboxHash,
|
2020-08-17 15:31:30 +03:00
|
|
|
mailbox_entries: &IndexMap<MailboxHash, MailboxEntry>,
|
2020-02-08 23:42:31 +02:00
|
|
|
depth: usize,
|
2020-02-26 10:54:10 +02:00
|
|
|
) -> MailboxNode {
|
|
|
|
let mut node = MailboxNode {
|
2020-01-15 12:31:49 +02:00
|
|
|
hash: h,
|
2020-02-08 23:42:31 +02:00
|
|
|
children: Vec::new(),
|
|
|
|
depth,
|
2020-01-15 12:31:49 +02:00
|
|
|
};
|
2020-02-26 10:54:10 +02:00
|
|
|
for &c in mailbox_entries[&h].ref_mailbox.children() {
|
|
|
|
if mailbox_entries.contains_key(&c) {
|
|
|
|
node.children.push(rec(c, mailbox_entries, depth + 1));
|
2020-02-22 10:57:59 +02:00
|
|
|
}
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
|
|
|
node
|
|
|
|
};
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
tree.push(rec(*h, &mailbox_entries, 0));
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tree.sort_unstable_by(|a, b| {
|
2020-02-26 10:54:10 +02:00
|
|
|
if mailbox_entries[&b.hash]
|
|
|
|
.ref_mailbox
|
2020-02-10 02:11:07 +02:00
|
|
|
.path()
|
|
|
|
.eq_ignore_ascii_case("INBOX")
|
|
|
|
{
|
2020-01-15 12:31:49 +02:00
|
|
|
std::cmp::Ordering::Greater
|
2020-02-26 10:54:10 +02:00
|
|
|
} else if mailbox_entries[&a.hash]
|
|
|
|
.ref_mailbox
|
2020-02-10 02:11:07 +02:00
|
|
|
.path()
|
|
|
|
.eq_ignore_ascii_case("INBOX")
|
|
|
|
{
|
2020-01-15 12:31:49 +02:00
|
|
|
std::cmp::Ordering::Less
|
|
|
|
} else {
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_entries[&a.hash]
|
|
|
|
.ref_mailbox
|
2020-01-15 12:31:49 +02:00
|
|
|
.path()
|
2020-02-26 10:54:10 +02:00
|
|
|
.cmp(&mailbox_entries[&b.hash].ref_mailbox.path())
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-26 10:54:10 +02:00
|
|
|
let mut stack: SmallVec<[Option<&MailboxNode>; 16]> = SmallVec::new();
|
2020-01-15 12:31:49 +02:00
|
|
|
for n in tree.iter_mut() {
|
2020-02-26 10:54:10 +02:00
|
|
|
mailboxes_order.push(n.hash);
|
2020-02-08 23:42:31 +02:00
|
|
|
n.children.sort_unstable_by(|a, b| {
|
2020-02-26 10:54:10 +02:00
|
|
|
if mailbox_entries[&b.hash]
|
|
|
|
.ref_mailbox
|
2020-02-10 02:11:07 +02:00
|
|
|
.path()
|
|
|
|
.eq_ignore_ascii_case("INBOX")
|
|
|
|
{
|
2020-01-15 12:31:49 +02:00
|
|
|
std::cmp::Ordering::Greater
|
2020-02-26 10:54:10 +02:00
|
|
|
} else if mailbox_entries[&a.hash]
|
|
|
|
.ref_mailbox
|
2020-02-10 02:11:07 +02:00
|
|
|
.path()
|
|
|
|
.eq_ignore_ascii_case("INBOX")
|
|
|
|
{
|
2020-01-15 12:31:49 +02:00
|
|
|
std::cmp::Ordering::Less
|
|
|
|
} else {
|
2020-02-26 10:54:10 +02:00
|
|
|
mailbox_entries[&a.hash]
|
|
|
|
.ref_mailbox
|
2020-01-15 12:31:49 +02:00
|
|
|
.path()
|
2020-02-26 10:54:10 +02:00
|
|
|
.cmp(&mailbox_entries[&b.hash].ref_mailbox.path())
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
|
|
|
});
|
2020-02-08 23:42:31 +02:00
|
|
|
stack.extend(n.children.iter().rev().map(Some));
|
2020-01-15 12:31:49 +02:00
|
|
|
while let Some(Some(next)) = stack.pop() {
|
2020-02-26 10:54:10 +02:00
|
|
|
mailboxes_order.push(next.hash);
|
2020-02-08 23:42:31 +02:00
|
|
|
stack.extend(next.children.iter().rev().map(Some));
|
2020-01-15 12:31:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|