Remove code comments
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
c522e3aa3e
commit
33fbe7d5fc
17 changed files with 18 additions and 335 deletions
|
|
@ -1,6 +1,3 @@
|
|||
//! The blocking IRC transport: a plain-TCP or TLS byte stream wrapped with
|
||||
//! line reading and convenience senders.
|
||||
|
||||
use std::io::{self, BufRead, BufReader, Read, Write};
|
||||
use std::net::TcpStream;
|
||||
|
||||
|
|
@ -8,29 +5,15 @@ use native_tls::TlsConnector;
|
|||
|
||||
use super::Message;
|
||||
|
||||
/// Any bidirectional byte stream: a plain `TcpStream` or a TLS session. The
|
||||
/// blanket impl covers both, so [`Connection`] can treat them uniformly.
|
||||
trait Stream: Read + Write {}
|
||||
impl<T: Read + Write> Stream for T {}
|
||||
|
||||
/// A blocking IRC connection over plain TCP or TLS.
|
||||
///
|
||||
/// The event loop is single-threaded (read a message, handle it, maybe write a
|
||||
/// reply, repeat), so one stream serves both directions: reads go through the
|
||||
/// `BufReader`, writes through `get_mut()`. This is what lets a single TLS
|
||||
/// session — which cannot be cloned into independent halves — work unchanged.
|
||||
pub struct Connection {
|
||||
inner: BufReader<Box<dyn Stream>>,
|
||||
/// Prefix prepended to wire logs (`[name] `) so simultaneous networks stay
|
||||
/// distinguishable. Empty for a single network, keeping its output terse.
|
||||
log_prefix: String,
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
/// Connect to `host:port`, optionally wrapping the socket in TLS.
|
||||
///
|
||||
/// TLS uses the system trust store (via `native-tls`/OpenSSL), with `host`
|
||||
/// supplied as the SNI name and for certificate verification.
|
||||
pub fn connect(host: &str, port: u16, use_tls: bool) -> io::Result<Connection> {
|
||||
let tcp = TcpStream::connect((host, port))?;
|
||||
let stream: Box<dyn Stream> = if use_tls {
|
||||
|
|
@ -46,9 +29,6 @@ impl Connection {
|
|||
Ok(Connection { inner: BufReader::new(stream), log_prefix: String::new() })
|
||||
}
|
||||
|
||||
/// Set a short label prepended to this connection's wire logs, so output
|
||||
/// from multiple simultaneous networks stays distinguishable. An empty
|
||||
/// label clears it (the terse single-network default).
|
||||
pub fn set_label(&mut self, label: &str) {
|
||||
self.log_prefix = if label.is_empty() {
|
||||
String::new()
|
||||
|
|
@ -57,13 +37,11 @@ impl Connection {
|
|||
};
|
||||
}
|
||||
|
||||
/// Read and parse the next message. Returns `Ok(None)` on a clean EOF
|
||||
/// (the server closed the connection). Blank/unparseable lines are skipped.
|
||||
pub fn read_message(&mut self) -> io::Result<Option<Message>> {
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
if self.inner.read_line(&mut line)? == 0 {
|
||||
return Ok(None); // EOF
|
||||
return Ok(None);
|
||||
}
|
||||
let trimmed = line.trim_end_matches(['\r', '\n']);
|
||||
if trimmed.is_empty() {
|
||||
|
|
@ -73,12 +51,9 @@ impl Connection {
|
|||
if let Some(msg) = Message::parse(trimmed) {
|
||||
return Ok(Some(msg));
|
||||
}
|
||||
// Unparseable line: skip and keep reading.
|
||||
}
|
||||
}
|
||||
|
||||
/// Send one raw, pre-formatted IRC line. The CRLF terminator is added here;
|
||||
/// any embedded CR/LF are neutralised to prevent command injection.
|
||||
pub fn send_raw(&mut self, line: &str) -> io::Result<()> {
|
||||
let clean = line.replace(['\r', '\n'], " ");
|
||||
eprintln!("{}>> {clean}", self.log_prefix);
|
||||
|
|
@ -87,8 +62,6 @@ impl Connection {
|
|||
writer.flush()
|
||||
}
|
||||
|
||||
// --- Convenience senders -------------------------------------------------
|
||||
|
||||
pub fn pass(&mut self, password: &str) -> io::Result<()> {
|
||||
self.send_raw(&format!("PASS {password}"))
|
||||
}
|
||||
|
|
@ -121,8 +94,6 @@ impl Connection {
|
|||
self.send_raw(&format!("QUIT :{reason}"))
|
||||
}
|
||||
|
||||
// --- IRCv3 capability negotiation ---------------------------------------
|
||||
|
||||
pub fn cap_ls(&mut self) -> io::Result<()> {
|
||||
self.send_raw("CAP LS 302")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue