From d59672ef269d884ae286e7ada510aca356ab58fa Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sat, 4 Apr 2015 23:32:14 +0200 Subject: [PATCH] Get ready for rust 1.0.0-beta --- examples/client.rs | 134 ++++----------------------------------------- src/client/mod.rs | 1 + src/extension.rs | 4 +- src/lib.rs | 1 - src/response.rs | 2 +- src/sender/mod.rs | 8 +-- 6 files changed, 19 insertions(+), 131 deletions(-) diff --git a/examples/client.rs b/examples/client.rs index c76b358..5a1b53a 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -7,150 +7,38 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(core, old_io, rustc_private, collections)] - #[macro_use] extern crate log; extern crate env_logger; extern crate smtp; -extern crate getopts; -use std::old_io::stdin; -use std::string::String; -use std::env; -use getopts::{optopt, optflag, getopts, OptGroup, usage}; use std::net::TcpStream; use smtp::sender::{Sender, SenderBuilder}; -use smtp::error::SmtpResult; use smtp::mailer::EmailBuilder; -fn sendmail(source_address: String, recipient_addresses: Vec, message: String, subject: String, - server: String, port: u16, my_hostname: String, number: u16) -> SmtpResult { +fn main() { + env_logger::init().unwrap(); let mut email_builder = EmailBuilder::new(); - for destination in recipient_addresses.iter() { - email_builder = email_builder.to(destination.as_slice()); - } - let email = email_builder.from(source_address.as_slice()) - .body(message.as_slice()) - .subject(subject.as_slice()) + + email_builder = email_builder.to("user@localhost"); + let email = email_builder.from("user@localhost") + .body("Hello World!") + .subject("Hello") .build(); - let mut sender: Sender = SenderBuilder::new((server.as_slice(), port)).hello_name(my_hostname.as_slice()) + let mut sender: Sender = SenderBuilder::localhost().hello_name("localhost") .enable_connection_reuse(true).build(); - for _ in (1..number) { + for _ in (1..5) { let _ = sender.send(email.clone()); } let result = sender.send(email); sender.close(); - result -} - -fn print_usage(description: String, _opts: &[OptGroup]) { - println!("{}", usage(description.as_slice(), _opts)); -} - -fn main() { - env_logger::init().unwrap(); - - let args = env::args(); - - let mut args_string = Vec::new(); - for arg in args { - args_string.push(arg.clone()); - }; - - let program = args_string[0].clone(); - let description = format!("Usage: {0} [options...] recipients\n\n\ - This program reads a message on standard input until it reaches\ - EOF, then tries to send it using the given paramters.\n\n\ - Example: {0} -r user@example.org user@example.com < message.txt", - program); - - let opts = [ - optopt("n", "number", "set the number of emails to send", "NUMBER"), - optopt("s", "subject", "set the email subject", "SUBJECT"), - optopt("r", "reverse-path", "set the sender address", "FROM_ADDRESS"), - optopt("p", "port", "set the port to use, default is 25", "PORT"), - optopt("a", "server", "set the server to use, default is localhost", "SERVER"), - optopt("m", "my-hostname", "set the hostname used by the client", "MY_HOSTNAME"), - optflag("h", "help", "print this help menu"), - ]; - - let matches = match getopts(args_string.tail(), &opts) { - Ok(m) => m, - Err(f) => panic!("{}", f), - }; - - if matches.opt_present("h") { - print_usage(description, &opts); - return; - } - - if !matches.opt_present("r") { - println!("The sender option is required"); - print_usage(program, &opts); - return; - } - - let recipients_str: &str = if !matches.free.is_empty() { - matches.free[0].as_slice() - } else { - print_usage(description, &opts); - return; - }; - - let mut recipients = Vec::new(); - for recipient in recipients_str.split(' ') { - recipients.push(recipient.to_string()); - } - - let mut message = String::new(); - - let mut line = stdin().read_line(); - while line.is_ok() { - message.push_str(line.unwrap().as_slice()); - line = stdin().read_line(); - } - - match sendmail( - // sender - matches.opt_str("r").unwrap().clone(), - // recipients - recipients, - // message content - message, - // subject - match matches.opt_str("s") { - Some(ref subject) => subject.clone(), - None => "(empty subject)".to_string(), - }, - // server - match matches.opt_str("a") { - Some(ref server) => server.clone(), - None => "localhost".to_string(), - }, - // port - match matches.opt_str("p") { - Some(port) => port.as_slice().parse::().unwrap(), - None => 25, - }, - // my hostname - match matches.opt_str("m") { - Some(ref my_hostname) => my_hostname.clone(), - None => "localhost".to_string(), - }, - // number of copies - match matches.opt_str("n") { - Some(ref n) => n.as_slice().parse::().unwrap(), - None => 1, - }, - ) - { + match result { Ok(..) => info!("Email sent successfully"), - Err(error) => error!("{}", error), + Err(error) => error!("{:?}", error), } } diff --git a/src/client/mod.rs b/src/client/mod.rs index befa765..6942b24 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -74,6 +74,7 @@ impl Client { /// Closes the SMTP transaction if possible pub fn close(&mut self) { let _ = self.quit(); + self.stream = None; } /// Connects to the configured server diff --git a/src/extension.rs b/src/extension.rs index 86e50ef..4710194 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -68,7 +68,9 @@ impl Extension { for line in response.message() { if let Ok(keywords) = Extension::from_str(&line) { - esmtp_features.push_all(&keywords); + for keyword in keywords { + esmtp_features.push(keyword); + } }; } diff --git a/src/lib.rs b/src/lib.rs index 167cea1..f9519a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,7 +140,6 @@ //! let _ = email_client.quit(); //! ``` -#![feature(plugin, collections, str_words, slice_patterns)] #![deny(missing_docs)] #[macro_use] extern crate log; diff --git a/src/response.rs b/src/response.rs index e3b3098..72ba039 100644 --- a/src/response.rs +++ b/src/response.rs @@ -189,7 +189,7 @@ impl Response { pub fn first_word(&self) -> Option { match self.message.is_empty() { true => None, - false => Some(self.message[0].words().next().unwrap().to_string()) + false => Some(self.message[0].split(" ").next().unwrap().to_string()), } } diff --git a/src/sender/mod.rs b/src/sender/mod.rs index 558a4c0..a01d035 100644 --- a/src/sender/mod.rs +++ b/src/sender/mod.rs @@ -121,7 +121,7 @@ macro_rules! try_smtp ( Err(err) => { if !$client.state.panic { $client.state.panic = true; - $client.client.close(); + $client.reset(); } return Err(err) }, @@ -265,10 +265,8 @@ impl Sender { // Log the message info!("{}: conn_use={}, size={}, status=sent ({})", current_message, - self.state.connection_reuse_count, message.len(), match result.as_ref().ok().unwrap().message().as_ref() { - [ref line, ..] => line.as_ref(), - [] => "no response", - } + self.state.connection_reuse_count, message.len(), + result.as_ref().ok().unwrap().message().iter().next().unwrap_or(&"no response".to_string()) ); }