Made encoding into a feature as encode.
This commit is contained in:
parent
7b22fada51
commit
b4cda39134
9 changed files with 306 additions and 60 deletions
|
@ -12,11 +12,13 @@ readme = "README.md"
|
|||
|
||||
[features]
|
||||
|
||||
encode = ["encoding"]
|
||||
ssl = ["openssl"]
|
||||
|
||||
[dependencies.encoding]
|
||||
|
||||
encoding = "~0.2.4"
|
||||
optional = true
|
||||
|
||||
[dependencies.openssl]
|
||||
|
||||
|
|
|
@ -9,19 +9,7 @@ use irc::server::{IrcServer, Server};
|
|||
use irc::server::utils::Wrapper;
|
||||
|
||||
fn main() {
|
||||
let config = Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
};
|
||||
let config = config();
|
||||
let irc_server = Arc::new(IrcServer::from_config(config).unwrap());
|
||||
// The wrapper provides us with methods like send_privmsg(...) and identify(...)
|
||||
let server = Wrapper::new(&*irc_server);
|
||||
|
@ -35,3 +23,35 @@ fn main() {
|
|||
});
|
||||
}
|
||||
|
||||
#[cfg(feature = "encode")]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,19 +8,7 @@ use irc::server::{IrcServer, Server};
|
|||
use irc::server::utils::Wrapper;
|
||||
|
||||
fn main() {
|
||||
let config = Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
};
|
||||
let config = config();
|
||||
let irc_server = IrcServer::from_config(config).unwrap();
|
||||
// The wrapper provides us with methods like send_privmsg(...) and identify(...)
|
||||
let server = Wrapper::new(&irc_server);
|
||||
|
@ -36,3 +24,36 @@ fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "encode")]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,19 +8,7 @@ use irc::server::{IrcServer, Server};
|
|||
use irc::server::utils::Wrapper;
|
||||
|
||||
fn main() {
|
||||
let config = Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6697,
|
||||
use_ssl: true,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
};
|
||||
let config = config();
|
||||
let irc_server = IrcServer::from_config(config).unwrap();
|
||||
// The wrapper provides us with methods like send_privmsg(...) and identify(...)
|
||||
let server = Wrapper::new(&irc_server);
|
||||
|
@ -36,3 +24,36 @@ fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "encode")]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,19 +10,7 @@ use irc::server::{IrcServer, Server};
|
|||
use irc::server::utils::Wrapper;
|
||||
|
||||
fn main() {
|
||||
let config = Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "tweeter".into_string(),
|
||||
username: "tweeter".into_string(),
|
||||
realname: "tweeter".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
};
|
||||
let config = config();
|
||||
let irc_server = Arc::new(IrcServer::from_config_with_timeout(config, 10 * 1000).unwrap());
|
||||
let irc_server2 = irc_server.clone();
|
||||
// The wrapper provides us with methods like send_privmsg(...) and identify(...)
|
||||
|
@ -46,3 +34,35 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "encode")]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
encoding: format!("UTF-8"),
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn config() -> Config {
|
||||
Config {
|
||||
owners: vec!("awe".into_string()),
|
||||
nickname: "pickles".into_string(),
|
||||
username: "pickles".into_string(),
|
||||
realname: "pickles".into_string(),
|
||||
password: "".into_string(),
|
||||
server: "irc.fyrechat.net".into_string(),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec!("#vana".into_string()),
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
|
53
src/conn.rs
53
src/conn.rs
|
@ -1,9 +1,10 @@
|
|||
//! Thread-safe connections on IrcStreams.
|
||||
#![experimental]
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use std::io::{BufferedStream, IoError, IoErrorKind, IoResult, MemWriter, TcpStream};
|
||||
use encoding::{DecoderTrap, EncoderTrap, Encoding};
|
||||
use encoding::label::encoding_from_whatwg_label;
|
||||
use std::io::{BufferedStream, IoResult, MemWriter, TcpStream};
|
||||
#[cfg(any(feature = "encode", feature = "ssl"))] use std::io::{IoError, IoErrorKind};
|
||||
#[cfg(feature = "encode")] use encoding::{DecoderTrap, EncoderTrap, Encoding};
|
||||
#[cfg(feature = "encode")] use encoding::label::encoding_from_whatwg_label;
|
||||
use data::kinds::{IrcReader, IrcStream, IrcWriter};
|
||||
use data::message::Message;
|
||||
#[cfg(feature = "ssl")] use openssl::ssl::{SslContext, SslStream, Tlsv1};
|
||||
|
@ -86,6 +87,7 @@ impl<T: IrcStream> Connection<T> {
|
|||
|
||||
/// Sends a Message over this connection.
|
||||
#[experimental]
|
||||
#[cfg(feature = "encode")]
|
||||
pub fn send(&self, message: Message, encoding: &str) -> IoResult<()> {
|
||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||
Some(enc) => enc,
|
||||
|
@ -108,8 +110,18 @@ impl<T: IrcStream> Connection<T> {
|
|||
stream.flush()
|
||||
}
|
||||
|
||||
/// Sends a message over this connection.
|
||||
#[experimental]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
pub fn send(&self, message: Message) -> IoResult<()> {
|
||||
let mut stream = self.stream.lock();
|
||||
try!(stream.write_str(message.into_string()[]));
|
||||
stream.flush()
|
||||
}
|
||||
|
||||
/// Receives a single line from this connection.
|
||||
#[experimental]
|
||||
#[cfg(feature = "encoding")]
|
||||
pub fn recv(&self, encoding: &str) -> IoResult<String> {
|
||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||
Some(enc) => enc,
|
||||
|
@ -131,6 +143,13 @@ impl<T: IrcStream> Connection<T> {
|
|||
)
|
||||
}
|
||||
|
||||
/// Receives a single line from this connection.
|
||||
#[experimental]
|
||||
#[cfg(not(feature = "encoding"))]
|
||||
pub fn recv(&self) -> IoResult<String> {
|
||||
self.stream.lock().read_line()
|
||||
}
|
||||
|
||||
/// Acquires the Stream lock.
|
||||
#[experimental]
|
||||
pub fn stream<'a>(&'a self) -> MutexGuard<'a, T> {
|
||||
|
@ -231,10 +250,22 @@ mod test {
|
|||
use std::io::{MemReader, MemWriter};
|
||||
use std::io::util::{NullReader, NullWriter};
|
||||
use data::message::Message;
|
||||
use encoding::{DecoderTrap, Encoding};
|
||||
use encoding::all::ISO_8859_15;
|
||||
#[cfg(feature = "encode")] use encoding::{DecoderTrap, Encoding};
|
||||
#[cfg(feature = "encode")] use encoding::all::ISO_8859_15;
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn send() {
|
||||
let conn = Connection::new(IoStream::new(MemWriter::new(), NullReader));
|
||||
assert!(conn.send(
|
||||
Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!"))
|
||||
).is_ok());
|
||||
let data = String::from_utf8(conn.stream().value()).unwrap();
|
||||
assert_eq!(data[], "PRIVMSG test :Testing!\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn send_utf8() {
|
||||
let conn = Connection::new(IoStream::new(MemWriter::new(), NullReader));
|
||||
assert!(conn.send(
|
||||
|
@ -245,11 +276,22 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn send_iso885915() {
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn recv() {
|
||||
let conn = Connection::new(IoStream::new(
|
||||
NullWriter, MemReader::new("PRIVMSG test :Testing!\r\n".as_bytes().to_vec())
|
||||
));
|
||||
assert_eq!(conn.recv().unwrap()[], "PRIVMSG test :Testing!\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn recv_utf8() {
|
||||
let conn = Connection::new(IoStream::new(
|
||||
NullWriter, MemReader::new(b"PRIVMSG test :Testing!\r\n".to_vec())
|
||||
|
@ -258,6 +300,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn recv_iso885915() {
|
||||
let conn = Connection::new(IoStream::new(
|
||||
NullWriter, MemReader::new({
|
||||
|
|
|
@ -28,6 +28,7 @@ pub struct Config {
|
|||
pub use_ssl: bool,
|
||||
/// The encoding type used for this connection.
|
||||
/// This is typically UTF-8, but could be something else.
|
||||
#[cfg(feature = "encoding")]
|
||||
pub encoding: String,
|
||||
/// A list of channels to join on connection.
|
||||
pub channels: Vec<String>,
|
||||
|
@ -74,6 +75,7 @@ mod test {
|
|||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn load() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test")],
|
||||
|
@ -92,6 +94,25 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn load() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test")],
|
||||
nickname: format!("test"),
|
||||
username: format!("test"),
|
||||
realname: format!("test"),
|
||||
password: String::new(),
|
||||
server: format!("irc.test.net"),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec![format!("#test"), format!("#test2")],
|
||||
options: HashMap::new(),
|
||||
};
|
||||
assert_eq!(Config::load(Path::new("config.json")), Ok(cfg));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn load_utf8() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test")],
|
||||
|
@ -110,6 +131,26 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn load_utf8() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test")],
|
||||
nickname: format!("test"),
|
||||
username: format!("test"),
|
||||
realname: format!("test"),
|
||||
password: String::new(),
|
||||
server: format!("irc.test.net"),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec![format!("#test"), format!("#test2")],
|
||||
options: HashMap::new(),
|
||||
};
|
||||
assert_eq!(Config::load_utf8("config.json"), Ok(cfg));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn is_owner() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test"), format!("test2")],
|
||||
|
@ -130,6 +171,27 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn is_owner() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test"), format!("test2")],
|
||||
nickname: format!("test"),
|
||||
username: format!("test"),
|
||||
realname: format!("test"),
|
||||
password: String::new(),
|
||||
server: format!("irc.test.net"),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec![format!("#test"), format!("#test2")],
|
||||
options: HashMap::new(),
|
||||
};
|
||||
assert!(cfg.is_owner("test"));
|
||||
assert!(cfg.is_owner("test2"));
|
||||
assert!(!cfg.is_owner("test3"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "encode")]
|
||||
fn get_option() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test")],
|
||||
|
@ -150,4 +212,26 @@ mod test {
|
|||
};
|
||||
assert_eq!(cfg.get_option("testing"), "test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn get_option() {
|
||||
let cfg = Config {
|
||||
owners: vec![format!("test")],
|
||||
nickname: format!("test"),
|
||||
username: format!("test"),
|
||||
realname: format!("test"),
|
||||
password: String::new(),
|
||||
server: format!("irc.test.net"),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec![format!("#test"), format!("#test2")],
|
||||
options: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(format!("testing"), format!("test"));
|
||||
map
|
||||
},
|
||||
};
|
||||
assert_eq!(cfg.get_option("testing"), "test");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#![unstable]
|
||||
|
||||
#![feature(if_let, slicing_syntax)]
|
||||
extern crate encoding;
|
||||
#[cfg(feature = "encode")] extern crate encoding;
|
||||
extern crate serialize;
|
||||
#[cfg(feature = "ssl")] extern crate openssl;
|
||||
|
||||
|
|
|
@ -83,10 +83,16 @@ impl<'a, T> Server<'a, T> for IrcServer<T> where T: IrcStream {
|
|||
&self.config
|
||||
}
|
||||
|
||||
#[cfg(feature = "encode")]
|
||||
fn send(&self, command: Command) -> IoResult<()> {
|
||||
self.conn.send(command.to_message(), self.config.encoding[])
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn send(&self, command: Command) -> IoResult<()> {
|
||||
self.conn.send(command.to_message())
|
||||
}
|
||||
|
||||
fn iter(&'a self) -> ServerIterator<'a, T> {
|
||||
ServerIterator::new(self)
|
||||
}
|
||||
|
@ -173,12 +179,23 @@ impl<'a, T> ServerIterator<'a, T> where T: IrcStream {
|
|||
server: server
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the next line from the connection.
|
||||
#[cfg(feature = "encode")]
|
||||
fn get_next_line(&self) -> IoResult<String> {
|
||||
self.server.conn.recv(self.server.config.encoding[])
|
||||
}
|
||||
|
||||
/// Gets the next line from the connection.
|
||||
#[cfg(not(feature = "encode"))]
|
||||
fn get_next_line(&self) -> IoResult<String> {
|
||||
self.server.conn.recv()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator<Message> for ServerIterator<'a, T> where T: IrcStream {
|
||||
fn next(&mut self) -> Option<Message> {
|
||||
let line = self.server.conn.recv(self.server.config.encoding[]);
|
||||
match line {
|
||||
match self.get_next_line() {
|
||||
Err(_) => None,
|
||||
Ok(msg) => {
|
||||
let message = from_str(msg[]);
|
||||
|
@ -200,6 +217,7 @@ mod test {
|
|||
use data::command::Command::PRIVMSG;
|
||||
use data::kinds::IrcReader;
|
||||
|
||||
#[cfg(feature = "encode")]
|
||||
pub fn test_config() -> Config {
|
||||
Config {
|
||||
owners: vec![format!("test")],
|
||||
|
@ -216,6 +234,23 @@ mod test {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encode"))]
|
||||
pub fn test_config() -> Config {
|
||||
Config {
|
||||
owners: vec![format!("test")],
|
||||
nickname: format!("test"),
|
||||
username: format!("test"),
|
||||
realname: format!("test"),
|
||||
password: String::new(),
|
||||
server: format!("irc.test.net"),
|
||||
port: 6667,
|
||||
use_ssl: false,
|
||||
channels: vec![format!("#test"), format!("#test2")],
|
||||
options: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn get_server_value<U>(server: IrcServer<IoStream<MemWriter, U>>) -> String
|
||||
where U: IrcReader {
|
||||
String::from_utf8(server.conn().stream().value()).unwrap()
|
||||
|
|
Loading…
Reference in a new issue