rust-irc/examples/reactor.rs
Christoph Herzog 2aff64b645 Refactor IrcClientFuture and ConnectionFuture to own the config
This commit refactors IrcClientFuture and ConnectionFuture to own
the config instead of holding a reference.

This is required for reconnecting in dynamic contexts.
This is not possible with the old API, because Config is a reference,
requiring the value to live for the whole execution of the reactor.
2018-10-04 02:52:53 +02:00

31 lines
961 B
Rust

extern crate irc;
use std::default::Default;
use irc::client::prelude::*;
// This example is meant to be a direct analogue to simple.rs using the reactor API.
fn main() {
let config = Config {
nickname: Some("pickles".to_owned()),
alt_nicks: Some(vec!["bananas".to_owned(), "apples".to_owned()]),
server: Some("irc.mozilla.org".to_owned()),
channels: Some(vec!["#rust-spam".to_owned()]),
..Default::default()
};
let mut reactor = IrcReactor::new().unwrap();
let client = reactor.prepare_client_and_connect(config).unwrap();
client.identify().unwrap();
reactor.register_client_with_handler(client, |client, message| {
print!("{}", message);
if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.contains("pickles") {
client.send_privmsg(target, "Hi!")?;
}
}
Ok(())
});
reactor.run().unwrap();
}