irc-rs

A simple Rustlang IRC Lib optimized for bots
git clone git://archive.git.mtrnord.blog/MTRNord/irc-rs.git
Log | Files | Refs | README | LICENSE

commit a0f6255889790cb62dfe935392e2d1757d9bd4ea
parent d06d4828da912f6e01a52ee009c248680ef40c76
Author: Marcel <mtrnord1@gmail.com>
Date:   Fri, 20 Jan 2017 14:35:57 +0100

add docs and try to use mio

Diffstat:
MCargo.toml | 2+-
Msrc/lib.rs | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
2 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" authors = ["Marcel <mtrnord1@gmail.com>"] [dependencies] -bufstream = "0.1" +mio = "0.6.2" diff --git a/src/lib.rs b/src/lib.rs @@ -1,4 +1,23 @@ -extern crate bufstream; +//! A crate for the irc chat protocol +//! +//! +//! # Usage +//! +//! ```toml +//! [dependencies] +//! irc = "0.1" +//! ``` +//! +//! ```no_run +//! use irc::client::*; +//! +//! +//! let host: String = format!("130.83.198.4"); +//! let username: String = format!("nordbot"); +//! let stream = connect(host, 6667, &username, &username); +//! ``` + +extern crate mio; #[cfg(test)] mod tests { @@ -6,18 +25,37 @@ mod tests { #[test] fn Tconnect() { - use irc::connect; - let host: String = format!("130.83.198.4"); + use client::connect; + let host: String = format!("148.251.206.139"); let username: String = format!("nordbot"); let stream = connect(host, 6667, &username, &username); } } -mod irc { - use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream}; - use std::io::{Write, BufRead}; - use bufstream::BufStream; - pub fn connect(host: String, port: u16, username: &String, realname: &String) -> BufStream<TcpStream> { +pub mod client { + use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + use std::io::{Write, Read}; + use mio::tcp::TcpStream; + /// Returns a TcpStream which is connected to the defined IRC Server + /// + /// # Arguments + /// + /// * `host` - A String which contains a IPv4 to the IRC Serevr + /// * `port` - A u16 integer which contains the port of the IRC Server + /// * `username` - A String containing the username with that the lib should connect to the IRC Server + /// * `realname` - A String which contains the realname of the bot + /// + /// # Example + /// + /// ``` + /// use irc::client::*; + /// + /// let host: String = format!("130.83.198.4"); + /// let username: String = format!("nordbot"); + /// let stream = connect(host, 6667, &username, &username); + /// + /// ``` + pub fn connect(host: String, port: u16, username: &String, realname: &String) -> TcpStream { let host_split: Vec<&str> = host.split('.').collect(); let ip1_1: i32 = host_split[0].parse().unwrap(); let ip1_2: u8 = ip1_1 as _; @@ -30,37 +68,41 @@ mod irc { let ServerAddress = IpAddr::V4(Ipv4Addr::new(ip1_2, ip2_2, ip3_2, ip4_2)); // Connect to localhost let SocketAddress = SocketAddr::new(ServerAddress, port); let mut isConnected = false; - let mut stream = BufStream::new(TcpStream::connect(SocketAddress).unwrap()); + let mut stream = TcpStream::connect(&SocketAddress).unwrap(); let nick = format!("NICK {}\r\n", username); let user = format!("USER {} 0 * : {}\r\n", username, realname); + println!("{:?}", stream); + stream.write(nick.as_bytes()); stream.write(user.as_bytes()); - stream.flush(); + //stream.flush(); let mut g = 1; while g > 0 { let mut r = String::new(); - - while stream.read_line(&mut r).unwrap() > 0 { - if r.is_empty(){ + stream.read_to_string(&mut r); + let server_message_raw = r.clone(); + while server_message_raw.len() > 0 { + println!("raw: {:?}", r); + //println!("len: {:?}", r.len()); + if server_message_raw.is_empty(){ g = 0; }else{ - let server_message_raw = r.clone(); - println!("{}", r.clone()); if server_message_raw.contains("004") { isConnected = true; - println!("connected"); + println!("connected: {}", isConnected); } else if server_message_raw.contains("PING") { let mut server_message_vec: Vec<&str> = server_message_raw.split("\n").collect(); server_message_vec.pop(); let server_message_ping_index = server_message_vec.iter().position(|&r| r.contains("PING")).unwrap(); let server_message: Vec<&str> = server_message_vec[server_message_ping_index].split(" ").collect(); - let pong = format!("PONG :{}\r\n", server_message_vec[server_message_ping_index].trim_left()); + let pong = format!("PONG {}\r\n", server_message[1].trim_left()); stream.write(pong.as_bytes()); - println!("pong"); - stream.flush(); + //stream.flush(); + println!("{}", pong); } + g = 1; } } }