diff --git a/irc-stream b/irc-stream index 5d134f7..bf53e62 160000 --- a/irc-stream +++ b/irc-stream @@ -1 +1 @@ -Subproject commit 5d134f716dfe205c6ebef627f08d8330073cbf1d +Subproject commit bf53e62f1713dfe83198f75992cc0e889de398ff diff --git a/src/main.rs b/src/main.rs index 7346e8a..a913402 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,10 +65,13 @@ fn get_config>(config_path: P) Ok(Value::from(config)) } -fn get_tcp_stream (config: &Value) -> Result> { +fn get_tcp_stream (config: &Value) + -> Result> { let address = 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")? + config.get("server").unwrap().as_str() + .ok_or("Could not get server adress from config")?, + config.get("port").unwrap().as_str() + .ok_or("Could not get port from config")? ); println!("Connectiing to: {}", address); Ok(TcpStream::connect(address)?) @@ -85,14 +88,10 @@ fn get_tls_stream (config: &Value) ) } -fn get_irc_identify_messages ( - queue: VecDeque, - pass: Option, - nick: String, - user: String, - real_name: String -) -> VecDeque{ - let mut queue = queue; +fn get_irc_identify_messages (config: &Value) + -> Result, Box> { + let mut queue = VecDeque::new(); + queue.push_back(Message::from( Command::CAP( None, @@ -100,22 +99,40 @@ fn get_irc_identify_messages ( None, None ))); - match pass { + + match config.get("password") { Some(p) => queue.push_back(Message::from( - Command::PASS(p) + Command::PASS( + String::from(p.as_str().ok_or("Could not parse password.")?) + ) )), None => () } - queue.push_back(Message::from( - Command::NICK(nick) - )); - queue.push_back(Message::from( - Command::USER( - user, - "0".to_owned(), - real_name) - )); - queue + + let nick: String; + match config.get("nickname") { + Some(n) => { + nick = String::from(n.as_str().ok_or("Could not parse nickname.")?); + queue.push_back(Message::from(Command::NICK(nick.clone()))); + }, + None => return Err("No nickname supplied!".into()), + } + + match config.get("username") { + Some(u) => queue.push_back(Message::from( + Command::USER( + String::from(u.as_str().ok_or("Could not parse username.")?), + "0".to_owned(), + String::from(u.as_str().ok_or("Could not parse username.")?) + ) + )), + None => queue.push_back(Message::from( + // nick.clone() is only used once because the value + // can be moved the second time + Command::USER(nick.clone(), "0".to_owned(), nick) + )) + } + Ok(queue) } fn main() { @@ -134,21 +151,15 @@ fn main() { } else { stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap())); } - - let mut message_queue: VecDeque = VecDeque::new(); - message_queue = get_irc_identify_messages( - message_queue, - None, - config["nickname"].to_string(), - config["nickname"].to_string(), - config["nickname"].to_string() - ); + let mut message_queue = get_irc_identify_messages(&config).unwrap(); while let Some(message) = message_queue.pop_front() { stream.write(message).unwrap(); } + stream.flush().unwrap(); + loop { let message = match stream.read() { Ok(m) => m, @@ -159,20 +170,23 @@ fn main() { ) ) }; -// let sender = match message.source_nickname() { -// Some(s) => s.clone(), -// None => "anonymous" -// }; - match message.command { - Command::PING(_s1, _s2) => stream.write(Message::from( - Command::PONG(String::from("PONG"), None) - )).unwrap(), - Command::PRIVMSG(rec, msg) => println!( - "someone -> {}: {}", - &rec, - &msg + let sender = match message.clone().source_nickname() { + Some(s) => String::from(s), + None => String::from("anonymous") + }; + match message.clone().command { + Command::PING(ref data, _) => { + stream.write(Message::from( + Command::PONG(data.to_owned(), None) + )).unwrap(); + }, + Command::PRIVMSG(ref rec, ref msg) => println!( + "{} -> {}: {}", + rec, + msg, + sender ), - _ => () + _ => println!("Other message: {}", message.clone().to_string()) } } }