diff --git a/src/smtp/client.rs b/src/smtp/client.rs index ba1f7a6..b32c78f 100644 --- a/src/smtp/client.rs +++ b/src/smtp/client.rs @@ -233,8 +233,7 @@ impl SmtpClient { None => fail!("No banner on {}", self.host) } } - - + /// Sends an email pub fn send_mail(&mut self, from_address: StrBuf, to_addresses: Vec, message: StrBuf) { let my_hostname = self.my_hostname.clone(); @@ -242,12 +241,12 @@ impl SmtpClient { // Connect match self.connect() { Ok(..) => {}, - Err(response) => fail!("Cannot connect to {:s}:{:u}. Server says: {}", + Err(response) => fail!("Cannot connect to {:s}:{:u}. Server says: {}", self.host, self.port, response ) } - + // Extended Hello or Hello match self.ehlo(my_hostname.clone()) { Err(SmtpResponse{code: 550, message: _}) => { @@ -347,7 +346,7 @@ impl SmtpClient { pub fn close(&mut self) { drop(self.stream.clone().unwrap()); } - + /// Send a HELO command pub fn helo(&mut self, my_hostname: StrBuf) -> Result, SmtpResponse> { check_state_in!(vec!(Connected)); @@ -366,8 +365,7 @@ impl SmtpClient { Err(response) => Err(response) } } - - + /// Sends a EHLO command pub fn ehlo(&mut self, my_hostname: StrBuf) -> Result, SmtpResponse> { check_state_not_in!(vec!(Unconnected)); @@ -386,7 +384,7 @@ impl SmtpClient { Err(response) => Err(response) } } - + /// Sends a MAIL command pub fn mail(&mut self, from_address: StrBuf, options: Option>) -> Result, SmtpResponse> { check_state_in!(vec!(HeloSent)); @@ -401,7 +399,7 @@ impl SmtpClient { } } } - + /// Sends a RCPT command pub fn rcpt(&mut self, to_address: StrBuf, options: Option>) -> Result, SmtpResponse> { check_state_in!(vec!(MailSent, RcptSent)); @@ -416,7 +414,7 @@ impl SmtpClient { } } } - + /// Sends a DATA command pub fn data(&mut self) -> Result, SmtpResponse> { check_state_in!(vec!(RcptSent)); @@ -431,7 +429,7 @@ impl SmtpClient { } } } - + /// Sends the message content pub fn message(&mut self, message_content: StrBuf) -> Result, SmtpResponse> { check_state_in!(vec!(DataSent)); @@ -446,8 +444,7 @@ impl SmtpClient { } } } - - + /// Sends a QUIT command pub fn quit(&mut self) -> Result, SmtpResponse> { check_state_not_in!(vec!(Unconnected)); @@ -461,7 +458,7 @@ impl SmtpClient { } } } - + /// Sends a RSET command pub fn rset(&mut self) -> Result, SmtpResponse> { check_state_not_in!(vec!(Unconnected)); @@ -477,13 +474,13 @@ impl SmtpClient { } } } - + /// Sends a NOOP commands pub fn noop(&mut self) -> Result, SmtpResponse> { check_state_not_in!(vec!(Unconnected)); self.send_command(commands::Noop).with_code(vec!(250)) } - + /// Sends a VRFY command pub fn vrfy(&mut self, to_address: StrBuf) -> Result, SmtpResponse> { check_state_not_in!(vec!(Unconnected)); @@ -535,19 +532,19 @@ mod test { #[test] fn test_smtp_response_from_str() { - assert!(from_str::>("200 response message") == + assert!(from_str::>("200 response message") == Some(SmtpResponse{ code: 200, message: StrBuf::from_str("response message") }) ); - assert!(from_str::>("200-response message") == + assert!(from_str::>("200-response message") == Some(SmtpResponse{ code: 200, message: StrBuf::from_str("response message") }) ); - assert!(from_str::>("200-response\r\nmessage") == + assert!(from_str::>("200-response\r\nmessage") == Some(SmtpResponse{ code: 200, message: StrBuf::from_str("response\r\nmessage") @@ -556,17 +553,17 @@ mod test { assert!(from_str::>("2000response message") == None); assert!(from_str::>("20a response message") == None); } - + #[test] fn test_smtp_response_with_code() { - assert!(SmtpResponse{code: 200, message: "message"}.with_code(vec!(200)) == + assert!(SmtpResponse{code: 200, message: "message"}.with_code(vec!(200)) == Ok(SmtpResponse{code: 200, message: "message"})); - assert!(SmtpResponse{code: 400, message: "message"}.with_code(vec!(200)) == + assert!(SmtpResponse{code: 400, message: "message"}.with_code(vec!(200)) == Err(SmtpResponse{code: 400, message: "message"})); - assert!(SmtpResponse{code: 200, message: "message"}.with_code(vec!(200, 300)) == + assert!(SmtpResponse{code: 200, message: "message"}.with_code(vec!(200, 300)) == Ok(SmtpResponse{code: 200, message: "message"})); } - + #[test] fn test_smtp_server_info_fmt() { assert!(format!("{}", SmtpServerInfo{ @@ -582,9 +579,14 @@ mod test { esmtp_features: None }) == ~"name with no supported features"); } - + #[test] fn test_smtp_server_info_parse_esmtp_response() { - + assert!(SmtpServerInfo::parse_esmtp_response("me\r\n250-8BITMIME\r\n250 SIZE 42") == + Some(vec!(commands::EightBitMime, commands::Size(42)))); + assert!(SmtpServerInfo::parse_esmtp_response("me\r\n250-8BITMIME\r\n250 UNKNON 42") == + Some(vec!(commands::EightBitMime))); + assert!(SmtpServerInfo::parse_esmtp_response("me\r\n250-9BITMIME\r\n250 SIZE a") == + None); } } diff --git a/src/smtp/commands.rs b/src/smtp/commands.rs index 9130316..618bab9 100644 --- a/src/smtp/commands.rs +++ b/src/smtp/commands.rs @@ -21,7 +21,7 @@ pub static SMTP_PORT: Port = 25; /// Supported SMTP commands /// /// We do not implement the following SMTP commands, as they were deprecated in RFC 5321 -/// and must not be used by clients : +/// and must not be used by clients: /// SEND, SOML, SAML, TURN #[deriving(Eq,Clone)] pub enum SmtpCommand { @@ -112,8 +112,8 @@ impl FromStr for EsmtpParameter { "8BITMIME" => Some(EightBitMime), _ => None }, - 2 => match (splitted[0], splitted[1]) { - ("SIZE", size) => Some(Size(from_str::(size).unwrap())), + 2 => match (splitted[0], from_str::(splitted[1])) { + ("SIZE", Some(size)) => Some(Size(size)), _ => None }, _ => None @@ -145,5 +145,9 @@ mod test { fn test_esmtp_parameter_from_str() { assert!(from_str::("8BITMIME") == Some(super::EightBitMime)); assert!(from_str::("SIZE 42") == Some(super::Size(42))); + assert!(from_str::("SIZ 42") == None); + assert!(from_str::("SIZE 4a2") == None); + // TODO: accept trailing spaces + assert!(from_str::("SIZE 42 ") == None); } } diff --git a/src/smtp/common.rs b/src/smtp/common.rs index 7ea600d..4b16845 100644 --- a/src/smtp/common.rs +++ b/src/smtp/common.rs @@ -9,8 +9,8 @@ //! Common definitions for SMTP //! -//! Needs to be organized later. - +//! Needs to be organized later + use std::strbuf::StrBuf; pub static SP: &'static str = " "; diff --git a/src/smtp/lib.rs b/src/smtp/lib.rs index 682d818..dc108f1 100644 --- a/src/smtp/lib.rs +++ b/src/smtp/lib.rs @@ -31,7 +31,7 @@ //! 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")), +//! vec!(StrBuf::from_str("user@example.org")), //! StrBuf::from_str("Test email") //! ); //! ```