2 04e4e993 2023-08-14 op * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 04e4e993 2023-08-14 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 04e4e993 2023-08-14 op * Permission to use, copy, modify, and distribute this software for any
6 04e4e993 2023-08-14 op * purpose with or without fee is hereby granted, provided that the above
7 04e4e993 2023-08-14 op * copyright notice and this permission notice appear in all copies.
9 04e4e993 2023-08-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 04e4e993 2023-08-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 04e4e993 2023-08-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 04e4e993 2023-08-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 04e4e993 2023-08-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 04e4e993 2023-08-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 04e4e993 2023-08-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 04e4e993 2023-08-14 op #include "config.h"
20 04e4e993 2023-08-14 op #include <sys/socket.h>
21 04e4e993 2023-08-14 op #include <sys/types.h>
22 04e4e993 2023-08-14 op #include <sys/un.h>
24 04e4e993 2023-08-14 op #include <ctype.h>
25 04e4e993 2023-08-14 op #include <errno.h>
26 04e4e993 2023-08-14 op #include <fnmatch.h>
27 04e4e993 2023-08-14 op #include <limits.h>
28 04e4e993 2023-08-14 op #include <locale.h>
29 04e4e993 2023-08-14 op #include <netdb.h>
30 04e4e993 2023-08-14 op #include <poll.h>
31 04e4e993 2023-08-14 op #include <signal.h>
32 04e4e993 2023-08-14 op #include <stdio.h>
33 04e4e993 2023-08-14 op #include <stdlib.h>
34 04e4e993 2023-08-14 op #include <string.h>
35 04e4e993 2023-08-14 op #include <syslog.h>
36 04e4e993 2023-08-14 op #include <unistd.h>
38 04e4e993 2023-08-14 op #include "amused.h"
39 3634fa70 2023-08-31 op #include "bufio.h"
40 80d5f5ad 2023-08-25 op #include "ev.h"
41 04e4e993 2023-08-14 op #include "http.h"
42 04e4e993 2023-08-14 op #include "log.h"
43 04e4e993 2023-08-14 op #include "playlist.h"
44 b42d807f 2023-09-02 op #include "ws.h"
45 04e4e993 2023-08-14 op #include "xmalloc.h"
47 04e4e993 2023-08-14 op #ifndef nitems
48 04e4e993 2023-08-14 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
51 04e4e993 2023-08-14 op #define FORM_URLENCODED "application/x-www-form-urlencoded"
53 04e4e993 2023-08-14 op #define ICON_REPEAT_ALL "🔁"
54 04e4e993 2023-08-14 op #define ICON_REPEAT_ONE "🔂"
55 04e4e993 2023-08-14 op #define ICON_PREV "⏮"
56 04e4e993 2023-08-14 op #define ICON_NEXT "⏭"
57 04e4e993 2023-08-14 op #define ICON_STOP "⏹"
58 04e4e993 2023-08-14 op #define ICON_PAUSE "⏸"
59 04e4e993 2023-08-14 op #define ICON_TOGGLE "⏯"
60 04e4e993 2023-08-14 op #define ICON_PLAY "⏵"
62 b42d807f 2023-09-02 op static struct clthead clients;
63 50e0da0e 2024-01-21 op static struct imsgbuf imsgbuf;
64 20dff2d2 2023-08-31 op static struct playlist playlist_tmp;
65 20dff2d2 2023-08-31 op static struct player_status player_status;
66 20dff2d2 2023-08-31 op static uint64_t position, duration;
68 b42d807f 2023-09-02 op static void client_ev(int, int, void *);
70 04e4e993 2023-08-14 op const char *head = "<!doctype html>"
73 04e4e993 2023-08-14 op "<meta name='viewport' content='width=device-width, initial-scale=1'/>"
74 04e4e993 2023-08-14 op "<title>Amused Web</title>"
75 cc932200 2023-08-31 op "<link rel='stylesheet' href='/style.css?v=0'>"
80 cc932200 2023-08-31 op const char *css = "*{box-sizing:border-box}"
82 04e4e993 2023-08-14 op " padding: 0;"
87 04e4e993 2023-08-14 op " display: flex;"
88 04e4e993 2023-08-14 op " flex-direction: column;"
90 04e4e993 2023-08-14 op "button{cursor:pointer}"
92 04e4e993 2023-08-14 op " position: sticky;"
95 04e4e993 2023-08-14 op ".searchbox input{"
96 04e4e993 2023-08-14 op " width: 100%;"
97 04e4e993 2023-08-14 op " padding: 9px;"
99 04e4e993 2023-08-14 op ".playlist-wrapper{min-height:80vh}"
101 04e4e993 2023-08-14 op " list-style: none;"
102 04e4e993 2023-08-14 op " padding: 0;"
103 04e4e993 2023-08-14 op " margin: 0;"
105 04e4e993 2023-08-14 op ".playlist button{"
106 04e4e993 2023-08-14 op " font-family: monospace;"
107 04e4e993 2023-08-14 op " text-align: left;"
108 04e4e993 2023-08-14 op " width: 100%;"
109 04e4e993 2023-08-14 op " padding: 5px;"
110 04e4e993 2023-08-14 op " border: 0;"
111 04e4e993 2023-08-14 op " background: transparent;"
112 04e4e993 2023-08-14 op " transition: background-color .25s ease-in-out;"
114 04e4e993 2023-08-14 op ".playlist button::before{"
115 04e4e993 2023-08-14 op " content: \"\";"
116 04e4e993 2023-08-14 op " width: 2ch;"
117 04e4e993 2023-08-14 op " display: inline-block;"
119 04e4e993 2023-08-14 op ".playlist button:hover{"
120 04e4e993 2023-08-14 op " background-color: #dfdddd;"
122 04e4e993 2023-08-14 op ".playlist #current button{"
123 04e4e993 2023-08-14 op " font-weight: bold;"
125 04e4e993 2023-08-14 op ".playlist #current button::before{"
126 04e4e993 2023-08-14 op " content: \"→ \";"
127 04e4e993 2023-08-14 op " font-weight: bold;"
130 04e4e993 2023-08-14 op " position: sticky;"
131 04e4e993 2023-08-14 op " width: 100%;"
132 04e4e993 2023-08-14 op " max-width: 800px;"
133 04e4e993 2023-08-14 op " margin: 0 auto;"
134 04e4e993 2023-08-14 op " bottom: 0;"
135 04e4e993 2023-08-14 op " background-color: white;"
136 04e4e993 2023-08-14 op " background: #3d3d3d;"
137 04e4e993 2023-08-14 op " color: white;"
138 04e4e993 2023-08-14 op " border-radius: 10px 10px 0 0;"
139 04e4e993 2023-08-14 op " padding: 10px;"
140 04e4e993 2023-08-14 op " text-align: center;"
143 04e4e993 2023-08-14 op ".controls p{"
144 04e4e993 2023-08-14 op " margin: .4rem;"
146 04e4e993 2023-08-14 op ".controls a{"
147 04e4e993 2023-08-14 op " color: white;"
149 04e4e993 2023-08-14 op ".controls .status{"
150 04e4e993 2023-08-14 op " font-size: 0.9rem;"
152 04e4e993 2023-08-14 op ".controls button{"
153 04e4e993 2023-08-14 op " margin: 5px;"
154 04e4e993 2023-08-14 op " padding: 5px 20px;"
156 04e4e993 2023-08-14 op ".mode-active{"
157 04e4e993 2023-08-14 op " color: #0064ff;"
160 cc932200 2023-08-31 op const char *js =
162 b42d807f 2023-09-02 op "let pos=0, dur=0;"
163 b42d807f 2023-09-02 op "const playlist=document.querySelector('.playlist');"
164 04e4e993 2023-08-14 op "function cur(e) {"
165 04e4e993 2023-08-14 op " if (e) {e.preventDefault()}"
166 04e4e993 2023-08-14 op " let cur = document.querySelector('#current');"
167 04e4e993 2023-08-14 op " if (cur) {cur.scrollIntoView(); window.scrollBy(0, -100);}"
169 b42d807f 2023-09-02 op "function b(x){return x=='on'};"
170 b42d807f 2023-09-02 op "function c(p, c){"
171 b42d807f 2023-09-02 op " const l=document.createElement('li');"
172 b42d807f 2023-09-02 op " if(c){l.id='current'};"
173 b42d807f 2023-09-02 op " const b=document.createElement('button');"
174 b42d807f 2023-09-02 op " b.type='submit'; b.name='jump'; b.value=p;"
175 b42d807f 2023-09-02 op " b.innerText=p;"
176 b42d807f 2023-09-02 op " l.appendChild(b);"
177 b42d807f 2023-09-02 op " playlist.appendChild(l);"
179 b42d807f 2023-09-02 op "function d(t){"
180 b42d807f 2023-09-02 op " const [, type, payload] = t.split(/^(.):(.*)$/);"
181 b42d807f 2023-09-02 op " if (type=='s'){"
182 b42d807f 2023-09-02 op " let s=payload.split(' ');"
183 b42d807f 2023-09-02 op " pos=s[0], dur=s[1];"
184 b42d807f 2023-09-02 op " } else if (type=='S') {"
185 b42d807f 2023-09-02 op " const btn=document.querySelector('#toggle');"
186 b42d807f 2023-09-02 op " if (payload=='playing') {"
187 b42d807f 2023-09-02 op " btn.innerHTML='"ICON_PAUSE"';"
188 b42d807f 2023-09-02 op " btn.value='pause';"
190 b42d807f 2023-09-02 op " btn.innerHTML='"ICON_PLAY"';"
191 b42d807f 2023-09-02 op " btn.value='play';"
193 b42d807f 2023-09-02 op " } else if (type=='r') {"
194 b42d807f 2023-09-02 op " const btn=document.querySelector('#rone');"
195 b42d807f 2023-09-02 op " btn.className=b(payload)?'mode-active':'';"
196 b42d807f 2023-09-02 op " } else if (type=='R') {"
197 b42d807f 2023-09-02 op " const btn=document.querySelector('#rall');"
198 b42d807f 2023-09-02 op " btn.className=b(payload)?'mode-active':'';"
199 b42d807f 2023-09-02 op " } else if (type=='c') {"
200 b42d807f 2023-09-02 op /* consume */
201 b42d807f 2023-09-02 op " } else if (type=='x') {"
202 b42d807f 2023-09-02 op " playlist.innerHTML='';"
203 0e016c89 2023-09-07 op " } else if (type=='X') {"
204 0e016c89 2023-09-07 op " dofilt();" /* done with the list */
205 b42d807f 2023-09-02 op " } else if (type=='A') {"
206 b42d807f 2023-09-02 op " c(payload, true);"
207 b42d807f 2023-09-02 op " } else if (type=='a') {"
208 b42d807f 2023-09-02 op " c(payload, false);"
209 b42d807f 2023-09-02 op " } else if (type=='C') {"
210 b42d807f 2023-09-02 op " const t=document.querySelector('.controls>p>a');"
211 b42d807f 2023-09-02 op " t.innerText = payload.replace(/.*\\//, '');"
214 b42d807f 2023-09-02 op " console.log('unknown:',t);"
217 b42d807f 2023-09-02 op "function w(){"
218 b42d807f 2023-09-02 op " ws = new WebSocket((location.protocol=='http:'?'ws://':'wss://')"
219 b42d807f 2023-09-02 op " + location.host + '/ws');"
220 b42d807f 2023-09-02 op " ws.addEventListener('open', () => console.log('ws: connected'));"
221 ed3946c3 2023-09-07 op " ws.addEventListener('close', () => {"
222 ed3946c3 2023-09-07 op " alert('Websocket closed. The interface won\\'t update itself.'"
223 ed3946c3 2023-09-07 op " + ' Please refresh the page');"
225 b42d807f 2023-09-02 op " ws.addEventListener('message', e => d(e.data))"
229 b42d807f 2023-09-02 op "document.querySelector('.controls a').addEventListener('click',cur);"
230 b42d807f 2023-09-02 op "document.querySelectorAll('form').forEach(f => {"
231 b42d807f 2023-09-02 op " f.action='/a/'+f.getAttribute('action');"
232 b42d807f 2023-09-02 op " f.addEventListener('submit', e => {"
233 b42d807f 2023-09-02 op " e.preventDefault();"
234 b42d807f 2023-09-02 op " const fd = new FormData(f);"
235 b42d807f 2023-09-02 op " if (e.submitter && e.submitter.value && e.submitter.value != '')"
236 b42d807f 2023-09-02 op " fd.append(e.submitter.name, e.submitter.value);"
237 b42d807f 2023-09-02 op " fetch(f.action, {"
238 b42d807f 2023-09-02 op " method:'POST',"
239 b42d807f 2023-09-02 op " body: new URLSearchParams(fd)"
241 b42d807f 2023-09-02 op " .catch(x => console.log('failed to submit form:', x));"
244 0e016c89 2023-09-07 op "const sb = document.createElement('section');"
245 0e016c89 2023-09-07 op "sb.className = 'searchbox';"
246 0e016c89 2023-09-07 op "const filter = document.createElement('input');"
247 0e016c89 2023-09-07 op "filter.type = 'search';"
248 0e016c89 2023-09-07 op "filter.setAttribute('aria-label', 'Filter Playlist');"
249 0e016c89 2023-09-07 op "filter.placeholder = 'Filter Playlist';"
250 0e016c89 2023-09-07 op "sb.append(filter);"
251 0e016c89 2023-09-07 op "document.querySelector('main').prepend(sb);"
252 0e016c89 2023-09-07 op "function dofilt() {"
253 0e016c89 2023-09-07 op " let t = filter.value.toLowerCase();"
254 0e016c89 2023-09-07 op " document.querySelectorAll('.playlist li').forEach(e => {"
255 0e016c89 2023-09-07 op " if (e.querySelector('button').value.toLowerCase().indexOf(t) == -1)"
256 0e016c89 2023-09-07 op " e.setAttribute('hidden', 'true');"
258 0e016c89 2023-09-07 op " e.removeAttribute('hidden');"
261 0e016c89 2023-09-07 op "function dbc(fn, wait) {"
263 0e016c89 2023-09-07 op " return function() {"
264 0e016c89 2023-09-07 op " let later = () => {tout = null; fn()};"
265 0e016c89 2023-09-07 op " clearTimeout(tout);"
266 0e016c89 2023-09-07 op " if (!tout) fn();"
267 0e016c89 2023-09-07 op " tout = setTimeout(later, wait);"
270 0e016c89 2023-09-07 op "filter.addEventListener('input', dbc(dofilt, 400));"
273 cc932200 2023-08-31 op const char *foot = "<script src='/app.js?v=0'></script></body></html>";
276 04e4e993 2023-08-14 op dial(const char *sock)
278 04e4e993 2023-08-14 op struct sockaddr_un sa;
282 04e4e993 2023-08-14 op memset(&sa, 0, sizeof(sa));
283 04e4e993 2023-08-14 op sa.sun_family = AF_UNIX;
284 04e4e993 2023-08-14 op len = strlcpy(sa.sun_path, sock, sizeof(sa.sun_path));
285 04e4e993 2023-08-14 op if (len >= sizeof(sa.sun_path))
286 04e4e993 2023-08-14 op err(1, "path too long: %s", sock);
288 04e4e993 2023-08-14 op if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
289 04e4e993 2023-08-14 op err(1, "socket");
290 04e4e993 2023-08-14 op if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
291 04e4e993 2023-08-14 op err(1, "failed to connect to %s", sock);
297 04e4e993 2023-08-14 op * Adapted from usr.sbin/httpd/httpd.c' url_decode.
300 04e4e993 2023-08-14 op url_decode(char *url)
303 04e4e993 2023-08-14 op char hex[3] = {0};
304 04e4e993 2023-08-14 op unsigned long x;
307 04e4e993 2023-08-14 op while (*p != '\0') {
308 04e4e993 2023-08-14 op switch (*p) {
310 04e4e993 2023-08-14 op /* Encoding character is followed by two hex chars */
311 04e4e993 2023-08-14 op if (!isxdigit((unsigned char)p[1]) ||
312 04e4e993 2023-08-14 op !isxdigit((unsigned char)p[2]) ||
313 04e4e993 2023-08-14 op (p[1] == '0' && p[2] == '0'))
316 04e4e993 2023-08-14 op hex[0] = p[1];
317 04e4e993 2023-08-14 op hex[1] = p[2];
320 04e4e993 2023-08-14 op * We don't have to validate "hex" because it is
321 04e4e993 2023-08-14 op * guaranteed to include two hex chars followed
324 04e4e993 2023-08-14 op x = strtoul(hex, NULL, 16);
325 04e4e993 2023-08-14 op *q = (char)x;
344 b42d807f 2023-09-02 op dispatch_event(const char *msg)
346 b42d807f 2023-09-02 op struct client *clt;
350 b42d807f 2023-09-02 op len = strlen(msg);
351 b42d807f 2023-09-02 op TAILQ_FOREACH(clt, &clients, clients) {
352 b42d807f 2023-09-02 op if (!clt->ws || clt->done || clt->err)
355 b42d807f 2023-09-02 op if (ws_compose(clt, WST_TEXT, msg, len) == -1)
358 b42d807f 2023-09-02 op ev_add(clt->bio.fd, POLLIN|POLLOUT, client_ev, clt);
361 b42d807f 2023-09-02 op return (ret);
365 b42d807f 2023-09-02 op dispatch_event_status(void)
367 b42d807f 2023-09-02 op const char *status;
368 b42d807f 2023-09-02 op char buf[PATH_MAX + 2];
371 b42d807f 2023-09-02 op switch (player_status.status) {
372 b42d807f 2023-09-02 op case STATE_STOPPED: status = "stopped"; break;
373 b42d807f 2023-09-02 op case STATE_PLAYING: status = "playing"; break;
374 b42d807f 2023-09-02 op case STATE_PAUSED: status = "paused"; break;
375 b42d807f 2023-09-02 op default: status = "unknown";
378 b42d807f 2023-09-02 op r = snprintf(buf, sizeof(buf), "S:%s", status);
379 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(buf)) {
380 b42d807f 2023-09-02 op log_warn("snprintf");
383 b42d807f 2023-09-02 op dispatch_event(buf);
385 b42d807f 2023-09-02 op r = snprintf(buf, sizeof(buf), "r:%s",
386 b42d807f 2023-09-02 op player_status.mode.repeat_one == MODE_ON ? "on" : "off");
387 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(buf)) {
388 b42d807f 2023-09-02 op log_warn("snprintf");
391 b42d807f 2023-09-02 op dispatch_event(buf);
393 b42d807f 2023-09-02 op r = snprintf(buf, sizeof(buf), "R:%s",
394 b42d807f 2023-09-02 op player_status.mode.repeat_all == MODE_ON ? "on" : "off");
395 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(buf)) {
396 b42d807f 2023-09-02 op log_warn("snprintf");
399 b42d807f 2023-09-02 op dispatch_event(buf);
401 b42d807f 2023-09-02 op r = snprintf(buf, sizeof(buf), "c:%s",
402 b42d807f 2023-09-02 op player_status.mode.consume == MODE_ON ? "on" : "off");
403 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(buf)) {
404 b42d807f 2023-09-02 op log_warn("snprintf");
407 b42d807f 2023-09-02 op dispatch_event(buf);
409 b42d807f 2023-09-02 op r = snprintf(buf, sizeof(buf), "C:%s", player_status.path);
410 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(buf)) {
411 b42d807f 2023-09-02 op log_warn("snprintf");
414 b42d807f 2023-09-02 op dispatch_event(buf);
420 b42d807f 2023-09-02 op dispatch_event_track(struct player_status *ps)
422 b42d807f 2023-09-02 op char p[PATH_MAX + 2];
425 b42d807f 2023-09-02 op r = snprintf(p, sizeof(p), "%c:%s",
426 b42d807f 2023-09-02 op ps->status == STATE_PLAYING ? 'A' : 'a', ps->path);
427 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(p))
430 b42d807f 2023-09-02 op return dispatch_event(p);
434 20dff2d2 2023-08-31 op imsg_dispatch(int fd, int ev, void *d)
436 20dff2d2 2023-08-31 op static ssize_t off;
437 20dff2d2 2023-08-31 op static int off_found;
438 b42d807f 2023-09-02 op char seekmsg[128];
439 20dff2d2 2023-08-31 op struct imsg imsg;
440 9307af9e 2024-01-21 op struct ibuf ibuf;
441 20dff2d2 2023-08-31 op struct player_status ps;
442 20dff2d2 2023-08-31 op struct player_event event;
443 20dff2d2 2023-08-31 op const char *msg;
445 20dff2d2 2023-08-31 op size_t datalen;
448 20dff2d2 2023-08-31 op if (ev & (POLLIN|POLLHUP)) {
449 50e0da0e 2024-01-21 op if ((n = imsg_read(&imsgbuf)) == -1 && errno != EAGAIN)
450 20dff2d2 2023-08-31 op fatal("imsg_read");
452 20dff2d2 2023-08-31 op fatalx("pipe closed");
454 20dff2d2 2023-08-31 op if (ev & POLLOUT) {
455 50e0da0e 2024-01-21 op if ((n = msgbuf_write(&imsgbuf.w)) == -1 && errno != EAGAIN)
456 20dff2d2 2023-08-31 op fatal("msgbuf_write");
458 20dff2d2 2023-08-31 op fatalx("pipe closed");
462 50e0da0e 2024-01-21 op if ((n = imsg_get(&imsgbuf, &imsg)) == -1)
463 20dff2d2 2023-08-31 op fatal("imsg_get");
467 20dff2d2 2023-08-31 op datalen = IMSG_DATA_SIZE(imsg);
469 ddaffed6 2024-01-21 op switch (imsg_get_type(&imsg)) {
470 20dff2d2 2023-08-31 op case IMSG_CTL_ERR:
471 9307af9e 2024-01-21 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
472 9307af9e 2024-01-21 op (datalen = ibuf_size(&ibuf)) == 0 ||
473 9307af9e 2024-01-21 op (msg = ibuf_data(&ibuf)) == NULL ||
474 9307af9e 2024-01-21 op msg[datalen - 1] != '\0')
475 20dff2d2 2023-08-31 op fatalx("malformed error message");
476 20dff2d2 2023-08-31 op log_warnx("error: %s", msg);
479 20dff2d2 2023-08-31 op case IMSG_CTL_ADD:
480 20dff2d2 2023-08-31 op playlist_free(&playlist_tmp);
481 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_SHOW, 0, 0, -1,
485 20dff2d2 2023-08-31 op case IMSG_CTL_MONITOR:
486 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &event, sizeof(event)) == -1)
487 20dff2d2 2023-08-31 op fatalx("corrupted IMSG_CTL_MONITOR");
488 20dff2d2 2023-08-31 op switch (event.event) {
489 20dff2d2 2023-08-31 op case IMSG_CTL_PLAY:
490 20dff2d2 2023-08-31 op case IMSG_CTL_PAUSE:
491 20dff2d2 2023-08-31 op case IMSG_CTL_STOP:
492 20dff2d2 2023-08-31 op case IMSG_CTL_MODE:
493 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_STATUS, 0, 0,
494 50e0da0e 2024-01-21 op -1, NULL, 0);
497 20dff2d2 2023-08-31 op case IMSG_CTL_NEXT:
498 20dff2d2 2023-08-31 op case IMSG_CTL_PREV:
499 20dff2d2 2023-08-31 op case IMSG_CTL_JUMP:
500 20dff2d2 2023-08-31 op case IMSG_CTL_COMMIT:
501 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_SHOW, 0, 0, -1,
503 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_STATUS, 0, 0,
504 50e0da0e 2024-01-21 op -1, NULL, 0);
507 20dff2d2 2023-08-31 op case IMSG_CTL_SEEK:
508 20dff2d2 2023-08-31 op position = event.position;
509 20dff2d2 2023-08-31 op duration = event.duration;
510 b42d807f 2023-09-02 op r = snprintf(seekmsg, sizeof(seekmsg),
511 b42d807f 2023-09-02 op "s:%lld %lld", (long long)position,
512 b42d807f 2023-09-02 op (long long)duration);
513 b42d807f 2023-09-02 op if (r < 0 || (size_t)r >= sizeof(seekmsg)) {
514 b42d807f 2023-09-02 op log_warn("snprintf failed");
517 b42d807f 2023-09-02 op dispatch_event(seekmsg);
521 20dff2d2 2023-08-31 op log_debug("ignoring event %d", event.event);
526 20dff2d2 2023-08-31 op case IMSG_CTL_SHOW:
527 9307af9e 2024-01-21 op if (imsg_get_len(&imsg) == 0) {
528 b42d807f 2023-09-02 op if (playlist_tmp.len == 0) {
529 b42d807f 2023-09-02 op dispatch_event("x:");
531 03069bc3 2023-09-02 op } else if (playlist_tmp.len == off)
533 0e016c89 2023-09-07 op dispatch_event("X:");
534 20dff2d2 2023-08-31 op playlist_swap(&playlist_tmp, off);
535 20dff2d2 2023-08-31 op memset(&playlist_tmp, 0, sizeof(playlist_tmp));
537 20dff2d2 2023-08-31 op off_found = 0;
540 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &ps, sizeof(ps)) == -1)
541 20dff2d2 2023-08-31 op fatalx("corrupted IMSG_CTL_SHOW");
542 20dff2d2 2023-08-31 op if (ps.path[sizeof(ps.path) - 1] != '\0')
543 20dff2d2 2023-08-31 op fatalx("corrupted IMSG_CTL_SHOW");
544 b42d807f 2023-09-02 op if (playlist_tmp.len == 0)
545 b42d807f 2023-09-02 op dispatch_event("x:");
546 b42d807f 2023-09-02 op dispatch_event_track(&ps);
547 20dff2d2 2023-08-31 op playlist_push(&playlist_tmp, ps.path);
548 20dff2d2 2023-08-31 op if (ps.status == STATE_PLAYING)
549 20dff2d2 2023-08-31 op off_found = 1;
550 20dff2d2 2023-08-31 op if (!off_found)
554 20dff2d2 2023-08-31 op case IMSG_CTL_STATUS:
555 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &player_status,
556 9307af9e 2024-01-21 op sizeof(player_status)) == -1)
557 20dff2d2 2023-08-31 op fatalx("corrupted IMSG_CTL_STATUS");
558 20dff2d2 2023-08-31 op if (player_status.path[sizeof(player_status.path) - 1]
560 20dff2d2 2023-08-31 op fatalx("corrupted IMSG_CTL_STATUS");
561 b42d807f 2023-09-02 op dispatch_event_status();
567 50e0da0e 2024-01-21 op if (imsgbuf.w.queued)
568 20dff2d2 2023-08-31 op ev |= POLLOUT;
569 20dff2d2 2023-08-31 op ev_add(fd, ev, imsg_dispatch, NULL);
573 3634fa70 2023-08-31 op route_notfound(struct client *clt)
575 3634fa70 2023-08-31 op if (http_reply(clt, 404, "Not Found", "text/plain") == -1 ||
576 3634fa70 2023-08-31 op http_writes(clt, "Page not found\n") == -1)
581 3634fa70 2023-08-31 op render_playlist(struct client *clt)
584 9ae51b1f 2023-09-02 op const char *path;
587 3634fa70 2023-08-31 op http_writes(clt, "<section class='playlist-wrapper'>");
588 3634fa70 2023-08-31 op http_writes(clt, "<form action=jump method=post"
589 04e4e993 2023-08-14 op " enctype='"FORM_URLENCODED"'>");
590 3634fa70 2023-08-31 op http_writes(clt, "<ul class=playlist>");
592 20dff2d2 2023-08-31 op for (i = 0; i < playlist.len; ++i) {
593 20dff2d2 2023-08-31 op current = play_off == i;
595 9ae51b1f 2023-09-02 op path = playlist.songs[i];
597 20dff2d2 2023-08-31 op http_fmt(clt, "<li%s>", current ? " id=current" : "");
598 20dff2d2 2023-08-31 op http_writes(clt, "<button type=submit name=jump value=\"");
599 20dff2d2 2023-08-31 op http_htmlescape(clt, path);
600 20dff2d2 2023-08-31 op http_writes(clt, "\">");
601 9ae51b1f 2023-09-02 op http_htmlescape(clt, path);
602 20dff2d2 2023-08-31 op http_writes(clt, "</button></li>");
605 3634fa70 2023-08-31 op http_writes(clt, "</ul>");
606 3634fa70 2023-08-31 op http_writes(clt, "</form>");
607 3634fa70 2023-08-31 op http_writes(clt, "</section>");
611 3634fa70 2023-08-31 op render_controls(struct client *clt)
613 04e4e993 2023-08-14 op const char *oc, *ac, *p;
616 20dff2d2 2023-08-31 op ac = player_status.mode.repeat_all ? " class='mode-active'" : "";
617 20dff2d2 2023-08-31 op oc = player_status.mode.repeat_one ? " class='mode-active'" : "";
618 20dff2d2 2023-08-31 op playing = player_status.status == STATE_PLAYING;
620 20dff2d2 2023-08-31 op if ((p = strrchr(player_status.path, '/')) != NULL)
623 20dff2d2 2023-08-31 op p = player_status.path;
625 3634fa70 2023-08-31 op if (http_writes(clt, "<section class=controls>") == -1 ||
626 3634fa70 2023-08-31 op http_writes(clt, "<p><a href='#current'>") == -1 ||
627 3634fa70 2023-08-31 op http_htmlescape(clt, p) == -1 ||
628 3634fa70 2023-08-31 op http_writes(clt, "</a></p>") == -1 ||
629 3634fa70 2023-08-31 op http_writes(clt, "<form action=ctrls method=post"
630 04e4e993 2023-08-14 op " enctype='"FORM_URLENCODED"'>") == -1 ||
631 3634fa70 2023-08-31 op http_writes(clt, "<button type=submit name=ctl value=prev>"
632 04e4e993 2023-08-14 op ICON_PREV"</button>") == -1 ||
633 b42d807f 2023-09-02 op http_fmt(clt, "<button id='toggle' type=submit name=ctl value=%s>"
634 04e4e993 2023-08-14 op "%s</button>", playing ? "pause" : "play",
635 04e4e993 2023-08-14 op playing ? ICON_PAUSE : ICON_PLAY) == -1 ||
636 3634fa70 2023-08-31 op http_writes(clt, "<button type=submit name=ctl value=next>"
637 04e4e993 2023-08-14 op ICON_NEXT"</button>") == -1 ||
638 3634fa70 2023-08-31 op http_writes(clt, "</form>") == -1 ||
639 3634fa70 2023-08-31 op http_writes(clt, "<form action=mode method=post"
640 3634fa70 2023-08-31 op " enctype='"FORM_URLENCODED"'>") == -1 ||
641 b42d807f 2023-09-02 op http_fmt(clt, "<button%s id=rall type=submit name=mode value=all>"
642 04e4e993 2023-08-14 op ICON_REPEAT_ALL"</button>", ac) == -1 ||
643 b42d807f 2023-09-02 op http_fmt(clt, "<button%s id=rone type=submit name=mode value=one>"
644 04e4e993 2023-08-14 op ICON_REPEAT_ONE"</button>", oc) == -1 ||
645 3634fa70 2023-08-31 op http_writes(clt, "</form>") == -1 ||
646 3634fa70 2023-08-31 op http_writes(clt, "</section>") == -1)
651 3634fa70 2023-08-31 op route_home(struct client *clt)
653 3634fa70 2023-08-31 op if (http_reply(clt, 200, "OK", "text/html;charset=UTF-8") == -1)
656 3634fa70 2023-08-31 op if (http_write(clt, head, strlen(head)) == -1)
659 3634fa70 2023-08-31 op if (http_writes(clt, "<main>") == -1)
662 3634fa70 2023-08-31 op render_controls(clt);
663 3634fa70 2023-08-31 op render_playlist(clt);
665 3634fa70 2023-08-31 op if (http_writes(clt, "</main>") == -1)
668 3634fa70 2023-08-31 op http_write(clt, foot, strlen(foot));
672 3634fa70 2023-08-31 op route_jump(struct client *clt)
674 04e4e993 2023-08-14 op char path[PATH_MAX];
675 04e4e993 2023-08-14 op char *form, *field;
676 04e4e993 2023-08-14 op int found = 0;
678 2216d3fb 2023-09-07 op http_postdata(clt, &form, NULL);
679 04e4e993 2023-08-14 op while ((field = strsep(&form, "&")) != NULL) {
680 04e4e993 2023-08-14 op if (url_decode(field) == -1)
683 04e4e993 2023-08-14 op if (strncmp(field, "jump=", 5) != 0)
688 99b28969 2023-08-14 op memset(&path, 0, sizeof(path));
689 04e4e993 2023-08-14 op if (strlcpy(path, field, sizeof(path)) >= sizeof(path))
692 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_JUMP, 0, 0, -1,
693 04e4e993 2023-08-14 op path, sizeof(path));
694 50e0da0e 2024-01-21 op ev_add(imsgbuf.w.fd, POLLIN|POLLOUT, imsg_dispatch, NULL);
701 b42d807f 2023-09-02 op if (!strncmp(clt->req.path, "/a/", 2))
702 b42d807f 2023-09-02 op http_reply(clt, 200, "OK", "text/plain");
704 b42d807f 2023-09-02 op http_reply(clt, 302, "See Other", "/");
708 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", "text/plain");
709 3634fa70 2023-08-31 op http_writes(clt, "Bad Request.\n");
713 3634fa70 2023-08-31 op route_controls(struct client *clt)
715 04e4e993 2023-08-14 op char *form, *field;
716 04e4e993 2023-08-14 op int cmd, found = 0;
718 2216d3fb 2023-09-07 op http_postdata(clt, &form, NULL);
719 04e4e993 2023-08-14 op while ((field = strsep(&form, "&")) != NULL) {
720 04e4e993 2023-08-14 op if (url_decode(field) == -1)
723 04e4e993 2023-08-14 op if (strncmp(field, "ctl=", 4) != 0)
728 04e4e993 2023-08-14 op if (!strcmp(field, "play"))
729 04e4e993 2023-08-14 op cmd = IMSG_CTL_PLAY;
730 04e4e993 2023-08-14 op else if (!strcmp(field, "pause"))
731 04e4e993 2023-08-14 op cmd = IMSG_CTL_PAUSE;
732 04e4e993 2023-08-14 op else if (!strcmp(field, "next"))
733 04e4e993 2023-08-14 op cmd = IMSG_CTL_NEXT;
734 04e4e993 2023-08-14 op else if (!strcmp(field, "prev"))
735 04e4e993 2023-08-14 op cmd = IMSG_CTL_PREV;
739 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, cmd, 0, 0, -1, NULL, 0);
740 50e0da0e 2024-01-21 op imsg_flush(&imsgbuf);
747 b42d807f 2023-09-02 op if (!strncmp(clt->req.path, "/a/", 2))
748 b42d807f 2023-09-02 op http_reply(clt, 200, "OK", "text/plain");
750 b42d807f 2023-09-02 op http_reply(clt, 302, "See Other", "/");
754 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", "text/plain");
755 3634fa70 2023-08-31 op http_writes(clt, "Bad Request.\n");
759 3634fa70 2023-08-31 op route_mode(struct client *clt)
761 04e4e993 2023-08-14 op char *form, *field;
762 04e4e993 2023-08-14 op int found = 0;
763 04e4e993 2023-08-14 op struct player_mode pm;
765 04e4e993 2023-08-14 op pm.repeat_one = pm.repeat_all = pm.consume = MODE_UNDEF;
767 2216d3fb 2023-09-07 op http_postdata(clt, &form, NULL);
768 04e4e993 2023-08-14 op while ((field = strsep(&form, "&")) != NULL) {
769 04e4e993 2023-08-14 op if (url_decode(field) == -1)
772 04e4e993 2023-08-14 op if (strncmp(field, "mode=", 5) != 0)
777 04e4e993 2023-08-14 op if (!strcmp(field, "all"))
778 04e4e993 2023-08-14 op pm.repeat_all = MODE_TOGGLE;
779 04e4e993 2023-08-14 op else if (!strcmp(field, "one"))
780 04e4e993 2023-08-14 op pm.repeat_one = MODE_TOGGLE;
784 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_MODE, 0, 0, -1,
785 50e0da0e 2024-01-21 op &pm, sizeof(pm));
786 50e0da0e 2024-01-21 op ev_add(imsgbuf.w.fd, POLLIN|POLLOUT, imsg_dispatch, NULL);
793 b42d807f 2023-09-02 op if (!strncmp(clt->req.path, "/a/", 2))
794 b42d807f 2023-09-02 op http_reply(clt, 200, "OK", "text/plain");
796 b42d807f 2023-09-02 op http_reply(clt, 302, "See Other", "/");
800 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", "text/plain");
801 3634fa70 2023-08-31 op http_writes(clt, "Bad Request.\n");
805 b42d807f 2023-09-02 op route_handle_ws(struct client *clt)
807 b42d807f 2023-09-02 op struct buffer *rbuf = &clt->bio.rbuf;
811 b42d807f 2023-09-02 op if (ws_read(clt, &type, &len) == -1) {
812 b42d807f 2023-09-02 op if (errno != EAGAIN) {
813 b42d807f 2023-09-02 op log_warn("ws_read");
814 b42d807f 2023-09-02 op clt->done = 1;
819 b42d807f 2023-09-02 op switch (type) {
820 b42d807f 2023-09-02 op case WST_PING:
821 b42d807f 2023-09-02 op ws_compose(clt, WST_PONG, rbuf->buf, len);
823 b42d807f 2023-09-02 op case WST_TEXT:
824 b42d807f 2023-09-02 op /* log_info("<<< %.*s", (int)len, rbuf->buf); */
826 b42d807f 2023-09-02 op case WST_CLOSE:
827 b42d807f 2023-09-02 op /* TODO send a close too (ack) */
828 b42d807f 2023-09-02 op clt->done = 1;
831 b42d807f 2023-09-02 op log_info("got unexpected ws frame type 0x%02x", type);
835 b42d807f 2023-09-02 op buf_drain(rbuf, len);
839 b42d807f 2023-09-02 op route_init_ws(struct client *clt)
841 b42d807f 2023-09-02 op if (!(clt->req.flags & (R_CONNUPGR|R_UPGRADEWS|R_WSVERSION)) ||
842 b42d807f 2023-09-02 op clt->req.secret == NULL) {
843 b42d807f 2023-09-02 op http_reply(clt, 400, "Bad Request", "text/plain");
844 b42d807f 2023-09-02 op http_writes(clt, "Invalid websocket handshake.\r\n");
849 b42d807f 2023-09-02 op clt->done = 0;
850 b42d807f 2023-09-02 op clt->route = route_handle_ws;
851 b42d807f 2023-09-02 op http_reply(clt, 101, "Switching Protocols", NULL);
855 cc932200 2023-08-31 op route_assets(struct client *clt)
857 cc932200 2023-08-31 op if (!strcmp(clt->req.path, "/style.css")) {
858 cc932200 2023-08-31 op http_reply(clt, 200, "OK", "text/css");
859 cc932200 2023-08-31 op http_write(clt, css, strlen(css));
863 cc932200 2023-08-31 op if (!strcmp(clt->req.path, "/app.js")) {
864 cc932200 2023-08-31 op http_reply(clt, 200, "OK", "application/javascript");
865 cc932200 2023-08-31 op http_write(clt, js, strlen(js));
869 cc932200 2023-08-31 op route_notfound(clt);
873 3634fa70 2023-08-31 op route_dispatch(struct client *clt)
875 04e4e993 2023-08-14 op static const struct route {
877 3634fa70 2023-08-31 op const char *path;
878 3634fa70 2023-08-31 op route_fn route;
879 04e4e993 2023-08-14 op } routes[] = {
880 04e4e993 2023-08-14 op { METHOD_GET, "/", &route_home },
882 04e4e993 2023-08-14 op { METHOD_POST, "/jump", &route_jump },
883 04e4e993 2023-08-14 op { METHOD_POST, "/ctrls", &route_controls },
884 04e4e993 2023-08-14 op { METHOD_POST, "/mode", &route_mode },
886 b42d807f 2023-09-02 op { METHOD_POST, "/a/jump", &route_jump },
887 b42d807f 2023-09-02 op { METHOD_POST, "/a/ctrls", &route_controls },
888 b42d807f 2023-09-02 op { METHOD_POST, "/a/mode", &route_mode },
890 b42d807f 2023-09-02 op { METHOD_GET, "/ws", &route_init_ws },
892 cc932200 2023-08-31 op { METHOD_GET, "/style.css", &route_assets },
893 cc932200 2023-08-31 op { METHOD_GET, "/app.js", &route_assets },
895 04e4e993 2023-08-14 op { METHOD_GET, "*", &route_notfound },
896 04e4e993 2023-08-14 op { METHOD_POST, "*", &route_notfound },
898 3634fa70 2023-08-31 op struct request *req = &clt->req;
901 04e4e993 2023-08-14 op if ((req->method != METHOD_GET && req->method != METHOD_POST) ||
902 04e4e993 2023-08-14 op (req->ctype != NULL && strcmp(req->ctype, FORM_URLENCODED) != 0) ||
903 04e4e993 2023-08-14 op req->path == NULL) {
904 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", NULL);
908 04e4e993 2023-08-14 op for (i = 0; i < nitems(routes); ++i) {
909 04e4e993 2023-08-14 op if (req->method != routes[i].method ||
910 04e4e993 2023-08-14 op fnmatch(routes[i].path, req->path, 0) != 0)
912 3634fa70 2023-08-31 op clt->done = 1; /* assume with one round is done */
913 3634fa70 2023-08-31 op clt->route = routes[i].route;
914 3634fa70 2023-08-31 op clt->route(clt);
915 3634fa70 2023-08-31 op if (clt->done)
916 3634fa70 2023-08-31 op http_close(clt);
922 3634fa70 2023-08-31 op client_ev(int fd, int ev, void *d)
924 3634fa70 2023-08-31 op struct client *clt = d;
926 3634fa70 2023-08-31 op if (ev & (POLLIN|POLLHUP)) {
927 3634fa70 2023-08-31 op if (bufio_read(&clt->bio) == -1 && errno != EAGAIN) {
928 3634fa70 2023-08-31 op log_warn("bufio_read");
933 3634fa70 2023-08-31 op if (ev & POLLOUT) {
934 3634fa70 2023-08-31 op if (bufio_write(&clt->bio) == -1 && errno != EAGAIN) {
935 3d47cf24 2023-09-11 op log_warn("bufio_write");
940 3634fa70 2023-08-31 op if (clt->route == NULL) {
941 3634fa70 2023-08-31 op if (http_parse(clt) == -1) {
942 3634fa70 2023-08-31 op if (errno == EAGAIN)
944 3634fa70 2023-08-31 op log_warnx("HTTP parse request failed");
947 3634fa70 2023-08-31 op if (clt->req.method == METHOD_POST &&
948 3634fa70 2023-08-31 op http_read(clt) == -1) {
949 3634fa70 2023-08-31 op if (errno == EAGAIN)
951 3634fa70 2023-08-31 op log_warnx("failed to read POST data");
954 3634fa70 2023-08-31 op route_dispatch(clt);
958 6d777267 2023-09-02 op if (!clt->done && !clt->err)
959 3634fa70 2023-08-31 op clt->route(clt);
962 3634fa70 2023-08-31 op ev = bufio_pollev(&clt->bio);
963 6d777267 2023-09-02 op if (ev == POLLIN && (clt->done || clt->err)) {
964 3634fa70 2023-08-31 op goto err; /* done with this client */
967 3634fa70 2023-08-31 op ev_add(fd, ev, client_ev, clt);
972 b42d807f 2023-09-02 op TAILQ_REMOVE(&clients, clt, clients);
973 3634fa70 2023-08-31 op http_free(clt);
977 703c260b 2023-08-25 op web_accept(int psock, int ev, void *d)
979 3634fa70 2023-08-31 op struct client *clt;
982 04e4e993 2023-08-14 op if ((sock = accept(psock, NULL, NULL)) == -1) {
983 04e4e993 2023-08-14 op warn("accept");
986 3634fa70 2023-08-31 op if ((clt = calloc(1, sizeof(*clt))) == NULL ||
987 3634fa70 2023-08-31 op http_init(clt, sock) == -1) {
988 3634fa70 2023-08-31 op log_warn("failed to initialize client");
994 b42d807f 2023-09-02 op TAILQ_INSERT_TAIL(&clients, clt, clients);
996 3634fa70 2023-08-31 op client_ev(sock, POLLIN, clt);
1003 9ae51b1f 2023-09-02 op fprintf(stderr, "usage: %s [-v] [-s sock] [[host] port]\n",
1004 04e4e993 2023-08-14 op getprogname());
1009 04e4e993 2023-08-14 op main(int argc, char **argv)
1011 04e4e993 2023-08-14 op struct addrinfo hints, *res, *res0;
1012 04e4e993 2023-08-14 op const char *cause = NULL;
1013 04e4e993 2023-08-14 op const char *host = NULL;
1014 04e4e993 2023-08-14 op const char *port = "9090";
1015 04e4e993 2023-08-14 op char *sock = NULL;
1016 80d5f5ad 2023-08-25 op size_t nsock, error, save_errno;
1017 80d5f5ad 2023-08-25 op int ch, v, amused_sock, fd;
1018 04e4e993 2023-08-14 op int verbose = 0;
1020 b42d807f 2023-09-02 op TAILQ_INIT(&clients);
1021 04e4e993 2023-08-14 op setlocale(LC_ALL, NULL);
1023 04e4e993 2023-08-14 op log_init(1, LOG_DAEMON);
1025 04e4e993 2023-08-14 op if (pledge("stdio rpath unix inet dns", NULL) == -1)
1026 04e4e993 2023-08-14 op err(1, "pledge");
1028 9ae51b1f 2023-09-02 op while ((ch = getopt(argc, argv, "s:v")) != -1) {
1029 04e4e993 2023-08-14 op switch (ch) {
1031 04e4e993 2023-08-14 op sock = optarg;
1034 04e4e993 2023-08-14 op verbose = 1;
1040 04e4e993 2023-08-14 op argc -= optind;
1041 04e4e993 2023-08-14 op argv += optind;
1043 04e4e993 2023-08-14 op if (argc == 1)
1044 04e4e993 2023-08-14 op port = argv[0];
1045 04e4e993 2023-08-14 op if (argc == 2) {
1046 04e4e993 2023-08-14 op host = argv[0];
1047 04e4e993 2023-08-14 op port = argv[1];
1049 04e4e993 2023-08-14 op if (argc > 2)
1052 04e4e993 2023-08-14 op log_setverbose(verbose);
1054 83f1c895 2023-12-10 op if (sock == NULL) {
1055 83f1c895 2023-12-10 op const char *tmpdir;
1057 83f1c895 2023-12-10 op if ((tmpdir = getenv("TMPDIR")) == NULL)
1058 83f1c895 2023-12-10 op tmpdir = "/tmp";
1060 83f1c895 2023-12-10 op xasprintf(&sock, "%s/amused-%d", tmpdir, getuid());
1063 04e4e993 2023-08-14 op signal(SIGPIPE, SIG_IGN);
1065 80d5f5ad 2023-08-25 op if (ev_init() == -1)
1066 80d5f5ad 2023-08-25 op fatal("ev_init");
1068 04e4e993 2023-08-14 op amused_sock = dial(sock);
1069 50e0da0e 2024-01-21 op imsg_init(&imsgbuf, amused_sock);
1070 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
1071 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
1072 50e0da0e 2024-01-21 op imsg_compose(&imsgbuf, IMSG_CTL_MONITOR, 0, 0, -1, NULL, 0);
1073 20dff2d2 2023-08-31 op ev_add(amused_sock, POLLIN|POLLOUT, imsg_dispatch, NULL);
1075 04e4e993 2023-08-14 op memset(&hints, 0, sizeof(hints));
1076 04e4e993 2023-08-14 op hints.ai_family = AF_UNSPEC;
1077 04e4e993 2023-08-14 op hints.ai_socktype = SOCK_STREAM;
1078 04e4e993 2023-08-14 op hints.ai_flags = AI_PASSIVE;
1079 04e4e993 2023-08-14 op error = getaddrinfo(host, port, &hints, &res0);
1081 04e4e993 2023-08-14 op errx(1, "%s", gai_strerror(error));
1084 80d5f5ad 2023-08-25 op for (res = res0; res; res = res->ai_next) {
1085 80d5f5ad 2023-08-25 op fd = socket(res->ai_family, res->ai_socktype,
1086 04e4e993 2023-08-14 op res->ai_protocol);
1087 80d5f5ad 2023-08-25 op if (fd == -1) {
1088 04e4e993 2023-08-14 op cause = "socket";
1093 80d5f5ad 2023-08-25 op if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1094 04e4e993 2023-08-14 op &v, sizeof(v)) == -1)
1095 04e4e993 2023-08-14 op fatal("setsockopt(SO_REUSEADDR)");
1097 80d5f5ad 2023-08-25 op if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
1098 04e4e993 2023-08-14 op cause = "bind";
1099 04e4e993 2023-08-14 op save_errno = errno;
1101 04e4e993 2023-08-14 op errno = save_errno;
1105 80d5f5ad 2023-08-25 op if (listen(fd, 5) == -1)
1106 04e4e993 2023-08-14 op err(1, "listen");
1108 703c260b 2023-08-25 op if (ev_add(fd, POLLIN, web_accept, NULL) == -1)
1109 80d5f5ad 2023-08-25 op fatal("ev_add");
1112 04e4e993 2023-08-14 op if (nsock == 0)
1113 04e4e993 2023-08-14 op err(1, "%s", cause);
1114 04e4e993 2023-08-14 op freeaddrinfo(res0);
1116 04e4e993 2023-08-14 op if (pledge("stdio inet", NULL) == -1)
1117 04e4e993 2023-08-14 op err(1, "pledge");
1119 6e0ebd7a 2023-09-09 op log_info("listening on port %s", port);