diff --git a/examples/simple.rs b/examples/simple.rs new file mode 100644 index 0000000..bf2de9f --- /dev/null +++ b/examples/simple.rs @@ -0,0 +1,26 @@ +#![feature(slicing_syntax)] +extern crate irc; + +use std::collections::HashMap; +use irc::data::config::Config; +use irc::server::{IrcServer, Server}; +use irc::server::utils::identify; + +fn main() { + let config = Config { + owners: vec!("awe".into_string()), + nickname: "pickles".into_string(), + username: "pickles".into_string(), + realname: "pickles".into_string(), + password: "".into_string(), + server: "irc.fyrechat.net".into_string(), + port: 6667, + channels: vec!("#vana".into_string()), + options: HashMap::new(), + }; + let server = IrcServer::from_config(config).unwrap(); + identify(&server).unwrap(); + for message in server.iter() { + println!("RCV: {}", message); + } +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 38456ba..483d820 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -30,15 +30,19 @@ pub struct IrcServer<'a, T, U> where T: IrcWriter, U: IrcReader { } impl<'a> IrcServer<'a, BufferedWriter, BufferedReader> { - /// Creates a new IRC Server connection from the specified configuration, connecting immediately. + /// Creates a new IRC Server connection from the configuration at the specified path, connecting immediately. #[experimental] pub fn new(config: &str) -> IoResult, BufferedReader>> { let config = try!(Config::load_utf8(config)); let conn = try!(Connection::connect(config.server[], config.port)); - Ok(IrcServer { - conn: conn, - config: config - }) + IrcServer::from_connection(config, conn) + } + + /// Creates a new IRC server connection from the specified configuration, connecting immediately. + #[experimental] + pub fn from_config(config: Config) -> IoResult, BufferedReader>> { + let conn = try!(Connection::connect(config.server[], config.port)); + IrcServer::from_connection(config, conn) } } @@ -59,10 +63,10 @@ impl<'a, T, U> Server<'a, T, U> for IrcServer<'a, T, U> where T: IrcWriter, U: I impl<'a, T, U> IrcServer<'a, T, U> where T: IrcWriter, U: IrcReader { /// Creates an IRC server from the specified configuration, and any arbitrary Connection #[experimental] - pub fn from_connection(config: &str, conn: Connection) -> IoResult> { + pub fn from_connection(config: Config, conn: Connection) -> IoResult> { Ok(IrcServer { conn: conn, - config: try!(Config::load_utf8(config)) + config: config }) } }