mirror of
https://github.com/chatmail/core.git
synced 2026-07-13 06:23:09 +03:00
Compare commits
2 Commits
refactor-i
...
hpk-imap-r
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b7bead869 | ||
|
|
4cebdf3b84 |
@@ -490,15 +490,12 @@ class Account(object):
|
||||
self._threads.stop(wait=wait)
|
||||
|
||||
def shutdown(self, wait=True):
|
||||
print("SHUTDOWN", self)
|
||||
""" stop threads and close and remove underlying dc_context and callbacks. """
|
||||
if hasattr(self, "_dc_context") and hasattr(self, "_threads"):
|
||||
print("stop_threads", self)
|
||||
self.stop_threads(wait=wait)
|
||||
print("close", self)
|
||||
# print("SHUTDOWN", self)
|
||||
self.stop_threads(wait=False)
|
||||
lib.dc_close(self._dc_context)
|
||||
print("clear", self)
|
||||
#self.stop_threads(wait=wait) # to wait for threads
|
||||
self.stop_threads(wait=wait) # to wait for threads
|
||||
deltachat.clear_context_callback(self._dc_context)
|
||||
del self._dc_context
|
||||
atexit.unregister(self.shutdown)
|
||||
@@ -570,45 +567,37 @@ class IOThreads:
|
||||
lib.dc_interrupt_sentbox_idle(self._dc_context)
|
||||
if wait:
|
||||
for name, thread in self._name2thread.items():
|
||||
print("joining", name)
|
||||
thread.join()
|
||||
|
||||
def imap_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "INBOX THREAD START")
|
||||
while not self._thread_quitflag:
|
||||
lib.dc_perform_imap_jobs(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_imap_fetch(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_imap_idle(self._dc_context)
|
||||
lib.dc_perform_imap_fetch(self._dc_context)
|
||||
lib.dc_perform_imap_idle(self._dc_context)
|
||||
self._log_event("py-bindings-info", 0, "INBOX THREAD FINISHED")
|
||||
|
||||
def mvbox_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "MVBOX THREAD START")
|
||||
while not self._thread_quitflag:
|
||||
lib.dc_perform_mvbox_jobs(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_mvbox_fetch(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_mvbox_idle(self._dc_context)
|
||||
lib.dc_perform_mvbox_fetch(self._dc_context)
|
||||
lib.dc_perform_mvbox_idle(self._dc_context)
|
||||
self._log_event("py-bindings-info", 0, "MVBOX THREAD FINISHED")
|
||||
|
||||
def sentbox_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "SENTBOX THREAD START")
|
||||
while not self._thread_quitflag:
|
||||
lib.dc_perform_sentbox_jobs(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_sentbox_fetch(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_sentbox_idle(self._dc_context)
|
||||
lib.dc_perform_sentbox_fetch(self._dc_context)
|
||||
lib.dc_perform_sentbox_idle(self._dc_context)
|
||||
self._log_event("py-bindings-info", 0, "SENTBOX THREAD FINISHED")
|
||||
|
||||
def smtp_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "SMTP THREAD START")
|
||||
while not self._thread_quitflag:
|
||||
lib.dc_perform_smtp_jobs(self._dc_context)
|
||||
if not self._thread_quitflag:
|
||||
lib.dc_perform_smtp_idle(self._dc_context)
|
||||
lib.dc_perform_smtp_idle(self._dc_context)
|
||||
self._log_event("py-bindings-info", 0, "SMTP THREAD FINISHED")
|
||||
|
||||
|
||||
|
||||
@@ -62,16 +62,16 @@ pub fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context) {
|
||||
|
||||
let mut param_autoconfig: Option<LoginParam> = None;
|
||||
|
||||
context.inbox.write().unwrap().disconnect(context);
|
||||
context.inbox.read().unwrap().disconnect(context);
|
||||
context
|
||||
.sentbox_thread
|
||||
.write()
|
||||
.read()
|
||||
.unwrap()
|
||||
.imap
|
||||
.disconnect(context);
|
||||
context
|
||||
.mvbox_thread
|
||||
.write()
|
||||
.read()
|
||||
.unwrap()
|
||||
.imap
|
||||
.disconnect(context);
|
||||
@@ -357,7 +357,7 @@ pub fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context) {
|
||||
};
|
||||
context
|
||||
.inbox
|
||||
.write()
|
||||
.read()
|
||||
.unwrap()
|
||||
.configure_folders(context, flags);
|
||||
true
|
||||
@@ -398,7 +398,7 @@ pub fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context) {
|
||||
}
|
||||
}
|
||||
if imap_connected_here {
|
||||
context.inbox.write().unwrap().disconnect(context);
|
||||
context.inbox.read().unwrap().disconnect(context);
|
||||
}
|
||||
if smtp_connected_here {
|
||||
context.smtp.clone().lock().unwrap().disconnect();
|
||||
@@ -484,7 +484,7 @@ fn try_imap_one_param(context: &Context, param: &LoginParam) -> Option<bool> {
|
||||
param.mail_user, param.mail_server, param.mail_port, param.server_flags
|
||||
);
|
||||
info!(context, "Trying: {}", inf);
|
||||
if context.inbox.write().unwrap().connect(context, ¶m) {
|
||||
if context.inbox.read().unwrap().connect(context, ¶m) {
|
||||
info!(context, "success: {}", inf);
|
||||
return Some(true);
|
||||
}
|
||||
@@ -556,7 +556,7 @@ fn try_smtp_one_param(context: &Context, param: &LoginParam) -> Option<bool> {
|
||||
/*******************************************************************************
|
||||
* Connect to configured account
|
||||
******************************************************************************/
|
||||
pub fn dc_connect_to_configured_imap(context: &Context, imap: &mut Imap) -> libc::c_int {
|
||||
pub fn dc_connect_to_configured_imap(context: &Context, imap: &Imap) -> libc::c_int {
|
||||
let mut ret_connected = 0;
|
||||
|
||||
if imap.is_connected() {
|
||||
|
||||
@@ -43,7 +43,6 @@ pub struct Context {
|
||||
blobdir: PathBuf,
|
||||
pub sql: Sql,
|
||||
pub inbox: Arc<RwLock<Imap>>,
|
||||
pub(crate) inbox_watch: Arc<(Mutex<bool>, Condvar)>,
|
||||
pub perform_inbox_jobs_needed: Arc<RwLock<bool>>,
|
||||
pub probe_imap_network: Arc<RwLock<bool>>,
|
||||
pub sentbox_thread: Arc<RwLock<JobThread>>,
|
||||
@@ -116,15 +115,10 @@ impl Context {
|
||||
"Blobdir does not exist: {}",
|
||||
blobdir.display()
|
||||
);
|
||||
|
||||
let inbox_watch = Arc::new((Mutex::new(false), Condvar::new()));
|
||||
let inbox = Arc::new(RwLock::new(Imap::new(inbox_watch.clone())));
|
||||
|
||||
let ctx = Context {
|
||||
blobdir,
|
||||
dbfile,
|
||||
inbox,
|
||||
inbox_watch,
|
||||
inbox: Arc::new(RwLock::new(Imap::new())),
|
||||
cb,
|
||||
os_name: Some(os_name),
|
||||
running_state: Arc::new(RwLock::new(Default::default())),
|
||||
@@ -138,10 +132,12 @@ impl Context {
|
||||
sentbox_thread: Arc::new(RwLock::new(JobThread::new(
|
||||
"SENTBOX",
|
||||
"configured_sentbox_folder",
|
||||
Imap::new(),
|
||||
))),
|
||||
mvbox_thread: Arc::new(RwLock::new(JobThread::new(
|
||||
"MVBOX",
|
||||
"configured_mvbox_folder",
|
||||
Imap::new(),
|
||||
))),
|
||||
probe_imap_network: Arc::new(RwLock::new(false)),
|
||||
perform_inbox_jobs_needed: Arc::new(RwLock::new(false)),
|
||||
@@ -402,24 +398,21 @@ impl Context {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn is_inbox(&self, folder_name: impl AsRef<str>) -> bool {
|
||||
folder_name.as_ref() == "INBOX"
|
||||
pub fn is_inbox(&self, folder_name: &str) -> bool {
|
||||
folder_name == "INBOX"
|
||||
}
|
||||
|
||||
pub fn is_sentbox(&self, folder_name: impl AsRef<str>) -> bool {
|
||||
let sentbox_name = self.sql.get_raw_config(self, "configured_sentbox_folder");
|
||||
if let Some(name) = sentbox_name {
|
||||
name == folder_name.as_ref()
|
||||
pub fn is_sentbox(&self, folder_name: &str) -> bool {
|
||||
if let Some(name) = self.sql.get_raw_config(self, "configured_sentbox_folder") {
|
||||
name == folder_name
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_mvbox(&self, folder_name: impl AsRef<str>) -> bool {
|
||||
let mvbox_name = self.sql.get_raw_config(self, "configured_mvbox_folder");
|
||||
|
||||
if let Some(name) = mvbox_name {
|
||||
name == folder_name.as_ref()
|
||||
pub fn is_mvbox(&self, folder_name: &str) -> bool {
|
||||
if let Some(name) = self.sql.get_raw_config(self, "configured_mvbox_folder") {
|
||||
name == folder_name
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@@ -458,28 +451,19 @@ impl Context {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interrupt_inbox_idle(&self) {
|
||||
let &(ref lock, ref cvar) = &*self.inbox_watch.clone();
|
||||
let mut watch = lock.lock().unwrap();
|
||||
|
||||
*watch = true;
|
||||
cvar.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Context {
|
||||
fn drop(&mut self) {
|
||||
info!(self, "disconnecting INBOX-watch",);
|
||||
self.inbox.write().unwrap().disconnect(self);
|
||||
self.inbox.read().unwrap().disconnect(self);
|
||||
info!(self, "disconnecting sentbox-thread",);
|
||||
self.sentbox_thread.write().unwrap().imap.disconnect(self);
|
||||
self.sentbox_thread.read().unwrap().imap.disconnect(self);
|
||||
info!(self, "disconnecting mvbox-thread",);
|
||||
self.mvbox_thread.write().unwrap().imap.disconnect(self);
|
||||
self.mvbox_thread.read().unwrap().imap.disconnect(self);
|
||||
info!(self, "disconnecting SMTP");
|
||||
self.smtp.clone().lock().unwrap().disconnect();
|
||||
self.sql.close(self);
|
||||
info!(self, "Context closed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
484
src/imap.rs
484
src/imap.rs
@@ -1,5 +1,8 @@
|
||||
use std::net;
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc, Condvar, Mutex, RwLock,
|
||||
};
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use crate::configure::dc_connect_to_configured_imap;
|
||||
@@ -8,7 +11,7 @@ use crate::context::Context;
|
||||
use crate::dc_receive_imf::dc_receive_imf;
|
||||
use crate::error::Error;
|
||||
use crate::events::Event;
|
||||
use crate::job::{connect_to_inbox, job_add_no_interrupt, Action};
|
||||
use crate::job::{connect_to_inbox, job_add, Action};
|
||||
use crate::login_param::{dc_build_tls, CertificateChecks, LoginParam};
|
||||
use crate::message::{self, update_msg_move_state, update_server_uid};
|
||||
use crate::oauth2::dc_get_oauth2_access_token;
|
||||
@@ -33,13 +36,14 @@ const SELECT_ALL: &str = "1:*";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Imap {
|
||||
config: ImapConfig,
|
||||
config: Arc<RwLock<ImapConfig>>,
|
||||
watch: Arc<(Mutex<bool>, Condvar)>,
|
||||
|
||||
session: Arc<Mutex<Option<Session>>>,
|
||||
stream: Option<net::TcpStream>,
|
||||
connected: bool,
|
||||
should_reconnect: bool,
|
||||
stream: Arc<RwLock<Option<net::TcpStream>>>,
|
||||
connected: Arc<Mutex<bool>>,
|
||||
|
||||
should_reconnect: AtomicBool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -357,44 +361,42 @@ impl Default for ImapConfig {
|
||||
}
|
||||
|
||||
impl Imap {
|
||||
pub fn new(watch: Arc<(Mutex<bool>, Condvar)>) -> Self {
|
||||
pub fn new() -> Self {
|
||||
Imap {
|
||||
session: Arc::new(Mutex::new(None)),
|
||||
stream: None,
|
||||
config: ImapConfig::default(),
|
||||
watch,
|
||||
connected: false,
|
||||
should_reconnect: false,
|
||||
stream: Arc::new(RwLock::new(None)),
|
||||
config: Arc::new(RwLock::new(ImapConfig::default())),
|
||||
watch: Arc::new((Mutex::new(false), Condvar::new())),
|
||||
connected: Arc::new(Mutex::new(false)),
|
||||
should_reconnect: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_connected(&self) -> bool {
|
||||
self.connected
|
||||
*self.connected.lock().unwrap()
|
||||
}
|
||||
|
||||
pub fn should_reconnect(&self) -> bool {
|
||||
self.should_reconnect
|
||||
self.should_reconnect.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn setup_handle_if_needed(&mut self, context: &Context) -> bool {
|
||||
if self.config.imap_server.is_empty() {
|
||||
return false;
|
||||
}
|
||||
fn setup_handle_if_needed(&self, context: &Context) -> Result<(), Error> {
|
||||
ensure!(!self.config.read().unwrap().imap_server.is_empty(), "no imap server configured");
|
||||
|
||||
if self.should_reconnect() {
|
||||
self.unsetup_handle(context);
|
||||
}
|
||||
|
||||
if self.is_connected() && self.stream.is_some() {
|
||||
self.should_reconnect = false;
|
||||
return true;
|
||||
if self.is_connected() && self.stream.read().unwrap().is_some() {
|
||||
self.should_reconnect.store(false, Ordering::Relaxed);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let server_flags = self.config.server_flags as i32;
|
||||
let server_flags = self.config.read().unwrap().server_flags as i32;
|
||||
|
||||
let connection_res: imap::error::Result<Client> =
|
||||
if (server_flags & (DC_LP_IMAP_SOCKET_STARTTLS | DC_LP_IMAP_SOCKET_PLAIN)) != 0 {
|
||||
let config = &self.config;
|
||||
let config = self.config.read().unwrap();
|
||||
let imap_server: &str = config.imap_server.as_ref();
|
||||
let imap_port = config.imap_port;
|
||||
|
||||
@@ -406,7 +408,7 @@ impl Imap {
|
||||
}
|
||||
})
|
||||
} else {
|
||||
let config = &self.config;
|
||||
let config = self.config.read().unwrap();
|
||||
let imap_server: &str = config.imap_server.as_ref();
|
||||
let imap_port = config.imap_port;
|
||||
|
||||
@@ -419,7 +421,7 @@ impl Imap {
|
||||
|
||||
let login_res = match connection_res {
|
||||
Ok(client) => {
|
||||
let config = &self.config;
|
||||
let config = self.config.read().unwrap();
|
||||
let imap_user: &str = config.imap_user.as_ref();
|
||||
let imap_pw: &str = config.imap_pw.as_ref();
|
||||
|
||||
@@ -433,14 +435,14 @@ impl Imap {
|
||||
};
|
||||
client.authenticate("XOAUTH2", &auth)
|
||||
} else {
|
||||
return false;
|
||||
bail!("could not authenticate with XOAUTH2");
|
||||
}
|
||||
} else {
|
||||
client.login(imap_user, imap_pw)
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
let config = &self.config;
|
||||
let config = self.config.read().unwrap();
|
||||
let imap_server: &str = config.imap_server.as_ref();
|
||||
let imap_port = config.imap_port;
|
||||
let message = context.stock_string_repl_str2(
|
||||
@@ -451,21 +453,21 @@ impl Imap {
|
||||
|
||||
emit_event!(context, Event::ErrorNetwork(message));
|
||||
|
||||
return false;
|
||||
bail!("login error: {}", err);
|
||||
}
|
||||
};
|
||||
|
||||
self.should_reconnect = false;
|
||||
self.should_reconnect.store(false, Ordering::Relaxed);
|
||||
|
||||
match login_res {
|
||||
Ok((session, stream)) => {
|
||||
*self.session.lock().unwrap() = Some(session);
|
||||
self.stream = Some(stream);
|
||||
true
|
||||
*self.stream.write().unwrap() = Some(stream);
|
||||
Ok(())
|
||||
}
|
||||
Err((err, _)) => {
|
||||
let imap_user = &self.config.imap_user;
|
||||
let message = context.stock_string_repl_str(StockMessage::CannotLogin, imap_user);
|
||||
let imap_user = self.config.read().unwrap().imap_user.to_owned();
|
||||
let message = context.stock_string_repl_str(StockMessage::CannotLogin, &imap_user);
|
||||
|
||||
emit_event!(
|
||||
context,
|
||||
@@ -473,15 +475,15 @@ impl Imap {
|
||||
);
|
||||
self.unsetup_handle(context);
|
||||
|
||||
false
|
||||
bail!("{}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unsetup_handle(&mut self, context: &Context) {
|
||||
fn unsetup_handle(&self, context: &Context) {
|
||||
info!(context, "IMAP unsetup_handle step 1 (closing down stream).");
|
||||
|
||||
if let Some(stream) = self.stream.take() {
|
||||
let stream = self.stream.write().unwrap().take();
|
||||
if let Some(stream) = stream {
|
||||
if let Err(err) = stream.shutdown(net::Shutdown::Both) {
|
||||
warn!(context, "failed to shutdown connection: {:?}", err);
|
||||
}
|
||||
@@ -498,13 +500,13 @@ impl Imap {
|
||||
}
|
||||
|
||||
info!(context, "IMAP unsetup_handle step 3 (clearing config).");
|
||||
self.config.selected_folder = None;
|
||||
self.config.selected_mailbox = None;
|
||||
self.config.write().unwrap().selected_folder = None;
|
||||
self.config.write().unwrap().selected_mailbox = None;
|
||||
info!(context, "IMAP unsetup_handle step 4 (disconnected).",);
|
||||
}
|
||||
|
||||
fn free_connect_params(&mut self) {
|
||||
let mut cfg = &mut self.config;
|
||||
fn free_connect_params(&self) {
|
||||
let mut cfg = self.config.write().unwrap();
|
||||
|
||||
cfg.addr = "".into();
|
||||
cfg.imap_server = "".into();
|
||||
@@ -518,7 +520,7 @@ impl Imap {
|
||||
cfg.watch_folder = None;
|
||||
}
|
||||
|
||||
pub fn connect(&mut self, context: &Context, lp: &LoginParam) -> bool {
|
||||
pub fn connect(&self, context: &Context, lp: &LoginParam) -> bool {
|
||||
if lp.mail_server.is_empty() || lp.mail_user.is_empty() || lp.mail_pw.is_empty() {
|
||||
return false;
|
||||
}
|
||||
@@ -535,7 +537,7 @@ impl Imap {
|
||||
let imap_pw = &lp.mail_pw;
|
||||
let server_flags = lp.server_flags as usize;
|
||||
|
||||
let mut config = &mut self.config;
|
||||
let mut config = self.config.write().unwrap();
|
||||
config.addr = addr.to_string();
|
||||
config.imap_server = imap_server.to_string();
|
||||
config.imap_port = imap_port;
|
||||
@@ -545,7 +547,7 @@ impl Imap {
|
||||
config.server_flags = server_flags;
|
||||
}
|
||||
|
||||
if !self.setup_handle_if_needed(context) {
|
||||
if self.setup_handle_if_needed(context).is_err() {
|
||||
self.free_connect_params();
|
||||
return false;
|
||||
}
|
||||
@@ -583,44 +585,42 @@ impl Imap {
|
||||
if teardown {
|
||||
self.unsetup_handle(context);
|
||||
self.free_connect_params();
|
||||
self.connected = false;
|
||||
false
|
||||
} else {
|
||||
self.config.can_idle = can_idle;
|
||||
self.config.has_xlist = has_xlist;
|
||||
self.connected = true;
|
||||
self.config.write().unwrap().can_idle = can_idle;
|
||||
self.config.write().unwrap().has_xlist = has_xlist;
|
||||
*self.connected.lock().unwrap() = true;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disconnect(&mut self, context: &Context) {
|
||||
// if self.is_connected() {
|
||||
info!(context, "disconnecting imap connection");
|
||||
self.unsetup_handle(context);
|
||||
self.free_connect_params();
|
||||
// }
|
||||
self.connected = false;
|
||||
pub fn disconnect(&self, context: &Context) {
|
||||
if self.is_connected() {
|
||||
self.unsetup_handle(context);
|
||||
self.free_connect_params();
|
||||
*self.connected.lock().unwrap() = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_watch_folder(&mut self, watch_folder: String) {
|
||||
self.config.watch_folder = Some(watch_folder);
|
||||
pub fn set_watch_folder(&self, watch_folder: String) {
|
||||
self.config.write().unwrap().watch_folder = Some(watch_folder);
|
||||
}
|
||||
|
||||
pub fn fetch(&mut self, context: &Context) -> bool {
|
||||
pub fn fetch(&self, context: &Context) -> bool {
|
||||
if !self.is_connected() || !context.sql.is_open() {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.setup_handle_if_needed(context);
|
||||
|
||||
let watch_folder = self.config.watch_folder.clone();
|
||||
let watch_folder = self.config.read().unwrap().watch_folder.to_owned();
|
||||
|
||||
if let Some(ref watch_folder) = watch_folder {
|
||||
// as during the fetch commands, new messages may arrive, we fetch until we do not
|
||||
// get any more. if IDLE is called directly after, there is only a small chance that
|
||||
// messages are missed and delayed until the next IDLE call
|
||||
loop {
|
||||
info!(context, "imap: fetching single folder");
|
||||
if self.fetch_from_single_folder(context, watch_folder) == 0 {
|
||||
if self.fetch_from_single_folder(context, watch_folder).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -630,90 +630,76 @@ impl Imap {
|
||||
}
|
||||
}
|
||||
|
||||
fn select_folder<S: AsRef<str>>(&mut self, context: &Context, folder: Option<S>) -> usize {
|
||||
info!(context, "select folder, waiting for session lock");
|
||||
if !self.is_connected() {
|
||||
let mut cfg = &mut self.config;
|
||||
fn expunge_folder(&self, context: &Context) -> bool {
|
||||
if let Some(ref folder) = self.config.read().unwrap().selected_folder {
|
||||
info!(context, "Expunge messages in \"{}\".", folder);
|
||||
|
||||
// A CLOSE-SELECT is considerably faster than an EXPUNGE-SELECT, see
|
||||
// https://tools.ietf.org/html/rfc3501#section-6.4.2
|
||||
if let Some(ref mut session) = &mut *self.session.lock().unwrap() {
|
||||
match session.close() {
|
||||
Ok(_) => {
|
||||
info!(context, "close/expunge succeeded");
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(context, "failed to close session: {:?}", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn select_folder(&self, context: &Context, folder: &str) -> Result<(), Error> {
|
||||
if self.session.lock().unwrap().is_none() {
|
||||
let mut cfg = self.config.write().unwrap();
|
||||
cfg.selected_folder = None;
|
||||
cfg.selected_folder_needs_expunge = false;
|
||||
return 0;
|
||||
bail!("select_folder: session is closed");
|
||||
}
|
||||
|
||||
// if there is a new folder and the new folder is equal to the selected one, there's nothing to do.
|
||||
// if there is _no_ new folder, we continue as we might want to expunge below.
|
||||
if let Some(ref folder) = folder {
|
||||
if let Some(ref selected_folder) = self.config.selected_folder {
|
||||
if folder.as_ref() == selected_folder {
|
||||
return 1;
|
||||
}
|
||||
if let Some(ref selected_folder) = self.config.read().unwrap().selected_folder {
|
||||
if folder == selected_folder {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
// deselect existing folder, if needed (it's also done implicitly by SELECT, however, without EXPUNGE then)
|
||||
let needs_expunge: bool = self.config.selected_folder_needs_expunge;
|
||||
let needs_expunge = { self.config.read().unwrap().selected_folder_needs_expunge };
|
||||
if needs_expunge {
|
||||
if !self.is_connected() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if let Some(ref folder) = self.config.selected_folder {
|
||||
info!(context, "Expunge messages in \"{}\".", folder);
|
||||
|
||||
// A CLOSE-SELECT is considerably faster than an EXPUNGE-SELECT, see
|
||||
// https://tools.ietf.org/html/rfc3501#section-6.4.2
|
||||
if let Some(ref mut session) = &mut *self.session.lock().unwrap() {
|
||||
match session.close() {
|
||||
Ok(_) => {
|
||||
info!(context, "close/expunge succeeded");
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(context, "failed to close session: {:?}", err);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
self.config.selected_folder_needs_expunge = false;
|
||||
self.expunge_folder(context);
|
||||
self.config.write().unwrap().selected_folder_needs_expunge = false;
|
||||
}
|
||||
|
||||
// select new folder
|
||||
if let Some(ref folder) = folder {
|
||||
if !self.is_connected() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if let Some(ref mut session) = &mut *self.session.lock().unwrap() {
|
||||
match session.select(folder) {
|
||||
Ok(mailbox) => {
|
||||
let mut config = &mut self.config;
|
||||
config.selected_folder = Some(folder.as_ref().to_string());
|
||||
config.selected_mailbox = Some(mailbox);
|
||||
}
|
||||
Err(err) => {
|
||||
info!(
|
||||
context,
|
||||
"Cannot select folder: {}; {:?}.",
|
||||
folder.as_ref(),
|
||||
err
|
||||
);
|
||||
|
||||
self.config.selected_folder = None;
|
||||
self.should_reconnect = true;
|
||||
return 0;
|
||||
}
|
||||
if let Some(ref mut session) = &mut *self.session.lock().unwrap() {
|
||||
match session.select(&folder) {
|
||||
Ok(mailbox) => {
|
||||
let mut config = self.config.write().unwrap();
|
||||
config.selected_folder = Some(folder.to_string());
|
||||
config.selected_mailbox = Some(mailbox);
|
||||
}
|
||||
Err(err) => {
|
||||
self.config.write().unwrap().selected_folder = None;
|
||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
||||
bail!("Cannot select folder: {}; {:?}.", folder, err);
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
1
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_config_last_seen_uid<S: AsRef<str>>(&self, context: &Context, folder: S) -> (u32, u32) {
|
||||
let key = format!("imap.mailbox.{}", folder.as_ref());
|
||||
fn get_config_last_seen_uid(&self, context: &Context, folder: &str) -> (u32, u32) {
|
||||
let key = format!("imap.mailbox.{}", folder);
|
||||
if let Some(entry) = context.sql.get_raw_config(context, &key) {
|
||||
// the entry has the format `imap.mailbox.<folder>=<uidvalidity>:<lastseenuid>`
|
||||
let mut parts = entry.split(':');
|
||||
@@ -734,55 +720,25 @@ impl Imap {
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_from_single_folder<S: AsRef<str>>(&mut self, context: &Context, folder: S) -> usize {
|
||||
if !self.is_connected() {
|
||||
info!(
|
||||
context,
|
||||
"Cannot fetch from \"{}\" - not connected.",
|
||||
folder.as_ref()
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
info!(context, "selecting folder");
|
||||
let r = self.select_folder(context, Some(&folder));
|
||||
info!(context, "selecting folder done {}", r);
|
||||
if r == 0 {
|
||||
info!(
|
||||
context,
|
||||
"Cannot select folder \"{}\" for fetching.",
|
||||
folder.as_ref()
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn
|
||||
|
||||
// compare last seen UIDVALIDITY against the current one
|
||||
let (mut uid_validity, mut last_seen_uid) = self.get_config_last_seen_uid(context, &folder);
|
||||
let (last_uid_validity, last_seen_uid) = self.get_config_last_seen_uid(context, &folder);
|
||||
let mailbox = self.config.read().unwrap().selected_mailbox.as_ref().expect("just selected");
|
||||
|
||||
let mailbox = self
|
||||
.config
|
||||
.selected_mailbox
|
||||
.as_ref()
|
||||
.expect("just selected");
|
||||
ensure!(mailbox.uid_validity.is_some(),
|
||||
"Cannot get UIDVALIDITY for folder \"{}\".", folder,);
|
||||
|
||||
if mailbox.uid_validity.is_none() {
|
||||
error!(
|
||||
context,
|
||||
"Cannot get UIDVALIDITY for folder \"{}\".",
|
||||
folder.as_ref(),
|
||||
);
|
||||
let mailbox = mailbox.unwrap();
|
||||
let current_uid_validity = mailbox.uid_validity.unwrap_or_default();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if mailbox.uid_validity.unwrap_or_default() != uid_validity {
|
||||
// first time this folder is selected or UIDVALIDITY has changed, init lastseenuid and save it to config
|
||||
if current_uid_validity != last_uid_validity {
|
||||
// first time this folder is selected or UIDVALIDITY has changed,
|
||||
// init lastseenuid and save it to config
|
||||
|
||||
if mailbox.exists == 0 {
|
||||
info!(context, "Folder \"{}\" is empty.", folder.as_ref());
|
||||
|
||||
info!(context, "Folder \"{}\" is empty.", folder);
|
||||
// set lastseenuid=0 for empty folders.
|
||||
// id we do not do this here, we'll miss the first message
|
||||
// as we will get in here again and fetch from lastseenuid+1 then
|
||||
@@ -790,75 +746,68 @@ impl Imap {
|
||||
self.set_config_last_seen_uid(
|
||||
context,
|
||||
&folder,
|
||||
mailbox.uid_validity.unwrap_or_default(),
|
||||
current_uid_validity,
|
||||
0,
|
||||
);
|
||||
return 0;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let list = if let Some(ref mut session) = &mut *self.session.lock().unwrap() {
|
||||
if !self.is_connected() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// `FETCH <message sequence number> (UID)`
|
||||
let set = format!("{}", mailbox.exists);
|
||||
info!(context, "session fetch {}", &set);
|
||||
match session.fetch(set, PREFETCH_FLAGS) {
|
||||
Ok(list) => list,
|
||||
Err(_err) => {
|
||||
self.should_reconnect = true;
|
||||
info!(
|
||||
context,
|
||||
"No result returned for folder \"{}\".",
|
||||
folder.as_ref()
|
||||
);
|
||||
|
||||
return 0;
|
||||
Err(err) => {
|
||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
||||
bail!("IMAP FETCH {} {} failed on folder '{}': {}'",
|
||||
set, PREFETCH_FLAGS, folder, err);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
unreachable!();
|
||||
};
|
||||
|
||||
last_seen_uid = list[0].uid.unwrap_or_else(|| 0);
|
||||
last_seen_uid = list[0].uid.unwrap_or_default();
|
||||
|
||||
// if the UIDVALIDITY has _changed_, decrease lastseenuid by one to avoid gaps (well add 1 below
|
||||
if uid_validity > 0 && last_seen_uid > 1 {
|
||||
if last_uid_validity > 0 && last_seen_uid > 1 {
|
||||
last_seen_uid -= 1;
|
||||
}
|
||||
|
||||
uid_validity = mailbox.uid_validity.unwrap_or_default();
|
||||
self.set_config_last_seen_uid(context, &folder, uid_validity, last_seen_uid);
|
||||
info!(
|
||||
context,
|
||||
"lastseenuid initialized to {} for {}@{}",
|
||||
last_seen_uid,
|
||||
folder.as_ref(),
|
||||
folder,
|
||||
uid_validity,
|
||||
);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn fetch_from_single_folder(&self, context: &Context, folder: &str) -> Result<(), Error> {
|
||||
self.select_folder(context, &folder)?;
|
||||
|
||||
if self.folder_is_uidvalid_and_empty(&self, context: &Context, folder: &str)? {
|
||||
return
|
||||
|
||||
let mut read_cnt = 0;
|
||||
let mut read_errors = 0;
|
||||
let mut new_last_seen_uid = 0;
|
||||
|
||||
let set = format!("{}:*", last_seen_uid + 1);
|
||||
let list = if let Some(ref mut session) = &mut *self.session.lock().unwrap() {
|
||||
if !self.is_connected() {
|
||||
return 0;
|
||||
}
|
||||
// fetch messages with larger UID than the last one seen
|
||||
// (`UID FETCH lastseenuid+1:*)`, see RFC 4549
|
||||
let set = format!("{}:*", last_seen_uid + 1);
|
||||
match session.uid_fetch(set, PREFETCH_FLAGS) {
|
||||
Ok(list) => list,
|
||||
Err(err) => {
|
||||
warn!(context, "failed to fetch uids: {}", err);
|
||||
return 0;
|
||||
bail!("failed to fetch uids: {}", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
unreachable!();
|
||||
};
|
||||
|
||||
// go through all mails in folder (this is typically _fast_ as we already have the whole list)
|
||||
@@ -869,14 +818,14 @@ impl Imap {
|
||||
|
||||
let message_id = prefetch_get_message_id(msg).unwrap_or_default();
|
||||
|
||||
if !precheck_imf(context, &message_id, folder.as_ref(), cur_uid) {
|
||||
if !precheck_imf(context, &message_id, &folder, cur_uid) {
|
||||
// check passed, go fetch the rest
|
||||
if self.fetch_single_msg(context, &folder, cur_uid) == 0 {
|
||||
info!(
|
||||
context,
|
||||
"Read error for message {} from \"{}\", trying over later.",
|
||||
message_id,
|
||||
folder.as_ref()
|
||||
folder
|
||||
);
|
||||
|
||||
read_errors += 1;
|
||||
@@ -887,7 +836,7 @@ impl Imap {
|
||||
context,
|
||||
"Skipping message {} from \"{}\" by precheck.",
|
||||
message_id,
|
||||
folder.as_ref(),
|
||||
folder
|
||||
);
|
||||
}
|
||||
if cur_uid > new_last_seen_uid {
|
||||
@@ -907,7 +856,7 @@ impl Imap {
|
||||
context,
|
||||
"{} mails read from \"{}\" with {} errors.",
|
||||
read_cnt,
|
||||
folder.as_ref(),
|
||||
folder,
|
||||
read_errors
|
||||
);
|
||||
} else {
|
||||
@@ -915,30 +864,30 @@ impl Imap {
|
||||
context,
|
||||
"{} mails read from \"{}\".",
|
||||
read_cnt,
|
||||
folder.as_ref()
|
||||
folder
|
||||
);
|
||||
}
|
||||
|
||||
read_cnt
|
||||
Ok(read_cnt)
|
||||
}
|
||||
|
||||
fn set_config_last_seen_uid<S: AsRef<str>>(
|
||||
fn set_config_last_seen_uid(
|
||||
&self,
|
||||
context: &Context,
|
||||
folder: S,
|
||||
folder: &str,
|
||||
uidvalidity: u32,
|
||||
lastseenuid: u32,
|
||||
) {
|
||||
let key = format!("imap.mailbox.{}", folder.as_ref());
|
||||
let key = format!("imap.mailbox.{}", folder);
|
||||
let val = format!("{}:{}", uidvalidity, lastseenuid);
|
||||
|
||||
context.sql.set_raw_config(context, &key, Some(&val)).ok();
|
||||
}
|
||||
|
||||
fn fetch_single_msg<S: AsRef<str>>(
|
||||
&mut self,
|
||||
fn fetch_single_msg(
|
||||
&self,
|
||||
context: &Context,
|
||||
folder: S,
|
||||
folder: &str,
|
||||
server_uid: u32,
|
||||
) -> usize {
|
||||
// the function returns:
|
||||
@@ -954,12 +903,12 @@ impl Imap {
|
||||
match session.uid_fetch(set, BODY_FLAGS) {
|
||||
Ok(msgs) => msgs,
|
||||
Err(err) => {
|
||||
self.should_reconnect = true;
|
||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
||||
warn!(
|
||||
context,
|
||||
"Error on fetching message #{} from folder \"{}\"; retry={}; error={}.",
|
||||
server_uid,
|
||||
folder.as_ref(),
|
||||
folder,
|
||||
self.should_reconnect(),
|
||||
err
|
||||
);
|
||||
@@ -975,7 +924,7 @@ impl Imap {
|
||||
context,
|
||||
"Message #{} does not exist in folder \"{}\".",
|
||||
server_uid,
|
||||
folder.as_ref()
|
||||
folder,
|
||||
);
|
||||
} else {
|
||||
let msg = &msgs[0];
|
||||
@@ -995,7 +944,7 @@ impl Imap {
|
||||
if !is_deleted && msg.body().is_some() {
|
||||
let body = msg.body().unwrap_or_default();
|
||||
unsafe {
|
||||
dc_receive_imf(context, &body, folder.as_ref(), server_uid, flags as u32);
|
||||
dc_receive_imf(context, &body, &folder, server_uid, flags as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1003,19 +952,16 @@ impl Imap {
|
||||
1
|
||||
}
|
||||
|
||||
pub fn idle(&mut self, context: &Context) {
|
||||
info!(context, "IDLE START");
|
||||
if !self.config.can_idle {
|
||||
return self.fake_idle(context);
|
||||
pub fn idle(&self, context: &Context) -> Result<(), Error> {
|
||||
if !self.config.read().unwrap().can_idle {
|
||||
self.fake_idle(context);
|
||||
}
|
||||
|
||||
self.setup_handle_if_needed(context);
|
||||
self.setup_handle_if_needed(context)?;
|
||||
|
||||
let watch_folder = self.config.watch_folder.clone();
|
||||
if self.select_folder(context, watch_folder) == 0 {
|
||||
warn!(context, "IMAP-IDLE not setup.",);
|
||||
|
||||
return self.fake_idle(context);
|
||||
let watch_folder = self.config.read().unwrap().watch_folder.clone();
|
||||
if let Some(wfolder) = watch_folder {
|
||||
self.select_folder(context, &wfolder)?;
|
||||
}
|
||||
|
||||
let session = self.session.clone();
|
||||
@@ -1069,7 +1015,7 @@ impl Imap {
|
||||
let &(ref lock, ref cvar) = &*self.watch.clone();
|
||||
let mut watch = lock.lock().unwrap();
|
||||
|
||||
let mut handle_res = |res| match res {
|
||||
let handle_res = |res| match res {
|
||||
Ok(()) => {
|
||||
info!(context, "IMAP-IDLE has data.");
|
||||
}
|
||||
@@ -1080,7 +1026,7 @@ impl Imap {
|
||||
info!(context, "IMAP-IDLE wait cancelled, we will reconnect soon.");
|
||||
self.unsetup_handle(context);
|
||||
info!(context, "IMAP-IDLE has SHUTDOWN");
|
||||
self.should_reconnect = true;
|
||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
||||
}
|
||||
_ => {
|
||||
warn!(context, "IMAP-IDLE returns unknown value: {}", err);
|
||||
@@ -1096,25 +1042,23 @@ impl Imap {
|
||||
let res = cvar.wait(watch).unwrap();
|
||||
watch = res;
|
||||
if *watch {
|
||||
let msg = worker.as_ref().unwrap().try_recv();
|
||||
drop(worker.take());
|
||||
|
||||
if let Ok(res) = msg {
|
||||
if let Ok(res) = worker.as_ref().unwrap().try_recv() {
|
||||
handle_res(res);
|
||||
} else {
|
||||
info!(context, "IMAP-IDLE interrupted");
|
||||
}
|
||||
|
||||
drop(worker.take());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*watch = false;
|
||||
|
||||
info!(context, "IDLE STOP");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fake_idle(&mut self, context: &Context) {
|
||||
fn fake_idle(&self, context: &Context) {
|
||||
// Idle using timeouts. This is also needed if we're not yet configured -
|
||||
// in this case, we're waiting for a configure job
|
||||
let fake_idle_start_time = SystemTime::now();
|
||||
@@ -1159,7 +1103,7 @@ impl Imap {
|
||||
// try to connect with proper login params
|
||||
// (setup_handle_if_needed might not know about them if we
|
||||
// never successfully connected)
|
||||
if dc_connect_to_configured_imap(context, self) != 0 {
|
||||
if dc_connect_to_configured_imap(context, &self) != 0 {
|
||||
return;
|
||||
}
|
||||
// we cannot connect, wait long next time (currently 60 secs, see above)
|
||||
@@ -1171,25 +1115,26 @@ impl Imap {
|
||||
// will have already fetched the messages so perform_*_fetch
|
||||
// will not find any new.
|
||||
|
||||
if let Some(ref watch_folder) = self.config.watch_folder.clone() {
|
||||
if 0 != self.fetch_from_single_folder(context, watch_folder) {
|
||||
let watch_folder = self.config.read().unwrap().watch_folder.clone();
|
||||
if let Some(watch_folder) = watch_folder {
|
||||
if self.fetch_from_single_folder(context, &watch_folder).is_ok() {
|
||||
do_fake_idle = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn interrupt_idle(&self) {
|
||||
// // interrupt idle
|
||||
// let &(ref lock, ref cvar) = &*self.watch.clone();
|
||||
// let mut watch = lock.lock().unwrap();
|
||||
pub fn interrupt_idle(&self) {
|
||||
// interrupt idle
|
||||
let &(ref lock, ref cvar) = &*self.watch.clone();
|
||||
let mut watch = lock.lock().unwrap();
|
||||
|
||||
// *watch = true;
|
||||
// cvar.notify_one();
|
||||
// }
|
||||
*watch = true;
|
||||
cvar.notify_one();
|
||||
}
|
||||
|
||||
pub fn mv(
|
||||
&mut self,
|
||||
&self,
|
||||
context: &Context,
|
||||
folder: &str,
|
||||
uid: u32,
|
||||
@@ -1247,7 +1192,7 @@ impl Imap {
|
||||
warn!(context, "Cannot mark {} as \"Deleted\" after copy.", uid);
|
||||
ImapResult::Failed
|
||||
} else {
|
||||
self.config.selected_folder_needs_expunge = true;
|
||||
self.config.write().unwrap().selected_folder_needs_expunge = true;
|
||||
ImapResult::Success
|
||||
}
|
||||
}
|
||||
@@ -1296,7 +1241,7 @@ impl Imap {
|
||||
}
|
||||
|
||||
pub fn prepare_imap_operation_on_msg(
|
||||
&mut self,
|
||||
&self,
|
||||
context: &Context,
|
||||
folder: &str,
|
||||
uid: u32,
|
||||
@@ -1304,23 +1249,21 @@ impl Imap {
|
||||
if uid == 0 {
|
||||
return Some(ImapResult::Failed);
|
||||
} else if !self.is_connected() {
|
||||
connect_to_inbox(context, self);
|
||||
connect_to_inbox(context, &self);
|
||||
if !self.is_connected() {
|
||||
return Some(ImapResult::RetryLater);
|
||||
}
|
||||
}
|
||||
if self.select_folder(context, Some(&folder)) == 0 {
|
||||
warn!(
|
||||
context,
|
||||
"Cannot select folder {} for preparing IMAP operation", folder
|
||||
);
|
||||
Some(ImapResult::RetryLater)
|
||||
} else {
|
||||
None
|
||||
match self.select_folder(context, &folder) {
|
||||
Ok(()) => None,
|
||||
Err(err) => {
|
||||
warn!(context, "Cannot select folder {}: {}", folder, err);
|
||||
Some(ImapResult::RetryLater)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_seen(&mut self, context: &Context, folder: &str, uid: u32) -> ImapResult {
|
||||
pub fn set_seen(&self, context: &Context, folder: &str, uid: u32) -> ImapResult {
|
||||
if let Some(imapresult) = self.prepare_imap_operation_on_msg(context, folder, uid) {
|
||||
return imapresult;
|
||||
}
|
||||
@@ -1340,7 +1283,7 @@ impl Imap {
|
||||
|
||||
// only returns 0 on connection problems; we should try later again in this case *
|
||||
pub fn delete_msg(
|
||||
&mut self,
|
||||
&self,
|
||||
context: &Context,
|
||||
message_id: &str,
|
||||
folder: &str,
|
||||
@@ -1407,12 +1350,12 @@ impl Imap {
|
||||
display_imap_id, message_id
|
||||
))
|
||||
);
|
||||
self.config.selected_folder_needs_expunge = true;
|
||||
self.config.write().unwrap().selected_folder_needs_expunge = true;
|
||||
ImapResult::Success
|
||||
}
|
||||
}
|
||||
|
||||
pub fn configure_folders(&mut self, context: &Context, flags: libc::c_int) {
|
||||
pub fn configure_folders(&self, context: &Context, flags: libc::c_int) {
|
||||
if !self.is_connected() {
|
||||
return;
|
||||
}
|
||||
@@ -1420,7 +1363,7 @@ impl Imap {
|
||||
info!(context, "Configuring IMAP-folders.");
|
||||
|
||||
let folders = self.list_folders(context).unwrap();
|
||||
let delimiter = self.config.imap_delimiter;
|
||||
let delimiter = self.config.read().unwrap().imap_delimiter;
|
||||
let fallback_folder = format!("INBOX{}DeltaChat", delimiter);
|
||||
|
||||
let mut mvbox_folder = folders
|
||||
@@ -1522,28 +1465,17 @@ impl Imap {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty_folder(&mut self, context: &Context, folder: &str) {
|
||||
pub fn empty_folder(&self, context: &Context, folder: &str) -> Result<(), Error> {
|
||||
info!(context, "emptying folder {}", folder);
|
||||
|
||||
if folder.is_empty() || self.select_folder(context, Some(&folder)) == 0 {
|
||||
warn!(context, "Cannot select folder '{}' for emptying", folder);
|
||||
return;
|
||||
}
|
||||
self.select_folder(context, &folder)?;
|
||||
|
||||
if !self.add_flag_finalized_with_set(context, SELECT_ALL, "\\Deleted") {
|
||||
warn!(context, "Cannot empty folder {}", folder);
|
||||
} else {
|
||||
// we now trigger expunge to actually delete messages
|
||||
self.config.selected_folder_needs_expunge = true;
|
||||
if self.select_folder::<String>(context, None) == 0 {
|
||||
warn!(
|
||||
context,
|
||||
"could not perform expunge on empty-marked folder {}", folder
|
||||
);
|
||||
} else {
|
||||
emit_event!(context, Event::ImapFolderEmptied(folder.to_string()));
|
||||
}
|
||||
}
|
||||
ensure!(self.add_flag_finalized_with_set(context, SELECT_ALL, "\\Deleted"),
|
||||
"Could not set \\Deleted flags to empty {}", folder);
|
||||
|
||||
ensure!(self.expunge_folder(context), "Could not expunge");
|
||||
emit_event!(context, Event::ImapFolderEmptied(folder.to_string()));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1597,7 +1529,7 @@ fn precheck_imf(context: &Context, rfc724_mid: &str, server_folder: &str, server
|
||||
{
|
||||
if old_server_folder.is_empty() && old_server_uid == 0 {
|
||||
info!(context, "[move] detected bbc-self {}", rfc724_mid,);
|
||||
job_add_no_interrupt(
|
||||
job_add(
|
||||
context,
|
||||
Action::MarkseenMsgOnImap,
|
||||
msg_id.to_u32() as i32,
|
||||
|
||||
84
src/job.rs
84
src/job.rs
@@ -219,7 +219,7 @@ impl Job {
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn do_DC_JOB_MOVE_MSG(&mut self, context: &Context) {
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
|
||||
if let Ok(msg) = Message::load_from_db(context, MsgId::new(self.foreign_id)) {
|
||||
if context
|
||||
@@ -264,7 +264,7 @@ impl Job {
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn do_DC_JOB_DELETE_MSG_ON_IMAP(&mut self, context: &Context) {
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
|
||||
if let Ok(mut msg) = Message::load_from_db(context, MsgId::new(self.foreign_id)) {
|
||||
if !msg.rfc724_mid.is_empty() {
|
||||
@@ -291,24 +291,25 @@ impl Job {
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn do_DC_JOB_EMPTY_SERVER(&mut self, context: &Context) {
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
fn do_DC_JOB_EMPTY_SERVER(&mut self, context: &Context) -> Result<(), Error> {
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
if self.foreign_id & DC_EMPTY_MVBOX > 0 {
|
||||
if let Some(mvbox_folder) = context
|
||||
.sql
|
||||
.get_raw_config(context, "configured_mvbox_folder")
|
||||
{
|
||||
inbox.empty_folder(context, &mvbox_folder);
|
||||
inbox.empty_folder(context, &mvbox_folder)?;
|
||||
}
|
||||
}
|
||||
if self.foreign_id & DC_EMPTY_INBOX > 0 {
|
||||
inbox.empty_folder(context, "INBOX");
|
||||
inbox.empty_folder(context, "INBOX")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn do_DC_JOB_MARKSEEN_MSG_ON_IMAP(&mut self, context: &Context) {
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
|
||||
if let Ok(msg) = Message::load_from_db(context, MsgId::new(self.foreign_id)) {
|
||||
let folder = msg.server_folder.as_ref().unwrap();
|
||||
@@ -342,7 +343,7 @@ impl Job {
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
let uid = self.param.get_int(Param::ServerUid).unwrap_or_default() as u32;
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
if inbox.set_seen(context, &folder, uid) == ImapResult::RetryLater {
|
||||
self.try_again_later(3i32, None);
|
||||
return;
|
||||
@@ -383,10 +384,10 @@ pub fn job_kill_action(context: &Context, action: Action) -> bool {
|
||||
}
|
||||
|
||||
pub fn perform_imap_fetch(context: &Context) {
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
if 0 == connect_to_inbox(context, &mut inbox) {
|
||||
if 0 == connect_to_inbox(context, &inbox) {
|
||||
return;
|
||||
}
|
||||
if !context.get_config_bool(Config::InboxWatch) {
|
||||
@@ -407,9 +408,9 @@ pub fn perform_imap_fetch(context: &Context) {
|
||||
}
|
||||
|
||||
pub fn perform_imap_idle(context: &Context) {
|
||||
let mut inbox = context.inbox.write().unwrap();
|
||||
let inbox = context.inbox.read().unwrap();
|
||||
|
||||
connect_to_inbox(context, &mut inbox);
|
||||
connect_to_inbox(context, &inbox);
|
||||
|
||||
if *context.perform_inbox_jobs_needed.clone().read().unwrap() {
|
||||
info!(
|
||||
@@ -418,11 +419,8 @@ pub fn perform_imap_idle(context: &Context) {
|
||||
);
|
||||
return;
|
||||
}
|
||||
drop(inbox);
|
||||
info!(context, "INBOX-IDLE started...");
|
||||
|
||||
context.inbox.write().unwrap().idle(context);
|
||||
|
||||
inbox.idle(context);
|
||||
info!(context, "INBOX-IDLE ended.");
|
||||
}
|
||||
|
||||
@@ -441,7 +439,7 @@ pub fn perform_mvbox_idle(context: &Context) {
|
||||
|
||||
context
|
||||
.mvbox_thread
|
||||
.write()
|
||||
.read()
|
||||
.unwrap()
|
||||
.idle(context, use_network);
|
||||
}
|
||||
@@ -465,7 +463,7 @@ pub fn perform_sentbox_idle(context: &Context) {
|
||||
|
||||
context
|
||||
.sentbox_thread
|
||||
.write()
|
||||
.read()
|
||||
.unwrap()
|
||||
.idle(context, use_network);
|
||||
}
|
||||
@@ -796,7 +794,14 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
|
||||
warn!(context, "Unknown job id found");
|
||||
}
|
||||
Action::SendMsgToSmtp => job.do_DC_JOB_SEND(context),
|
||||
Action::EmptyServer => job.do_DC_JOB_EMPTY_SERVER(context),
|
||||
Action::EmptyServer => {
|
||||
match job.do_DC_JOB_EMPTY_SERVER(context) {
|
||||
Ok(()) => {},
|
||||
Err(err) => {
|
||||
warn!(context, "emptying server folder(s) failed: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Action::DeleteMsgOnImap => job.do_DC_JOB_DELETE_MSG_ON_IMAP(context),
|
||||
Action::MarkseenMsgOnImap => job.do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context),
|
||||
Action::MarkseenMdnOnImap => job.do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context),
|
||||
@@ -929,7 +934,7 @@ fn suspend_smtp_thread(context: &Context, suspend: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn connect_to_inbox(context: &Context, inbox: &mut Imap) -> libc::c_int {
|
||||
pub fn connect_to_inbox(context: &Context, inbox: &Imap) -> libc::c_int {
|
||||
let ret_connected = dc_connect_to_configured_imap(context, inbox);
|
||||
if 0 != ret_connected {
|
||||
inbox.set_watch_folder("INBOX".into());
|
||||
@@ -983,24 +988,6 @@ pub fn job_add(
|
||||
foreign_id: libc::c_int,
|
||||
param: Params,
|
||||
delay_seconds: i64,
|
||||
) {
|
||||
job_add_no_interrupt(context, action, foreign_id, param, delay_seconds);
|
||||
|
||||
let thread: Thread = action.into();
|
||||
|
||||
match thread {
|
||||
Thread::Imap => interrupt_imap_idle(context),
|
||||
Thread::Smtp => interrupt_smtp_idle(context),
|
||||
Thread::Unknown => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn job_add_no_interrupt(
|
||||
context: &Context,
|
||||
action: Action,
|
||||
foreign_id: libc::c_int,
|
||||
param: Params,
|
||||
delay_seconds: i64,
|
||||
) {
|
||||
if action == Action::Unknown {
|
||||
error!(context, "Invalid action passed to job_add");
|
||||
@@ -1023,30 +1010,29 @@ pub fn job_add_no_interrupt(
|
||||
(timestamp + delay_seconds as i64)
|
||||
]
|
||||
).ok();
|
||||
|
||||
match thread {
|
||||
Thread::Imap => interrupt_imap_idle(context),
|
||||
Thread::Smtp => interrupt_smtp_idle(context),
|
||||
Thread::Unknown => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interrupt_smtp_idle(context: &Context) {
|
||||
info!(context, "Interrupting SMTP-idle...",);
|
||||
|
||||
let &(ref lock, ref cvar) = &*context.smtp_state.clone();
|
||||
{
|
||||
let mut state = lock.lock().unwrap();
|
||||
let mut state = lock.lock().unwrap();
|
||||
|
||||
state.perform_jobs_needed = 1;
|
||||
state.idle = true;
|
||||
info!(context, "smtp interrupt jobs written");
|
||||
}
|
||||
state.perform_jobs_needed = 1;
|
||||
state.idle = true;
|
||||
cvar.notify_one();
|
||||
info!(context, "smtp interrupt done");
|
||||
}
|
||||
|
||||
pub fn interrupt_imap_idle(context: &Context) {
|
||||
info!(context, "Interrupting INBOX-IDLE...",);
|
||||
|
||||
*context.perform_inbox_jobs_needed.write().unwrap() = true;
|
||||
info!(context, "interrupt jobs written");
|
||||
|
||||
context.interrupt_inbox_idle();
|
||||
|
||||
info!(context, "interrupt imap done");
|
||||
context.inbox.read().unwrap().interrupt_idle();
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ pub struct JobThread {
|
||||
pub name: &'static str,
|
||||
pub folder_config_name: &'static str,
|
||||
pub imap: Imap,
|
||||
watch: Arc<(Mutex<bool>, Condvar)>,
|
||||
pub state: Arc<(Mutex<JobState>, Condvar)>,
|
||||
}
|
||||
|
||||
@@ -22,15 +21,11 @@ pub struct JobState {
|
||||
}
|
||||
|
||||
impl JobThread {
|
||||
pub fn new(name: &'static str, folder_config_name: &'static str) -> Self {
|
||||
let watch = Arc::new((Mutex::new(false), Condvar::new()));
|
||||
let imap = Imap::new(watch.clone());
|
||||
|
||||
pub fn new(name: &'static str, folder_config_name: &'static str, imap: Imap) -> Self {
|
||||
JobThread {
|
||||
name,
|
||||
folder_config_name,
|
||||
imap,
|
||||
watch,
|
||||
state: Arc::new((Mutex::new(Default::default()), Condvar::new())),
|
||||
}
|
||||
}
|
||||
@@ -68,22 +63,13 @@ impl JobThread {
|
||||
|
||||
info!(context, "Interrupting {}-IDLE...", self.name);
|
||||
|
||||
// interrupt imap idle
|
||||
let &(ref lock, ref cvar) = &*self.watch.clone();
|
||||
{
|
||||
let mut watch = lock.lock().unwrap();
|
||||
|
||||
*watch = true;
|
||||
cvar.notify_one();
|
||||
}
|
||||
self.imap.interrupt_idle();
|
||||
|
||||
let &(ref lock, ref cvar) = &*self.state.clone();
|
||||
{
|
||||
let mut state = lock.lock().unwrap();
|
||||
state.idle = true;
|
||||
}
|
||||
let mut state = lock.lock().unwrap();
|
||||
|
||||
state.idle = true;
|
||||
cvar.notify_one();
|
||||
info!(context, "{}-idle interrupt done", self.name);
|
||||
}
|
||||
|
||||
pub fn fetch(&mut self, context: &Context, use_network: bool) {
|
||||
@@ -120,12 +106,12 @@ impl JobThread {
|
||||
self.state.0.lock().unwrap().using_handle = false;
|
||||
}
|
||||
|
||||
fn connect_to_imap(&mut self, context: &Context) -> bool {
|
||||
fn connect_to_imap(&self, context: &Context) -> bool {
|
||||
if self.imap.is_connected() {
|
||||
return true;
|
||||
}
|
||||
|
||||
let mut ret_connected = dc_connect_to_configured_imap(context, &mut self.imap) != 0;
|
||||
let mut ret_connected = dc_connect_to_configured_imap(context, &self.imap) != 0;
|
||||
|
||||
if ret_connected {
|
||||
if context
|
||||
@@ -148,7 +134,7 @@ impl JobThread {
|
||||
ret_connected
|
||||
}
|
||||
|
||||
pub fn idle(&mut self, context: &Context, use_network: bool) {
|
||||
pub fn idle(&self, context: &Context, use_network: bool) {
|
||||
{
|
||||
let &(ref lock, ref cvar) = &*self.state.clone();
|
||||
let mut state = lock.lock().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user