melib/imap: add suggestion on STARTTLS error

If server port is 993 (IMAPS) and starttls is enabled, suggest turning
it off if starttls fails.
jmap-eventsource
Manos Pitsidianakis 2020-10-20 14:58:16 +03:00
parent 7a9c150f33
commit 91fe7435f7
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 19 additions and 2 deletions

View File

@ -146,26 +146,37 @@ impl ImapStream {
)) ))
.chain_err_kind(crate::error::ErrorKind::Network)?; .chain_err_kind(crate::error::ErrorKind::Network)?;
if server_conf.use_starttls { if server_conf.use_starttls {
let err_fn = || {
if server_conf.server_port == 993 {
"STARTTLS failed. Server port is set to 993, which normally uses TLS. Maybe try disabling use_starttls."
} else {
"STARTTLS failed. Is the connection already encrypted?"
}
};
let mut buf = vec![0; Connection::IO_BUF_SIZE]; let mut buf = vec![0; Connection::IO_BUF_SIZE];
match server_conf.protocol { match server_conf.protocol {
ImapProtocol::IMAP { .. } => socket ImapProtocol::IMAP { .. } => socket
.write_all(format!("M{} STARTTLS\r\n", cmd_id).as_bytes()) .write_all(format!("M{} STARTTLS\r\n", cmd_id).as_bytes())
.await .await
.chain_err_summary(err_fn)
.chain_err_kind(crate::error::ErrorKind::Network)?, .chain_err_kind(crate::error::ErrorKind::Network)?,
ImapProtocol::ManageSieve => { ImapProtocol::ManageSieve => {
socket socket
.read(&mut buf) .read(&mut buf)
.await .await
.chain_err_summary(err_fn)
.chain_err_kind(crate::error::ErrorKind::Network)?; .chain_err_kind(crate::error::ErrorKind::Network)?;
socket socket
.write_all(b"STARTTLS\r\n") .write_all(b"STARTTLS\r\n")
.await .await
.chain_err_summary(err_fn)
.chain_err_kind(crate::error::ErrorKind::Network)?; .chain_err_kind(crate::error::ErrorKind::Network)?;
} }
} }
socket socket
.flush() .flush()
.await .await
.chain_err_summary(err_fn)
.chain_err_kind(crate::error::ErrorKind::Network)?; .chain_err_kind(crate::error::ErrorKind::Network)?;
let mut response = Vec::with_capacity(1024); let mut response = Vec::with_capacity(1024);
let mut broken = false; let mut broken = false;
@ -175,6 +186,7 @@ impl ImapStream {
let len = socket let len = socket
.read(&mut buf) .read(&mut buf)
.await .await
.chain_err_summary(err_fn)
.chain_err_kind(crate::error::ErrorKind::Network)?; .chain_err_kind(crate::error::ErrorKind::Network)?;
response.extend_from_slice(&buf[0..len]); response.extend_from_slice(&buf[0..len]);
match server_conf.protocol { match server_conf.protocol {
@ -200,7 +212,7 @@ impl ImapStream {
} }
if !broken { if !broken {
return Err(MeliError::new(format!( return Err(MeliError::new(format!(
"Could not initiate TLS negotiation to {}.", "Could not initiate STARTTLS negotiation to {}.",
path path
))); )));
} }
@ -232,8 +244,13 @@ impl ImapStream {
} }
} }
AsyncWrapper::new(Connection::Tls( AsyncWrapper::new(Connection::Tls(
conn_result.chain_err_kind(crate::error::ErrorKind::Network)?, conn_result
.chain_err_summary(|| {
format!("Could not initiate TLS negotiation to {}.", path)
})
.chain_err_kind(crate::error::ErrorKind::Network)?,
)) ))
.chain_err_summary(|| format!("Could not initiate TLS negotiation to {}.", path))
.chain_err_kind(crate::error::ErrorKind::Network)? .chain_err_kind(crate::error::ErrorKind::Network)?
} }
} else { } else {