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