Simplify login using ToSocketAddrs
This commit is contained in:
parent
8477a858f2
commit
eee1ad355a
2 changed files with 15 additions and 19 deletions
|
|
@ -6,7 +6,7 @@ use imap::client::IMAPStream;
|
|||
use imap::client::IMAPMailbox;
|
||||
|
||||
fn main() {
|
||||
let mut imap_socket = match IMAPStream::connect("imap.gmail.com", 993, Some(SslContext::new(SslMethod::Sslv23).unwrap())) {
|
||||
let mut imap_socket = match IMAPStream::connect(("imap.gmail.com", 993), Some(SslContext::new(SslMethod::Sslv23).unwrap())) {
|
||||
Ok(s) => s,
|
||||
Err(e) => panic!("{}", e)
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::net::TcpStream;
|
||||
use std::net::{TcpStream, ToSocketAddrs};
|
||||
use openssl::ssl::{SslContext, SslStream};
|
||||
use std::io::{Error, ErrorKind, Read, Result, Write};
|
||||
use regex::Regex;
|
||||
|
|
@ -10,8 +10,6 @@ enum IMAPStreamTypes {
|
|||
|
||||
pub struct IMAPStream {
|
||||
stream: IMAPStreamTypes,
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
tag: u32,
|
||||
tag_prefix: &'static str
|
||||
}
|
||||
|
|
@ -28,21 +26,19 @@ pub struct IMAPMailbox {
|
|||
|
||||
impl IMAPStream {
|
||||
|
||||
pub fn connect<S: Into<String>>(host: S, port: u16, ssl_context: Option<SslContext>) -> Result<IMAPStream> {
|
||||
let host_string = host.into();
|
||||
let connect_string = format!("{}:{}", host_string, port);
|
||||
let tcp_stream = try!(TcpStream::connect(&*connect_string));
|
||||
pub fn connect<A: ToSocketAddrs>(addr: A, ssl_context: Option<SslContext>) -> Result<IMAPStream> {
|
||||
match TcpStream::connect(addr) {
|
||||
Ok(stream) => {
|
||||
let mut socket = match ssl_context {
|
||||
Some(context) => IMAPStream { stream: IMAPStreamTypes::Ssl(SslStream::connect(&context, tcp_stream).unwrap()), host: host_string, port: port, tag: 1, tag_prefix: "a"},
|
||||
None => IMAPStream { stream: IMAPStreamTypes::Basic(tcp_stream), host: host_string, port: port, tag: 1, tag_prefix: "a"},
|
||||
Some(context) => IMAPStream { stream: IMAPStreamTypes::Ssl(SslStream::connect(&context, stream).unwrap()), tag: 1, tag_prefix: "a"},
|
||||
None => IMAPStream { stream: IMAPStreamTypes::Basic(stream), tag: 1, tag_prefix: "a"},
|
||||
};
|
||||
|
||||
match socket.read_greeting() {
|
||||
Ok(_) => (),
|
||||
Err(_) => return Err(Error::new(ErrorKind::Other, "Failed to read greet response"))
|
||||
}
|
||||
|
||||
try!(socket.read_greeting());
|
||||
Ok(socket)
|
||||
},
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
|
||||
//LOGIN
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue