diff --git a/src/main.rs b/src/main.rs index 761ca37..8e4837e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>(config_path: P) Ok(config) } -fn connect_irc (config: &Value) -> Result> { +fn get_tcp_stream (config: &Value) -> Result> { 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> Ok(TcpStream::connect("127.0.0.1:8080")?) } -fn connect_irc_tls (config: &Value) +fn get_tls_stream (config: &Value) -> Result, Box> { 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 ( + stream: &mut IrcStream, + pass: Option, + 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() + ); }