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;
345 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
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 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
357 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
362 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
366 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
370 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
372 2ba66cea 2021-03-22 op struct tab *tab;
374 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
376 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
378 de2a69bb 2021-05-17 op if (tab->fd == -1) {
379 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
382 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
383 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
388 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
391 de2a69bb 2021-05-17 op close(tab->fd);
392 de2a69bb 2021-05-17 op tab->fd = -1;
393 de2a69bb 2021-05-17 op free(tab->path);
394 de2a69bb 2021-05-17 op tab->path = NULL;
397 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
398 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
402 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
406 740f578b 2021-03-15 op if (datalen != sizeof(res))
409 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
410 740f578b 2021-03-15 op if (res == 0)
411 7f963c41 2021-06-20 op message("Added to bookmarks!");
413 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
414 740f578b 2021-03-15 op strerror(res));
418 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
422 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
424 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
425 3a227e9a 2021-03-18 op if (res != 0)
426 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
427 3a227e9a 2021-03-18 op strerror(res));
431 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
435 288fd238 2021-04-25 op if (datalen != sizeof(res))
437 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
439 7f963c41 2021-06-20 op message("Failed to update the certificate");
443 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
445 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
446 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
450 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
452 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
453 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
455 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
457 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
458 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
462 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
464 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
466 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
468 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
469 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
470 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
474 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
478 2eef3403 2021-04-22 op stop_tab(tab);
479 2eef3403 2021-04-22 op tab->id = tab_new_id();
481 31f1a758 2021-04-22 op len = sizeof(tab->hist_cur->h);
482 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
483 31f1a758 2021-04-22 op tab->hist_cur->h, len);
484 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
489 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
491 31f1a758 2021-04-22 op struct phos_uri uri;
492 31f1a758 2021-04-22 op struct proto *p;
495 de2a69bb 2021-05-17 op if (tab->fd != -1) {
496 de2a69bb 2021-05-17 op close(tab->fd);
497 de2a69bb 2021-05-17 op tab->fd = -1;
498 de2a69bb 2021-05-17 op free(tab->path);
499 de2a69bb 2021-05-17 op tab->path = NULL;
502 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
504 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
505 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
506 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
507 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
509 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
510 31f1a758 2021-04-22 op load_page_from_str(tab, t);
515 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
516 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
518 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
519 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
520 4d3785b1 2021-03-09 op p->loadfn(tab, url);
525 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
529 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
531 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
532 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
534 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
535 2051e653 2021-03-13 op event_loopbreak();
538 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
539 2051e653 2021-03-13 op do_load_url(tab, url);
540 46f6e974 2021-05-17 op empty_vlist(&tab->buffer);
541 46f6e974 2021-05-17 op empty_linelist(&tab->buffer);
545 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
547 2051e653 2021-03-13 op struct hist *h;
549 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
551 2051e653 2021-03-13 op tab->hist_cur = h;
552 2051e653 2021-03-13 op do_load_url(tab, h->h);
557 2051e653 2021-03-13 op load_next_page(struct tab *tab)
559 2051e653 2021-03-13 op struct hist *h;
561 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
563 2051e653 2021-03-13 op tab->hist_cur = h;
564 2051e653 2021-03-13 op do_load_url(tab, h->h);
569 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
571 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
572 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
574 de2a69bb 2021-05-17 op if (tab->fd != -1) {
575 de2a69bb 2021-05-17 op close(tab->fd);
576 de2a69bb 2021-05-17 op tab->fd = -1;
577 de2a69bb 2021-05-17 op free(tab->path);
578 de2a69bb 2021-05-17 op tab->path = NULL;
579 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
584 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
586 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
587 c7107cec 2021-04-01 op imsg_flush(fsibuf);
591 c7107cec 2021-04-01 op save_session(void)
593 c7107cec 2021-04-01 op struct tab *tab;
595 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
596 c7107cec 2021-04-01 op imsg_flush(fsibuf);
598 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
599 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
600 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
601 c7107cec 2021-04-01 op imsg_flush(fsibuf);
604 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
605 740f578b 2021-03-15 op imsg_flush(fsibuf);
609 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
611 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
612 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
614 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
615 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
617 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
618 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
621 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
622 5e11c00c 2021-03-02 op err(1, "socketpair");
624 5e11c00c 2021-03-02 op switch (fork()) {
626 5e11c00c 2021-03-02 op err(1, "fork");
629 37d429b0 2021-04-01 op setproctitle("fs");
630 35e1f40a 2021-03-14 op close(fs_fds[0]);
631 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
632 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
634 35e1f40a 2021-03-14 op close(fs_fds[1]);
635 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
636 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
639 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
640 35e1f40a 2021-03-14 op err(1, "socketpair");
642 35e1f40a 2021-03-14 op switch (fork()) {
644 35e1f40a 2021-03-14 op err(1, "fork");
647 37d429b0 2021-04-01 op setproctitle("client");
648 35e1f40a 2021-03-14 op close(net_fds[0]);
649 35e1f40a 2021-03-14 op close(fs_fds[0]);
650 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
651 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
653 35e1f40a 2021-03-14 op close(net_fds[1]);
654 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
655 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
658 37d429b0 2021-04-01 op setproctitle("ui");
660 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
661 3a227e9a 2021-03-18 op load_certs(&certs);
663 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
665 5e11c00c 2021-03-02 op event_init();
667 4ad01575 2021-06-13 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
668 4ad01575 2021-06-13 op handle_dispatch_imsg, netibuf);
669 35e1f40a 2021-03-14 op event_add(&netev, NULL);
671 4ad01575 2021-06-13 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
672 4ad01575 2021-06-13 op handle_dispatch_imsg, fsibuf);
673 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
675 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
676 941b3761 2021-03-18 op sandbox_ui_process();
677 941b3761 2021-03-18 op event_dispatch();
681 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
682 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
684 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
685 35e1f40a 2021-03-14 op imsg_flush(fsibuf);