Multi-server support in IRC bot

This commit is contained in:
Tiffany Bennett 2016-08-08 22:21:07 -04:00
parent e692e623a5
commit 4a142003af
2 changed files with 54 additions and 35 deletions

View file

@ -11,11 +11,13 @@ keywords = ["unit", "math", "conversion", "cli", "tool"]
[features] [features]
default = ["rustyline"] default = ["rustyline"]
ircbot = ["irc", "glob"]
[dependencies] [dependencies]
rust-gmp = "0.3.2" rust-gmp = "0.3.2"
rustyline = { version = "0.2.3", optional = true } rustyline = { version = "0.2.3", optional = true }
irc = { version = "0.11.3", optional = true } irc = { version = "0.11.3", optional = true }
glob = { version = "0.2.11", optional = true }
[[bin]] [[bin]]
name = "rink" name = "rink"

View file

@ -1,52 +1,69 @@
#[cfg(feature = "irc")] #[cfg(feature = "ircbot")]
extern crate irc; extern crate irc;
#[cfg(feature = "irc")] #[cfg(feature = "ircbot")]
extern crate glob;
#[cfg(feature = "ircbot")]
extern crate rink; extern crate rink;
#[cfg(feature = "irc")] #[cfg(feature = "ircbot")]
fn main() { fn main() {
use irc::client::prelude::*; use irc::client::prelude::*;
use rink::*; use rink::*;
use glob::glob;
use std::thread;
let mut ctx = load().unwrap(); fn run(config: &str) {
ctx.short_output = true; let mut ctx = load().unwrap();
let server = IrcServer::new("config.json").unwrap(); ctx.short_output = true;
server.identify().unwrap(); let server = IrcServer::new(config).unwrap();
let nick = server.config().nickname.clone().unwrap(); server.identify().unwrap();
let mut prefix = nick.clone(); let nick = server.config().nickname.clone().unwrap();
prefix.push(':'); let mut prefix = nick.clone();
for message in server.iter() { prefix.push(':');
if let Ok(Message { command: Command::PRIVMSG(ref chan, ref message_str), ..}) = message { for message in server.iter() {
if message_str.starts_with(&*prefix) { if let Ok(Message { command: Command::PRIVMSG(ref chan, ref message_str), ..}) = message {
let reply_to = if &*chan == &*nick { if message_str.starts_with(&*prefix) {
message.as_ref().unwrap().source_nickname().unwrap() let reply_to = if &*chan == &*nick {
} else { message.as_ref().unwrap().source_nickname().unwrap()
&*chan } else {
}; &*chan
let line = message_str[prefix.len()..].trim(); };
let reply = match one_line(&mut ctx, line) { let line = message_str[prefix.len()..].trim();
Ok(v) => v, let reply = match one_line(&mut ctx, line) {
Err(e) => e Ok(v) => v,
}; Err(e) => e
let mut i = 0; };
for line in reply.lines() { let mut i = 0;
if line.trim().len() > 0 { for line in reply.lines() {
server.send(Command::NOTICE(reply_to.to_owned(), line.to_owned())).unwrap(); if line.trim().len() > 0 {
i += 1; server.send(Command::NOTICE(reply_to.to_owned(), line.to_owned())).unwrap();
} i += 1;
// cut off early }
if i > 4 { // cut off early
break; if i > 4 {
break;
}
} }
} }
} else if let Err(e) = message {
println!("{}", e);
} }
} else if let Err(e) = message {
println!("{}", e);
} }
} }
let mut threads = vec![];
for config in glob("servers/*.json").expect("Glob failed") {
match config {
Ok(config) => threads.push(thread::spawn(move || run(config.to_str().unwrap()))),
Err(e) => println!("{:?}", e)
}
}
for thread in threads {
thread.join().unwrap()
}
} }
#[cfg(not(feature = "irc"))] #[cfg(not(feature = "ircbot"))]
fn main() { fn main() {
println!("Rink was not compiled with IRC support."); println!("Rink was not compiled with IRC support.");
} }