From d1bef702d6bdf6d8406ecc426ea14566b432a633 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sat, 14 May 2016 12:17:09 +0200 Subject: [PATCH] style(all): run rustfmt 0.5 --- src/email/mod.rs | 62 +++++++++++------------ src/transport/sendmail/mod.rs | 75 ++++++++++++++++++++++++++++ src/transport/smtp/authentication.rs | 15 +++--- src/transport/smtp/client/mod.rs | 12 ++--- src/transport/smtp/client/net.rs | 2 +- src/transport/smtp/extension.rs | 22 ++++---- src/transport/smtp/mod.rs | 15 +++--- src/transport/smtp/response.rs | 8 +-- 8 files changed, 140 insertions(+), 71 deletions(-) create mode 100644 src/transport/sendmail/mod.rs diff --git a/src/email/mod.rs b/src/email/mod.rs index e404c03..8876ea2 100644 --- a/src/email/mod.rs +++ b/src/email/mod.rs @@ -349,16 +349,14 @@ impl EmailBuilder { alternate.set_message_type(MimeMultipartType::Alternative); let text = PartBuilder::new() - .body(body_text) - .header(("Content-Type", - format!("{}", mime!(Text/Plain; Charset=Utf8)).as_ref())) - .build(); + .body(body_text) + .header(("Content-Type", format!("{}", mime!(Text/Plain; Charset=Utf8)).as_ref())) + .build(); let html = PartBuilder::new() - .body(body_html) - .header(("Content-Type", - format!("{}", mime!(Text/Html; Charset=Utf8)).as_ref())) - .build(); + .body(body_html) + .header(("Content-Type", format!("{}", mime!(Text/Html; Charset=Utf8)).as_ref())) + .build(); alternate.add_child(text); alternate.add_child(html); @@ -495,12 +493,12 @@ mod test { email.message.headers.insert(Header::new_with_value("Message-ID".to_string(), format!("<{}@rust-smtp>", current_message)) - .unwrap()); + .unwrap()); email.message - .headers - .insert(Header::new_with_value("To".to_string(), "to@example.com".to_string()) - .unwrap()); + .headers + .insert(Header::new_with_value("To".to_string(), "to@example.com".to_string()) + .unwrap()); email.message.body = "body".to_string(); @@ -516,16 +514,16 @@ mod test { let date_now = now(); let email = email_builder.to("user@localhost") - .from("user@localhost") - .cc(("cc@localhost", "Alias")) - .reply_to("reply@localhost") - .sender("sender@localhost") - .body("Hello World!") - .date(&date_now) - .subject("Hello") - .header(("X-test", "value")) - .build() - .unwrap(); + .from("user@localhost") + .cc(("cc@localhost", "Alias")) + .reply_to("reply@localhost") + .sender("sender@localhost") + .body("Hello World!") + .date(&date_now) + .subject("Hello") + .header(("X-test", "value")) + .build() + .unwrap(); assert_eq!(format!("{}", email), format!("To: \r\nFrom: \r\nCc: \"Alias\" \ @@ -543,16 +541,16 @@ mod test { let date_now = now(); let email = email_builder.to("user@localhost") - .from("user@localhost") - .cc(("cc@localhost", "Alias")) - .reply_to("reply@localhost") - .sender("sender@localhost") - .body("Hello World!") - .date(&date_now) - .subject("Hello") - .header(("X-test", "value")) - .build() - .unwrap(); + .from("user@localhost") + .cc(("cc@localhost", "Alias")) + .reply_to("reply@localhost") + .sender("sender@localhost") + .body("Hello World!") + .date(&date_now) + .subject("Hello") + .header(("X-test", "value")) + .build() + .unwrap(); assert_eq!(email.from_address(), "sender@localhost".to_string()); assert_eq!(email.to_addresses(), diff --git a/src/transport/sendmail/mod.rs b/src/transport/sendmail/mod.rs new file mode 100644 index 0000000..8198b20 --- /dev/null +++ b/src/transport/sendmail/mod.rs @@ -0,0 +1,75 @@ +//! This transport creates a file for each email, containing the enveloppe information and the email +//! itself. + +use transport::EmailTransport; +use transport::error::EmailResult; +use transport::smtp::response::Response; +use transport::smtp::response::{Category, Code, Severity}; +use email::SendableEmail; + +/// Writes the content and the enveloppe information to a file +pub struct SendmailEmailTransport { + command: String, +} + +impl SendmailEmailTransport { + /// Creates a new transport to the default "sendmail" command + pub fn new() -> SendmailEmailTransport { + SendmailEmailTransport { command: "sendmail".to_string() } + } + + /// Creates a new transport with a custom sendmail command + pub fn new_with_command(command: &str) -> SendmailEmailTransport { + SendmailEmailTransport { command: command.to_string() } + } +} + +impl EmailTransport for SendmailEmailTransport { + fn send(&mut self, email: T) -> EmailResult { + + + // Build TO list + // Set FROM + // Send content + + let sendmail_sh_frist_half = "sendmail ".to_string() + &to_address; + let sendmail_sh_second_half = " < email.txt".to_string(); + let sendmail_sh = sendmail_sh_frist_half + &sendmail_sh_second_half; + + let output = Command::new(self.command) + .arg("-c") + .arg(sendmail_sh) + .output() + .unwrap_or_else(|e| panic!("failed to execute process: {}", e)); + + println!("status: {}", output.status); + println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); + println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); + + + + + let mut file = self.path.clone(); + file.push(format!("{}.txt", email.message_id())); + + let mut f = try!(File::create(file.as_path())); + + let log_line = format!("{}: from=<{}> to=<{}>\n", + email.message_id(), + email.from_address(), + email.to_addresses().join("> to=<")); + + try!(f.write_all(log_line.as_bytes())); + try!(f.write_all(format!("{}", email.message()).as_bytes())); + + info!("{} status=", log_line); + + Ok(Response::new(Code::new(Severity::PositiveCompletion, Category::MailSystem, 0), + vec![format!("Ok: email written to {}", + file.to_str().unwrap_or("non-UTF-8 path"))])) + } + + fn close(&mut self) { + () + } +} diff --git a/src/transport/smtp/authentication.rs b/src/transport/smtp/authentication.rs index 6966e16..5f69e58 100644 --- a/src/transport/smtp/authentication.rs +++ b/src/transport/smtp/authentication.rs @@ -58,8 +58,8 @@ impl Mechanism { } None => { Ok(format!("{}{}{}{}", NUL, username, NUL, password) - .as_bytes() - .to_base64(base64::STANDARD)) + .as_bytes() + .to_base64(base64::STANDARD)) } } } @@ -80,8 +80,8 @@ impl Mechanism { hmac.input(&decoded_challenge); Ok(format!("{} {}", username, hmac.result().code().to_hex()) - .as_bytes() - .to_base64(base64::STANDARD)) + .as_bytes() + .to_base64(base64::STANDARD)) } } } @@ -105,10 +105,9 @@ mod test { let mechanism = Mechanism::CramMd5; assert_eq!(mechanism.response("alice", - "wonderland", - Some("PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg=\ - =")) - .unwrap(), + "wonderland", + Some("PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg==")) + .unwrap(), "YWxpY2UgNjRiMmE0M2MxZjZlZDY4MDZhOTgwOTE0ZTIzZTc1ZjA="); assert!(mechanism.response("alice", "wonderland", Some("tést")).is_err()); assert!(mechanism.response("alice", "wonderland", None).is_err()); diff --git a/src/transport/smtp/client/mod.rs b/src/transport/smtp/client/mod.rs index 710b247..1217bf7 100644 --- a/src/transport/smtp/client/mod.rs +++ b/src/transport/smtp/client/mod.rs @@ -23,12 +23,12 @@ pub mod net; #[inline] fn escape_dot(string: &str) -> String { if string.starts_with(".") { - format!(".{}", string) - } else { - string.to_string() - } - .replace("\r.", "\r..") - .replace("\n.", "\n..") + format!(".{}", string) + } else { + string.to_string() + } + .replace("\r.", "\r..") + .replace("\n.", "\n..") } /// Returns the string replacing all the CRLF with "\" diff --git a/src/transport/smtp/client/net.rs b/src/transport/smtp/client/net.rs index 15472a1..d771f26 100644 --- a/src/transport/smtp/client/net.rs +++ b/src/transport/smtp/client/net.rs @@ -45,7 +45,7 @@ impl Connector for NetworkStream { }; Ok(()) } - + fn is_encrypted(&self) -> bool { match *self { NetworkStream::Plain(_) => false, diff --git a/src/transport/smtp/extension.rs b/src/transport/smtp/extension.rs index a1e2289..a53e8ce 100644 --- a/src/transport/smtp/extension.rs +++ b/src/transport/smtp/extension.rs @@ -171,12 +171,9 @@ mod test { #[test] fn test_serverinfo() { - let response = Response::new(Code::new(Severity::PositiveCompletion, - Category::Unspecified4, - 1), - vec!["me".to_string(), - "8BITMIME".to_string(), - "SIZE 42".to_string()]); + let response = + Response::new(Code::new(Severity::PositiveCompletion, Category::Unspecified4, 1), + vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]); let mut features = HashSet::new(); assert!(features.insert(Extension::EightBitMime)); @@ -192,13 +189,12 @@ mod test { assert!(!server_info.supports_feature(&Extension::StartTls)); assert!(!server_info.supports_auth_mechanism(Mechanism::CramMd5)); - let response2 = Response::new(Code::new(Severity::PositiveCompletion, - Category::Unspecified4, - 1), - vec!["me".to_string(), - "AUTH PLAIN CRAM-MD5 OTHER".to_string(), - "8BITMIME".to_string(), - "SIZE 42".to_string()]); + let response2 = + Response::new(Code::new(Severity::PositiveCompletion, Category::Unspecified4, 1), + vec!["me".to_string(), + "AUTH PLAIN CRAM-MD5 OTHER".to_string(), + "8BITMIME".to_string(), + "SIZE 42".to_string()]); let mut features2 = HashSet::new(); assert!(features2.insert(Extension::EightBitMime)); diff --git a/src/transport/smtp/mod.rs b/src/transport/smtp/mod.rs index a194b64..c0e0932 100644 --- a/src/transport/smtp/mod.rs +++ b/src/transport/smtp/mod.rs @@ -330,7 +330,8 @@ impl EmailTransport for SmtpTransport { Some(mechanism) => vec![mechanism], None => { match self.client.is_encrypted() { - // If encrypted, allow all mechanisms, with a preference for the simplest + // If encrypted, allow all mechanisms, with a preference for the + // simplest true => vec![Mechanism::Plain, Mechanism::CramMd5], // If not encrypted, do not all clear-text passwords false => vec![Mechanism::CramMd5], @@ -354,13 +355,13 @@ impl EmailTransport for SmtpTransport { // Mail let mail_options = match (self.server_info - .as_ref() - .unwrap() - .supports_feature(&Extension::EightBitMime), + .as_ref() + .unwrap() + .supports_feature(&Extension::EightBitMime), self.server_info - .as_ref() - .unwrap() - .supports_feature(&Extension::SmtpUtfEight)) { + .as_ref() + .unwrap() + .supports_feature(&Extension::SmtpUtfEight)) { (true, true) => Some("BODY=8BITMIME SMTPUTF8"), (true, false) => Some("BODY=8BITMIME"), (false, _) => None, diff --git a/src/transport/smtp/response.rs b/src/transport/smtp/response.rs index 0a9810c..0988ba0 100644 --- a/src/transport/smtp/response.rs +++ b/src/transport/smtp/response.rs @@ -410,7 +410,7 @@ mod test { vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]) - .is_positive()); + .is_positive()); assert!(!Response::new(Code { severity: "5".parse::().unwrap(), category: "4".parse::().unwrap(), @@ -419,7 +419,7 @@ mod test { vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]) - .is_positive()); + .is_positive()); } #[test] @@ -521,7 +521,7 @@ mod test { vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]) - .has_code(241)); + .has_code(241)); assert!(!Response::new(Code { severity: "2".parse::().unwrap(), category: "4".parse::().unwrap(), @@ -530,7 +530,7 @@ mod test { vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]) - .has_code(251)); + .has_code(251)); } #[test]