From 2d0e12770dda67c1d2d1351c5d62f22ac7315fc2 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Thu, 8 May 2014 14:54:21 +0200 Subject: [PATCH] Get the value of ESMTP parameters --- src/smtp/client.rs | 35 ++++++++++++++++++++++++++++++----- src/smtp/commands.rs | 23 ++++++++++++++++++++++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/smtp/client.rs b/src/smtp/client.rs index 0e7a0c8..a009ab2 100644 --- a/src/smtp/client.rs +++ b/src/smtp/client.rs @@ -121,12 +121,17 @@ impl SmtpServerInfo { } /// Checks if the server supports an ESMTP feature - fn supports_feature(&self, keyword: EsmtpParameter) -> bool { + fn supports_feature(&self, keyword: EsmtpParameter) -> Result { match self.esmtp_features.clone() { Some(esmtp_features) => { - esmtp_features.contains(&keyword) + for feature in esmtp_features.iter() { + if keyword.same_keyword_as(*feature) { + return Ok(*feature); + } + } + Err({}) }, - None => false + None => Err({}) } } } @@ -266,8 +271,8 @@ impl SmtpClient { // Checks message encoding according to the server's capability // TODO : Add an encoding check. - if ! self.server_info.clone().unwrap().supports_feature(commands::EightBitMime) { - if message.clone().into_owned().is_ascii() { + if ! self.server_info.clone().unwrap().supports_feature(commands::EightBitMime).is_ok() { + if ! message.clone().into_owned().is_ascii() { self.smtp_fail("Server does not accepts UTF-8 strings"); } } @@ -605,4 +610,24 @@ mod test { assert_eq!(SmtpServerInfo::parse_esmtp_response("me\r\n250-SIZE 42\r\n250 SIZE 43"), Some(vec!(commands::Size(42), commands::Size(43)))); } + + #[test] + fn test_smtp_server_info_supports_feature() { + assert_eq!(SmtpServerInfo{ + name: "name", + esmtp_features: Some(vec!(commands::EightBitMime)) + }.supports_feature(commands::EightBitMime), Ok(commands::EightBitMime)); + assert_eq!(SmtpServerInfo{ + name: "name", + esmtp_features: Some(vec!(commands::Size(42), commands::EightBitMime)) + }.supports_feature(commands::EightBitMime), Ok(commands::EightBitMime)); + assert_eq!(SmtpServerInfo{ + name: "name", + esmtp_features: Some(vec!(commands::Size(42), commands::EightBitMime)) + }.supports_feature(commands::Size(0)), Ok(commands::Size(42))); + assert!(SmtpServerInfo{ + name: "name", + esmtp_features: Some(vec!(commands::EightBitMime)) + }.supports_feature(commands::Size(42)).is_err()); + } } diff --git a/src/smtp/commands.rs b/src/smtp/commands.rs index 899b873..580d8f8 100644 --- a/src/smtp/commands.rs +++ b/src/smtp/commands.rs @@ -97,7 +97,7 @@ impl Show for EsmtpParameter { fn fmt(&self, f: &mut Formatter) -> Result { f.buf.write( match self { - &EightBitMime => "8BITMIME".to_owned(), + &EightBitMime => "8BITMIME".to_owned(), &Size(ref size) => format!("SIZE={}", size) }.as_bytes() ) @@ -121,6 +121,19 @@ impl FromStr for EsmtpParameter { } } +impl EsmtpParameter { + /// Checks if the ESMTP keyword is the same + pub fn same_keyword_as(&self, other: EsmtpParameter) -> bool { + if *self == other { + return true; + } + match (*self, other) { + (Size(_), Size(_)) => true, + _ => false + } + } +} + #[cfg(test)] mod test { use super::{SmtpCommand, EsmtpParameter}; @@ -135,6 +148,14 @@ mod test { ); } + #[test] + fn test_esmtp_parameter_same_keyword_as() { + assert_eq!(super::EightBitMime.same_keyword_as(super::EightBitMime), true); + assert_eq!(super::Size(42).same_keyword_as(super::Size(42)), true); + assert_eq!(super::Size(42).same_keyword_as(super::Size(43)), true); + assert_eq!(super::Size(42).same_keyword_as(super::EightBitMime), false); + } + #[test] fn test_esmtp_parameter_fmt() { assert_eq!(format!("{}", super::EightBitMime), "8BITMIME".to_owned());