Correct implementation for irc registration.

This commit is contained in:
Thelie 2021-04-29 00:30:58 +02:00
parent 91d8ea079b
commit c046e2b464

View file

@ -21,6 +21,7 @@ use std::{
path::Path, path::Path,
io::Read, io::Read,
net::TcpStream, net::TcpStream,
collections::VecDeque,
}; };
use irc_proto::command::{ use irc_proto::command::{
CapSubCommand, CapSubCommand,
@ -31,6 +32,9 @@ use irc_stream::{IrcStream, IrcRead, IrcWrite};
use toml::Value; use toml::Value;
use native_tls::{TlsConnector,TlsStream}; use native_tls::{TlsConnector,TlsStream};
trait IrcReadWrite: IrcRead + IrcWrite {}
impl<T: IrcRead + IrcWrite> IrcReadWrite for T {}
fn get_config<P: AsRef<Path>>(config_path: P) fn get_config<P: AsRef<Path>>(config_path: P)
-> Result<Value, Box<dyn std::error::Error>> { -> Result<Value, Box<dyn std::error::Error>> {
let mut config = String::new(); let mut config = String::new();
@ -72,37 +76,37 @@ fn get_tls_stream (config: &Value)
) )
} }
fn identify_irc<S: std::io::Read + std::io::Write> ( fn get_irc_identify_messages (
stream: &mut IrcStream<S>, queue: VecDeque<Message>,
pass: Option<String>, pass: Option<String>,
nick: String, nick: String,
user: String, user: String,
real_name: String real_name: String
) -> Result<(), irc_stream::IrcStreamError>{ ) -> VecDeque<Message>{
stream.write(Message::from( let mut queue = queue;
queue.push_back(Message::from(
Command::CAP( Command::CAP(
None, None,
CapSubCommand::END, CapSubCommand::END,
None, None,
None None
) )));
))?;
match pass { match pass {
Some(p) => stream.write(Message::from( Some(p) => queue.push_back(Message::from(
Command::PASS(p) Command::PASS(p)
))?, )),
None => () None => ()
} }
stream.write(Message::from( queue.push_back(Message::from(
Command::NICK(nick) Command::NICK(nick)
))?; ));
stream.write(Message::from( queue.push_back(Message::from(
Command::USER( Command::USER(
user, user,
"0".to_owned(), "0".to_owned(),
real_name) real_name)
))?; ));
Ok(()) queue
} }
fn main() { fn main() {
@ -113,23 +117,27 @@ fn main() {
// Weird workaround for multitrait dyn pointer // Weird workaround for multitrait dyn pointer
// https://github.com/rust-lang/rfcs/issues/2035 // https://github.com/rust-lang/rfcs/issues/2035
// would be a charm... // would be a charm...
trait IrcReadWrite: IrcRead + IrcWrite {}
impl<T: IrcRead + IrcWrite> IrcReadWrite for T {} let mut stream: Box<dyn IrcReadWrite>;
let mut stream: &dyn IrcReadWrite;
if config["tls"].as_bool().unwrap() == true { if config["tls"].as_bool().unwrap() == true {
stream = &IrcStream::new(get_tls_stream(&config).unwrap()); stream = Box::new(IrcStream::new(get_tls_stream(&config).unwrap()));
} else { } else {
stream = &IrcStream::new(get_tcp_stream(&config).unwrap()); stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap()));
} }
identify_irc( let mut message_queue: VecDeque<Message> = VecDeque::new();
&mut stream,
message_queue = get_irc_identify_messages(
message_queue,
None, None,
config["Nick"].to_string(), config["Nick"].to_string(),
config["User"].to_string(), config["User"].to_string(),
config["Real"].to_string() config["Real"].to_string()
); );
while let Some(message) = message_queue.pop_front() {
stream.write(message).unwrap();
}
} }