commit e5e9e633cd22c041665027c1c365687a68349a8c
parent 4cd4ab8487a3814dafe53ca5889f2c0b8adff660
Author: Marcel <mtrnord1@gmail.com>
Date: Mon, 16 Jan 2017 14:35:48 +0100
(fix|feature) fix ip to u8 conversation|add test(WIP)
Diffstat:
| M | src/lib.rs | | | 41 | ++++++++++++++++++++++++++++++++++------- |
1 file changed, 34 insertions(+), 7 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
@@ -1,17 +1,43 @@
extern crate bufstream;
-mod irc {
- fn connect(host: String, port: u16, username: String, realname: String) {
- use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
- use std::io::{Write, BufRead};
- use bufstream::BufStream;
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn Tconnect() {
+ use irc::connect;
+ let host: String = format!("130.83.198.4");
+ let username: String = format!("testbot");
+ 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> {
let host_split: Vec<&str> = host.split('.').collect();
- let ServerAddress = IpAddr::V4(Ipv4Addr::new(host_split[0].as_bytes()[0], host_split[1].as_bytes()[0], host_split[2].as_bytes()[0], host_split[3].as_bytes()[0])); // Connect to localhost
+ let ip1_1: i32 = host_split[0].parse().unwrap();
+ let ip1_2: u8 = ip1_1 as _;
+ let ip2_1: i32 = host_split[1].parse().unwrap();
+ let ip2_2: u8 = ip2_1 as _;
+ let ip3_1: i32 = host_split[2].parse().unwrap();
+ let ip3_2: u8 = ip3_1 as _;
+ let ip4_1: i32 = host_split[3].parse().unwrap();
+ let ip4_2: u8 = ip4_1 as _;
+ let ServerAddress = IpAddr::V4(Ipv4Addr::new(ip1_2, ip2_2, ip3_2, ip4_2)); // Connect to localhost
let SocketAddress = SocketAddr::new(ServerAddress, port);
+
+ println!("split: {}", host_split[0]);
+ println!("int bytes: {}", ip1_2);
+ println!("bytes: {}", host_split[0].as_bytes()[1]);
+ println!("bytes: {}", SocketAddress);
let mut isConnected = false;
let mut stream = BufStream::new(TcpStream::connect(SocketAddress).unwrap());
let nick = format!("NICK {}\r\n", username);
- let user = format!("USER {} 8 * : {}\r\n", username, realname);
+ let user = format!("USER {} 0 * : {}\r\n", username, realname);
stream.write(nick.as_bytes());
stream.write(user.as_bytes());
@@ -34,5 +60,6 @@ mod irc {
}
}
}
+ return stream;
}
}