atradio 0.3.0

atradio.fm in your terminal — a TUI radio player on the AT Protocol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Command-line surface. With no subcommand, the interactive TUI launches.

use anyhow::Result;
use clap::{Parser, Subcommand};

use crate::appview::AppView;
use crate::atproto::Atproto;
use crate::config::Config;
use crate::radio::RadioBrowser;
use crate::theme;

// The `service` subcommand is compiled in only where we know how to manage a
// background service, and dispatches to the matching init-system backend:
// systemd on Linux, rc.d on FreeBSD/NetBSD. Other platforms compile it out.
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
use crate::rcd as service_impl;
#[cfg(target_os = "linux")]
use crate::systemd as service_impl;

#[derive(Parser)]
#[command(
    name = "atradio",
    version,
    about = "atradio.fm in your terminal — a synthwave radio player on the AT Protocol",
    long_about = "atradio.fm in your terminal.\n\nRun with no arguments to open the interactive TUI. \
Credentials for posting are read from ATPROTO_IDENTIFIER and ATPROTO_APP_PASSWORD, \
or run `atradio login`.",
    styles = theme::clap_styles(),
    arg_required_else_help = false
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Command>,

    /// Run headless as an atradio Connect device (no TUI): stay online and let
    /// the web app or other clients control playback. Waits until Ctrl-C.
    #[arg(long, global = true)]
    pub no_tui: bool,
}

#[derive(Subcommand)]
pub enum Command {
    /// Launch the interactive TUI (default when no command is given).
    Tui,

    /// Search stations on radio-browser and print the results.
    Search {
        /// Free-text query (station name).
        query: Vec<String>,
        /// Max results.
        #[arg(short, long, default_value_t = 20)]
        limit: u32,
    },

    /// Play a station stream URL directly (headless, no TUI).
    Play {
        /// A stream URL, or a radio-browser search query to play the top hit.
        target: Vec<String>,
    },

    /// Show trending / recent stations from the AppView.
    Trending {
        #[arg(short, long, default_value_t = 20)]
        limit: u32,
    },

    /// Sign in so you can favorite and comment.
    ///
    /// Defaults to an app-password login (set ATPROTO_APP_PASSWORD), which stays
    /// signed in the longest — recommended for a long-running `daemon`. Pass
    /// `--oauth` for the interactive browser flow instead.
    Login {
        /// Handle, DID, or PDS URL (optional; falls back to env / prompt).
        identifier: Option<String>,
        /// Use the browser OAuth flow instead of an app password. Convenient for
        /// interactive use, but its session expires sooner than an app password.
        #[arg(long)]
        oauth: bool,
    },

    /// Sign out (forget the stored session).
    Logout,

    /// Show the currently signed-in account.
    Whoami,

    /// Push your local audio settings (EQ + DSP chain) to your PDS.
    ///
    /// Uploads the settings from `settings.toml` to the `fm.atradio.audio.settings`
    /// record so they sync to the web app and your other devices. Requires sign-in.
    Push,

    /// Pull your audio settings (EQ + DSP chain) from your PDS.
    ///
    /// Downloads the `fm.atradio.audio.settings` record and writes it into your
    /// local `settings.toml`, replacing the local DSP chain. Requires sign-in.
    Pull,

    /// Manage a background service that runs atradio Connect headless.
    ///
    /// Installs `atradio --no-tui` under the platform's init system — systemd
    /// (`systemctl --user`) on Linux, rc.d on FreeBSD/NetBSD — so the device
    /// stays online across logout/reboot. Not available on macOS or elsewhere.
    #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
    Service {
        #[command(subcommand)]
        action: ServiceAction,
    },
}

/// Subcommands for `atradio service` (Linux / FreeBSD / NetBSD).
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
#[derive(Subcommand)]
pub enum ServiceAction {
    /// Install the service and start it.
    Install,
    /// Show the service status.
    Status,
    /// Stop, disable, and remove the service.
    Uninstall,
}

pub async fn run(cli: Cli) -> Result<()> {
    let config = Config::from_env();
    let no_tui = cli.no_tui;

    match cli.command.unwrap_or(Command::Tui) {
        Command::Tui => {
            if no_tui {
                cmd_daemon(config).await
            } else {
                crate::tui::run(config).await
            }
        }
        Command::Search { query, limit } => cmd_search(query.join(" "), limit).await,
        Command::Play { target } => cmd_play(target.join(" "), config).await,
        Command::Trending { limit } => cmd_trending(limit, &config).await,
        Command::Login { identifier, oauth } => cmd_login(identifier, oauth, &config).await,
        Command::Logout => cmd_logout(&config),
        Command::Whoami => cmd_whoami(&config),
        Command::Push => cmd_push(&config).await,
        Command::Pull => cmd_pull(&config).await,
        #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
        Command::Service { action } => match action {
            ServiceAction::Install => service_impl::install(),
            ServiceAction::Status => service_impl::status(),
            ServiceAction::Uninstall => service_impl::uninstall(),
        },
    }
}

async fn cmd_search(query: String, limit: u32) -> Result<()> {
    if query.trim().is_empty() {
        anyhow::bail!("provide a search query, e.g. `atradio search lofi`");
    }
    let rb = RadioBrowser::new();
    let results = rb.search(&query, limit).await?;
    if results.is_empty() {
        println!("No stations found for “{query}”.");
        return Ok(());
    }
    for (i, s) in results.iter().enumerate() {
        let sub = s.subtitle();
        println!(
            "{:>2}. {}{}",
            i + 1,
            s.name,
            if sub.is_empty() {
                String::new()
            } else {
                format!("  ({sub})")
            }
        );
        println!("    {}", s.stream_url);
    }
    Ok(())
}

async fn cmd_trending(limit: u32, config: &Config) -> Result<()> {
    let av = AppView::new(&config.appview_url);
    let items = av.recent_stations(limit).await?;
    if items.is_empty() {
        println!("Nothing trending right now.");
        return Ok(());
    }
    for (i, v) in items.iter().enumerate() {
        println!("{:>2}. {}", i + 1, v.station.name);
    }
    Ok(())
}

async fn cmd_play(target: String, _config: Config) -> Result<()> {
    if target.trim().is_empty() {
        anyhow::bail!("provide a stream URL or search query");
    }
    // Resolve the URL: use it directly if it looks like one, else search.
    let url = if target.starts_with("http://") || target.starts_with("https://") {
        target.clone()
    } else {
        let rb = RadioBrowser::new();
        let hits = rb.search(&target, 1).await?;
        let s = hits
            .into_iter()
            .next()
            .ok_or_else(|| anyhow::anyhow!("no station found for “{target}”"))?;
        println!("{}", s.name);
        s.stream_url
    };

    let player = std::sync::Arc::new(crate::player::Player::new()?);

    // MPRIS (Linux): snapshots out over a watch channel, transport commands
    // back over an mpsc — the engine handle itself is !Send.
    #[cfg(target_os = "linux")]
    let (mpris_np_tx, mut mpris_cmd_rx) = {
        let (tx, rx) = tokio::sync::watch::channel(crate::player::NowPlaying::default());
        (tx, crate::mpris::spawn(rx))
    };

    // Resolve playlists (TuneIn/.pls/.m3u) into a direct stream the engine can
    // decode; source is unknown for a raw URL, so rely on the extension.
    let resolved = crate::radio::resolve_stream(&url, "").await;
    if resolved.is_hls {
        anyhow::bail!("HLS streams (.m3u8) aren't playable from the CLI yet: {url}");
    }
    player.play_url(&resolved.url);
    println!("Playing {}\nPress Ctrl-C to stop.", resolved.url);

    // Poll and print now-playing until interrupted.
    let mut last = String::new();
    loop {
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
        let np = player.now_playing();

        #[cfg(target_os = "linux")]
        {
            let _ = mpris_np_tx.send(np.clone());
            while let Ok(cmd) = mpris_cmd_rx.try_recv() {
                use crate::player::MprisCmd;
                match cmd {
                    MprisCmd::Play => player.play(),
                    MprisCmd::Pause => player.pause(),
                    MprisCmd::PlayPause => player.toggle(),
                    MprisCmd::Stop => player.stop(),
                    MprisCmd::SetVolume(v) => player.set_volume(v),
                }
            }
        }

        if let Some(line) = np.line() {
            if line != last {
                println!("{line}");
                last = line;
            }
        }
    }
}

/// Headless atradio Connect device: no TUI, just an online controllable player.
async fn cmd_daemon(config: Config) -> Result<()> {
    use std::sync::Arc;
    use std::time::Duration;

    use crate::appview::StationInfo;
    use crate::player::State as PlayState;
    use crate::remote::{RemoteCmd, RemoteConfig, RemoteEvent, StationLite, WireState};

    let atproto = Arc::new(Atproto::new(config.session_path.clone()));
    if !atproto.is_logged_in() {
        anyhow::bail!("atradio Connect requires sign-in — run `atradio login` first");
    }
    // Daemons run unattended for a long time. OAuth refresh tokens are short-
    // lived, so nudge toward an app-password session, which stays signed in far
    // longer (and refreshes silently). OAuth still works — it just may need the
    // occasional re-login.
    if atproto.is_oauth_session() {
        println!(
            "ℹ signed in via OAuth — for an unattended daemon, an app-password login \
             (`atradio login <handle>` with ATPROTO_APP_PASSWORD) lasts much longer."
        );
    }

    let settings = crate::settings::Settings::load(&config.session_path);
    let player = Arc::new(crate::player::Player::new()?);
    player.set_volume(settings.volume);
    player.apply_dsp(&settings.audio());

    let device_id = crate::remote::load_or_create_device_id(&config.session_path);
    let device_name = settings
        .device_name
        .clone()
        .filter(|s| !s.trim().is_empty())
        .unwrap_or_else(crate::remote::default_device_name);
    let (state_tx, state_rx) = tokio::sync::watch::channel(WireState::default());
    let mut remote = crate::remote::spawn(
        RemoteConfig {
            base_url: config.appview_url.clone(),
            device_id,
            device_name: device_name.clone(),
            atproto: atproto.clone(),
        },
        state_rx,
    );

    println!("atradio Connect device “{device_name}” is online.");
    println!("Control it from the web app or another client. Press Ctrl-C to stop.");

    let mut current: Option<StationLite> = None;
    let mut ticker = tokio::time::interval(Duration::from_millis(500));
    loop {
        tokio::select! {
            _ = tokio::signal::ctrl_c() => break,
            Some(cmd) = remote.cmd_rx.recv() => match cmd {
                RemoteCmd::Play => player.play(),
                RemoteCmd::Pause => player.pause(),
                RemoteCmd::PlayPause => player.toggle(),
                RemoteCmd::Stop => { player.stop(); current = None; }
                RemoteCmd::SetVolume(v) => player.set_volume(v),
                RemoteCmd::ToggleMute => player.toggle_mute(),
                RemoteCmd::LoadStation(s) => {
                    let source = if s.id.starts_with("tunein:") {
                        "tunein"
                    } else if s.id.starts_with("custom:") {
                        "custom"
                    } else {
                        "radio-browser"
                    };
                    // Unwrap playlists into a direct stream before playing.
                    let resolved = crate::radio::resolve_stream(&s.url, source).await;
                    if resolved.is_hls {
                        println!("{} — HLS not supported, skipping", s.name);
                        continue;
                    }
                    player.play_url(&resolved.url);
                    println!("{}", s.name);
                    let station = StationInfo {
                        station_id: s.id.clone(),
                        name: s.name.clone(),
                        stream_url: s.url.clone(),
                        source: source.to_string(),
                        logo: s.favicon.clone(),
                        ..Default::default()
                    };
                    current = Some(s);
                    let at = atproto.clone();
                    tokio::spawn(async move { let _ = at.set_play_status(&station).await; });
                }
            },
            Some(evt) = remote.evt_rx.recv() => match evt {
                RemoteEvent::Status(online) => {
                    println!("{}", if online { "● connected" } else { "○ disconnected — retrying" });
                }
                RemoteEvent::Presence { cleanup, .. } => {
                    if cleanup {
                        let at = atproto.clone();
                        tokio::spawn(async move { let _ = at.delete_play_status().await; });
                    }
                }
                RemoteEvent::AuthExpired(detail) => {
                    println!("○ couldn't authenticate the Connect device: {detail}");
                    println!(
                        "  run `atradio login` to reconnect \
                         (an app-password login lasts longer for a daemon)"
                    );
                }
                _ => {}
            },
            _ = ticker.tick() => {
                let np = player.now_playing();
                let _ = state_tx.send(WireState {
                    playing: matches!(np.state, PlayState::Playing),
                    station: current.clone(),
                    title: np.line(),
                    volume: player.volume(),
                    muted: player.is_muted(),
                });
            }
        }
    }

    println!("\nStopping atradio Connect device.");
    Ok(())
}

async fn cmd_login(identifier: Option<String>, oauth: bool, config: &Config) -> Result<()> {
    let at = Atproto::new(config.session_path.clone());

    let profile = if oauth {
        let hint = identifier.or_else(|| config.identifier.clone());
        at.login_oauth(hint.as_deref()).await?
    } else {
        let id = identifier
            .or_else(|| config.identifier.clone())
            .ok_or_else(|| {
                anyhow::anyhow!("provide a handle/DID (arg or ATPROTO_IDENTIFIER), or use --oauth")
            })?;
        let pw = config.app_password.clone().ok_or_else(|| {
            anyhow::anyhow!(
                "set ATPROTO_APP_PASSWORD to sign in with an app password, or use --oauth"
            )
        })?;
        at.login_password(&id, &pw).await?
    };

    println!("✓ Signed in as @{} ({})", profile.handle, profile.did);
    Ok(())
}

fn cmd_logout(config: &Config) -> Result<()> {
    let at = Atproto::new(config.session_path.clone());
    if at.is_logged_in() {
        at.logout();
        println!("Signed out.");
    } else {
        println!("Not signed in.");
    }
    Ok(())
}

async fn cmd_push(config: &Config) -> Result<()> {
    let atproto = Atproto::new(config.session_path.clone());
    if !atproto.is_logged_in() {
        anyhow::bail!("pushing audio settings requires sign-in — run `atradio login` first");
    }
    let settings = crate::settings::Settings::load(&config.session_path);
    atproto.put_audio_settings(&settings.audio()).await?;
    println!("✓ Pushed your audio settings to your PDS.");
    Ok(())
}

async fn cmd_pull(config: &Config) -> Result<()> {
    let atproto = Atproto::new(config.session_path.clone());
    if !atproto.is_logged_in() {
        anyhow::bail!("pulling audio settings requires sign-in — run `atradio login` first");
    }
    match atproto.get_audio_settings().await? {
        Some(remote) => {
            let mut settings = crate::settings::Settings::load(&config.session_path);
            // Keep the local volume; only the DSP chain lives in the record.
            settings.update_from(&remote, settings.volume);
            settings.save(&config.session_path);
            println!("✓ Pulled your audio settings into settings.toml.");
        }
        None => println!("No audio settings record found in your PDS."),
    }
    Ok(())
}

fn cmd_whoami(config: &Config) -> Result<()> {
    let at = Atproto::new(config.session_path.clone());
    match at.profile() {
        Some(p) => {
            println!("@{}", p.handle);
            println!("  did:    {}", p.did);
            println!(
                "  via:    {}",
                if p.method.is_empty() {
                    "password"
                } else {
                    &p.method
                }
            );
            if let Some(pds) = p.pds {
                println!("  pds:    {pds}");
            }
        }
        None => println!("Not signed in. Run `atradio login`."),
    }
    Ok(())
}