use core::fmt;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use log::trace;
use thiserror::Error;
use crate::{coroutine::*, rfc5321::types::reply_code::ReplyCode, send::*, smtp_try};
pub struct SmtpStartTlsCommand;
impl From<SmtpStartTlsCommand> for Vec<u8> {
fn from(_: SmtpStartTlsCommand) -> Vec<u8> {
b"STARTTLS\r\n".to_vec()
}
}
#[derive(Clone, Debug, Error)]
pub enum SmtpStartTlsError {
#[error("SMTP STARTTLS failed: rejected {code} {message}")]
Rejected { code: u16, message: String },
#[error("SMTP STARTTLS failed: {0}")]
Send(#[from] SendSmtpCommandError),
}
pub struct SmtpStartTls {
state: State,
}
impl SmtpStartTls {
pub fn new() -> Self {
Self {
state: State::Send(SendSmtpCommand::new(SmtpStartTlsCommand)),
}
}
}
impl Default for SmtpStartTls {
fn default() -> Self {
Self::new()
}
}
impl SmtpCoroutine for SmtpStartTls {
type Yield = SmtpYield;
type Return = Result<Vec<u8>, SmtpStartTlsError>;
fn resume(&mut self, arg: Option<&[u8]>) -> SmtpCoroutineState<Self::Yield, Self::Return> {
loop {
trace!("starttls: {}", self.state);
match &mut self.state {
State::Send(send) => {
let out = smtp_try!(send, arg);
if out.response.code == ReplyCode::SERVICE_READY {
return SmtpCoroutineState::Complete(Ok(Vec::new()));
}
let code = out.response.code.code();
let message = out.response.text().to_string();
return SmtpCoroutineState::Complete(Err(SmtpStartTlsError::Rejected {
code,
message,
}));
}
}
}
}
}
enum State {
Send(SendSmtpCommand<SmtpStartTlsCommand>),
}
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Send(_) => f.write_str("send starttls"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn success_returns_empty_remaining() {
let mut starttls = SmtpStartTls::new();
let bytes = expect_wants_write(&mut starttls, None);
assert_eq!(bytes, b"STARTTLS\r\n");
expect_wants_read(&mut starttls);
let remaining = expect_complete_ok(&mut starttls, b"220 ready\r\n");
assert!(remaining.is_empty());
}
#[test]
fn rejected_returns_rejected_error() {
let mut starttls = SmtpStartTls::new();
let _ = expect_wants_write(&mut starttls, None);
expect_wants_read(&mut starttls);
let err = expect_complete_err(&mut starttls, b"454 TLS not available\r\n");
let SmtpStartTlsError::Rejected { code, message } = err else {
panic!("expected SmtpStartTlsError::Rejected, got {err:?}");
};
assert_eq!(code, 454);
assert_eq!(message, "TLS not available");
}
#[test]
fn eof_returns_eof_error() {
let mut starttls = SmtpStartTls::new();
let _ = expect_wants_write(&mut starttls, None);
expect_wants_read(&mut starttls);
let err = expect_complete_err(&mut starttls, b"");
assert!(matches!(
err,
SmtpStartTlsError::Send(SendSmtpCommandError::Eof)
));
}
fn expect_wants_write(cor: &mut SmtpStartTls, arg: Option<&[u8]>) -> Vec<u8> {
match cor.resume(arg) {
SmtpCoroutineState::Yielded(SmtpYield::WantsWrite(bytes)) => bytes,
state => panic!("expected WantsWrite, got {state:?}"),
}
}
fn expect_wants_read(cor: &mut SmtpStartTls) {
match cor.resume(None) {
SmtpCoroutineState::Yielded(SmtpYield::WantsRead) => {}
state => panic!("expected WantsRead, got {state:?}"),
}
}
fn expect_complete_ok(cor: &mut SmtpStartTls, reply: &[u8]) -> Vec<u8> {
match cor.resume(Some(reply)) {
SmtpCoroutineState::Complete(Ok(remaining)) => remaining,
state => panic!("expected Complete(Ok), got {state:?}"),
}
}
fn expect_complete_err(cor: &mut SmtpStartTls, reply: &[u8]) -> SmtpStartTlsError {
match cor.resume(Some(reply)) {
SmtpCoroutineState::Complete(Err(err)) => err,
state => panic!("expected Complete(Err), got {state:?}"),
}
}
}