Blame


1 5e11c00c 2021-03-02 op #include <sys/socket.h>
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <errno.h>
4 dc761924 2021-07-15 op #include <getopt.h>
5 d2544989 2021-07-08 op #include <limits.h>
6 5e11c00c 2021-03-02 op #include <signal.h>
7 5e11c00c 2021-03-02 op #include <stdio.h>
8 5e11c00c 2021-03-02 op #include <stdlib.h>
9 5e11c00c 2021-03-02 op #include <string.h>
10 5e11c00c 2021-03-02 op #include <unistd.h>
11 5e11c00c 2021-03-02 op
12 e659558c 2021-07-13 op #include "defaults.h"
13 5a824be4 2021-07-13 op #include "pages.h"
14 395b9f4e 2021-07-12 op #include "parser.h"
15 395b9f4e 2021-07-12 op #include "telescope.h"
16 d1a0f2a3 2021-07-12 op #include "ui.h"
17 dc761924 2021-07-15 op
18 dc761924 2021-07-15 op static struct option longopts[] = {
19 1853ee50 2021-07-15 op {"colors", no_argument, NULL, 'c'},
20 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
21 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
22 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
23 dc761924 2021-07-15 op };
24 dc761924 2021-07-15 op
25 1853ee50 2021-07-15 op static const char *opts = "Cc:hnT:v";
26 395b9f4e 2021-07-12 op
27 bc10f6a5 2021-07-12 op static struct imsgev *iev_fs, *iev_net;
28 bc10f6a5 2021-07-12 op
29 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
30 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
31 6cc5fcfe 2021-07-08 op
32 6cc5fcfe 2021-07-08 op enum telescope_process {
33 6cc5fcfe 2021-07-08 op PROC_UI,
34 6cc5fcfe 2021-07-08 op PROC_FS,
35 6cc5fcfe 2021-07-08 op PROC_NET,
36 6cc5fcfe 2021-07-08 op };
37 5e11c00c 2021-03-02 op
38 c07ac996 2021-03-12 op /* the first is also the fallback one */
39 c07ac996 2021-03-12 op static struct proto protos[] = {
40 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
41 31f1a758 2021-04-22 op { "about", load_about_url },
42 4d3785b1 2021-03-09 op { NULL, NULL },
43 4d3785b1 2021-03-09 op };
44 4d3785b1 2021-03-09 op
45 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
46 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
47 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
48 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
49 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
50 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
51 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
52 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
53 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
54 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
55 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
56 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
57 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
58 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
59 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
60 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
61 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
62 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
63 adea4eec 2021-07-16 op static int do_load_url(struct tab*, const char*);
64 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
65 bc10f6a5 2021-07-12 op static int ui_send_net(int, uint32_t, const void *, uint16_t);
66 bc10f6a5 2021-07-12 op static int ui_send_fs(int, uint32_t, const void *, uint16_t);
67 5e11c00c 2021-03-02 op
68 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
69 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
70 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
71 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
72 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
73 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
74 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
75 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
76 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
77 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
78 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
79 5e11c00c 2021-03-02 op };
80 5e11c00c 2021-03-02 op
81 cbcc75fb 2021-03-17 op static struct ohash certs;
82 cbcc75fb 2021-03-17 op
83 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
84 5e11c00c 2021-03-02 op die(void)
85 5e11c00c 2021-03-02 op {
86 5e11c00c 2021-03-02 op abort(); /* TODO */
87 5e11c00c 2021-03-02 op }
88 5e11c00c 2021-03-02 op
89 5e11c00c 2021-03-02 op static struct tab *
90 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
91 5e11c00c 2021-03-02 op {
92 5e11c00c 2021-03-02 op struct tab *t;
93 5e11c00c 2021-03-02 op
94 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
95 5e11c00c 2021-03-02 op if (t->id == id)
96 5e11c00c 2021-03-02 op return t;
97 5e11c00c 2021-03-02 op }
98 5e11c00c 2021-03-02 op
99 5e11c00c 2021-03-02 op die();
100 5e11c00c 2021-03-02 op }
101 5e11c00c 2021-03-02 op
102 5e11c00c 2021-03-02 op static void
103 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
104 5e11c00c 2021-03-02 op {
105 3a9b9365 2021-03-09 op struct tab *tab;
106 3a9b9365 2021-03-09 op char *page;
107 3a9b9365 2021-03-09 op
108 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
109 3a9b9365 2021-03-09 op
110 3a9b9365 2021-03-09 op page = imsg->data;
111 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
112 3a9b9365 2021-03-09 op
113 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
114 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
115 3a9b9365 2021-03-09 op die();
116 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
117 3a9b9365 2021-03-09 op free(page);
118 5e11c00c 2021-03-02 op }
119 5e11c00c 2021-03-02 op
120 5e11c00c 2021-03-02 op static void
121 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
122 5e11c00c 2021-03-02 op {
123 984245ce 2021-06-23 op const char *hash, *host, *port;
124 cbcc75fb 2021-03-17 op int tofu_res;
125 cbcc75fb 2021-03-17 op struct tofu_entry *e;
126 cbcc75fb 2021-03-17 op struct tab *tab;
127 5e11c00c 2021-03-02 op
128 cbcc75fb 2021-03-17 op hash = imsg->data;
129 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
130 cbcc75fb 2021-03-17 op abort();
131 cbcc75fb 2021-03-17 op
132 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
133 cbcc75fb 2021-03-17 op
134 984245ce 2021-06-23 op if (tab->proxy != NULL) {
135 984245ce 2021-06-23 op host = tab->proxy->host;
136 984245ce 2021-06-23 op port = tab->proxy->port;
137 984245ce 2021-06-23 op } else {
138 984245ce 2021-06-23 op host = tab->uri.host;
139 984245ce 2021-06-23 op port = tab->uri.port;
140 984245ce 2021-06-23 op }
141 984245ce 2021-06-23 op
142 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
143 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
144 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
145 cbcc75fb 2021-03-17 op * the certs! */
146 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
147 cbcc75fb 2021-03-17 op abort();
148 cbcc75fb 2021-03-17 op
149 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
150 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
151 cbcc75fb 2021-03-17 op abort();
152 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
153 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
154 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
155 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
156 eb4388ee 2021-04-25 op }
157 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
158 3768e50f 2021-04-25 op tofu_add(&certs, e);
159 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
160 cbcc75fb 2021-03-17 op } else
161 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
162 cbcc75fb 2021-03-17 op
163 5d1bac73 2021-03-25 op if (tofu_res) {
164 a2fd3805 2021-07-06 op if (e->verified == -1)
165 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
166 a2fd3805 2021-07-06 op else if (e->verified == 1)
167 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
168 a2fd3805 2021-07-06 op else
169 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
170 a2fd3805 2021-07-06 op
171 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
172 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
173 5d1bac73 2021-03-25 op } else {
174 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
175 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
176 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
177 288fd238 2021-04-25 op die();
178 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
179 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
180 cbcc75fb 2021-03-17 op }
181 5d1bac73 2021-03-25 op }
182 5d1bac73 2021-03-25 op
183 5d1bac73 2021-03-25 op static void
184 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
185 5d1bac73 2021-03-25 op {
186 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
187 bc10f6a5 2021-07-12 op sizeof(accept));
188 288fd238 2021-04-25 op
189 a2fd3805 2021-07-06 op if (accept) {
190 a2fd3805 2021-07-06 op /*
191 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
192 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
193 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
194 a2fd3805 2021-07-06 op * tabid (one request == one tab id) and crash. It
195 a2fd3805 2021-07-06 op * also makes sense to save it for the current session
196 a2fd3805 2021-07-06 op * if the user accepted it.
197 a2fd3805 2021-07-06 op */
198 a2fd3805 2021-07-06 op tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
199 a2fd3805 2021-07-06 op
200 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
201 a2fd3805 2021-07-06 op handle_maybe_save_new_cert, tab);
202 a2fd3805 2021-07-06 op } else {
203 288fd238 2021-04-25 op free(tab->cert);
204 288fd238 2021-04-25 op tab->cert = NULL;
205 288fd238 2021-04-25 op }
206 5e11c00c 2021-03-02 op }
207 5e11c00c 2021-03-02 op
208 288fd238 2021-04-25 op static void
209 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
210 288fd238 2021-04-25 op {
211 288fd238 2021-04-25 op struct tofu_entry *e;
212 984245ce 2021-06-23 op const char *host, *port;
213 288fd238 2021-04-25 op
214 984245ce 2021-06-23 op if (tab->proxy != NULL) {
215 984245ce 2021-06-23 op host = tab->proxy->host;
216 984245ce 2021-06-23 op port = tab->proxy->port;
217 984245ce 2021-06-23 op } else {
218 984245ce 2021-06-23 op host = tab->uri.host;
219 984245ce 2021-06-23 op port = tab->uri.port;
220 984245ce 2021-06-23 op }
221 984245ce 2021-06-23 op
222 288fd238 2021-04-25 op if (!accept)
223 288fd238 2021-04-25 op goto end;
224 288fd238 2021-04-25 op
225 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
226 288fd238 2021-04-25 op die();
227 288fd238 2021-04-25 op
228 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
229 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
230 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
231 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
232 288fd238 2021-04-25 op }
233 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
234 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
235 288fd238 2021-04-25 op
236 288fd238 2021-04-25 op tofu_update(&certs, e);
237 288fd238 2021-04-25 op
238 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
239 288fd238 2021-04-25 op
240 288fd238 2021-04-25 op end:
241 288fd238 2021-04-25 op free(tab->cert);
242 288fd238 2021-04-25 op tab->cert = NULL;
243 288fd238 2021-04-25 op }
244 288fd238 2021-04-25 op
245 5cd2ebb1 2021-03-11 op static inline int
246 5cd2ebb1 2021-03-11 op normalize_code(int n)
247 5cd2ebb1 2021-03-11 op {
248 5cd2ebb1 2021-03-11 op if (n < 20) {
249 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
250 5cd2ebb1 2021-03-11 op return n;
251 5cd2ebb1 2021-03-11 op return 10;
252 5cd2ebb1 2021-03-11 op } else if (n < 30) {
253 5cd2ebb1 2021-03-11 op return 20;
254 5cd2ebb1 2021-03-11 op } else if (n < 40) {
255 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
256 5cd2ebb1 2021-03-11 op return n;
257 5cd2ebb1 2021-03-11 op return 30;
258 5cd2ebb1 2021-03-11 op } else if (n < 50) {
259 5cd2ebb1 2021-03-11 op if (n <= 44)
260 5cd2ebb1 2021-03-11 op return n;
261 5cd2ebb1 2021-03-11 op return 40;
262 5cd2ebb1 2021-03-11 op } else if (n < 60) {
263 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
264 5cd2ebb1 2021-03-11 op return n;
265 5cd2ebb1 2021-03-11 op return 50;
266 5cd2ebb1 2021-03-11 op } else if (n < 70) {
267 5cd2ebb1 2021-03-11 op if (n <= 62)
268 5cd2ebb1 2021-03-11 op return n;
269 5cd2ebb1 2021-03-11 op return 60;
270 5cd2ebb1 2021-03-11 op } else
271 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
272 5cd2ebb1 2021-03-11 op }
273 5cd2ebb1 2021-03-11 op
274 5e11c00c 2021-03-02 op static void
275 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
276 5e11c00c 2021-03-02 op {
277 0972d8b2 2021-03-02 op struct tab *tab;
278 5e11c00c 2021-03-02 op
279 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
280 0972d8b2 2021-03-02 op
281 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
282 5e11c00c 2021-03-02 op die();
283 5e11c00c 2021-03-02 op
284 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
285 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
286 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
287 0972d8b2 2021-03-02 op tab->redirect_count = 0;
288 5e11c00c 2021-03-02 op }
289 5e11c00c 2021-03-02 op
290 5e11c00c 2021-03-02 op static void
291 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
292 5e11c00c 2021-03-02 op {
293 0972d8b2 2021-03-02 op struct tab *tab;
294 0972d8b2 2021-03-02 op
295 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
296 0972d8b2 2021-03-02 op
297 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
298 0972d8b2 2021-03-02 op die();
299 5cd2ebb1 2021-03-11 op
300 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
301 0972d8b2 2021-03-02 op
302 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
303 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
304 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
305 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
306 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
307 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
308 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
309 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
310 c07ac996 2021-03-12 op } else {
311 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
312 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
313 a2fd3805 2021-07-06 op handle_maybe_save_page, tab);
314 c07ac996 2021-03-12 op }
315 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
316 3a9b9365 2021-03-09 op tab->redirect_count++;
317 0972d8b2 2021-03-02 op
318 3a9b9365 2021-03-09 op /* TODO: make customizable? */
319 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
320 3a9b9365 2021-03-09 op load_page_from_str(tab,
321 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
322 5cd2ebb1 2021-03-11 op } else
323 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
324 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
325 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
326 3a9b9365 2021-03-09 op }
327 de2a69bb 2021-05-17 op }
328 de2a69bb 2021-05-17 op
329 de2a69bb 2021-05-17 op static void
330 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
331 de2a69bb 2021-05-17 op {
332 de2a69bb 2021-05-17 op if (dosave)
333 d1353324 2021-07-13 op ui_read("Save to path", handle_save_page_path, tab);
334 de2a69bb 2021-05-17 op else
335 a2fd3805 2021-07-06 op stop_tab(tab);
336 de2a69bb 2021-05-17 op }
337 de2a69bb 2021-05-17 op
338 de2a69bb 2021-05-17 op static void
339 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
340 d1353324 2021-07-13 op {
341 de2a69bb 2021-05-17 op if (path == NULL) {
342 d1353324 2021-07-13 op stop_tab(tab);
343 de2a69bb 2021-05-17 op return;
344 de2a69bb 2021-05-17 op }
345 de2a69bb 2021-05-17 op
346 de2a69bb 2021-05-17 op tab->path = strdup(path);
347 de2a69bb 2021-05-17 op
348 d1353324 2021-07-13 op ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
349 5e11c00c 2021-03-02 op }
350 5e11c00c 2021-03-02 op
351 5e11c00c 2021-03-02 op static void
352 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
353 de2a69bb 2021-05-17 op {
354 de2a69bb 2021-05-17 op struct tab *tab;
355 de2a69bb 2021-05-17 op char *page;
356 de2a69bb 2021-05-17 op const char *e;
357 de2a69bb 2021-05-17 op int l;
358 de2a69bb 2021-05-17 op
359 de2a69bb 2021-05-17 op tab = tab_by_id(imsg->hdr.peerid);
360 de2a69bb 2021-05-17 op
361 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
362 de2a69bb 2021-05-17 op stop_tab(tab);
363 de2a69bb 2021-05-17 op
364 de2a69bb 2021-05-17 op e = imsg->data;
365 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
366 de2a69bb 2021-05-17 op die();
367 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
368 de2a69bb 2021-05-17 op tab->path, e);
369 de2a69bb 2021-05-17 op if (l == -1)
370 de2a69bb 2021-05-17 op die();
371 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
372 de2a69bb 2021-05-17 op free(page);
373 de2a69bb 2021-05-17 op } else {
374 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
375 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
376 de2a69bb 2021-05-17 op }
377 de2a69bb 2021-05-17 op }
378 de2a69bb 2021-05-17 op
379 de2a69bb 2021-05-17 op static void
380 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
381 5e11c00c 2021-03-02 op {
382 0972d8b2 2021-03-02 op struct tab *tab;
383 de2a69bb 2021-05-17 op int l;
384 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
385 5e11c00c 2021-03-02 op
386 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
387 5e11c00c 2021-03-02 op
388 de2a69bb 2021-05-17 op tab->bytes += datalen;
389 de2a69bb 2021-05-17 op if (tab->fd == -1) {
390 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
391 de2a69bb 2021-05-17 op imsg->data, datalen))
392 de2a69bb 2021-05-17 op die();
393 de2a69bb 2021-05-17 op } else {
394 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
395 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
396 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
397 de2a69bb 2021-05-17 op tab->path,
398 0e1eff5b 2021-06-23 op buf);
399 de2a69bb 2021-05-17 op if (l == -1)
400 de2a69bb 2021-05-17 op die();
401 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
402 de2a69bb 2021-05-17 op free(page);
403 de2a69bb 2021-05-17 op }
404 5e11c00c 2021-03-02 op
405 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
406 5e11c00c 2021-03-02 op }
407 5e11c00c 2021-03-02 op
408 5e11c00c 2021-03-02 op static void
409 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
410 5e11c00c 2021-03-02 op {
411 2ba66cea 2021-03-22 op struct tab *tab;
412 de2a69bb 2021-05-17 op int l;
413 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
414 a5c3e03d 2021-03-02 op
415 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
416 de2a69bb 2021-05-17 op
417 de2a69bb 2021-05-17 op if (tab->fd == -1) {
418 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
419 de2a69bb 2021-05-17 op die();
420 de2a69bb 2021-05-17 op } else {
421 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
422 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
423 de2a69bb 2021-05-17 op tab->path,
424 0e1eff5b 2021-06-23 op buf);
425 de2a69bb 2021-05-17 op if (l == -1)
426 de2a69bb 2021-05-17 op die();
427 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
428 de2a69bb 2021-05-17 op free(page);
429 a5c3e03d 2021-03-02 op
430 de2a69bb 2021-05-17 op close(tab->fd);
431 de2a69bb 2021-05-17 op tab->fd = -1;
432 de2a69bb 2021-05-17 op free(tab->path);
433 de2a69bb 2021-05-17 op tab->path = NULL;
434 de2a69bb 2021-05-17 op }
435 de2a69bb 2021-05-17 op
436 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
437 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
438 5e11c00c 2021-03-02 op }
439 5e11c00c 2021-03-02 op
440 5e11c00c 2021-03-02 op static void
441 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
442 740f578b 2021-03-15 op {
443 740f578b 2021-03-15 op int res;
444 740f578b 2021-03-15 op
445 740f578b 2021-03-15 op if (datalen != sizeof(res))
446 740f578b 2021-03-15 op die();
447 740f578b 2021-03-15 op
448 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
449 740f578b 2021-03-15 op if (res == 0)
450 7f963c41 2021-06-20 op message("Added to bookmarks!");
451 740f578b 2021-03-15 op else
452 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
453 740f578b 2021-03-15 op strerror(res));
454 740f578b 2021-03-15 op }
455 740f578b 2021-03-15 op
456 740f578b 2021-03-15 op static void
457 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
458 3a227e9a 2021-03-18 op {
459 3a227e9a 2021-03-18 op int res;
460 3a227e9a 2021-03-18 op
461 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
462 3a227e9a 2021-03-18 op die();
463 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
464 3a227e9a 2021-03-18 op if (res != 0)
465 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
466 3a227e9a 2021-03-18 op strerror(res));
467 288fd238 2021-04-25 op }
468 288fd238 2021-04-25 op
469 288fd238 2021-04-25 op static void
470 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
471 288fd238 2021-04-25 op {
472 288fd238 2021-04-25 op int res;
473 288fd238 2021-04-25 op
474 288fd238 2021-04-25 op if (datalen != sizeof(res))
475 288fd238 2021-04-25 op die();
476 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
477 288fd238 2021-04-25 op if (!res)
478 7f963c41 2021-06-20 op message("Failed to update the certificate");
479 3a227e9a 2021-03-18 op }
480 3a227e9a 2021-03-18 op
481 3a227e9a 2021-03-18 op static void
482 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
483 5e11c00c 2021-03-02 op {
484 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
485 bc10f6a5 2021-07-12 op dispatch_imsg(iev, ev, handlers, sizeof(handlers));
486 5e11c00c 2021-03-02 op }
487 5e11c00c 2021-03-02 op
488 0972d8b2 2021-03-02 op static void
489 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
490 0972d8b2 2021-03-02 op {
491 00ccb53d 2021-07-01 op erase_buffer(&tab->buffer);
492 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
493 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
494 0972d8b2 2021-03-02 op die();
495 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
496 0972d8b2 2021-03-02 op die();
497 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
498 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
499 0972d8b2 2021-03-02 op }
500 0972d8b2 2021-03-02 op
501 bcb0b073 2021-03-07 op void
502 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
503 0972d8b2 2021-03-02 op {
504 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
505 2051e653 2021-03-13 op
506 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
507 35e1f40a 2021-03-14 op
508 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_GET, tab->id,
509 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
510 4d3785b1 2021-03-09 op }
511 0972d8b2 2021-03-02 op
512 4d3785b1 2021-03-09 op void
513 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
514 4d3785b1 2021-03-09 op {
515 984245ce 2021-06-23 op struct get_req req;
516 754622a2 2021-03-15 op
517 2eef3403 2021-04-22 op stop_tab(tab);
518 2eef3403 2021-04-22 op tab->id = tab_new_id();
519 2eef3403 2021-04-22 op
520 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
521 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
522 984245ce 2021-06-23 op strlcpy(req.port, tab->uri.port, sizeof(req.host));
523 984245ce 2021-06-23 op
524 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
525 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
526 984245ce 2021-06-23 op
527 984245ce 2021-06-23 op req.proto = PROTO_GEMINI;
528 984245ce 2021-06-23 op
529 bc10f6a5 2021-07-12 op ui_send_net(IMSG_GET_RAW, tab->id,
530 984245ce 2021-06-23 op &req, sizeof(req));
531 0972d8b2 2021-03-02 op }
532 0972d8b2 2021-03-02 op
533 984245ce 2021-06-23 op void
534 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
535 984245ce 2021-06-23 op {
536 984245ce 2021-06-23 op struct get_req req;
537 984245ce 2021-06-23 op
538 984245ce 2021-06-23 op stop_tab(tab);
539 984245ce 2021-06-23 op tab->id = tab_new_id();
540 984245ce 2021-06-23 op tab->proxy = p;
541 984245ce 2021-06-23 op
542 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
543 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
544 984245ce 2021-06-23 op strlcpy(req.port, p->port, sizeof(req.host));
545 984245ce 2021-06-23 op
546 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
547 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
548 984245ce 2021-06-23 op
549 984245ce 2021-06-23 op req.proto = p->proto;
550 984245ce 2021-06-23 op
551 bc10f6a5 2021-07-12 op ui_send_net(IMSG_GET_RAW, tab->id,
552 984245ce 2021-06-23 op &req, sizeof(req));
553 984245ce 2021-06-23 op }
554 984245ce 2021-06-23 op
555 adea4eec 2021-07-16 op /*
556 adea4eec 2021-07-16 op * Effectively load the given url in the given tab. Return 1 when
557 adea4eec 2021-07-16 op * loading the page asynchronously, and thus when an erase_buffer can
558 adea4eec 2021-07-16 op * be done right after this function return, or 0 when loading the
559 adea4eec 2021-07-16 op * page synchronously. In this last case, erase_buffer *MUST* be
560 adea4eec 2021-07-16 op * called by the handling function (such as load_page_from_str).
561 adea4eec 2021-07-16 op */
562 adea4eec 2021-07-16 op static int
563 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
564 4d3785b1 2021-03-09 op {
565 31f1a758 2021-04-22 op struct phos_uri uri;
566 31f1a758 2021-04-22 op struct proto *p;
567 984245ce 2021-06-23 op struct proxy *proxy;
568 31f1a758 2021-04-22 op char *t;
569 7943bb81 2021-07-10 op
570 7943bb81 2021-07-10 op tab->proxy = NULL;
571 de2a69bb 2021-05-17 op
572 de2a69bb 2021-05-17 op if (tab->fd != -1) {
573 de2a69bb 2021-05-17 op close(tab->fd);
574 de2a69bb 2021-05-17 op tab->fd = -1;
575 de2a69bb 2021-05-17 op free(tab->path);
576 de2a69bb 2021-05-17 op tab->path = NULL;
577 de2a69bb 2021-05-17 op }
578 4d3785b1 2021-03-09 op
579 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
580 10346511 2021-03-17 op
581 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
582 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
583 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
584 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
585 31f1a758 2021-04-22 op die();
586 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
587 31f1a758 2021-04-22 op load_page_from_str(tab, t);
588 31f1a758 2021-04-22 op free(t);
589 adea4eec 2021-07-16 op return 0;
590 31f1a758 2021-04-22 op }
591 31f1a758 2021-04-22 op
592 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
593 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
594 31f1a758 2021-04-22 op
595 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
596 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
597 4d3785b1 2021-03-09 op p->loadfn(tab, url);
598 adea4eec 2021-07-16 op return 1;
599 4d3785b1 2021-03-09 op }
600 4d3785b1 2021-03-09 op }
601 4d3785b1 2021-03-09 op
602 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
603 984245ce 2021-06-23 op if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
604 984245ce 2021-06-23 op load_via_proxy(tab, url, proxy);
605 adea4eec 2021-07-16 op return 1;
606 984245ce 2021-06-23 op }
607 984245ce 2021-06-23 op }
608 984245ce 2021-06-23 op
609 adea4eec 2021-07-16 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
610 adea4eec 2021-07-16 op return 0;
611 4d3785b1 2021-03-09 op }
612 4d3785b1 2021-03-09 op
613 9ad4627d 2021-03-10 op void
614 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
615 2051e653 2021-03-13 op {
616 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
617 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
618 2051e653 2021-03-13 op
619 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
620 2051e653 2021-03-13 op event_loopbreak();
621 2051e653 2021-03-13 op return;
622 2051e653 2021-03-13 op }
623 984245ce 2021-06-23 op
624 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
625 adea4eec 2021-07-16 op if (do_load_url(tab, url))
626 adea4eec 2021-07-16 op erase_buffer(&tab->buffer);
627 2051e653 2021-03-13 op }
628 2051e653 2021-03-13 op
629 2051e653 2021-03-13 op int
630 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
631 2051e653 2021-03-13 op {
632 2051e653 2021-03-13 op struct hist *h;
633 2051e653 2021-03-13 op
634 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
635 2051e653 2021-03-13 op return 0;
636 2051e653 2021-03-13 op tab->hist_cur = h;
637 2051e653 2021-03-13 op do_load_url(tab, h->h);
638 2051e653 2021-03-13 op return 1;
639 2051e653 2021-03-13 op }
640 2051e653 2021-03-13 op
641 2051e653 2021-03-13 op int
642 2051e653 2021-03-13 op load_next_page(struct tab *tab)
643 2051e653 2021-03-13 op {
644 2051e653 2021-03-13 op struct hist *h;
645 2051e653 2021-03-13 op
646 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
647 2051e653 2021-03-13 op return 0;
648 2051e653 2021-03-13 op tab->hist_cur = h;
649 2051e653 2021-03-13 op do_load_url(tab, h->h);
650 2051e653 2021-03-13 op return 1;
651 2051e653 2021-03-13 op }
652 2051e653 2021-03-13 op
653 2051e653 2021-03-13 op void
654 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
655 9ad4627d 2021-03-10 op {
656 bc10f6a5 2021-07-12 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
657 de2a69bb 2021-05-17 op
658 de2a69bb 2021-05-17 op if (tab->fd != -1) {
659 de2a69bb 2021-05-17 op close(tab->fd);
660 de2a69bb 2021-05-17 op tab->fd = -1;
661 de2a69bb 2021-05-17 op free(tab->path);
662 de2a69bb 2021-05-17 op tab->path = NULL;
663 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
664 de2a69bb 2021-05-17 op }
665 9ad4627d 2021-03-10 op }
666 9ad4627d 2021-03-10 op
667 740f578b 2021-03-15 op void
668 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
669 740f578b 2021-03-15 op {
670 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
671 bc10f6a5 2021-07-12 op str, strlen(str)+1);
672 c7107cec 2021-04-01 op }
673 c7107cec 2021-04-01 op
674 c7107cec 2021-04-01 op void
675 c7107cec 2021-04-01 op save_session(void)
676 c7107cec 2021-04-01 op {
677 c7107cec 2021-04-01 op struct tab *tab;
678 c7107cec 2021-04-01 op
679 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
680 c7107cec 2021-04-01 op
681 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
682 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_TAB, 0,
683 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
684 c7107cec 2021-04-01 op }
685 c7107cec 2021-04-01 op
686 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
687 d2544989 2021-07-08 op }
688 d2544989 2021-07-08 op
689 d2544989 2021-07-08 op static void
690 d2544989 2021-07-08 op session_new_tab_cb(const char *url)
691 d2544989 2021-07-08 op {
692 d2544989 2021-07-08 op new_tab(url);
693 d2544989 2021-07-08 op }
694 d2544989 2021-07-08 op
695 6cc5fcfe 2021-07-08 op static pid_t
696 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
697 6cc5fcfe 2021-07-08 op {
698 6cc5fcfe 2021-07-08 op const char *argv[4];
699 6cc5fcfe 2021-07-08 op int argc = 0;
700 6cc5fcfe 2021-07-08 op pid_t pid;
701 6cc5fcfe 2021-07-08 op
702 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
703 6cc5fcfe 2021-07-08 op case -1:
704 6cc5fcfe 2021-07-08 op die();
705 6cc5fcfe 2021-07-08 op case 0:
706 6cc5fcfe 2021-07-08 op break;
707 6cc5fcfe 2021-07-08 op default:
708 6cc5fcfe 2021-07-08 op close(fd);
709 6cc5fcfe 2021-07-08 op return pid;
710 6cc5fcfe 2021-07-08 op }
711 6cc5fcfe 2021-07-08 op
712 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
713 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
714 6cc5fcfe 2021-07-08 op
715 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
716 6cc5fcfe 2021-07-08 op switch (p) {
717 6cc5fcfe 2021-07-08 op case PROC_UI:
718 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
719 6cc5fcfe 2021-07-08 op case PROC_FS:
720 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tf";
721 6cc5fcfe 2021-07-08 op break;
722 6cc5fcfe 2021-07-08 op case PROC_NET:
723 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
724 6cc5fcfe 2021-07-08 op break;
725 6cc5fcfe 2021-07-08 op }
726 6cc5fcfe 2021-07-08 op
727 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
728 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
729 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
730 6cc5fcfe 2021-07-08 op }
731 6cc5fcfe 2021-07-08 op
732 bc10f6a5 2021-07-12 op static int
733 bc10f6a5 2021-07-12 op ui_send_net(int type, uint32_t peerid, const void *data,
734 bc10f6a5 2021-07-12 op uint16_t datalen)
735 bc10f6a5 2021-07-12 op {
736 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
737 bc10f6a5 2021-07-12 op datalen);
738 bc10f6a5 2021-07-12 op }
739 bc10f6a5 2021-07-12 op
740 bc10f6a5 2021-07-12 op static int
741 bc10f6a5 2021-07-12 op ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
742 bc10f6a5 2021-07-12 op {
743 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
744 bc10f6a5 2021-07-12 op datalen);
745 bc10f6a5 2021-07-12 op }
746 bc10f6a5 2021-07-12 op
747 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
748 6cc5fcfe 2021-07-08 op usage(int r)
749 d2544989 2021-07-08 op {
750 dc761924 2021-07-15 op fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
751 dc761924 2021-07-15 op getprogname());
752 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
753 6cc5fcfe 2021-07-08 op exit(r);
754 740f578b 2021-03-15 op }
755 740f578b 2021-03-15 op
756 5e11c00c 2021-03-02 op int
757 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
758 5e11c00c 2021-03-02 op {
759 bc10f6a5 2021-07-12 op struct imsgev net_ibuf, fs_ibuf;
760 bc10f6a5 2021-07-12 op int pipe2net[2], pipe2fs[2];
761 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
762 d2544989 2021-07-08 op int has_url = 0;
763 6cc5fcfe 2021-07-08 op int proc = -1;
764 d0fd368a 2021-07-15 op int sessionfd;
765 d2544989 2021-07-08 op char path[PATH_MAX];
766 6cc5fcfe 2021-07-08 op const char *url = NEW_TAB_URL;
767 6cc5fcfe 2021-07-08 op const char *argv0;
768 5e11c00c 2021-03-02 op
769 6cc5fcfe 2021-07-08 op argv0 = argv[0];
770 6cc5fcfe 2021-07-08 op
771 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
772 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
773 5e11c00c 2021-03-02 op
774 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
775 d2544989 2021-07-08 op enable_colors = 0;
776 d2544989 2021-07-08 op
777 d2544989 2021-07-08 op strlcpy(path, getenv("HOME"), sizeof(path));
778 d2544989 2021-07-08 op strlcat(path, "/.telescope/config", sizeof(path));
779 d2544989 2021-07-08 op
780 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
781 d2544989 2021-07-08 op switch (ch) {
782 1853ee50 2021-07-15 op case 'C':
783 1853ee50 2021-07-15 op exit(ui_print_colors());
784 d2544989 2021-07-08 op case 'c':
785 d2544989 2021-07-08 op fail = 1;
786 d2544989 2021-07-08 op strlcpy(path, optarg, sizeof(path));
787 d2544989 2021-07-08 op break;
788 d2544989 2021-07-08 op case 'n':
789 d2544989 2021-07-08 op configtest = 1;
790 d2544989 2021-07-08 op break;
791 d2544989 2021-07-08 op case 'h':
792 6cc5fcfe 2021-07-08 op usage(0);
793 6cc5fcfe 2021-07-08 op case 'T':
794 6cc5fcfe 2021-07-08 op switch (*optarg) {
795 6cc5fcfe 2021-07-08 op case 'f':
796 6cc5fcfe 2021-07-08 op proc = PROC_FS;
797 6cc5fcfe 2021-07-08 op break;
798 6cc5fcfe 2021-07-08 op case 'n':
799 6cc5fcfe 2021-07-08 op proc = PROC_NET;
800 6cc5fcfe 2021-07-08 op break;
801 6cc5fcfe 2021-07-08 op default:
802 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
803 6cc5fcfe 2021-07-08 op *optarg);
804 6cc5fcfe 2021-07-08 op }
805 6cc5fcfe 2021-07-08 op break;
806 dc761924 2021-07-15 op case 'v':
807 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
808 dc761924 2021-07-15 op exit(0);
809 dc761924 2021-07-15 op break;
810 d2544989 2021-07-08 op default:
811 6cc5fcfe 2021-07-08 op usage(1);
812 d2544989 2021-07-08 op }
813 d2544989 2021-07-08 op }
814 d2544989 2021-07-08 op
815 d2544989 2021-07-08 op argc -= optind;
816 d2544989 2021-07-08 op argv += optind;
817 d2544989 2021-07-08 op
818 6cc5fcfe 2021-07-08 op if (proc != -1) {
819 6cc5fcfe 2021-07-08 op if (argc > 0)
820 6cc5fcfe 2021-07-08 op usage(1);
821 6cc5fcfe 2021-07-08 op else if (proc == PROC_FS)
822 6cc5fcfe 2021-07-08 op return fs_main();
823 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
824 6cc5fcfe 2021-07-08 op return client_main();
825 6cc5fcfe 2021-07-08 op else
826 6cc5fcfe 2021-07-08 op usage(1);
827 6cc5fcfe 2021-07-08 op }
828 6cc5fcfe 2021-07-08 op
829 d2544989 2021-07-08 op if (argc != 0) {
830 d2544989 2021-07-08 op has_url = 1;
831 d2544989 2021-07-08 op url = argv[0];
832 d2544989 2021-07-08 op }
833 d2544989 2021-07-08 op
834 d5bdf203 2021-07-08 op /* setup keys before reading the config */
835 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
836 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
837 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
838 d5bdf203 2021-07-08 op
839 d2544989 2021-07-08 op config_init();
840 d2544989 2021-07-08 op parseconfig(path, fail);
841 d2544989 2021-07-08 op if (configtest){
842 d2544989 2021-07-08 op puts("config OK");
843 d2544989 2021-07-08 op exit(0);
844 d2544989 2021-07-08 op }
845 d2544989 2021-07-08 op
846 d0fd368a 2021-07-15 op fs_init();
847 d0fd368a 2021-07-15 op if ((sessionfd = lock_session()) == -1)
848 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
849 d0fd368a 2021-07-15 op "telescope already running?");
850 d0fd368a 2021-07-15 op
851 6cc5fcfe 2021-07-08 op /* Start children. */
852 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
853 5e11c00c 2021-03-02 op err(1, "socketpair");
854 bc10f6a5 2021-07-12 op start_child(PROC_FS, argv0, pipe2fs[1]);
855 bc10f6a5 2021-07-12 op imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
856 bc10f6a5 2021-07-12 op iev_fs = &fs_ibuf;
857 bc10f6a5 2021-07-12 op iev_fs->handler = handle_dispatch_imsg;
858 5e11c00c 2021-03-02 op
859 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
860 35e1f40a 2021-03-14 op err(1, "socketpair");
861 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
862 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
863 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
864 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
865 5e11c00c 2021-03-02 op
866 37d429b0 2021-04-01 op setproctitle("ui");
867 35e1f40a 2021-03-14 op
868 6cc5fcfe 2021-07-08 op /* initialize tofu & load certificates */
869 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
870 3a227e9a 2021-03-18 op load_certs(&certs);
871 cbcc75fb 2021-03-17 op
872 5e11c00c 2021-03-02 op event_init();
873 5e11c00c 2021-03-02 op
874 bc10f6a5 2021-07-12 op /* Setup event handlers for pipes to fs/net */
875 bc10f6a5 2021-07-12 op iev_fs->events = EV_READ;
876 bc10f6a5 2021-07-12 op event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
877 bc10f6a5 2021-07-12 op iev_fs->handler, iev_fs);
878 bc10f6a5 2021-07-12 op event_add(&iev_fs->ev, NULL);
879 5e11c00c 2021-03-02 op
880 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
881 bc10f6a5 2021-07-12 op event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
882 bc10f6a5 2021-07-12 op iev_net->handler, iev_net);
883 bc10f6a5 2021-07-12 op event_add(&iev_net->ev, NULL);
884 35e1f40a 2021-03-14 op
885 d2544989 2021-07-08 op if (ui_init()) {
886 d2544989 2021-07-08 op load_last_session(session_new_tab_cb);
887 d2544989 2021-07-08 op if (has_url || TAILQ_EMPTY(&tabshead))
888 d2544989 2021-07-08 op new_tab(url);
889 d2544989 2021-07-08 op
890 941b3761 2021-03-18 op sandbox_ui_process();
891 941b3761 2021-03-18 op event_dispatch();
892 941b3761 2021-03-18 op ui_end();
893 941b3761 2021-03-18 op }
894 5e11c00c 2021-03-02 op
895 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_QUIT, 0, NULL, 0);
896 bc10f6a5 2021-07-12 op ui_send_net(IMSG_QUIT, 0, NULL, 0);
897 3e5fd7be 2021-07-13 op imsg_flush(&iev_fs->ibuf);
898 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
899 5e11c00c 2021-03-02 op
900 d0fd368a 2021-07-15 op close(sessionfd);
901 d0fd368a 2021-07-15 op
902 5e11c00c 2021-03-02 op return 0;
903 5e11c00c 2021-03-02 op }