From 5c8e09f073a8f97a49b0df1093e66ccb70268f80 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Wed, 10 Dec 2014 13:54:02 +0100 Subject: [PATCH] Fix client closing --- src/client/mod.rs | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index ef9cc7a..c3497c0 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -15,7 +15,7 @@ use std::io::net::tcp::TcpStream; use std::io::net::ip::{SocketAddr, ToSocketAddr}; use tools::get_first_word; -use common::{CRLF, MESSAGE_ENDING}; +use common::{CRLF, MESSAGE_ENDING, SMTP_PORT}; use response::Response; use extension::Extension; use command::Command; @@ -57,7 +57,7 @@ macro_rules! try_smtp ( macro_rules! fail_with_err ( ($err: expr $client: ident) => ({ - $client.close_on_error(); + $client.close(); return Err(FromError::from_error($err)) }) ) @@ -90,20 +90,30 @@ impl Client { /// /// It does not connects to the server, but only create the `Client` pub fn localhost() -> Client { - Client::new("localhost", Some("localhost")) + Client::new(("localhost", SMTP_PORT), Some("localhost")) } } impl Client { - /// Closes the SMTP transaction if possible, and then closes the TCP session - fn close_on_error(&mut self) { - if self.is_connected() { - let _ = self.quit(); + /// Closes the TCP stream + pub fn close(&mut self) { + if self.stream.is_some() { + if self.is_connected() { + let _ = self.quit(); + } + // Close the TCP connection + drop(self.stream.as_mut().unwrap()); } - self.close(); + + // Reset client state + self.stream = None; + self.state = TransactionState::new(); + self.server_info = None; } /// Sends an email + /// + /// This methods logs all steps of the message sending. pub fn send_email(&mut self, email: T) -> SmtpResult { let from_address = email.from_address(); @@ -111,7 +121,7 @@ impl Client { let message = email.message(); // Connect to the server - if !self.is_connected() { + if self.noop().is_err() { try!(self.connect()); } @@ -153,15 +163,13 @@ impl Client { info!("to=<{}>, status=sent ({})", to_addresses.connect(">, to=<"), sent); - // Quit - //try_smtp!(self.quit() self); - return Ok(sent); } /// Connects to the configured server pub fn connect(&mut self) -> SmtpResult { let command = Command::Connect; + check_command_sequence!(command self); // Connect should not be called when the client is already connected @@ -189,6 +197,7 @@ impl Client { if !command.is_ascii() { fail_with_err!("Non-ASCII string" self); } + self.send_server(command, None) } @@ -224,16 +233,6 @@ impl Client { self.noop().is_ok() } - /// Closes the TCP stream - pub fn close(&mut self) { - // Close the TCP connection - drop(self.stream.as_mut().unwrap()); - // Reset client state - self.stream = None; - self.state = TransactionState::new(); - self.server_info = None; - } - /// Send a HELO command and fills `server_info` pub fn helo(&mut self) -> SmtpResult { let hostname = self.my_hostname.clone();