aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTill Hoeppner2015-04-18 21:42:44 +0200
committerTill Hoeppner2015-04-18 21:42:44 +0200
commit9bc7096cdb6dcda1229eedf5ac57e6853efa4952 (patch)
tree72d17c76da723f4a786777b1d45ea39bea9e5017 /src
parent5206c59657d3a8f197735b747abf0ae3685a0cea (diff)
downloadirsc-9bc7096cdb6dcda1229eedf5ac57e6853efa4952.tar.gz
irsc-9bc7096cdb6dcda1229eedf5ac57e6853efa4952.tar.xz
irsc-9bc7096cdb6dcda1229eedf5ac57e6853efa4952.zip
Cleaned up some parts.
The new code in send_raw is poetry, isn't it? ;) No? Well, I like it...
Diffstat (limited to 'src')
-rw-r--r--src/message.rs2
-rw-r--r--src/server.rs77
2 files changed, 35 insertions, 44 deletions
diff --git a/src/message.rs b/src/message.rs
index 1765176..e192a9d 100644
--- a/src/message.rs
+++ b/src/message.rs
@@ -2,7 +2,7 @@
use std::str::FromStr;
use std::string::{ ToString };
-use std::borrow::{ Cow, Borrow, ToOwned };
+use std::borrow::{ Cow, ToOwned };
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum MsgType {
diff --git a/src/server.rs b/src/server.rs
index 8f48938..62ee071 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -10,6 +10,8 @@ use std::net::TcpStream;
use message;
use message::{ Command, Message };
+use ::{ DEBUG, Result, IrscError };
+
#[cfg(feature = "ssl")]
use openssl::ssl::{ SslContext, SslMethod, SslStream };
@@ -66,24 +68,24 @@ impl Server {
}
}
- pub fn connect(&mut self, host: String, port: u16) -> ::Result<()> {
+ pub fn connect(&mut self, host: String, port: u16) -> Result<()> {
let s = &mut self.stream;
- match *s { Some(_) => return Err(::IrscError::AlreadyConnected), _ => () };
+ match *s { Some(_) => return Err(IrscError::AlreadyConnected), _ => () };
*s = match TcpStream::connect((host.as_ref(), port)) {
Ok(tcp) => Some(StreamKind::Plain(tcp)),
- Err(e) => return Err(::IrscError::Io(e))
+ Err(e) => return Err(IrscError::Io(e))
};
Ok(())
}
#[cfg(feature = "ssl")]
- pub fn connect_ssl(&mut self, host: String, port: u16) -> ::Result<()> {
+ pub fn connect_ssl(&mut self, host: String, port: u16) -> Result<()> {
let mut s = self.stream.lock();
- match *s { Some(_) => return Err(::IrscError::AlreadyConnected), _ => () };
+ match *s { Some(_) => return Err(IrscError::AlreadyConnected), _ => () };
let tcp_stream = match TcpStream::connect((host.as_ref(), port)) {
Ok(tcp) => Some(tcp),
- Err(e) => return Err(::IrscError::Io(e))
+ Err(e) => return Err(IrscError::Io(e))
};
let ssl = SslContext::new(SslMethod::Tlsv1);
@@ -94,64 +96,53 @@ impl Server {
}
#[inline]
- fn sendraw(&mut self, s: &str, newline: bool) -> ::Result<usize> {
+ fn sendraw(&mut self, s: &str) -> Result<()> {
info!(">> {}", s);
- if ::DEBUG && s.len() > 510 {
- panic!("Message too long, kitties will die if this runs in release mode. Msg: {}", s)
- }
- let stream = &mut self.stream;
- if stream.is_some() {
- stream.as_mut().map(|stream| {
- match stream.write(s.as_bytes()) {
- Ok(a) => match { if newline { stream.write(b"\r\n").map(|s| s + a) } else { Ok(a) } } {
- Ok(b) => match stream.flush() {
- Ok(_) => Ok(b),
- Err(e) => return Err(::IrscError::Io(e))
- },
- Err(e) => return Err(::IrscError::Io(e))
- },
- Err(e) => return Err(::IrscError::Io(e))
- }
- }).unwrap()
- } else {
- Err(::IrscError::NotConnected)
+ if DEBUG && s.len() > 512 {
+ panic!("Message too long, kittens will die if this runs in release mode. Msg: {}", s)
}
+
+ self.stream.as_mut()
+ .ok_or(IrscError::NotConnected)
+ .and_then(|mut stream| stream.write_all(s.as_bytes())
+ .and_then(|_| stream.flush())
+ .map_err(IrscError::Io))
}
- pub fn send(&mut self, msg: message::Message) -> ::Result<usize> {
- self.sendraw(&msg.to_string(), true)
+ pub fn send(&mut self, msg: message::Message) -> Result<()> {
+ self.sendraw(&msg.to_string())
}
- pub fn join(&mut self, channel: &str) -> ::Result<usize> {
- self.sendraw(format!("JOIN {}", channel).as_ref(), true)
+ pub fn join(&mut self, channel: &str) -> Result<()> {
+ self.sendraw(format!("JOIN {}", channel).as_ref())
}
- pub fn part(&mut self, channel: &str) -> ::Result<usize> {
- self.sendraw(format!("PART {}", channel).as_ref(), true)
+ pub fn part(&mut self, channel: &str) -> Result<()> {
+ self.sendraw(format!("PART {}", channel).as_ref())
}
- pub fn nick(&mut self, nick: &str) -> ::Result<usize> {
- self.sendraw(format!("NICK {}", nick).as_ref(), true)
+ pub fn nick(&mut self, nick: &str) -> Result<()> {
+ self.sendraw(format!("NICK {}", nick).as_ref())
}
- pub fn user(&mut self, username: &str, hostname: &str, servername: &str, realname: &str) -> ::Result<usize> {
- self.sendraw(format!("USER {} {} {} :{}", username, hostname, servername, realname).as_ref(), true)
+ pub fn user(&mut self, username: &str, hostname: &str, servername: &str, realname: &str) -> Result<()> {
+ self.sendraw(format!("USER {} {} {} :{}", username, hostname, servername, realname).as_ref())
}
- pub fn password(&mut self, password: &str) -> ::Result<usize> {
- self.sendraw(format!("PASS {}", password).as_ref(), true)
+ pub fn password(&mut self, password: &str) -> Result<()> {
+ self.sendraw(format!("PASS {}", password).as_ref())
}
- pub fn msg(&mut self, target: &str, message: &str) -> ::Result<usize> {
- self.sendraw(format!("PRIVMSG {} :{}", target, message).as_ref(), true)
+ pub fn msg(&mut self, target: &str, message: &str) -> Result<()> {
+ self.sendraw(format!("PRIVMSG {} :{}", target, message).as_ref())
}
- pub fn listen(&mut self, events: &[fn(&mut Server, &Message)]) -> ::Result<()> {
- let mut reader = BufReader::new(match self.stream {
+ pub fn listen(&mut self, events: &[fn(&mut Server, &Message)]) -> Result<()> {
+ let reader = BufReader::new(match self.stream {
Some(StreamKind::Plain(ref s)) => (*s).try_clone().unwrap(),
#[cfg(feature = "ssl")]
Some(StreamKind::Ssl(ref s)) => (*s).try_clone().unwrap(),
- None => return Err(::IrscError::NotConnected)
+ None => return Err(IrscError::NotConnected)
});
for line in reader.lines() {