Blob


1 #include "telescope.h"
3 #include <sys/socket.h>
5 #include <err.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 struct event netev, fsev;
14 struct tabshead tabshead;
16 /* the first is also the fallback one */
17 static struct proto protos[] = {
18 { "gemini:", load_gemini_url },
19 { "about:", load_about_url },
20 { NULL, NULL },
21 };
23 static struct imsgbuf *netibuf, *fsibuf;
25 static void die(void) __attribute__((__noreturn__));
26 static struct tab *tab_by_id(uint32_t);
27 static void handle_imsg_err(struct imsg*, size_t);
28 static void handle_imsg_check_cert(struct imsg*, size_t);
29 static void handle_imsg_got_code(struct imsg*, size_t);
30 static void handle_imsg_got_meta(struct imsg*, size_t);
31 static void handle_imsg_buf(struct imsg*, size_t);
32 static void handle_imsg_eof(struct imsg*, size_t);
33 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 static void dispatch_imsg(int, short, void*);
35 static void load_page_from_str(struct tab*, const char*);
36 static void do_load_url(struct tab*, const char*);
38 static imsg_handlerfn *handlers[] = {
39 [IMSG_ERR] = handle_imsg_err,
40 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
41 [IMSG_GOT_CODE] = handle_imsg_got_code,
42 [IMSG_GOT_META] = handle_imsg_got_meta,
43 [IMSG_BUF] = handle_imsg_buf,
44 [IMSG_EOF] = handle_imsg_eof,
45 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
46 };
48 static void __attribute__((__noreturn__))
49 die(void)
50 {
51 abort(); /* TODO */
52 }
54 static struct tab *
55 tab_by_id(uint32_t id)
56 {
57 struct tab *t;
59 TAILQ_FOREACH(t, &tabshead, tabs) {
60 if (t->id == id)
61 return t;
62 }
64 die();
65 }
67 static void
68 handle_imsg_err(struct imsg *imsg, size_t datalen)
69 {
70 struct tab *tab;
71 char *page;
73 tab = tab_by_id(imsg->hdr.peerid);
75 page = imsg->data;
76 page[datalen-1] = '\0';
78 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
79 tab->hist_cur->h, page) == -1)
80 die();
81 load_page_from_str(tab, page);
82 free(page);
83 }
85 static void
86 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
87 {
88 int tofu_res = 1;
90 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
91 imsg_flush(netibuf);
92 }
94 static inline int
95 normalize_code(int n)
96 {
97 if (n < 20) {
98 if (n == 10 || n == 11)
99 return n;
100 return 10;
101 } else if (n < 30) {
102 return 20;
103 } else if (n < 40) {
104 if (n == 30 || n == 31)
105 return n;
106 return 30;
107 } else if (n < 50) {
108 if (n <= 44)
109 return n;
110 return 40;
111 } else if (n < 60) {
112 if (n <= 53 || n == 59)
113 return n;
114 return 50;
115 } else if (n < 70) {
116 if (n <= 62)
117 return n;
118 return 60;
119 } else
120 return MALFORMED_RESPONSE;
123 static void
124 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
126 struct tab *tab;
128 tab = tab_by_id(imsg->hdr.peerid);
130 if (sizeof(tab->code) != datalen)
131 die();
133 memcpy(&tab->code, imsg->data, sizeof(tab->code));
134 tab->code = normalize_code(tab->code);
135 if (tab->code != 30 && tab->code != 31)
136 tab->redirect_count = 0;
139 static void
140 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
142 struct tab *tab;
144 tab = tab_by_id(imsg->hdr.peerid);
146 if (sizeof(tab->meta) <= datalen)
147 die();
149 memcpy(tab->meta, imsg->data, datalen);
151 if (tab->code < 10) { /* internal errors */
152 load_page_from_str(tab, err_pages[tab->code]);
153 } else if (tab->code < 20) { /* 1x */
154 load_page_from_str(tab, err_pages[tab->code]);
155 ui_require_input(tab, tab->code == 11);
156 } else if (tab->code == 20) {
157 if (setup_parser_for(tab)) {
158 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
159 imsg_flush(netibuf);
160 } else {
161 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
163 } else if (tab->code < 40) { /* 3x */
164 tab->redirect_count++;
166 /* TODO: make customizable? */
167 if (tab->redirect_count > 5) {
168 load_page_from_str(tab,
169 err_pages[TOO_MUCH_REDIRECTS]);
170 } else
171 do_load_url(tab, tab->meta);
172 } else { /* 4x, 5x & 6x */
173 load_page_from_str(tab, err_pages[tab->code]);
177 static void
178 handle_imsg_buf(struct imsg *imsg, size_t datalen)
180 struct tab *tab;
182 tab = tab_by_id(imsg->hdr.peerid);
184 if (!tab->page.parse(&tab->page, imsg->data, datalen))
185 die();
187 ui_on_tab_refresh(tab);
190 static void
191 handle_imsg_eof(struct imsg *imsg, size_t datalen)
193 struct tab *t;
195 t = tab_by_id(imsg->hdr.peerid);
196 if (!t->page.free(&t->page))
197 die();
199 ui_on_tab_refresh(t);
200 ui_on_tab_loaded(t);
203 static void
204 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
206 int res;
208 if (datalen != sizeof(res))
209 die();
211 memcpy(&res, imsg->data, sizeof(res));
212 if (res == 0)
213 ui_notify("Added to bookmarks!");
214 else
215 ui_notify("Failed to add to bookmarks: %s",
216 strerror(res));
219 static void
220 dispatch_imsg(int fd, short ev, void *d)
222 struct imsgbuf *ibuf = d;
223 struct imsg imsg;
224 size_t datalen;
225 ssize_t n;
227 if ((n = imsg_read(ibuf)) == -1) {
228 if (errno == EAGAIN || errno == EWOULDBLOCK)
229 return;
230 die();
233 if (n == 0)
234 _exit(1);
236 for (;;) {
237 if ((n = imsg_get(ibuf, &imsg)) == -1)
238 die();
239 if (n == 0)
240 return;
241 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
242 handlers[imsg.hdr.type](&imsg, datalen);
243 imsg_free(&imsg);
247 static void
248 load_page_from_str(struct tab *tab, const char *page)
250 gemtext_initparser(&tab->page);
251 if (!tab->page.parse(&tab->page, page, strlen(page)))
252 die();
253 if (!tab->page.free(&tab->page))
254 die();
255 ui_on_tab_refresh(tab);
256 ui_on_tab_loaded(tab);
259 void
260 load_about_url(struct tab *tab, const char *url)
262 char *m;
263 size_t len;
266 memset(&tab->url, 0, sizeof(tab->url));
268 m = strchr(url, ':');
269 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
270 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
272 len = sizeof(tab->hist_cur->h)-1;
273 strlcpy(tab->hist_cur->h, url, len);
275 gemtext_initparser(&tab->page);
277 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
278 tab->hist_cur->h, len+1);
279 imsg_flush(fsibuf);
282 void
283 load_gemini_url(struct tab *tab, const char *url)
285 const char *err;
286 char *p;
287 size_t len;
289 if (has_prefix(url, "gemini:")) {
290 if (!url_parse(url, &tab->url, &err))
291 goto err;
292 } else {
293 if (!url_resolve_from(&tab->url, url, &err))
294 goto err;
297 len = sizeof(tab->hist_cur->h)-1;
298 url_unparse(&tab->url, tab->hist_cur->h, len);
299 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
300 tab->hist_cur->h, len+1);
301 imsg_flush(netibuf);
302 return;
304 err:
305 if (asprintf(&p, "#error loading %s\n>%s\n",
306 url, err) == -1)
307 die();
308 strlcpy(tab->hist_cur->h, url, len);
309 load_page_from_str(tab, p);
310 free(p);
313 static void
314 do_load_url(struct tab *tab, const char *url)
316 struct proto *p;
318 for (p = protos; p->schema != NULL; ++p) {
319 if (has_prefix(url, p->schema)) {
320 p->loadfn(tab, url);
321 return;
325 protos[0].loadfn(tab, url);
328 void
329 load_url(struct tab *tab, const char *url)
331 if (tab->hist_cur != NULL)
332 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
334 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
335 event_loopbreak();
336 return;
338 hist_push(&tab->hist, tab->hist_cur);
339 do_load_url(tab, url);
342 int
343 load_previous_page(struct tab *tab)
345 struct hist *h;
347 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
348 return 0;
349 tab->hist_cur = h;
350 do_load_url(tab, h->h);
351 return 1;
354 int
355 load_next_page(struct tab *tab)
357 struct hist *h;
359 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
360 return 0;
361 tab->hist_cur = h;
362 do_load_url(tab, h->h);
363 return 1;
366 void
367 stop_tab(struct tab *tab)
369 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
370 imsg_flush(netibuf);
373 void
374 add_to_bookmarks(const char *str)
376 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
377 imsg_flush(fsibuf);
380 int
381 main(void)
383 struct imsgbuf network_ibuf, fs_ibuf;
384 int net_fds[2], fs_fds[2];
385 pid_t pid;
387 pid = getpid();
389 signal(SIGCHLD, SIG_IGN);
391 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
392 err(1, "socketpair");
394 switch (fork()) {
395 case -1:
396 err(1, "fork");
397 case 0:
398 /* child */
399 setproctitle("(%d) fs", pid);
400 close(fs_fds[0]);
401 imsg_init(&fs_ibuf, fs_fds[1]);
402 exit(fs_main(&fs_ibuf));
403 default:
404 close(fs_fds[1]);
405 imsg_init(&fs_ibuf, fs_fds[0]);
406 fsibuf = &fs_ibuf;
409 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
410 err(1, "socketpair");
412 switch (fork()) {
413 case -1:
414 err(1, "fork");
415 case 0:
416 /* child */
417 setproctitle("(%d) client", pid);
418 close(net_fds[0]);
419 close(fs_fds[0]);
420 imsg_init(&network_ibuf, net_fds[1]);
421 exit(client_main(&network_ibuf));
422 default:
423 close(net_fds[1]);
424 imsg_init(&network_ibuf, net_fds[0]);
425 netibuf = &network_ibuf;
428 setproctitle("(%d) ui", pid);
430 TAILQ_INIT(&tabshead);
432 event_init();
434 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, netibuf);
435 event_add(&netev, NULL);
437 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, fsibuf);
438 event_add(&fsev, NULL);
440 ui_init();
442 sandbox_ui_process();
444 event_dispatch();
446 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
447 imsg_flush(netibuf);
449 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
450 imsg_flush(fsibuf);
452 ui_end();
454 return 0;