lib.rs (3935B)
1 //! A crate for the irc chat protocol 2 //! 3 //! 4 //! # Usage 5 //! 6 //! ```toml 7 //! [dependencies] 8 //! irc = "0.1" 9 //! ``` 10 //! 11 //! ```no_run 12 //! use irc::client::*; 13 //! 14 //! 15 //! let host: String = format!("130.83.198.4"); 16 //! let username: String = format!("nordbot"); 17 //! let stream = connect(host, 6667, &username, &username); 18 //! ``` 19 20 extern crate mio; 21 22 #[cfg(test)] 23 mod tests { 24 #[test] 25 fn Tconnect() { 26 use client::connect; 27 let host: String = format!("148.251.206.139"); 28 let username: String = format!("nordbot"); 29 let stream = connect(host, 6667, &username, &username); 30 } 31 } 32 33 pub mod client { 34 use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 35 use std::io::{Write, Read}; 36 use mio::tcp::TcpStream; 37 /// Returns a TcpStream which is connected to the defined IRC Server 38 /// 39 /// # Arguments 40 /// 41 /// * `host` - A String which contains a IPv4 to the IRC Serevr 42 /// * `port` - A u16 integer which contains the port of the IRC Server 43 /// * `username` - A String containing the username with that the lib should connect to the IRC Server 44 /// * `realname` - A String which contains the realname of the bot 45 /// 46 /// # Example 47 /// 48 /// ``` 49 /// use irc::client::*; 50 /// 51 /// let host: String = format!("130.83.198.4"); 52 /// let username: String = format!("nordbot"); 53 /// let stream = connect(host, 6667, &username, &username); 54 /// 55 /// ``` 56 pub fn connect(host: String, port: u16, username: &String, realname: &String) -> TcpStream { 57 let host_split: Vec<&str> = host.split('.').collect(); 58 let ip1_1: i32 = host_split[0].parse().unwrap(); 59 let ip1_2: u8 = ip1_1 as _; 60 let ip2_1: i32 = host_split[1].parse().unwrap(); 61 let ip2_2: u8 = ip2_1 as _; 62 let ip3_1: i32 = host_split[2].parse().unwrap(); 63 let ip3_2: u8 = ip3_1 as _; 64 let ip4_1: i32 = host_split[3].parse().unwrap(); 65 let ip4_2: u8 = ip4_1 as _; 66 let ServerAddress = IpAddr::V4(Ipv4Addr::new(ip1_2, ip2_2, ip3_2, ip4_2)); // Connect to localhost 67 let SocketAddress = SocketAddr::new(ServerAddress, port); 68 let mut isConnected = false; 69 let mut stream = TcpStream::connect(&SocketAddress).unwrap(); 70 let nick = format!("NICK {}\r\n", username); 71 let user = format!("USER {} 0 * : {}\r\n", username, realname); 72 73 println!("{:?}", stream); 74 75 stream.write(nick.as_bytes()); 76 stream.write(user.as_bytes()); 77 //stream.flush(); 78 79 let mut g = 1; 80 while g > 0 { 81 let mut r = String::new(); 82 stream.read_to_string(&mut r); 83 let server_message_raw = r.clone(); 84 while server_message_raw.len() > 0 { 85 println!("raw: {:?}", r); 86 //println!("len: {:?}", r.len()); 87 if server_message_raw.is_empty(){ 88 g = 0; 89 }else{ 90 if server_message_raw.contains("004") { 91 isConnected = true; 92 println!("connected: {}", isConnected); 93 } else if server_message_raw.contains("PING") { 94 let mut server_message_vec: Vec<&str> = server_message_raw.split("\n").collect(); 95 server_message_vec.pop(); 96 let server_message_ping_index = server_message_vec.iter().position(|&r| r.contains("PING")).unwrap(); 97 let server_message: Vec<&str> = server_message_vec[server_message_ping_index].split(" ").collect(); 98 let pong = format!("PONG {}\r\n", server_message[1].trim_left()); 99 stream.write(pong.as_bytes()); 100 //stream.flush(); 101 println!("{}", pong); 102 } 103 g = 1; 104 } 105 } 106 } 107 return stream; 108 } 109 }