Blame


1 4540067e 2021-07-18 op /*
2 44008d59 2024-01-16 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 4540067e 2021-07-18 op *
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.
7 4540067e 2021-07-18 op *
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.
15 4540067e 2021-07-18 op */
16 1f496e7b 2021-07-21 op
17 1f496e7b 2021-07-21 op #include "compat.h"
18 4540067e 2021-07-18 op
19 5e11c00c 2021-03-02 op #include <sys/socket.h>
20 98d3e6c1 2024-02-18 op #include <sys/time.h>
21 4cf6ba13 2022-02-11 op #include <sys/un.h>
22 be97d6e6 2021-08-15 op #include <sys/wait.h>
23 5e11c00c 2021-03-02 op
24 be97d6e6 2021-08-15 op #include <errno.h>
25 f63b8f73 2022-04-24 op #include <fcntl.h>
26 dc761924 2021-07-15 op #include <getopt.h>
27 f63b8f73 2022-04-24 op #include <limits.h>
28 5e11c00c 2021-03-02 op #include <signal.h>
29 5e11c00c 2021-03-02 op #include <stdio.h>
30 5e11c00c 2021-03-02 op #include <stdlib.h>
31 5e11c00c 2021-03-02 op #include <string.h>
32 5e11c00c 2021-03-02 op #include <unistd.h>
33 5e11c00c 2021-03-02 op
34 d35e18b3 2024-02-04 op #include "certs.h"
35 d252d87c 2024-02-05 op #include "cmd.h"
36 4cf6ba13 2022-02-11 op #include "control.h"
37 e659558c 2021-07-13 op #include "defaults.h"
38 98d3e6c1 2024-02-18 op #include "ev.h"
39 4575b59c 2024-06-26 op #include "exec.h"
40 eb2ed626 2021-10-07 op #include "fs.h"
41 65c49665 2024-01-23 op #include "hist.h"
42 87d297d1 2024-02-22 op #include "imsgev.h"
43 44008d59 2024-01-16 op #include "iri.h"
44 f7db6d13 2024-02-06 op #include "keymap.h"
45 7b7a69f2 2024-06-13 thomas.ad #include "mailcap.h"
46 d5af38cc 2022-01-10 op #include "mcache.h"
47 4bc446b9 2021-07-21 op #include "minibuffer.h"
48 395b9f4e 2021-07-12 op #include "parser.h"
49 c1d27b0e 2024-06-14 op #include "parser.h"
50 1fce2e75 2021-08-14 op #include "session.h"
51 395b9f4e 2021-07-12 op #include "telescope.h"
52 d163c210 2024-02-06 op #include "tofu.h"
53 d1a0f2a3 2021-07-12 op #include "ui.h"
54 9d65b1d9 2022-01-11 op #include "utils.h"
55 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
56 dc761924 2021-07-15 op
57 0d877486 2022-02-24 op static const struct option longopts[] = {
58 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
59 d0971653 2021-09-15 op {"safe", no_argument, NULL, 'S'},
60 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
61 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
62 dc761924 2021-07-15 op };
63 dc761924 2021-07-15 op
64 d08fccd2 2024-01-23 op static const char *opts = "c:hnST:v";
65 a37d1a1c 2021-08-14 op
66 bb28f1c2 2021-12-30 op static int has_url;
67 bb28f1c2 2021-12-30 op static char url[GEMINI_URL_LEN];
68 bb28f1c2 2021-12-30 op
69 a37d1a1c 2021-08-14 op /*
70 a37d1a1c 2021-08-14 op * Used to know when we're finished loading.
71 a37d1a1c 2021-08-14 op */
72 1fce2e75 2021-08-14 op int operating;
73 395b9f4e 2021-07-12 op
74 d0971653 2021-09-15 op /*
75 d0971653 2021-09-15 op * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
76 d0971653 2021-09-15 op * anything to the filesystem or execute external programs.
77 d0971653 2021-09-15 op */
78 d0971653 2021-09-15 op int safe_mode;
79 d0971653 2021-09-15 op
80 f63b8f73 2022-04-24 op static struct imsgev *iev_net;
81 ecfbb1d4 2021-08-14 op
82 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
83 6c74799d 2022-01-05 op struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
84 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
85 6cc5fcfe 2021-07-08 op
86 6cc5fcfe 2021-07-08 op enum telescope_process {
87 6cc5fcfe 2021-07-08 op PROC_UI,
88 6cc5fcfe 2021-07-08 op PROC_NET,
89 6cc5fcfe 2021-07-08 op };
90 5e11c00c 2021-03-02 op
91 9a8a7402 2021-07-18 op #define CANNOT_FETCH 0
92 ef88dc16 2024-02-02 op #define TOO_MANY_REDIRECTS 1
93 9a8a7402 2021-07-18 op #define MALFORMED_RESPONSE 2
94 9a8a7402 2021-07-18 op #define UNKNOWN_TYPE_OR_CSET 3
95 9a8a7402 2021-07-18 op #define UNKNOWN_PROTOCOL 4
96 9a8a7402 2021-07-18 op
97 a21e0756 2021-08-14 op /* XXX: keep in sync with normalize_code */
98 9a8a7402 2021-07-18 op const char *err_pages[70] = {
99 9a8a7402 2021-07-18 op [CANNOT_FETCH] = "# Couldn't load the page\n",
100 ef88dc16 2024-02-02 op [TOO_MANY_REDIRECTS] = "# Too many redirects\n",
101 9a8a7402 2021-07-18 op [MALFORMED_RESPONSE] = "# Got a malformed response\n",
102 5014b6a9 2024-02-02 op [UNKNOWN_TYPE_OR_CSET] = "# Unsupported document format or charset\n",
103 9a8a7402 2021-07-18 op [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
104 9a8a7402 2021-07-18 op
105 9a8a7402 2021-07-18 op [10] = "# Input required\n",
106 9a8a7402 2021-07-18 op [11] = "# Input required\n",
107 9a8a7402 2021-07-18 op [40] = "# Temporary failure\n",
108 9a8a7402 2021-07-18 op [41] = "# Server unavailable\n",
109 9a8a7402 2021-07-18 op [42] = "# CGI error\n",
110 9a8a7402 2021-07-18 op [43] = "# Proxy error\n",
111 9a8a7402 2021-07-18 op [44] = "# Slow down\n",
112 9a8a7402 2021-07-18 op [50] = "# Permanent failure\n",
113 9a8a7402 2021-07-18 op [51] = "# Not found\n",
114 9a8a7402 2021-07-18 op [52] = "# Gone\n",
115 9a8a7402 2021-07-18 op [53] = "# Proxy request refused\n",
116 9a8a7402 2021-07-18 op [59] = "# Bad request\n",
117 9a8a7402 2021-07-18 op [60] = "# Client certificate required\n",
118 9a8a7402 2021-07-18 op [61] = "# Certificate not authorised\n",
119 9a8a7402 2021-07-18 op [62] = "# Certificate not valid\n"
120 9a8a7402 2021-07-18 op };
121 9a8a7402 2021-07-18 op
122 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
123 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
124 b148be71 2024-01-31 op static void handle_imsg_check_cert(struct imsg *);
125 057e7eb6 2024-06-22 op static void handle_check_cert_user_choice(int, void *);
126 057e7eb6 2024-06-22 op static void handle_maybe_save_new_cert(int, void *);
127 057e7eb6 2024-06-22 op static void handle_maybe_save_page(int, void *);
128 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
129 98d3e6c1 2024-02-18 op static void handle_dispatch_imsg(int, int, void *);
130 cd1d043d 2024-01-13 op static void load_about_url(struct tab *, const char *);
131 cd1d043d 2024-01-13 op static void load_file_url(struct tab *, const char *);
132 cd1d043d 2024-01-13 op static void load_finger_url(struct tab *, const char *);
133 cd1d043d 2024-01-13 op static void load_gemini_url(struct tab *, const char *);
134 cd1d043d 2024-01-13 op static void load_gopher_url(struct tab *, const char *);
135 cd1d043d 2024-01-13 op static void load_via_proxy(struct tab *, const char *,
136 cd1d043d 2024-01-13 op struct proxy *);
137 cd1d043d 2024-01-13 op static void make_request(struct tab *, struct get_req *, int,
138 71bc1636 2024-02-06 op const char *);
139 cd1d043d 2024-01-13 op static void do_load_url(struct tab *, const char *, const char *, int);
140 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
141 4cf6ba13 2022-02-11 op static void send_url(const char *);
142 dfb5eb39 2021-07-25 op
143 0d877486 2022-02-24 op static const struct proto {
144 352e0355 2021-07-25 op const char *schema;
145 ba782cb8 2021-07-25 op const char *port;
146 cd1d043d 2024-01-13 op void (*loadfn)(struct tab *, const char *);
147 352e0355 2021-07-25 op } protos[] = {
148 ba782cb8 2021-07-25 op {"about", NULL, load_about_url},
149 0b1053d2 2021-08-13 op {"file", NULL, load_file_url},
150 ba782cb8 2021-07-25 op {"finger", "79", load_finger_url},
151 ba782cb8 2021-07-25 op {"gemini", "1965", load_gemini_url},
152 1663bf1e 2021-07-25 op {"gopher", "70", load_gopher_url},
153 ba782cb8 2021-07-25 op {NULL, NULL, NULL},
154 dfb5eb39 2021-07-25 op };
155 5e11c00c 2021-03-02 op
156 cbcc75fb 2021-03-17 op static struct ohash certs;
157 cbcc75fb 2021-03-17 op
158 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
159 5e11c00c 2021-03-02 op die(void)
160 5e11c00c 2021-03-02 op {
161 5e11c00c 2021-03-02 op abort(); /* TODO */
162 5e11c00c 2021-03-02 op }
163 5e11c00c 2021-03-02 op
164 5e11c00c 2021-03-02 op static struct tab *
165 f6243012 2021-07-19 op tab_by_id(uint32_t id)
166 5e11c00c 2021-03-02 op {
167 5e11c00c 2021-03-02 op struct tab *t;
168 5e11c00c 2021-03-02 op
169 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
170 5e11c00c 2021-03-02 op if (t->id == id)
171 5e11c00c 2021-03-02 op return t;
172 5e11c00c 2021-03-02 op }
173 5e11c00c 2021-03-02 op
174 5d6c2088 2021-07-19 op return NULL;
175 5e11c00c 2021-03-02 op }
176 5e11c00c 2021-03-02 op
177 5e11c00c 2021-03-02 op static void
178 b148be71 2024-01-31 op handle_imsg_check_cert(struct imsg *imsg)
179 5e11c00c 2021-03-02 op {
180 62cbcdae 2024-02-02 op char *hash;
181 62cbcdae 2024-02-02 op const char *host, *port;
182 cbcc75fb 2021-03-17 op int tofu_res;
183 62cbcdae 2024-02-02 op struct ibuf ibuf;
184 cbcc75fb 2021-03-17 op struct tofu_entry *e;
185 cbcc75fb 2021-03-17 op struct tab *tab;
186 b148be71 2024-01-31 op size_t datalen;
187 5e11c00c 2021-03-02 op
188 62cbcdae 2024-02-02 op if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
189 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &hash) == -1)
190 cbcc75fb 2021-03-17 op abort();
191 b148be71 2024-01-31 op datalen = strlen(hash);
192 cbcc75fb 2021-03-17 op
193 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
194 f6243012 2021-07-19 op return;
195 cbcc75fb 2021-03-17 op
196 984245ce 2021-06-23 op if (tab->proxy != NULL) {
197 984245ce 2021-06-23 op host = tab->proxy->host;
198 984245ce 2021-06-23 op port = tab->proxy->port;
199 984245ce 2021-06-23 op } else {
200 25905047 2024-01-16 op host = tab->iri.iri_host;
201 25905047 2024-01-16 op port = tab->iri.iri_portstr;
202 984245ce 2021-06-23 op }
203 984245ce 2021-06-23 op
204 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
205 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
206 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
207 cbcc75fb 2021-03-17 op * the certs! */
208 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
209 cbcc75fb 2021-03-17 op abort();
210 cbcc75fb 2021-03-17 op
211 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
212 3d89457c 2024-06-18 thomas.ad e = xcalloc(1, sizeof(*e));
213 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
214 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
215 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
216 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
217 eb4388ee 2021-04-25 op }
218 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
219 4fa88f57 2022-04-24 op tofu_save(&certs, e);
220 cbcc75fb 2021-03-17 op } else
221 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
222 cbcc75fb 2021-03-17 op
223 5d1bac73 2021-03-25 op if (tofu_res) {
224 a2fd3805 2021-07-06 op if (e->verified == -1)
225 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
226 a2fd3805 2021-07-06 op else if (e->verified == 1)
227 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
228 a2fd3805 2021-07-06 op else
229 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
230 a2fd3805 2021-07-06 op
231 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid, -1,
232 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
233 5d1bac73 2021-03-25 op } else {
234 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
235 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
236 3d89457c 2024-06-18 thomas.ad tab->cert = xstrdup(hash);
237 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
238 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
239 cbcc75fb 2021-03-17 op }
240 5d1bac73 2021-03-25 op }
241 5d1bac73 2021-03-25 op
242 5d1bac73 2021-03-25 op static void
243 057e7eb6 2024-06-22 op handle_check_cert_user_choice(int accept, void *d)
244 5d1bac73 2021-03-25 op {
245 057e7eb6 2024-06-22 op struct tab *tab = d;
246 057e7eb6 2024-06-22 op
247 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
248 bc10f6a5 2021-07-12 op sizeof(accept));
249 288fd238 2021-04-25 op
250 a2fd3805 2021-07-06 op if (accept) {
251 25905047 2024-01-16 op const char *host, *port;
252 25905047 2024-01-16 op
253 25905047 2024-01-16 op host = tab->iri.iri_host;
254 25905047 2024-01-16 op port = tab->iri.iri_portstr;
255 25905047 2024-01-16 op
256 a2fd3805 2021-07-06 op /*
257 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
258 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
259 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
260 7b4e4feb 2021-08-15 op * tabid (one request == one tab id). It also makes
261 7b4e4feb 2021-08-15 op * sense to save it for the current session if the
262 7b4e4feb 2021-08-15 op * user accepted it.
263 a2fd3805 2021-07-06 op */
264 25905047 2024-01-16 op tofu_temp_trust(&certs, host, port, tab->cert);
265 a2fd3805 2021-07-06 op
266 d0971653 2021-09-15 op if (!safe_mode)
267 d0971653 2021-09-15 op ui_yornp("Save the new certificate?",
268 d0971653 2021-09-15 op handle_maybe_save_new_cert, tab);
269 d0971653 2021-09-15 op else
270 d0971653 2021-09-15 op message("Certificate temporarly trusted");
271 a2fd3805 2021-07-06 op } else {
272 288fd238 2021-04-25 op free(tab->cert);
273 288fd238 2021-04-25 op tab->cert = NULL;
274 288fd238 2021-04-25 op }
275 5e11c00c 2021-03-02 op }
276 5e11c00c 2021-03-02 op
277 288fd238 2021-04-25 op static void
278 057e7eb6 2024-06-22 op handle_maybe_save_new_cert(int accept, void *data)
279 288fd238 2021-04-25 op {
280 057e7eb6 2024-06-22 op struct tab *tab = data;
281 288fd238 2021-04-25 op struct tofu_entry *e;
282 984245ce 2021-06-23 op const char *host, *port;
283 288fd238 2021-04-25 op
284 984245ce 2021-06-23 op if (tab->proxy != NULL) {
285 984245ce 2021-06-23 op host = tab->proxy->host;
286 984245ce 2021-06-23 op port = tab->proxy->port;
287 984245ce 2021-06-23 op } else {
288 25905047 2024-01-16 op host = tab->iri.iri_host;
289 25905047 2024-01-16 op port = tab->iri.iri_portstr;
290 984245ce 2021-06-23 op }
291 984245ce 2021-06-23 op
292 288fd238 2021-04-25 op if (!accept)
293 288fd238 2021-04-25 op goto end;
294 288fd238 2021-04-25 op
295 3d89457c 2024-06-18 thomas.ad e = xcalloc(1, sizeof(*e));
296 288fd238 2021-04-25 op
297 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
298 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
299 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
300 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
301 288fd238 2021-04-25 op }
302 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
303 288fd238 2021-04-25 op
304 3078e1bc 2022-04-24 op tofu_update_persist(&certs, e);
305 288fd238 2021-04-25 op
306 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
307 288fd238 2021-04-25 op
308 288fd238 2021-04-25 op end:
309 288fd238 2021-04-25 op free(tab->cert);
310 288fd238 2021-04-25 op tab->cert = NULL;
311 288fd238 2021-04-25 op }
312 288fd238 2021-04-25 op
313 5cd2ebb1 2021-03-11 op static inline int
314 5cd2ebb1 2021-03-11 op normalize_code(int n)
315 5cd2ebb1 2021-03-11 op {
316 5cd2ebb1 2021-03-11 op if (n < 20) {
317 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
318 5cd2ebb1 2021-03-11 op return n;
319 5cd2ebb1 2021-03-11 op return 10;
320 5cd2ebb1 2021-03-11 op } else if (n < 30) {
321 5cd2ebb1 2021-03-11 op return 20;
322 5cd2ebb1 2021-03-11 op } else if (n < 40) {
323 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
324 5cd2ebb1 2021-03-11 op return n;
325 5cd2ebb1 2021-03-11 op return 30;
326 5cd2ebb1 2021-03-11 op } else if (n < 50) {
327 5cd2ebb1 2021-03-11 op if (n <= 44)
328 5cd2ebb1 2021-03-11 op return n;
329 5cd2ebb1 2021-03-11 op return 40;
330 5cd2ebb1 2021-03-11 op } else if (n < 60) {
331 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
332 5cd2ebb1 2021-03-11 op return n;
333 5cd2ebb1 2021-03-11 op return 50;
334 5cd2ebb1 2021-03-11 op } else if (n < 70) {
335 5cd2ebb1 2021-03-11 op if (n <= 62)
336 5cd2ebb1 2021-03-11 op return n;
337 5cd2ebb1 2021-03-11 op return 60;
338 5cd2ebb1 2021-03-11 op } else
339 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
340 5cd2ebb1 2021-03-11 op }
341 5cd2ebb1 2021-03-11 op
342 5e11c00c 2021-03-02 op static void
343 b148be71 2024-01-31 op handle_request_response(struct tab *tab)
344 5e11c00c 2021-03-02 op {
345 fcd99a0d 2021-11-05 op char buf[128];
346 54764e41 2024-02-02 op
347 54764e41 2024-02-02 op if (tab->code != 30 && tab->code != 31)
348 54764e41 2024-02-02 op tab->redirect_count = 0;
349 0972d8b2 2021-03-02 op
350 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
351 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
352 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
353 ed504b9e 2022-02-07 op free(tab->last_input_url);
354 3d89457c 2024-06-18 thomas.ad tab->last_input_url = xstrdup(hist_cur(tab->hist));
355 ed504b9e 2022-02-07 op
356 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
357 693188cb 2022-02-07 op ui_require_input(tab, tab->code == 11, ir_select_gemini);
358 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
359 65c49665 2024-01-23 op history_add(hist_cur(tab->hist));
360 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
361 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, tab->id, -1, NULL, 0);
362 fcd99a0d 2021-11-05 op } else if (safe_mode) {
363 69f32f56 2021-08-16 op load_page_from_str(tab,
364 69f32f56 2021-08-16 op err_pages[UNKNOWN_TYPE_OR_CSET]);
365 fcd99a0d 2021-11-05 op } else {
366 65c49665 2024-01-23 op hist_prev(tab->hist);
367 fcd99a0d 2021-11-05 op snprintf(buf, sizeof(buf),
368 fcd99a0d 2021-11-05 op "Can't display \"%s\", save it?", tab->meta);
369 fcd99a0d 2021-11-05 op ui_yornp(buf, handle_maybe_save_page, tab);
370 c07ac996 2021-03-12 op }
371 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
372 3a9b9365 2021-03-09 op tab->redirect_count++;
373 0972d8b2 2021-03-02 op
374 3a9b9365 2021-03-09 op /* TODO: make customizable? */
375 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
376 3a9b9365 2021-03-09 op load_page_from_str(tab,
377 ef88dc16 2024-02-02 op err_pages[TOO_MANY_REDIRECTS]);
378 5cd2ebb1 2021-03-11 op } else
379 65c49665 2024-01-23 op do_load_url(tab, tab->meta, hist_cur(tab->hist),
380 33133ab1 2024-01-16 op LU_MODE_NOCACHE);
381 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
382 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
383 7a7f8f40 2024-10-22 op if (tab->code >= 60) {
384 5a39f593 2024-02-05 op cmd_use_certificate(&tab->buffer);
385 7a7f8f40 2024-10-22 op ui_force_tab_refresh(tab);
386 7a7f8f40 2024-10-22 op }
387 3a9b9365 2021-03-09 op }
388 de2a69bb 2021-05-17 op }
389 de2a69bb 2021-05-17 op
390 de2a69bb 2021-05-17 op static void
391 057e7eb6 2024-06-22 op handle_maybe_save_page(int dosave, void *data)
392 de2a69bb 2021-05-17 op {
393 057e7eb6 2024-06-22 op struct tab *tab = data;
394 bb9912eb 2021-08-29 op const char *f;
395 bb9912eb 2021-08-29 op char input[PATH_MAX];
396 bb9912eb 2021-08-29 op
397 fcd99a0d 2021-11-05 op /* XXX: this print a message that is confusing */
398 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
399 fcd99a0d 2021-11-05 op
400 bb9912eb 2021-08-29 op if (!dosave) {
401 a2fd3805 2021-07-06 op stop_tab(tab);
402 bb9912eb 2021-08-29 op return;
403 bb9912eb 2021-08-29 op }
404 bb9912eb 2021-08-29 op
405 25905047 2024-01-16 op if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
406 bb9912eb 2021-08-29 op f = "";
407 bb9912eb 2021-08-29 op else
408 bb9912eb 2021-08-29 op f++;
409 bb9912eb 2021-08-29 op
410 112b9046 2021-08-29 op strlcpy(input, download_path, sizeof(input));
411 bb9912eb 2021-08-29 op strlcat(input, f, sizeof(input));
412 bb9912eb 2021-08-29 op
413 bb9912eb 2021-08-29 op ui_read("Save to path", handle_save_page_path, tab, input);
414 de2a69bb 2021-05-17 op }
415 de2a69bb 2021-05-17 op
416 de2a69bb 2021-05-17 op static void
417 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
418 d1353324 2021-07-13 op {
419 f63b8f73 2022-04-24 op struct download *d;
420 f63b8f73 2022-04-24 op int fd;
421 f63b8f73 2022-04-24 op
422 de2a69bb 2021-05-17 op if (path == NULL) {
423 d1353324 2021-07-13 op stop_tab(tab);
424 de2a69bb 2021-05-17 op return;
425 de2a69bb 2021-05-17 op }
426 de2a69bb 2021-05-17 op
427 ff48dbfd 2022-05-05 op if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1) {
428 ff48dbfd 2022-05-05 op message("Can't open file %s: %s", path, strerror(errno));
429 ff48dbfd 2022-05-05 op stop_tab(tab);
430 ff48dbfd 2022-05-05 op return;
431 ff48dbfd 2022-05-05 op }
432 868b3a8f 2022-04-13 op
433 ff48dbfd 2022-05-05 op ui_show_downloads_pane();
434 7b7a69f2 2024-06-13 thomas.ad d = enqueue_download(tab->id, path, tab->meta);
435 ff48dbfd 2022-05-05 op d->fd = fd;
436 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
437 89e46508 2022-05-05 op
438 89e46508 2022-05-05 op /*
439 89e46508 2022-05-05 op * Change this tab id, the old one is associated with the
440 89e46508 2022-05-05 op * download now.
441 89e46508 2022-05-05 op */
442 89e46508 2022-05-05 op tab->id = tab_new_id();
443 de2a69bb 2021-05-17 op }
444 de2a69bb 2021-05-17 op
445 de2a69bb 2021-05-17 op static void
446 98d3e6c1 2024-02-18 op handle_dispatch_imsg(int fd, int event, void *data)
447 5e11c00c 2021-03-02 op {
448 b148be71 2024-01-31 op struct imsgev *iev = data;
449 b148be71 2024-01-31 op struct imsgbuf *imsgbuf = &iev->ibuf;
450 b148be71 2024-01-31 op struct imsg imsg;
451 54764e41 2024-02-02 op struct ibuf ibuf;
452 b148be71 2024-01-31 op struct tab *tab;
453 b148be71 2024-01-31 op struct download *d;
454 54764e41 2024-02-02 op const char *h;
455 54764e41 2024-02-02 op char *str, *page;
456 b148be71 2024-01-31 op ssize_t n;
457 54764e41 2024-02-02 op int code;
458 5e11c00c 2021-03-02 op
459 b148be71 2024-01-31 op if (event & EV_READ) {
460 c8b8dc5c 2025-03-28 op if (imsgbuf_read(imsgbuf) == -1)
461 b148be71 2024-01-31 op err(1, "imsg_read");
462 de2a69bb 2021-05-17 op }
463 b148be71 2024-01-31 op if (event & EV_WRITE) {
464 c8b8dc5c 2025-03-28 op if (imsgbuf_write(imsgbuf) == -1)
465 b148be71 2024-01-31 op err(1, "msgbuf_write");
466 b148be71 2024-01-31 op }
467 5e11c00c 2021-03-02 op
468 b148be71 2024-01-31 op for (;;) {
469 b148be71 2024-01-31 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
470 b148be71 2024-01-31 op err(1, "imsg_get");
471 b148be71 2024-01-31 op if (n == 0)
472 b148be71 2024-01-31 op break;
473 a5c3e03d 2021-03-02 op
474 b148be71 2024-01-31 op switch (imsg_get_type(&imsg)) {
475 b148be71 2024-01-31 op case IMSG_ERR:
476 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
477 b148be71 2024-01-31 op break;
478 62cbcdae 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
479 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
480 b148be71 2024-01-31 op die();
481 3d89457c 2024-06-18 thomas.ad xasprintf(&page, "# Error loading %s\n\n> %s\n",
482 3d89457c 2024-06-18 thomas.ad hist_cur(tab->hist), str);
483 b148be71 2024-01-31 op load_page_from_str(tab, page);
484 b148be71 2024-01-31 op free(page);
485 b148be71 2024-01-31 op break;
486 b148be71 2024-01-31 op case IMSG_CHECK_CERT:
487 b148be71 2024-01-31 op handle_imsg_check_cert(&imsg);
488 b148be71 2024-01-31 op break;
489 54764e41 2024-02-02 op case IMSG_REPLY:
490 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
491 b148be71 2024-01-31 op break;
492 54764e41 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
493 54764e41 2024-02-02 op ibuf_get(&ibuf, &code, sizeof(code)) == -1 ||
494 54764e41 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
495 b148be71 2024-01-31 op die();
496 b148be71 2024-01-31 op if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
497 b148be71 2024-01-31 op sizeof(tab->meta))
498 b148be71 2024-01-31 op die();
499 54764e41 2024-02-02 op tab->code = normalize_code(code);
500 b148be71 2024-01-31 op handle_request_response(tab);
501 b148be71 2024-01-31 op break;
502 b148be71 2024-01-31 op case IMSG_BUF:
503 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
504 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
505 b148be71 2024-01-31 op return;
506 de2a69bb 2021-05-17 op
507 b148be71 2024-01-31 op if (tab) {
508 c1d27b0e 2024-06-14 op if (!parser_parse(&tab->buffer, imsg.data,
509 b148be71 2024-01-31 op imsg_get_len(&imsg)))
510 b148be71 2024-01-31 op die();
511 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
512 b148be71 2024-01-31 op } else {
513 b148be71 2024-01-31 op size_t datalen = imsg_get_len(&imsg);
514 b10ba331 2024-01-23 op
515 b148be71 2024-01-31 op d->bytes += datalen;
516 b148be71 2024-01-31 op write(d->fd, imsg.data, datalen);
517 b148be71 2024-01-31 op ui_on_download_refresh();
518 b148be71 2024-01-31 op }
519 b148be71 2024-01-31 op break;
520 a3e4d56b 2024-05-25 thomas.ad case IMSG_FAULTY_GEMSERVER:
521 a3e4d56b 2024-05-25 thomas.ad if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
522 a3e4d56b 2024-05-25 thomas.ad ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
523 a3e4d56b 2024-05-25 thomas.ad return;
524 a3e4d56b 2024-05-25 thomas.ad
525 a3e4d56b 2024-05-25 thomas.ad if (tab) {
526 a3e4d56b 2024-05-25 thomas.ad tab->faulty_gemserver = 1;
527 a3e4d56b 2024-05-25 thomas.ad ui_on_tab_refresh(tab);
528 b1ccf425 2024-05-25 op } else {
529 b1ccf425 2024-05-25 op message("Download interrupted!");
530 a3e4d56b 2024-05-25 thomas.ad }
531 a3e4d56b 2024-05-25 thomas.ad break;
532 b148be71 2024-01-31 op case IMSG_EOF:
533 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
534 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
535 4e32d3a6 2024-06-04 thomas.ad return;
536 4e32d3a6 2024-06-04 thomas.ad
537 4e32d3a6 2024-06-04 thomas.ad if (tab == NULL && d != NULL) {
538 4e32d3a6 2024-06-04 thomas.ad download_finished(d);
539 b148be71 2024-01-31 op return;
540 4e32d3a6 2024-06-04 thomas.ad }
541 b10ba331 2024-01-23 op
542 b148be71 2024-01-31 op if (tab != NULL) {
543 b148be71 2024-01-31 op if (!parser_free(tab))
544 b148be71 2024-01-31 op die();
545 b148be71 2024-01-31 op h = hist_cur(tab->hist);
546 b148be71 2024-01-31 op if (!strncmp(h, "gemini://", 9) ||
547 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9) ||
548 b148be71 2024-01-31 op !strncmp(h, "finger://", 9))
549 b148be71 2024-01-31 op mcache_tab(tab);
550 ea080950 2021-07-20 op
551 b148be71 2024-01-31 op /*
552 b148be71 2024-01-31 op * Gemini is handled as soon as a 2x
553 b148be71 2024-01-31 op * reply is got.
554 b148be71 2024-01-31 op */
555 b148be71 2024-01-31 op if (!strncmp(h, "finger://", 9) ||
556 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9))
557 b148be71 2024-01-31 op history_add(h);
558 b148be71 2024-01-31 op
559 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
560 b148be71 2024-01-31 op ui_on_tab_loaded(tab);
561 b148be71 2024-01-31 op }
562 b148be71 2024-01-31 op break;
563 b148be71 2024-01-31 op default:
564 b148be71 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
565 b148be71 2024-01-31 op }
566 b148be71 2024-01-31 op
567 b148be71 2024-01-31 op imsg_free(&imsg);
568 b148be71 2024-01-31 op }
569 b148be71 2024-01-31 op
570 b148be71 2024-01-31 op imsg_event_add(iev);
571 0972d8b2 2021-03-02 op }
572 0972d8b2 2021-03-02 op
573 cd1d043d 2024-01-13 op static void
574 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
575 0972d8b2 2021-03-02 op {
576 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
577 f63b8f73 2022-04-24 op fs_load_url(tab, url);
578 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
579 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
580 4d3785b1 2021-03-09 op }
581 0972d8b2 2021-03-02 op
582 cd1d043d 2024-01-13 op static void
583 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
584 0b1053d2 2021-08-13 op {
585 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
586 f63b8f73 2022-04-24 op fs_load_url(tab, url);
587 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
588 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
589 0b1053d2 2021-08-13 op }
590 0b1053d2 2021-08-13 op
591 cd1d043d 2024-01-13 op static void
592 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
593 5fb52fc0 2021-07-25 op {
594 5fb52fc0 2021-07-25 op struct get_req req;
595 25905047 2024-01-16 op const char *path;
596 5fb52fc0 2021-07-25 op
597 5fb52fc0 2021-07-25 op memset(&req, 0, sizeof(req));
598 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
599 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
600 5fb52fc0 2021-07-25 op
601 5fb52fc0 2021-07-25 op /*
602 5fb52fc0 2021-07-25 op * Sometimes the finger url have the user as path component
603 5fb52fc0 2021-07-25 op * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
604 5fb52fc0 2021-07-25 op * userinfo (e.g. finger://cobradile@finger.farm).
605 5fb52fc0 2021-07-25 op */
606 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_UINFO) {
607 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_uinfo, sizeof(req.req));
608 5fb52fc0 2021-07-25 op } else {
609 25905047 2024-01-16 op path = tab->iri.iri_path;
610 25905047 2024-01-16 op while (*path == '/')
611 25905047 2024-01-16 op ++path;
612 25905047 2024-01-16 op strlcpy(req.req, path, sizeof(req.req));
613 5fb52fc0 2021-07-25 op }
614 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
615 5fb52fc0 2021-07-25 op
616 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, &textplain_parser);
617 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_FINGER, NULL);
618 5fb52fc0 2021-07-25 op }
619 5fb52fc0 2021-07-25 op
620 cd1d043d 2024-01-13 op static void
621 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
622 4d3785b1 2021-03-09 op {
623 984245ce 2021-06-23 op struct get_req req;
624 d35e18b3 2024-02-04 op
625 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
626 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
627 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
628 984245ce 2021-06-23 op
629 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
630 ca190c99 2021-08-14 op }
631 ca190c99 2021-08-14 op
632 ca190c99 2021-08-14 op static inline const char *
633 ca190c99 2021-08-14 op gopher_skip_selector(const char *path, int *ret_type)
634 ca190c99 2021-08-14 op {
635 ca190c99 2021-08-14 op *ret_type = 0;
636 ca190c99 2021-08-14 op
637 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
638 ca190c99 2021-08-14 op *ret_type = '1';
639 ca190c99 2021-08-14 op return path;
640 ca190c99 2021-08-14 op }
641 ca190c99 2021-08-14 op
642 ca190c99 2021-08-14 op if (*path != '/')
643 ca190c99 2021-08-14 op return path;
644 ca190c99 2021-08-14 op path++;
645 ca190c99 2021-08-14 op
646 ca190c99 2021-08-14 op switch (*ret_type = *path) {
647 ca190c99 2021-08-14 op case '0':
648 ca190c99 2021-08-14 op case '1':
649 ca190c99 2021-08-14 op case '7':
650 ca190c99 2021-08-14 op break;
651 ca190c99 2021-08-14 op
652 ca190c99 2021-08-14 op default:
653 ca190c99 2021-08-14 op *ret_type = 0;
654 ca190c99 2021-08-14 op path -= 1;
655 ca190c99 2021-08-14 op return path;
656 ca190c99 2021-08-14 op }
657 ca190c99 2021-08-14 op
658 95bb01e2 2021-08-28 op return ++path;
659 1663bf1e 2021-07-25 op }
660 1663bf1e 2021-07-25 op
661 cd1d043d 2024-01-13 op static void
662 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
663 1663bf1e 2021-07-25 op {
664 81839fee 2021-07-25 op struct get_req req;
665 ca190c99 2021-08-14 op int type;
666 81839fee 2021-07-25 op const char *path;
667 1663bf1e 2021-07-25 op
668 1663bf1e 2021-07-25 op memset(&req, 0, sizeof(req));
669 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
670 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
671 1663bf1e 2021-07-25 op
672 25905047 2024-01-16 op path = gopher_skip_selector(tab->iri.iri_path, &type);
673 ca190c99 2021-08-14 op switch (type) {
674 ca190c99 2021-08-14 op case '0':
675 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, &textplain_parser);
676 ca190c99 2021-08-14 op break;
677 ca190c99 2021-08-14 op case '1':
678 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, &gophermap_parser);
679 ca190c99 2021-08-14 op break;
680 ca190c99 2021-08-14 op case '7':
681 acf9defe 2022-02-08 op free(tab->last_input_url);
682 3d89457c 2024-06-18 thomas.ad tab->last_input_url = xstrdup(url);
683 693188cb 2022-02-07 op ui_require_input(tab, 0, ir_select_gopher);
684 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[10]);
685 cd1d043d 2024-01-13 op return;
686 ca190c99 2021-08-14 op default:
687 cd1d043d 2024-01-13 op load_page_from_str(tab, "Unknown gopher selector");
688 cd1d043d 2024-01-13 op return;
689 81839fee 2021-07-25 op }
690 81839fee 2021-07-25 op
691 62c0f697 2024-02-21 op if (iri_urlunescape(path, req.req, sizeof(req.req)) == -1)
692 62c0f697 2024-02-21 op strlcpy(req.req, path, sizeof(req.req));
693 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
694 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
695 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
696 9a28e7e5 2021-08-03 op }
697 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
698 9a28e7e5 2021-08-03 op
699 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
700 0972d8b2 2021-03-02 op }
701 0972d8b2 2021-03-02 op
702 cd1d043d 2024-01-13 op static void
703 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
704 984245ce 2021-06-23 op {
705 984245ce 2021-06-23 op struct get_req req;
706 984245ce 2021-06-23 op
707 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
708 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
709 f94c7ac1 2021-11-24 op strlcpy(req.port, p->port, sizeof(req.port));
710 984245ce 2021-06-23 op
711 cf1195ce 2021-07-25 op tab->proxy = p;
712 984245ce 2021-06-23 op
713 71bc1636 2024-02-06 op make_request(tab, &req, p->proto, hist_cur(tab->hist));
714 cf1195ce 2021-07-25 op }
715 984245ce 2021-06-23 op
716 cd1d043d 2024-01-13 op static void
717 71bc1636 2024-02-06 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
718 cf1195ce 2021-07-25 op {
719 71bc1636 2024-02-06 op int use_cert = 0, fd = -1;
720 71bc1636 2024-02-06 op
721 71bc1636 2024-02-06 op if (proto == PROTO_GEMINI) {
722 71bc1636 2024-02-06 op tab->client_cert = cert_for(&tab->iri, &tab->client_cert_temp);
723 71bc1636 2024-02-06 op use_cert = (tab->client_cert != NULL);
724 71bc1636 2024-02-06 op }
725 d35e18b3 2024-02-04 op
726 cf1195ce 2021-07-25 op stop_tab(tab);
727 cf1195ce 2021-07-25 op tab->id = tab_new_id();
728 a3e4d56b 2024-05-25 thomas.ad tab->faulty_gemserver = 0;
729 cf1195ce 2021-07-25 op req->proto = proto;
730 cf1195ce 2021-07-25 op
731 cf1195ce 2021-07-25 op if (r != NULL) {
732 cf1195ce 2021-07-25 op strlcpy(req->req, r, sizeof(req->req));
733 cf1195ce 2021-07-25 op strlcat(req->req, "\r\n", sizeof(req->req));
734 cf1195ce 2021-07-25 op }
735 cf1195ce 2021-07-25 op
736 5df02e0d 2022-01-11 op start_loading_anim(tab);
737 d35e18b3 2024-02-04 op
738 d35e18b3 2024-02-04 op if (!use_cert)
739 d35e18b3 2024-02-04 op tab->client_cert = NULL;
740 d35e18b3 2024-02-04 op if (use_cert && (fd = cert_open(tab->client_cert)) == -1) {
741 d35e18b3 2024-02-04 op tab->client_cert = NULL;
742 d35e18b3 2024-02-04 op message("failed to open certificate: %s", strerror(errno));
743 d35e18b3 2024-02-04 op }
744 d35e18b3 2024-02-04 op
745 d35e18b3 2024-02-04 op ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
746 984245ce 2021-06-23 op }
747 984245ce 2021-06-23 op
748 9a28e7e5 2021-08-03 op void
749 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
750 9a28e7e5 2021-08-03 op {
751 9a28e7e5 2021-08-03 op struct get_req req;
752 1b0e6efc 2021-08-08 op
753 9a28e7e5 2021-08-03 op memset(&req, 0, sizeof(req));
754 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
755 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
756 9a28e7e5 2021-08-03 op
757 9a28e7e5 2021-08-03 op /* +2 to skip /7 */
758 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_path+2, sizeof(req.req));
759 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
760 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
761 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
762 9a28e7e5 2021-08-03 op }
763 9a28e7e5 2021-08-03 op
764 9a28e7e5 2021-08-03 op strlcat(req.req, "\t", sizeof(req.req));
765 9a28e7e5 2021-08-03 op strlcat(req.req, text, sizeof(req.req));
766 9a28e7e5 2021-08-03 op strlcat(req.req, "\r\n", sizeof(req.req));
767 9a28e7e5 2021-08-03 op
768 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
769 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, &gophermap_parser);
770 9a28e7e5 2021-08-03 op
771 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
772 9a28e7e5 2021-08-03 op }
773 9a28e7e5 2021-08-03 op
774 cd1d043d 2024-01-13 op void
775 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
776 1bcb8303 2022-01-19 op {
777 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, &gemtext_parser);
778 c1d27b0e 2024-06-14 op if (!parser_parse(&tab->buffer, page, strlen(page)))
779 1bcb8303 2022-01-19 op abort();
780 5d7d3b89 2024-06-03 op if (!parser_free(tab))
781 1bcb8303 2022-01-19 op abort();
782 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
783 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
784 1bcb8303 2022-01-19 op }
785 1bcb8303 2022-01-19 op
786 4575b59c 2024-06-26 op static void
787 4575b59c 2024-06-26 op do_load_url_cmd(int res, void *d)
788 4575b59c 2024-06-26 op {
789 4575b59c 2024-06-26 op struct tab *tab = d;
790 4575b59c 2024-06-26 op const char *argv[3];
791 4575b59c 2024-06-26 op
792 4575b59c 2024-06-26 op if (!res)
793 4575b59c 2024-06-26 op return;
794 4575b59c 2024-06-26 op
795 4575b59c 2024-06-26 op argv[0] = DEFAULT_OPENER;
796 4575b59c 2024-06-26 op argv[1] = hist_cur(tab->hist);
797 4575b59c 2024-06-26 op argv[2] = NULL;
798 4575b59c 2024-06-26 op exec_cmd((char **)argv, EXEC_BACKGROUND);
799 4575b59c 2024-06-26 op message("Link opened with %s", DEFAULT_OPENER);
800 4575b59c 2024-06-26 op }
801 4575b59c 2024-06-26 op
802 adea4eec 2021-07-16 op /*
803 cd1d043d 2024-01-13 op * Effectively load the given url in the given tab.
804 adea4eec 2021-07-16 op */
805 cd1d043d 2024-01-13 op static void
806 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
807 4d3785b1 2021-03-09 op {
808 0d877486 2022-02-24 op const struct proto *p;
809 4660114d 2024-06-26 op struct proxy *proxy;
810 4660114d 2024-06-26 op int nocache = mode & LU_MODE_NOCACHE;
811 4660114d 2024-06-26 op char *t;
812 4660114d 2024-06-26 op char buf[1025];
813 7943bb81 2021-07-10 op
814 7943bb81 2021-07-10 op tab->proxy = NULL;
815 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
816 10346511 2021-03-17 op
817 25905047 2024-01-16 op if (iri_parse(base, url, &tab->iri) == -1) {
818 3d89457c 2024-06-18 thomas.ad xasprintf(&t, "# error loading %s\n>%s\n", url,
819 4660114d 2024-06-26 op "Can't parse the IRI");
820 65c49665 2024-01-23 op hist_set_cur(tab->hist, url);
821 31f1a758 2021-04-22 op load_page_from_str(tab, t);
822 31f1a758 2021-04-22 op free(t);
823 cd1d043d 2024-01-13 op return;
824 31f1a758 2021-04-22 op }
825 31f1a758 2021-04-22 op
826 65c49665 2024-01-23 op iri_unparse(&tab->iri, buf, sizeof(buf));
827 65c49665 2024-01-23 op hist_set_cur(tab->hist, buf);
828 31f1a758 2021-04-22 op
829 65c49665 2024-01-23 op if (!nocache && mcache_lookup(buf, tab)) {
830 8f3c9af8 2022-01-11 op ui_on_tab_refresh(tab);
831 8f3c9af8 2022-01-11 op ui_on_tab_loaded(tab);
832 cd1d043d 2024-01-13 op return;
833 8f3c9af8 2022-01-11 op }
834 d5af38cc 2022-01-10 op
835 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
836 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, p->schema)) {
837 3b726df7 2024-01-16 op /* patch the port */
838 25905047 2024-01-16 op if (*tab->iri.iri_portstr == '\0' &&
839 25905047 2024-01-16 op p->port != NULL)
840 3b726df7 2024-01-16 op iri_setport(&tab->iri, p->port);
841 ba782cb8 2021-07-25 op
842 65c49665 2024-01-23 op p->loadfn(tab, buf);
843 cd1d043d 2024-01-13 op return;
844 4d3785b1 2021-03-09 op }
845 4d3785b1 2021-03-09 op }
846 4d3785b1 2021-03-09 op
847 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
848 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, proxy->match_proto)) {
849 cd1d043d 2024-01-13 op load_via_proxy(tab, url, proxy);
850 cd1d043d 2024-01-13 op return;
851 cd1d043d 2024-01-13 op }
852 984245ce 2021-06-23 op }
853 984245ce 2021-06-23 op
854 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
855 4575b59c 2024-06-26 op ui_yornp("Open page with "DEFAULT_OPENER"?",
856 4575b59c 2024-06-26 op do_load_url_cmd, tab);
857 4d3785b1 2021-03-09 op }
858 4d3785b1 2021-03-09 op
859 2ddbda6b 2021-07-17 op /*
860 ea639250 2022-01-02 op * Load url in tab and handle history. If a tab is marked as lazy, only
861 ea639250 2022-01-02 op * prepare the url but don't load it.
862 2ddbda6b 2021-07-17 op */
863 9ad4627d 2021-03-10 op void
864 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
865 2051e653 2021-03-13 op {
866 65c49665 2024-01-23 op size_t line_off, curr_off;
867 ea639250 2022-01-02 op int lazy = tab->flags & TAB_LAZY;
868 65c49665 2024-01-23 op int dohist = !(mode & LU_MODE_NOHIST);
869 bc55cdb9 2021-08-12 op
870 fb8dcd1c 2022-01-18 op if (operating && lazy) {
871 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
872 fb8dcd1c 2022-01-18 op lazy = 0;
873 65c49665 2024-01-23 op } else if (hist_size(tab->hist) != 0) {
874 65c49665 2024-01-23 op get_scroll_position(tab, &line_off, &curr_off);
875 65c49665 2024-01-23 op hist_set_offs(tab->hist, line_off, curr_off);
876 65c49665 2024-01-23 op }
877 fb8dcd1c 2022-01-18 op
878 65c49665 2024-01-23 op if (dohist) {
879 65c49665 2024-01-23 op if (hist_push(tab->hist, url) == -1) {
880 98d3e6c1 2024-02-18 op ev_break();
881 2ddbda6b 2021-07-17 op return;
882 2ddbda6b 2021-07-17 op }
883 2ddbda6b 2021-07-17 op
884 c1d27b0e 2024-06-14 op strlcpy(tab->buffer.title, url, sizeof(tab->buffer.title));
885 2ddbda6b 2021-07-17 op }
886 2ddbda6b 2021-07-17 op
887 9eca3d7c 2021-11-02 op if (!lazy)
888 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
889 2051e653 2021-03-13 op }
890 2051e653 2021-03-13 op
891 a37d1a1c 2021-08-14 op void
892 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
893 a37d1a1c 2021-08-14 op {
894 0259f38d 2022-01-11 op if (operating) {
895 0259f38d 2022-01-11 op autosave_hook();
896 0259f38d 2022-01-11 op message("Loading %s...", url);
897 a37d1a1c 2021-08-14 op }
898 a37d1a1c 2021-08-14 op
899 0f7d8271 2024-01-22 op if (base == NULL)
900 65c49665 2024-01-23 op base = hist_cur(tab->hist);
901 0f7d8271 2024-01-22 op
902 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
903 a37d1a1c 2021-08-14 op }
904 a37d1a1c 2021-08-14 op
905 2051e653 2021-03-13 op int
906 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
907 2051e653 2021-03-13 op {
908 65c49665 2024-01-23 op const char *h;
909 2051e653 2021-03-13 op
910 65c49665 2024-01-23 op if ((h = hist_prev(tab->hist)) == NULL)
911 2051e653 2021-03-13 op return 0;
912 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
913 2051e653 2021-03-13 op return 1;
914 2051e653 2021-03-13 op }
915 2051e653 2021-03-13 op
916 2051e653 2021-03-13 op int
917 2051e653 2021-03-13 op load_next_page(struct tab *tab)
918 2051e653 2021-03-13 op {
919 65c49665 2024-01-23 op const char *h;
920 2051e653 2021-03-13 op
921 65c49665 2024-01-23 op if ((h = hist_next(tab->hist)) == NULL)
922 2051e653 2021-03-13 op return 0;
923 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
924 2051e653 2021-03-13 op return 1;
925 a37d1a1c 2021-08-14 op }
926 a37d1a1c 2021-08-14 op
927 2051e653 2021-03-13 op void
928 868b3a8f 2022-04-13 op write_buffer(const char *path, struct tab *tab)
929 868b3a8f 2022-04-13 op {
930 f63b8f73 2022-04-24 op FILE *fp;
931 f63b8f73 2022-04-24 op
932 868b3a8f 2022-04-13 op if (path == NULL)
933 868b3a8f 2022-04-13 op return;
934 868b3a8f 2022-04-13 op
935 f63b8f73 2022-04-24 op if ((fp = fopen(path, "w")) == NULL)
936 f63b8f73 2022-04-24 op return;
937 c1d27b0e 2024-06-14 op if (!parser_serialize(&tab->buffer, fp))
938 f63b8f73 2022-04-24 op message("Failed to save the page.");
939 f63b8f73 2022-04-24 op fclose(fp);
940 868b3a8f 2022-04-13 op }
941 868b3a8f 2022-04-13 op
942 c6efff96 2021-08-16 op /*
943 449ea6fe 2024-01-16 op * Given a user-entered URL, apply some heuristics to use it if
944 449ea6fe 2024-01-16 op * load-url-use-heuristic allows it.
945 c6efff96 2021-08-16 op *
946 f6d7d047 2021-12-30 op * - if it's a proper url use it
947 f6d7d047 2021-12-30 op * - if it starts with a `./' or a `/' assume its a file:// url
948 cfcbc23c 2024-01-16 op * - assume it's a default-protocol:// url
949 c6efff96 2021-08-16 op *
950 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
951 f6d7d047 2021-12-30 op * url.
952 c6efff96 2021-08-16 op */
953 c6efff96 2021-08-16 op void
954 449ea6fe 2024-01-16 op humanify_url(const char *raw, const char *base, char *ret, size_t len)
955 c6efff96 2021-08-16 op {
956 44008d59 2024-01-16 op static struct iri iri;
957 c7107cec 2021-04-01 op
958 449ea6fe 2024-01-16 op if (load_url_use_heuristic)
959 449ea6fe 2024-01-16 op base = NULL;
960 449ea6fe 2024-01-16 op
961 449ea6fe 2024-01-16 op if (iri_parse(base, raw, &iri) == 0) {
962 44008d59 2024-01-16 op iri_unparse(&iri, ret, len);
963 c6efff96 2021-08-16 op return;
964 c6efff96 2021-08-16 op }
965 c6efff96 2021-08-16 op
966 d89eb764 2022-04-24 op if (!strncmp(raw, "./", 2)) {
967 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
968 449ea6fe 2024-01-16 op strlcat(ret, cwd, len);
969 c6efff96 2021-08-16 op strlcat(ret, "/", len);
970 c6efff96 2021-08-16 op strlcat(ret, raw+2, len);
971 c6efff96 2021-08-16 op return;
972 c6efff96 2021-08-16 op }
973 c6efff96 2021-08-16 op
974 c6efff96 2021-08-16 op if (*raw == '/') {
975 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
976 c6efff96 2021-08-16 op strlcat(ret, raw, len);
977 c6efff96 2021-08-16 op return;
978 c6efff96 2021-08-16 op }
979 c6efff96 2021-08-16 op
980 cfcbc23c 2024-01-16 op strlcpy(ret, default_protocol, len);
981 cfcbc23c 2024-01-16 op strlcat(ret, "://", len);
982 c6efff96 2021-08-16 op strlcat(ret, raw, len);
983 486bde7d 2022-04-24 op }
984 486bde7d 2022-04-24 op
985 486bde7d 2022-04-24 op int
986 486bde7d 2022-04-24 op bookmark_page(const char *url)
987 486bde7d 2022-04-24 op {
988 486bde7d 2022-04-24 op FILE *fp;
989 486bde7d 2022-04-24 op
990 486bde7d 2022-04-24 op if ((fp = fopen(bookmark_file, "a")) == NULL)
991 486bde7d 2022-04-24 op return -1;
992 486bde7d 2022-04-24 op fprintf(fp, "=> %s\n", url);
993 486bde7d 2022-04-24 op fclose(fp);
994 486bde7d 2022-04-24 op return 0;
995 c6efff96 2021-08-16 op }
996 c6efff96 2021-08-16 op
997 6cc5fcfe 2021-07-08 op static pid_t
998 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
999 6cc5fcfe 2021-07-08 op {
1000 55ccd6d0 2021-09-15 op const char *argv[5];
1001 6cc5fcfe 2021-07-08 op int argc = 0;
1002 6cc5fcfe 2021-07-08 op pid_t pid;
1003 6cc5fcfe 2021-07-08 op
1004 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
1005 6cc5fcfe 2021-07-08 op case -1:
1006 6cc5fcfe 2021-07-08 op die();
1007 6cc5fcfe 2021-07-08 op case 0:
1008 6cc5fcfe 2021-07-08 op break;
1009 6cc5fcfe 2021-07-08 op default:
1010 6cc5fcfe 2021-07-08 op close(fd);
1011 6cc5fcfe 2021-07-08 op return pid;
1012 6cc5fcfe 2021-07-08 op }
1013 6cc5fcfe 2021-07-08 op
1014 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
1015 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
1016 6cc5fcfe 2021-07-08 op
1017 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
1018 6cc5fcfe 2021-07-08 op switch (p) {
1019 6cc5fcfe 2021-07-08 op case PROC_UI:
1020 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
1021 6cc5fcfe 2021-07-08 op case PROC_NET:
1022 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
1023 6cc5fcfe 2021-07-08 op break;
1024 6cc5fcfe 2021-07-08 op }
1025 6cc5fcfe 2021-07-08 op
1026 55ccd6d0 2021-09-15 op if (safe_mode)
1027 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
1028 55ccd6d0 2021-09-15 op
1029 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
1030 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
1031 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
1032 4cf6ba13 2022-02-11 op }
1033 4cf6ba13 2022-02-11 op
1034 4cf6ba13 2022-02-11 op static void
1035 4cf6ba13 2022-02-11 op send_url(const char *url)
1036 4cf6ba13 2022-02-11 op {
1037 4cf6ba13 2022-02-11 op struct sockaddr_un sun;
1038 4cf6ba13 2022-02-11 op struct imsgbuf ibuf;
1039 4cf6ba13 2022-02-11 op int ctl_sock;
1040 4cf6ba13 2022-02-11 op
1041 4cf6ba13 2022-02-11 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1042 4cf6ba13 2022-02-11 op err(1, "socket");
1043 4cf6ba13 2022-02-11 op
1044 4cf6ba13 2022-02-11 op memset(&sun, 0, sizeof(sun));
1045 4cf6ba13 2022-02-11 op sun.sun_family = AF_UNIX;
1046 4cf6ba13 2022-02-11 op strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1047 4cf6ba13 2022-02-11 op
1048 4cf6ba13 2022-02-11 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1049 4cf6ba13 2022-02-11 op err(1, "connect: %s", ctlsock_path);
1050 4cf6ba13 2022-02-11 op
1051 c8b8dc5c 2025-03-28 op if (imsgbuf_init(&ibuf, ctl_sock) == -1)
1052 c8b8dc5c 2025-03-28 op err(1, "imsgbuf_init");
1053 4cf6ba13 2022-02-11 op imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1054 4cf6ba13 2022-02-11 op strlen(url) + 1);
1055 c8b8dc5c 2025-03-28 op imsgbuf_flush(&ibuf);
1056 4cf6ba13 2022-02-11 op close(ctl_sock);
1057 6cc5fcfe 2021-07-08 op }
1058 6cc5fcfe 2021-07-08 op
1059 1fce2e75 2021-08-14 op int
1060 d35e18b3 2024-02-04 op ui_send_net(int type, uint32_t peerid, int fd, const void *data,
1061 bc10f6a5 2021-07-12 op uint16_t datalen)
1062 bc10f6a5 2021-07-12 op {
1063 d35e18b3 2024-02-04 op return imsg_compose_event(iev_net, type, peerid, 0, fd, data,
1064 bc10f6a5 2021-07-12 op datalen);
1065 bc10f6a5 2021-07-12 op }
1066 bc10f6a5 2021-07-12 op
1067 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1068 6cc5fcfe 2021-07-08 op usage(int r)
1069 d2544989 2021-07-08 op {
1070 335cfaec 2021-09-15 op fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1071 dc761924 2021-07-15 op getprogname());
1072 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1073 6cc5fcfe 2021-07-08 op exit(r);
1074 740f578b 2021-03-15 op }
1075 740f578b 2021-03-15 op
1076 5e11c00c 2021-03-02 op int
1077 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1078 5e11c00c 2021-03-02 op {
1079 f63b8f73 2022-04-24 op struct imsgev net_ibuf;
1080 be97d6e6 2021-08-15 op pid_t pid;
1081 4cf6ba13 2022-02-11 op int control_fd;
1082 f63b8f73 2022-04-24 op int pipe2net[2];
1083 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
1084 6cc5fcfe 2021-07-08 op int proc = -1;
1085 5d7c642a 2021-09-15 op int sessionfd = -1;
1086 be97d6e6 2021-08-15 op int status;
1087 6cc5fcfe 2021-07-08 op const char *argv0;
1088 5e11c00c 2021-03-02 op
1089 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1090 6cc5fcfe 2021-07-08 op
1091 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1092 5e11c00c 2021-03-02 op
1093 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1094 d2544989 2021-07-08 op enable_colors = 0;
1095 449ea6fe 2024-01-16 op
1096 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1097 d2544989 2021-07-08 op switch (ch) {
1098 d2544989 2021-07-08 op case 'c':
1099 d2544989 2021-07-08 op fail = 1;
1100 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1101 d2544989 2021-07-08 op break;
1102 d2544989 2021-07-08 op case 'n':
1103 d2544989 2021-07-08 op configtest = 1;
1104 d2544989 2021-07-08 op break;
1105 d2544989 2021-07-08 op case 'h':
1106 6cc5fcfe 2021-07-08 op usage(0);
1107 d0971653 2021-09-15 op case 'S':
1108 d0971653 2021-09-15 op safe_mode = 1;
1109 d0971653 2021-09-15 op break;
1110 6cc5fcfe 2021-07-08 op case 'T':
1111 6cc5fcfe 2021-07-08 op switch (*optarg) {
1112 6cc5fcfe 2021-07-08 op case 'n':
1113 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1114 6cc5fcfe 2021-07-08 op break;
1115 6cc5fcfe 2021-07-08 op default:
1116 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1117 6cc5fcfe 2021-07-08 op *optarg);
1118 6cc5fcfe 2021-07-08 op }
1119 6cc5fcfe 2021-07-08 op break;
1120 dc761924 2021-07-15 op case 'v':
1121 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1122 dc761924 2021-07-15 op exit(0);
1123 dc761924 2021-07-15 op break;
1124 d2544989 2021-07-08 op default:
1125 6cc5fcfe 2021-07-08 op usage(1);
1126 d2544989 2021-07-08 op }
1127 d2544989 2021-07-08 op }
1128 d2544989 2021-07-08 op
1129 d2544989 2021-07-08 op argc -= optind;
1130 d2544989 2021-07-08 op argv += optind;
1131 d2544989 2021-07-08 op
1132 6cc5fcfe 2021-07-08 op if (proc != -1) {
1133 6cc5fcfe 2021-07-08 op if (argc > 0)
1134 6cc5fcfe 2021-07-08 op usage(1);
1135 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1136 f45bd2e3 2021-07-24 op return net_main();
1137 6cc5fcfe 2021-07-08 op else
1138 6cc5fcfe 2021-07-08 op usage(1);
1139 6cc5fcfe 2021-07-08 op }
1140 f63b8f73 2022-04-24 op
1141 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1142 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1143 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1144 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1145 d5bdf203 2021-07-08 op
1146 7b7a69f2 2024-06-13 thomas.ad init_mailcap();
1147 7b7a69f2 2024-06-13 thomas.ad
1148 b8ec3578 2024-02-14 op if (fs_init() == -1)
1149 b8ec3578 2024-02-14 op err(1, "fs_init failed");
1150 b8ec3578 2024-02-14 op if (certs_init(certs_file) == -1)
1151 b8ec3578 2024-02-14 op err(1, "certs_init failed");
1152 d2544989 2021-07-08 op config_init();
1153 eb2ed626 2021-10-07 op parseconfig(config_path, fail);
1154 69f32f56 2021-08-16 op if (configtest) {
1155 d2544989 2021-07-08 op puts("config OK");
1156 d2544989 2021-07-08 op exit(0);
1157 d2544989 2021-07-08 op }
1158 d2544989 2021-07-08 op
1159 cfcbc23c 2024-01-16 op if (default_protocol == NULL &&
1160 cfcbc23c 2024-01-16 op (default_protocol = strdup("gemini")) == NULL)
1161 cfcbc23c 2024-01-16 op err(1, "strdup");
1162 cfcbc23c 2024-01-16 op
1163 112b9046 2021-08-29 op if (download_path == NULL &&
1164 112b9046 2021-08-29 op (download_path = strdup("/tmp/")) == NULL)
1165 112b9046 2021-08-29 op errx(1, "strdup");
1166 112b9046 2021-08-29 op
1167 449ea6fe 2024-01-16 op if (argc != 0) {
1168 449ea6fe 2024-01-16 op char *base;
1169 449ea6fe 2024-01-16 op
1170 3d89457c 2024-06-18 thomas.ad xasprintf(&base, "file://%s/", cwd);
1171 449ea6fe 2024-01-16 op
1172 449ea6fe 2024-01-16 op has_url = 1;
1173 449ea6fe 2024-01-16 op humanify_url(argv[0], base, url, sizeof(url));
1174 449ea6fe 2024-01-16 op
1175 449ea6fe 2024-01-16 op free(base);
1176 449ea6fe 2024-01-16 op }
1177 449ea6fe 2024-01-16 op
1178 4cf6ba13 2022-02-11 op if (!safe_mode && (sessionfd = lock_session()) == -1) {
1179 4cf6ba13 2022-02-11 op if (has_url) {
1180 4cf6ba13 2022-02-11 op send_url(url);
1181 4cf6ba13 2022-02-11 op exit(0);
1182 4cf6ba13 2022-02-11 op }
1183 4cf6ba13 2022-02-11 op
1184 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1185 d0fd368a 2021-07-15 op "telescope already running?");
1186 4cf6ba13 2022-02-11 op }
1187 d0fd368a 2021-07-15 op
1188 6cc5fcfe 2021-07-08 op /* Start children. */
1189 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1190 35e1f40a 2021-03-14 op err(1, "socketpair");
1191 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1192 c8b8dc5c 2025-03-28 op if (imsgbuf_init(&net_ibuf.ibuf, pipe2net[0]) == -1)
1193 c8b8dc5c 2025-03-28 op err(1, "imsgbuf_init");
1194 0082f426 2025-04-01 op imsgbuf_allow_fdpass(&net_ibuf.ibuf);
1195 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1196 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1197 5e11c00c 2021-03-02 op
1198 37d429b0 2021-04-01 op setproctitle("ui");
1199 35e1f40a 2021-03-14 op
1200 eb722b50 2022-01-03 op /* initialize tofu store */
1201 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1202 cbcc75fb 2021-03-17 op
1203 98d3e6c1 2024-02-18 op if (ev_init() == -1)
1204 98d3e6c1 2024-02-18 op err(1, "ev_init");
1205 5e11c00c 2021-03-02 op
1206 4cf6ba13 2022-02-11 op if (!safe_mode) {
1207 4cf6ba13 2022-02-11 op if ((control_fd = control_init(ctlsock_path)) == -1)
1208 4cf6ba13 2022-02-11 op err(1, "control_init %s", ctlsock_path);
1209 4cf6ba13 2022-02-11 op control_listen(control_fd);
1210 4cf6ba13 2022-02-11 op }
1211 4cf6ba13 2022-02-11 op
1212 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1213 d5af38cc 2022-01-10 op mcache_init();
1214 d5af38cc 2022-01-10 op
1215 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1216 1fce2e75 2021-08-14 op autosave_init();
1217 ecfbb1d4 2021-08-14 op
1218 f63b8f73 2022-04-24 op /* Setup event handlers for pipes to net */
1219 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1220 98d3e6c1 2024-02-18 op ev_add(iev_net->ibuf.fd, iev_net->events, iev_net->handler, iev_net);
1221 d2544989 2021-07-08 op
1222 bb28f1c2 2021-12-30 op if (ui_init()) {
1223 941b3761 2021-03-18 op sandbox_ui_process();
1224 64f4f8e2 2022-04-24 op load_session(&certs);
1225 d521a6a4 2022-05-11 op if (has_url)
1226 d521a6a4 2022-05-11 op new_tab(url, NULL, NULL);
1227 f63b8f73 2022-04-24 op operating = 1;
1228 f63b8f73 2022-04-24 op switch_to_tab(current_tab);
1229 f63b8f73 2022-04-24 op ui_main_loop();
1230 941b3761 2021-03-18 op ui_end();
1231 941b3761 2021-03-18 op }
1232 5e11c00c 2021-03-02 op
1233 d35e18b3 2024-02-04 op ui_send_net(IMSG_QUIT, 0, -1, NULL, 0);
1234 c8b8dc5c 2025-03-28 op imsgbuf_flush(&iev_net->ibuf);
1235 be97d6e6 2021-08-15 op
1236 be97d6e6 2021-08-15 op /* wait for children to terminate */
1237 be97d6e6 2021-08-15 op do {
1238 be97d6e6 2021-08-15 op pid = wait(&status);
1239 be97d6e6 2021-08-15 op if (pid == -1) {
1240 be97d6e6 2021-08-15 op if (errno != EINTR && errno != ECHILD)
1241 be97d6e6 2021-08-15 op err(1, "wait");
1242 be97d6e6 2021-08-15 op } else if (WIFSIGNALED(status))
1243 be97d6e6 2021-08-15 op warnx("child terminated; signal %d", WTERMSIG(status));
1244 be97d6e6 2021-08-15 op } while (pid != -1 || (pid == -1 && errno == EINTR));
1245 5e11c00c 2021-03-02 op
1246 f63b8f73 2022-04-24 op if (!safe_mode)
1247 f63b8f73 2022-04-24 op unlink(crashed_file);
1248 f63b8f73 2022-04-24 op
1249 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1250 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);
1251 d0fd368a 2021-07-15 op
1252 5e11c00c 2021-03-02 op return 0;
1253 5e11c00c 2021-03-02 op }