Skip to content

Instantly share code, notes, and snippets.

@emberian
Last active December 31, 2015 20:39
Show Gist options
  • Save emberian/8041902 to your computer and use it in GitHub Desktop.
Save emberian/8041902 to your computer and use it in GitHub Desktop.
use std::fmt;
use std::io::net::tcp::TcpStream;
use std::io::net::ip::{Ipv4Addr, SocketAddr};
use std::io::buffered::BufferedStream;
#[deriving(Clone)]
pub struct RustBot {
nick: ~str,
user: ~str,
stream: BufferedStream<TcpStream>,
verbose: bool
}
impl RustBot {
fn new(nick: ~str, user: ~str, stream: BufferedStream<TcpStream>, verbose: bool) -> RustBot {
RustBot { nick: nick, user: user, stream: stream, verbose: verbose }
}
fn listen(&mut self) -> ! {
println("Listening.");
write!(stream, "NICK {}\r\n", self.nick);
write!(stream, "USER {} 8 * : {} \r\n", self.nick, self.user);
stream.flush();
loop {
let msg = stream.read_line();
for value in msg.iter() {
print(*value);
let input: ~[&str] = value.split(':').collect();
if input[0].contains("PING") {
let response: &str = format!("PONG {}\r\n", input[1]);
print(response);
stream.write(response.as_bytes());
stream.flush();
}
}
}
}
}
impl fmt::Default for RustBot {
fn fmt(obj: &RustBot, f: &mut fmt::Formatter) {
write!(f.buf, "nick: {}, name: {}", obj.nick, obj.user);
}
}
fn main() {
// let ip = Ipv4Addr(127, 0, 0, 1);
let ip = Ipv4Addr(63, 245, 216, 214);
let port = 6667;
let addr = SocketAddr { ip: ip, port: port };
let socket = TcpStream::new(addr);
let mut bot = RustBot::new(~"rustbot", ~"rustbot", BufferedStream::new(socket.connect()), true);
bot.listen();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment