rust-irc/examples/autoreconnect.rs

44 lines
1.5 KiB
Rust
Raw Normal View History

2014-12-21 02:13:16 +01:00
extern crate irc;
use std::default::Default;
use std::sync::Arc;
use std::thread::spawn;
2015-02-23 03:22:33 +01:00
use irc::client::prelude::*;
2014-12-21 02:13:16 +01:00
fn main() {
let config = Config {
nickname: Some(format!("pickles")),
server: Some(format!("irc.fyrechat.net")),
channels: Some(vec![format!("#vana")]),
.. Default::default()
};
let irc_server = Arc::new(IrcServer::from_config(config).unwrap());
irc_server.conn().set_keepalive(Some(5)).unwrap();
// The wrapper provides us with methods like send_privmsg(...) and identify(...)
let _ = spawn(move || {
2014-12-21 02:13:16 +01:00
let server = Wrapper::new(&*irc_server);
server.identify().unwrap();
loop {
let mut quit = false;
for msg in server.iter() {
match msg {
Ok(msg) => {
print!("{}", msg.into_string());
match Command::from_message(&msg) {
Ok(Command::PRIVMSG(_, msg)) => if msg.contains("bye") {
server.send_quit("").unwrap()
},
Ok(Command::ERROR(ref msg)) if msg.contains("Quit") => quit = true,
2014-12-21 02:13:16 +01:00
_ => (),
}
},
Err(_) => break,
}
}
if quit { break }
irc_server.reconnect().unwrap();
server.identify().unwrap();
}
}).join();
2014-12-21 02:13:16 +01:00
}