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