Got rid of errors and exceptions, but the connection doesn't work yet.

This commit is contained in:
Thelie 2021-05-01 16:09:44 +02:00
parent c046e2b464
commit d8ee914654
2 changed files with 48 additions and 12 deletions

@ -1 +1 @@
Subproject commit cd15222685e3bbd80acaa279ceabc1514636b08c
Subproject commit 5d134f716dfe205c6ebef627f08d8330073cbf1d

View file

@ -29,7 +29,7 @@ use irc_proto::command::{
};
use irc_proto::message::Message;
use irc_stream::{IrcStream, IrcRead, IrcWrite};
use toml::Value;
use toml::value::Value;
use native_tls::{TlsConnector,TlsStream};
trait IrcReadWrite: IrcRead + IrcWrite {}
@ -37,32 +37,41 @@ impl<T: IrcRead + IrcWrite> IrcReadWrite for T {}
fn get_config<P: AsRef<Path>>(config_path: P)
-> Result<Value, Box<dyn std::error::Error>> {
// I once read that this multiple let style
// is the "rusty" way to do things.
// Personally, I find this specific instance to be somewhat sketchy.
let mut config = String::new();
std::fs::File::open(config_path)?
.read_to_string(&mut config);
.read_to_string(&mut config)?;
let mut config = config.parse::<Value>()?;
// The important thing here is that config is a mut Table in the end.
let mut config = config.as_table_mut().unwrap().clone();
if config.get("port") == None {
config["port"] = Value::from("6697");
config.insert(String::from("port"), Value::from("6697"));
}
if config.get("tls") == None {
if config["port"].as_integer() == Some(194) ||
config["port"].as_integer() == Some(6667) {
config["tls"] = Value::from(false);
config.insert(String::from("tls"), Value::from(false));
}else if config["port"].as_integer() == Some(6696) ||
config["port"].as_integer() == Some(6697) {
config["tls"] = Value::from(true);
config.insert(String::from("tls"), Value::from(true));
}
}
Ok(config)
Ok(Value::from(config))
}
fn get_tcp_stream (config: &Value) -> Result<TcpStream, Box<dyn std::error::Error>> {
let adress = format!("{}:{}",
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")?
);
Ok(TcpStream::connect("127.0.0.1:8080")?)
println!("Connectiing to: {}", address);
Ok(TcpStream::connect(address)?)
}
fn get_tls_stream (config: &Value)
@ -131,13 +140,40 @@ fn main() {
message_queue = get_irc_identify_messages(
message_queue,
None,
config["Nick"].to_string(),
config["User"].to_string(),
config["Real"].to_string()
config["nickname"].to_string(),
config["nickname"].to_string(),
config["nickname"].to_string()
);
while let Some(message) = message_queue.pop_front() {
stream.write(message).unwrap();
}
loop {
let message = match stream.read() {
Ok(m) => m,
Err(e) => Message::from(
Command::PRIVMSG(
String::from("Error"),
format!("{}", e)
)
)
};
// 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
),
_ => ()
}
}
}