2014-09-10 19:47:12 -04:00
|
|
|
#![feature(phase)]
|
|
|
|
extern crate regex;
|
|
|
|
#[phase(plugin)] extern crate regex_macros;
|
2014-09-23 20:11:13 -04:00
|
|
|
extern crate serialize;
|
2014-09-10 19:47:12 -04:00
|
|
|
|
2014-09-24 16:53:42 -04:00
|
|
|
use std::cell::RefCell;
|
2014-09-24 19:20:59 -04:00
|
|
|
use std::collections::HashMap;
|
2014-10-08 12:57:36 -04:00
|
|
|
use std::io::{BufferedReader, BufferedWriter, InvalidInput, IoError, IoResult, TcpStream};
|
2014-09-24 19:20:59 -04:00
|
|
|
use std::vec::Vec;
|
2014-10-08 12:57:36 -04:00
|
|
|
use conn::{Conn, Connection};
|
2014-10-08 13:10:55 -04:00
|
|
|
use data::{Config, IrcReader, IrcWriter, Message};
|
2014-09-11 01:09:28 -04:00
|
|
|
|
2014-09-23 20:11:13 -04:00
|
|
|
pub mod conn;
|
|
|
|
pub mod data;
|
2014-09-11 01:09:28 -04:00
|
|
|
|
2014-10-08 13:32:37 -04:00
|
|
|
pub trait Bot<'a> {
|
|
|
|
fn send_nick(&self, nick: &str) -> IoResult<()>;
|
|
|
|
fn send_user(&self, username: &str, real_name: &str) -> IoResult<()>;
|
|
|
|
fn send_join(&self, chan: &str) -> IoResult<()>;
|
|
|
|
fn send_mode(&self, chan: &str, mode: &str) -> IoResult<()>;
|
|
|
|
fn send_topic(&self, chan: &str, topic: &str) -> IoResult<()>;
|
|
|
|
fn send_invite(&self, person: &str, chan: &str) -> IoResult<()>;
|
|
|
|
fn send_privmsg(&self, chan: &str, msg: &str) -> IoResult<()>;
|
|
|
|
fn identify(&self) -> IoResult<()>;
|
|
|
|
fn output(&mut self) -> IoResult<()>;
|
2014-10-08 13:40:19 -04:00
|
|
|
fn config(&self) -> Config;
|
2014-10-08 13:32:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
2014-10-08 12:57:36 -04:00
|
|
|
pub conn: RefCell<Connection<T, U>>,
|
2014-09-23 20:11:13 -04:00
|
|
|
pub config: Config,
|
2014-10-08 13:32:37 -04:00
|
|
|
process: RefCell<|&IrcBot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>>,
|
2014-09-24 19:20:59 -04:00
|
|
|
pub chanlists: HashMap<String, Vec<String>>,
|
2014-09-10 19:47:12 -04:00
|
|
|
}
|
|
|
|
|
2014-10-08 13:44:36 -04:00
|
|
|
impl<'a> IrcBot<'a, BufferedWriter<TcpStream>, TcpStream> {
|
2014-10-08 13:32:37 -04:00
|
|
|
pub fn new(process: |&IrcBot<BufferedWriter<TcpStream>, TcpStream>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, BufferedWriter<TcpStream>, TcpStream>> {
|
2014-09-23 20:11:13 -04:00
|
|
|
let config = try!(Config::load());
|
2014-10-08 12:57:36 -04:00
|
|
|
let conn = try!(Connection::connect(config.server.as_slice(), config.port));
|
2014-10-08 13:32:37 -04:00
|
|
|
Ok(IrcBot {
|
2014-10-08 12:57:36 -04:00
|
|
|
conn: RefCell::new(conn),
|
2014-09-23 20:11:13 -04:00
|
|
|
config: config,
|
2014-09-24 16:53:42 -04:00
|
|
|
process: RefCell::new(process),
|
2014-09-24 19:20:59 -04:00
|
|
|
chanlists: HashMap::new(),
|
2014-09-11 01:09:28 -04:00
|
|
|
})
|
2014-09-10 19:47:12 -04:00
|
|
|
}
|
2014-10-08 13:44:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, U> Bot<'a> for IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
|
|
|
fn send_nick(&self, nick: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "NICK", [nick]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_user(&self, username: &str, real_name: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "USER", [username, "0", "*", real_name]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_join(&self, chan: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "JOIN", [chan.as_slice()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_mode(&self, chan: &str, mode: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "MODE", [chan.as_slice(), mode.as_slice()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_topic(&self, chan: &str, topic: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "TOPIC", [chan.as_slice(), topic.as_slice()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_invite(&self, person: &str, chan: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "INVITE", [person.as_slice(), chan.as_slice()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_privmsg(&self, chan: &str, msg: &str) -> IoResult<()> {
|
|
|
|
Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "PRIVMSG", [chan.as_slice(), msg.as_slice()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn identify(&self) -> IoResult<()> {
|
|
|
|
try!(self.send_nick(self.config.nickname.as_slice()));
|
|
|
|
self.send_user(self.config.username.as_slice(), self.config.realname.as_slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output(&mut self) -> IoResult<()> {
|
|
|
|
let mut reader = match self.conn.borrow_mut().deref_mut() {
|
|
|
|
&Conn(_, ref recv) => BufferedReader::new(recv.clone()),
|
|
|
|
};
|
|
|
|
for line in reader.lines() {
|
|
|
|
match line {
|
|
|
|
Ok(ln) => {
|
|
|
|
let (source, command, args) = try!(process(ln.as_slice()));
|
|
|
|
try!(self.handle_command(source, command, args.as_slice()));
|
|
|
|
println!("{}", ln)
|
|
|
|
},
|
|
|
|
Err(e) => println!("Shit, you're fucked! {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config(&self) -> Config {
|
|
|
|
self.config.clone()
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 19:47:12 -04:00
|
|
|
|
2014-10-08 13:44:36 -04:00
|
|
|
impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
2014-09-11 01:09:28 -04:00
|
|
|
fn handle_command(&mut self, source: &str, command: &str, args: &[&str]) -> IoResult<()> {
|
2014-09-10 19:47:12 -04:00
|
|
|
match (command, args) {
|
|
|
|
("PING", [msg]) => {
|
2014-10-08 12:57:36 -04:00
|
|
|
try!(Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "PONG", [msg])));
|
2014-09-10 19:47:12 -04:00
|
|
|
},
|
2014-09-11 01:31:33 -04:00
|
|
|
("376", _) => { // End of MOTD
|
2014-09-23 20:11:13 -04:00
|
|
|
for chan in self.config.channels.iter() {
|
|
|
|
try!(self.send_join(chan.as_slice()));
|
|
|
|
}
|
2014-09-10 19:47:12 -04:00
|
|
|
},
|
2014-09-11 01:31:33 -04:00
|
|
|
("422", _) => { // Missing MOTD
|
2014-09-23 20:11:13 -04:00
|
|
|
for chan in self.config.channels.iter() {
|
|
|
|
try!(self.send_join(chan.as_slice()));
|
|
|
|
}
|
2014-09-10 19:47:12 -04:00
|
|
|
},
|
2014-09-24 19:20:59 -04:00
|
|
|
("353", [_, _, chan, users]) => { // /NAMES
|
|
|
|
for user in users.split_str(" ") {
|
|
|
|
if !match self.chanlists.find_mut(&String::from_str(chan)) {
|
|
|
|
Some(vec) => {
|
|
|
|
vec.push(String::from_str(user));
|
|
|
|
true
|
|
|
|
},
|
|
|
|
None => false,
|
|
|
|
} {
|
|
|
|
self.chanlists.insert(String::from_str(chan), vec!(String::from_str(user)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
("JOIN", [chan]) => {
|
|
|
|
match self.chanlists.find_mut(&String::from_str(chan)) {
|
|
|
|
Some(vec) => {
|
|
|
|
match source.find('!') {
|
|
|
|
Some(i) => vec.push(String::from_str(source.slice_to(i))),
|
|
|
|
None => (),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
("PART", [chan, _]) => {
|
|
|
|
match self.chanlists.find_mut(&String::from_str(chan)) {
|
|
|
|
Some(vec) => {
|
|
|
|
match source.find('!') {
|
|
|
|
Some(i) => {
|
|
|
|
match vec.as_slice().position_elem(&String::from_str(source.slice_to(i))) {
|
|
|
|
Some(n) => {
|
|
|
|
vec.swap_remove(n);
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
},
|
2014-09-24 16:53:42 -04:00
|
|
|
_ => {
|
2014-10-06 16:33:37 -04:00
|
|
|
try!((*self.process.borrow_mut().deref_mut())(self, source, command, args));
|
2014-09-24 16:53:42 -04:00
|
|
|
},
|
2014-09-11 01:09:28 -04:00
|
|
|
};
|
|
|
|
Ok(())
|
2014-09-10 19:47:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process(msg: &str) -> IoResult<(&str, &str, Vec<&str>)> {
|
|
|
|
let reg = regex!(r"^(?::([^ ]+) )?([^ ]+)(.*)");
|
|
|
|
let cap = match reg.captures(msg) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Err(IoError {
|
|
|
|
kind: InvalidInput,
|
|
|
|
desc: "Failed to parse line",
|
|
|
|
detail: None,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
let source = cap.at(1);
|
|
|
|
let command = cap.at(2);
|
|
|
|
let args = parse_args(cap.at(3));
|
|
|
|
Ok((source, command, args))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_args(line: &str) -> Vec<&str> {
|
2014-09-24 19:20:59 -04:00
|
|
|
let reg = regex!(r" ([^: ]+)| :([^\r\n]*)[\r\n]*$");
|
2014-09-10 19:47:12 -04:00
|
|
|
reg.captures_iter(line).map(|cap| {
|
|
|
|
match cap.at(1) {
|
|
|
|
"" => cap.at(2),
|
|
|
|
x => x,
|
|
|
|
}
|
|
|
|
}).collect()
|
2014-09-10 16:23:09 -04:00
|
|
|
}
|
2014-09-25 15:58:50 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn process_line_test() {
|
|
|
|
let res = process(":flare.to.ca.fyrechat.net 353 pickles = #pickles :pickles awe\r\n").unwrap();
|
|
|
|
let (source, command, args) = res;
|
|
|
|
assert_eq!(source, "flare.to.ca.fyrechat.net");
|
|
|
|
assert_eq!(command, "353");
|
|
|
|
assert_eq!(args, vec!["pickles", "=", "#pickles", "pickles awe"]);
|
|
|
|
|
|
|
|
let res = process("PING :flare.to.ca.fyrechat.net\r\n").unwrap();
|
|
|
|
let (source, command, args) = res;
|
|
|
|
assert_eq!(source, "");
|
|
|
|
assert_eq!(command, "PING");
|
|
|
|
assert_eq!(args, vec!["flare.to.ca.fyrechat.net"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn process_args_test() {
|
|
|
|
let res = parse_args("PRIVMSG #vana :hi");
|
|
|
|
assert_eq!(res, vec!["#vana", "hi"])
|
|
|
|
}
|