use std::net::TcpStream; use native_tls::TlsStream; use crate::config::Config; pub(crate) enum ImapSession { Plain(imap::Session), Tls(imap::Session>), } impl ImapSession { pub(crate) fn noop(&mut self) -> imap::error::Result<()> { match self { Self::Plain(s) => s.noop(), Self::Tls(s) => s.noop(), } } pub(crate) fn logout(&mut self) -> imap::error::Result<()> { match self { Self::Plain(s) => s.logout(), Self::Tls(s) => s.logout(), } } } pub(crate) fn connect(config: &Config) -> Result { let imap_cfg = &config.imap; if imap_cfg.use_tls { let tls = native_tls::TlsConnector::builder() .build() .map_err(|e| e.to_string())?; let client = imap::connect( (&*imap_cfg.host, imap_cfg.port), &imap_cfg.host, &tls, ) .map_err(|e| e.to_string())?; let session = client .login(&imap_cfg.username, &imap_cfg.password) .map_err(|(e, _)| e.to_string())?; Ok(ImapSession::Tls(session)) } else { let stream = TcpStream::connect((&*imap_cfg.host, imap_cfg.port)).map_err(|e| e.to_string())?; let client = imap::Client::new(stream); let session = client .login(&imap_cfg.username, &imap_cfg.password) .map_err(|(e, _)| e.to_string())?; Ok(ImapSession::Plain(session)) } }