Add function to identify with servers. Does not work yet.

This commit is contained in:
Thelie 2021-04-28 00:35:03 +02:00
parent fdbdcf3ede
commit 2c865d43d2

View file

@ -22,7 +22,11 @@ use std::{
io::{prelude::*,Read}, io::{prelude::*,Read},
net::TcpStream, net::TcpStream,
}; };
use irc_proto; use irc_proto::command::{
CapSubCommand,
Command,
};
use irc_proto::message::Message;
use irc_stream::{IrcStream, IrcRead, IrcWrite}; use irc_stream::{IrcStream, IrcRead, IrcWrite};
use toml::Value; use toml::Value;
use native_tls::{TlsConnector,TlsStream}; use native_tls::{TlsConnector,TlsStream};
@ -60,7 +64,7 @@ fn get_config<P: AsRef<Path>>(config_path: P)
Ok(config) 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!("{}:{}", let adress = format!("{}:{}",
config["server"].as_str().ok_or("Could not get server adress from config")?, 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["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")?) 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>> { -> Result<TlsStream<TcpStream>, Box<dyn std::error::Error>> {
let connector = TlsConnector::new().unwrap(); let connector = TlsConnector::new().unwrap();
let stream = connect_irc(config)?; let stream = get_tcp_stream(config)?;
Ok(connector.connect(config["server"] Ok(connector.connect(config["server"]
.as_str() .as_str()
.ok_or("Could not get server adress from config")?, .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() { fn main() {
let config_path = args().nth(1) let config_path = args().nth(1)
.expect("no config given"); .expect("no config given");
@ -92,16 +129,17 @@ fn main() {
let mut stream: &dyn IrcReadWrite; let mut stream: &dyn IrcReadWrite;
if config["tls"].as_bool().unwrap() == true { if config["tls"].as_bool().unwrap() == true {
stream = &IrcStream::new(connect_irc_tls(&config).unwrap()); stream = &IrcStream::new(get_tls_stream(&config).unwrap());
} else { } else {
stream = &IrcStream::new(connect_irc(&config).unwrap()); stream = &IrcStream::new(get_tcp_stream(&config).unwrap());
} }
//let irc_stream = IrcStream::new(stream); identify_irc(
&mut stream,
// TODO: Identify with irc server None,
//stream.write(irc_proto.Message{ config["Nick"].to_string(),
// tags: None, config["User"].to_string(),
// prefix: config["Real"].to_string()
);
} }