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 void
90 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
91 {
92 const char *errpage;
93 struct tab *tab;
95 tab = tab_by_id(imsg->hdr.peerid);
97 if (sizeof(tab->code) != datalen)
98 die();
99 memcpy(&tab->code, imsg->data, sizeof(tab->code));
101 if (tab->code < 20) {
102 if (tab->code != 10 && tab->code != 11)
103 tab->code = 10;
104 } else if (tab->code < 30)
105 tab->code = 20;
106 else if (tab->code < 40)
107 tab->code = 30;
108 else if (tab->code < 50)
109 tab->code = 40;
110 else if (tab->code < 60)
111 tab->code = 50;
112 else
113 tab->code = 60;
115 if (tab->code != 30)
116 tab->redirect_count = 0;
119 static void
120 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
122 struct tab *tab;
124 tab = tab_by_id(imsg->hdr.peerid);
126 if (sizeof(tab->meta) <= datalen)
127 die();
128 memcpy(tab->meta, imsg->data, datalen);
130 if (tab->code != 30)
131 tab->redirect_count = 0;
133 if (tab->code == 20) {
134 /* TODO: parse the MIME type */
135 gemtext_initparser(&tab->page);
136 imsg_compose(ibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
137 imsg_flush(ibuf);
138 return;
141 if (tab->code == 30) {
142 tab->redirect_count++;
144 /* TODO: make customizable? */
145 if (tab->redirect_count > 5) {
146 load_page_from_str(tab,
147 err_pages[TOO_MUCH_REDIRECTS]);
148 return;
151 load_url(tab, tab->meta);
152 return;
155 /* 4x, 5x or 6x */
156 load_page_from_str(tab, err_pages[tab->code]);
159 static void
160 handle_imsg_buf(struct imsg *imsg, size_t datalen)
162 struct tab *tab;
164 tab = tab_by_id(imsg->hdr.peerid);
166 if (!tab->page.parse(&tab->page, imsg->data, datalen))
167 die();
169 ui_on_tab_refresh(tab);
172 static void
173 handle_imsg_eof(struct imsg *imsg, size_t datalen)
175 struct tab *t;
177 t = tab_by_id(imsg->hdr.peerid);
178 if (!t->page.free(&t->page))
179 die();
181 ui_on_tab_refresh(t);
182 ui_on_tab_loaded(t);
185 static void
186 dispatch_imsg(int fd, short ev, void *d)
188 struct imsg imsg;
189 size_t datalen;
190 ssize_t n;
192 if ((n = imsg_read(ibuf)) == -1) {
193 if (errno == EAGAIN || errno == EWOULDBLOCK)
194 return;
195 die();
198 if (n == 0) {
199 fprintf(stderr, "other side is dead\n");
200 exit(0);
203 for (;;) {
204 if ((n = imsg_get(ibuf, &imsg)) == -1)
205 die();
206 if (n == 0)
207 return;
208 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
209 handlers[imsg.hdr.type](&imsg, datalen);
210 imsg_free(&imsg);
214 static void
215 load_page_from_str(struct tab *tab, const char *page)
217 gemtext_initparser(&tab->page);
218 if (!tab->page.parse(&tab->page, page, strlen(page)))
219 die();
220 if (!tab->page.free(&tab->page))
221 die();
222 ui_on_tab_refresh(tab);
223 ui_on_tab_loaded(tab);
226 void
227 load_about_url(struct tab *tab, const char *url)
229 char *m;
231 memset(tab->urlstr, 0, sizeof(tab->urlstr));
232 memset(&tab->url, 0, sizeof(tab->url));
234 m = strchr(url, ':');
235 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
236 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
238 if (!strcmp(url, "about:new"))
239 load_page_from_str(tab, about_new);
240 else
241 load_page_from_str(tab, "# not found\n");
244 void
245 load_gemini_url(struct tab *tab, const char *url)
247 const char *err;
248 char *p;
250 if (has_prefix(url, "gemini:")) {
251 if (!url_parse(url, &tab->url, &err))
252 goto err;
253 } else {
254 if (!url_resolve_from(&tab->url, url, &err))
255 goto err;
258 url_unparse(&tab->url, tab->urlstr, sizeof(tab->urlstr));
259 imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1,
260 tab->urlstr, strlen(tab->urlstr)+1);
261 imsg_flush(ibuf);
262 return;
264 err:
265 if (asprintf(&p, "#error loading %s\n>%s\n",
266 url, err) == -1)
267 die();
268 strlcpy(tab->urlstr, url, sizeof(tab->urlstr));
269 load_page_from_str(tab, p);
270 free(p);
273 void
274 load_url(struct tab *tab, const char *url)
276 struct proto *p;
278 for (p = protos; p->schema != NULL; ++p) {
279 if (has_prefix(url, p->schema)) {
280 p->loadfn(tab, url);
281 return;
285 protos[0].loadfn(tab, url);
288 int
289 main(void)
291 struct imsgbuf main_ibuf, network_ibuf;
292 int imsg_fds[2];
294 signal(SIGCHLD, SIG_IGN);
296 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
297 err(1, "socketpair");
299 switch (fork()) {
300 case -1:
301 err(1, "fork");
302 case 0:
303 /* child */
304 setproctitle("client");
305 close(imsg_fds[0]);
306 imsg_init(&network_ibuf, imsg_fds[1]);
307 exit(client_main(&network_ibuf));
310 close(imsg_fds[1]);
311 imsg_init(&main_ibuf, imsg_fds[0]);
312 ibuf = &main_ibuf;
314 TAILQ_INIT(&tabshead);
316 event_init();
318 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
319 event_add(&imsgev, NULL);
321 ui_init();
323 event_dispatch();
325 imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
326 imsg_flush(ibuf);
328 ui_end();
330 return 0;