feat(cli)!: replace the one-shot commands with a scriptable surface

The one-shot half of the binary could not finish its headline task without a
human at the keyboard: `search` opened a full-screen picker, panicked with
exit 101 when there was no terminal, and no command could fetch a known file
at all. Everything exited 0 whatever happened, wrote emoji prose to stdout,
and ignored --download-dir, SOULSEEK_DOWNLOAD_DIR and config.toml.

The commands are now built for scripts and agents:

  search  download  get  browse  room list|say|listen  message send|read

stdout carries records only (tab-separated, or NDJSON under --json), stderr
carries progress, and the exit code carries the verdict: 2 usage, 3 connect,
4 no results, 5 timed out, 6 transfer failed. `get` does search, rank and
fetch in one step, and `search --json | jq ... | download --stdin` composes.
Global flags parse in any position and every one has a SOULSEEK_ variable and
a config.toml key resolved through a single precedence chain.

Defects found and fixed along the way, each covered by a test:

- a download with an unknown size was finalized against zero bytes and saved
  as an empty file; the size now comes from the peer's share listing
- transfers are keyed by a hash of the remote path, so the same path from two
  peers shadowed itself and two files sharing a basename overwrote each
  other; both collisions are now dropped with a warning
- `message send` and `room say` slept 500ms and claimed success; they now
  wait for a server reply queued behind the message before reporting
- Client::login waited on an unbounded channel and never returned when the
  server went quiet

Deleted the TUI picker the old `search` drove (download_selector,
MultiDownloadProgress, SearchConfig): 1,573 lines out, 926 in. The
interactive TUI is otherwise untouched.

BREAKING CHANGE: `rooms` and `chat` become `room list|say|listen` and
`message send|read`; `--disable-listener` becomes `--no-listener`; the
unprefixed DISABLE_LISTENER, LISTENER_PORT, LOG_FILE and
MAX_CONCURRENT_DOWNLOADS variables are now SOULSEEK_-prefixed; `search` no
longer opens a picker or downloads anything.
This commit is contained in:
Michel de Graaf
2026-07-25 01:30:48 +02:00
parent c2dd227cee
commit be6581ee6c
17 changed files with 3756 additions and 1583 deletions
+297
View File
@@ -0,0 +1,297 @@
//! Talking to people and looking at what they share: `browse`, `room`, and
//! `message`.
use super::{Ctx, Session, confirm_flush};
use crate::cli::BrowseArgs;
use crate::output::{
BrowseRecord, CliError, CliResult, Out, PrivateMessageRecord,
RoomEventRecord, RoomRecord,
};
use soulseek_rs::types::RoomEvent;
use soulseek_rs::{Client, SharedDirectory};
use std::time::{Duration, Instant};
/// How long to wait for the server to acknowledge something we sent.
const FLUSH_TIMEOUT: Duration = Duration::from_secs(10);
/// Polling cadence for the request/poll parts of the client API.
const POLL: Duration = Duration::from_millis(100);
/// Join a listing's directory and file name into the remote path that
/// `download` expects — the protocol reports the two separately.
#[must_use]
pub fn full_path(directory: &str, basename: &str) -> String {
let directory = directory.trim_end_matches('\\');
if directory.is_empty() {
basename.to_string()
} else {
format!("{directory}\\{basename}")
}
}
/// Ask a peer for its share listing and wait for the answer.
pub fn fetch_listing(
client: &Client,
user: &str,
timeout: Duration,
) -> CliResult<Vec<SharedDirectory>> {
client.browse_user(user).map_err(|e| {
CliError::connection(format!("cannot ask {user} for a listing: {e}"))
})?;
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if let Some(directories) = client.take_browse_result(user) {
return Ok(directories);
}
std::thread::sleep(POLL);
}
Err(CliError::timeout(format!(
"{user} did not send a file list within {}s",
timeout.as_secs()
)))
}
pub fn browse(ctx: &Ctx, args: &BrowseArgs) -> CliResult {
let session = Session::open(ctx)?;
ctx.out
.status(&format!("requesting {}'s shared files…", args.user));
let directories = fetch_listing(
&session.client,
&args.user,
Duration::from_secs(args.timeout),
)?;
let mut files = 0usize;
for directory in &directories {
for (basename, size) in &directory.files {
files += 1;
ctx.out.emit(&BrowseRecord {
user: args.user.clone(),
directory: directory.name.clone(),
path: full_path(&directory.name, basename),
size: *size,
});
}
}
if files == 0 {
return Err(CliError::no_results(format!(
"{} shares no files",
args.user
)));
}
Ok(())
}
pub fn room_list(ctx: &Ctx, timeout: Duration) -> CliResult {
let session = Session::open(ctx)?;
ctx.out.status("fetching the room list…");
let Some(mut rooms) =
super::await_room_list(&session.client, timeout, POLL)
else {
return Err(CliError::timeout(format!(
"the server sent no room list within {}s",
timeout.as_secs()
)));
};
if rooms.is_empty() {
return Err(CliError::no_results("the server lists no public rooms"));
}
rooms.sort_by(|a, b| {
b.user_count
.cmp(&a.user_count)
.then_with(|| a.name.cmp(&b.name))
});
for room in rooms {
ctx.out.emit(&RoomRecord {
room: room.name,
users: room.user_count,
});
}
Ok(())
}
pub fn room_say(ctx: &Ctx, room: &str, message: &str) -> CliResult {
let session = Session::open(ctx)?;
session.client.join_room(room).map_err(|e| {
CliError::connection(format!("cannot join {room}: {e}"))
})?;
session.client.say_in_room(room, message).map_err(|e| {
CliError::connection(format!("cannot post to {room}: {e}"))
})?;
if confirm_flush(&session.client, FLUSH_TIMEOUT) {
ctx.out.status(&format!("posted to {room}"));
return Ok(());
}
Err(CliError::timeout(format!(
"the server did not acknowledge the message to {room}"
)))
}
pub fn room_listen(ctx: &Ctx, room: &str, span: Option<Duration>) -> CliResult {
let session = Session::open(ctx)?;
session.client.join_room(room).map_err(|e| {
CliError::connection(format!("cannot join {room}: {e}"))
})?;
stream(&ctx.out, &format!("listening to {room}"), span, || {
for event in session.client.take_room_events() {
if let Some(record) = room_event_record(room, &event) {
ctx.out.emit(&record);
}
}
});
let _ = session.client.leave_room(room);
Ok(())
}
/// Drain `next` until `span` elapses, or forever when it is `None`
/// (`--follow`, ended by the operator).
fn stream(
out: &Out,
what: &str,
span: Option<Duration>,
mut next: impl FnMut(),
) {
out.status(&match span {
Some(span) => format!("{what} for {}s…", span.as_secs()),
None => format!("{what} until interrupted…"),
});
let deadline = span.map(|span| Instant::now() + span);
while deadline.is_none_or(|deadline| Instant::now() < deadline) {
next();
std::thread::sleep(POLL);
}
}
/// Turn a room event into a record, ignoring events for other rooms and the
/// bookkeeping ones a listener has no use for.
fn room_event_record(room: &str, event: &RoomEvent) -> Option<RoomEventRecord> {
match event {
RoomEvent::Message {
room: name,
username,
message,
} if name == room => Some(RoomEventRecord {
kind: "message",
room: name.clone(),
user: username.clone(),
message: message.clone(),
}),
RoomEvent::UserJoined {
room: name,
username,
} if name == room => Some(RoomEventRecord {
kind: "join",
room: name.clone(),
user: username.clone(),
message: String::new(),
}),
RoomEvent::UserLeft {
room: name,
username,
} if name == room => Some(RoomEventRecord {
kind: "leave",
room: name.clone(),
user: username.clone(),
message: String::new(),
}),
_ => None,
}
}
pub fn message_send(ctx: &Ctx, user: &str, message: &str) -> CliResult {
let session = Session::open(ctx)?;
session
.client
.send_private_message(user, message)
.map_err(|e| {
CliError::connection(format!("cannot send to {user}: {e}"))
})?;
if confirm_flush(&session.client, FLUSH_TIMEOUT) {
ctx.out.status(&format!("sent to {user}"));
return Ok(());
}
Err(CliError::timeout(format!(
"the server did not acknowledge the message to {user}"
)))
}
pub fn message_read(ctx: &Ctx, span: Option<Duration>) -> CliResult {
let session = Session::open(ctx)?;
stream(&ctx.out, "reading private messages", span, || {
for message in session.client.take_private_messages() {
ctx.out.emit(&PrivateMessageRecord {
timestamp: message.timestamp(),
user: message.username().to_string(),
message: message.message().to_string(),
});
}
});
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn full_path_joins_a_directory_and_basename_with_a_backslash() {
assert_eq!(full_path("@@abc\\Music", "a.mp3"), "@@abc\\Music\\a.mp3");
}
#[test]
fn full_path_does_not_double_a_trailing_separator() {
assert_eq!(full_path("@@abc\\Music\\", "a.mp3"), "@@abc\\Music\\a.mp3");
}
#[test]
fn full_path_of_a_rootless_listing_is_the_basename() {
assert_eq!(full_path("", "a.mp3"), "a.mp3");
}
#[test]
fn only_events_for_the_listened_room_become_records() {
let mine = RoomEvent::Message {
room: "lobby".into(),
username: "bob".into(),
message: "hi".into(),
};
let elsewhere = RoomEvent::Message {
room: "other".into(),
username: "bob".into(),
message: "hi".into(),
};
assert!(room_event_record("lobby", &mine).is_some());
assert!(room_event_record("lobby", &elsewhere).is_none());
}
#[test]
fn joins_and_leaves_are_labelled() {
let joined = RoomEvent::UserJoined {
room: "lobby".into(),
username: "sue".into(),
};
let left = RoomEvent::UserLeft {
room: "lobby".into(),
username: "sue".into(),
};
assert_eq!(room_event_record("lobby", &joined).unwrap().kind, "join");
assert_eq!(room_event_record("lobby", &left).unwrap().kind, "leave");
}
#[test]
fn our_own_join_confirmation_is_not_a_record() {
let joined = RoomEvent::Joined {
room: "lobby".into(),
users: vec!["bob".into()],
};
assert!(room_event_record("lobby", &joined).is_none());
}
}