From 543f56d904d554597434905f539ad6919f3e9cd5 Mon Sep 17 00:00:00 2001 From: mawalu Date: Sun, 2 Jul 2023 20:15:10 +0200 Subject: [PATCH] Fix var swapping in finalize_message --- src/smtp.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/smtp.rs b/src/smtp.rs index 001bfa3..5834a3a 100644 --- a/src/smtp.rs +++ b/src/smtp.rs @@ -1,4 +1,5 @@ use std::io::{BufRead, BufReader, Write}; +use std::mem; use std::net::{TcpListener, TcpStream}; pub fn start_server() { @@ -12,7 +13,13 @@ pub fn start_server() { let reader = BufReader::new(tcp_stream.try_clone().unwrap()); let mut connection = Connection::new(); - let _ = connection.handle(tcp_stream, reader); + if let Err(e) = connection.handle(tcp_stream, reader) { + eprintln!("System error: {}", e) + } else { + for mail in connection.messages { + println!("Mail from {} for {}\n{}", mail.sender, mail.recipients.join(", "), mail.data.unwrap_or("".to_string())) + } + } } Err(e) => { eprintln!("Failed to accept connection: {}", e) @@ -102,14 +109,14 @@ impl Connection { send(&mut writer, out)?; if out == MSG_BYE { - break + break; } } line.clear() } - return Ok(()) + return Ok(()); } fn handle_line(&mut self, line: &String) -> Result, &'static str> { @@ -147,7 +154,7 @@ impl Connection { ConnectionState::Data => { if line.trim_end() == CMD_DOT { self.state = ConnectionState::Done; - return Ok(Some(MSG_OK)) + return Ok(Some(MSG_OK)); } return match self.current_message { @@ -163,18 +170,17 @@ impl Connection { _ => { Err(MSG_FAIL) } - } - + }; } ConnectionState::Done => { if line.starts_with(CMD_QUIT) { self.finalize_mail(); - return Ok(Some(MSG_BYE)) + return Ok(Some(MSG_BYE)); } if line.starts_with(CMD_MAIL_FROM) { self.finalize_mail(); - return Ok(Some(MSG_OK)) + return Ok(Some(MSG_OK)); } } } @@ -183,9 +189,10 @@ impl Connection { } fn finalize_mail(&mut self) { - if let Some(mail) = &self.current_message { - self.current_message = None; - self.messages.push(*mail) + let current_message = mem::replace(&mut self.current_message, None); + + if let Some(mail) = current_message { + self.messages.push(mail) } }