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;
89 struct tab *tab;
91 tab = tab_by_id(imsg->hdr.peerid);
92 tab->trust = TS_TRUSTED;
93 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
94 &tofu_res, sizeof(tofu_res));
95 imsg_flush(netibuf);
96 }
98 static inline int
99 normalize_code(int n)
101 if (n < 20) {
102 if (n == 10 || n == 11)
103 return n;
104 return 10;
105 } else if (n < 30) {
106 return 20;
107 } else if (n < 40) {
108 if (n == 30 || n == 31)
109 return n;
110 return 30;
111 } else if (n < 50) {
112 if (n <= 44)
113 return n;
114 return 40;
115 } else if (n < 60) {
116 if (n <= 53 || n == 59)
117 return n;
118 return 50;
119 } else if (n < 70) {
120 if (n <= 62)
121 return n;
122 return 60;
123 } else
124 return MALFORMED_RESPONSE;
127 static void
128 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
130 struct tab *tab;
132 tab = tab_by_id(imsg->hdr.peerid);
134 if (sizeof(tab->code) != datalen)
135 die();
137 memcpy(&tab->code, imsg->data, sizeof(tab->code));
138 tab->code = normalize_code(tab->code);
139 if (tab->code != 30 && tab->code != 31)
140 tab->redirect_count = 0;
143 static void
144 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
146 struct tab *tab;
148 tab = tab_by_id(imsg->hdr.peerid);
150 if (sizeof(tab->meta) <= datalen)
151 die();
153 memcpy(tab->meta, imsg->data, datalen);
155 if (tab->code < 10) { /* internal errors */
156 load_page_from_str(tab, err_pages[tab->code]);
157 } else if (tab->code < 20) { /* 1x */
158 load_page_from_str(tab, err_pages[tab->code]);
159 ui_require_input(tab, tab->code == 11);
160 } else if (tab->code == 20) {
161 if (setup_parser_for(tab)) {
162 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
163 imsg_flush(netibuf);
164 } else {
165 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
167 } else if (tab->code < 40) { /* 3x */
168 tab->redirect_count++;
170 /* TODO: make customizable? */
171 if (tab->redirect_count > 5) {
172 load_page_from_str(tab,
173 err_pages[TOO_MUCH_REDIRECTS]);
174 } else
175 do_load_url(tab, tab->meta);
176 } else { /* 4x, 5x & 6x */
177 load_page_from_str(tab, err_pages[tab->code]);
181 static void
182 handle_imsg_buf(struct imsg *imsg, size_t datalen)
184 struct tab *tab;
186 tab = tab_by_id(imsg->hdr.peerid);
188 if (!tab->page.parse(&tab->page, imsg->data, datalen))
189 die();
191 ui_on_tab_refresh(tab);
194 static void
195 handle_imsg_eof(struct imsg *imsg, size_t datalen)
197 struct tab *t;
199 t = tab_by_id(imsg->hdr.peerid);
200 if (!t->page.free(&t->page))
201 die();
203 ui_on_tab_refresh(t);
204 ui_on_tab_loaded(t);
207 static void
208 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
210 int res;
212 if (datalen != sizeof(res))
213 die();
215 memcpy(&res, imsg->data, sizeof(res));
216 if (res == 0)
217 ui_notify("Added to bookmarks!");
218 else
219 ui_notify("Failed to add to bookmarks: %s",
220 strerror(res));
223 static void
224 handle_dispatch_imsg(int fd, short ev, void *d)
226 struct imsgbuf *ibuf = d;
227 dispatch_imsg(ibuf, handlers, sizeof(handlers));
230 static void
231 load_page_from_str(struct tab *tab, const char *page)
233 gemtext_initparser(&tab->page);
234 if (!tab->page.parse(&tab->page, page, strlen(page)))
235 die();
236 if (!tab->page.free(&tab->page))
237 die();
238 ui_on_tab_refresh(tab);
239 ui_on_tab_loaded(tab);
242 void
243 load_about_url(struct tab *tab, const char *url)
245 char *m;
246 size_t len;
248 tab->trust = TS_VERIFIED;
250 memset(&tab->url, 0, sizeof(tab->url));
252 m = strchr(url, ':');
253 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
254 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
256 len = sizeof(tab->hist_cur->h)-1;
257 strlcpy(tab->hist_cur->h, url, len);
259 gemtext_initparser(&tab->page);
261 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
262 tab->hist_cur->h, len+1);
263 imsg_flush(fsibuf);
266 void
267 load_gemini_url(struct tab *tab, const char *url)
269 const char *err;
270 char *p;
271 size_t len;
273 len = sizeof(tab->hist_cur->h)-1;
275 if (has_prefix(url, "gemini:")) {
276 if (!url_parse(url, &tab->url, &err))
277 goto err;
278 } else {
279 if (!url_resolve_from(&tab->url, url, &err))
280 goto err;
283 url_unparse(&tab->url, tab->hist_cur->h, len);
284 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
285 tab->hist_cur->h, len+1);
286 imsg_flush(netibuf);
287 return;
289 err:
290 if (asprintf(&p, "#error loading %s\n>%s\n",
291 url, err) == -1)
292 die();
293 strlcpy(tab->hist_cur->h, url, len);
294 load_page_from_str(tab, p);
295 free(p);
298 static void
299 do_load_url(struct tab *tab, const char *url)
301 struct proto *p;
303 tab->trust = TS_UNKNOWN;
305 for (p = protos; p->schema != NULL; ++p) {
306 if (has_prefix(url, p->schema)) {
307 p->loadfn(tab, url);
308 return;
312 protos[0].loadfn(tab, url);
315 void
316 load_url(struct tab *tab, const char *url)
318 if (tab->hist_cur != NULL)
319 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
321 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
322 event_loopbreak();
323 return;
325 hist_push(&tab->hist, tab->hist_cur);
326 do_load_url(tab, url);
329 int
330 load_previous_page(struct tab *tab)
332 struct hist *h;
334 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
335 return 0;
336 tab->hist_cur = h;
337 do_load_url(tab, h->h);
338 return 1;
341 int
342 load_next_page(struct tab *tab)
344 struct hist *h;
346 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
347 return 0;
348 tab->hist_cur = h;
349 do_load_url(tab, h->h);
350 return 1;
353 void
354 stop_tab(struct tab *tab)
356 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
357 imsg_flush(netibuf);
360 void
361 add_to_bookmarks(const char *str)
363 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
364 imsg_flush(fsibuf);
367 int
368 main(void)
370 struct imsgbuf network_ibuf, fs_ibuf;
371 int net_fds[2], fs_fds[2];
372 pid_t pid;
374 pid = getpid();
376 signal(SIGCHLD, SIG_IGN);
378 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
379 err(1, "socketpair");
381 switch (fork()) {
382 case -1:
383 err(1, "fork");
384 case 0:
385 /* child */
386 setproctitle("(%d) fs", pid);
387 close(fs_fds[0]);
388 imsg_init(&fs_ibuf, fs_fds[1]);
389 exit(fs_main(&fs_ibuf));
390 default:
391 close(fs_fds[1]);
392 imsg_init(&fs_ibuf, fs_fds[0]);
393 fsibuf = &fs_ibuf;
396 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
397 err(1, "socketpair");
399 switch (fork()) {
400 case -1:
401 err(1, "fork");
402 case 0:
403 /* child */
404 setproctitle("(%d) client", pid);
405 close(net_fds[0]);
406 close(fs_fds[0]);
407 imsg_init(&network_ibuf, net_fds[1]);
408 exit(client_main(&network_ibuf));
409 default:
410 close(net_fds[1]);
411 imsg_init(&network_ibuf, net_fds[0]);
412 netibuf = &network_ibuf;
415 setproctitle("(%d) ui", pid);
417 TAILQ_INIT(&tabshead);
419 event_init();
421 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
422 event_add(&netev, NULL);
424 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
425 event_add(&fsev, NULL);
427 ui_init();
429 sandbox_ui_process();
431 event_dispatch();
433 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
434 imsg_flush(netibuf);
436 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
437 imsg_flush(fsibuf);
439 ui_end();
441 return 0;