mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
Factor src/imap/session.rs out of src/imap/client.rs
This commit is contained in:
committed by
holger krekel
parent
4eb9660bfa
commit
4c42acc7e1
@@ -1,13 +1,11 @@
|
|||||||
use async_imap::{
|
use async_imap::{
|
||||||
error::{Error as ImapError, Result as ImapResult},
|
error::{Error as ImapError, Result as ImapResult},
|
||||||
extensions::idle::Handle as ImapIdleHandle,
|
Client as ImapClient,
|
||||||
types::{Capabilities, Fetch, Mailbox, Name},
|
|
||||||
Client as ImapClient, Session as ImapSession,
|
|
||||||
};
|
};
|
||||||
use async_native_tls::TlsStream;
|
use async_native_tls::TlsStream;
|
||||||
use async_std::net::{self, TcpStream};
|
use async_std::net::{self, TcpStream};
|
||||||
use async_std::prelude::*;
|
|
||||||
|
|
||||||
|
use super::session::Session;
|
||||||
use crate::login_param::{dc_build_tls, CertificateChecks};
|
use crate::login_param::{dc_build_tls, CertificateChecks};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -16,18 +14,6 @@ pub(crate) enum Client {
|
|||||||
Insecure(ImapClient<TcpStream>),
|
Insecure(ImapClient<TcpStream>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) enum Session {
|
|
||||||
Secure(ImapSession<TlsStream<TcpStream>>),
|
|
||||||
Insecure(ImapSession<TcpStream>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) enum IdleHandle {
|
|
||||||
Secure(ImapIdleHandle<TlsStream<TcpStream>>),
|
|
||||||
Insecure(ImapIdleHandle<TcpStream>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub async fn connect_secure<A: net::ToSocketAddrs, S: AsRef<str>>(
|
pub async fn connect_secure<A: net::ToSocketAddrs, S: AsRef<str>>(
|
||||||
addr: A,
|
addr: A,
|
||||||
@@ -116,174 +102,3 @@ impl Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Session {
|
|
||||||
pub async fn capabilities(&mut self) -> ImapResult<Capabilities> {
|
|
||||||
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<Vec<Name>> {
|
|
||||||
let res = match self {
|
|
||||||
Session::Secure(i) => {
|
|
||||||
i.list(reference_name, mailbox_pattern)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
Session::Insecure(i) => {
|
|
||||||
i.list(reference_name, mailbox_pattern)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create<S: AsRef<str>>(&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<S: AsRef<str>>(&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<S: AsRef<str>>(&mut self, mailbox_name: S) -> ImapResult<Mailbox> {
|
|
||||||
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<S1, S2>(&mut self, sequence_set: S1, query: S2) -> ImapResult<Vec<Fetch>>
|
|
||||||
where
|
|
||||||
S1: AsRef<str>,
|
|
||||||
S2: AsRef<str>,
|
|
||||||
{
|
|
||||||
let res = match self {
|
|
||||||
Session::Secure(i) => {
|
|
||||||
i.fetch(sequence_set, query)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
Session::Insecure(i) => {
|
|
||||||
i.fetch(sequence_set, query)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn uid_fetch<S1, S2>(&mut self, uid_set: S1, query: S2) -> ImapResult<Vec<Fetch>>
|
|
||||||
where
|
|
||||||
S1: AsRef<str>,
|
|
||||||
S2: AsRef<str>,
|
|
||||||
{
|
|
||||||
let res = match self {
|
|
||||||
Session::Secure(i) => {
|
|
||||||
i.uid_fetch(uid_set, query)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
Session::Insecure(i) => {
|
|
||||||
i.uid_fetch(uid_set, query)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.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<S1, S2>(&mut self, uid_set: S1, query: S2) -> ImapResult<Vec<Fetch>>
|
|
||||||
where
|
|
||||||
S1: AsRef<str>,
|
|
||||||
S2: AsRef<str>,
|
|
||||||
{
|
|
||||||
let res = match self {
|
|
||||||
Session::Secure(i) => {
|
|
||||||
i.uid_store(uid_set, query)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
Session::Insecure(i) => {
|
|
||||||
i.uid_store(uid_set, query)
|
|
||||||
.await?
|
|
||||||
.collect::<ImapResult<_>>()
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn uid_mv<S1: AsRef<str>, S2: AsRef<str>>(
|
|
||||||
&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<S1: AsRef<str>, S2: AsRef<str>>(
|
|
||||||
&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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use std::time::{Duration, SystemTime};
|
|||||||
|
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
|
||||||
use super::client::{IdleHandle, Session};
|
|
||||||
use super::select_folder;
|
use super::select_folder;
|
||||||
|
use super::session::{IdleHandle, Session};
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Error>;
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,10 @@ use crate::stock::StockMessage;
|
|||||||
mod client;
|
mod client;
|
||||||
mod idle;
|
mod idle;
|
||||||
pub mod select_folder;
|
pub mod select_folder;
|
||||||
|
mod session;
|
||||||
|
|
||||||
use client::{Client, Session};
|
use client::Client;
|
||||||
|
use session::Session;
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Error>;
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
|||||||
192
src/imap/session.rs
Normal file
192
src/imap/session.rs
Normal file
@@ -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<TlsStream<TcpStream>>),
|
||||||
|
Insecure(ImapSession<TcpStream>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) enum IdleHandle {
|
||||||
|
Secure(ImapIdleHandle<TlsStream<TcpStream>>),
|
||||||
|
Insecure(ImapIdleHandle<TcpStream>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Session {
|
||||||
|
pub async fn capabilities(&mut self) -> ImapResult<Capabilities> {
|
||||||
|
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<Vec<Name>> {
|
||||||
|
let res = match self {
|
||||||
|
Session::Secure(i) => {
|
||||||
|
i.list(reference_name, mailbox_pattern)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
Session::Insecure(i) => {
|
||||||
|
i.list(reference_name, mailbox_pattern)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create<S: AsRef<str>>(&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<S: AsRef<str>>(&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<S: AsRef<str>>(&mut self, mailbox_name: S) -> ImapResult<Mailbox> {
|
||||||
|
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<S1, S2>(&mut self, sequence_set: S1, query: S2) -> ImapResult<Vec<Fetch>>
|
||||||
|
where
|
||||||
|
S1: AsRef<str>,
|
||||||
|
S2: AsRef<str>,
|
||||||
|
{
|
||||||
|
let res = match self {
|
||||||
|
Session::Secure(i) => {
|
||||||
|
i.fetch(sequence_set, query)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
Session::Insecure(i) => {
|
||||||
|
i.fetch(sequence_set, query)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn uid_fetch<S1, S2>(&mut self, uid_set: S1, query: S2) -> ImapResult<Vec<Fetch>>
|
||||||
|
where
|
||||||
|
S1: AsRef<str>,
|
||||||
|
S2: AsRef<str>,
|
||||||
|
{
|
||||||
|
let res = match self {
|
||||||
|
Session::Secure(i) => {
|
||||||
|
i.uid_fetch(uid_set, query)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
Session::Insecure(i) => {
|
||||||
|
i.uid_fetch(uid_set, query)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.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<S1, S2>(&mut self, uid_set: S1, query: S2) -> ImapResult<Vec<Fetch>>
|
||||||
|
where
|
||||||
|
S1: AsRef<str>,
|
||||||
|
S2: AsRef<str>,
|
||||||
|
{
|
||||||
|
let res = match self {
|
||||||
|
Session::Secure(i) => {
|
||||||
|
i.uid_store(uid_set, query)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
Session::Insecure(i) => {
|
||||||
|
i.uid_store(uid_set, query)
|
||||||
|
.await?
|
||||||
|
.collect::<ImapResult<_>>()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn uid_mv<S1: AsRef<str>, S2: AsRef<str>>(
|
||||||
|
&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<S1: AsRef<str>, S2: AsRef<str>>(
|
||||||
|
&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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user