diff --git a/src/imap/client.rs b/src/imap/client.rs index 73bc7fbd6..b2674afa4 100644 --- a/src/imap/client.rs +++ b/src/imap/client.rs @@ -1,13 +1,11 @@ use async_imap::{ error::{Error as ImapError, Result as ImapResult}, - extensions::idle::Handle as ImapIdleHandle, - types::{Capabilities, Fetch, Mailbox, Name}, - Client as ImapClient, Session as ImapSession, + Client as ImapClient, }; use async_native_tls::TlsStream; use async_std::net::{self, TcpStream}; -use async_std::prelude::*; +use super::session::Session; use crate::login_param::{dc_build_tls, CertificateChecks}; #[derive(Debug)] @@ -16,18 +14,6 @@ pub(crate) enum Client { Insecure(ImapClient), } -#[derive(Debug)] -pub(crate) enum Session { - Secure(ImapSession>), - Insecure(ImapSession), -} - -#[derive(Debug)] -pub(crate) enum IdleHandle { - Secure(ImapIdleHandle>), - Insecure(ImapIdleHandle), -} - impl Client { pub async fn connect_secure>( addr: A, @@ -116,174 +102,3 @@ impl Client { } } } - -impl Session { - pub async fn capabilities(&mut self) -> ImapResult { - let res = match self { - Session::Secure(i) => i.capabilities().await?, - Session::Insecure(i) => i.capabilities().await?, - }; - - Ok(res) - } - - pub async fn list( - &mut self, - reference_name: Option<&str>, - mailbox_pattern: Option<&str>, - ) -> ImapResult> { - let res = match self { - Session::Secure(i) => { - i.list(reference_name, mailbox_pattern) - .await? - .collect::>() - .await? - } - Session::Insecure(i) => { - i.list(reference_name, mailbox_pattern) - .await? - .collect::>() - .await? - } - }; - Ok(res) - } - - pub async fn create>(&mut self, mailbox_name: S) -> ImapResult<()> { - match self { - Session::Secure(i) => i.create(mailbox_name).await?, - Session::Insecure(i) => i.create(mailbox_name).await?, - } - Ok(()) - } - - pub async fn subscribe>(&mut self, mailbox: S) -> ImapResult<()> { - match self { - Session::Secure(i) => i.subscribe(mailbox).await?, - Session::Insecure(i) => i.subscribe(mailbox).await?, - } - Ok(()) - } - - pub async fn close(&mut self) -> ImapResult<()> { - match self { - Session::Secure(i) => i.close().await?, - Session::Insecure(i) => i.close().await?, - } - Ok(()) - } - - pub async fn select>(&mut self, mailbox_name: S) -> ImapResult { - let mbox = match self { - Session::Secure(i) => i.select(mailbox_name).await?, - Session::Insecure(i) => i.select(mailbox_name).await?, - }; - - Ok(mbox) - } - - pub async fn fetch(&mut self, sequence_set: S1, query: S2) -> ImapResult> - where - S1: AsRef, - S2: AsRef, - { - let res = match self { - Session::Secure(i) => { - i.fetch(sequence_set, query) - .await? - .collect::>() - .await? - } - Session::Insecure(i) => { - i.fetch(sequence_set, query) - .await? - .collect::>() - .await? - } - }; - Ok(res) - } - - pub async fn uid_fetch(&mut self, uid_set: S1, query: S2) -> ImapResult> - where - S1: AsRef, - S2: AsRef, - { - let res = match self { - Session::Secure(i) => { - i.uid_fetch(uid_set, query) - .await? - .collect::>() - .await? - } - Session::Insecure(i) => { - i.uid_fetch(uid_set, query) - .await? - .collect::>() - .await? - } - }; - - Ok(res) - } - - pub fn idle(self) -> IdleHandle { - match self { - Session::Secure(i) => { - let h = i.idle(); - IdleHandle::Secure(h) - } - Session::Insecure(i) => { - let h = i.idle(); - IdleHandle::Insecure(h) - } - } - } - - pub async fn uid_store(&mut self, uid_set: S1, query: S2) -> ImapResult> - where - S1: AsRef, - S2: AsRef, - { - let res = match self { - Session::Secure(i) => { - i.uid_store(uid_set, query) - .await? - .collect::>() - .await? - } - Session::Insecure(i) => { - i.uid_store(uid_set, query) - .await? - .collect::>() - .await? - } - }; - Ok(res) - } - - pub async fn uid_mv, S2: AsRef>( - &mut self, - uid_set: S1, - mailbox_name: S2, - ) -> ImapResult<()> { - match self { - Session::Secure(i) => i.uid_mv(uid_set, mailbox_name).await?, - Session::Insecure(i) => i.uid_mv(uid_set, mailbox_name).await?, - } - Ok(()) - } - - pub async fn uid_copy, S2: AsRef>( - &mut self, - uid_set: S1, - mailbox_name: S2, - ) -> ImapResult<()> { - match self { - Session::Secure(i) => i.uid_copy(uid_set, mailbox_name).await?, - Session::Insecure(i) => i.uid_copy(uid_set, mailbox_name).await?, - } - - Ok(()) - } -} diff --git a/src/imap/idle.rs b/src/imap/idle.rs index 5c87dee19..f6962efea 100644 --- a/src/imap/idle.rs +++ b/src/imap/idle.rs @@ -8,8 +8,8 @@ use std::time::{Duration, SystemTime}; use crate::context::Context; -use super::client::{IdleHandle, Session}; use super::select_folder; +use super::session::{IdleHandle, Session}; type Result = std::result::Result; diff --git a/src/imap/mod.rs b/src/imap/mod.rs index 9b57cf67e..626a92feb 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -32,8 +32,10 @@ use crate::stock::StockMessage; mod client; mod idle; pub mod select_folder; +mod session; -use client::{Client, Session}; +use client::Client; +use session::Session; type Result = std::result::Result; diff --git a/src/imap/session.rs b/src/imap/session.rs new file mode 100644 index 000000000..c4a1ae9d7 --- /dev/null +++ b/src/imap/session.rs @@ -0,0 +1,192 @@ +use async_imap::{ + error::Result as ImapResult, + extensions::idle::Handle as ImapIdleHandle, + types::{Capabilities, Fetch, Mailbox, Name}, + Session as ImapSession, +}; +use async_native_tls::TlsStream; +use async_std::net::TcpStream; +use async_std::prelude::*; + +#[derive(Debug)] +pub(crate) enum Session { + Secure(ImapSession>), + Insecure(ImapSession), +} + +#[derive(Debug)] +pub(crate) enum IdleHandle { + Secure(ImapIdleHandle>), + Insecure(ImapIdleHandle), +} + +impl Session { + pub async fn capabilities(&mut self) -> ImapResult { + let res = match self { + Session::Secure(i) => i.capabilities().await?, + Session::Insecure(i) => i.capabilities().await?, + }; + + Ok(res) + } + + pub async fn list( + &mut self, + reference_name: Option<&str>, + mailbox_pattern: Option<&str>, + ) -> ImapResult> { + let res = match self { + Session::Secure(i) => { + i.list(reference_name, mailbox_pattern) + .await? + .collect::>() + .await? + } + Session::Insecure(i) => { + i.list(reference_name, mailbox_pattern) + .await? + .collect::>() + .await? + } + }; + Ok(res) + } + + pub async fn create>(&mut self, mailbox_name: S) -> ImapResult<()> { + match self { + Session::Secure(i) => i.create(mailbox_name).await?, + Session::Insecure(i) => i.create(mailbox_name).await?, + } + Ok(()) + } + + pub async fn subscribe>(&mut self, mailbox: S) -> ImapResult<()> { + match self { + Session::Secure(i) => i.subscribe(mailbox).await?, + Session::Insecure(i) => i.subscribe(mailbox).await?, + } + Ok(()) + } + + pub async fn close(&mut self) -> ImapResult<()> { + match self { + Session::Secure(i) => i.close().await?, + Session::Insecure(i) => i.close().await?, + } + Ok(()) + } + + pub async fn select>(&mut self, mailbox_name: S) -> ImapResult { + let mbox = match self { + Session::Secure(i) => i.select(mailbox_name).await?, + Session::Insecure(i) => i.select(mailbox_name).await?, + }; + + Ok(mbox) + } + + pub async fn fetch(&mut self, sequence_set: S1, query: S2) -> ImapResult> + where + S1: AsRef, + S2: AsRef, + { + let res = match self { + Session::Secure(i) => { + i.fetch(sequence_set, query) + .await? + .collect::>() + .await? + } + Session::Insecure(i) => { + i.fetch(sequence_set, query) + .await? + .collect::>() + .await? + } + }; + Ok(res) + } + + pub async fn uid_fetch(&mut self, uid_set: S1, query: S2) -> ImapResult> + where + S1: AsRef, + S2: AsRef, + { + let res = match self { + Session::Secure(i) => { + i.uid_fetch(uid_set, query) + .await? + .collect::>() + .await? + } + Session::Insecure(i) => { + i.uid_fetch(uid_set, query) + .await? + .collect::>() + .await? + } + }; + + Ok(res) + } + + pub fn idle(self) -> IdleHandle { + match self { + Session::Secure(i) => { + let h = i.idle(); + IdleHandle::Secure(h) + } + Session::Insecure(i) => { + let h = i.idle(); + IdleHandle::Insecure(h) + } + } + } + + pub async fn uid_store(&mut self, uid_set: S1, query: S2) -> ImapResult> + where + S1: AsRef, + S2: AsRef, + { + let res = match self { + Session::Secure(i) => { + i.uid_store(uid_set, query) + .await? + .collect::>() + .await? + } + Session::Insecure(i) => { + i.uid_store(uid_set, query) + .await? + .collect::>() + .await? + } + }; + Ok(res) + } + + pub async fn uid_mv, S2: AsRef>( + &mut self, + uid_set: S1, + mailbox_name: S2, + ) -> ImapResult<()> { + match self { + Session::Secure(i) => i.uid_mv(uid_set, mailbox_name).await?, + Session::Insecure(i) => i.uid_mv(uid_set, mailbox_name).await?, + } + Ok(()) + } + + pub async fn uid_copy, S2: AsRef>( + &mut self, + uid_set: S1, + mailbox_name: S2, + ) -> ImapResult<()> { + match self { + Session::Secure(i) => i.uid_copy(uid_set, mailbox_name).await?, + Session::Insecure(i) => i.uid_copy(uid_set, mailbox_name).await?, + } + + Ok(()) + } +}