2018-05-27 23:15:05 +02:00
|
|
|
extern crate irc;
|
|
|
|
extern crate tokio_timer;
|
|
|
|
|
|
|
|
use std::default::Default;
|
|
|
|
use std::time::Duration;
|
|
|
|
use irc::client::prelude::*;
|
|
|
|
use irc::error::IrcError;
|
|
|
|
|
|
|
|
// NOTE: this example is a conversion of `tweeter.rs` to an asynchronous style with `IrcReactor`.
|
|
|
|
fn main() {
|
|
|
|
let config = Config {
|
|
|
|
nickname: Some("mastodon".to_owned()),
|
2018-07-10 04:15:08 +02:00
|
|
|
server: Some("irc.mozilla.org".to_owned()),
|
|
|
|
channels: Some(vec!["#rust-spam".to_owned()]),
|
2018-05-27 23:15:05 +02:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
// We need to create a reactor first and foremost
|
|
|
|
let mut reactor = IrcReactor::new().unwrap();
|
|
|
|
// and then create a client via its API.
|
|
|
|
let client = reactor.prepare_client_and_connect(&config).unwrap();
|
|
|
|
// Then, we identify
|
|
|
|
client.identify().unwrap();
|
|
|
|
// and clone just as before.
|
|
|
|
let send_client = client.clone();
|
|
|
|
|
|
|
|
// Rather than spawn a thread that reads the messages separately, we register a handler with the
|
2018-05-27 23:24:37 +02:00
|
|
|
// reactor. just as in the original version, we don't do any real handling and instead just
|
|
|
|
// print the messages that are received.
|
2018-05-27 23:15:05 +02:00
|
|
|
reactor.register_client_with_handler(client, |_, message| {
|
|
|
|
print!("{}", message);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2018-05-27 23:24:37 +02:00
|
|
|
// We construct an interval using a wheel timer from tokio_timer. This interval will fire every
|
|
|
|
// ten seconds (and is roughly accurate to the second).
|
2018-05-27 23:15:05 +02:00
|
|
|
let send_interval = tokio_timer::wheel()
|
|
|
|
.tick_duration(Duration::from_secs(1))
|
|
|
|
.num_slots(256)
|
|
|
|
.build()
|
|
|
|
.interval(Duration::from_secs(10));
|
|
|
|
|
|
|
|
// And then spawn a new future that performs the given action each time it fires.
|
|
|
|
reactor.register_future(send_interval.map_err(IrcError::Timer).for_each(move |()| {
|
|
|
|
// Anything in here will happen every 10 seconds!
|
2018-07-10 04:15:08 +02:00
|
|
|
send_client.send_privmsg("#rust-spam", "AWOOOOOOOOOO")
|
2018-05-27 23:15:05 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Then, on the main thread, we finally run the reactor which blocks the program indefinitely.
|
|
|
|
reactor.run().unwrap();
|
|
|
|
}
|