From 2e0d8e55d8bffdefa9cb58e67f28c40cae53af5d Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Sat, 29 Nov 2014 05:26:55 -0500 Subject: [PATCH] Added an example showing how to send messages that are not responses to a user despite the fact that send and recv both block. --- examples/tweeter.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/tweeter.rs diff --git a/examples/tweeter.rs b/examples/tweeter.rs new file mode 100644 index 0000000..0f1a345 --- /dev/null +++ b/examples/tweeter.rs @@ -0,0 +1,47 @@ +#![feature(if_let, slicing_syntax)] +extern crate irc; + +use std::collections::HashMap; +use std::io::timer::sleep; +use std::sync::Arc; +use std::time::duration::Duration; +use irc::data::config::Config; +use irc::server::{IrcServer, Server}; +use irc::server::utils::Wrapper; + +fn main() { + let config = Config { + owners: vec!("awe".into_string()), + nickname: "tweeter".into_string(), + username: "tweeter".into_string(), + realname: "tweeter".into_string(), + password: "".into_string(), + server: "irc.fyrechat.net".into_string(), + port: 6667, + use_ssl: false, + channels: vec!("#vana".into_string()), + options: HashMap::new(), + }; + let irc_server = Arc::new(IrcServer::from_config_with_timeout(config, 10 * 1000).unwrap()); + let irc_server2 = irc_server.clone(); + // The wrapper provides us with methods like send_privmsg(...) and identify(...) + let server = Wrapper::new(&*irc_server); + server.identify().unwrap(); + // Let's set up a loop that ignores timeouts, and reads perpetually. + // n.b. this shouldn't exit automatically if the connection closes. + spawn(proc() { + let mut iter = irc_server2.iter(); + loop { + if let Some(msg) = iter.next() { + print!("{}", msg.into_string()); + } + } + }); + // Even though sending and reading both block, this will still be sent every ten seconds + // thanks to the timeout we have set for the server. + loop { + server.send_privmsg("#vana", "TWEET TWEET").unwrap(); + sleep(Duration::seconds(10)) + } +} +