From efaa1d0aa77d6a583f71d0cfa2787369cbf112a5 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Sun, 28 Jun 2015 18:51:59 +0200 Subject: Add example for event API --- examples/02.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/02.rs (limited to 'examples') diff --git a/examples/02.rs b/examples/02.rs new file mode 100644 index 0000000..69c9d0f --- /dev/null +++ b/examples/02.rs @@ -0,0 +1,40 @@ +extern crate irsc; +extern crate openssl; + +use irsc::*; +use irsc::Command::*; +use irsc::Reply::*; + +use openssl::ssl::{ Ssl, SslContext, SslMethod }; + +fn main() { + let mut s = OwnedClient::new(); + let ssl = Ssl::new(&SslContext::new(SslMethod::Tlsv1).unwrap()).unwrap(); + s.connect_ssl("irc.rizon.net", 6697, ssl); + s.register("irsc", "irsc", "Testing for kori", None); + + let mut shared = s.into_shared(); + let _ = shared.commands() + .map(|(mut cl, msg, c)| { + if let PRIVMSG(to, content) = c { + let from = msg.ident().unwrap(); + let response = format!("{} told me: {}", from.nickname, color::bold(&content)); + + // only send to global channels, to prevent recursion when we are pm'ed + // technically, there are other prefixes than '#', but ignoring them is fine + if to.starts_with("#") && content.starts_with("irsc") { + cl.msg(&to, &response); + } + } + }); + + let _b = shared.replies() + .map(|(mut cl, _msg, r)| { + if let RPL_WELCOME(_) = r { + cl.join("#meep!", None); + } + }); + + // Dedicate this thread to listening and event processing + shared.listen_with_events(); +} -- cgit v1.2.3