|
@@ -10,7 +10,7 @@ use std::collections::HashMap;
|
|
use std::fs::{create_dir_all, File};
|
|
use std::fs::{create_dir_all, File};
|
|
use std::future::Future;
|
|
use std::future::Future;
|
|
use std::io::{Write};
|
|
use std::io::{Write};
|
|
-use std::sync::{Mutex, RwLock};
|
|
|
|
|
|
+use std::sync::Mutex;
|
|
use std::time::{Instant};
|
|
use std::time::{Instant};
|
|
use anyhow::anyhow;
|
|
use anyhow::anyhow;
|
|
use serde::{Deserialize, Serialize};
|
|
use serde::{Deserialize, Serialize};
|
|
@@ -40,29 +40,46 @@ mod server;
|
|
mod js;
|
|
mod js;
|
|
mod sonic;
|
|
mod sonic;
|
|
|
|
|
|
-struct IdleTaskStorage{
|
|
|
|
- handlers: HashMap<String, JoinHandle<Result<(), anyhow::Error>>>
|
|
|
|
|
|
+struct ImapTaskStorage {
|
|
|
|
+ downloading: Option<JoinHandle<()>>,
|
|
|
|
+ idle_handlers: HashMap<String, JoinHandle<Result<(), anyhow::Error>>>
|
|
}
|
|
}
|
|
|
|
|
|
-impl IdleTaskStorage {
|
|
|
|
|
|
+impl ImapTaskStorage {
|
|
fn new() -> Self {
|
|
fn new() -> Self {
|
|
- IdleTaskStorage {
|
|
|
|
- handlers: HashMap::new(),
|
|
|
|
|
|
+ ImapTaskStorage {
|
|
|
|
+ downloading: None,
|
|
|
|
+ idle_handlers: HashMap::new(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn add(&mut self, mailbox: String, handler: JoinHandle<Result<(), anyhow::Error>>) {
|
|
|
|
- self.handlers.insert(mailbox, handler);
|
|
|
|
|
|
+ pub fn add_idle_task(&mut self, mailbox: String, handler: JoinHandle<Result<(), anyhow::Error>>) {
|
|
|
|
+ // TODO check if the task already exists
|
|
|
|
+ // TODO add a task when a new folder spotted on the IMAP
|
|
|
|
+ self.idle_handlers.insert(mailbox, handler);
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn delete(&mut self, mailbox: String){
|
|
|
|
- if self.handlers.contains_key(&mailbox.clone()) {
|
|
|
|
- self.handlers.remove(&mailbox.clone());
|
|
|
|
|
|
+ pub fn remove_idle_task(&mut self, mailbox: String){
|
|
|
|
+ if self.idle_handlers.contains_key(&mailbox.clone()) {
|
|
|
|
+ self.idle_handlers.remove(&mailbox.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ pub fn start_downloading(&mut self){
|
|
|
|
+ if let Some(ref mut handle) = self.downloading {
|
|
|
|
+ handle.abort();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ self.downloading = Some(tokio::spawn(async move {
|
|
|
|
+ match imap::download_email_from_imap().await {
|
|
|
|
+ Ok(_) => {}
|
|
|
|
+ Err(_) => println!("Cannot download new emails")
|
|
|
|
+ }
|
|
|
|
+ }));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-pub static IDLE_TASK_STORAGE: Lazy<Mutex<IdleTaskStorage>> = Lazy::new(|| Mutex::new(IdleTaskStorage::new()));
|
|
|
|
|
|
+pub static IMAP_TASK_STORAGE: Lazy<Mutex<ImapTaskStorage>> = Lazy::new(|| Mutex::new(ImapTaskStorage::new()));
|
|
|
|
|
|
|
|
|
|
/// appends an extension to a path
|
|
/// appends an extension to a path
|
|
@@ -311,8 +328,8 @@ pub async fn create_folder_lar(name: String) -> anyhow::Result<()>{
|
|
|
|
|
|
// start monitoring
|
|
// start monitoring
|
|
let handler = check_for_updates(name.clone());
|
|
let handler = check_for_updates(name.clone());
|
|
- let mut idle_task_storage = IDLE_TASK_STORAGE.lock().unwrap();
|
|
|
|
- idle_task_storage.add(name.clone(), handler);
|
|
|
|
|
|
+ let mut idle_task_storage = IMAP_TASK_STORAGE.lock().unwrap();
|
|
|
|
+ idle_task_storage.add_idle_task(name.clone(), handler);
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
}
|
|
}
|
|
@@ -333,8 +350,8 @@ pub async fn delete_folder_lar(name: String) -> anyhow::Result<()>{
|
|
}
|
|
}
|
|
|
|
|
|
// stop monitoring the folder
|
|
// stop monitoring the folder
|
|
- let mut idle_task_storage = IDLE_TASK_STORAGE.lock().unwrap();
|
|
|
|
- idle_task_storage.delete(name.clone());
|
|
|
|
|
|
+ let mut idle_task_storage = IMAP_TASK_STORAGE.lock().unwrap();
|
|
|
|
+ idle_task_storage.remove_idle_task(name.clone());
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
}
|
|
}
|
|
@@ -356,12 +373,12 @@ pub async fn rename_folder_lar(old_name: String, new_name: String) -> anyhow::Re
|
|
}
|
|
}
|
|
|
|
|
|
// stop monitoring old folder
|
|
// stop monitoring old folder
|
|
- let mut idle_task_storage = IDLE_TASK_STORAGE.lock().unwrap();
|
|
|
|
- idle_task_storage.delete(old_name.clone());
|
|
|
|
|
|
+ let mut idle_task_storage = IMAP_TASK_STORAGE.lock().unwrap();
|
|
|
|
+ idle_task_storage.remove_idle_task(old_name.clone());
|
|
|
|
|
|
// start monitoring new folder
|
|
// start monitoring new folder
|
|
let handler = check_for_updates(new_name.clone());
|
|
let handler = check_for_updates(new_name.clone());
|
|
- idle_task_storage.add(new_name.clone(), handler);
|
|
|
|
|
|
+ idle_task_storage.add_idle_task(new_name.clone(), handler);
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
}
|
|
}
|
|
@@ -379,35 +396,16 @@ fn parse_args(){
|
|
|
|
|
|
config.maildir = args.maildir.clone();
|
|
config.maildir = args.maildir.clone();
|
|
config.out_dir = args.out_dir.clone();
|
|
config.out_dir = args.out_dir.clone();
|
|
- INSTANCE.set(RwLock::new(config)).unwrap();
|
|
|
|
|
|
+ INSTANCE.set(config).unwrap();
|
|
}
|
|
}
|
|
|
|
|
|
-pub async fn run_download(){
|
|
|
|
- // downloading new emails
|
|
|
|
- match imap::download_email_from_imap().await {
|
|
|
|
- Ok(_) => {}
|
|
|
|
- Err(_) => println!("Cannot download new emails")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Uncomment to convert to HTML without downloading
|
|
|
|
- // let mut uid = 1; // fake uid for tests
|
|
|
|
- // for entry in std::fs::read_dir(Config::global().maildir.clone().join("INBOX").join("new")).expect("could not read maildir").filter_map(|m| m.ok()){
|
|
|
|
- // let path = entry.path();
|
|
|
|
- //
|
|
|
|
- // match add_email(path.clone(), uid.clone()){
|
|
|
|
- // Ok(_) => {}
|
|
|
|
- // Err(_) => {println!("Error adding email from {:?}", path.clone())}
|
|
|
|
- // };
|
|
|
|
- //
|
|
|
|
- // uid += 1;
|
|
|
|
- // }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+pub fn start_checkin_for_all_updates(){
|
|
// start updates monitoring
|
|
// start updates monitoring
|
|
for mailbox in Indexes::get_local_mailboxes() {
|
|
for mailbox in Indexes::get_local_mailboxes() {
|
|
let handler = check_for_updates(mailbox.clone());
|
|
let handler = check_for_updates(mailbox.clone());
|
|
- let mut idle_task_storage = IDLE_TASK_STORAGE.lock().unwrap();
|
|
|
|
- idle_task_storage.add(mailbox.clone(), handler);
|
|
|
|
|
|
+ let mut idle_task_storage = IMAP_TASK_STORAGE.lock().unwrap();
|
|
|
|
+ idle_task_storage.add_idle_task(mailbox.clone(), handler);
|
|
|
|
+ drop(idle_task_storage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -420,7 +418,10 @@ async fn run(){
|
|
run_api().await
|
|
run_api().await
|
|
});
|
|
});
|
|
|
|
|
|
- run_download().await;
|
|
|
|
|
|
+ let mut idle_task_storage = IMAP_TASK_STORAGE.lock().unwrap();
|
|
|
|
+ idle_task_storage.start_downloading();
|
|
|
|
+ drop(idle_task_storage);
|
|
|
|
+ start_checkin_for_all_updates();
|
|
|
|
|
|
// dont stop the program while API is running
|
|
// dont stop the program while API is running
|
|
api_task.await.expect("Error while running API");
|
|
api_task.await.expect("Error while running API");
|