From 83ffa36c1ea6ef9183ba8ad55345085c48d08aa5 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Mon, 5 May 2014 19:21:57 +0200 Subject: [PATCH] Logging --- src/smtp/client.rs | 32 +++++++++++++++++++++++--------- src/smtp/lib.rs | 11 ++++++++++- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/smtp/client.rs b/src/smtp/client.rs index 7faf11d..7297d55 100644 --- a/src/smtp/client.rs +++ b/src/smtp/client.rs @@ -19,7 +19,7 @@ use std::io::{IoResult, Reader, Writer}; use std::io::net::ip::{SocketAddr, Port}; use std::io::net::tcp::TcpStream; use std::io::net::addrinfo::get_host_addresses; -use common::{CRLF, get_first_word}; +use common::{CRLF, get_first_word, unquote_email_address}; use commands; use commands::{SMTP_PORT, SmtpCommand, EsmtpParameter}; @@ -170,7 +170,7 @@ macro_rules! smtp_fail_if_err( Err(response) => { self.smtp_fail(response) }, - Ok(..) => {} + Ok(_) => {} } ); ) @@ -220,6 +220,10 @@ impl SmtpClient { Ok(stream) => Some(stream), Err(..) => fail!("Cannot connect to {:s}:{:u}", self.host, self.port) }; + + // Log the connection + info!("Connection established to {}[{}]:{}", self.my_hostname.clone(), ip, self.port); + match self.get_reply() { Some(response) => match response.with_code(vec!(220)) { Ok(response) => { @@ -240,7 +244,7 @@ impl SmtpClient { // Connect match self.connect() { - Ok(..) => {}, + Ok(_) => {}, Err(response) => fail!("Cannot connect to {:s}:{:u}. Server says: {}", self.host, self.port, response @@ -250,7 +254,7 @@ impl SmtpClient { // Extended Hello or Hello match self.ehlo(my_hostname.clone()) { Err(SmtpResponse{code: 550, message: _}) => { - smtp_fail_if_err!(self.helo(my_hostname)) + smtp_fail_if_err!(self.helo(my_hostname.clone())) }, Err(response) => { self.smtp_fail(response) @@ -269,7 +273,11 @@ impl SmtpClient { } // Mail - smtp_fail_if_err!(self.mail(from_address, None)); + smtp_fail_if_err!(self.mail(from_address.clone(), None)); + + + // Log the mail command + info!("from=<{}>, size={}, nrcpt={}", from_address, 42, to_addresses.len()); // Recipient // TODO Return rejected addresses @@ -281,8 +289,14 @@ impl SmtpClient { // Data smtp_fail_if_err!(self.data()); - // Message content - smtp_fail_if_err!(self.message(message)); + // Message content + let sent = self.message(message); + + if sent.clone().is_err() { + self.smtp_fail(sent.clone().err().unwrap()) + } + + info!("to=<{}>, status=sent ({})", to_addresses.clone().connect(">, to=<"), sent.clone().ok().unwrap()); // Quit smtp_fail_if_err!(self.quit()); @@ -389,7 +403,7 @@ impl SmtpClient { pub fn mail(&mut self, from_address: StrBuf, options: Option>) -> Result, SmtpResponse> { check_state_in!(vec!(HeloSent)); - match self.send_command(commands::Mail(from_address, options)).with_code(vec!(250)) { + match self.send_command(commands::Mail(unquote_email_address(from_address), options)).with_code(vec!(250)) { Ok(response) => { self.state = MailSent; Ok(response) @@ -404,7 +418,7 @@ impl SmtpClient { pub fn rcpt(&mut self, to_address: StrBuf, options: Option>) -> Result, SmtpResponse> { check_state_in!(vec!(MailSent, RcptSent)); - match self.send_command(commands::Recipient(to_address, options)).with_code(vec!(250)) { + match self.send_command(commands::Recipient(unquote_email_address(to_address), options)).with_code(vec!(250)) { Ok(response) => { self.state = RcptSent; Ok(response) diff --git a/src/smtp/lib.rs b/src/smtp/lib.rs index dc108f1..b8ca895 100644 --- a/src/smtp/lib.rs +++ b/src/smtp/lib.rs @@ -28,7 +28,13 @@ //! ## Usage //! //! ```rust -//! let mut email_client: SmtpClient = SmtpClient::new(StrBuf::from_str("localhost"), None, None); +//! extern crate smtp; +//! use std::io::net::tcp::TcpStream; +//! use smtp::client::SmtpClient; +//! use std::strbuf::StrBuf; +//! +//! let mut email_client: SmtpClient = +//! SmtpClient::new(StrBuf::from_str("localhost"), None, None); //! email_client.send_mail( //! StrBuf::from_str("user@example.com"), //! vec!(StrBuf::from_str("user@example.org")), @@ -36,6 +42,9 @@ //! ); //! ``` +// May 4 21:48:18 mx1 smtp-gmail/smtp[12657]: Untrusted TLS connection established to gmail-smtp-in.l.google.com[173.194.66.26]:25: TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits) +// May 4 21:48:18 mx1 smtp-gmail/smtp[12657]: 69800746A7B: to=, orig_to=, relay=gmail-smtp-in.l.google.com[173.194.66.26]:25, delay=2.6, delays=1.7/0.03/0.19/0.61, dsn=2.0.0, status=sent (250 2.0.0 OK 1399232898 fy10si2284441wib.22 - gsmtp) + #![crate_id = "smtp#0.1-pre"] #![desc = "Rust SMTP client"]