Repository navigation
Expand file tree
/
Copy pathopen.go
More file actions
209 lines (190 loc) · 7.52 KB
/
Copy pathopen.go
File metadata and controls
209 lines (190 loc) · 7.52 KB
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
package main
// open.go implements the cliamp:// protocol handler.
//
// `cliamp open <uri>` is what a desktop environment runs when someone clicks
// a cliamp:// link. It is the only entry point that acts on a string chosen
// by a web page, so it does two things carefully:
//
// - Every URI goes through deeplink.Parse first, which validates the whole
// grammar and returns a closed struct. Nothing from the URI is ever used
// as an IPC operation name.
// - dispatch maps the parsed action onto a fixed set of operations written
// out longhand below. Adding a reachable operation requires editing this
// file, which is the point.
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/urfave/cli/v3"
"github.com/bjarneo/cliamp/applog"
"github.com/bjarneo/cliamp/config"
"github.com/bjarneo/cliamp/internal/deeplink"
"github.com/bjarneo/cliamp/ipc"
)
// deepLinkStartupTimeout bounds how long a cold start waits for its own IPC
// server to come up before giving up on the requested action. The player is
// already running by then, so exceeding it costs the action, not the session.
const deepLinkStartupTimeout = 15 * time.Second
func openCommand() *cli.Command {
return &cli.Command{
Name: "open",
Usage: "handle a cliamp:// link",
ArgsUsage: "<cliamp://...>",
Description: "Plays or queues the target of a cliamp:// URI. Registered as the\n" +
"system handler for the scheme by `cliamp protocol register`.\n\n" +
" cliamp open 'cliamp://play?url=https://example.com/stream.mp3'\n" +
" cliamp open 'cliamp://play?provider=navidrome&album=a1b2c3'\n" +
" cliamp open 'cliamp://queue?provider=ytmusic&q=aphex+twin'\n\n" +
"When cliamp is already running the action is sent over IPC and the\n" +
"command exits. Otherwise it starts the player in this terminal and\n" +
"performs the action once it is up.",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp open 'cliamp://play?url=https://example.com/stream.mp3'")
}
return openDeepLink(ctx, c.Args().First())
},
}
}
// openDeepLink parses uri and performs it, starting the player when nothing
// is listening on the IPC socket.
func openDeepLink(ctx context.Context, uri string) error {
action, err := deeplink.Parse(uri)
if err != nil {
return err
}
if err := validateDeepLink(action); err != nil {
return err
}
if ipcRunning() {
return dispatchDeepLink(action, ipcSendWithin)
}
return coldStartDeepLink(action)
}
func validateDeepLink(action deeplink.Action) error {
if action.Verb != deeplink.Play {
switch action.Target {
case deeplink.TargetAlbum:
return errors.New("queueing a provider album is not supported; use cliamp://play")
case deeplink.TargetPlaylist:
return errors.New("queueing a provider playlist is not supported; use cliamp://play")
}
}
return nil
}
// ipcRunning reports whether an instance is listening. It probes rather than
// inferring from a failed dispatch, so a genuine operation error is never
// mistaken for "not running" and answered by starting a second player.
func ipcRunning() bool {
_, err := ipc.SendV2(ipc.DefaultSocketPath(), ipc.V2Request{
ID: json.RawMessage(cliRequestID),
Method: "capabilities",
})
return !errors.Is(err, ipc.ErrNotRunning)
}
// coldStartDeepLink runs the player in this process and performs the action
// once its IPC server is accepting connections.
//
// Dispatching to our own socket rather than translating the action into
// startup arguments keeps one code path for every target. Provider albums and
// playlists have no command-line spelling at all, so the alternative would be
// a second, less capable implementation reachable only on a cold start.
func coldStartDeepLink(action deeplink.Action) error {
// Failures go to applog rather than stderr: the TUI owns the terminal by
// the time this runs, and writing to stderr would scribble over it.
// UserError both records the failure and surfaces it in the footer.
go func() {
if !waitForIPC(deepLinkStartupTimeout) {
applog.UserError("cliamp:// %s timed out waiting for the player to start", action.Verb)
return
}
if err := dispatchDeepLink(action, ipcSendWithin); err != nil {
applog.UserError("cliamp:// %s failed: %v", action.Verb, err)
}
}()
return run(config.Overrides{NoWelcome: true}, nil, false)
}
// waitForIPC polls until the socket accepts a request or timeout elapses.
func waitForIPC(timeout time.Duration) bool {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if ipcRunning() {
return true
}
time.Sleep(100 * time.Millisecond)
}
return false
}
// ipcSender submits one operation and waits up to timeout for its result.
// ipcSendWithin is the production sender. Tests pass a recorder.
type ipcSender func(operation string, params ipc.Request, timeout time.Duration) (ipc.Response, error)
// dispatchDeepLink maps a validated action onto IPC operations. The operation
// names are literals here and never come from the URI.
func dispatchDeepLink(action deeplink.Action, send ipcSender) error {
play := action.Verb == deeplink.Play
switch action.Target {
case deeplink.TargetURL:
// url.load routes through resolve, which admits only http and https
// and drops non-http entries from remote playlists. That is the same
// path `cliamp queue` uses, so a link cannot reach anything typing a
// URL could not.
_, err := send("url.load", ipc.Request{Path: action.URL, Play: play}, ipcLoadWait)
return err
case deeplink.TargetAlbum:
if !play {
return errors.New("queueing a provider album is not supported; use cliamp://play")
}
_, err := send("provider.load_album", ipc.Request{
Provider: action.Provider,
Album: action.Album,
}, ipcLoadWait)
return err
case deeplink.TargetPlaylist:
if !play {
return errors.New("queueing a provider playlist is not supported; use cliamp://play")
}
_, err := send("provider.load", ipc.Request{
Provider: action.Provider,
Playlist: action.Playlist,
}, ipcLoadWait)
return err
case deeplink.TargetSearch:
return dispatchDeepLinkSearch(action, play, send)
}
return fmt.Errorf("unsupported cliamp:// target")
}
// dispatchDeepLinkSearch plays or queues a provider's top match for the query.
//
// The track handed to track.play comes from the provider's own search
// response, not from the URI. Providers are services the user configured and
// authenticated against, so their results carry the same trust as browsing
// them in the TUI; only the query text originates with the link, and it is
// used solely as a search term.
func dispatchDeepLinkSearch(action deeplink.Action, play bool, send ipcSender) error {
response, err := send("provider.search", ipc.Request{
Provider: action.Provider,
Query: action.Query,
Limit: 1,
}, 60*time.Second)
if err != nil {
return err
}
if len(response.Tracks) == 0 {
return fmt.Errorf("%s has no match for %q", action.Provider, action.Query)
}
// track.play hands the path straight to playback, bypassing resolve, so
// the allowlist is applied here instead. The radio directory is public and
// its entries are filtered at ingest; this keeps the guarantee even for a
// provider added later that forwards a URL it did not construct.
if path := response.Tracks[0].Path; !deeplink.AllowsTrackPath(path) {
return fmt.Errorf("%s returned a result a cliamp:// link may not play: %q", action.Provider, path)
}
operation := "track.queue"
if play {
operation = "track.play"
}
_, err = send(operation, ipc.Request{Track: &response.Tracks[0]}, ipcWait)
return err
}