From b4e0d46f66cca1572ce59ffe8dc1e879f3190d5d Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Mon, 3 Nov 2014 12:00:00 +0100 Subject: [PATCH] Use the extensions supported by the server --- src/client/mod.rs | 91 ++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 2274298..aa72123 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -75,35 +75,17 @@ impl Client { } } - /// Connects to the configured server - pub fn connect(&mut self) -> Result { - // connect should not be called when the client is already connected - if !self.stream.is_none() { - panic!("The connection is already established"); - } - - // Try to connect - self.stream = match Connecter::connect(self.host.clone().as_slice(), self.port) { - Ok(stream) => Some(stream), - Err(..) => panic!("Cannot connect to the server") - }; - - // Log the connection - info!("Connection established to {}[{}]:{}", - self.host, self.stream.clone().unwrap().peer_name().unwrap().ip, self.port); - - match self.stream.clone().unwrap().get_reply() { - Some(response) => match response.with_code(vec!(220)) { - Ok(response) => { - self.state = transaction::Connected; - Ok(response) - }, - Err(response) => { - Err(response) - } - }, - None => panic!("No banner on {}", self.host) + /// Closes the connection and fail with a given messgage + fn smtp_fail(&mut self, reason: T) { + let is_connected = self.is_connected::(); + if is_connected { + match self.quit::() { + Ok(..) => {}, + Err(response) => panic!("Failed: {}", response) + } } + self.close(); + panic!("Failed: {}", reason); } /// Sends an email @@ -142,12 +124,23 @@ impl Client { } } + // Get maximum message size if defined + let max_size = match self.server_info.clone().unwrap().supports_feature(extension::Size(0)) { + Ok(extension::Size(max)) => max, + _ => -1 + }; + + // Check maximum message size + if max_size > 0 && message.len() > max_size { + self.smtp_fail::("Message is too big. The limit is {}"); + } + // Mail smtp_result = self.mail::(from_address.clone(), None); self.smtp_fail_if_err::(smtp_result); // Log the mail command - info!("from=<{}>, size={}, nrcpt={}", from_address, 42u, to_addresses.len()); + info!("from=<{}>, size={}, nrcpt={}", from_address, message.len(), to_addresses.len()); // Recipient // TODO Return rejected addresses @@ -185,22 +178,40 @@ impl Client { self.stream.clone().unwrap().send_and_get_response(format!("{}", command).as_slice()) } - /// Sends an email + /// Sends the email content fn send_message(&mut self, message: String) -> Response { self.stream.clone().unwrap().send_and_get_response(format!("{}{}.{}", message, CRLF, CRLF).as_slice()) } - /// Closes the connection and fail with a given messgage - fn smtp_fail(&mut self, reason: T) { - let is_connected = self.is_connected::(); - if is_connected { - match self.quit::() { - Ok(..) => {}, - Err(response) => panic!("Failed: {}", response) - } + /// Connects to the configured server + pub fn connect(&mut self) -> Result { + // connect should not be called when the client is already connected + if !self.stream.is_none() { + panic!("The connection is already established"); + } + + // Try to connect + self.stream = match Connecter::connect(self.host.clone().as_slice(), self.port) { + Ok(stream) => Some(stream), + Err(..) => panic!("Cannot connect to the server") + }; + + // Log the connection + info!("Connection established to {}[{}]:{}", + self.host, self.stream.clone().unwrap().peer_name().unwrap().ip, self.port); + + match self.stream.clone().unwrap().get_reply() { + Some(response) => match response.with_code(vec!(220)) { + Ok(response) => { + self.state = transaction::Connected; + Ok(response) + }, + Err(response) => { + Err(response) + } + }, + None => panic!("No banner on {}", self.host) } - self.close(); - panic!("Failed: {}", reason); } /// Checks if the server is connected