Blob


1 #include "telescope.h"
3 #include <sys/socket.h>
5 #include <err.h>
6 #include <errno.h>
7 #include <event.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 struct event imsgev;
15 struct tabshead tabshead;
17 struct proto protos[] = {
18 { "gemini:", load_gemini_url },
19 { "about:", load_about_url },
20 { NULL, NULL },
21 };
23 static struct imsgbuf *ibuf;
25 static void handle_imsg_err(struct imsg*, size_t);
26 static void handle_imsg_check_cert(struct imsg*, size_t);
27 static void handle_imsg_got_code(struct imsg*, size_t);
28 static void handle_imsg_got_meta(struct imsg*, size_t);
29 static void handle_imsg_buf(struct imsg*, size_t);
30 static void handle_imsg_eof(struct imsg*, size_t);
32 static void load_page_from_str(struct tab*, const char*);
34 static imsg_handlerfn *handlers[] = {
35 [IMSG_ERR] = handle_imsg_err,
36 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
37 [IMSG_GOT_CODE] = handle_imsg_got_code,
38 [IMSG_GOT_META] = handle_imsg_got_meta,
39 [IMSG_BUF] = handle_imsg_buf,
40 [IMSG_EOF] = handle_imsg_eof,
41 };
43 static void __attribute__((__noreturn__))
44 die(void)
45 {
46 abort(); /* TODO */
47 }
49 static struct tab *
50 tab_by_id(uint32_t id)
51 {
52 struct tab *t;
54 TAILQ_FOREACH(t, &tabshead, tabs) {
55 if (t->id == id)
56 return t;
57 }
59 die();
60 }
62 static void
63 handle_imsg_err(struct imsg *imsg, size_t datalen)
64 {
65 struct tab *tab;
66 char *page;
68 tab = tab_by_id(imsg->hdr.peerid);
70 page = imsg->data;
71 page[datalen-1] = '\0';
73 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
74 tab->urlstr, page) == -1)
75 die();
76 load_page_from_str(tab, page);
77 free(page);
78 }
80 static void
81 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
82 {
83 int tofu_res = 1;
85 imsg_compose(ibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
86 imsg_flush(ibuf);
87 }
89 static inline int
90 normalize_code(int n)
91 {
92 if (n < 20) {
93 if (n == 10 || n == 11)
94 return n;
95 return 10;
96 } else if (n < 30) {
97 return 20;
98 } else if (n < 40) {
99 if (n == 30 || n == 31)
100 return n;
101 return 30;
102 } else if (n < 50) {
103 if (n <= 44)
104 return n;
105 return 40;
106 } else if (n < 60) {
107 if (n <= 53 || n == 59)
108 return n;
109 return 50;
110 } else if (n < 70) {
111 if (n <= 62)
112 return n;
113 return 60;
114 } else
115 return MALFORMED_RESPONSE;
118 static void
119 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
121 struct tab *tab;
123 tab = tab_by_id(imsg->hdr.peerid);
125 if (sizeof(tab->code) != datalen)
126 die();
128 memcpy(&tab->code, imsg->data, sizeof(tab->code));
129 tab->code = normalize_code(tab->code);
130 if (tab->code != 30 && tab->code != 31)
131 tab->redirect_count = 0;
134 static void
135 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
137 struct tab *tab;
139 tab = tab_by_id(imsg->hdr.peerid);
141 if (sizeof(tab->meta) <= datalen)
142 die();
144 memcpy(tab->meta, imsg->data, datalen);
146 if (tab->code < 10) { /* internal errors */
147 load_page_from_str(tab, err_pages[tab->code]);
148 } else if (tab->code < 20) { /* 1x */
149 load_page_from_str(tab, err_pages[tab->code]);
150 ui_require_input(tab, tab->code == 11);
151 } else if (tab->code == 20) {
152 /* TODO: parse mime type */
153 gemtext_initparser(&tab->page);
154 imsg_compose(ibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
155 imsg_flush(ibuf);
156 } else if (tab->code < 40) { /* 3x */
157 tab->redirect_count++;
159 /* TODO: make customizable? */
160 if (tab->redirect_count > 5) {
161 load_page_from_str(tab,
162 err_pages[TOO_MUCH_REDIRECTS]);
163 } else
164 load_url(tab, tab->meta);
165 } else { /* 4x, 5x & 6x */
166 load_page_from_str(tab, err_pages[tab->code]);
170 static void
171 handle_imsg_buf(struct imsg *imsg, size_t datalen)
173 struct tab *tab;
175 tab = tab_by_id(imsg->hdr.peerid);
177 if (!tab->page.parse(&tab->page, imsg->data, datalen))
178 die();
180 ui_on_tab_refresh(tab);
183 static void
184 handle_imsg_eof(struct imsg *imsg, size_t datalen)
186 struct tab *t;
188 t = tab_by_id(imsg->hdr.peerid);
189 if (!t->page.free(&t->page))
190 die();
192 ui_on_tab_refresh(t);
193 ui_on_tab_loaded(t);
196 static void
197 dispatch_imsg(int fd, short ev, void *d)
199 struct imsg imsg;
200 size_t datalen;
201 ssize_t n;
203 if ((n = imsg_read(ibuf)) == -1) {
204 if (errno == EAGAIN || errno == EWOULDBLOCK)
205 return;
206 die();
209 if (n == 0) {
210 fprintf(stderr, "other side is dead\n");
211 exit(0);
214 for (;;) {
215 if ((n = imsg_get(ibuf, &imsg)) == -1)
216 die();
217 if (n == 0)
218 return;
219 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 handlers[imsg.hdr.type](&imsg, datalen);
221 imsg_free(&imsg);
225 static void
226 load_page_from_str(struct tab *tab, const char *page)
228 gemtext_initparser(&tab->page);
229 if (!tab->page.parse(&tab->page, page, strlen(page)))
230 die();
231 if (!tab->page.free(&tab->page))
232 die();
233 ui_on_tab_refresh(tab);
234 ui_on_tab_loaded(tab);
237 void
238 load_about_url(struct tab *tab, const char *url)
240 char *m;
242 memset(tab->urlstr, 0, sizeof(tab->urlstr));
243 memset(&tab->url, 0, sizeof(tab->url));
245 m = strchr(url, ':');
246 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
247 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
249 strlcpy(tab->urlstr, url, sizeof(tab->urlstr));
251 if (!strcmp(url, "about:new"))
252 load_page_from_str(tab, about_new);
253 else
254 load_page_from_str(tab, "# not found\n");
257 void
258 load_gemini_url(struct tab *tab, const char *url)
260 const char *err;
261 char *p;
263 if (has_prefix(url, "gemini:")) {
264 if (!url_parse(url, &tab->url, &err))
265 goto err;
266 } else {
267 if (!url_resolve_from(&tab->url, url, &err))
268 goto err;
271 url_unparse(&tab->url, tab->urlstr, sizeof(tab->urlstr));
272 imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1,
273 tab->urlstr, strlen(tab->urlstr)+1);
274 imsg_flush(ibuf);
275 return;
277 err:
278 if (asprintf(&p, "#error loading %s\n>%s\n",
279 url, err) == -1)
280 die();
281 strlcpy(tab->urlstr, url, sizeof(tab->urlstr));
282 load_page_from_str(tab, p);
283 free(p);
286 void
287 load_url(struct tab *tab, const char *url)
289 struct proto *p;
291 for (p = protos; p->schema != NULL; ++p) {
292 if (has_prefix(url, p->schema)) {
293 p->loadfn(tab, url);
294 return;
298 protos[0].loadfn(tab, url);
301 void
302 stop_tab(struct tab *tab)
304 imsg_compose(ibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
305 imsg_flush(ibuf);
308 int
309 main(void)
311 struct imsgbuf main_ibuf, network_ibuf;
312 int imsg_fds[2];
314 signal(SIGCHLD, SIG_IGN);
316 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
317 err(1, "socketpair");
319 switch (fork()) {
320 case -1:
321 err(1, "fork");
322 case 0:
323 /* child */
324 setproctitle("client");
325 close(imsg_fds[0]);
326 imsg_init(&network_ibuf, imsg_fds[1]);
327 exit(client_main(&network_ibuf));
330 close(imsg_fds[1]);
331 imsg_init(&main_ibuf, imsg_fds[0]);
332 ibuf = &main_ibuf;
334 TAILQ_INIT(&tabshead);
336 event_init();
338 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
339 event_add(&imsgev, NULL);
341 ui_init();
343 event_dispatch();
345 imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
346 imsg_flush(ibuf);
348 ui_end();
350 return 0;