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 handle_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 handle_dispatch_imsg(int fd, short ev, void *d)
222 struct imsgbuf *ibuf = d;
223 dispatch_imsg(ibuf, handlers, sizeof(handlers));
226 static void
227 load_page_from_str(struct tab *tab, const char *page)
229 gemtext_initparser(&tab->page);
230 if (!tab->page.parse(&tab->page, page, strlen(page)))
231 die();
232 if (!tab->page.free(&tab->page))
233 die();
234 ui_on_tab_refresh(tab);
235 ui_on_tab_loaded(tab);
238 void
239 load_about_url(struct tab *tab, const char *url)
241 char *m;
242 size_t len;
245 memset(&tab->url, 0, sizeof(tab->url));
247 m = strchr(url, ':');
248 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
249 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
251 len = sizeof(tab->hist_cur->h)-1;
252 strlcpy(tab->hist_cur->h, url, len);
254 gemtext_initparser(&tab->page);
256 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
257 tab->hist_cur->h, len+1);
258 imsg_flush(fsibuf);
261 void
262 load_gemini_url(struct tab *tab, const char *url)
264 const char *err;
265 char *p;
266 size_t len;
268 len = sizeof(tab->hist_cur->h)-1;
270 if (has_prefix(url, "gemini:")) {
271 if (!url_parse(url, &tab->url, &err))
272 goto err;
273 } else {
274 if (!url_resolve_from(&tab->url, url, &err))
275 goto err;
278 url_unparse(&tab->url, tab->hist_cur->h, len);
279 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
280 tab->hist_cur->h, len+1);
281 imsg_flush(netibuf);
282 return;
284 err:
285 if (asprintf(&p, "#error loading %s\n>%s\n",
286 url, err) == -1)
287 die();
288 strlcpy(tab->hist_cur->h, url, len);
289 load_page_from_str(tab, p);
290 free(p);
293 static void
294 do_load_url(struct tab *tab, const char *url)
296 struct proto *p;
298 for (p = protos; p->schema != NULL; ++p) {
299 if (has_prefix(url, p->schema)) {
300 p->loadfn(tab, url);
301 return;
305 protos[0].loadfn(tab, url);
308 void
309 load_url(struct tab *tab, const char *url)
311 if (tab->hist_cur != NULL)
312 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
314 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
315 event_loopbreak();
316 return;
318 hist_push(&tab->hist, tab->hist_cur);
319 do_load_url(tab, url);
322 int
323 load_previous_page(struct tab *tab)
325 struct hist *h;
327 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
328 return 0;
329 tab->hist_cur = h;
330 do_load_url(tab, h->h);
331 return 1;
334 int
335 load_next_page(struct tab *tab)
337 struct hist *h;
339 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
340 return 0;
341 tab->hist_cur = h;
342 do_load_url(tab, h->h);
343 return 1;
346 void
347 stop_tab(struct tab *tab)
349 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
350 imsg_flush(netibuf);
353 void
354 add_to_bookmarks(const char *str)
356 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
357 imsg_flush(fsibuf);
360 int
361 main(void)
363 struct imsgbuf network_ibuf, fs_ibuf;
364 int net_fds[2], fs_fds[2];
365 pid_t pid;
367 pid = getpid();
369 signal(SIGCHLD, SIG_IGN);
371 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
372 err(1, "socketpair");
374 switch (fork()) {
375 case -1:
376 err(1, "fork");
377 case 0:
378 /* child */
379 setproctitle("(%d) fs", pid);
380 close(fs_fds[0]);
381 imsg_init(&fs_ibuf, fs_fds[1]);
382 exit(fs_main(&fs_ibuf));
383 default:
384 close(fs_fds[1]);
385 imsg_init(&fs_ibuf, fs_fds[0]);
386 fsibuf = &fs_ibuf;
389 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
390 err(1, "socketpair");
392 switch (fork()) {
393 case -1:
394 err(1, "fork");
395 case 0:
396 /* child */
397 setproctitle("(%d) client", pid);
398 close(net_fds[0]);
399 close(fs_fds[0]);
400 imsg_init(&network_ibuf, net_fds[1]);
401 exit(client_main(&network_ibuf));
402 default:
403 close(net_fds[1]);
404 imsg_init(&network_ibuf, net_fds[0]);
405 netibuf = &network_ibuf;
408 setproctitle("(%d) ui", pid);
410 TAILQ_INIT(&tabshead);
412 event_init();
414 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
415 event_add(&netev, NULL);
417 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
418 event_add(&fsev, NULL);
420 ui_init();
422 sandbox_ui_process();
424 event_dispatch();
426 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
427 imsg_flush(netibuf);
429 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
430 imsg_flush(fsibuf);
432 ui_end();
434 return 0;