1 5e11c00c 2021-03-02 op #include "telescope.h"
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
5 5e11c00c 2021-03-02 op #include <errno.h>
6 5e11c00c 2021-03-02 op #include <signal.h>
7 5e11c00c 2021-03-02 op #include <stdio.h>
8 5e11c00c 2021-03-02 op #include <stdlib.h>
9 5e11c00c 2021-03-02 op #include <string.h>
10 5e11c00c 2021-03-02 op #include <unistd.h>
12 35e1f40a 2021-03-14 op struct event netev, fsev;
13 5e11c00c 2021-03-02 op struct tabshead tabshead;
15 c07ac996 2021-03-12 op /* the first is also the fallback one */
16 c07ac996 2021-03-12 op static struct proto protos[] = {
17 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
18 31f1a758 2021-04-22 op { "about", load_about_url },
19 4d3785b1 2021-03-09 op { NULL, NULL },
22 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
24 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
25 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
26 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
27 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
28 b3575139 2021-04-01 op static void handle_check_cert_user_choice(int, unsigned int);
29 288fd238 2021-04-25 op static void handle_maybe_save_new_cert(int, unsigned int);
30 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
32 de2a69bb 2021-05-17 op static void handle_maybe_save_page(int, unsigned int);
33 de2a69bb 2021-05-17 op static void handle_save_page_path(const char *, unsigned int);
34 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
35 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
36 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
37 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
38 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
39 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
40 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
41 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
42 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
44 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
45 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
46 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
47 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
48 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
49 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
50 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
51 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
52 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
53 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
54 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
57 cbcc75fb 2021-03-17 op static struct ohash certs;
59 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
62 5e11c00c 2021-03-02 op abort(); /* TODO */
65 5e11c00c 2021-03-02 op static struct tab *
66 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
68 5e11c00c 2021-03-02 op struct tab *t;
70 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
71 5e11c00c 2021-03-02 op if (t->id == id)
79 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
81 3a9b9365 2021-03-09 op struct tab *tab;
84 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
86 3a9b9365 2021-03-09 op page = imsg->data;
87 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
89 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
90 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
92 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
97 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
99 cbcc75fb 2021-03-17 op const char *hash;
100 cbcc75fb 2021-03-17 op int tofu_res;
101 cbcc75fb 2021-03-17 op struct tofu_entry *e;
102 cbcc75fb 2021-03-17 op struct tab *tab;
104 cbcc75fb 2021-03-17 op hash = imsg->data;
105 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
108 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
110 3768e50f 2021-04-25 op if ((e = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == NULL) {
111 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
112 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
113 cbcc75fb 2021-03-17 op * the certs! */
114 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
117 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
118 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
120 31f1a758 2021-04-22 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
121 eb4388ee 2021-04-25 op if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
122 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
123 eb4388ee 2021-04-25 op strlcat(e->domain, tab->uri.port, sizeof(e->domain));
125 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
126 3768e50f 2021-04-25 op tofu_add(&certs, e);
127 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
128 3a227e9a 2021-03-18 op e, sizeof(*e));
129 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
131 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
133 5d1bac73 2021-03-25 op if (tofu_res) {
134 cbcc75fb 2021-03-17 op tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
135 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
136 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
137 5d1bac73 2021-03-25 op imsg_flush(netibuf);
139 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
140 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
141 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
143 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
144 b3575139 2021-04-01 op handle_check_cert_user_choice, tab->id);
149 b3575139 2021-04-01 op handle_check_cert_user_choice(int accept, unsigned int tabid)
151 288fd238 2021-04-25 op struct tab *tab;
153 288fd238 2021-04-25 op tab = tab_by_id(tabid);
155 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
156 5d1bac73 2021-03-25 op &accept, sizeof(accept));
157 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
160 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
161 288fd238 2021-04-25 op handle_maybe_save_new_cert, tabid);
163 288fd238 2021-04-25 op free(tab->cert);
164 288fd238 2021-04-25 op tab->cert = NULL;
169 288fd238 2021-04-25 op handle_maybe_save_new_cert(int accept, unsigned int tabid)
171 288fd238 2021-04-25 op struct tab *tab;
172 288fd238 2021-04-25 op struct tofu_entry *e;
174 288fd238 2021-04-25 op tab = tab_by_id(tabid);
179 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
182 288fd238 2021-04-25 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
183 288fd238 2021-04-25 op if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
184 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
185 288fd238 2021-04-25 op strlcat(e->domain, tab->uri.port, sizeof(e->domain));
187 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
188 288fd238 2021-04-25 op imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
189 288fd238 2021-04-25 op imsg_flush(fsibuf);
191 288fd238 2021-04-25 op tofu_update(&certs, e);
193 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
196 288fd238 2021-04-25 op free(tab->cert);
197 288fd238 2021-04-25 op tab->cert = NULL;
200 5cd2ebb1 2021-03-11 op static inline int
201 5cd2ebb1 2021-03-11 op normalize_code(int n)
203 5cd2ebb1 2021-03-11 op if (n < 20) {
204 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
207 5cd2ebb1 2021-03-11 op } else if (n < 30) {
209 5cd2ebb1 2021-03-11 op } else if (n < 40) {
210 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
213 5cd2ebb1 2021-03-11 op } else if (n < 50) {
217 5cd2ebb1 2021-03-11 op } else if (n < 60) {
218 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
221 5cd2ebb1 2021-03-11 op } else if (n < 70) {
226 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
230 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
232 0972d8b2 2021-03-02 op struct tab *tab;
234 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
236 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
239 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
240 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
241 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
242 0972d8b2 2021-03-02 op tab->redirect_count = 0;
246 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
248 0972d8b2 2021-03-02 op struct tab *tab;
250 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
252 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
255 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
257 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
258 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
259 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
260 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
261 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
262 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
263 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
264 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
265 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
267 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
268 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
269 de2a69bb 2021-05-17 op handle_maybe_save_page, tab->id);
271 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
272 3a9b9365 2021-03-09 op tab->redirect_count++;
274 3a9b9365 2021-03-09 op /* TODO: make customizable? */
275 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
276 3a9b9365 2021-03-09 op load_page_from_str(tab,
277 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
279 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
280 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
281 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
286 de2a69bb 2021-05-17 op handle_maybe_save_page(int dosave, unsigned int tabid)
289 de2a69bb 2021-05-17 op ui_read("Save to path", handle_save_page_path, tabid);
291 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
295 de2a69bb 2021-05-17 op handle_save_page_path(const char *path, unsigned int tabid)
297 de2a69bb 2021-05-17 op struct tab *tab;
299 de2a69bb 2021-05-17 op if (path == NULL) {
300 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
304 de2a69bb 2021-05-17 op tab = tab_by_id(tabid);
305 de2a69bb 2021-05-17 op tab->path = strdup(path);
307 de2a69bb 2021-05-17 op imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
308 de2a69bb 2021-05-17 op imsg_flush(fsibuf);
312 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
314 de2a69bb 2021-05-17 op struct tab *tab;
316 de2a69bb 2021-05-17 op const char *e;
319 de2a69bb 2021-05-17 op tab = tab_by_id(imsg->hdr.peerid);
321 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
322 de2a69bb 2021-05-17 op stop_tab(tab);
324 de2a69bb 2021-05-17 op e = imsg->data;
325 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
327 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
328 de2a69bb 2021-05-17 op tab->path, e);
331 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
334 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
335 de2a69bb 2021-05-17 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
336 de2a69bb 2021-05-17 op imsg_flush(netibuf);
341 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
343 0972d8b2 2021-03-02 op struct tab *tab;
347 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
349 de2a69bb 2021-05-17 op tab->bytes += datalen;
350 de2a69bb 2021-05-17 op if (tab->fd == -1) {
351 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
352 de2a69bb 2021-05-17 op imsg->data, datalen))
355 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
356 de2a69bb 2021-05-17 op l = asprintf(&page, "Writing \"%s\"... (%zu bytes)\n",
361 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
365 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
369 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
371 2ba66cea 2021-03-22 op struct tab *tab;
375 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
377 de2a69bb 2021-05-17 op if (tab->fd == -1) {
378 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
381 de2a69bb 2021-05-17 op l = asprintf(&page, "Wrote %s (%zu bytes)\n",
386 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
389 de2a69bb 2021-05-17 op close(tab->fd);
390 de2a69bb 2021-05-17 op tab->fd = -1;
391 de2a69bb 2021-05-17 op free(tab->path);
392 de2a69bb 2021-05-17 op tab->path = NULL;
395 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
396 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
400 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
404 740f578b 2021-03-15 op if (datalen != sizeof(res))
407 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
408 740f578b 2021-03-15 op if (res == 0)
409 7f963c41 2021-06-20 op message("Added to bookmarks!");
411 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
412 740f578b 2021-03-15 op strerror(res));
416 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
420 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
422 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
423 3a227e9a 2021-03-18 op if (res != 0)
424 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
425 3a227e9a 2021-03-18 op strerror(res));
429 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
433 288fd238 2021-04-25 op if (datalen != sizeof(res))
435 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
437 7f963c41 2021-06-20 op message("Failed to update the certificate");
441 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
443 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
444 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
448 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
450 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
451 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
453 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
455 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
456 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
460 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
462 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
464 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
466 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
467 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
468 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
472 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
476 2eef3403 2021-04-22 op stop_tab(tab);
477 2eef3403 2021-04-22 op tab->id = tab_new_id();
479 31f1a758 2021-04-22 op len = sizeof(tab->hist_cur->h);
480 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
481 31f1a758 2021-04-22 op tab->hist_cur->h, len);
482 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
487 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
489 31f1a758 2021-04-22 op struct phos_uri uri;
490 31f1a758 2021-04-22 op struct proto *p;
493 de2a69bb 2021-05-17 op if (tab->fd != -1) {
494 de2a69bb 2021-05-17 op close(tab->fd);
495 de2a69bb 2021-05-17 op tab->fd = -1;
496 de2a69bb 2021-05-17 op free(tab->path);
497 de2a69bb 2021-05-17 op tab->path = NULL;
500 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
502 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
503 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
504 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
505 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
507 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
508 31f1a758 2021-04-22 op load_page_from_str(tab, t);
513 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
514 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
516 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
517 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
518 4d3785b1 2021-03-09 op p->loadfn(tab, url);
523 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
527 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
529 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
530 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
532 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
533 2051e653 2021-03-13 op event_loopbreak();
536 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
537 2051e653 2021-03-13 op do_load_url(tab, url);
538 46f6e974 2021-05-17 op empty_vlist(&tab->buffer);
539 46f6e974 2021-05-17 op empty_linelist(&tab->buffer);
543 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
545 2051e653 2021-03-13 op struct hist *h;
547 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
549 2051e653 2021-03-13 op tab->hist_cur = h;
550 2051e653 2021-03-13 op do_load_url(tab, h->h);
555 2051e653 2021-03-13 op load_next_page(struct tab *tab)
557 2051e653 2021-03-13 op struct hist *h;
559 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
561 2051e653 2021-03-13 op tab->hist_cur = h;
562 2051e653 2021-03-13 op do_load_url(tab, h->h);
567 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
569 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
570 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
572 de2a69bb 2021-05-17 op if (tab->fd != -1) {
573 de2a69bb 2021-05-17 op close(tab->fd);
574 de2a69bb 2021-05-17 op tab->fd = -1;
575 de2a69bb 2021-05-17 op free(tab->path);
576 de2a69bb 2021-05-17 op tab->path = NULL;
577 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
582 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
584 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
585 c7107cec 2021-04-01 op imsg_flush(fsibuf);
589 c7107cec 2021-04-01 op save_session(void)
591 c7107cec 2021-04-01 op struct tab *tab;
593 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
594 c7107cec 2021-04-01 op imsg_flush(fsibuf);
596 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
597 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
598 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
599 c7107cec 2021-04-01 op imsg_flush(fsibuf);
602 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
603 740f578b 2021-03-15 op imsg_flush(fsibuf);
607 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
609 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
610 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
612 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
613 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
615 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
616 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
619 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
620 5e11c00c 2021-03-02 op err(1, "socketpair");
622 5e11c00c 2021-03-02 op switch (fork()) {
624 5e11c00c 2021-03-02 op err(1, "fork");
627 37d429b0 2021-04-01 op setproctitle("fs");
628 35e1f40a 2021-03-14 op close(fs_fds[0]);
629 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
630 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
632 35e1f40a 2021-03-14 op close(fs_fds[1]);
633 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
634 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
637 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
638 35e1f40a 2021-03-14 op err(1, "socketpair");
640 35e1f40a 2021-03-14 op switch (fork()) {
642 35e1f40a 2021-03-14 op err(1, "fork");
645 37d429b0 2021-04-01 op setproctitle("client");
646 35e1f40a 2021-03-14 op close(net_fds[0]);
647 35e1f40a 2021-03-14 op close(fs_fds[0]);
648 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
649 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
651 35e1f40a 2021-03-14 op close(net_fds[1]);
652 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
653 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
656 37d429b0 2021-04-01 op setproctitle("ui");
658 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
659 3a227e9a 2021-03-18 op load_certs(&certs);
661 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
663 5e11c00c 2021-03-02 op event_init();
665 4ad01575 2021-06-13 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
666 4ad01575 2021-06-13 op handle_dispatch_imsg, netibuf);
667 35e1f40a 2021-03-14 op event_add(&netev, NULL);
669 4ad01575 2021-06-13 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
670 4ad01575 2021-06-13 op handle_dispatch_imsg, fsibuf);
671 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
673 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
674 941b3761 2021-03-18 op sandbox_ui_process();
675 941b3761 2021-03-18 op event_dispatch();
679 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
680 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
682 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
683 35e1f40a 2021-03-14 op imsg_flush(fsibuf);