diff --git a/examples/client.rs b/examples/client.rs index 5a1b53a..55525d4 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -28,7 +28,7 @@ fn main() { .subject("Hello") .build(); - let mut sender: Sender = SenderBuilder::localhost().hello_name("localhost") + let mut sender: Sender = SenderBuilder::localhost().hello_name("localhost") .enable_connection_reuse(true).build(); for _ in (1..5) { diff --git a/src/client/mod.rs b/src/client/mod.rs index 6942b24..17257c1 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -10,17 +10,16 @@ //! SMTP client use std::string::String; -use std::net::TcpStream; use std::net::{SocketAddr, ToSocketAddrs}; use std::io::{BufRead, BufStream, Read, Write}; use response::{Response, Severity, Category}; use error::SmtpResult; -use client::connecter::Connecter; +use client::net::{Connector, SmtpStream}; use client::authentication::{plain, cram_md5}; use {CRLF, MESSAGE_ENDING}; -pub mod connecter; +pub mod net; mod authentication; /// Returns the string after adding a dot at the beginning of each line starting with a dot @@ -43,7 +42,7 @@ fn escape_crlf(string: &str) -> String { } /// Structure that implements the SMTP client -pub struct Client { +pub struct Client { /// TCP stream between client and server /// Value is None before connection stream: Option>, @@ -58,7 +57,7 @@ macro_rules! return_err ( }) ); -impl Client { +impl Client { /// Creates a new SMTP client /// /// It does not connects to the server, but only creates the `Client` @@ -70,7 +69,7 @@ impl Client { } } -impl Client { +impl Client { /// Closes the SMTP transaction if possible pub fn close(&mut self) { let _ = self.quit(); @@ -85,7 +84,7 @@ impl Client { } // Try to connect - self.stream = Some(BufStream::new(try!(Connecter::connect(&self.server_addr)))); + self.stream = Some(BufStream::new(try!(Connector::connect(&self.server_addr)))); self.get_reply() } @@ -172,7 +171,7 @@ impl Client { self.command(&format!("AUTH CRAM-MD5 {}", cram_md5(username, password, &encoded_challenge))) } - /// Sends the message content and close + /// Sends the message content pub fn message(&mut self, message_content: &str) -> SmtpResult { self.send_server(&escape_dot(message_content), MESSAGE_ENDING) } diff --git a/src/client/connecter.rs b/src/client/net.rs similarity index 73% rename from src/client/connecter.rs rename to src/client/net.rs index 9bb5098..601c975 100644 --- a/src/client/connecter.rs +++ b/src/client/net.rs @@ -7,20 +7,24 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! A trait to represent a connected stream +//! A trait to represent a stream use std::io; use std::net::SocketAddr; use std::net::TcpStream; /// A trait for the concept of opening a stream -pub trait Connecter { +pub trait Connector { /// Opens a connection to the given IP socket fn connect(addr: &SocketAddr) -> io::Result; } -impl Connecter for TcpStream { - fn connect(addr: &SocketAddr) -> io::Result { +impl Connector for SmtpStream { + fn connect(addr: &SocketAddr) -> io::Result { TcpStream::connect(addr) } } + +/// Represents an atual SMTP network stream +//Used later for ssl +pub type SmtpStream = TcpStream; diff --git a/src/lib.rs b/src/lib.rs index f9519a2..97781e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,6 @@ //! ```rust,no_run //! use smtp::sender::{Sender, SenderBuilder}; //! use smtp::mailer::EmailBuilder; -//! use std::net::TcpStream; //! //! // Create an email //! let email = EmailBuilder::new() @@ -53,7 +52,7 @@ //! .build(); //! //! // Open a local connection on port 25 -//! let mut sender: Sender = SenderBuilder::localhost().build(); +//! let mut sender = SenderBuilder::localhost().build(); //! // Send the email //! let result = sender.send(email); //! @@ -65,7 +64,6 @@ //! ```rust,no_run //! use smtp::sender::{Sender, SenderBuilder}; //! use smtp::mailer::EmailBuilder; -//! use std::net::TcpStream; //! //! let mut builder = EmailBuilder::new(); //! builder = builder.to(("user@example.org", "Alias name")); @@ -81,7 +79,7 @@ //! let email = builder.build(); //! //! // Connect to a remote server on a custom port -//! let mut sender: Sender = SenderBuilder::new(("server.tld", 10025)) +//! let mut sender = SenderBuilder::new(("server.tld", 10025)) //! // Set the name sent during EHLO/HELO, default is `localhost` //! .hello_name("my.hostname.tld") //! // Add credentials for authentication @@ -107,7 +105,6 @@ //! ```rust,no_run //! use smtp::sender::{Sender, SenderBuilder}; //! use smtp::sendable_email::SimpleSendableEmail; -//! use std::net::TcpStream; //! //! // Create a minimal email //! let email = SimpleSendableEmail::new( @@ -116,7 +113,7 @@ //! "Hello world !" //! ); //! -//! let mut sender: Sender = SenderBuilder::localhost().build(); +//! let mut sender = SenderBuilder::localhost().build(); //! let result = sender.send(email); //! assert!(result.is_ok()); //! ``` @@ -127,10 +124,11 @@ //! //! ```rust,no_run //! use smtp::client::Client; +//! use smtp::client::net::SmtpStream; //! use smtp::SMTP_PORT; //! use std::net::TcpStream; //! -//! let mut email_client: Client = Client::new(("localhost", SMTP_PORT)); +//! let mut email_client: Client = Client::new(("localhost", SMTP_PORT)); //! let _ = email_client.connect(); //! let _ = email_client.ehlo("my_hostname"); //! let _ = email_client.mail("user@example.com", None); diff --git a/src/sender/mod.rs b/src/sender/mod.rs index a01d035..3a9322d 100644 --- a/src/sender/mod.rs +++ b/src/sender/mod.rs @@ -10,9 +10,7 @@ //! Sends an email using the client use std::string::String; -use std::net::TcpStream; use std::net::{SocketAddr, ToSocketAddrs}; -use std::io::{Read, Write}; use uuid::Uuid; @@ -22,7 +20,7 @@ use error::{SmtpResult, SmtpError}; use sendable_email::SendableEmail; use sender::server_info::ServerInfo; use client::Client; -use client::connecter::Connecter; +use client::net::SmtpStream; mod server_info; @@ -87,7 +85,7 @@ impl SenderBuilder { /// Build the SMTP client /// /// It does not connects to the server, but only creates the `Sender` - pub fn build(self) -> Sender { + pub fn build(self) -> Sender { Sender::new(self) } } @@ -102,7 +100,7 @@ struct State { } /// Structure that implements the high level SMTP client -pub struct Sender { +pub struct Sender { /// Information about the server /// Value is None before HELO/EHLO server_info: Option, @@ -111,7 +109,7 @@ pub struct Sender { /// Information about the client client_info: SenderBuilder, /// Low level client - client: Client, + client: Client, } macro_rules! try_smtp ( @@ -129,12 +127,12 @@ macro_rules! try_smtp ( }) ); -impl Sender { +impl Sender { /// Creates a new SMTP client /// /// It does not connects to the server, but only creates the `Sender` - pub fn new(builder: SenderBuilder) -> Sender { - let client: Client = Client::new(builder.server_addr); + pub fn new(builder: SenderBuilder) -> Sender { + let client: Client = Client::new(builder.server_addr); Sender{ client: client, server_info: None, @@ -145,9 +143,7 @@ impl Sender { }, } } -} -impl Sender { /// Reset the client state fn reset(&mut self) { // Close the SMTP transaction if needed