2 44008d59 2024-01-16 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
4 4540067e 2021-07-18 op * Permission to use, copy, modify, and distribute this software for any
5 4540067e 2021-07-18 op * purpose with or without fee is hereby granted, provided that the above
6 4540067e 2021-07-18 op * copyright notice and this permission notice appear in all copies.
8 4540067e 2021-07-18 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4540067e 2021-07-18 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4540067e 2021-07-18 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4540067e 2021-07-18 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4540067e 2021-07-18 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4540067e 2021-07-18 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4540067e 2021-07-18 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1f496e7b 2021-07-21 op #include "compat.h"
19 5e11c00c 2021-03-02 op #include <sys/socket.h>
20 4cf6ba13 2022-02-11 op #include <sys/un.h>
21 be97d6e6 2021-08-15 op #include <sys/wait.h>
23 be97d6e6 2021-08-15 op #include <errno.h>
24 f63b8f73 2022-04-24 op #include <fcntl.h>
25 dc761924 2021-07-15 op #include <getopt.h>
26 f63b8f73 2022-04-24 op #include <limits.h>
27 5e11c00c 2021-03-02 op #include <signal.h>
28 5e11c00c 2021-03-02 op #include <stdio.h>
29 5e11c00c 2021-03-02 op #include <stdlib.h>
30 5e11c00c 2021-03-02 op #include <string.h>
31 5e11c00c 2021-03-02 op #include <unistd.h>
33 d35e18b3 2024-02-04 op #include "certs.h"
34 d252d87c 2024-02-05 op #include "cmd.h"
35 4cf6ba13 2022-02-11 op #include "control.h"
36 e659558c 2021-07-13 op #include "defaults.h"
37 eb2ed626 2021-10-07 op #include "fs.h"
38 65c49665 2024-01-23 op #include "hist.h"
39 44008d59 2024-01-16 op #include "iri.h"
40 f7db6d13 2024-02-06 op #include "keymap.h"
41 d5af38cc 2022-01-10 op #include "mcache.h"
42 4bc446b9 2021-07-21 op #include "minibuffer.h"
43 395b9f4e 2021-07-12 op #include "parser.h"
44 1fce2e75 2021-08-14 op #include "session.h"
45 395b9f4e 2021-07-12 op #include "telescope.h"
46 d163c210 2024-02-06 op #include "tofu.h"
47 d1a0f2a3 2021-07-12 op #include "ui.h"
48 9d65b1d9 2022-01-11 op #include "utils.h"
50 0d877486 2022-02-24 op static const struct option longopts[] = {
51 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
52 d0971653 2021-09-15 op {"safe", no_argument, NULL, 'S'},
53 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
54 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
57 d08fccd2 2024-01-23 op static const char *opts = "c:hnST:v";
59 bb28f1c2 2021-12-30 op static int has_url;
60 bb28f1c2 2021-12-30 op static char url[GEMINI_URL_LEN];
63 a37d1a1c 2021-08-14 op * Used to know when we're finished loading.
65 1fce2e75 2021-08-14 op int operating;
68 d0971653 2021-09-15 op * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
69 d0971653 2021-09-15 op * anything to the filesystem or execute external programs.
71 d0971653 2021-09-15 op int safe_mode;
73 f63b8f73 2022-04-24 op static struct imsgev *iev_net;
75 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
76 6c74799d 2022-01-05 op struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
77 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
79 6cc5fcfe 2021-07-08 op enum telescope_process {
84 9a8a7402 2021-07-18 op #define CANNOT_FETCH 0
85 ef88dc16 2024-02-02 op #define TOO_MANY_REDIRECTS 1
86 9a8a7402 2021-07-18 op #define MALFORMED_RESPONSE 2
87 9a8a7402 2021-07-18 op #define UNKNOWN_TYPE_OR_CSET 3
88 9a8a7402 2021-07-18 op #define UNKNOWN_PROTOCOL 4
90 a21e0756 2021-08-14 op /* XXX: keep in sync with normalize_code */
91 9a8a7402 2021-07-18 op const char *err_pages[70] = {
92 9a8a7402 2021-07-18 op [CANNOT_FETCH] = "# Couldn't load the page\n",
93 ef88dc16 2024-02-02 op [TOO_MANY_REDIRECTS] = "# Too many redirects\n",
94 9a8a7402 2021-07-18 op [MALFORMED_RESPONSE] = "# Got a malformed response\n",
95 5014b6a9 2024-02-02 op [UNKNOWN_TYPE_OR_CSET] = "# Unsupported document format or charset\n",
96 9a8a7402 2021-07-18 op [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
98 9a8a7402 2021-07-18 op [10] = "# Input required\n",
99 9a8a7402 2021-07-18 op [11] = "# Input required\n",
100 9a8a7402 2021-07-18 op [40] = "# Temporary failure\n",
101 9a8a7402 2021-07-18 op [41] = "# Server unavailable\n",
102 9a8a7402 2021-07-18 op [42] = "# CGI error\n",
103 9a8a7402 2021-07-18 op [43] = "# Proxy error\n",
104 9a8a7402 2021-07-18 op [44] = "# Slow down\n",
105 9a8a7402 2021-07-18 op [50] = "# Permanent failure\n",
106 9a8a7402 2021-07-18 op [51] = "# Not found\n",
107 9a8a7402 2021-07-18 op [52] = "# Gone\n",
108 9a8a7402 2021-07-18 op [53] = "# Proxy request refused\n",
109 9a8a7402 2021-07-18 op [59] = "# Bad request\n",
110 9a8a7402 2021-07-18 op [60] = "# Client certificate required\n",
111 9a8a7402 2021-07-18 op [61] = "# Certificate not authorised\n",
112 9a8a7402 2021-07-18 op [62] = "# Certificate not valid\n"
115 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
116 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
117 b148be71 2024-01-31 op static void handle_imsg_check_cert(struct imsg *);
118 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
119 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
120 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
121 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
122 69f32f56 2021-08-16 op static void handle_dispatch_imsg(int, short, void *);
123 cd1d043d 2024-01-13 op static void load_about_url(struct tab *, const char *);
124 cd1d043d 2024-01-13 op static void load_file_url(struct tab *, const char *);
125 cd1d043d 2024-01-13 op static void load_finger_url(struct tab *, const char *);
126 cd1d043d 2024-01-13 op static void load_gemini_url(struct tab *, const char *);
127 cd1d043d 2024-01-13 op static void load_gopher_url(struct tab *, const char *);
128 cd1d043d 2024-01-13 op static void load_via_proxy(struct tab *, const char *,
129 cd1d043d 2024-01-13 op struct proxy *);
130 cd1d043d 2024-01-13 op static void make_request(struct tab *, struct get_req *, int,
131 71bc1636 2024-02-06 op const char *);
132 cd1d043d 2024-01-13 op static void do_load_url(struct tab *, const char *, const char *, int);
133 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
134 4cf6ba13 2022-02-11 op static void send_url(const char *);
136 0d877486 2022-02-24 op static const struct proto {
137 352e0355 2021-07-25 op const char *schema;
138 ba782cb8 2021-07-25 op const char *port;
139 cd1d043d 2024-01-13 op void (*loadfn)(struct tab *, const char *);
140 352e0355 2021-07-25 op } protos[] = {
141 ba782cb8 2021-07-25 op {"about", NULL, load_about_url},
142 0b1053d2 2021-08-13 op {"file", NULL, load_file_url},
143 ba782cb8 2021-07-25 op {"finger", "79", load_finger_url},
144 ba782cb8 2021-07-25 op {"gemini", "1965", load_gemini_url},
145 1663bf1e 2021-07-25 op {"gopher", "70", load_gopher_url},
146 ba782cb8 2021-07-25 op {NULL, NULL, NULL},
149 cbcc75fb 2021-03-17 op static struct ohash certs;
151 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
154 5e11c00c 2021-03-02 op abort(); /* TODO */
157 5e11c00c 2021-03-02 op static struct tab *
158 f6243012 2021-07-19 op tab_by_id(uint32_t id)
160 5e11c00c 2021-03-02 op struct tab *t;
162 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
163 5e11c00c 2021-03-02 op if (t->id == id)
171 b148be71 2024-01-31 op handle_imsg_check_cert(struct imsg *imsg)
174 62cbcdae 2024-02-02 op const char *host, *port;
175 cbcc75fb 2021-03-17 op int tofu_res;
176 62cbcdae 2024-02-02 op struct ibuf ibuf;
177 cbcc75fb 2021-03-17 op struct tofu_entry *e;
178 cbcc75fb 2021-03-17 op struct tab *tab;
179 b148be71 2024-01-31 op size_t datalen;
181 62cbcdae 2024-02-02 op if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
182 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &hash) == -1)
184 b148be71 2024-01-31 op datalen = strlen(hash);
186 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
189 984245ce 2021-06-23 op if (tab->proxy != NULL) {
190 984245ce 2021-06-23 op host = tab->proxy->host;
191 984245ce 2021-06-23 op port = tab->proxy->port;
193 25905047 2024-01-16 op host = tab->iri.iri_host;
194 25905047 2024-01-16 op port = tab->iri.iri_portstr;
197 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
198 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
199 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
200 cbcc75fb 2021-03-17 op * the certs! */
201 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
204 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
205 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
207 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
208 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
209 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
210 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
212 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
213 4fa88f57 2022-04-24 op tofu_save(&certs, e);
215 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
217 5d1bac73 2021-03-25 op if (tofu_res) {
218 a2fd3805 2021-07-06 op if (e->verified == -1)
219 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
220 a2fd3805 2021-07-06 op else if (e->verified == 1)
221 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
223 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
225 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid, -1,
226 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
228 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
229 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
230 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
232 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
233 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
238 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
240 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
241 bc10f6a5 2021-07-12 op sizeof(accept));
243 a2fd3805 2021-07-06 op if (accept) {
244 25905047 2024-01-16 op const char *host, *port;
246 25905047 2024-01-16 op host = tab->iri.iri_host;
247 25905047 2024-01-16 op port = tab->iri.iri_portstr;
250 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
251 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
252 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
253 7b4e4feb 2021-08-15 op * tabid (one request == one tab id). It also makes
254 7b4e4feb 2021-08-15 op * sense to save it for the current session if the
255 7b4e4feb 2021-08-15 op * user accepted it.
257 25905047 2024-01-16 op tofu_temp_trust(&certs, host, port, tab->cert);
259 d0971653 2021-09-15 op if (!safe_mode)
260 d0971653 2021-09-15 op ui_yornp("Save the new certificate?",
261 d0971653 2021-09-15 op handle_maybe_save_new_cert, tab);
263 d0971653 2021-09-15 op message("Certificate temporarly trusted");
265 288fd238 2021-04-25 op free(tab->cert);
266 288fd238 2021-04-25 op tab->cert = NULL;
271 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
273 288fd238 2021-04-25 op struct tofu_entry *e;
274 984245ce 2021-06-23 op const char *host, *port;
276 984245ce 2021-06-23 op if (tab->proxy != NULL) {
277 984245ce 2021-06-23 op host = tab->proxy->host;
278 984245ce 2021-06-23 op port = tab->proxy->port;
280 25905047 2024-01-16 op host = tab->iri.iri_host;
281 25905047 2024-01-16 op port = tab->iri.iri_portstr;
287 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
290 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
291 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
292 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
293 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
295 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
297 3078e1bc 2022-04-24 op tofu_update_persist(&certs, e);
299 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
302 288fd238 2021-04-25 op free(tab->cert);
303 288fd238 2021-04-25 op tab->cert = NULL;
306 5cd2ebb1 2021-03-11 op static inline int
307 5cd2ebb1 2021-03-11 op normalize_code(int n)
309 5cd2ebb1 2021-03-11 op if (n < 20) {
310 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
313 5cd2ebb1 2021-03-11 op } else if (n < 30) {
315 5cd2ebb1 2021-03-11 op } else if (n < 40) {
316 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
319 5cd2ebb1 2021-03-11 op } else if (n < 50) {
323 5cd2ebb1 2021-03-11 op } else if (n < 60) {
324 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
327 5cd2ebb1 2021-03-11 op } else if (n < 70) {
332 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
336 b148be71 2024-01-31 op handle_request_response(struct tab *tab)
338 fcd99a0d 2021-11-05 op char buf[128];
340 54764e41 2024-02-02 op if (tab->code != 30 && tab->code != 31)
341 54764e41 2024-02-02 op tab->redirect_count = 0;
343 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
344 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
345 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
346 ed504b9e 2022-02-07 op free(tab->last_input_url);
347 65c49665 2024-01-23 op tab->last_input_url = strdup(hist_cur(tab->hist));
348 ed504b9e 2022-02-07 op if (tab->last_input_url == NULL)
351 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
352 693188cb 2022-02-07 op ui_require_input(tab, tab->code == 11, ir_select_gemini);
353 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
354 65c49665 2024-01-23 op history_add(hist_cur(tab->hist));
355 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
356 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, tab->id, -1, NULL, 0);
357 fcd99a0d 2021-11-05 op } else if (safe_mode) {
358 69f32f56 2021-08-16 op load_page_from_str(tab,
359 69f32f56 2021-08-16 op err_pages[UNKNOWN_TYPE_OR_CSET]);
361 65c49665 2024-01-23 op hist_prev(tab->hist);
362 fcd99a0d 2021-11-05 op snprintf(buf, sizeof(buf),
363 fcd99a0d 2021-11-05 op "Can't display \"%s\", save it?", tab->meta);
364 fcd99a0d 2021-11-05 op ui_yornp(buf, handle_maybe_save_page, tab);
366 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
367 3a9b9365 2021-03-09 op tab->redirect_count++;
369 3a9b9365 2021-03-09 op /* TODO: make customizable? */
370 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
371 3a9b9365 2021-03-09 op load_page_from_str(tab,
372 ef88dc16 2024-02-02 op err_pages[TOO_MANY_REDIRECTS]);
374 65c49665 2024-01-23 op do_load_url(tab, tab->meta, hist_cur(tab->hist),
375 33133ab1 2024-01-16 op LU_MODE_NOCACHE);
376 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
377 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
378 5a39f593 2024-02-05 op if (tab->code >= 60)
379 5a39f593 2024-02-05 op cmd_use_certificate(&tab->buffer);
384 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
386 bb9912eb 2021-08-29 op const char *f;
387 bb9912eb 2021-08-29 op char input[PATH_MAX];
389 fcd99a0d 2021-11-05 op /* XXX: this print a message that is confusing */
390 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
392 bb9912eb 2021-08-29 op if (!dosave) {
393 a2fd3805 2021-07-06 op stop_tab(tab);
397 25905047 2024-01-16 op if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
402 112b9046 2021-08-29 op strlcpy(input, download_path, sizeof(input));
403 bb9912eb 2021-08-29 op strlcat(input, f, sizeof(input));
405 bb9912eb 2021-08-29 op ui_read("Save to path", handle_save_page_path, tab, input);
409 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
411 f63b8f73 2022-04-24 op struct download *d;
414 de2a69bb 2021-05-17 op if (path == NULL) {
415 d1353324 2021-07-13 op stop_tab(tab);
419 ff48dbfd 2022-05-05 op if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1) {
420 ff48dbfd 2022-05-05 op message("Can't open file %s: %s", path, strerror(errno));
421 ff48dbfd 2022-05-05 op stop_tab(tab);
425 ff48dbfd 2022-05-05 op ui_show_downloads_pane();
426 fd984e76 2022-05-05 op d = enqueue_download(tab->id, path);
428 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
431 89e46508 2022-05-05 op * Change this tab id, the old one is associated with the
432 89e46508 2022-05-05 op * download now.
434 89e46508 2022-05-05 op tab->id = tab_new_id();
438 b148be71 2024-01-31 op handle_dispatch_imsg(int fd, short event, void *data)
440 b148be71 2024-01-31 op struct imsgev *iev = data;
441 b148be71 2024-01-31 op struct imsgbuf *imsgbuf = &iev->ibuf;
442 b148be71 2024-01-31 op struct imsg imsg;
443 54764e41 2024-02-02 op struct ibuf ibuf;
444 b148be71 2024-01-31 op struct tab *tab;
445 b148be71 2024-01-31 op struct download *d;
446 54764e41 2024-02-02 op const char *h;
447 54764e41 2024-02-02 op char *str, *page;
451 b148be71 2024-01-31 op if (event & EV_READ) {
452 b148be71 2024-01-31 op if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
453 b148be71 2024-01-31 op err(1, "imsg_read");
455 b148be71 2024-01-31 op err(1, "connection closed");
457 b148be71 2024-01-31 op if (event & EV_WRITE) {
458 b148be71 2024-01-31 op if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
459 b148be71 2024-01-31 op err(1, "msgbuf_write");
461 b148be71 2024-01-31 op err(1, "connection closed");
465 b148be71 2024-01-31 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
466 b148be71 2024-01-31 op err(1, "imsg_get");
470 b148be71 2024-01-31 op switch (imsg_get_type(&imsg)) {
471 b148be71 2024-01-31 op case IMSG_ERR:
472 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
474 62cbcdae 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
475 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
477 b148be71 2024-01-31 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
478 b148be71 2024-01-31 op hist_cur(tab->hist), str) == -1)
480 b148be71 2024-01-31 op load_page_from_str(tab, page);
483 b148be71 2024-01-31 op case IMSG_CHECK_CERT:
484 b148be71 2024-01-31 op handle_imsg_check_cert(&imsg);
486 54764e41 2024-02-02 op case IMSG_REPLY:
487 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
489 54764e41 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
490 54764e41 2024-02-02 op ibuf_get(&ibuf, &code, sizeof(code)) == -1 ||
491 54764e41 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
493 b148be71 2024-01-31 op if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
494 b148be71 2024-01-31 op sizeof(tab->meta))
496 54764e41 2024-02-02 op tab->code = normalize_code(code);
497 b148be71 2024-01-31 op handle_request_response(tab);
499 b148be71 2024-01-31 op case IMSG_BUF:
500 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
501 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
505 b148be71 2024-01-31 op if (!parser_parse(tab, imsg.data,
506 b148be71 2024-01-31 op imsg_get_len(&imsg)))
508 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
510 b148be71 2024-01-31 op size_t datalen = imsg_get_len(&imsg);
512 b148be71 2024-01-31 op d->bytes += datalen;
513 b148be71 2024-01-31 op write(d->fd, imsg.data, datalen);
514 b148be71 2024-01-31 op ui_on_download_refresh();
517 b148be71 2024-01-31 op case IMSG_EOF:
518 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
519 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
522 b148be71 2024-01-31 op if (tab != NULL) {
523 b148be71 2024-01-31 op if (!parser_free(tab))
525 b148be71 2024-01-31 op h = hist_cur(tab->hist);
526 b148be71 2024-01-31 op if (!strncmp(h, "gemini://", 9) ||
527 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9) ||
528 b148be71 2024-01-31 op !strncmp(h, "finger://", 9))
529 b148be71 2024-01-31 op mcache_tab(tab);
532 b148be71 2024-01-31 op * Gemini is handled as soon as a 2x
533 b148be71 2024-01-31 op * reply is got.
535 b148be71 2024-01-31 op if (!strncmp(h, "finger://", 9) ||
536 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9))
537 b148be71 2024-01-31 op history_add(h);
539 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
540 b148be71 2024-01-31 op ui_on_tab_loaded(tab);
542 b148be71 2024-01-31 op close(d->fd);
544 b148be71 2024-01-31 op ui_on_download_refresh();
548 b148be71 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
551 b148be71 2024-01-31 op imsg_free(&imsg);
554 b148be71 2024-01-31 op imsg_event_add(iev);
558 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
560 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
561 f63b8f73 2022-04-24 op fs_load_url(tab, url);
562 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
563 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
567 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
569 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
570 f63b8f73 2022-04-24 op fs_load_url(tab, url);
571 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
572 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
576 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
578 5fb52fc0 2021-07-25 op struct get_req req;
579 25905047 2024-01-16 op const char *path;
581 5fb52fc0 2021-07-25 op memset(&req, 0, sizeof(req));
582 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
583 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
586 5fb52fc0 2021-07-25 op * Sometimes the finger url have the user as path component
587 5fb52fc0 2021-07-25 op * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
588 5fb52fc0 2021-07-25 op * userinfo (e.g. finger://cobradile@finger.farm).
590 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_UINFO) {
591 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_uinfo, sizeof(req.req));
593 25905047 2024-01-16 op path = tab->iri.iri_path;
594 25905047 2024-01-16 op while (*path == '/')
596 25905047 2024-01-16 op strlcpy(req.req, path, sizeof(req.req));
598 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
600 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
601 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_FINGER, NULL);
605 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
607 984245ce 2021-06-23 op struct get_req req;
609 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
610 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
611 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
613 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
616 ca190c99 2021-08-14 op static inline const char *
617 ca190c99 2021-08-14 op gopher_skip_selector(const char *path, int *ret_type)
619 ca190c99 2021-08-14 op *ret_type = 0;
621 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
622 ca190c99 2021-08-14 op *ret_type = '1';
626 ca190c99 2021-08-14 op if (*path != '/')
630 ca190c99 2021-08-14 op switch (*ret_type = *path) {
637 ca190c99 2021-08-14 op *ret_type = 0;
642 95bb01e2 2021-08-28 op return ++path;
646 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
648 81839fee 2021-07-25 op struct get_req req;
650 81839fee 2021-07-25 op const char *path;
652 1663bf1e 2021-07-25 op memset(&req, 0, sizeof(req));
653 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
654 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
656 25905047 2024-01-16 op path = gopher_skip_selector(tab->iri.iri_path, &type);
657 ca190c99 2021-08-14 op switch (type) {
659 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
662 ca190c99 2021-08-14 op parser_init(tab, gophermap_initparser);
665 acf9defe 2022-02-08 op free(tab->last_input_url);
666 acf9defe 2022-02-08 op tab->last_input_url = strdup(url);
667 acf9defe 2022-02-08 op if (tab->last_input_url == NULL)
669 693188cb 2022-02-07 op ui_require_input(tab, 0, ir_select_gopher);
670 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[10]);
673 cd1d043d 2024-01-13 op load_page_from_str(tab, "Unknown gopher selector");
677 9a28e7e5 2021-08-03 op strlcpy(req.req, path, sizeof(req.req));
678 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
679 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
680 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
682 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
684 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
688 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
690 984245ce 2021-06-23 op struct get_req req;
692 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
693 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
694 f94c7ac1 2021-11-24 op strlcpy(req.port, p->port, sizeof(req.port));
696 cf1195ce 2021-07-25 op tab->proxy = p;
698 71bc1636 2024-02-06 op make_request(tab, &req, p->proto, hist_cur(tab->hist));
702 71bc1636 2024-02-06 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
704 71bc1636 2024-02-06 op int use_cert = 0, fd = -1;
706 71bc1636 2024-02-06 op if (proto == PROTO_GEMINI) {
707 71bc1636 2024-02-06 op tab->client_cert = cert_for(&tab->iri, &tab->client_cert_temp);
708 71bc1636 2024-02-06 op use_cert = (tab->client_cert != NULL);
711 cf1195ce 2021-07-25 op stop_tab(tab);
712 cf1195ce 2021-07-25 op tab->id = tab_new_id();
713 cf1195ce 2021-07-25 op req->proto = proto;
715 cf1195ce 2021-07-25 op if (r != NULL) {
716 cf1195ce 2021-07-25 op strlcpy(req->req, r, sizeof(req->req));
717 cf1195ce 2021-07-25 op strlcat(req->req, "\r\n", sizeof(req->req));
720 5df02e0d 2022-01-11 op start_loading_anim(tab);
722 d35e18b3 2024-02-04 op if (!use_cert)
723 d35e18b3 2024-02-04 op tab->client_cert = NULL;
724 d35e18b3 2024-02-04 op if (use_cert && (fd = cert_open(tab->client_cert)) == -1) {
725 d35e18b3 2024-02-04 op tab->client_cert = NULL;
726 d35e18b3 2024-02-04 op message("failed to open certificate: %s", strerror(errno));
729 d35e18b3 2024-02-04 op ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
733 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
735 9a28e7e5 2021-08-03 op struct get_req req;
737 9a28e7e5 2021-08-03 op memset(&req, 0, sizeof(req));
738 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
739 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
741 9a28e7e5 2021-08-03 op /* +2 to skip /7 */
742 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_path+2, sizeof(req.req));
743 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
744 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
745 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
748 9a28e7e5 2021-08-03 op strlcat(req.req, "\t", sizeof(req.req));
749 9a28e7e5 2021-08-03 op strlcat(req.req, text, sizeof(req.req));
750 9a28e7e5 2021-08-03 op strlcat(req.req, "\r\n", sizeof(req.req));
752 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
753 9a28e7e5 2021-08-03 op parser_init(tab, gophermap_initparser);
755 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
759 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
761 1bcb8303 2022-01-19 op parser_init(tab, gemtext_initparser);
762 1bcb8303 2022-01-19 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
764 1bcb8303 2022-01-19 op if (!tab->buffer.page.free(&tab->buffer.page))
766 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
767 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
771 cd1d043d 2024-01-13 op * Effectively load the given url in the given tab.
774 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
776 0d877486 2022-02-24 op const struct proto *p;
777 984245ce 2021-06-23 op struct proxy *proxy;
778 8f3c9af8 2022-01-11 op int nocache = mode & LU_MODE_NOCACHE;
780 65c49665 2024-01-23 op char buf[1025];
782 7943bb81 2021-07-10 op tab->proxy = NULL;
783 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
785 25905047 2024-01-16 op if (iri_parse(base, url, &tab->iri) == -1) {
786 25905047 2024-01-16 op if (asprintf(&t, "# error loading %s\n>%s\n",
787 25905047 2024-01-16 op url, "Can't parse the IRI") == -1)
789 65c49665 2024-01-23 op hist_set_cur(tab->hist, url);
790 31f1a758 2021-04-22 op load_page_from_str(tab, t);
795 65c49665 2024-01-23 op iri_unparse(&tab->iri, buf, sizeof(buf));
796 65c49665 2024-01-23 op hist_set_cur(tab->hist, buf);
798 65c49665 2024-01-23 op if (!nocache && mcache_lookup(buf, tab)) {
799 8f3c9af8 2022-01-11 op ui_on_tab_refresh(tab);
800 8f3c9af8 2022-01-11 op ui_on_tab_loaded(tab);
804 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
805 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, p->schema)) {
806 3b726df7 2024-01-16 op /* patch the port */
807 25905047 2024-01-16 op if (*tab->iri.iri_portstr == '\0' &&
808 25905047 2024-01-16 op p->port != NULL)
809 3b726df7 2024-01-16 op iri_setport(&tab->iri, p->port);
811 65c49665 2024-01-23 op p->loadfn(tab, buf);
816 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
817 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, proxy->match_proto)) {
818 cd1d043d 2024-01-13 op load_via_proxy(tab, url, proxy);
823 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
827 ea639250 2021-01-02 op * Load url in tab and handle history. If a tab is marked as lazy, only
828 ea639250 2021-01-02 op * prepare the url but don't load it.
831 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
833 65c49665 2024-01-23 op size_t line_off, curr_off;
834 ea639250 2021-01-02 op int lazy = tab->flags & TAB_LAZY;
835 65c49665 2024-01-23 op int dohist = !(mode & LU_MODE_NOHIST);
837 fb8dcd1c 2022-01-18 op if (operating && lazy) {
838 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
840 65c49665 2024-01-23 op } else if (hist_size(tab->hist) != 0) {
841 65c49665 2024-01-23 op get_scroll_position(tab, &line_off, &curr_off);
842 65c49665 2024-01-23 op hist_set_offs(tab->hist, line_off, curr_off);
845 65c49665 2024-01-23 op if (dohist) {
846 65c49665 2024-01-23 op if (hist_push(tab->hist, url) == -1) {
847 2ddbda6b 2021-07-17 op event_loopbreak();
851 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
852 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
856 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
860 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
862 0259f38d 2022-01-11 op if (operating) {
863 0259f38d 2022-01-11 op autosave_hook();
864 0259f38d 2022-01-11 op message("Loading %s...", url);
867 0f7d8271 2024-01-22 op if (base == NULL)
868 65c49665 2024-01-23 op base = hist_cur(tab->hist);
870 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
874 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
876 65c49665 2024-01-23 op const char *h;
878 65c49665 2024-01-23 op if ((h = hist_prev(tab->hist)) == NULL)
880 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
885 2051e653 2021-03-13 op load_next_page(struct tab *tab)
887 65c49665 2024-01-23 op const char *h;
889 65c49665 2024-01-23 op if ((h = hist_next(tab->hist)) == NULL)
891 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
896 868b3a8f 2022-04-13 op write_buffer(const char *path, struct tab *tab)
900 868b3a8f 2022-04-13 op if (path == NULL)
903 f63b8f73 2022-04-24 op if ((fp = fopen(path, "w")) == NULL)
905 f63b8f73 2022-04-24 op if (!parser_serialize(tab, fp))
906 f63b8f73 2022-04-24 op message("Failed to save the page.");
911 449ea6fe 2024-01-16 op * Given a user-entered URL, apply some heuristics to use it if
912 449ea6fe 2024-01-16 op * load-url-use-heuristic allows it.
914 f6d7d047 2021-12-30 op * - if it's a proper url use it
915 f6d7d047 2021-12-30 op * - if it starts with a `./' or a `/' assume its a file:// url
916 cfcbc23c 2024-01-16 op * - assume it's a default-protocol:// url
918 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
922 449ea6fe 2024-01-16 op humanify_url(const char *raw, const char *base, char *ret, size_t len)
924 44008d59 2024-01-16 op static struct iri iri;
926 449ea6fe 2024-01-16 op if (load_url_use_heuristic)
929 449ea6fe 2024-01-16 op if (iri_parse(base, raw, &iri) == 0) {
930 44008d59 2024-01-16 op iri_unparse(&iri, ret, len);
934 d89eb764 2022-04-24 op if (!strncmp(raw, "./", 2)) {
935 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
936 449ea6fe 2024-01-16 op strlcat(ret, cwd, len);
937 c6efff96 2021-08-16 op strlcat(ret, "/", len);
938 c6efff96 2021-08-16 op strlcat(ret, raw+2, len);
942 c6efff96 2021-08-16 op if (*raw == '/') {
943 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
944 c6efff96 2021-08-16 op strlcat(ret, raw, len);
948 cfcbc23c 2024-01-16 op strlcpy(ret, default_protocol, len);
949 cfcbc23c 2024-01-16 op strlcat(ret, "://", len);
950 c6efff96 2021-08-16 op strlcat(ret, raw, len);
954 486bde7d 2022-04-24 op bookmark_page(const char *url)
958 486bde7d 2022-04-24 op if ((fp = fopen(bookmark_file, "a")) == NULL)
960 486bde7d 2022-04-24 op fprintf(fp, "=> %s\n", url);
966 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
968 55ccd6d0 2021-09-15 op const char *argv[5];
969 6cc5fcfe 2021-07-08 op int argc = 0;
972 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
982 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
983 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
985 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
987 6cc5fcfe 2021-07-08 op case PROC_UI:
988 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
989 6cc5fcfe 2021-07-08 op case PROC_NET:
990 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
994 55ccd6d0 2021-09-15 op if (safe_mode)
995 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
997 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
998 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
999 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
1003 4cf6ba13 2022-02-11 op send_url(const char *url)
1005 4cf6ba13 2022-02-11 op struct sockaddr_un sun;
1006 4cf6ba13 2022-02-11 op struct imsgbuf ibuf;
1007 4cf6ba13 2022-02-11 op int ctl_sock;
1009 4cf6ba13 2022-02-11 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1010 4cf6ba13 2022-02-11 op err(1, "socket");
1012 4cf6ba13 2022-02-11 op memset(&sun, 0, sizeof(sun));
1013 4cf6ba13 2022-02-11 op sun.sun_family = AF_UNIX;
1014 4cf6ba13 2022-02-11 op strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1016 4cf6ba13 2022-02-11 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1017 4cf6ba13 2022-02-11 op err(1, "connect: %s", ctlsock_path);
1019 4cf6ba13 2022-02-11 op imsg_init(&ibuf, ctl_sock);
1020 4cf6ba13 2022-02-11 op imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1021 4cf6ba13 2022-02-11 op strlen(url) + 1);
1022 4cf6ba13 2022-02-11 op imsg_flush(&ibuf);
1023 4cf6ba13 2022-02-11 op close(ctl_sock);
1027 d35e18b3 2024-02-04 op ui_send_net(int type, uint32_t peerid, int fd, const void *data,
1028 bc10f6a5 2021-07-12 op uint16_t datalen)
1030 d35e18b3 2024-02-04 op return imsg_compose_event(iev_net, type, peerid, 0, fd, data,
1034 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1035 6cc5fcfe 2021-07-08 op usage(int r)
1037 335cfaec 2021-09-15 op fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1038 dc761924 2021-07-15 op getprogname());
1039 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1044 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1046 f63b8f73 2022-04-24 op struct imsgev net_ibuf;
1048 4cf6ba13 2022-02-11 op int control_fd;
1049 f63b8f73 2022-04-24 op int pipe2net[2];
1050 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
1051 6cc5fcfe 2021-07-08 op int proc = -1;
1052 5d7c642a 2021-09-15 op int sessionfd = -1;
1054 6cc5fcfe 2021-07-08 op const char *argv0;
1056 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1058 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
1059 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1061 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1062 d2544989 2021-07-08 op enable_colors = 0;
1064 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1065 d2544989 2021-07-08 op switch (ch) {
1068 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1071 d2544989 2021-07-08 op configtest = 1;
1076 d0971653 2021-09-15 op safe_mode = 1;
1079 6cc5fcfe 2021-07-08 op switch (*optarg) {
1081 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1084 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1089 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1097 d2544989 2021-07-08 op argc -= optind;
1098 d2544989 2021-07-08 op argv += optind;
1100 6cc5fcfe 2021-07-08 op if (proc != -1) {
1101 6cc5fcfe 2021-07-08 op if (argc > 0)
1103 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1104 f45bd2e3 2021-07-24 op return net_main();
1109 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1110 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1111 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1112 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1115 d35e18b3 2024-02-04 op certs_init(certs_file);
1116 d2544989 2021-07-08 op config_init();
1117 eb2ed626 2021-10-07 op parseconfig(config_path, fail);
1118 69f32f56 2021-08-16 op if (configtest) {
1119 d2544989 2021-07-08 op puts("config OK");
1123 cfcbc23c 2024-01-16 op if (default_protocol == NULL &&
1124 cfcbc23c 2024-01-16 op (default_protocol = strdup("gemini")) == NULL)
1125 cfcbc23c 2024-01-16 op err(1, "strdup");
1127 112b9046 2021-08-29 op if (download_path == NULL &&
1128 112b9046 2021-08-29 op (download_path = strdup("/tmp/")) == NULL)
1129 112b9046 2021-08-29 op errx(1, "strdup");
1131 449ea6fe 2024-01-16 op if (argc != 0) {
1134 449ea6fe 2024-01-16 op if (asprintf(&base, "file://%s/", cwd) == -1)
1135 449ea6fe 2024-01-16 op err(1, "asprintf");
1137 449ea6fe 2024-01-16 op has_url = 1;
1138 449ea6fe 2024-01-16 op humanify_url(argv[0], base, url, sizeof(url));
1143 4cf6ba13 2022-02-11 op if (!safe_mode && (sessionfd = lock_session()) == -1) {
1144 4cf6ba13 2022-02-11 op if (has_url) {
1145 4cf6ba13 2022-02-11 op send_url(url);
1149 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1150 d0fd368a 2021-07-15 op "telescope already running?");
1153 6cc5fcfe 2021-07-08 op /* Start children. */
1154 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1155 35e1f40a 2021-03-14 op err(1, "socketpair");
1156 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1157 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1158 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1159 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1161 37d429b0 2021-04-01 op setproctitle("ui");
1163 eb722b50 2022-01-03 op /* initialize tofu store */
1164 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1166 5e11c00c 2021-03-02 op event_init();
1168 4cf6ba13 2022-02-11 op if (!safe_mode) {
1169 4cf6ba13 2022-02-11 op if ((control_fd = control_init(ctlsock_path)) == -1)
1170 4cf6ba13 2022-02-11 op err(1, "control_init %s", ctlsock_path);
1171 4cf6ba13 2022-02-11 op control_listen(control_fd);
1174 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1175 d5af38cc 2022-01-10 op mcache_init();
1177 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1178 1fce2e75 2021-08-14 op autosave_init();
1180 f63b8f73 2022-04-24 op /* Setup event handlers for pipes to net */
1181 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1182 bc10f6a5 2021-07-12 op event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1183 bc10f6a5 2021-07-12 op iev_net->handler, iev_net);
1184 bc10f6a5 2021-07-12 op event_add(&iev_net->ev, NULL);
1186 bb28f1c2 2021-12-30 op if (ui_init()) {
1187 941b3761 2021-03-18 op sandbox_ui_process();
1188 64f4f8e2 2022-04-24 op load_session(&certs);
1189 d521a6a4 2022-05-11 op if (has_url)
1190 d521a6a4 2022-05-11 op new_tab(url, NULL, NULL);
1191 f63b8f73 2022-04-24 op operating = 1;
1192 f63b8f73 2022-04-24 op switch_to_tab(current_tab);
1193 f63b8f73 2022-04-24 op ui_main_loop();
1197 d35e18b3 2024-02-04 op ui_send_net(IMSG_QUIT, 0, -1, NULL, 0);
1198 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
1200 be97d6e6 2021-08-15 op /* wait for children to terminate */
1202 be97d6e6 2021-08-15 op pid = wait(&status);
1203 be97d6e6 2021-08-15 op if (pid == -1) {
1204 be97d6e6 2021-08-15 op if (errno != EINTR && errno != ECHILD)
1205 be97d6e6 2021-08-15 op err(1, "wait");
1206 be97d6e6 2021-08-15 op } else if (WIFSIGNALED(status))
1207 be97d6e6 2021-08-15 op warnx("child terminated; signal %d", WTERMSIG(status));
1208 be97d6e6 2021-08-15 op } while (pid != -1 || (pid == -1 && errno == EINTR));
1210 f63b8f73 2022-04-24 op if (!safe_mode)
1211 f63b8f73 2022-04-24 op unlink(crashed_file);
1213 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1214 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);