Added a multiserver example.

This commit is contained in:
Aaron Weiss 2017-06-25 05:24:10 -04:00
parent 196d6425bd
commit c93d817735
No known key found for this signature in database
GPG key ID: 0237035D9BF03AE2

50
examples/multiserver.rs Normal file
View file

@ -0,0 +1,50 @@
extern crate futures;
extern crate irc;
use std::default::Default;
use futures::stream::MergedItem;
use irc::error;
use irc::client::prelude::*;
fn main() {
let cfg1 = Config {
nickname: Some("pickles".to_owned()),
server: Some("irc.fyrechat.net".to_owned()),
channels: Some(vec!["#irc-crate".to_owned()]),
..Default::default()
};
let cfg2 = Config {
nickname: Some("pickles".to_owned()),
server: Some("irc.pdgn.co".to_owned()),
channels: Some(vec!["#irc-crate".to_owned()]),
use_ssl: Some(true),
..Default::default()
};
let server1 = IrcServer::from_config(cfg1).unwrap();
let server2 = IrcServer::from_config(cfg2).unwrap();
server1.identify().unwrap();
server2.identify().unwrap();
server1.stream().merge(server2.stream()).for_each(|pair| match pair {
MergedItem::First(message) => process_msg(&server1, message),
MergedItem::Second(message) => process_msg(&server2, message),
MergedItem::Both(msg1, msg2) => {
process_msg(&server1, msg1).unwrap();
process_msg(&server2, msg2)
}
}).wait().unwrap()
}
fn process_msg(server: &IrcServer, message: Message) -> error::Result<()> {
print!("{}", message);
match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains("pickles") {
server.send_privmsg(target, "Hi!")?;
}
}
_ => (),
}
Ok(())
}