diff options
author | Till Hoeppner | 2015-06-17 17:03:56 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-06-17 17:04:06 +0200 |
commit | 2902d8734f5c1f036eb340e35e8d1dc7a74c23b6 (patch) | |
tree | d09ff702397a73c66c938b5891765e92e18abd69 /examples/01.rs | |
parent | 63838165c31397fec199bf99c96497a1169c4d52 (diff) | |
download | irsc-2902d8734f5c1f036eb340e35e8d1dc7a74c23b6.tar.gz irsc-2902d8734f5c1f036eb340e35e8d1dc7a74c23b6.tar.xz irsc-2902d8734f5c1f036eb340e35e8d1dc7a74c23b6.zip |
This commit is a little too large. Sorry. :S
Diffstat (limited to 'examples/01.rs')
-rw-r--r-- | examples/01.rs | 46 |
1 files changed, 13 insertions, 33 deletions
diff --git a/examples/01.rs b/examples/01.rs index 96848f6..2f82839 100644 --- a/examples/01.rs +++ b/examples/01.rs @@ -1,67 +1,47 @@ extern crate irsc; extern crate env_logger; -#[cfg(feature = "ssl")] extern crate openssl; -use std::borrow::ToOwned; -use std::borrow::Cow::*; - -use irsc::client::Client; use irsc::color::bold; 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."; -fn callback(server: &mut Client, msg: &Message) { - match Command::from_message(msg) { - Some(PRIVMSG(to, content)) => { - let from = msg.prefix().and_then(Ident::parse).unwrap(); +fn callback(server: &mut Client, msg: &Message, event: Option<Event>) { + match event { + Some(Event::Command(PRIVMSG(to, content))) => { + let from = msg.ident().unwrap(); let response = match msg.msg_type { MsgType::Irc => format!("{} wrote: {}", from.nickname, bold(&content)), - MsgType::Ctcp => format!("{} emoted: {}", from.nickname, bold(&content["ACTION ".len()..])) + MsgType::Ctcp => format!("{} emoted: {}", from.nickname, + bold(&content["ACTION ".len()..])) }; // only send to channels, to prevent recursion when we are pm'ed // technically, there are other prefixes than '#', but ignoring them is fine if to.starts_with("#") { - server.send(PRIVMSG(to, Owned(response))).unwrap(); + server.msg(&to, &response); } }, - _ => () - } - - match Reply::from_message(msg) { - Some(RPL_WELCOME(_)) => { - server.send(JOIN(vec![Borrowed("#botzoo")], vec![])).unwrap(); + Some(Event::Reply(RPL_WELCOME(_))) => { + server.join("#botzoo", None); }, _ => () } } -#[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(); - connect(&mut s); - s.send(NICK(Borrowed(NAME))).unwrap(); - s.send(USER(Borrowed(NAME), Borrowed("*"), Borrowed("*"), Borrowed(DESC))).unwrap(); + let ssl = Ssl::new(&SslContext::new(SslMethod::Tlsv1).unwrap()).unwrap(); + s.connect_ssl("irc.mozilla.org", 6697, ssl); + s.register(NAME, NAME, DESC); // Dedicate this thread to listening and event processing - s.listen(callback).unwrap(); + s.listen(Some(callback)); } |