From 5b7a3ad76f35f431ff77ee0e9d6f7c21db5f7ffc Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Mon, 2 Mar 2015 02:14:23 +0100 Subject: [PATCH] Log only in send method --- src/client/mod.rs | 66 +++++++++++++---------------------------------- 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index b015b14..b63c3e6 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -15,7 +15,6 @@ use std::error::FromError; use std::old_io::net::tcp::TcpStream; use std::old_io::net::ip::{SocketAddr, ToSocketAddr}; -use log::LogLevel::Info; use uuid::Uuid; use serialize::base64::{self, ToBase64, FromBase64}; use serialize::hex::ToHex; @@ -59,8 +58,6 @@ pub struct State { pub panic: bool, /// Connection reuse counter pub connection_reuse_count: u16, - /// Current message id - pub current_message: Option, } /// Represents the credentials @@ -143,7 +140,6 @@ impl Client { state: State { panic: false, connection_reuse_count: 0, - current_message: None, }, credentials: None, } @@ -196,7 +192,6 @@ impl Client { self.server_info = None; self.state.panic = false; self.state.connection_reuse_count = 0; - self.state.current_message = None; } /// Sends an email @@ -213,6 +208,10 @@ impl Client { if self.stream.is_none() { try!(self.connect()); + // Log the connection + info!("connection established to {}", + self.stream.as_mut().unwrap().peer_name().unwrap()); + // Extended Hello or Hello if needed if let Err(error) = self.ehlo() { match error.kind { @@ -246,8 +245,8 @@ impl Client { } } - self.state.current_message = Some(Uuid::new_v4()); - email.set_message_id(format!("<{}@{}>", self.state.current_message.as_ref().unwrap(), + let current_message = Uuid::new_v4(); + email.set_message_id(format!("<{}@{}>", current_message, self.configuration.hello_name.clone())); let from_address = email.from_address(); @@ -257,11 +256,16 @@ impl Client { // Mail try_smtp!(self.mail(from_address.as_slice()), self); + // Log the mail command + info!("{}: from=<{}>", current_message, from_address); + // Recipient // TODO Return rejected addresses // TODO Limit the number of recipients for to_address in to_addresses.iter() { try_smtp!(self.rcpt(to_address.as_slice()), self); + // Log the rcpt command + info!("{}: to=<{}>", current_message, to_address); } // Data @@ -273,6 +277,10 @@ impl Client { if result.is_ok() { // Increment the connection reuse counter self.state.connection_reuse_count = self.state.connection_reuse_count + 1; + + // Log the message + info!("{}: conn_use={}, size={}, status=sent ({})", current_message, + self.state.connection_reuse_count, message.len(), result.as_ref().ok().unwrap()); } // Test if we can reuse the existing connection @@ -294,10 +302,6 @@ impl Client { // Try to connect self.stream = Some(try!(Connecter::connect(self.server_addr))); - // Log the connection - info!("connection established to {}", - self.stream.as_mut().unwrap().peer_name().unwrap()); - let result = self.stream.as_mut().unwrap().get_reply(); with_code!(result, [220].iter()) } @@ -355,36 +359,12 @@ impl Client { None => "", }; - let result = self.command( - format!("MAIL FROM:<{}> {}", address, options).as_slice(), [250].iter() - ); - - if result.is_ok() { - // Log the mail command - if log_enabled!(Info) { - // Generate an ID for the logs if None was provided - if self.state.current_message.is_none() { - self.state.current_message = Some(Uuid::new_v4()); - } - info!("{}: from=<{}>", self.state.current_message.as_ref().unwrap(), address); - } - } - - result + self.command(format!("MAIL FROM:<{}> {}", address, options).as_slice(), [250].iter()) } /// Sends a RCPT command pub fn rcpt(&mut self, address: &str) -> SmtpResult { - let result = self.command( - format!("RCPT TO:<{}>", address).as_slice(), [250, 251].iter() - ); - - if result.is_ok() { - // Log the rcpt command - info!("{}: to=<{}>", self.state.current_message.as_ref().unwrap(), address); - } - - result + self.command(format!("RCPT TO:<{}>", address).as_slice(), [250, 251].iter()) } /// Sends a DATA command @@ -439,16 +419,6 @@ impl Client { /// Sends the message content and close pub fn message(&mut self, message_content: &str) -> SmtpResult { - let result = self.send_server(message_content, MESSAGE_ENDING, [250].iter()); - - if result.is_ok() { - // Log the message - info!("{}: conn_use={}, size={}, status=sent ({})", self.state.current_message.as_ref().unwrap(), - self.state.connection_reuse_count, message_content.len(), result.as_ref().ok().unwrap()); - } - - self.state.current_message = None; - - result + self.send_server(message_content, MESSAGE_ENDING, [250].iter()) } }