2018-01-09 21:02:57 +01:00
|
|
|
extern crate irc;
|
|
|
|
|
|
|
|
use std::default::Default;
|
|
|
|
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("bananas".to_owned()),
|
|
|
|
server: Some("irc.fyrechat.net".to_owned()),
|
|
|
|
channels: Some(vec!["#irc-crate".to_owned()]),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let configs = vec![cfg1, cfg2];
|
|
|
|
|
|
|
|
let mut reactor = IrcReactor::new().unwrap();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let res = configs.iter().fold(Ok(()), |acc, config| {
|
|
|
|
acc.and(
|
2018-01-28 02:16:50 +01:00
|
|
|
reactor.prepare_client_and_connect(config).and_then(|client| {
|
|
|
|
client.identify().and(Ok(client))
|
|
|
|
}).and_then(|client| {
|
|
|
|
reactor.register_client_with_handler(client, process_msg);
|
2018-01-09 21:02:57 +01:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}).and_then(|()| reactor.run());
|
|
|
|
|
|
|
|
match res {
|
2018-01-28 03:19:05 +01:00
|
|
|
// The connections ended normally (for example, they sent a QUIT message to the server).
|
2018-01-09 21:02:57 +01:00
|
|
|
Ok(_) => break,
|
2018-01-24 12:28:07 +01:00
|
|
|
// Something went wrong! We'll print the error, and restart the connections.
|
2018-01-09 21:02:57 +01:00
|
|
|
Err(e) => eprintln!("{}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 02:16:50 +01:00
|
|
|
fn process_msg(client: &IrcClient, message: Message) -> error::Result<()> {
|
2018-01-09 21:02:57 +01:00
|
|
|
print!("{}", message);
|
2018-01-28 02:00:04 +01:00
|
|
|
if let Command::PRIVMSG(ref target, ref msg) = message.command {
|
|
|
|
if msg.contains("pickles") {
|
2018-01-28 02:16:50 +01:00
|
|
|
client.send_privmsg(target, "Hi!")?;
|
2018-01-28 02:00:04 +01:00
|
|
|
} else if msg.contains("quit") {
|
2018-01-28 02:16:50 +01:00
|
|
|
client.send_quit("bye")?;
|
2018-01-09 21:02:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|