diff options
author | Till Hoeppner | 2015-05-04 21:14:52 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-05-04 21:14:52 +0200 |
commit | fbb7251493ff5a9bc42f849b9b781e69aef9d184 (patch) | |
tree | a0c7bb5196ccc970f67bca40679d34a04b1102c2 | |
parent | f3f58e1770d1f2ae9fe69415376690125e3c8269 (diff) | |
download | irsc-fbb7251493ff5a9bc42f849b9b781e69aef9d184.tar.gz irsc-fbb7251493ff5a9bc42f849b9b781e69aef9d184.tar.xz irsc-fbb7251493ff5a9bc42f849b9b781e69aef9d184.zip |
More control about SSL methods, and proper selection between ssl vs plain at compile time in example
-rw-r--r-- | examples/01.rs | 22 | ||||
-rw-r--r-- | src/client.rs | 12 |
2 files changed, 20 insertions, 14 deletions
diff --git a/examples/01.rs b/examples/01.rs index 595bc70..96848f6 100644 --- a/examples/01.rs +++ b/examples/01.rs @@ -1,5 +1,7 @@ extern crate irsc; extern crate env_logger; +#[cfg(feature = "ssl")] +extern crate openssl; use std::borrow::ToOwned; use std::borrow::Cow::*; @@ -10,6 +12,9 @@ use irsc::*; use irsc::Command::*; use irsc::Reply::*; +#[cfg(feature = "ssl")] +use openssl::ssl::{ Ssl, SslContext, SslMethod }; + static NAME: &'static str = "rusticbot"; static DESC: &'static str = "A bot, written in Rust."; @@ -39,14 +44,21 @@ fn callback(server: &mut Client, msg: &Message) { } } +#[cfg(feature = "ssl")] +fn connect(s: &mut Client) { + let ssl = Ssl::new(&SslContext::new(SslMethod::Tlsv1).unwrap()).unwrap(); + s.connect_ssl("irc.mozilla.org".to_owned(), 6697, ssl).unwrap(); +} + +#[cfg(not(feature = "ssl"))] +fn connect(s: &mut Client) { + s.connect("irc.mozilla.org".to_owned(), 6667).unwrap(); +} + fn main() { env_logger::init().unwrap(); let mut s = Client::new(); - if cfg!(feature = "ssl") { - s.connect_ssl("irc.mozilla.org".to_owned(), 6697).unwrap(); - } else { - s.connect("irc.mozilla.org".to_owned(), 6667).unwrap(); - } + connect(&mut s); s.send(NICK(Borrowed(NAME))).unwrap(); s.send(USER(Borrowed(NAME), Borrowed("*"), Borrowed("*"), Borrowed(DESC))).unwrap(); diff --git a/src/client.rs b/src/client.rs index a688565..e573596 100644 --- a/src/client.rs +++ b/src/client.rs @@ -14,7 +14,7 @@ use command::Command; use ::{ DEBUG, Result, IrscError }; #[cfg(feature = "ssl")] -use openssl::ssl::{ SslContext, SslMethod, SslStream }; +use openssl::ssl::{ Ssl, SslContext, SslMethod, SslStream }; /// Yes, I don't like the name either, but it's private, so... enum StreamKind { @@ -81,7 +81,7 @@ impl Client { } #[cfg(feature = "ssl")] - pub fn connect_ssl(&mut self, host: String, port: u16) -> Result<()> { + pub fn connect_ssl(&mut self, host: String, port: u16, ssl: Ssl) -> Result<()> { let s = &mut self.stream; match *s { Some(_) => return Err(IrscError::AlreadyConnected), _ => () }; let tcp_stream = match TcpStream::connect((host.as_ref(), port)) { @@ -89,19 +89,13 @@ impl Client { Err(e) => return Err(IrscError::Io(e)) }; - let ssl = try!(SslContext::new(SslMethod::Tlsv1)); - match tcp_stream.map(|tcp| SslStream::new(&ssl, tcp)) { + match tcp_stream.map(|tcp| SslStream::new_from(ssl, tcp)) { Some(Ok(ssl_stream)) => { *s = Some(StreamKind::Ssl(ssl_stream)); Ok(()) }, Some(Err(ssl_error)) => Err(IrscError::Ssl(ssl_error)), None => Err(IrscError::NotConnected) } } - #[cfg(not(feature = "ssl"))] - pub fn connect_ssl(&mut self, _host: String, _port: u16) -> Result<()> { - panic!("Not compiled with ssl feature.") - } - #[inline] fn send_raw(&mut self, s: &str) -> Result<()> { info!(">> {}", s); |