diff --git a/imap/src/client/inner.rs b/imap/src/client/inner.rs index c529aa0..e62165c 100644 --- a/imap/src/client/inner.rs +++ b/imap/src/client/inner.rs @@ -1,11 +1,12 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{HashMap, HashSet, VecDeque}; +use std::mem; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll, Waker}; use anyhow::{Context as AnyhowContext, Result}; use futures::future::{self, Either, Future, FutureExt}; -use parking_lot::RwLock; +use parking_lot::{RwLock, RwLockWriteGuard}; use tokio::{ io::{ self, AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, ReadHalf, WriteHalf, @@ -18,14 +19,15 @@ use tokio_rustls::{ }; use crate::command::Command; +use crate::parser::{parse_capability, parse_response}; use crate::response::{Capability, Response, ResponseCode, Status}; // use crate::types::{Capability as Capability_, Status}; use super::ClientConfig; pub type CapsLock = Arc>>>; -pub type ResultMap = Arc, Option)>>>; -pub type GreetingState = Arc)>>; +pub type ResultMap = Arc, Vec, Option)>>>; +pub type GreetingState = Arc, Option)>>; pub const TAG_PREFIX: &str = "panorama"; /// The lower-level Client struct, that is shared by all of the exported structs in the state machine. @@ -56,9 +58,9 @@ where /// Creates a new client that wraps a connection pub fn new(conn: C, config: ClientConfig) -> Self { let (read_half, write_half) = io::split(conn); - let results = Arc::new(RwLock::new(HashMap::new())); + let results = Arc::new(RwLock::new(VecDeque::new())); let (exit_tx, exit_rx) = mpsc::channel(1); - let greeting = Arc::new(RwLock::new((false, None))); + let greeting = Arc::new(RwLock::new((None, None))); let caps: CapsLock = Arc::new(RwLock::new(None)); let listener_handle = tokio::spawn(listen( @@ -88,13 +90,13 @@ where } /// Sends a command to the server and returns a handle to retrieve the result - pub async fn execute(&mut self, cmd: Command) -> Result { + pub async fn execute(&mut self, cmd: Command) -> Result<(Response, Vec)> { debug!("executing command {:?}", cmd); let id = self.id; self.id += 1; { let mut handlers = self.results.write(); - handlers.insert(id, (None, None)); + handlers.push_back((id, None, vec![], None)); } let cmd_str = format!("{}{} {}\r\n", TAG_PREFIX, id, cmd); @@ -103,23 +105,39 @@ where self.conn.flush().await?; debug!("[{}] written.", id); - ExecWaiter(self, id).await; - let resp = { - let mut handlers = self.results.write(); - handlers.remove(&id).unwrap().0.unwrap() - }; + let resp = ExecWaiter(self, id, false).await; + // let resp = { + // let mut handlers = self.results.write(); + // handlers.remove(&id).unwrap().0.unwrap() + // }; Ok(resp) } /// Executes the CAPABILITY command - pub async fn capabilities(&mut self) -> Result<()> { + pub async fn capabilities(&mut self, force: bool) -> Result<()> { + { + let caps = &*self.caps.read(); + if caps.is_some() && !force { + return Ok(()); + } + } + let cmd = Command::Capability; debug!("sending: {:?} {:?}", cmd, cmd.to_string()); - let result = self + let (result, intermediate) = self .execute(cmd) .await .context("error executing CAPABILITY command")?; debug!("cap resp: {:?}", result); + + if let Some(Response::Capabilities(new_caps)) = intermediate + .iter() + .find(|resp| matches!(resp, Response::Capabilities(_))) + { + let caps = &mut *self.caps.write(); + *caps = Some(new_caps.iter().cloned().collect()); + } + // if let Response::Capabilities(caps) = resp { // debug!("capabilities: {:?}", caps); // } @@ -159,24 +177,12 @@ where } /// Check if this client has a particular capability - pub async fn has_capability(&self, cap: impl AsRef) -> Result { + pub async fn has_capability(&mut self, cap: impl AsRef) -> Result { let cap = cap.as_ref().to_owned(); debug!("checking for the capability: {:?}", cap); + let cap = parse_capability(cap)?; - let cap_bytes = cap.as_bytes(); - debug!("cap_bytes {:?}", cap_bytes); - // let (_, cap) = match crate::oldparser::rfc3501::capability(cap_bytes) { - // Ok(v) => v, - // Err(err) => { - // error!("ERROR PARSING {:?} {} {:?}", cap, err, err); - // use std::error::Error; - // let bt = err.backtrace().unwrap(); - // error!("{}", bt); - // std::process::exit(1); - // } - // }; - let cap = Capability::from(Capability::Atom(cap)); - + self.capabilities(false).await?; let caps = &*self.caps.read(); // TODO: refresh caps @@ -190,7 +196,7 @@ where pub struct GreetingWaiter(GreetingState); impl Future for GreetingWaiter { - type Output = (); + type Output = Response; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { let (state, waker) = &mut *self.0.write(); debug!("g {:?}", state); @@ -198,32 +204,43 @@ impl Future for GreetingWaiter { *waker = Some(cx.waker().clone()); } - match state { - true => Poll::Ready(()), - false => Poll::Pending, + match state.take() { + Some(v) => Poll::Ready(v), + None => Poll::Pending, } } } -pub struct ExecWaiter<'a, C>(&'a Client, usize); +pub struct ExecWaiter<'a, C>(&'a Client, usize, bool); impl<'a, C> Future for ExecWaiter<'a, C> { - type Output = (); - fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { - let mut handlers = self.0.results.write(); - let state = handlers.get_mut(&self.1); - - // TODO: handle the None case here - debug!("f[{}] {:?}", self.1, state); - let (result, waker) = state.unwrap(); - - match result { - Some(_) => Poll::Ready(()), - None => { - *waker = Some(cx.waker().clone()); - Poll::Pending + type Output = (Response, Vec); + fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll { + // add the waker + let mut results = self.0.results.write(); + if !self.2 { + if let Some((_, _, _, waker_ref)) = + results.iter_mut().find(|(id, _, _, _)| *id == self.1) + { + let waker = cx.waker().clone(); + *waker_ref = Some(waker); + self.2 = true; } } + + // if this struct exists then there's definitely at least one entry + let (id, last_response, _, _) = &results[0]; + if *id != self.1 || last_response.is_none() { + return Poll::Pending; + } + + let (_, last_response, intermediate_responses, _) = results.pop_front().unwrap(); + mem::drop(results); + + Poll::Ready(( + last_response.expect("already checked"), + intermediate_responses, + )) } } @@ -257,26 +274,17 @@ where } debug!("got a new line {:?}", next_line); - let resp = Response::Capabilities(vec![]); - // let (_, resp) = match crate::oldparser::parse_response(next_line.as_bytes()) { - // Ok(v) => v, - // Err(err) => { - // debug!("shiet: {:?}", err); - // continue; - // } - // }; + let resp = parse_response(next_line)?; if let Some(greeting) = greeting.take() { let (greeting, waker) = &mut *greeting.write(); debug!("received greeting!"); - *greeting = true; + *greeting = Some(resp.clone()); if let Some(waker) = waker.take() { waker.wake(); } } - let resp = Response::from(resp); - debug!("resp: {:?}", resp); match &resp { // capabilities list Response::Capabilities(new_caps) @@ -302,8 +310,8 @@ where if tag.starts_with(TAG_PREFIX) { let id = tag.trim_start_matches(TAG_PREFIX).parse::()?; let mut results = results.write(); - if let Some((c, waker)) = results.get_mut(&id) { - *c = Some(resp); + if let Some((_, opt, _, waker)) = results.iter_mut().next() { + *opt = Some(resp); if let Some(waker) = waker.take() { waker.wake(); } @@ -311,7 +319,7 @@ where } } - _ => todo!("unhandled response: {:?}", resp), + _ => {} } // debug!("parsed as: {:?}", resp); diff --git a/imap/src/client/mod.rs b/imap/src/client/mod.rs index 23b63f6..409a056 100644 --- a/imap/src/client/mod.rs +++ b/imap/src/client/mod.rs @@ -43,6 +43,9 @@ use tokio_rustls::{ client::TlsStream, rustls::ClientConfig as RustlsConfig, webpki::DNSNameRef, TlsConnector, }; +use crate::command::Command; +use crate::response::Response; + pub use self::inner::Client; /// Struct used to start building the config for a client. @@ -118,12 +121,20 @@ impl ClientUnauthenticated { } } - pub async fn capabilities(&mut self) -> Result<()> { + /// TODO: Exposing low-level execute , shoudl remove later + pub async fn execute(&mut self, cmd: Command) -> Result<(Response, Vec)> { match self { - ClientUnauthenticated::Encrypted(e) => e.inner.capabilities().await?, - ClientUnauthenticated::Unencrypted(e) => e.inner.capabilities().await?, + ClientUnauthenticated::Encrypted(e) => e.inner.execute(cmd).await, + ClientUnauthenticated::Unencrypted(e) => e.inner.execute(cmd).await, + } + } + + /// Checks if the server that the client is talking to has support for the given capability. + pub async fn has_capability(&mut self, cap: impl AsRef) -> Result { + match self { + ClientUnauthenticated::Encrypted(e) => e.inner.has_capability(cap).await, + ClientUnauthenticated::Unencrypted(e) => e.inner.has_capability(cap).await, } - Ok(()) } } diff --git a/imap/src/command/mod.rs b/imap/src/command/mod.rs index 5bbcb54..b922226 100644 --- a/imap/src/command/mod.rs +++ b/imap/src/command/mod.rs @@ -5,6 +5,7 @@ use std::fmt; pub enum Command { Capability, Starttls, + Login { username: String, password: String }, } impl fmt::Display for Command { @@ -12,6 +13,7 @@ impl fmt::Display for Command { match self { Command::Capability => write!(f, "CAPABILITY"), Command::Starttls => write!(f, "STARTTLS"), + Command::Login { username, password } => write!(f, "LOGIN {} {}", username, password), } } } diff --git a/imap/src/parser/mod.rs b/imap/src/parser/mod.rs index 59a13c8..3483e9b 100644 --- a/imap/src/parser/mod.rs +++ b/imap/src/parser/mod.rs @@ -16,22 +16,7 @@ struct Rfc3501; pub fn parse_capability(s: impl AsRef) -> Result> { let mut pairs = Rfc3501::parse(Rule::capability, s.as_ref())?; let pair = pairs.next().unwrap(); - let cap = match pair.as_rule() { - Rule::capability => { - let mut pairs = pair.into_inner(); - let pair = pairs.next().unwrap(); - match pair.as_rule() { - Rule::auth_type => Capability::Auth(pair.as_str().to_uppercase().to_owned()), - Rule::atom => match pair.as_str() { - "IMAP4rev1" => Capability::Imap4rev1, - s => Capability::Atom(s.to_uppercase().to_owned()), - }, - _ => unreachable!("{:?}", pair), - } - } - _ => unreachable!("{:?}", pair), - }; - Ok(cap) + Ok(build_capability(pair)) } pub fn parse_response(s: impl AsRef) -> Result> { @@ -82,6 +67,7 @@ fn build_response(pair: Pair) -> Response { } } Rule::mailbox_data => Response::MailboxData(build_mailbox_data(pair)), + Rule::capability_data => Response::Capabilities(build_capabilities(pair)), _ => unreachable!("{:#?}", pair), } } @@ -100,7 +86,6 @@ fn build_resp_cond_state(pair: Pair) -> (Status, Option, Opt let mut code = None; let mut information = None; - println!("pairs: {:#?}", pairs); let pair = pairs.next().unwrap(); let pairs = pair.into_inner(); for pair in pairs { @@ -119,9 +104,13 @@ fn build_resp_code(pair: Pair) -> Option { unreachable!("{:#?}", pair); } + // panic!("pair: {:#?}", pair); + debug!("pair: {:#?}", pair); + let mut pairs = pair.into_inner(); let pair = pairs.next()?; Some(match pair.as_rule() { + Rule::capability_data => ResponseCode::Capabilities(build_capabilities(pair)), Rule::resp_text_code_readwrite => ResponseCode::ReadWrite, Rule::resp_text_code_uidvalidity => ResponseCode::UidValidity(build_number(pair)), Rule::resp_text_code_unseen => ResponseCode::Unseen(build_number(pair)), @@ -129,6 +118,31 @@ fn build_resp_code(pair: Pair) -> Option { }) } +fn build_capability(pair: Pair) -> Capability { + if !matches!(pair.as_rule(), Rule::capability) { + unreachable!("{:#?}", pair); + } + + let mut pairs = pair.into_inner(); + let pair = pairs.next().unwrap(); + match pair.as_rule() { + Rule::auth_type => Capability::Auth(pair.as_str().to_uppercase().to_owned()), + Rule::atom => match pair.as_str() { + "IMAP4rev1" => Capability::Imap4rev1, + s => Capability::Atom(s.to_uppercase().to_owned()), + }, + _ => unreachable!("{:?}", pair), + } +} + +fn build_capabilities(pair: Pair) -> Vec { + if !matches!(pair.as_rule(), Rule::capability_data) { + unreachable!("{:#?}", pair); + } + + pair.into_inner().map(build_capability).collect() +} + fn build_status(pair: Pair) -> Status { match pair.as_rule() { Rule::resp_status => match pair.as_str().to_uppercase().as_str() { @@ -202,7 +216,7 @@ mod tests { use pest::Parser; #[test] -#[rustfmt::skip] + #[rustfmt::skip] fn test_capability() { assert_eq!(parse_capability("IMAP4rev1"), Ok(Capability::Imap4rev1)); assert_eq!(parse_capability("LOGINDISABLED"), Ok(Capability::Atom("LOGINDISABLED".to_owned()))); @@ -214,7 +228,7 @@ mod tests { } #[test] -#[rustfmt::skip] + #[rustfmt::skip] fn test_nil() { assert!(Rfc3501::parse(Rule::nil, "NIL").is_ok()); assert!(Rfc3501::parse(Rule::nil, "anything else").is_err()); @@ -292,5 +306,13 @@ mod tests { information: Some("SELECT completed".to_owned()), }) ); + + // assert_eq!( + // parse_response(concat!( + // r#"* 12 FETCH (FLAGS (\Seen) INTERNALDATE "17-Jul-1996 02:44:25 -0700" RFC822.SIZE 4286 ENVELOPE ("Wed, 17 Jul 1996 02:23:25 -0700 (PDT)" "IMAP4rev1 WG mtg summary and minutes" (("Terry Gray" NIL "gray" "cac.washington.edu")) (("Terry Gray" NIL "gray" "cac.washington.edu")) (("Terry Gray" NIL "gray" "cac.washington.edu")) ((NIL NIL "imap" "cac.washington.edu")) ((NIL NIL "minutes" "CNRI.Reston.VA.US") ("John Klensin" NIL "KLENSIN" "MIT.EDU")) NIL NIL "") BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028 92))"#, + // "\r\n", + // )), + // Ok(Response::Fetch(12, vec![])) + // ); } } diff --git a/imap/src/parser/rfc3501.pest b/imap/src/parser/rfc3501.pest index d72904f..04b9095 100644 --- a/imap/src/parser/rfc3501.pest +++ b/imap/src/parser/rfc3501.pest @@ -34,7 +34,7 @@ body_type_mpart = { body{1,} ~ sp ~ media_subtype ~ (sp ~ body_ext_mpart)? } body_type_msg = { media_message ~ sp ~ body_fields ~ sp ~ envelope ~ sp ~ body ~ sp ~ body_fld_lines } body_type_text = { media_text ~ sp ~ body_fields ~ sp ~ body_fld_lines } capability = ${ ^"AUTH=" ~ auth_type | atom } -capability_data = { ^"CAPABILITY" ~ (sp ~ capability)* ~ sp ~ "IMAP4rev1" ~ (sp ~ capability)* } +capability_data = { ^"CAPABILITY" ~ (sp ~ ("IMAP4rev1" ~ capability))* ~ sp ~ "IMAP4rev1" ~ (sp ~ capability)* } char8 = @{ '\x01'..'\xff' } continue_req = { "+" ~ sp ~ (resp_text | base64) ~ crlf } date_day_fixed = { (sp ~ digit) | digit{2} } diff --git a/src/mail/mod.rs b/src/mail/mod.rs index b589f72..77d45fd 100644 --- a/src/mail/mod.rs +++ b/src/mail/mod.rs @@ -9,7 +9,7 @@ use panorama_imap::{ use tokio::{sync::mpsc::UnboundedReceiver, task::JoinHandle}; use tokio_stream::wrappers::WatchStream; -use crate::config::{Config, ConfigWatcher, MailAccountConfig, TlsMethod}; +use crate::config::{Config, ConfigWatcher, ImapAuth, MailAccountConfig, TlsMethod}; /// Command sent to the mail thread by something else (i.e. UI) pub enum MailCommand { @@ -89,7 +89,13 @@ async fn imap_main(acct: MailAccountConfig) -> Result<()> { debug!("preparing to auth"); // check if the authentication method is supported - unauth.capabilities().await?; + let authed = match acct.imap.auth { + ImapAuth::Plain { username, password } => { + let ok = unauth.has_capability("AUTH=PLAIN").await?; + let res = unauth.execute(ImapCommand::Login { username, password }).await?; + debug!("res: {:?}", res); + } + }; // debug!("sending CAPABILITY"); // let result = unauth.capabilities().await?;