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