Compare commits

...

2 commits

2 changed files with 52 additions and 14 deletions

@ -1 +1 @@
Subproject commit fea845a5335691b8e75b3981f93ae59335043eb1
Subproject commit cd15222685e3bbd80acaa279ceabc1514636b08c

View file

@ -22,7 +22,11 @@ use std::{
io::{prelude::*,Read},
net::TcpStream,
};
use irc_proto;
use irc_proto::command::{
CapSubCommand,
Command,
};
use irc_proto::message::Message;
use irc_stream::{IrcStream, IrcRead, IrcWrite};
use toml::Value;
use native_tls::{TlsConnector,TlsStream};
@ -60,7 +64,7 @@ fn get_config<P: AsRef<Path>>(config_path: P)
Ok(config)
}
fn connect_irc (config: &Value) -> Result<TcpStream, Box<dyn std::error::Error>> {
fn get_tcp_stream (config: &Value) -> Result<TcpStream, Box<dyn std::error::Error>> {
let adress = format!("{}:{}",
config["server"].as_str().ok_or("Could not get server adress from config")?,
config["port"].as_str().ok_or("Could not get port from config")?
@ -68,10 +72,10 @@ fn connect_irc (config: &Value) -> Result<TcpStream, Box<dyn std::error::Error>>
Ok(TcpStream::connect("127.0.0.1:8080")?)
}
fn connect_irc_tls (config: &Value)
fn get_tls_stream (config: &Value)
-> Result<TlsStream<TcpStream>, Box<dyn std::error::Error>> {
let connector = TlsConnector::new().unwrap();
let stream = connect_irc(config)?;
let stream = get_tcp_stream(config)?;
Ok(connector.connect(config["server"]
.as_str()
.ok_or("Could not get server adress from config")?,
@ -79,6 +83,39 @@ fn connect_irc_tls (config: &Value)
)
}
fn identify_irc<S: std::io::Read + std::io::Write> (
stream: &mut IrcStream<S>,
pass: Option<String>,
nick: String,
user: String,
real_name: String
) -> Result<(), irc_stream::IrcStreamError>{
stream.write(Message::from(
Command::CAP(
None,
CapSubCommand::END,
None,
None
)
))?;
match pass {
Some(p) => stream.write(Message::from(
Command::PASS(p)
))?,
None => ()
}
stream.write(Message::from(
Command::NICK(nick)
))?;
stream.write(Message::from(
Command::USER(
user,
"0".to_owned(),
real_name)
))?;
Ok(())
}
fn main() {
let config_path = args().nth(1)
.expect("no config given");
@ -92,16 +129,17 @@ fn main() {
let mut stream: &dyn IrcReadWrite;
if config["tls"].as_bool().unwrap() == true {
stream = &IrcStream::new(connect_irc_tls(&config).unwrap());
stream = &IrcStream::new(get_tls_stream(&config).unwrap());
} else {
stream = &IrcStream::new(connect_irc(&config).unwrap());
stream = &IrcStream::new(get_tcp_stream(&config).unwrap());
}
//let irc_stream = IrcStream::new(stream);
// TODO: Identify with irc server
//stream.write(irc_proto.Message{
// tags: None,
// prefix:
identify_irc(
&mut stream,
None,
config["Nick"].to_string(),
config["User"].to_string(),
config["Real"].to_string()
);
}