summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-06 17:26:33 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-06 17:26:33 -0400
commitdf1b563b1f14443d685f3b035f749090b764d63d (patch)
tree0bd382b38f52e287a4ffbe9704118853e3789a35
parent52997d0cd46d093a5be1129e22cba4e86ec98525 (diff)
downloadrust-irc-df1b563b1f14443d685f3b035f749090b764d63d.tar.gz
rust-irc-df1b563b1f14443d685f3b035f749090b764d63d.zip
better default for hostname
-rw-r--r--src/client.rs37
1 files changed, 29 insertions, 8 deletions
diff --git a/src/client.rs b/src/client.rs
index 4c063f6..2824df2 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -17,6 +17,7 @@ pub struct ClientBuilder {
pub struct Client {
conn: io::BufferedStream<io::TcpStream>,
+ socket_name: Option<String>,
}
impl ClientBuilder {
@@ -62,19 +63,24 @@ impl ClientBuilder {
pub fn connect (self) -> Client {
let nick = self.nick.clone();
let pass = self.pass.clone();
- let hostname = match self.hostname {
- Some(ref host) => host.clone(),
- None => {
- // XXX get the name of the local end of the connection
- "localhost".to_string()
- },
- };
+ let hostname = self.hostname.clone();
let username = self.username.clone();
let servername = self.servername.clone();
let realname = self.realname.clone();
let mut client = self.connect_raw();
+ let hostname = match hostname {
+ Some(host) => host,
+ None => {
+ match client.socket_name() {
+ Some(host) => host.to_string(),
+ // XXX something better here?
+ None => "localhost".to_string(),
+ }
+ },
+ };
+
match pass {
Some(pass) => {
client.write(Message::new(None, Pass, vec![pass]));
@@ -94,7 +100,15 @@ impl ClientBuilder {
pub fn connect_raw (self) -> Client {
let mut stream = io::TcpStream::connect(self.servername.as_slice(), self.port);
- Client { conn: io::BufferedStream::new(stream.unwrap()) }
+ let mut stream = stream.unwrap();
+ let socket_name = match stream.socket_name() {
+ Ok(addr) => Some(addr.ip.to_string()),
+ Err(_) => None,
+ };
+ Client {
+ conn: io::BufferedStream::new(stream),
+ socket_name: socket_name,
+ }
}
}
@@ -112,4 +126,11 @@ impl Client {
pub fn write (&mut self, msg: Message) {
msg.write_protocol_string(&mut self.conn);
}
+
+ pub fn socket_name (&self) -> Option<&str> {
+ match self.socket_name {
+ Some(ref name) => Some(name.as_slice()),
+ None => None,
+ }
+ }
}