|
@@ -57,15 +57,20 @@ pub async fn connect_to_imap() -> anyhow::Result<Client<TlsStream<TcpStream>>>{
|
|
Ok(client)
|
|
Ok(client)
|
|
}
|
|
}
|
|
|
|
|
|
-/// download all emails from the server and persist them
|
|
|
|
-pub async fn download_email_from_imap() -> anyhow::Result<Vec<(u32, PathBuf)>>{
|
|
|
|
|
|
+async fn get_session() -> anyhow::Result<Session<TlsStream<TcpStream>>>{
|
|
let mut client = connect_to_imap().await?;
|
|
let mut client = connect_to_imap().await?;
|
|
let mut session_result = client.login(Config::global().username.clone(), Config::global().password.clone()).await;
|
|
let mut session_result = client.login(Config::global().username.clone(), Config::global().password.clone()).await;
|
|
- let mut session = match session_result {
|
|
|
|
|
|
+ let session = match session_result {
|
|
Ok(session) => {session}
|
|
Ok(session) => {session}
|
|
Err(_) => {return Err(anyhow!("Unable to login to IMAP server"))}
|
|
Err(_) => {return Err(anyhow!("Unable to login to IMAP server"))}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ Ok(session)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/// download all emails from the server and persist them
|
|
|
|
+pub async fn download_email_from_imap() -> anyhow::Result<Vec<(u32, PathBuf)>>{
|
|
|
|
+ let mut session = get_session().await?;
|
|
let mut stored_paths: Vec<(u32, PathBuf)> = vec![];
|
|
let mut stored_paths: Vec<(u32, PathBuf)> = vec![];
|
|
match list_imap_folders(&mut session).await {
|
|
match list_imap_folders(&mut session).await {
|
|
Ok(folders) => {
|
|
Ok(folders) => {
|
|
@@ -246,14 +251,8 @@ fn update_local_uids(uid: String, list: String, uids_path: PathBuf) -> anyhow::R
|
|
|
|
|
|
/// delete single message remotely by uid (marks deleted and deletes from the server at the same time)
|
|
/// delete single message remotely by uid (marks deleted and deletes from the server at the same time)
|
|
pub async fn delete_email_by_uid(list: String, uid: u32) -> anyhow::Result<()>{
|
|
pub async fn delete_email_by_uid(list: String, uid: u32) -> anyhow::Result<()>{
|
|
- // TODO split to mark_deleted() and expunge()
|
|
|
|
- let mut client = connect_to_imap().await?;
|
|
|
|
- let mut session_result = client.login(Config::global().username.clone(), Config::global().password.clone()).await;
|
|
|
|
- let mut session = match session_result {
|
|
|
|
- Ok(session) => {session}
|
|
|
|
- Err(_) => {return Err(anyhow!("Unable to login to IMAP server"))}
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
|
|
+ let mut session = get_session().await?;
|
|
|
|
+
|
|
session.select(list.clone()).await?;
|
|
session.select(list.clone()).await?;
|
|
let updates_stream = session.store(format!("{}", uid), "+FLAGS (\\Deleted)").await?;
|
|
let updates_stream = session.store(format!("{}", uid), "+FLAGS (\\Deleted)").await?;
|
|
let _updates: Vec<_> = updates_stream.try_collect().await?;
|
|
let _updates: Vec<_> = updates_stream.try_collect().await?;
|
|
@@ -265,12 +264,8 @@ pub async fn delete_email_by_uid(list: String, uid: u32) -> anyhow::Result<()>{
|
|
|
|
|
|
/// create a folder remotely
|
|
/// create a folder remotely
|
|
pub async fn create_folder(name: String) -> anyhow::Result<()>{
|
|
pub async fn create_folder(name: String) -> anyhow::Result<()>{
|
|
- let mut client = connect_to_imap().await?;
|
|
|
|
- let mut session_result = client.login(Config::global().username.clone(), Config::global().password.clone()).await;
|
|
|
|
- let mut session = match session_result {
|
|
|
|
- Ok(session) => {session}
|
|
|
|
- Err(_) => {return Err(anyhow!("Unable to login to IMAP server"))}
|
|
|
|
- };
|
|
|
|
|
|
+ let mut session = get_session().await?;
|
|
|
|
+
|
|
session.create(name.clone()).await?;
|
|
session.create(name.clone()).await?;
|
|
session.logout().await?;
|
|
session.logout().await?;
|
|
Ok(())
|
|
Ok(())
|
|
@@ -278,12 +273,8 @@ pub async fn create_folder(name: String) -> anyhow::Result<()>{
|
|
|
|
|
|
/// rename a folder remotely
|
|
/// rename a folder remotely
|
|
pub async fn rename_folder(name: String, new_name: String) -> anyhow::Result<()>{
|
|
pub async fn rename_folder(name: String, new_name: String) -> anyhow::Result<()>{
|
|
- let mut client = connect_to_imap().await?;
|
|
|
|
- let mut session_result = client.login(Config::global().username.clone(), Config::global().password.clone()).await;
|
|
|
|
- let mut session = match session_result {
|
|
|
|
- Ok(session) => {session}
|
|
|
|
- Err(_) => {return Err(anyhow!("Unable to login to IMAP server"))}
|
|
|
|
- };
|
|
|
|
|
|
+ let mut session = get_session().await?;
|
|
|
|
+
|
|
session.rename(name.clone(), new_name.clone()).await?;
|
|
session.rename(name.clone(), new_name.clone()).await?;
|
|
session.logout().await?;
|
|
session.logout().await?;
|
|
Ok(())
|
|
Ok(())
|
|
@@ -291,12 +282,8 @@ pub async fn rename_folder(name: String, new_name: String) -> anyhow::Result<()>
|
|
|
|
|
|
/// delete a folder remotely
|
|
/// delete a folder remotely
|
|
pub async fn delete_folder(name: String) -> anyhow::Result<()>{
|
|
pub async fn delete_folder(name: String) -> anyhow::Result<()>{
|
|
- let mut client = connect_to_imap().await?;
|
|
|
|
- let mut session_result = client.login(Config::global().username.clone(), Config::global().password.clone()).await;
|
|
|
|
- let mut session = match session_result {
|
|
|
|
- Ok(session) => {session}
|
|
|
|
- Err(_) => {return Err(anyhow!("Unable to login to IMAP server"))}
|
|
|
|
- };
|
|
|
|
|
|
+ let mut session = get_session().await?;
|
|
|
|
+
|
|
session.delete(name.clone()).await?;
|
|
session.delete(name.clone()).await?;
|
|
session.logout().await?;
|
|
session.logout().await?;
|
|
Ok(())
|
|
Ok(())
|
|
@@ -304,22 +291,7 @@ pub async fn delete_folder(name: String) -> anyhow::Result<()>{
|
|
|
|
|
|
/// deletes all emails locally that were deleted remotely
|
|
/// deletes all emails locally that were deleted remotely
|
|
pub async fn remove_deleted_emails(mailbox: String) -> anyhow::Result<u32> {
|
|
pub async fn remove_deleted_emails(mailbox: String) -> anyhow::Result<u32> {
|
|
- // TODO move session creation to the function
|
|
|
|
- let mut client = match connect_to_imap().await {
|
|
|
|
- Ok(client) => client,
|
|
|
|
- Err(e) => {
|
|
|
|
- return Err(anyhow!("Failed to connect to IMAP"));
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- let session_result = client
|
|
|
|
- .login(Config::global().username.clone(), Config::global().password.clone())
|
|
|
|
- .await;
|
|
|
|
-
|
|
|
|
- let mut session = match session_result {
|
|
|
|
- Ok(session) => session,
|
|
|
|
- Err(_) => return Err(anyhow!("Unable to login to IMAP server")),
|
|
|
|
- };
|
|
|
|
|
|
+ let mut session = get_session().await?;
|
|
|
|
|
|
session.select(mailbox.clone()).await?;
|
|
session.select(mailbox.clone()).await?;
|
|
|
|
|
|
@@ -350,21 +322,7 @@ pub async fn remove_deleted_emails(mailbox: String) -> anyhow::Result<u32> {
|
|
/// run a async task to monitor any changes in the mailbox
|
|
/// run a async task to monitor any changes in the mailbox
|
|
pub fn check_for_updates(mailbox: String) -> JoinHandle<Result<(), anyhow::Error>> {
|
|
pub fn check_for_updates(mailbox: String) -> JoinHandle<Result<(), anyhow::Error>> {
|
|
task::spawn(async move {
|
|
task::spawn(async move {
|
|
- let mut client = match connect_to_imap().await {
|
|
|
|
- Ok(client) => client,
|
|
|
|
- Err(e) => {
|
|
|
|
- return Err::<(), anyhow::Error>(anyhow!("Failed to connect to IMAP"));
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- let session_result = client
|
|
|
|
- .login(Config::global().username.clone(), Config::global().password.clone())
|
|
|
|
- .await;
|
|
|
|
-
|
|
|
|
- let mut session = match session_result {
|
|
|
|
- Ok(session) => session,
|
|
|
|
- Err(_) => return Err(anyhow!("Unable to login to IMAP server")),
|
|
|
|
- };
|
|
|
|
|
|
+ let mut session = get_session().await?;
|
|
|
|
|
|
session.select(mailbox.clone()).await?;
|
|
session.select(mailbox.clone()).await?;
|
|
|
|
|