Refactored basic library significantly, but sending isn't working properly.
This commit is contained in:
parent
49055e39d3
commit
9b6919d0bc
1 changed files with 66 additions and 29 deletions
95
src/lib.rs
95
src/lib.rs
|
@ -4,27 +4,71 @@ extern crate regex;
|
||||||
|
|
||||||
use std::io::{BufferedReader, BufferedWriter, InvalidInput, IoError, IoResult, TcpStream};
|
use std::io::{BufferedReader, BufferedWriter, InvalidInput, IoError, IoResult, TcpStream};
|
||||||
|
|
||||||
|
pub struct Connection(TcpStream);
|
||||||
|
|
||||||
|
pub fn connect(host: &str, port: u16) -> IoResult<Connection> {
|
||||||
|
let socket = try!(TcpStream::connect(host, port));
|
||||||
|
Ok(Connection(socket))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_internal(conn: &Connection, msg: &str) -> IoResult<()> {
|
||||||
|
match conn {
|
||||||
|
&Connection(ref tcp) => {
|
||||||
|
let mut writer = BufferedWriter::new(tcp.clone());
|
||||||
|
writer.write_str(msg);
|
||||||
|
writer.flush()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Message<'a> {
|
||||||
|
source: Option<&'a str>,
|
||||||
|
command: &'a str,
|
||||||
|
args: &'a [&'a str],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Message<'a> {
|
||||||
|
pub fn new(source: Option<&'a str>, command: &'a str, args: &'a [&'a str]) -> Message<'a> {
|
||||||
|
Message {
|
||||||
|
source: source,
|
||||||
|
command: command,
|
||||||
|
args: args,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {
|
||||||
|
let arg_string = msg.args.init().connect(" ").append(" :").append(*msg.args.last().unwrap());
|
||||||
|
send_internal(conn, msg.command.to_string().append(" ").append(arg_string.as_slice()).as_slice())
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Bot {
|
pub struct Bot {
|
||||||
pub sock: TcpStream
|
pub conn: Connection,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bot {
|
impl Bot {
|
||||||
pub fn new() -> Bot {
|
pub fn new() -> IoResult<Bot> {
|
||||||
let sock = TcpStream::connect("irc.fyrechat.net", 6667).unwrap();
|
let conn = try!(connect("irc.fyrechat.net", 6667));
|
||||||
Bot {
|
Ok(Bot {
|
||||||
sock: sock,
|
conn: conn,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn identify(&mut self) {
|
pub fn send_nick(&mut self, nick: &str) -> IoResult<()> {
|
||||||
let mut writer = BufferedWriter::new(self.sock.clone());
|
send(&self.conn, Message::new(None, "NICK", [nick]))
|
||||||
writer.write_str("NICK :pickles\r\n").unwrap();
|
}
|
||||||
writer.write_str("USER pickles 0 * :pickles\r\n").unwrap();
|
|
||||||
writer.flush().unwrap();
|
pub fn send_user(&mut self, username: &str, real_name: &str) -> IoResult<()> {
|
||||||
|
send(&self.conn, Message::new(None, "USER", [username, "0", "*", real_name]))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn identify(&mut self) -> IoResult<()> {
|
||||||
|
self.send_nick("pickles");
|
||||||
|
self.send_user("pickles", "pickles")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn output(&mut self) {
|
pub fn output(&mut self) {
|
||||||
let mut reader = BufferedReader::new(self.sock.clone());
|
let mut reader = { let Connection(ref tcp) = self.conn; BufferedReader::new(tcp.clone()) };
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
match line {
|
match line {
|
||||||
Ok(ln) => {
|
Ok(ln) => {
|
||||||
|
@ -37,31 +81,24 @@ impl Bot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_command(&mut self, source: &str, command: &str, args: &[&str]) -> () {
|
fn handle_command(&mut self, source: &str, command: &str, args: &[&str]) -> IoResult<()> {
|
||||||
match (command, args) {
|
match (command, args) {
|
||||||
("PING", [msg]) => {
|
("PING", [msg]) => {
|
||||||
self.send("PONG", msg);
|
try!(send(&self.conn, Message::new(None, "PONG", [msg])));
|
||||||
},
|
},
|
||||||
("376", _) => {
|
("376", _) => {
|
||||||
self.send("JOIN", "#vana");
|
try!(send(&self.conn, Message::new(None, "JOIN", ["#vana"])));
|
||||||
},
|
},
|
||||||
("PRIVMSG", [channel, msg]) => {
|
("PRIVMSG", [_, msg]) => {
|
||||||
if msg.contains("pickles") && msg.contains("hi") {
|
if msg.contains("pickles") && msg.contains("hi") {
|
||||||
self.send("PRIVMSG #vana", "hi");
|
try!(send(&self.conn, Message::new(None, "PRIVMSG", ["#vana", "hi"])));
|
||||||
}
|
} else if msg.starts_with(". ") {
|
||||||
|
try!(send(&self.conn, Message::new(None, "PRIVMSG", ["#vana", msg.slice_from(2)])));
|
||||||
if msg.starts_with(". ") {
|
};
|
||||||
self.send("PRIVMSG #vana", msg.slice_from(2));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
};
|
||||||
}
|
Ok(())
|
||||||
|
|
||||||
fn send(&mut self, command: &str, arg: &str) {
|
|
||||||
let mut writer = BufferedWriter::new(self.sock.clone());
|
|
||||||
write!(writer, "{} :{}\r\n", command, arg);
|
|
||||||
writer.flush().unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue