|
@@ -1,7 +1,7 @@
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::{io};
|
|
use std::{io};
|
|
use std::convert::TryFrom;
|
|
use std::convert::TryFrom;
|
|
-use std::fs::File;
|
|
|
|
|
|
+use std::fs::{create_dir_all, File};
|
|
use std::future::Future;
|
|
use std::future::Future;
|
|
use std::path::{PathBuf};
|
|
use std::path::{PathBuf};
|
|
use std::io::{BufReader, BufWriter, ErrorKind, Read, Write};
|
|
use std::io::{BufReader, BufWriter, ErrorKind, Read, Write};
|
|
@@ -29,6 +29,7 @@ use async_imap::extensions::idle::IdleResponse::NewData;
|
|
use async_imap_wasi::{Client, Session};
|
|
use async_imap_wasi::{Client, Session};
|
|
#[cfg(target_os = "wasi")]
|
|
#[cfg(target_os = "wasi")]
|
|
use async_imap_wasi::extensions::idle::IdleResponse::NewData;
|
|
use async_imap_wasi::extensions::idle::IdleResponse::NewData;
|
|
|
|
+use crate::indexes::Indexes;
|
|
|
|
|
|
/// create TLS connect with the IMAP server
|
|
/// create TLS connect with the IMAP server
|
|
pub async fn connect_to_imap() -> anyhow::Result<Client<TlsStream<TcpStream>>>{
|
|
pub async fn connect_to_imap() -> anyhow::Result<Client<TlsStream<TcpStream>>>{
|
|
@@ -104,14 +105,17 @@ pub async fn list_imap_folders(session: &mut Session<TlsStream<TcpStream>>) -> a
|
|
|
|
|
|
/// download all emails from one mailbox
|
|
/// download all emails from one mailbox
|
|
pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>, list: String) -> anyhow::Result<Vec<(u32, PathBuf)>> {
|
|
pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>, list: String) -> anyhow::Result<Vec<(u32, PathBuf)>> {
|
|
|
|
+ let out_dir_name = Config::global().out_dir.clone().join(list.clone());
|
|
|
|
+ if !out_dir_name.exists() { create_dir_all(out_dir_name).ok(); }
|
|
|
|
+
|
|
session.select(list.clone()).await?;
|
|
session.select(list.clone()).await?;
|
|
|
|
|
|
// Create directories for maildir
|
|
// Create directories for maildir
|
|
- std::fs::create_dir_all(Config::global().maildir.clone().join(list.clone()).join("new"))
|
|
|
|
|
|
+ create_dir_all(Config::global().maildir.clone().join(list.clone()).join("new"))
|
|
.expect(&*("Unable to create 'new' directory in ".to_owned() + &*list.clone()));
|
|
.expect(&*("Unable to create 'new' directory in ".to_owned() + &*list.clone()));
|
|
- std::fs::create_dir_all(Config::global().maildir.clone().join(list.clone()).join("cur"))
|
|
|
|
|
|
+ create_dir_all(Config::global().maildir.clone().join(list.clone()).join("cur"))
|
|
.expect(&*("Unable to create 'cur' directory in ".to_owned() + &*list.clone()));
|
|
.expect(&*("Unable to create 'cur' directory in ".to_owned() + &*list.clone()));
|
|
- std::fs::create_dir_all(Config::global().maildir.clone().join(list.clone()).join("tmp"))
|
|
|
|
|
|
+ create_dir_all(Config::global().maildir.clone().join(list.clone()).join("tmp"))
|
|
.expect(&*("Unable to create 'tmp' directory in ".to_owned() + &*list.clone()));
|
|
.expect(&*("Unable to create 'tmp' directory in ".to_owned() + &*list.clone()));
|
|
|
|
|
|
let uids_path = Config::global().maildir.clone().join(".uids.json");
|
|
let uids_path = Config::global().maildir.clone().join(".uids.json");
|
|
@@ -144,7 +148,21 @@ pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>,
|
|
if let Some(body) = msg.body() {
|
|
if let Some(body) = msg.body() {
|
|
let mail_file = store(Config::global().maildir.clone().join(list.clone()), uid.clone().to_string(), "new".to_string(), body, "");
|
|
let mail_file = store(Config::global().maildir.clone().join(list.clone()), uid.clone().to_string(), "new".to_string(), body, "");
|
|
match mail_file {
|
|
match mail_file {
|
|
- Ok(file) => stored_paths.push((uid.to_string().parse().unwrap(), file)),
|
|
|
|
|
|
+ Ok(file) => {
|
|
|
|
+ // TODO convert and persist html
|
|
|
|
+ // persist to the maildir
|
|
|
|
+ stored_paths.push((uid.to_string().parse().unwrap(), file.clone()));
|
|
|
|
+
|
|
|
|
+ // persist to the output dir
|
|
|
|
+ match add_email(file.clone(), uid.clone()){
|
|
|
|
+ Ok(_) => {}
|
|
|
|
+ Err(_) => {println!("Error adding email from {:?}", file.clone())}
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // TODO adjust indexes
|
|
|
|
+ Indexes::persist_threads().expect("Unable to persist threads");
|
|
|
|
+ Indexes::persist_indexes(vec![list.clone()]).expect("Unable to persist indexes");
|
|
|
|
+ },
|
|
Err(e) => eprintln!("Failed to store email: {}", e),
|
|
Err(e) => eprintln!("Failed to store email: {}", e),
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -317,13 +335,14 @@ pub async fn check_for_updates(mailbox: String) -> anyhow::Result<()> {
|
|
// TODO add more cases like delete, move...
|
|
// TODO add more cases like delete, move...
|
|
NewData(data) => {
|
|
NewData(data) => {
|
|
// TODO do not update all emails (IMAP returns * {number} RECENT) and do it only for one mailbox
|
|
// TODO do not update all emails (IMAP returns * {number} RECENT) and do it only for one mailbox
|
|
- let new_paths = download_email_from_imap().await.expect("Cannot download new emails");
|
|
|
|
- for (uid, path) in new_paths.clone() {
|
|
|
|
- match add_email(path.clone(), uid.clone()){
|
|
|
|
- Ok(_) => {}
|
|
|
|
- Err(_) => {println!("Error adding email from {:?}", path.clone())}
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
|
|
+ // TODO CHANGE IT!!!
|
|
|
|
+ // let new_paths = download_email_from_imap().await.expect("Cannot download new emails");
|
|
|
|
+ // for (uid, path) in new_paths.clone() {
|
|
|
|
+ // match add_email(path.clone(), uid.clone()){
|
|
|
|
+ // Ok(_) => {}
|
|
|
|
+ // Err(_) => {println!("Error adding email from {:?}", path.clone())}
|
|
|
|
+ // };
|
|
|
|
+ // }
|
|
}
|
|
}
|
|
reason => {
|
|
reason => {
|
|
println!("IDLE failed {:?}", reason);
|
|
println!("IDLE failed {:?}", reason);
|