'auto' reconnect

This commit is contained in:
2025-03-12 11:59:24 +01:00
parent 736a8c98c7
commit a4035bdea9

View File

@@ -118,8 +118,7 @@ impl FromStr for Message {
} }
} }
#[tokio::main] async fn real_main() -> Result<()> {
async fn main() -> Result<()> {
dotenvy::dotenv().ok(); dotenvy::dotenv().ok();
let irc_server = std::env::var("IRC_SERVER")?; let irc_server = std::env::var("IRC_SERVER")?;
@@ -131,6 +130,7 @@ async fn main() -> Result<()> {
let mut buffer = String::new(); let mut buffer = String::new();
let stdin = io::stdin(); // We get `Stdin` here. let stdin = io::stdin(); // We get `Stdin` here.
let (term_tx, term_rx) = tokio::sync::broadcast::channel::<()>(1);
let (tx, mut rx) = tokio::sync::mpsc::channel(12); let (tx, mut rx) = tokio::sync::mpsc::channel(12);
let config = Config { let config = Config {
@@ -145,25 +145,16 @@ async fn main() -> Result<()> {
client.identify()?; client.identify()?;
let mut stream = client.stream()?; let mut stream = client.stream()?;
let mut term_rx_a = term_rx.resubscribe();
tokio::spawn(async move { tokio::spawn(async move {
while let Some(msg) = rx.recv().await { loop {
client.send(msg)?; tokio::select! {
} Some(msg) = rx.recv() => {
Ok::<(), Error>(()) client.send(msg)?;
}); },
_ = term_rx_a.recv() => {
let tx_ = tx.clone(); eprintln!("Closing irc send loop");
tokio::spawn(async move { break;
while let Some(message) = stream.next().await.transpose()? {
print!("{}", &message);
if let Command::NOTICE(a, b) = message.command {
if a == "p2000" && b.starts_with("This nickname is registered.") {
tx_.send(Command::NICKSERV(vec![
"identify".into(),
irc_password.clone(),
]))
.await?;
} }
} }
} }
@@ -171,43 +162,67 @@ async fn main() -> Result<()> {
Ok::<(), Error>(()) Ok::<(), Error>(())
}); });
// thread::spawn(move || { let tx_ = tx.clone();
// // irc thread let mut term_rx_a = term_rx.resubscribe();
// let config = rustls::ClientConfig::builder() tokio::spawn(async move {
// .with_root_certificates(rustls::RootCertStore { loop {
// roots: webpki_roots::TLS_SERVER_ROOTS.into(), tokio::select! {
// }) message = stream.next() => {
// .with_no_client_auth(); let Ok(Some(message)) = message.transpose() else {
// let mut conn = rustls::ClientConnection::new(Arc::new(config), "irc.avii.nl".try_into()?)?; return Err("".into());
// let mut socket = std::net::TcpStream::connect("irc.avii.nl:6697")?; };
// let mut tls = rustls::Stream::new(&mut conn, &mut socket); print!("{}", message);
// tls.write_all(b"CAP LS 302\r\n")?; if let Command::NOTICE(a, b) = message.command {
// tls.write_all(b"NICK p2000\r\n")?; if a == "p2000" && b.starts_with("This nickname is registered.") {
// tls.write_all(b"USER p2000 0 * :p2000\r\n")?; tx_.send(Command::NICKSERV(vec![
// tls.write_all(b"CAP END\r\n")?; "identify".into(),
irc_password.clone(),
]))
.await?;
}
}
},
_ = term_rx_a.recv() => {
eprintln!("Closing irc receiver loop");
break;
}
}
}
// tls.write_all(b"JOIN #p2000\r\n")?; Ok::<(), Error>(())
});
// while let Ok(msg) = rx.recv() { 'outer: while stdin.read_line(&mut buffer)? > 0 {
// dbg!(&msg);
// tls.write_all(format!("PRIVMSG #p2000 :{}\r\n", msg).as_bytes())?;
// }
// Ok::<(), Error>(())
// });
while stdin.read_line(&mut buffer)? > 0 {
if let Ok(msg) = Message::from_str(&buffer) { if let Ok(msg) = Message::from_str(&buffer) {
for msg in msg.to_string().lines() { for msg in msg.to_string().lines() {
println!("{}", msg); println!("{}", msg);
let _ = tx if let Err(e) = tx
.send(Command::PRIVMSG("#p2000".to_string(), msg.to_string())) .send(Command::PRIVMSG("#p2000".to_string(), msg.to_string()))
.await; .await
{
// shut everything down, we need to reconnect/restart
eprintln!("{:?}", e);
let _ = term_tx.send(());
break 'outer;
};
} }
} }
buffer.clear(); buffer.clear();
} }
eprintln!("End of program...");
Ok(()) Ok(())
} }
#[tokio::main]
async fn main() -> Result<()> {
loop {
if let Err(e) = real_main().await {
eprintln!("{:?}", e);
continue;
}
eprintln!("Restarting...");
}
}