diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/01.rs | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/examples/01.rs b/examples/01.rs index eb662ca..5021aac 100644 --- a/examples/01.rs +++ b/examples/01.rs @@ -1,46 +1,49 @@ #![feature(plugin)] #![plugin(regex_macros)] -/*extern crate irsc; +extern crate irsc; +extern crate env_logger; use std::borrow::ToOwned; +use std::borrow::Cow::*; -// use std::sync::{Once, ONCE_INIT}; - -use irsc::server::Server; +use irsc::client::Client; use irsc::color::bold; -use irsc::message::{ Message, Command }; +use irsc::*; +use irsc::Command::*; +use irsc::Reply::*; static NAME: &'static str = "rusticbot"; static DESC: &'static str = "A bot, written in Rust."; -// static START: Once = ONCE_INIT; - -fn callback(server: &mut Server, msg: &Message) { +fn callback(server: &mut Client, msg: &Message) { match Command::from_message(msg) { - Some(Command::PrivMsg { from, content, .. }) => { - let response = format!("You wrote: {}", bold(&content)); - server.msg(&from.unwrap(), &response).unwrap(); + Some(PRIVMSG(to, content)) => { + let from = msg.prefix().and_then(Ident::parse).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()..])) + }; + server.send(PRIVMSG(to, Owned(response))).unwrap(); }, - _ => {} + _ => () } - /* - "001" => { - START.call_once(|| { - }) + match Reply::from_message(msg) { + Some(RPL_WELCOME(_)) => { + server.send(JOIN(vec![Borrowed("#botzoo")], vec![])).unwrap(); }, _ => () - }*/ + } } -*/ + fn main() { - /*let mut s = Server::new(); + env_logger::init().unwrap(); + let mut s = Client::new(); s.connect("irc.mozilla.org".to_owned(), 6667).unwrap(); - s.nick(NAME).unwrap(); - s.user(NAME, "*", "*", DESC).unwrap(); - s.join("#botzoo").unwrap(); + s.send(NICK(Borrowed(NAME))).unwrap(); + s.send(USER(Borrowed(NAME), Borrowed("*"), Borrowed("*"), Borrowed(DESC))).unwrap(); // Dedicate this thread to listening and event processing - s.listen(&[callback]).unwrap();*/ + s.listen(callback).unwrap(); } |