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 4cf6ba13 2022-02-11 op #include <sys/un.h>
21 be97d6e6 2021-08-15 op #include <sys/wait.h>
22 5e11c00c 2021-03-02 op
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>
32 5e11c00c 2021-03-02 op
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"
49 dc761924 2021-07-15 op
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},
55 dc761924 2021-07-15 op };
56 dc761924 2021-07-15 op
57 d08fccd2 2024-01-23 op static const char *opts = "c:hnST:v";
58 a37d1a1c 2021-08-14 op
59 bb28f1c2 2021-12-30 op static int has_url;
60 bb28f1c2 2021-12-30 op static char url[GEMINI_URL_LEN];
61 bb28f1c2 2021-12-30 op
62 a37d1a1c 2021-08-14 op /*
63 a37d1a1c 2021-08-14 op * Used to know when we're finished loading.
64 a37d1a1c 2021-08-14 op */
65 1fce2e75 2021-08-14 op int operating;
66 395b9f4e 2021-07-12 op
67 d0971653 2021-09-15 op /*
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.
70 d0971653 2021-09-15 op */
71 d0971653 2021-09-15 op int safe_mode;
72 d0971653 2021-09-15 op
73 f63b8f73 2022-04-24 op static struct imsgev *iev_net;
74 ecfbb1d4 2021-08-14 op
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);
78 6cc5fcfe 2021-07-08 op
79 6cc5fcfe 2021-07-08 op enum telescope_process {
80 6cc5fcfe 2021-07-08 op PROC_UI,
81 6cc5fcfe 2021-07-08 op PROC_NET,
82 6cc5fcfe 2021-07-08 op };
83 5e11c00c 2021-03-02 op
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
89 9a8a7402 2021-07-18 op
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",
97 9a8a7402 2021-07-18 op
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"
113 9a8a7402 2021-07-18 op };
114 9a8a7402 2021-07-18 op
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 *);
135 dfb5eb39 2021-07-25 op
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},
147 dfb5eb39 2021-07-25 op };
148 5e11c00c 2021-03-02 op
149 cbcc75fb 2021-03-17 op static struct ohash certs;
150 cbcc75fb 2021-03-17 op
151 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
152 5e11c00c 2021-03-02 op die(void)
153 5e11c00c 2021-03-02 op {
154 5e11c00c 2021-03-02 op abort(); /* TODO */
155 5e11c00c 2021-03-02 op }
156 5e11c00c 2021-03-02 op
157 5e11c00c 2021-03-02 op static struct tab *
158 f6243012 2021-07-19 op tab_by_id(uint32_t id)
159 5e11c00c 2021-03-02 op {
160 5e11c00c 2021-03-02 op struct tab *t;
161 5e11c00c 2021-03-02 op
162 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
163 5e11c00c 2021-03-02 op if (t->id == id)
164 5e11c00c 2021-03-02 op return t;
165 5e11c00c 2021-03-02 op }
166 5e11c00c 2021-03-02 op
167 5d6c2088 2021-07-19 op return NULL;
168 5e11c00c 2021-03-02 op }
169 5e11c00c 2021-03-02 op
170 5e11c00c 2021-03-02 op static void
171 b148be71 2024-01-31 op handle_imsg_check_cert(struct imsg *imsg)
172 5e11c00c 2021-03-02 op {
173 62cbcdae 2024-02-02 op char *hash;
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;
180 5e11c00c 2021-03-02 op
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)
183 cbcc75fb 2021-03-17 op abort();
184 b148be71 2024-01-31 op datalen = strlen(hash);
185 cbcc75fb 2021-03-17 op
186 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
187 f6243012 2021-07-19 op return;
188 cbcc75fb 2021-03-17 op
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;
192 984245ce 2021-06-23 op } else {
193 25905047 2024-01-16 op host = tab->iri.iri_host;
194 25905047 2024-01-16 op port = tab->iri.iri_portstr;
195 984245ce 2021-06-23 op }
196 984245ce 2021-06-23 op
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))
202 cbcc75fb 2021-03-17 op abort();
203 cbcc75fb 2021-03-17 op
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)
206 cbcc75fb 2021-03-17 op abort();
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));
211 eb4388ee 2021-04-25 op }
212 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
213 4fa88f57 2022-04-24 op tofu_save(&certs, e);
214 cbcc75fb 2021-03-17 op } else
215 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
216 cbcc75fb 2021-03-17 op
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;
222 a2fd3805 2021-07-06 op else
223 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
224 a2fd3805 2021-07-06 op
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));
227 5d1bac73 2021-03-25 op } else {
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)
231 288fd238 2021-04-25 op die();
232 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
233 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
234 cbcc75fb 2021-03-17 op }
235 5d1bac73 2021-03-25 op }
236 5d1bac73 2021-03-25 op
237 5d1bac73 2021-03-25 op static void
238 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
239 5d1bac73 2021-03-25 op {
240 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
241 bc10f6a5 2021-07-12 op sizeof(accept));
242 288fd238 2021-04-25 op
243 a2fd3805 2021-07-06 op if (accept) {
244 25905047 2024-01-16 op const char *host, *port;
245 25905047 2024-01-16 op
246 25905047 2024-01-16 op host = tab->iri.iri_host;
247 25905047 2024-01-16 op port = tab->iri.iri_portstr;
248 25905047 2024-01-16 op
249 a2fd3805 2021-07-06 op /*
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.
256 a2fd3805 2021-07-06 op */
257 25905047 2024-01-16 op tofu_temp_trust(&certs, host, port, tab->cert);
258 a2fd3805 2021-07-06 op
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);
262 d0971653 2021-09-15 op else
263 d0971653 2021-09-15 op message("Certificate temporarly trusted");
264 a2fd3805 2021-07-06 op } else {
265 288fd238 2021-04-25 op free(tab->cert);
266 288fd238 2021-04-25 op tab->cert = NULL;
267 288fd238 2021-04-25 op }
268 5e11c00c 2021-03-02 op }
269 5e11c00c 2021-03-02 op
270 288fd238 2021-04-25 op static void
271 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
272 288fd238 2021-04-25 op {
273 288fd238 2021-04-25 op struct tofu_entry *e;
274 984245ce 2021-06-23 op const char *host, *port;
275 288fd238 2021-04-25 op
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;
279 984245ce 2021-06-23 op } else {
280 25905047 2024-01-16 op host = tab->iri.iri_host;
281 25905047 2024-01-16 op port = tab->iri.iri_portstr;
282 984245ce 2021-06-23 op }
283 984245ce 2021-06-23 op
284 288fd238 2021-04-25 op if (!accept)
285 288fd238 2021-04-25 op goto end;
286 288fd238 2021-04-25 op
287 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
288 288fd238 2021-04-25 op die();
289 288fd238 2021-04-25 op
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));
294 288fd238 2021-04-25 op }
295 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
296 288fd238 2021-04-25 op
297 3078e1bc 2022-04-24 op tofu_update_persist(&certs, e);
298 288fd238 2021-04-25 op
299 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
300 288fd238 2021-04-25 op
301 288fd238 2021-04-25 op end:
302 288fd238 2021-04-25 op free(tab->cert);
303 288fd238 2021-04-25 op tab->cert = NULL;
304 288fd238 2021-04-25 op }
305 288fd238 2021-04-25 op
306 5cd2ebb1 2021-03-11 op static inline int
307 5cd2ebb1 2021-03-11 op normalize_code(int n)
308 5cd2ebb1 2021-03-11 op {
309 5cd2ebb1 2021-03-11 op if (n < 20) {
310 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
311 5cd2ebb1 2021-03-11 op return n;
312 5cd2ebb1 2021-03-11 op return 10;
313 5cd2ebb1 2021-03-11 op } else if (n < 30) {
314 5cd2ebb1 2021-03-11 op return 20;
315 5cd2ebb1 2021-03-11 op } else if (n < 40) {
316 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
317 5cd2ebb1 2021-03-11 op return n;
318 5cd2ebb1 2021-03-11 op return 30;
319 5cd2ebb1 2021-03-11 op } else if (n < 50) {
320 5cd2ebb1 2021-03-11 op if (n <= 44)
321 5cd2ebb1 2021-03-11 op return n;
322 5cd2ebb1 2021-03-11 op return 40;
323 5cd2ebb1 2021-03-11 op } else if (n < 60) {
324 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
325 5cd2ebb1 2021-03-11 op return n;
326 5cd2ebb1 2021-03-11 op return 50;
327 5cd2ebb1 2021-03-11 op } else if (n < 70) {
328 5cd2ebb1 2021-03-11 op if (n <= 62)
329 5cd2ebb1 2021-03-11 op return n;
330 5cd2ebb1 2021-03-11 op return 60;
331 5cd2ebb1 2021-03-11 op } else
332 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
333 5cd2ebb1 2021-03-11 op }
334 5cd2ebb1 2021-03-11 op
335 5e11c00c 2021-03-02 op static void
336 b148be71 2024-01-31 op handle_request_response(struct tab *tab)
337 5e11c00c 2021-03-02 op {
338 fcd99a0d 2021-11-05 op char buf[128];
339 54764e41 2024-02-02 op
340 54764e41 2024-02-02 op if (tab->code != 30 && tab->code != 31)
341 54764e41 2024-02-02 op tab->redirect_count = 0;
342 0972d8b2 2021-03-02 op
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)
349 ed504b9e 2022-02-07 op die();
350 ed504b9e 2022-02-07 op
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]);
360 fcd99a0d 2021-11-05 op } else {
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);
365 c07ac996 2021-03-12 op }
366 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
367 3a9b9365 2021-03-09 op tab->redirect_count++;
368 0972d8b2 2021-03-02 op
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]);
373 5cd2ebb1 2021-03-11 op } else
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);
380 3a9b9365 2021-03-09 op }
381 de2a69bb 2021-05-17 op }
382 de2a69bb 2021-05-17 op
383 de2a69bb 2021-05-17 op static void
384 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
385 de2a69bb 2021-05-17 op {
386 bb9912eb 2021-08-29 op const char *f;
387 bb9912eb 2021-08-29 op char input[PATH_MAX];
388 bb9912eb 2021-08-29 op
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);
391 fcd99a0d 2021-11-05 op
392 bb9912eb 2021-08-29 op if (!dosave) {
393 a2fd3805 2021-07-06 op stop_tab(tab);
394 bb9912eb 2021-08-29 op return;
395 bb9912eb 2021-08-29 op }
396 bb9912eb 2021-08-29 op
397 25905047 2024-01-16 op if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
398 bb9912eb 2021-08-29 op f = "";
399 bb9912eb 2021-08-29 op else
400 bb9912eb 2021-08-29 op f++;
401 bb9912eb 2021-08-29 op
402 112b9046 2021-08-29 op strlcpy(input, download_path, sizeof(input));
403 bb9912eb 2021-08-29 op strlcat(input, f, sizeof(input));
404 bb9912eb 2021-08-29 op
405 bb9912eb 2021-08-29 op ui_read("Save to path", handle_save_page_path, tab, input);
406 de2a69bb 2021-05-17 op }
407 de2a69bb 2021-05-17 op
408 de2a69bb 2021-05-17 op static void
409 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
410 d1353324 2021-07-13 op {
411 f63b8f73 2022-04-24 op struct download *d;
412 f63b8f73 2022-04-24 op int fd;
413 f63b8f73 2022-04-24 op
414 de2a69bb 2021-05-17 op if (path == NULL) {
415 d1353324 2021-07-13 op stop_tab(tab);
416 de2a69bb 2021-05-17 op return;
417 de2a69bb 2021-05-17 op }
418 de2a69bb 2021-05-17 op
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);
422 ff48dbfd 2022-05-05 op return;
423 ff48dbfd 2022-05-05 op }
424 868b3a8f 2022-04-13 op
425 ff48dbfd 2022-05-05 op ui_show_downloads_pane();
426 fd984e76 2022-05-05 op d = enqueue_download(tab->id, path);
427 ff48dbfd 2022-05-05 op d->fd = fd;
428 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
429 89e46508 2022-05-05 op
430 89e46508 2022-05-05 op /*
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.
433 89e46508 2022-05-05 op */
434 89e46508 2022-05-05 op tab->id = tab_new_id();
435 de2a69bb 2021-05-17 op }
436 de2a69bb 2021-05-17 op
437 de2a69bb 2021-05-17 op static void
438 b148be71 2024-01-31 op handle_dispatch_imsg(int fd, short event, void *data)
439 5e11c00c 2021-03-02 op {
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;
448 b148be71 2024-01-31 op ssize_t n;
449 54764e41 2024-02-02 op int code;
450 5e11c00c 2021-03-02 op
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");
454 b148be71 2024-01-31 op if (n == 0)
455 b148be71 2024-01-31 op err(1, "connection closed");
456 de2a69bb 2021-05-17 op }
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");
460 b148be71 2024-01-31 op if (n == 0)
461 b148be71 2024-01-31 op err(1, "connection closed");
462 b148be71 2024-01-31 op }
463 5e11c00c 2021-03-02 op
464 b148be71 2024-01-31 op for (;;) {
465 b148be71 2024-01-31 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
466 b148be71 2024-01-31 op err(1, "imsg_get");
467 b148be71 2024-01-31 op if (n == 0)
468 b148be71 2024-01-31 op break;
469 a5c3e03d 2021-03-02 op
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)
473 b148be71 2024-01-31 op break;
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)
476 b148be71 2024-01-31 op die();
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)
479 b148be71 2024-01-31 op die();
480 b148be71 2024-01-31 op load_page_from_str(tab, page);
481 b148be71 2024-01-31 op free(page);
482 b148be71 2024-01-31 op break;
483 b148be71 2024-01-31 op case IMSG_CHECK_CERT:
484 b148be71 2024-01-31 op handle_imsg_check_cert(&imsg);
485 b148be71 2024-01-31 op break;
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)
488 b148be71 2024-01-31 op break;
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)
492 b148be71 2024-01-31 op die();
493 b148be71 2024-01-31 op if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
494 b148be71 2024-01-31 op sizeof(tab->meta))
495 b148be71 2024-01-31 op die();
496 54764e41 2024-02-02 op tab->code = normalize_code(code);
497 b148be71 2024-01-31 op handle_request_response(tab);
498 b148be71 2024-01-31 op break;
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)
502 b148be71 2024-01-31 op return;
503 de2a69bb 2021-05-17 op
504 b148be71 2024-01-31 op if (tab) {
505 b148be71 2024-01-31 op if (!parser_parse(tab, imsg.data,
506 b148be71 2024-01-31 op imsg_get_len(&imsg)))
507 b148be71 2024-01-31 op die();
508 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
509 b148be71 2024-01-31 op } else {
510 b148be71 2024-01-31 op size_t datalen = imsg_get_len(&imsg);
511 b10ba331 2024-01-23 op
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();
515 b148be71 2024-01-31 op }
516 b148be71 2024-01-31 op break;
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)
520 b148be71 2024-01-31 op return;
521 b10ba331 2024-01-23 op
522 b148be71 2024-01-31 op if (tab != NULL) {
523 b148be71 2024-01-31 op if (!parser_free(tab))
524 b148be71 2024-01-31 op die();
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);
530 ea080950 2021-07-20 op
531 b148be71 2024-01-31 op /*
532 b148be71 2024-01-31 op * Gemini is handled as soon as a 2x
533 b148be71 2024-01-31 op * reply is got.
534 b148be71 2024-01-31 op */
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);
538 b148be71 2024-01-31 op
539 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
540 b148be71 2024-01-31 op ui_on_tab_loaded(tab);
541 b148be71 2024-01-31 op } else {
542 b148be71 2024-01-31 op close(d->fd);
543 b148be71 2024-01-31 op d->fd = -1;
544 b148be71 2024-01-31 op ui_on_download_refresh();
545 b148be71 2024-01-31 op }
546 b148be71 2024-01-31 op break;
547 b148be71 2024-01-31 op default:
548 b148be71 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
549 b148be71 2024-01-31 op }
550 b148be71 2024-01-31 op
551 b148be71 2024-01-31 op imsg_free(&imsg);
552 b148be71 2024-01-31 op }
553 b148be71 2024-01-31 op
554 b148be71 2024-01-31 op imsg_event_add(iev);
555 0972d8b2 2021-03-02 op }
556 0972d8b2 2021-03-02 op
557 cd1d043d 2024-01-13 op static void
558 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
559 0972d8b2 2021-03-02 op {
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);
564 4d3785b1 2021-03-09 op }
565 0972d8b2 2021-03-02 op
566 cd1d043d 2024-01-13 op static void
567 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
568 0b1053d2 2021-08-13 op {
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);
573 0b1053d2 2021-08-13 op }
574 0b1053d2 2021-08-13 op
575 cd1d043d 2024-01-13 op static void
576 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
577 5fb52fc0 2021-07-25 op {
578 5fb52fc0 2021-07-25 op struct get_req req;
579 25905047 2024-01-16 op const char *path;
580 5fb52fc0 2021-07-25 op
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));
584 5fb52fc0 2021-07-25 op
585 5fb52fc0 2021-07-25 op /*
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).
589 5fb52fc0 2021-07-25 op */
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));
592 5fb52fc0 2021-07-25 op } else {
593 25905047 2024-01-16 op path = tab->iri.iri_path;
594 25905047 2024-01-16 op while (*path == '/')
595 25905047 2024-01-16 op ++path;
596 25905047 2024-01-16 op strlcpy(req.req, path, sizeof(req.req));
597 5fb52fc0 2021-07-25 op }
598 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
599 5fb52fc0 2021-07-25 op
600 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
601 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_FINGER, NULL);
602 5fb52fc0 2021-07-25 op }
603 5fb52fc0 2021-07-25 op
604 cd1d043d 2024-01-13 op static void
605 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
606 4d3785b1 2021-03-09 op {
607 984245ce 2021-06-23 op struct get_req req;
608 d35e18b3 2024-02-04 op
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));
612 984245ce 2021-06-23 op
613 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
614 ca190c99 2021-08-14 op }
615 ca190c99 2021-08-14 op
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)
618 ca190c99 2021-08-14 op {
619 ca190c99 2021-08-14 op *ret_type = 0;
620 ca190c99 2021-08-14 op
621 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
622 ca190c99 2021-08-14 op *ret_type = '1';
623 ca190c99 2021-08-14 op return path;
624 ca190c99 2021-08-14 op }
625 ca190c99 2021-08-14 op
626 ca190c99 2021-08-14 op if (*path != '/')
627 ca190c99 2021-08-14 op return path;
628 ca190c99 2021-08-14 op path++;
629 ca190c99 2021-08-14 op
630 ca190c99 2021-08-14 op switch (*ret_type = *path) {
631 ca190c99 2021-08-14 op case '0':
632 ca190c99 2021-08-14 op case '1':
633 ca190c99 2021-08-14 op case '7':
634 ca190c99 2021-08-14 op break;
635 ca190c99 2021-08-14 op
636 ca190c99 2021-08-14 op default:
637 ca190c99 2021-08-14 op *ret_type = 0;
638 ca190c99 2021-08-14 op path -= 1;
639 ca190c99 2021-08-14 op return path;
640 ca190c99 2021-08-14 op }
641 ca190c99 2021-08-14 op
642 95bb01e2 2021-08-28 op return ++path;
643 1663bf1e 2021-07-25 op }
644 1663bf1e 2021-07-25 op
645 cd1d043d 2024-01-13 op static void
646 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
647 1663bf1e 2021-07-25 op {
648 81839fee 2021-07-25 op struct get_req req;
649 ca190c99 2021-08-14 op int type;
650 81839fee 2021-07-25 op const char *path;
651 1663bf1e 2021-07-25 op
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));
655 1663bf1e 2021-07-25 op
656 25905047 2024-01-16 op path = gopher_skip_selector(tab->iri.iri_path, &type);
657 ca190c99 2021-08-14 op switch (type) {
658 ca190c99 2021-08-14 op case '0':
659 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
660 ca190c99 2021-08-14 op break;
661 ca190c99 2021-08-14 op case '1':
662 ca190c99 2021-08-14 op parser_init(tab, gophermap_initparser);
663 ca190c99 2021-08-14 op break;
664 ca190c99 2021-08-14 op case '7':
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)
668 acf9defe 2022-02-08 op die();
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]);
671 cd1d043d 2024-01-13 op return;
672 ca190c99 2021-08-14 op default:
673 cd1d043d 2024-01-13 op load_page_from_str(tab, "Unknown gopher selector");
674 cd1d043d 2024-01-13 op return;
675 81839fee 2021-07-25 op }
676 81839fee 2021-07-25 op
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));
681 9a28e7e5 2021-08-03 op }
682 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
683 9a28e7e5 2021-08-03 op
684 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
685 0972d8b2 2021-03-02 op }
686 0972d8b2 2021-03-02 op
687 cd1d043d 2024-01-13 op static void
688 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
689 984245ce 2021-06-23 op {
690 984245ce 2021-06-23 op struct get_req req;
691 984245ce 2021-06-23 op
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));
695 984245ce 2021-06-23 op
696 cf1195ce 2021-07-25 op tab->proxy = p;
697 984245ce 2021-06-23 op
698 71bc1636 2024-02-06 op make_request(tab, &req, p->proto, hist_cur(tab->hist));
699 cf1195ce 2021-07-25 op }
700 984245ce 2021-06-23 op
701 cd1d043d 2024-01-13 op static void
702 71bc1636 2024-02-06 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
703 cf1195ce 2021-07-25 op {
704 71bc1636 2024-02-06 op int use_cert = 0, fd = -1;
705 71bc1636 2024-02-06 op
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);
709 71bc1636 2024-02-06 op }
710 d35e18b3 2024-02-04 op
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;
714 cf1195ce 2021-07-25 op
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));
718 cf1195ce 2021-07-25 op }
719 cf1195ce 2021-07-25 op
720 5df02e0d 2022-01-11 op start_loading_anim(tab);
721 d35e18b3 2024-02-04 op
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));
727 d35e18b3 2024-02-04 op }
728 d35e18b3 2024-02-04 op
729 d35e18b3 2024-02-04 op ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
730 984245ce 2021-06-23 op }
731 984245ce 2021-06-23 op
732 9a28e7e5 2021-08-03 op void
733 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
734 9a28e7e5 2021-08-03 op {
735 9a28e7e5 2021-08-03 op struct get_req req;
736 1b0e6efc 2021-08-08 op
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));
740 9a28e7e5 2021-08-03 op
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));
746 9a28e7e5 2021-08-03 op }
747 9a28e7e5 2021-08-03 op
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));
751 9a28e7e5 2021-08-03 op
752 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
753 9a28e7e5 2021-08-03 op parser_init(tab, gophermap_initparser);
754 9a28e7e5 2021-08-03 op
755 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
756 9a28e7e5 2021-08-03 op }
757 9a28e7e5 2021-08-03 op
758 cd1d043d 2024-01-13 op void
759 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
760 1bcb8303 2022-01-19 op {
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)))
763 1bcb8303 2022-01-19 op abort();
764 1bcb8303 2022-01-19 op if (!tab->buffer.page.free(&tab->buffer.page))
765 1bcb8303 2022-01-19 op abort();
766 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
767 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
768 1bcb8303 2022-01-19 op }
769 1bcb8303 2022-01-19 op
770 adea4eec 2021-07-16 op /*
771 cd1d043d 2024-01-13 op * Effectively load the given url in the given tab.
772 adea4eec 2021-07-16 op */
773 cd1d043d 2024-01-13 op static void
774 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
775 4d3785b1 2021-03-09 op {
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;
779 31f1a758 2021-04-22 op char *t;
780 65c49665 2024-01-23 op char buf[1025];
781 7943bb81 2021-07-10 op
782 7943bb81 2021-07-10 op tab->proxy = NULL;
783 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
784 10346511 2021-03-17 op
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)
788 31f1a758 2021-04-22 op die();
789 65c49665 2024-01-23 op hist_set_cur(tab->hist, url);
790 31f1a758 2021-04-22 op load_page_from_str(tab, t);
791 31f1a758 2021-04-22 op free(t);
792 cd1d043d 2024-01-13 op return;
793 31f1a758 2021-04-22 op }
794 31f1a758 2021-04-22 op
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);
797 31f1a758 2021-04-22 op
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);
801 cd1d043d 2024-01-13 op return;
802 8f3c9af8 2022-01-11 op }
803 d5af38cc 2022-01-10 op
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);
810 ba782cb8 2021-07-25 op
811 65c49665 2024-01-23 op p->loadfn(tab, buf);
812 cd1d043d 2024-01-13 op return;
813 4d3785b1 2021-03-09 op }
814 4d3785b1 2021-03-09 op }
815 4d3785b1 2021-03-09 op
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);
819 cd1d043d 2024-01-13 op return;
820 cd1d043d 2024-01-13 op }
821 984245ce 2021-06-23 op }
822 984245ce 2021-06-23 op
823 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
824 4d3785b1 2021-03-09 op }
825 4d3785b1 2021-03-09 op
826 2ddbda6b 2021-07-17 op /*
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.
829 2ddbda6b 2021-07-17 op */
830 9ad4627d 2021-03-10 op void
831 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
832 2051e653 2021-03-13 op {
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);
836 bc55cdb9 2021-08-12 op
837 fb8dcd1c 2022-01-18 op if (operating && lazy) {
838 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
839 fb8dcd1c 2022-01-18 op lazy = 0;
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);
843 65c49665 2024-01-23 op }
844 fb8dcd1c 2022-01-18 op
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();
848 2ddbda6b 2021-07-17 op return;
849 2ddbda6b 2021-07-17 op }
850 2ddbda6b 2021-07-17 op
851 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
852 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
853 2ddbda6b 2021-07-17 op }
854 2ddbda6b 2021-07-17 op
855 9eca3d7c 2021-11-02 op if (!lazy)
856 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
857 2051e653 2021-03-13 op }
858 2051e653 2021-03-13 op
859 a37d1a1c 2021-08-14 op void
860 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
861 a37d1a1c 2021-08-14 op {
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);
865 a37d1a1c 2021-08-14 op }
866 a37d1a1c 2021-08-14 op
867 0f7d8271 2024-01-22 op if (base == NULL)
868 65c49665 2024-01-23 op base = hist_cur(tab->hist);
869 0f7d8271 2024-01-22 op
870 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
871 a37d1a1c 2021-08-14 op }
872 a37d1a1c 2021-08-14 op
873 2051e653 2021-03-13 op int
874 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
875 2051e653 2021-03-13 op {
876 65c49665 2024-01-23 op const char *h;
877 2051e653 2021-03-13 op
878 65c49665 2024-01-23 op if ((h = hist_prev(tab->hist)) == NULL)
879 2051e653 2021-03-13 op return 0;
880 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
881 2051e653 2021-03-13 op return 1;
882 2051e653 2021-03-13 op }
883 2051e653 2021-03-13 op
884 2051e653 2021-03-13 op int
885 2051e653 2021-03-13 op load_next_page(struct tab *tab)
886 2051e653 2021-03-13 op {
887 65c49665 2024-01-23 op const char *h;
888 2051e653 2021-03-13 op
889 65c49665 2024-01-23 op if ((h = hist_next(tab->hist)) == NULL)
890 2051e653 2021-03-13 op return 0;
891 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
892 2051e653 2021-03-13 op return 1;
893 a37d1a1c 2021-08-14 op }
894 a37d1a1c 2021-08-14 op
895 2051e653 2021-03-13 op void
896 868b3a8f 2022-04-13 op write_buffer(const char *path, struct tab *tab)
897 868b3a8f 2022-04-13 op {
898 f63b8f73 2022-04-24 op FILE *fp;
899 f63b8f73 2022-04-24 op
900 868b3a8f 2022-04-13 op if (path == NULL)
901 868b3a8f 2022-04-13 op return;
902 868b3a8f 2022-04-13 op
903 f63b8f73 2022-04-24 op if ((fp = fopen(path, "w")) == NULL)
904 f63b8f73 2022-04-24 op return;
905 f63b8f73 2022-04-24 op if (!parser_serialize(tab, fp))
906 f63b8f73 2022-04-24 op message("Failed to save the page.");
907 f63b8f73 2022-04-24 op fclose(fp);
908 868b3a8f 2022-04-13 op }
909 868b3a8f 2022-04-13 op
910 c6efff96 2021-08-16 op /*
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.
913 c6efff96 2021-08-16 op *
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
917 c6efff96 2021-08-16 op *
918 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
919 f6d7d047 2021-12-30 op * url.
920 c6efff96 2021-08-16 op */
921 c6efff96 2021-08-16 op void
922 449ea6fe 2024-01-16 op humanify_url(const char *raw, const char *base, char *ret, size_t len)
923 c6efff96 2021-08-16 op {
924 44008d59 2024-01-16 op static struct iri iri;
925 c7107cec 2021-04-01 op
926 449ea6fe 2024-01-16 op if (load_url_use_heuristic)
927 449ea6fe 2024-01-16 op base = NULL;
928 449ea6fe 2024-01-16 op
929 449ea6fe 2024-01-16 op if (iri_parse(base, raw, &iri) == 0) {
930 44008d59 2024-01-16 op iri_unparse(&iri, ret, len);
931 c6efff96 2021-08-16 op return;
932 c6efff96 2021-08-16 op }
933 c6efff96 2021-08-16 op
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);
939 c6efff96 2021-08-16 op return;
940 c6efff96 2021-08-16 op }
941 c6efff96 2021-08-16 op
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);
945 c6efff96 2021-08-16 op return;
946 c6efff96 2021-08-16 op }
947 c6efff96 2021-08-16 op
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);
951 486bde7d 2022-04-24 op }
952 486bde7d 2022-04-24 op
953 486bde7d 2022-04-24 op int
954 486bde7d 2022-04-24 op bookmark_page(const char *url)
955 486bde7d 2022-04-24 op {
956 486bde7d 2022-04-24 op FILE *fp;
957 486bde7d 2022-04-24 op
958 486bde7d 2022-04-24 op if ((fp = fopen(bookmark_file, "a")) == NULL)
959 486bde7d 2022-04-24 op return -1;
960 486bde7d 2022-04-24 op fprintf(fp, "=> %s\n", url);
961 486bde7d 2022-04-24 op fclose(fp);
962 486bde7d 2022-04-24 op return 0;
963 c6efff96 2021-08-16 op }
964 c6efff96 2021-08-16 op
965 6cc5fcfe 2021-07-08 op static pid_t
966 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
967 6cc5fcfe 2021-07-08 op {
968 55ccd6d0 2021-09-15 op const char *argv[5];
969 6cc5fcfe 2021-07-08 op int argc = 0;
970 6cc5fcfe 2021-07-08 op pid_t pid;
971 6cc5fcfe 2021-07-08 op
972 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
973 6cc5fcfe 2021-07-08 op case -1:
974 6cc5fcfe 2021-07-08 op die();
975 6cc5fcfe 2021-07-08 op case 0:
976 6cc5fcfe 2021-07-08 op break;
977 6cc5fcfe 2021-07-08 op default:
978 6cc5fcfe 2021-07-08 op close(fd);
979 6cc5fcfe 2021-07-08 op return pid;
980 6cc5fcfe 2021-07-08 op }
981 6cc5fcfe 2021-07-08 op
982 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
983 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
984 6cc5fcfe 2021-07-08 op
985 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
986 6cc5fcfe 2021-07-08 op switch (p) {
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";
991 6cc5fcfe 2021-07-08 op break;
992 6cc5fcfe 2021-07-08 op }
993 6cc5fcfe 2021-07-08 op
994 55ccd6d0 2021-09-15 op if (safe_mode)
995 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
996 55ccd6d0 2021-09-15 op
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);
1000 4cf6ba13 2022-02-11 op }
1001 4cf6ba13 2022-02-11 op
1002 4cf6ba13 2022-02-11 op static void
1003 4cf6ba13 2022-02-11 op send_url(const char *url)
1004 4cf6ba13 2022-02-11 op {
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;
1008 4cf6ba13 2022-02-11 op
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");
1011 4cf6ba13 2022-02-11 op
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));
1015 4cf6ba13 2022-02-11 op
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);
1018 4cf6ba13 2022-02-11 op
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);
1024 6cc5fcfe 2021-07-08 op }
1025 6cc5fcfe 2021-07-08 op
1026 1fce2e75 2021-08-14 op int
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)
1029 bc10f6a5 2021-07-12 op {
1030 d35e18b3 2024-02-04 op return imsg_compose_event(iev_net, type, peerid, 0, fd, data,
1031 bc10f6a5 2021-07-12 op datalen);
1032 bc10f6a5 2021-07-12 op }
1033 bc10f6a5 2021-07-12 op
1034 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1035 6cc5fcfe 2021-07-08 op usage(int r)
1036 d2544989 2021-07-08 op {
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");
1040 6cc5fcfe 2021-07-08 op exit(r);
1041 740f578b 2021-03-15 op }
1042 740f578b 2021-03-15 op
1043 5e11c00c 2021-03-02 op int
1044 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1045 5e11c00c 2021-03-02 op {
1046 f63b8f73 2022-04-24 op struct imsgev net_ibuf;
1047 be97d6e6 2021-08-15 op pid_t pid;
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;
1053 be97d6e6 2021-08-15 op int status;
1054 6cc5fcfe 2021-07-08 op const char *argv0;
1055 5e11c00c 2021-03-02 op
1056 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1057 6cc5fcfe 2021-07-08 op
1058 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
1059 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1060 5e11c00c 2021-03-02 op
1061 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1062 d2544989 2021-07-08 op enable_colors = 0;
1063 449ea6fe 2024-01-16 op
1064 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1065 d2544989 2021-07-08 op switch (ch) {
1066 d2544989 2021-07-08 op case 'c':
1067 d2544989 2021-07-08 op fail = 1;
1068 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1069 d2544989 2021-07-08 op break;
1070 d2544989 2021-07-08 op case 'n':
1071 d2544989 2021-07-08 op configtest = 1;
1072 d2544989 2021-07-08 op break;
1073 d2544989 2021-07-08 op case 'h':
1074 6cc5fcfe 2021-07-08 op usage(0);
1075 d0971653 2021-09-15 op case 'S':
1076 d0971653 2021-09-15 op safe_mode = 1;
1077 d0971653 2021-09-15 op break;
1078 6cc5fcfe 2021-07-08 op case 'T':
1079 6cc5fcfe 2021-07-08 op switch (*optarg) {
1080 6cc5fcfe 2021-07-08 op case 'n':
1081 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1082 6cc5fcfe 2021-07-08 op break;
1083 6cc5fcfe 2021-07-08 op default:
1084 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1085 6cc5fcfe 2021-07-08 op *optarg);
1086 6cc5fcfe 2021-07-08 op }
1087 6cc5fcfe 2021-07-08 op break;
1088 dc761924 2021-07-15 op case 'v':
1089 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1090 dc761924 2021-07-15 op exit(0);
1091 dc761924 2021-07-15 op break;
1092 d2544989 2021-07-08 op default:
1093 6cc5fcfe 2021-07-08 op usage(1);
1094 d2544989 2021-07-08 op }
1095 d2544989 2021-07-08 op }
1096 d2544989 2021-07-08 op
1097 d2544989 2021-07-08 op argc -= optind;
1098 d2544989 2021-07-08 op argv += optind;
1099 d2544989 2021-07-08 op
1100 6cc5fcfe 2021-07-08 op if (proc != -1) {
1101 6cc5fcfe 2021-07-08 op if (argc > 0)
1102 6cc5fcfe 2021-07-08 op usage(1);
1103 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1104 f45bd2e3 2021-07-24 op return net_main();
1105 6cc5fcfe 2021-07-08 op else
1106 6cc5fcfe 2021-07-08 op usage(1);
1107 6cc5fcfe 2021-07-08 op }
1108 f63b8f73 2022-04-24 op
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);
1113 d5bdf203 2021-07-08 op
1114 d2734f08 2024-02-03 op fs_init();
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");
1120 d2544989 2021-07-08 op exit(0);
1121 d2544989 2021-07-08 op }
1122 d2544989 2021-07-08 op
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");
1126 cfcbc23c 2024-01-16 op
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");
1130 112b9046 2021-08-29 op
1131 449ea6fe 2024-01-16 op if (argc != 0) {
1132 449ea6fe 2024-01-16 op char *base;
1133 449ea6fe 2024-01-16 op
1134 449ea6fe 2024-01-16 op if (asprintf(&base, "file://%s/", cwd) == -1)
1135 449ea6fe 2024-01-16 op err(1, "asprintf");
1136 449ea6fe 2024-01-16 op
1137 449ea6fe 2024-01-16 op has_url = 1;
1138 449ea6fe 2024-01-16 op humanify_url(argv[0], base, url, sizeof(url));
1139 449ea6fe 2024-01-16 op
1140 449ea6fe 2024-01-16 op free(base);
1141 449ea6fe 2024-01-16 op }
1142 449ea6fe 2024-01-16 op
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);
1146 4cf6ba13 2022-02-11 op exit(0);
1147 4cf6ba13 2022-02-11 op }
1148 4cf6ba13 2022-02-11 op
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?");
1151 4cf6ba13 2022-02-11 op }
1152 d0fd368a 2021-07-15 op
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;
1160 5e11c00c 2021-03-02 op
1161 37d429b0 2021-04-01 op setproctitle("ui");
1162 35e1f40a 2021-03-14 op
1163 eb722b50 2022-01-03 op /* initialize tofu store */
1164 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1165 cbcc75fb 2021-03-17 op
1166 5e11c00c 2021-03-02 op event_init();
1167 5e11c00c 2021-03-02 op
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);
1172 4cf6ba13 2022-02-11 op }
1173 4cf6ba13 2022-02-11 op
1174 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1175 d5af38cc 2022-01-10 op mcache_init();
1176 d5af38cc 2022-01-10 op
1177 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1178 1fce2e75 2021-08-14 op autosave_init();
1179 ecfbb1d4 2021-08-14 op
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);
1185 d2544989 2021-07-08 op
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();
1194 941b3761 2021-03-18 op ui_end();
1195 941b3761 2021-03-18 op }
1196 5e11c00c 2021-03-02 op
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);
1199 be97d6e6 2021-08-15 op
1200 be97d6e6 2021-08-15 op /* wait for children to terminate */
1201 be97d6e6 2021-08-15 op do {
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));
1209 5e11c00c 2021-03-02 op
1210 f63b8f73 2022-04-24 op if (!safe_mode)
1211 f63b8f73 2022-04-24 op unlink(crashed_file);
1212 f63b8f73 2022-04-24 op
1213 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1214 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);
1215 d0fd368a 2021-07-15 op
1216 5e11c00c 2021-03-02 op return 0;
1217 5e11c00c 2021-03-02 op }