Blame


1 5e11c00c 2021-03-02 op #include "telescope.h"
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
4 5e11c00c 2021-03-02 op
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>
11 5e11c00c 2021-03-02 op
12 35e1f40a 2021-03-14 op struct event netev, fsev;
13 5e11c00c 2021-03-02 op struct tabshead tabshead;
14 5e11c00c 2021-03-02 op
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 },
20 4d3785b1 2021-03-09 op };
21 4d3785b1 2021-03-09 op
22 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
23 5e11c00c 2021-03-02 op
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 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
33 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
34 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
35 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
36 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
37 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
38 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
39 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
40 5e11c00c 2021-03-02 op
41 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
42 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
43 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
44 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
45 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
46 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
47 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
48 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
49 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
50 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
51 5e11c00c 2021-03-02 op };
52 5e11c00c 2021-03-02 op
53 cbcc75fb 2021-03-17 op static struct ohash certs;
54 cbcc75fb 2021-03-17 op
55 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
56 5e11c00c 2021-03-02 op die(void)
57 5e11c00c 2021-03-02 op {
58 5e11c00c 2021-03-02 op abort(); /* TODO */
59 5e11c00c 2021-03-02 op }
60 5e11c00c 2021-03-02 op
61 5e11c00c 2021-03-02 op static struct tab *
62 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
63 5e11c00c 2021-03-02 op {
64 5e11c00c 2021-03-02 op struct tab *t;
65 5e11c00c 2021-03-02 op
66 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
67 5e11c00c 2021-03-02 op if (t->id == id)
68 5e11c00c 2021-03-02 op return t;
69 5e11c00c 2021-03-02 op }
70 5e11c00c 2021-03-02 op
71 5e11c00c 2021-03-02 op die();
72 5e11c00c 2021-03-02 op }
73 5e11c00c 2021-03-02 op
74 5e11c00c 2021-03-02 op static void
75 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
76 5e11c00c 2021-03-02 op {
77 3a9b9365 2021-03-09 op struct tab *tab;
78 3a9b9365 2021-03-09 op char *page;
79 3a9b9365 2021-03-09 op
80 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
81 3a9b9365 2021-03-09 op
82 3a9b9365 2021-03-09 op page = imsg->data;
83 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
84 3a9b9365 2021-03-09 op
85 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
86 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
87 3a9b9365 2021-03-09 op die();
88 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
89 3a9b9365 2021-03-09 op free(page);
90 5e11c00c 2021-03-02 op }
91 5e11c00c 2021-03-02 op
92 5e11c00c 2021-03-02 op static void
93 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
94 5e11c00c 2021-03-02 op {
95 cbcc75fb 2021-03-17 op const char *hash;
96 cbcc75fb 2021-03-17 op int tofu_res;
97 cbcc75fb 2021-03-17 op struct tofu_entry *e;
98 cbcc75fb 2021-03-17 op struct tab *tab;
99 5e11c00c 2021-03-02 op
100 cbcc75fb 2021-03-17 op hash = imsg->data;
101 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
102 cbcc75fb 2021-03-17 op abort();
103 cbcc75fb 2021-03-17 op
104 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
105 cbcc75fb 2021-03-17 op
106 3768e50f 2021-04-25 op if ((e = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == NULL) {
107 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
108 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
109 cbcc75fb 2021-03-17 op * the certs! */
110 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
111 cbcc75fb 2021-03-17 op abort();
112 cbcc75fb 2021-03-17 op
113 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
114 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
115 cbcc75fb 2021-03-17 op abort();
116 31f1a758 2021-04-22 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
117 eb4388ee 2021-04-25 op if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
118 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
119 eb4388ee 2021-04-25 op strlcat(e->domain, tab->uri.port, sizeof(e->domain));
120 eb4388ee 2021-04-25 op }
121 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
122 3768e50f 2021-04-25 op tofu_add(&certs, e);
123 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
124 3a227e9a 2021-03-18 op e, sizeof(*e));
125 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
126 cbcc75fb 2021-03-17 op } else
127 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
128 cbcc75fb 2021-03-17 op
129 5d1bac73 2021-03-25 op if (tofu_res) {
130 cbcc75fb 2021-03-17 op tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
131 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
132 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
133 5d1bac73 2021-03-25 op imsg_flush(netibuf);
134 5d1bac73 2021-03-25 op } else {
135 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
136 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
137 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
138 288fd238 2021-04-25 op die();
139 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
140 b3575139 2021-04-01 op handle_check_cert_user_choice, tab->id);
141 cbcc75fb 2021-03-17 op }
142 5d1bac73 2021-03-25 op }
143 5d1bac73 2021-03-25 op
144 5d1bac73 2021-03-25 op static void
145 b3575139 2021-04-01 op handle_check_cert_user_choice(int accept, unsigned int tabid)
146 5d1bac73 2021-03-25 op {
147 288fd238 2021-04-25 op struct tab *tab;
148 288fd238 2021-04-25 op
149 288fd238 2021-04-25 op tab = tab_by_id(tabid);
150 288fd238 2021-04-25 op
151 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
152 5d1bac73 2021-03-25 op &accept, sizeof(accept));
153 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
154 288fd238 2021-04-25 op
155 288fd238 2021-04-25 op if (accept)
156 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
157 288fd238 2021-04-25 op handle_maybe_save_new_cert, tabid);
158 288fd238 2021-04-25 op else {
159 288fd238 2021-04-25 op free(tab->cert);
160 288fd238 2021-04-25 op tab->cert = NULL;
161 288fd238 2021-04-25 op }
162 5e11c00c 2021-03-02 op }
163 5e11c00c 2021-03-02 op
164 288fd238 2021-04-25 op static void
165 288fd238 2021-04-25 op handle_maybe_save_new_cert(int accept, unsigned int tabid)
166 288fd238 2021-04-25 op {
167 288fd238 2021-04-25 op struct tab *tab;
168 288fd238 2021-04-25 op struct tofu_entry *e;
169 288fd238 2021-04-25 op
170 288fd238 2021-04-25 op tab = tab_by_id(tabid);
171 288fd238 2021-04-25 op
172 288fd238 2021-04-25 op if (!accept)
173 288fd238 2021-04-25 op goto end;
174 288fd238 2021-04-25 op
175 288fd238 2021-04-25 op if ((e = calloc(1, sizeof(e))) == NULL)
176 288fd238 2021-04-25 op die();
177 288fd238 2021-04-25 op
178 288fd238 2021-04-25 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
179 288fd238 2021-04-25 op if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
180 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
181 288fd238 2021-04-25 op strlcat(e->domain, tab->uri.port, sizeof(e->domain));
182 288fd238 2021-04-25 op }
183 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
184 288fd238 2021-04-25 op imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
185 288fd238 2021-04-25 op imsg_flush(fsibuf);
186 288fd238 2021-04-25 op
187 288fd238 2021-04-25 op tofu_update(&certs, e);
188 288fd238 2021-04-25 op
189 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
190 288fd238 2021-04-25 op
191 288fd238 2021-04-25 op end:
192 288fd238 2021-04-25 op free(tab->cert);
193 288fd238 2021-04-25 op tab->cert = NULL;
194 288fd238 2021-04-25 op }
195 288fd238 2021-04-25 op
196 5cd2ebb1 2021-03-11 op static inline int
197 5cd2ebb1 2021-03-11 op normalize_code(int n)
198 5cd2ebb1 2021-03-11 op {
199 5cd2ebb1 2021-03-11 op if (n < 20) {
200 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
201 5cd2ebb1 2021-03-11 op return n;
202 5cd2ebb1 2021-03-11 op return 10;
203 5cd2ebb1 2021-03-11 op } else if (n < 30) {
204 5cd2ebb1 2021-03-11 op return 20;
205 5cd2ebb1 2021-03-11 op } else if (n < 40) {
206 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
207 5cd2ebb1 2021-03-11 op return n;
208 5cd2ebb1 2021-03-11 op return 30;
209 5cd2ebb1 2021-03-11 op } else if (n < 50) {
210 5cd2ebb1 2021-03-11 op if (n <= 44)
211 5cd2ebb1 2021-03-11 op return n;
212 5cd2ebb1 2021-03-11 op return 40;
213 5cd2ebb1 2021-03-11 op } else if (n < 60) {
214 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
215 5cd2ebb1 2021-03-11 op return n;
216 5cd2ebb1 2021-03-11 op return 50;
217 5cd2ebb1 2021-03-11 op } else if (n < 70) {
218 5cd2ebb1 2021-03-11 op if (n <= 62)
219 5cd2ebb1 2021-03-11 op return n;
220 5cd2ebb1 2021-03-11 op return 60;
221 5cd2ebb1 2021-03-11 op } else
222 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
223 5cd2ebb1 2021-03-11 op }
224 5cd2ebb1 2021-03-11 op
225 5e11c00c 2021-03-02 op static void
226 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
227 5e11c00c 2021-03-02 op {
228 0972d8b2 2021-03-02 op struct tab *tab;
229 5e11c00c 2021-03-02 op
230 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
231 0972d8b2 2021-03-02 op
232 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
233 5e11c00c 2021-03-02 op die();
234 5e11c00c 2021-03-02 op
235 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
236 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
237 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
238 0972d8b2 2021-03-02 op tab->redirect_count = 0;
239 5e11c00c 2021-03-02 op }
240 5e11c00c 2021-03-02 op
241 5e11c00c 2021-03-02 op static void
242 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
243 5e11c00c 2021-03-02 op {
244 0972d8b2 2021-03-02 op struct tab *tab;
245 0972d8b2 2021-03-02 op
246 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
247 0972d8b2 2021-03-02 op
248 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
249 0972d8b2 2021-03-02 op die();
250 5cd2ebb1 2021-03-11 op
251 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
252 0972d8b2 2021-03-02 op
253 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
254 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
255 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
256 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
257 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
258 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
259 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
260 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
261 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
262 c07ac996 2021-03-12 op } else {
263 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
264 c07ac996 2021-03-12 op }
265 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
266 3a9b9365 2021-03-09 op tab->redirect_count++;
267 0972d8b2 2021-03-02 op
268 3a9b9365 2021-03-09 op /* TODO: make customizable? */
269 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
270 3a9b9365 2021-03-09 op load_page_from_str(tab,
271 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
272 5cd2ebb1 2021-03-11 op } else
273 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
274 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
275 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
276 3a9b9365 2021-03-09 op }
277 5e11c00c 2021-03-02 op }
278 5e11c00c 2021-03-02 op
279 5e11c00c 2021-03-02 op static void
280 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
281 5e11c00c 2021-03-02 op {
282 0972d8b2 2021-03-02 op struct tab *tab;
283 5e11c00c 2021-03-02 op
284 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
285 5e11c00c 2021-03-02 op
286 2ba66cea 2021-03-22 op if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
287 5e11c00c 2021-03-02 op die();
288 5e11c00c 2021-03-02 op
289 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
290 5e11c00c 2021-03-02 op }
291 5e11c00c 2021-03-02 op
292 5e11c00c 2021-03-02 op static void
293 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
294 5e11c00c 2021-03-02 op {
295 2ba66cea 2021-03-22 op struct tab *tab;
296 a5c3e03d 2021-03-02 op
297 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
298 2ba66cea 2021-03-22 op if (!tab->window.page.free(&tab->window.page))
299 a5c3e03d 2021-03-02 op die();
300 a5c3e03d 2021-03-02 op
301 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
302 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
303 5e11c00c 2021-03-02 op }
304 5e11c00c 2021-03-02 op
305 5e11c00c 2021-03-02 op static void
306 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
307 740f578b 2021-03-15 op {
308 740f578b 2021-03-15 op int res;
309 740f578b 2021-03-15 op
310 740f578b 2021-03-15 op if (datalen != sizeof(res))
311 740f578b 2021-03-15 op die();
312 740f578b 2021-03-15 op
313 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
314 740f578b 2021-03-15 op if (res == 0)
315 740f578b 2021-03-15 op ui_notify("Added to bookmarks!");
316 740f578b 2021-03-15 op else
317 740f578b 2021-03-15 op ui_notify("Failed to add to bookmarks: %s",
318 740f578b 2021-03-15 op strerror(res));
319 740f578b 2021-03-15 op }
320 740f578b 2021-03-15 op
321 740f578b 2021-03-15 op static void
322 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
323 3a227e9a 2021-03-18 op {
324 3a227e9a 2021-03-18 op int res;
325 3a227e9a 2021-03-18 op
326 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
327 3a227e9a 2021-03-18 op die();
328 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
329 3a227e9a 2021-03-18 op if (res != 0)
330 3a227e9a 2021-03-18 op ui_notify("Failed to save the cert for: %s",
331 3a227e9a 2021-03-18 op strerror(res));
332 288fd238 2021-04-25 op }
333 288fd238 2021-04-25 op
334 288fd238 2021-04-25 op static void
335 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
336 288fd238 2021-04-25 op {
337 288fd238 2021-04-25 op int res;
338 288fd238 2021-04-25 op
339 288fd238 2021-04-25 op if (datalen != sizeof(res))
340 288fd238 2021-04-25 op die();
341 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
342 288fd238 2021-04-25 op if (!res)
343 288fd238 2021-04-25 op ui_notify("Failed to update the certificate");
344 3a227e9a 2021-03-18 op }
345 3a227e9a 2021-03-18 op
346 3a227e9a 2021-03-18 op static void
347 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
348 5e11c00c 2021-03-02 op {
349 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
350 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
351 5e11c00c 2021-03-02 op }
352 5e11c00c 2021-03-02 op
353 0972d8b2 2021-03-02 op static void
354 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
355 0972d8b2 2021-03-02 op {
356 2ba66cea 2021-03-22 op gemtext_initparser(&tab->window.page);
357 2ba66cea 2021-03-22 op if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
358 0972d8b2 2021-03-02 op die();
359 2ba66cea 2021-03-22 op if (!tab->window.page.free(&tab->window.page))
360 0972d8b2 2021-03-02 op die();
361 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
362 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
363 0972d8b2 2021-03-02 op }
364 0972d8b2 2021-03-02 op
365 bcb0b073 2021-03-07 op void
366 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
367 0972d8b2 2021-03-02 op {
368 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
369 2051e653 2021-03-13 op
370 2ba66cea 2021-03-22 op gemtext_initparser(&tab->window.page);
371 35e1f40a 2021-03-14 op
372 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
373 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
374 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
375 4d3785b1 2021-03-09 op }
376 0972d8b2 2021-03-02 op
377 4d3785b1 2021-03-09 op void
378 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
379 4d3785b1 2021-03-09 op {
380 2051e653 2021-03-13 op size_t len;
381 754622a2 2021-03-15 op
382 2eef3403 2021-04-22 op stop_tab(tab);
383 2eef3403 2021-04-22 op tab->id = tab_new_id();
384 2eef3403 2021-04-22 op
385 31f1a758 2021-04-22 op len = sizeof(tab->hist_cur->h);
386 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
387 31f1a758 2021-04-22 op tab->hist_cur->h, len);
388 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
389 3a9b9365 2021-03-09 op return;
390 0972d8b2 2021-03-02 op }
391 0972d8b2 2021-03-02 op
392 2051e653 2021-03-13 op static void
393 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
394 4d3785b1 2021-03-09 op {
395 31f1a758 2021-04-22 op struct phos_uri uri;
396 31f1a758 2021-04-22 op struct proto *p;
397 31f1a758 2021-04-22 op char *t;
398 4d3785b1 2021-03-09 op
399 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
400 10346511 2021-03-17 op
401 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
402 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
403 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
404 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
405 31f1a758 2021-04-22 op die();
406 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
407 31f1a758 2021-04-22 op load_page_from_str(tab, t);
408 31f1a758 2021-04-22 op free(t);
409 31f1a758 2021-04-22 op return;
410 31f1a758 2021-04-22 op }
411 31f1a758 2021-04-22 op
412 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
413 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
414 31f1a758 2021-04-22 op
415 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
416 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
417 4d3785b1 2021-03-09 op p->loadfn(tab, url);
418 31f1a758 2021-04-22 op return;
419 4d3785b1 2021-03-09 op }
420 4d3785b1 2021-03-09 op }
421 4d3785b1 2021-03-09 op
422 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
423 4d3785b1 2021-03-09 op }
424 4d3785b1 2021-03-09 op
425 9ad4627d 2021-03-10 op void
426 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
427 2051e653 2021-03-13 op {
428 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
429 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
430 2051e653 2021-03-13 op
431 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
432 2051e653 2021-03-13 op event_loopbreak();
433 2051e653 2021-03-13 op return;
434 2051e653 2021-03-13 op }
435 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
436 2051e653 2021-03-13 op do_load_url(tab, url);
437 31f1a758 2021-04-22 op empty_vlist(&tab->window);
438 31f1a758 2021-04-22 op empty_linelist(&tab->window);
439 2051e653 2021-03-13 op }
440 2051e653 2021-03-13 op
441 2051e653 2021-03-13 op int
442 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
443 2051e653 2021-03-13 op {
444 2051e653 2021-03-13 op struct hist *h;
445 2051e653 2021-03-13 op
446 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
447 2051e653 2021-03-13 op return 0;
448 2051e653 2021-03-13 op tab->hist_cur = h;
449 2051e653 2021-03-13 op do_load_url(tab, h->h);
450 2051e653 2021-03-13 op return 1;
451 2051e653 2021-03-13 op }
452 2051e653 2021-03-13 op
453 2051e653 2021-03-13 op int
454 2051e653 2021-03-13 op load_next_page(struct tab *tab)
455 2051e653 2021-03-13 op {
456 2051e653 2021-03-13 op struct hist *h;
457 2051e653 2021-03-13 op
458 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
459 2051e653 2021-03-13 op return 0;
460 2051e653 2021-03-13 op tab->hist_cur = h;
461 2051e653 2021-03-13 op do_load_url(tab, h->h);
462 2051e653 2021-03-13 op return 1;
463 2051e653 2021-03-13 op }
464 2051e653 2021-03-13 op
465 2051e653 2021-03-13 op void
466 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
467 9ad4627d 2021-03-10 op {
468 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
469 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
470 9ad4627d 2021-03-10 op }
471 9ad4627d 2021-03-10 op
472 740f578b 2021-03-15 op void
473 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
474 740f578b 2021-03-15 op {
475 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
476 c7107cec 2021-04-01 op imsg_flush(fsibuf);
477 c7107cec 2021-04-01 op }
478 c7107cec 2021-04-01 op
479 c7107cec 2021-04-01 op void
480 c7107cec 2021-04-01 op save_session(void)
481 c7107cec 2021-04-01 op {
482 c7107cec 2021-04-01 op struct tab *tab;
483 c7107cec 2021-04-01 op
484 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
485 c7107cec 2021-04-01 op imsg_flush(fsibuf);
486 c7107cec 2021-04-01 op
487 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
488 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
489 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
490 c7107cec 2021-04-01 op imsg_flush(fsibuf);
491 c7107cec 2021-04-01 op }
492 c7107cec 2021-04-01 op
493 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
494 740f578b 2021-03-15 op imsg_flush(fsibuf);
495 740f578b 2021-03-15 op }
496 740f578b 2021-03-15 op
497 5e11c00c 2021-03-02 op int
498 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
499 5e11c00c 2021-03-02 op {
500 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
501 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
502 5e11c00c 2021-03-02 op
503 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
504 5e11c00c 2021-03-02 op
505 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
506 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
507 3a227e9a 2021-03-18 op fs_init();
508 3a227e9a 2021-03-18 op
509 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
510 5e11c00c 2021-03-02 op err(1, "socketpair");
511 5e11c00c 2021-03-02 op
512 5e11c00c 2021-03-02 op switch (fork()) {
513 5e11c00c 2021-03-02 op case -1:
514 5e11c00c 2021-03-02 op err(1, "fork");
515 5e11c00c 2021-03-02 op case 0:
516 5e11c00c 2021-03-02 op /* child */
517 37d429b0 2021-04-01 op setproctitle("fs");
518 35e1f40a 2021-03-14 op close(fs_fds[0]);
519 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
520 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
521 35e1f40a 2021-03-14 op default:
522 35e1f40a 2021-03-14 op close(fs_fds[1]);
523 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
524 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
525 5e11c00c 2021-03-02 op }
526 b1d4d01b 2021-03-14 op
527 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
528 35e1f40a 2021-03-14 op err(1, "socketpair");
529 5e11c00c 2021-03-02 op
530 35e1f40a 2021-03-14 op switch (fork()) {
531 35e1f40a 2021-03-14 op case -1:
532 35e1f40a 2021-03-14 op err(1, "fork");
533 35e1f40a 2021-03-14 op case 0:
534 35e1f40a 2021-03-14 op /* child */
535 37d429b0 2021-04-01 op setproctitle("client");
536 35e1f40a 2021-03-14 op close(net_fds[0]);
537 35e1f40a 2021-03-14 op close(fs_fds[0]);
538 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
539 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
540 35e1f40a 2021-03-14 op default:
541 35e1f40a 2021-03-14 op close(net_fds[1]);
542 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
543 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
544 35e1f40a 2021-03-14 op }
545 5e11c00c 2021-03-02 op
546 37d429b0 2021-04-01 op setproctitle("ui");
547 35e1f40a 2021-03-14 op
548 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
549 3a227e9a 2021-03-18 op load_certs(&certs);
550 cbcc75fb 2021-03-17 op
551 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
552 5e11c00c 2021-03-02 op
553 5e11c00c 2021-03-02 op event_init();
554 5e11c00c 2021-03-02 op
555 1304bbdd 2021-03-15 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
556 35e1f40a 2021-03-14 op event_add(&netev, NULL);
557 5e11c00c 2021-03-02 op
558 1304bbdd 2021-03-15 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
559 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
560 35e1f40a 2021-03-14 op
561 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
562 941b3761 2021-03-18 op sandbox_ui_process();
563 941b3761 2021-03-18 op event_dispatch();
564 941b3761 2021-03-18 op ui_end();
565 941b3761 2021-03-18 op }
566 5e11c00c 2021-03-02 op
567 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
568 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
569 5e11c00c 2021-03-02 op
570 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
571 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
572 35e1f40a 2021-03-14 op
573 5e11c00c 2021-03-02 op return 0;
574 5e11c00c 2021-03-02 op }