aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client/event.rs50
-rw-r--r--src/client/handler.rs250
-rw-r--r--src/client/mod.rs176
-rw-r--r--src/client/state.rs62
-rw-r--r--src/connection/mod.rs82
-rw-r--r--src/lib.rs3
-rw-r--r--src/main.rs268
-rw-r--r--src/proto/codec.rs98
-rw-r--r--src/proto/error.rs25
-rw-r--r--src/proto/message.rs163
-rw-r--r--src/proto/mod.rs5
-rw-r--r--src/proto/parser.rs170
-rw-r--r--src/proto/serializer.rs78
-rw-r--r--src/tui/app.rs130
-rw-r--r--src/tui/mod.rs2
-rw-r--r--src/tui/ui.rs252
16 files changed, 1814 insertions, 0 deletions
diff --git a/src/client/event.rs b/src/client/event.rs
new file mode 100644
index 0000000..d34908d
--- /dev/null
+++ b/src/client/event.rs
@@ -0,0 +1,50 @@
+/// Events produced by the IRC client and surfaced to your application.
+/// Match on these in your main loop to drive UI, bot logic, etc.
+#[derive(Debug, Clone)]
+pub enum Event {
+ /// Successfully registered with the server (001 received)
+ Connected { server: String, nick: String },
+
+ /// A PRIVMSG or NOTICE in a channel or as a PM
+ Message {
+ from: String,
+ target: String,
+ text: String,
+ is_notice: bool,
+ },
+
+ /// We joined a channel
+ Joined { channel: String },
+
+ /// We or someone else left a channel
+ Parted {
+ channel: String,
+ nick: String,
+ reason: Option<String>,
+ },
+
+ /// Someone quit the server
+ Quit {
+ nick: String,
+ reason: Option<String>,
+ },
+
+ /// A nick change (could be ours)
+ NickChanged { old_nick: String, new_nick: String },
+
+ /// Channel topic was set or changed
+ Topic { channel: String, topic: String },
+
+ /// NAMES list entry (members of a channel)
+ Names {
+ channel: String,
+ members: Vec<String>,
+ },
+
+ /// A raw message we didn't handle specifically
+ /// Useful for debugging or handling custom commands
+ Raw(crate::proto::message::IrcMessage),
+
+ /// The connection was closed
+ Disconnected,
+}
diff --git a/src/client/handler.rs b/src/client/handler.rs
new file mode 100644
index 0000000..91aa5ea
--- /dev/null
+++ b/src/client/handler.rs
@@ -0,0 +1,250 @@
+use tracing::{debug, warn};
+
+use crate::client::event::Event;
+use crate::client::state::{ClientState, RegistrationState};
+use crate::connection::Sender;
+use crate::proto::message::{Command, IrcMessage, Prefix};
+use crate::proto::serializer::serialize;
+
+/// Dispatch a single incoming `IrcMessage`, updating `state` and returning
+/// zero or more `Event`s for the application to handle.
+pub fn handle(msg: IrcMessage, state: &mut ClientState, sender: &Sender) -> Vec<Event> {
+ let mut events = Vec::new();
+
+ match &msg.command {
+ // --- PING: must reply immediately or the server drops us ---
+ Command::Ping => {
+ let token = msg.params.first().cloned().unwrap_or_default();
+ sender.send(IrcMessage::new(Command::Pong, vec![token]));
+ }
+
+ // --- CAP: capability negotiation ---
+ Command::Cap => {
+ handle_cap(&msg, state, sender);
+ }
+
+ // --- 001: welcome — registration complete ---
+ Command::Numeric(1) => {
+ let server = msg
+ .prefix
+ .as_ref()
+ .map(|p| match p {
+ Prefix::Server(s) => s.clone(),
+ Prefix::User { nick, .. } => nick.clone(),
+ })
+ .unwrap_or_default();
+
+ // Server may have assigned us a different nick
+ if let Some(nick) = msg.params.first() {
+ state.nick = nick.clone();
+ }
+
+ state.reg = RegistrationState::Registered;
+ state.server_name = Some(server.clone());
+
+ events.push(Event::Connected {
+ server,
+ nick: state.nick.clone(),
+ });
+ }
+
+ // --- 353: NAMES reply (list of members in a channel) ---
+ Command::Numeric(353) => {
+ // params: [our_nick, ("=" / "*" / "@"), channel, ":member1 member2 ..."]
+ if let (Some(channel), Some(members_str)) = (msg.params.get(2), msg.params.get(3)) {
+ let members: Vec<String> = members_str
+ .split_whitespace()
+ // Strip membership prefixes (@, +, etc.)
+ .map(|m| {
+ m.trim_start_matches(&['@', '+', '%', '~', '&'][..])
+ .to_string()
+ })
+ .collect();
+
+ let ch = state.channel_mut(channel);
+ ch.members.extend(members.clone());
+
+ events.push(Event::Names {
+ channel: channel.clone(),
+ members,
+ });
+ }
+ }
+
+ // --- 332: topic on join ---
+ Command::Numeric(332) => {
+ if let (Some(channel), Some(topic)) = (msg.params.get(1), msg.params.get(2)) {
+ state.channel_mut(channel).topic = Some(topic.clone());
+ events.push(Event::Topic {
+ channel: channel.clone(),
+ topic: topic.clone(),
+ });
+ }
+ }
+
+ // --- JOIN ---
+ Command::Join => {
+ let nick = nick_from_prefix(&msg.prefix);
+ if let Some(channel) = msg.params.first() {
+ if nick == state.nick {
+ // We joined
+ state.channel_mut(channel);
+ events.push(Event::Joined {
+ channel: channel.clone(),
+ });
+ } else {
+ // Someone else joined
+ state.channel_mut(channel).members.insert(nick);
+ }
+ }
+ }
+
+ // --- PART ---
+ Command::Part => {
+ let nick = nick_from_prefix(&msg.prefix);
+ let channel = msg.params.first().cloned().unwrap_or_default();
+ let reason = msg.params.get(1).cloned();
+
+ if nick == state.nick {
+ state.remove_channel(&channel);
+ } else {
+ state.channel_mut(&channel).members.remove(&nick);
+ }
+
+ events.push(Event::Parted {
+ channel,
+ nick,
+ reason,
+ });
+ }
+
+ // --- QUIT ---
+ Command::Quit => {
+ let nick = nick_from_prefix(&msg.prefix);
+ let reason = msg.params.first().cloned();
+
+ // Remove them from all channels
+ for ch in state.channels.values_mut() {
+ ch.members.remove(&nick);
+ }
+
+ events.push(Event::Quit { nick, reason });
+ }
+
+ // --- NICK ---
+ Command::Nick => {
+ let old_nick = nick_from_prefix(&msg.prefix);
+ let new_nick = msg.params.first().cloned().unwrap_or_default();
+
+ if old_nick == state.nick {
+ state.nick = new_nick.clone();
+ }
+
+ // Update in all channels
+ for ch in state.channels.values_mut() {
+ if ch.members.remove(&old_nick) {
+ ch.members.insert(new_nick.clone());
+ }
+ }
+
+ events.push(Event::NickChanged { old_nick, new_nick });
+ }
+
+ // --- PRIVMSG / NOTICE ---
+ Command::Privmsg | Command::Notice => {
+ let from = nick_from_prefix(&msg.prefix);
+ let target = msg.params.first().cloned().unwrap_or_default();
+ let text = msg.params.get(1).cloned().unwrap_or_default();
+ let is_notice = msg.command == Command::Notice;
+
+ events.push(Event::Message {
+ from,
+ target,
+ text,
+ is_notice,
+ });
+ }
+
+ // --- TOPIC (live change) ---
+ Command::Topic => {
+ if let (Some(channel), Some(topic)) = (msg.params.first(), msg.params.get(1)) {
+ state.channel_mut(channel).topic = Some(topic.clone());
+ events.push(Event::Topic {
+ channel: channel.clone(),
+ topic: topic.clone(),
+ });
+ }
+ }
+
+ // --- Everything else: surface as Raw ---
+ _ => {
+ debug!("Unhandled: {}", serialize(&msg));
+ events.push(Event::Raw(msg));
+ }
+ }
+
+ events
+}
+
+/// Handle CAP sub-commands during capability negotiation.
+fn handle_cap(msg: &IrcMessage, state: &mut ClientState, sender: &Sender) {
+ // params: [target, subcommand, (optional "*",) params]
+ let subcommand = msg.params.get(1).map(|s| s.as_str()).unwrap_or("");
+
+ match subcommand {
+ "LS" => {
+ // Server listed its capabilities.
+ // For now, request a small set of common useful caps.
+ let want = ["multi-prefix", "away-notify", "server-time", "message-tags"];
+ let server_caps = msg.params.last().map(|s| s.as_str()).unwrap_or("");
+
+ let to_request: Vec<&str> = want
+ .iter()
+ .copied()
+ .filter(|cap| server_caps.split_whitespace().any(|s| s == *cap))
+ .collect();
+
+ if to_request.is_empty() {
+ sender.send(IrcMessage::new(Command::Cap, vec!["END".into()]));
+ state.reg = RegistrationState::WaitingForWelcome;
+ } else {
+ sender.send(IrcMessage::new(
+ Command::Cap,
+ vec!["REQ".into(), to_request.join(" ")],
+ ));
+ state.reg = RegistrationState::CapPending;
+ }
+ }
+
+ "ACK" => {
+ // Server acknowledged our capability requests
+ if let Some(caps) = msg.params.last() {
+ for cap in caps.split_whitespace() {
+ state.caps.insert(cap.to_string());
+ }
+ }
+ sender.send(IrcMessage::new(Command::Cap, vec!["END".into()]));
+ state.reg = RegistrationState::WaitingForWelcome;
+ }
+
+ "NAK" => {
+ // Server rejected our request — just end negotiation
+ warn!("CAP NAK: {:?}", msg.params.last());
+ sender.send(IrcMessage::new(Command::Cap, vec!["END".into()]));
+ state.reg = RegistrationState::WaitingForWelcome;
+ }
+
+ other => {
+ debug!("Unhandled CAP subcommand: {}", other);
+ }
+ }
+}
+
+/// Extract the nick from a message prefix, returning empty string if absent.
+fn nick_from_prefix(prefix: &Option<Prefix>) -> String {
+ match prefix {
+ Some(Prefix::User { nick, .. }) => nick.clone(),
+ Some(Prefix::Server(s)) => s.clone(),
+ None => String::new(),
+ }
+}
diff --git a/src/client/mod.rs b/src/client/mod.rs
new file mode 100644
index 0000000..6d32017
--- /dev/null
+++ b/src/client/mod.rs
@@ -0,0 +1,176 @@
+use tokio::sync::mpsc;
+use tracing::info;
+
+use crate::client::event::Event;
+use crate::client::handler::handle;
+use crate::client::state::ClientState;
+use crate::connection::{self, Sender};
+use crate::proto::message::{Command, IrcMessage};
+
+pub mod event;
+pub mod handler;
+pub mod state;
+
+/// Configuration for the IRC client.
+pub struct Config {
+ /// Server address, e.g. "irc.libera.chat:6667"
+ pub server: String,
+ /// Desired nick
+ pub nick: String,
+ /// IRC username (shown in /whois)
+ pub user: String,
+ /// Real name (shown in /whois)
+ pub realname: String,
+ /// Optional server password
+ pub password: Option<String>,
+ /// Channels to auto-join after registration
+ pub autojoin: Vec<String>,
+}
+
+/// The main IRC client.
+///
+/// Call `Client::connect` to establish a connection, then drive the event
+/// loop with `client.next_event().await` in your application loop.
+pub struct Client {
+ state: ClientState,
+ sender: Sender,
+ inbox: mpsc::UnboundedReceiver<IrcMessage>,
+ config: Config,
+}
+
+impl Client {
+ /// Connect to the server and begin the registration handshake.
+ pub async fn connect(config: Config) -> Result<Self, std::io::Error> {
+ let (sender, inbox) = connection::connect(&config.server).await?;
+ let state = ClientState::new(&config.nick);
+
+ let client = Self {
+ state,
+ sender,
+ inbox,
+ config,
+ };
+ client.register();
+ Ok(client)
+ }
+
+ /// Send a raw `IrcMessage` to the server.
+ pub fn send(&self, msg: IrcMessage) {
+ self.sender.send(msg);
+ }
+
+ /// Send a PRIVMSG to a channel or user.
+ pub fn privmsg(&self, target: &str, text: &str) {
+ self.sender.send(IrcMessage::new(
+ Command::Privmsg,
+ vec![target.to_string(), text.to_string()],
+ ));
+ }
+
+ /// Join a channel.
+ pub fn join(&self, channel: &str) {
+ self.sender
+ .send(IrcMessage::new(Command::Join, vec![channel.to_string()]));
+ }
+
+ /// Part a channel.
+ pub fn part(&self, channel: &str, reason: Option<&str>) {
+ let mut params = vec![channel.to_string()];
+ if let Some(r) = reason {
+ params.push(r.to_string());
+ }
+ self.sender.send(IrcMessage::new(Command::Part, params));
+ }
+
+ /// Change nick.
+ pub fn nick(&self, new_nick: &str) {
+ self.sender
+ .send(IrcMessage::new(Command::Nick, vec![new_nick.to_string()]));
+ }
+
+ /// Read-only view of current client state.
+ pub fn state(&self) -> &ClientState {
+ &self.state
+ }
+
+ /// Wait for the next event from the server.
+ /// Returns `None` if the connection has closed.
+ pub async fn next_event(&mut self) -> Option<Event> {
+ loop {
+ let msg = self.inbox.recv().await?;
+ let events = handle(msg, &mut self.state, &self.sender);
+
+ // Handle auto-join after registration
+ for event in &events {
+ if let Event::Connected { .. } = event {
+ for channel in &self.config.autojoin.clone() {
+ info!("Auto-joining {}", channel);
+ self.join(channel);
+ }
+ }
+ }
+
+ // Return the first event; re-queue the rest
+ // (simple approach: process one at a time via recursive buffering)
+ if let Some(first) = events.into_iter().next() {
+ return Some(first);
+ }
+ // If no events were produced (e.g. a PING), loop and wait for next message
+ }
+ }
+
+ /// Send the registration sequence to the server.
+ fn register(&self) {
+ // Optional server password
+ if let Some(pass) = &self.config.password {
+ self.sender
+ .send(IrcMessage::new(Command::Pass, vec![pass.clone()]));
+ }
+
+ // Begin CAP negotiation first — lets us request IRCv3 caps
+ // before NICK/USER so the server doesn't rush past registration
+ self.sender.send(IrcMessage::new(
+ Command::Cap,
+ vec!["LS".into(), "302".into()],
+ ));
+
+ self.sender.send(IrcMessage::new(
+ Command::Nick,
+ vec![self.config.nick.clone()],
+ ));
+
+ self.sender.send(IrcMessage::new(
+ Command::User,
+ vec![
+ self.config.user.clone(),
+ "0".into(),
+ "*".into(),
+ self.config.realname.clone(),
+ ],
+ ));
+ }
+}
+
+impl Client {
+ /// Non-blocking version of `next_event`.
+ /// Returns `Some(event)` if one is immediately available, `None` otherwise.
+ /// Used by the TUI loop to drain events without blocking the render tick.
+ pub fn next_event_nowait(&mut self) -> Option<Event> {
+ loop {
+ let msg = self.inbox.try_recv().ok()?;
+ let mut events = handle(msg, &mut self.state, &self.sender);
+
+ for event in &events {
+ if let Event::Connected { .. } = event {
+ for channel in &self.config.autojoin.clone() {
+ self.join(channel);
+ }
+ }
+ }
+
+ if !events.is_empty() {
+ return Some(events.remove(0));
+ }
+ }
+ }
+}
diff --git a/src/client/state.rs b/src/client/state.rs
new file mode 100644
index 0000000..b987509
--- /dev/null
+++ b/src/client/state.rs
@@ -0,0 +1,62 @@
+use std::collections::{HashMap, HashSet};
+
+/// The full state of a connected IRC client.
+#[derive(Debug, Default)]
+pub struct ClientState {
+ pub nick: String,
+ pub channels: HashMap<String, Channel>,
+ pub caps: HashSet<String>,
+ pub server_name: Option<String>,
+ pub reg: RegistrationState,
+}
+
+impl ClientState {
+ pub fn new(nick: impl Into<String>) -> Self {
+ Self {
+ nick: nick.into(),
+ ..Default::default()
+ }
+ }
+
+ pub fn channel(&self, name: &str) -> Option<&Channel> {
+ self.channels.get(&name.to_lowercase())
+ }
+
+ pub fn channel_mut(&mut self, name: &str) -> &mut Channel {
+ self.channels
+ .entry(name.to_lowercase())
+ .or_insert_with(|| Channel::new(name))
+ }
+
+ pub fn remove_channel(&mut self, name: &str) {
+ self.channels.remove(&name.to_lowercase());
+ }
+}
+
+/// State of the registration handshake.
+#[derive(Debug, Default, PartialEq, Eq)]
+pub enum RegistrationState {
+ #[default]
+ CapNegotiation,
+ CapPending,
+ WaitingForWelcome,
+ Registered,
+}
+
+/// A joined channel and its current state.
+#[derive(Debug)]
+pub struct Channel {
+ pub name: String,
+ pub members: HashSet<String>,
+ pub topic: Option<String>,
+}
+
+impl Channel {
+ pub fn new(name: impl Into<String>) -> Self {
+ Self {
+ name: name.into(),
+ members: HashSet::new(),
+ topic: None,
+ }
+ }
+}
diff --git a/src/connection/mod.rs b/src/connection/mod.rs
new file mode 100644
index 0000000..bc837c0
--- /dev/null
+++ b/src/connection/mod.rs
@@ -0,0 +1,82 @@
+use futures::SinkExt;
+use tokio::net::TcpStream;
+use tokio::sync::mpsc;
+use tokio_util::codec::Framed;
+use tracing::{debug, error, info};
+
+use crate::proto::codec::IrcCodec;
+use crate::proto::message::IrcMessage;
+
+/// A handle to send messages to the server.
+/// Cheaply cloneable — pass it wherever you need to write.
+#[derive(Clone)]
+pub struct Sender {
+ tx: mpsc::UnboundedSender<IrcMessage>,
+}
+
+impl Sender {
+ pub fn send(&self, msg: IrcMessage) {
+ // Only fails if the connection task has shut down
+ let _ = self.tx.send(msg);
+ }
+}
+
+/// Establish a TCP connection and return:
+/// - A `Sender` handle for writing messages
+/// - An `mpsc::UnboundedReceiver<IrcMessage>` for reading incoming messages
+///
+/// Two background tasks are spawned:
+/// - A **writer task**: drains the sender channel and writes to the TCP stream
+/// - A **reader task**: reads from the TCP stream and forwards to the inbox
+///
+/// This split means the caller never has to hold a lock to send a message.
+pub async fn connect(
+ addr: &str,
+) -> Result<(Sender, mpsc::UnboundedReceiver<IrcMessage>), std::io::Error> {
+ info!("Connecting to {}", addr);
+ let stream = TcpStream::connect(addr).await?;
+ info!("TCP connected to {}", addr);
+
+ let framed = Framed::new(stream, IrcCodec::new());
+ let (mut sink, mut stream) = futures::StreamExt::split(framed);
+
+ // Channel for outbound messages (caller → writer task)
+ let (out_tx, mut out_rx) = mpsc::unbounded_channel::<IrcMessage>();
+
+ // Channel for inbound messages (reader task → caller)
+ let (in_tx, in_rx) = mpsc::unbounded_channel::<IrcMessage>();
+
+ // Writer task: takes messages from out_rx and sends them to the server
+ tokio::spawn(async move {
+ while let Some(msg) = out_rx.recv().await {
+ debug!("--> {}", crate::proto::serializer::serialize(&msg));
+ if let Err(e) = sink.send(msg).await {
+ error!("Write error: {}", e);
+ break;
+ }
+ }
+ debug!("Writer task shut down");
+ });
+
+ // Reader task: receives messages from the server and forwards to in_tx
+ tokio::spawn(async move {
+ use futures::StreamExt;
+ while let Some(result) = stream.next().await {
+ match result {
+ Ok(msg) => {
+ debug!("<-- {}", crate::proto::serializer::serialize(&msg));
+ if in_tx.send(msg).is_err() {
+ break; // Receiver dropped, shut down
+ }
+ }
+ Err(e) => {
+ error!("Read error: {}", e);
+ break;
+ }
+ }
+ }
+ debug!("Reader task shut down");
+ });
+
+ Ok((Sender { tx: out_tx }, in_rx))
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..f625d00
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,3 @@
+pub mod client;
+pub mod connection;
+pub mod proto;
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..b7d4365
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,268 @@
+use std::io;
+use std::time::Duration;
+
+use crossterm::{
+ event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
+ execute,
+ terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
+};
+use ratatui::{Terminal, backend::CrosstermBackend};
+
+use irc_client::client::event::Event as IrcEvent;
+use irc_client::client::{Client, Config};
+use irc_client::proto::message::{Command, IrcMessage};
+use tui::app::{AppState, CHANNEL};
+use tui::ui;
+mod tui;
+
+const NICK: &str = "";
+
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ // ── Terminal setup ────────────────────────────────────────────────────
+ enable_raw_mode()?;
+ let mut stdout = io::stdout();
+ execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
+ let backend = CrosstermBackend::new(stdout);
+ let mut terminal = Terminal::new(backend)?;
+ terminal.clear()?;
+
+ let result = run(&mut terminal).await;
+
+ // ── Restore terminal ──────────────────────────────────────────────────
+ disable_raw_mode()?;
+ execute!(
+ terminal.backend_mut(),
+ LeaveAlternateScreen,
+ DisableMouseCapture,
+ )?;
+ terminal.show_cursor()?;
+
+ result
+}
+
+async fn run(
+ terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let mut app = AppState::new(NICK, CHANNEL);
+
+ // ── Connect to IRC ────────────────────────────────────────────────────
+ let config = Config {
+ server: "irc.libera.chat:6667".to_string(),
+ nick: NICK.to_string(),
+ user: "speakez".to_string(),
+ realname: "speakez".to_string(),
+ password: None,
+ autojoin: vec![CHANNEL.to_string()],
+ };
+
+ let mut client = Client::connect(config).await?;
+
+ // ── Main event loop ───────────────────────────────────────────────────
+ // We poll both IRC events and keyboard events with short timeouts so
+ // neither blocks the other.
+ loop {
+ // Draw
+ terminal.draw(|f| ui::draw(f, &app))?;
+
+ // Poll crossterm for keyboard input (non-blocking, 20ms timeout)
+ if event::poll(Duration::from_millis(20))? {
+ if let Event::Key(key) = event::read()? {
+ match (key.modifiers, key.code) {
+ // Quit
+ (KeyModifiers::CONTROL, KeyCode::Char('c')) => break,
+
+ // Send message / command
+ (_, KeyCode::Enter) => {
+ let text = app.take_input();
+ if !text.is_empty() {
+ handle_input(&text, &mut app, &mut client);
+ }
+ }
+
+ // Typing
+ (KeyModifiers::NONE | KeyModifiers::SHIFT, KeyCode::Char(c)) => {
+ app.input_insert(c);
+ }
+ (_, KeyCode::Backspace) => app.input_backspace(),
+ (_, KeyCode::Left) => app.cursor_left(),
+ (_, KeyCode::Right) => app.cursor_right(),
+ (_, KeyCode::Home) => app.cursor = 0,
+ (_, KeyCode::End) => app.cursor = app.input.len(),
+
+ // Scroll
+ (_, KeyCode::PageUp) => app.scroll = app.scroll.saturating_add(5),
+ (_, KeyCode::PageDown) => app.scroll = app.scroll.saturating_sub(5),
+
+ _ => {}
+ }
+ }
+ }
+
+ // Drain IRC events (non-blocking — try_recv drains without waiting)
+ // We use a small loop to process a burst without starving the UI
+ for _ in 0..16 {
+ match client.next_event_nowait() {
+ Some(irc_event) => handle_irc_event(irc_event, &mut app),
+ None => break,
+ }
+ }
+ }
+
+ Ok(())
+}
+
+/// Handle a line entered in the input box.
+fn handle_input(text: &str, app: &mut AppState, client: &mut Client) {
+ if let Some(cmd) = text.strip_prefix('/') {
+ // It's a command
+ let mut parts = cmd.splitn(2, ' ');
+ let verb = parts.next().unwrap_or("").to_uppercase();
+ let args = parts.next().unwrap_or("");
+
+ match verb.as_str() {
+ "JOIN" => {
+ client.join(args.trim());
+ }
+ "PART" => {
+ let channel = if args.is_empty() {
+ &app.channel
+ } else {
+ args.trim()
+ };
+ client.part(channel, None);
+ }
+ "NICK" => {
+ client.nick(args.trim());
+ }
+ "QUIT" => {
+ client.send(IrcMessage::new(Command::Quit, vec![args.to_string()]));
+ }
+ "ME" => {
+ // CTCP ACTION
+ let ctcp = format!("\x01ACTION {}\x01", args);
+ client.privmsg(&app.channel, &ctcp);
+ app.push_system(&format!("* {} {}", app.nick, args));
+ }
+ "MSG" => {
+ let mut p = args.splitn(2, ' ');
+ if let (Some(target), Some(msg)) = (p.next(), p.next()) {
+ client.privmsg(target, msg);
+ app.push_system(&format!("→ {}: {}", target, msg));
+ }
+ }
+ other => {
+ app.push_system(&format!("Unknown command: /{}", other));
+ }
+ }
+ } else {
+ // Regular chat message to active channel
+ client.privmsg(&app.channel, text);
+ app.push_message(&app.nick.clone(), text, true);
+ }
+}
+
+/// Apply an IRC event to the app state.
+fn handle_irc_event(event: IrcEvent, app: &mut AppState) {
+ match event {
+ IrcEvent::Connected { server, nick } => {
+ app.nick = nick.clone();
+ app.connected = true;
+ app.status = format!("connected to {}", server);
+ app.push_system(&format!("Connected to {} as {}", server, nick));
+ }
+
+ IrcEvent::Joined { channel } => {
+ app.push_system(&format!("You joined {}", channel));
+ }
+
+ IrcEvent::Message {
+ from,
+ target,
+ text,
+ is_notice: _,
+ } => {
+ // Only show messages for our active channel (or PMs to us)
+ let is_self = from == app.nick;
+ if !is_self {
+ // Don't re-echo our own messages (we already pushed them in handle_input)
+ if target == app.channel || target == app.nick {
+ app.push_message(&from, &text, false);
+ }
+ }
+ }
+
+ IrcEvent::Parted {
+ channel,
+ nick,
+ reason,
+ } => {
+ app.members.retain(|m| {
+ let bare = m.trim_start_matches(&['@', '+', '%'][..]);
+ bare != nick
+ });
+ app.push_system(&format!(
+ "{} left {}{}",
+ nick,
+ channel,
+ reason.map(|r| format!(" ({})", r)).unwrap_or_default()
+ ));
+ }
+
+ IrcEvent::Quit { nick, reason } => {
+ app.members.retain(|m| {
+ let bare = m.trim_start_matches(&['@', '+', '%'][..]);
+ bare != nick
+ });
+ app.push_system(&format!(
+ "{} quit{}",
+ nick,
+ reason.map(|r| format!(" ({})", r)).unwrap_or_default()
+ ));
+ }
+
+ IrcEvent::NickChanged { old_nick, new_nick } => {
+ for m in &mut app.members {
+ let bare = m.trim_start_matches(&['@', '+', '%'][..]).to_string();
+ if bare == old_nick {
+ let sigil: String = m.chars().take_while(|c| "@+%~&".contains(*c)).collect();
+ *m = format!("{}{}", sigil, new_nick);
+ break;
+ }
+ }
+ if old_nick == app.nick {
+ app.nick = new_nick.clone();
+ }
+ app.push_system(&format!("{} is now {}", old_nick, new_nick));
+ }
+
+ IrcEvent::Topic { channel, topic } => {
+ app.status = format!("{}: {}", channel, topic);
+ app.push_system(&format!("Topic: {}", topic));
+ }
+
+ IrcEvent::Names {
+ channel: _,
+ members,
+ } => {
+ for m in members {
+ if !app
+ .members
+ .iter()
+ .any(|existing| existing.trim_start_matches(&['@', '+', '%'][..]) == m)
+ {
+ app.members.push(m);
+ }
+ }
+ app.sort_members();
+ }
+
+ IrcEvent::Disconnected => {
+ app.connected = false;
+ app.status = "disconnected".to_string();
+ app.push_system("--- Disconnected ---");
+ }
+
+ IrcEvent::Raw(_) => {}
+ }
+}
diff --git a/src/proto/codec.rs b/src/proto/codec.rs
new file mode 100644
index 0000000..d8f9b10
--- /dev/null
+++ b/src/proto/codec.rs
@@ -0,0 +1,98 @@
+use bytes::{BufMut, BytesMut};
+use tokio_util::codec::{Decoder, Encoder};
+
+use crate::proto::error::CodecError;
+use crate::proto::message::IrcMessage;
+use crate::proto::parser::parse;
+use crate::proto::serializer::serialize;
+
+const MAX_LINE_LENGTH: usize = 512;
+
+pub struct IrcCodec {
+ max_line_length: usize,
+}
+
+impl IrcCodec {
+ pub fn new() -> Self {
+ Self {
+ max_line_length: MAX_LINE_LENGTH,
+ }
+ }
+
+ pub fn with_max_length(max_line_length: usize) -> Self {
+ Self { max_line_length }
+ }
+}
+
+impl Default for IrcCodec {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Decoder for IrcCodec {
+ type Item = IrcMessage;
+ type Error = CodecError;
+
+ fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
+ loop {
+ let newline_pos = src.iter().position(|&b| b == b'\n');
+
+ match newline_pos {
+ None => {
+ if src.len() > self.max_line_length {
+ return Err(CodecError::Parse(
+ crate::proto::error::ParseError::MessageTooLong,
+ ));
+ }
+ return Ok(None);
+ }
+ Some(pos) => {
+ let line_bytes = src.split_to(pos + 1);
+
+ let line = &line_bytes[..line_bytes.len() - 1]; // strip \n
+ let line = if line.last() == Some(&b'\r') {
+ &line[..line.len() - 1] // strip \r
+ } else {
+ line
+ };
+
+ // Skip empty lines silently
+ if line.is_empty() {
+ continue;
+ }
+
+ let line_str = std::str::from_utf8(line).map_err(|_| {
+ CodecError::Io(std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "IRC message is not valid UTF-8",
+ ))
+ })?;
+
+ let msg = parse(line_str)?;
+ return Ok(Some(msg));
+ }
+ }
+ }
+ }
+}
+
+impl Encoder<IrcMessage> for IrcCodec {
+ type Error = CodecError;
+
+ fn encode(&mut self, msg: IrcMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
+ let line = serialize(&msg);
+
+ // +2 for \r\n
+ if line.len() + 2 > self.max_line_length {
+ return Err(CodecError::Parse(
+ crate::proto::error::ParseError::MessageTooLong,
+ ));
+ }
+
+ dst.reserve(line.len() + 2);
+ dst.put_slice(line.as_bytes());
+ dst.put_slice(b"\r\n");
+ Ok(())
+ }
+}
diff --git a/src/proto/error.rs b/src/proto/error.rs
new file mode 100644
index 0000000..8a901ce
--- /dev/null
+++ b/src/proto/error.rs
@@ -0,0 +1,25 @@
+use thiserror::Error;
+
+#[derive(Debug, Error, PartialEq)]
+pub enum ParseError {
+ #[error("message is empty")]
+ EmptyMessage,
+
+ #[error("missing command")]
+ MissingCommand,
+
+ #[error("invalid tag format: {0}")]
+ InvalidTag(String),
+
+ #[error("line exceeds max message length")]
+ MessageTooLong,
+}
+
+#[derive(Debug, Error)]
+pub enum CodecError {
+ #[error("parse error: {0}")]
+ Parse(#[from] ParseError),
+
+ #[error("I/O error: {0}")]
+ Io(#[from] std::io::Error),
+}
diff --git a/src/proto/message.rs b/src/proto/message.rs
new file mode 100644
index 0000000..dbe4a69
--- /dev/null
+++ b/src/proto/message.rs
@@ -0,0 +1,163 @@
+use std::collections::HashMap;
+use std::fmt;
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct IrcMessage {
+ pub tags: HashMap<String, Option<String>>,
+ pub prefix: Option<Prefix>,
+ pub command: Command,
+ pub params: Vec<String>,
+}
+
+impl IrcMessage {
+ pub fn trailing(&self) -> Option<&str> {
+ self.params.last().map(|s| s.as_str())
+ }
+
+ pub fn new(command: Command, params: Vec<String>) -> Self {
+ Self {
+ tags: HashMap::new(),
+ prefix: None,
+ command,
+ params,
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Prefix {
+ Server(String),
+ User {
+ nick: String,
+ user: Option<String>,
+ host: Option<String>,
+ },
+}
+
+impl fmt::Display for Prefix {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Prefix::Server(s) => write!(f, "{}", s),
+ Prefix::User { nick, user, host } => {
+ write!(f, "{}", nick)?;
+ if let Some(u) = user {
+ write!(f, "!{}", u)?;
+ }
+ if let Some(h) = host {
+ write!(f, "@{}", h)?;
+ }
+ Ok(())
+ }
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum Command {
+ // connection
+ Cap,
+ Nick,
+ User,
+ Pass,
+ Quit,
+ Ping,
+ Pong,
+
+ // channel operations
+ Join,
+ Part,
+ Kick,
+ Topic,
+ Names,
+ List,
+ Invite,
+
+ // messaging
+ Privmsg,
+ Notice,
+
+ // mode & status
+ Mode,
+ Who,
+ Whois,
+ Whowas,
+
+ // Server
+ Oper,
+ Kill,
+ Rehash,
+
+ // Numeric (001-999)
+ Numeric(u16),
+
+ Other(String),
+}
+
+impl Command {
+ pub fn from_str(s: &str) -> Self {
+ if s.len() == 3 && s.chars().all(|c| c.is_ascii_digit()) {
+ if let Ok(n) = s.parse::<u16>() {
+ return Command::Numeric(n);
+ }
+ }
+
+ match s.to_ascii_uppercase().as_str() {
+ "CAP" => Command::Cap,
+ "NICK" => Command::Nick,
+ "USER" => Command::User,
+ "PASS" => Command::Pass,
+ "QUIT" => Command::Quit,
+ "PING" => Command::Ping,
+ "PONG" => Command::Pong,
+ "JOIN" => Command::Join,
+ "PART" => Command::Part,
+ "KICK" => Command::Kick,
+ "TOPIC" => Command::Topic,
+ "NAMES" => Command::Names,
+ "LIST" => Command::List,
+ "INVITE" => Command::Invite,
+ "PRIVMSG" => Command::Privmsg,
+ "NOTICE" => Command::Notice,
+ "MODE" => Command::Mode,
+ "WHO" => Command::Who,
+ "WHOIS" => Command::Whois,
+ "WHOWAS" => Command::Whowas,
+ "OPER" => Command::Oper,
+ "KILL" => Command::Kill,
+ "REHASH" => Command::Rehash,
+ other => Command::Other(other.to_string()),
+ }
+ }
+}
+
+impl fmt::Display for Command {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Command::Cap => write!(f, "CAP"),
+ Command::Nick => write!(f, "NICK"),
+ Command::User => write!(f, "USER"),
+ Command::Pass => write!(f, "PASS"),
+ Command::Quit => write!(f, "QUIT"),
+ Command::Ping => write!(f, "PING"),
+ Command::Pong => write!(f, "PONG"),
+ Command::Join => write!(f, "JOIN"),
+ Command::Part => write!(f, "PART"),
+ Command::Kick => write!(f, "KICK"),
+ Command::Topic => write!(f, "TOPIC"),
+ Command::Names => write!(f, "NAMES"),
+ Command::List => write!(f, "LIST"),
+ Command::Invite => write!(f, "INVITE"),
+ Command::Privmsg => write!(f, "PRIVMSG"),
+ Command::Notice => write!(f, "NOTICE"),
+ Command::Mode => write!(f, "MODE"),
+ Command::Who => write!(f, "WHO"),
+ Command::Whois => write!(f, "WHOIS"),
+ Command::Whowas => write!(f, "WHOWAS"),
+ Command::Oper => write!(f, "OPER"),
+ Command::Kill => write!(f, "KILL"),
+ Command::Rehash => write!(f, "REHASH"),
+ Command::Numeric(n) => write!(f, "{:03}", n),
+ Command::Other(s) => write!(f, "{}", s),
+ }
+ }
+}
diff --git a/src/proto/mod.rs b/src/proto/mod.rs
new file mode 100644
index 0000000..e82784c
--- /dev/null
+++ b/src/proto/mod.rs
@@ -0,0 +1,5 @@
+pub mod codec;
+pub mod error;
+pub mod message;
+pub mod parser;
+pub mod serializer;
diff --git a/src/proto/parser.rs b/src/proto/parser.rs
new file mode 100644
index 0000000..42f516d
--- /dev/null
+++ b/src/proto/parser.rs
@@ -0,0 +1,170 @@
+use crate::proto::error::ParseError;
+use crate::proto::message::{Command, IrcMessage, Prefix};
+use std::collections::HashMap;
+
+pub fn parse(line: &str) -> Result<IrcMessage, ParseError> {
+ if line.is_empty() {
+ return Err(ParseError::EmptyMessage);
+ }
+
+ let mut rest = line;
+
+ // parse tags
+ let tags = if rest.starts_with('@') {
+ let (tag_str, remaining) = rest[1..]
+ .split_once(' ')
+ .ok_or(ParseError::MissingCommand)?;
+ rest = remaining;
+ parse_tags(tag_str)?
+ } else {
+ HashMap::new()
+ };
+
+ // parse prefix
+ let prefix = if rest.starts_with(':') {
+ let (prefix_str, remaining) = rest[1..]
+ .split_once(' ')
+ .ok_or(ParseError::MissingCommand)?;
+ rest = remaining;
+ Some(parse_prefix(prefix_str))
+ } else {
+ None
+ };
+
+ // parse command
+ let (command_str, rest) = match rest.split_once(' ') {
+ Some((cmd, params)) => (cmd, params),
+ None => (rest, ""),
+ };
+
+ if command_str.is_empty() {
+ return Err(ParseError::MissingCommand);
+ }
+
+ let command = Command::from_str(command_str);
+
+ // parse params
+ let params = parse_params(rest);
+
+ Ok(IrcMessage {
+ tags,
+ prefix,
+ command,
+ params,
+ })
+}
+
+fn parse_tags(tag_str: &str) -> Result<HashMap<String, Option<String>>, ParseError> {
+ let mut tags = HashMap::new();
+
+ for tag in tag_str.split(';') {
+ if tag.is_empty() {
+ continue;
+ }
+ match tag.split_once('=') {
+ Some((key, value)) => {
+ tags.insert(key.to_string(), Some(unescape_tag_value(value)));
+ }
+ None => {
+ // boolean tags
+ tags.insert(tag.to_string(), None);
+ }
+ }
+ }
+
+ Ok(tags)
+}
+
+fn unescape_tag_value(value: &str) -> String {
+ let mut result = String::with_capacity(value.len());
+ let mut chars = value.chars().peekable();
+
+ while let Some(ch) = chars.next() {
+ if ch == '\\' {
+ match chars.next() {
+ Some(':') => result.push(';'),
+ Some('s') => result.push(' '),
+ Some('\\') => result.push('\\'),
+ Some('r') => result.push('\r'),
+ Some('n') => result.push('\n'),
+ Some(c) => {
+ result.push('\\');
+ result.push(c);
+ }
+ None => result.push('\\'),
+ }
+ } else {
+ result.push(ch);
+ }
+ }
+
+ result
+}
+
+fn parse_prefix(prefix: &str) -> Prefix {
+ if prefix.contains('!') || prefix.contains('@') {
+ let (nick, rest) = prefix
+ .split_once('!')
+ .map(|(n, r)| (n, Some(r)))
+ .unwrap_or((prefix, None));
+
+ let (user, host) = match rest {
+ Some(r) => r
+ .split_once('@')
+ .map(|(u, h)| (Some(u.to_string()), Some(h.to_string())))
+ .unwrap_or((Some(r.to_string()), None)),
+ None => {
+ // Could be nick@host with no user
+ if let Some((n2, h)) = nick.split_once('@') {
+ return Prefix::User {
+ nick: n2.to_string(),
+ user: None,
+ host: Some(h.to_string()),
+ };
+ }
+ (None, None)
+ }
+ };
+
+ Prefix::User {
+ nick: nick.to_string(),
+ user,
+ host,
+ }
+ } else {
+ // Heuristic: if it contains a dot, it's likely a server name
+ // (nick-only prefixes are also possible but rare without user/host)
+ Prefix::Server(prefix.to_string())
+ }
+}
+
+fn parse_params(params_str: &str) -> Vec<String> {
+ let mut params = Vec::new();
+ let mut rest = params_str;
+
+ loop {
+ rest = rest.trim_start_matches(' ');
+
+ if rest.is_empty() {
+ break;
+ }
+
+ if rest.starts_with(':') {
+ params.push(rest[1..].to_string());
+ break;
+ }
+
+ match rest.split_once(' ') {
+ Some((param, remaining)) => {
+ params.push(param.to_string());
+ rest = remaining;
+ }
+ None => {
+ params.push(rest.to_string());
+ break;
+ }
+ }
+ }
+
+ params
+}
diff --git a/src/proto/serializer.rs b/src/proto/serializer.rs
new file mode 100644
index 0000000..603cc0b
--- /dev/null
+++ b/src/proto/serializer.rs
@@ -0,0 +1,78 @@
+use crate::proto::message::{IrcMessage, Prefix};
+use std::fmt::Write;
+
+pub fn serialize(msg: &IrcMessage) -> String {
+ let mut out = String::with_capacity(512);
+
+ // tags
+ if !msg.tags.is_empty() {
+ out.push('@');
+ let mut first = true;
+ for (key, value) in &msg.tags {
+ if !first {
+ out.push(';');
+ }
+ first = false;
+ out.push_str(key);
+ if let Some(v) = value {
+ out.push('=');
+ escape_tag_value(&mut out, v);
+ }
+ }
+ out.push(' ');
+ }
+
+ // prefix
+ if let Some(prefix) = &msg.prefix {
+ out.push(':');
+ match prefix {
+ Prefix::Server(s) => out.push_str(s),
+ Prefix::User { nick, user, host } => {
+ out.push_str(nick);
+ if let Some(u) = user {
+ out.push('!');
+ out.push_str(u);
+ }
+ if let Some(h) = host {
+ out.push('@');
+ out.push_str(h);
+ }
+ }
+ }
+ out.push(' ');
+ }
+
+ // command
+ let _ = write!(out, "{}", msg.command);
+
+ // params
+ let last_idx = msg.params.len().saturating_sub(1);
+ for (i, param) in msg.params.iter().enumerate() {
+ out.push(' ');
+ // The last param must be trailing if it contains spaces or starts with ':'
+ let needs_trailing = i == last_idx
+ && (param.contains(' ')
+ || param.starts_with(':')
+ || param.is_empty()
+ || msg.params.len() > 1);
+ if needs_trailing {
+ out.push(':');
+ }
+ out.push_str(param);
+ }
+
+ out
+}
+
+fn escape_tag_value(out: &mut String, value: &str) {
+ for ch in value.chars() {
+ match ch {
+ ';' => out.push_str(r"\:"),
+ ' ' => out.push_str(r"\s"),
+ '\\' => out.push_str(r"\\"),
+ '\r' => out.push_str(r"\r"),
+ '\n' => out.push_str(r"\n"),
+ c => out.push(c),
+ }
+ }
+}
diff --git a/src/tui/app.rs b/src/tui/app.rs
new file mode 100644
index 0000000..1e7d021
--- /dev/null
+++ b/src/tui/app.rs
@@ -0,0 +1,130 @@
+/// Compile-time channel to join on startup
+pub const CHANNEL: &str = "#speakez";
+
+/// A single chat message in the log
+#[derive(Clone)]
+pub struct ChatLine {
+ pub nick: String,
+ pub text: String,
+ /// true = server/system message (JOIN, PART, topic, etc.)
+ pub is_system: bool,
+ /// true = NOTICE
+ pub is_notice: bool,
+ /// true = this is our own message
+ pub is_self: bool,
+}
+
+/// All mutable state the TUI needs to render and respond to input
+pub struct AppState {
+ /// Our nick
+ pub nick: String,
+ /// The active channel name
+ pub channel: String,
+ /// Chat log for the active channel
+ pub messages: Vec<ChatLine>,
+ /// Member list for the active channel
+ pub members: Vec<String>,
+ /// Current contents of the input box
+ pub input: String,
+ /// Cursor position within `input` (byte index)
+ pub cursor: usize,
+ /// Scroll offset from the bottom (0 = pinned to latest)
+ pub scroll: usize,
+ /// Status line text (connection state, errors, etc.)
+ pub status: String,
+ /// Whether we've fully registered
+ pub connected: bool,
+}
+
+impl AppState {
+ pub fn new(nick: impl Into<String>, channel: impl Into<String>) -> Self {
+ Self {
+ nick: nick.into(),
+ channel: channel.into(),
+ messages: Vec::new(),
+ members: Vec::new(),
+ input: String::new(),
+ cursor: 0,
+ scroll: 0,
+ status: "Connecting...".into(),
+ connected: false,
+ }
+ }
+
+ /// Push a chat message
+ pub fn push_message(&mut self, nick: &str, text: &str, is_self: bool) {
+ self.messages.push(ChatLine {
+ nick: nick.to_string(),
+ text: text.to_string(),
+ is_system: false,
+ is_notice: false,
+ is_self,
+ });
+ }
+
+ /// Push a system/event line (joins, parts, topic changes)
+ pub fn push_system(&mut self, text: &str) {
+ self.messages.push(ChatLine {
+ nick: String::new(),
+ text: text.to_string(),
+ is_system: true,
+ is_notice: false,
+ is_self: false,
+ });
+ }
+
+ /// Insert a character at the cursor
+ pub fn input_insert(&mut self, ch: char) {
+ self.input.insert(self.cursor, ch);
+ self.cursor += ch.len_utf8();
+ }
+
+ /// Delete the character before the cursor
+ pub fn input_backspace(&mut self) {
+ if self.cursor == 0 {
+ return;
+ }
+ // Find the start of the previous character
+ let prev = self.input[..self.cursor]
+ .char_indices()
+ .last()
+ .map(|(i, _)| i)
+ .unwrap_or(0);
+ self.input.remove(prev);
+ self.cursor = prev;
+ }
+
+ /// Move cursor left one character
+ pub fn cursor_left(&mut self) {
+ self.cursor = self.input[..self.cursor]
+ .char_indices()
+ .last()
+ .map(|(i, _)| i)
+ .unwrap_or(0);
+ }
+
+ /// Move cursor right one character
+ pub fn cursor_right(&mut self) {
+ if self.cursor < self.input.len() {
+ let ch = self.input[self.cursor..].chars().next().unwrap();
+ self.cursor += ch.len_utf8();
+ }
+ }
+
+ /// Take the current input, clear the box, return the text
+ pub fn take_input(&mut self) -> String {
+ self.cursor = 0;
+ std::mem::take(&mut self.input)
+ }
+
+ /// Sort and deduplicate the member list
+ pub fn sort_members(&mut self) {
+ self.members.sort_by(|a, b| {
+ // Strip sigils for sorting (@, +, %)
+ let a = a.trim_start_matches(&['@', '+', '%', '~', '&'][..]);
+ let b = b.trim_start_matches(&['@', '+', '%', '~', '&'][..]);
+ a.to_lowercase().cmp(&b.to_lowercase())
+ });
+ self.members.dedup();
+ }
+}
diff --git a/src/tui/mod.rs b/src/tui/mod.rs
new file mode 100644
index 0000000..3e2facd
--- /dev/null
+++ b/src/tui/mod.rs
@@ -0,0 +1,2 @@
+pub mod app;
+pub mod ui;
diff --git a/src/tui/ui.rs b/src/tui/ui.rs
new file mode 100644
index 0000000..a38834a
--- /dev/null
+++ b/src/tui/ui.rs
@@ -0,0 +1,252 @@
+use ratatui::{
+ Frame,
+ layout::{Constraint, Direction, Layout, Rect},
+ style::{Color, Modifier, Style},
+ text::{Line, Span, Text},
+ widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
+};
+
+use super::app::AppState;
+
+// ── Colour palette ────────────────────────────────────────────────────────────
+// Dark terminal aesthetic: near-black background, cool grey chrome,
+// amber accent for our own nick, cyan for others, muted green for system.
+
+const BG: Color = Color::Rgb(16, 16, 16);
+const ORANGE: Color = Color::Rgb(251, 84, 43); // border / panel bg
+const FG: Color = Color::Rgb(204, 204, 204); // main foreground
+const GRAY: Color = Color::Rgb(74, 74, 74); // other nicks
+const LIGHT_ORANGE: Color = Color::Rgb(255, 122, 89); // system messages
+
+pub fn draw(f: &mut Frame, state: &AppState) {
+ let area = f.area();
+
+ // Fill background
+ f.render_widget(Block::default().style(Style::default().bg(BG)), area);
+
+ // ── Outer layout: title bar + body + status bar ───────────────────────
+ let outer = Layout::default()
+ .direction(Direction::Vertical)
+ .constraints([
+ Constraint::Length(1), // title bar
+ Constraint::Min(0), // body
+ Constraint::Length(1), // status bar
+ ])
+ .split(area);
+
+ draw_titlebar(f, outer[0], state);
+ draw_body(f, outer[1], state);
+ draw_statusbar(f, outer[2], state);
+}
+
+fn draw_titlebar(f: &mut Frame, area: Rect, state: &AppState) {
+ let title = Line::from(vec![
+ Span::styled(
+ "  ",
+ Style::default().fg(ORANGE).add_modifier(Modifier::BOLD),
+ ),
+ Span::styled(
+ "speakez",
+ Style::default().fg(FG).add_modifier(Modifier::BOLD),
+ ),
+ Span::styled(" │ ", Style::default().fg(FG)),
+ Span::styled(
+ &state.channel,
+ Style::default().fg(FG).add_modifier(Modifier::BOLD),
+ ),
+ Span::styled(" │ ", Style::default().fg(FG)),
+ Span::styled(&state.nick, Style::default().fg(ORANGE)),
+ ]);
+
+ f.render_widget(Paragraph::new(title).style(Style::default()), area);
+}
+
+fn draw_body(f: &mut Frame, area: Rect, state: &AppState) {
+ // Body: [chat (fill)] | [members (18)]
+ let cols = Layout::default()
+ .direction(Direction::Horizontal)
+ .constraints([
+ Constraint::Min(0), // centre: chat log + input
+ Constraint::Length(18), // right: member list
+ ])
+ .split(area);
+
+ draw_center(f, cols[0], state);
+ draw_members_panel(f, cols[1], state);
+}
+
+fn draw_center(f: &mut Frame, area: Rect, state: &AppState) {
+ // Centre column: chat log on top, input box on bottom
+ let rows = Layout::default()
+ .direction(Direction::Vertical)
+ .constraints([
+ Constraint::Min(0), // chat log
+ Constraint::Length(3), // input box
+ ])
+ .split(area);
+
+ draw_chat_log(f, rows[0], state);
+ draw_input(f, rows[1], state);
+}
+
+fn draw_chat_log(f: &mut Frame, area: Rect, state: &AppState) {
+ let inner_height = area.height.saturating_sub(2) as usize; // subtract borders
+
+ // Build all rendered lines first so we can scroll from the bottom
+ let lines: Vec<Line> = state
+ .messages
+ .iter()
+ .map(|msg| render_chat_line(msg))
+ .collect();
+
+ // Apply scroll offset
+ // scroll=0 means pinned to bottom (newest). scroll=N means N lines back from bottom.
+ let total = lines.len();
+ let max_scroll = total.saturating_sub(inner_height);
+ let scroll = state.scroll.min(max_scroll);
+ let visible_start = total.saturating_sub(inner_height + scroll);
+ let visible: Vec<Line> = lines
+ .into_iter()
+ .skip(visible_start)
+ .take(inner_height)
+ .collect();
+
+ let block = Block::default()
+ .borders(Borders::ALL)
+ .border_type(BorderType::Plain)
+ .border_style(Style::default().fg(ORANGE))
+ .style(Style::default().bg(BG));
+
+ f.render_widget(
+ Paragraph::new(Text::from(visible))
+ .block(block)
+ .wrap(Wrap { trim: false }),
+ area,
+ );
+}
+
+fn render_chat_line(msg: &super::app::ChatLine) -> Line<'static> {
+ if msg.is_system {
+ return Line::from(Span::styled(
+ format!(" ∙ {}", msg.text),
+ Style::default()
+ .fg(LIGHT_ORANGE)
+ .add_modifier(Modifier::DIM),
+ ));
+ }
+
+ let nick_style = if msg.is_self {
+ Style::default().fg(ORANGE).add_modifier(Modifier::BOLD)
+ } else if msg.is_notice {
+ Style::default().fg(ORANGE)
+ } else {
+ Style::default().fg(GRAY)
+ };
+
+ let nick = format!("{}", msg.nick);
+
+ Line::from(vec![
+ Span::styled(nick, nick_style),
+ Span::styled(" ", Style::default()),
+ Span::styled(msg.text.clone(), Style::default().fg(FG)),
+ ])
+}
+
+fn draw_input(f: &mut Frame, area: Rect, state: &AppState) {
+ // Show a blinking cursor indicator at the cursor position
+ let before = &state.input[..state.cursor];
+ let after = &state.input[state.cursor..];
+
+ let cursor_char = if after.is_empty() {
+ " "
+ } else {
+ &after[..after.chars().next().map(|c| c.len_utf8()).unwrap_or(1)]
+ };
+ let after_cursor = if after.is_empty() {
+ ""
+ } else {
+ &after[cursor_char.len()..]
+ };
+
+ let line = Line::from(vec![
+ Span::styled(before.to_string(), Style::default().fg(FG)),
+ Span::styled(
+ cursor_char.to_string(),
+ Style::default().bg(FG).fg(BG).add_modifier(Modifier::BOLD),
+ ),
+ Span::styled(after_cursor.to_string(), Style::default().fg(FG)),
+ ]);
+
+ let block = Block::default()
+ .borders(Borders::ALL)
+ .border_type(BorderType::Plain)
+ .border_style(Style::default().fg(ORANGE))
+ .title(Span::styled(" send ", Style::default().fg(FG)));
+
+ f.render_widget(Paragraph::new(line).block(block), area);
+}
+
+fn draw_members_panel(f: &mut Frame, area: Rect, state: &AppState) {
+ let items: Vec<ListItem> = state
+ .members
+ .iter()
+ .map(|nick| {
+ // Highlight ops (@) differently
+ let (sigil, rest) = if nick.starts_with('@') {
+ ("@", &nick[1..])
+ } else if nick.starts_with('+') {
+ ("+", &nick[1..])
+ } else {
+ ("", nick.as_str())
+ };
+
+ let sigil_style = if sigil == "@" {
+ Style::default().fg(ORANGE)
+ } else {
+ Style::default().fg(FG)
+ };
+
+ ListItem::new(Line::from(vec![
+ Span::styled(sigil.to_string(), sigil_style),
+ Span::styled(rest.to_string(), Style::default().fg(FG)),
+ ]))
+ })
+ .collect();
+
+ let title = format!(" users ({}) ", state.members.len());
+ let block = panel_block(&title);
+
+ f.render_widget(List::new(items).block(block), area);
+}
+
+fn draw_statusbar(f: &mut Frame, area: Rect, state: &AppState) {
+ let (status_text, status_style) = if state.connected {
+ ("● connected", Style::default().fg(LIGHT_ORANGE))
+ } else {
+ ("○ connecting…", Style::default().fg(GRAY))
+ };
+
+ let line = Line::from(vec![
+ Span::styled(" ", Style::default()),
+ Span::styled(status_text, status_style),
+ Span::styled(" │ ", Style::default().fg(FG)),
+ Span::styled(&state.status, Style::default().fg(FG)),
+ Span::styled(" │ ", Style::default().fg(FG)),
+ Span::styled("PgUp/Dn scroll Ctrl-C quit", Style::default().fg(FG)),
+ ]);
+
+ f.render_widget(Paragraph::new(line).style(Style::default()), area);
+}
+
+/// Consistent panel block style
+fn panel_block(title: &str) -> Block<'static> {
+ Block::default()
+ .borders(Borders::ALL)
+ .border_type(BorderType::Plain)
+ .border_style(Style::default().fg(ORANGE))
+ .title(Span::styled(
+ format!(" {} ", title),
+ Style::default().fg(FG),
+ ))
+ .style(Style::default().bg(BG))
+}