cleanup and setup configure

This commit is contained in:
dignifiedquire
2020-03-18 00:36:13 +01:00
parent 846ef043d5
commit aa45716ef7
11 changed files with 475 additions and 720 deletions

View File

@@ -7,7 +7,6 @@ use crate::blob::BlobObject;
use crate::constants::DC_VERSION_STR;
use crate::context::Context;
use crate::dc_tools::*;
use crate::job::*;
use crate::mimefactory::RECOMMENDED_FILE_SIZE;
use crate::stock::StockMessage;

File diff suppressed because it is too large Load Diff

View File

@@ -14,16 +14,13 @@ use crate::constants::*;
use crate::contact::*;
use crate::error::*;
use crate::events::Event;
use crate::imap::*;
use crate::job::{self, Action};
use crate::job_thread::JobThread;
use crate::key::Key;
use crate::login_param::LoginParam;
use crate::lot::Lot;
use crate::message::{self, Message, MessengerMessage, MsgId};
use crate::param::Params;
use crate::scheduler::Scheduler;
use crate::smtp::Smtp;
use crate::sql::Sql;
#[derive(Clone, Debug)]
@@ -492,19 +489,6 @@ pub(crate) struct BobStatus {
pub qr_scan: Option<Lot>,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub(crate) enum PerformJobsNeeded {
Not,
AtOnce,
AvoidDos,
}
impl Default for PerformJobsNeeded {
fn default() -> Self {
Self::Not
}
}
pub fn get_version_str() -> &'static str {
&DC_VERSION_STR
}

View File

@@ -4,7 +4,6 @@ use async_imap::extensions::idle::{Handle as ImapIdleHandle, IdleResponse};
use async_native_tls::TlsStream;
use async_std::net::TcpStream;
use async_std::prelude::*;
use std::sync::atomic::Ordering;
use std::time::{Duration, SystemTime};
use crate::context::Context;

View File

@@ -3,8 +3,6 @@
//! uses [async-email/async-imap](https://github.com/async-email/async-imap)
//! to implement connect, fetch, delete functionality with standard IMAP servers.
use std::sync::atomic::{AtomicBool, Ordering};
use num_traits::FromPrimitive;
use async_imap::{
@@ -12,7 +10,7 @@ use async_imap::{
types::{Capability, Fetch, Flag, Mailbox, Name, NameAttribute},
};
use async_std::prelude::*;
use async_std::sync::{Mutex, Receiver, RwLock};
use async_std::sync::Receiver;
use crate::config::*;
use crate::constants::*;

View File

@@ -17,7 +17,7 @@ use crate::dc_tools::*;
use crate::e2ee;
use crate::error::*;
use crate::events::Event;
use crate::job::{self, Action, Job};
use crate::job::Job;
use crate::key::{self, Key};
use crate::message::{Message, MsgId};
use crate::mimeparser::SystemMessage;

View File

@@ -6,8 +6,6 @@
use std::future::Future;
use std::{fmt, time};
use async_std::prelude::*;
use deltachat_derive::{FromSql, ToSql};
use itertools::Itertools;
use rand::{thread_rng, Rng};
@@ -15,16 +13,13 @@ use rand::{thread_rng, Rng};
use crate::blob::BlobObject;
use crate::chat::{self, ChatId};
use crate::config::Config;
use crate::configure::*;
use crate::constants::*;
use crate::contact::Contact;
use crate::context::{Context, PerformJobsNeeded};
use crate::context::Context;
use crate::dc_tools::*;
use crate::error::{Error, Result};
use crate::events::Event;
use crate::imap::*;
use crate::imex::*;
use crate::job;
use crate::location;
use crate::login_param::LoginParam;
use crate::message::MsgId;

View File

@@ -1,218 +0,0 @@
use async_std::sync::{channel, Arc, Mutex, Receiver, Sender};
use crate::context::Context;
use crate::error::{Error, Result};
use crate::imap::Imap;
#[derive(Debug)]
pub struct JobThread {
pub name: &'static str,
pub folder_config_name: &'static str,
pub imap: Imap,
pub state: Arc<Mutex<JobState>>,
notify_sender: Sender<()>,
notify_receiver: Receiver<()>,
}
#[derive(Clone, Debug, Default)]
pub struct JobState {
jobs_needed: bool,
suspended: bool,
using_handle: bool,
}
impl JobThread {
pub fn new(name: &'static str, folder_config_name: &'static str, imap: Imap) -> Self {
let (notify_sender, notify_receiver) = channel(1);
JobThread {
name,
folder_config_name,
imap,
state: Arc::new(Mutex::new(Default::default())),
notify_sender,
notify_receiver,
}
}
pub async fn suspend(&mut self, context: &Context) {
info!(context, "Suspending {}-thread.", self.name,);
{
self.state.lock().await.suspended = true;
}
self.interrupt_idle(context).await;
loop {
let using_handle = self.state.lock().await.using_handle;
if !using_handle {
return;
}
async_std::task::sleep(std::time::Duration::from_micros(300 * 1000)).await;
}
}
pub async fn unsuspend(&self, context: &Context) {
info!(context, "Unsuspending {}-thread.", self.name);
{
let lock = &*self.state.clone();
let mut state = lock.lock().await;
state.suspended = false;
}
self.notify_sender.send(()).await;
}
pub async fn try_interrupt_idle(&mut self, context: &Context) -> bool {
if self.state.lock().await.using_handle {
self.interrupt_idle(context).await;
return true;
}
false
}
pub async fn interrupt_idle(&mut self, context: &Context) {
{
self.state.lock().await.jobs_needed = true;
}
info!(context, "Interrupting {}-IDLE...", self.name);
self.imap.interrupt_idle(context).await;
self.notify_sender.send(()).await;
info!(context, "Interrupting {}-IDLE... finished", self.name);
}
pub async fn fetch(&mut self, context: &Context, use_network: bool) {
{
let lock = &*self.state.clone();
let mut state = lock.lock().await;
if state.suspended {
return;
}
state.using_handle = true;
}
if use_network {
if let Err(err) = self.connect_and_fetch(context).await {
warn!(context, "connect+fetch failed: {}, reconnect & retry", err);
self.imap.trigger_reconnect();
if let Err(err) = self.connect_and_fetch(context).await {
warn!(context, "connect+fetch failed: {}", err);
}
}
}
{
self.state.lock().await.using_handle = false;
}
}
async fn connect_and_fetch(&mut self, context: &Context) -> Result<()> {
let prefix = format!("{}-fetch", self.name);
match self.imap.connect_configured(context).await {
Ok(()) => {
if let Some(watch_folder) = self.get_watch_folder(context).await {
let start = std::time::Instant::now();
info!(context, "{} started...", prefix);
let res = self
.imap
.fetch(context, &watch_folder)
.await
.map_err(Into::into);
let elapsed = start.elapsed().as_millis();
info!(context, "{} done in {:.3} ms.", prefix, elapsed);
res
} else {
Err(Error::WatchFolderNotFound("not-set".to_string()))
}
}
Err(err) => Err(crate::error::Error::Message(err.to_string())),
}
}
async fn get_watch_folder(&self, context: &Context) -> Option<String> {
match context
.sql
.get_raw_config(context, self.folder_config_name)
.await
{
Some(name) => Some(name),
None => {
if self.folder_config_name == "configured_inbox_folder" {
// initialized with old version, so has not set configured_inbox_folder
Some("INBOX".to_string())
} else {
None
}
}
}
}
pub async fn idle(&mut self, context: &Context, use_network: bool) {
{
let lock = &*self.state.clone();
let mut state = lock.lock().await;
if state.jobs_needed {
info!(
context,
"{}-IDLE will not be started as it was interrupted while not idling.",
self.name,
);
state.jobs_needed = false;
return;
}
if state.suspended {
self.notify_receiver.recv().await;
return;
}
state.using_handle = true;
if !use_network {
state.using_handle = false;
self.notify_receiver.recv().await;
return;
}
}
let prefix = format!("{}-IDLE", self.name);
let do_fake_idle = match self.imap.connect_configured(context).await {
Ok(()) => {
if !self.imap.can_idle() {
true // we have to do fake_idle
} else {
let watch_folder = self.get_watch_folder(context).await;
info!(context, "{} started...", prefix);
let res = self.imap.idle(context, watch_folder).await;
info!(context, "{} ended...", prefix);
if let Err(err) = res {
warn!(context, "{} failed: {} -> reconnecting", prefix, err);
// something is borked, let's start afresh on the next occassion
self.imap.disconnect(context).await;
}
false
}
}
Err(err) => {
info!(context, "{}-IDLE connection fail: {:?}", self.name, err);
// if the connection fails, use fake_idle to retry periodically
// fake_idle() will be woken up by interrupt_idle() as
// well so will act on maybe_network events
true
}
};
if do_fake_idle {
let watch_folder = self.get_watch_folder(context).await;
self.imap.fake_idle(context, watch_folder).await;
}
self.state.lock().await.using_handle = false;
}
}

View File

@@ -54,7 +54,6 @@ pub mod imex;
mod scheduler;
#[macro_use]
pub mod job;
mod job_thread;
pub mod key;
mod keyring;
pub mod location;

View File

@@ -217,14 +217,6 @@ impl Scheduler {
_ => false,
}
}
/// Check if the scheduler is stoppd.
pub fn is_stopped(&self) -> bool {
match self {
Scheduler::Stopped => true,
_ => false,
}
}
}
/// Connection state logic shared between imap and smtp connections.

View File

@@ -2,14 +2,13 @@
pub mod send;
use async_std::sync::{channel, Receiver, RwLock, Sender};
use std::time::{Duration, Instant};
use async_smtp::smtp::client::net::*;
use async_smtp::*;
use crate::constants::*;
use crate::context::{Context, PerformJobsNeeded};
use crate::context::Context;
use crate::events::Event;
use crate::login_param::{dc_build_tls, LoginParam};
use crate::oauth2::*;