Get the value of ESMTP parameters

This commit is contained in:
Alexis Mousset
2014-05-08 14:54:21 +02:00
parent c376bc24c1
commit 2d0e12770d
2 changed files with 52 additions and 6 deletions

View File

@@ -121,12 +121,17 @@ impl<T: Str> SmtpServerInfo<T> {
}
/// Checks if the server supports an ESMTP feature
fn supports_feature(&self, keyword: EsmtpParameter) -> bool {
fn supports_feature(&self, keyword: EsmtpParameter) -> Result<EsmtpParameter, ()> {
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<StrBuf, TcpStream> {
// 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());
}
}

View File

@@ -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());