1 5e11c00c 2021-03-02 op #include "telescope.h"
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <event.h>
8 5e11c00c 2021-03-02 op #include <signal.h>
9 5e11c00c 2021-03-02 op #include <stdio.h>
10 5e11c00c 2021-03-02 op #include <stdlib.h>
11 5e11c00c 2021-03-02 op #include <string.h>
12 5e11c00c 2021-03-02 op #include <unistd.h>
14 5e11c00c 2021-03-02 op struct event imsgev;
15 5e11c00c 2021-03-02 op struct tabshead tabshead;
17 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
19 5e11c00c 2021-03-02 op static void handle_imsg_err(struct imsg*, size_t);
20 5e11c00c 2021-03-02 op static void handle_imsg_check_cert(struct imsg*, size_t);
21 5e11c00c 2021-03-02 op static void handle_imsg_got_code(struct imsg*, size_t);
22 5e11c00c 2021-03-02 op static void handle_imsg_got_meta(struct imsg*, size_t);
23 5e11c00c 2021-03-02 op static void handle_imsg_buf(struct imsg*, size_t);
24 5e11c00c 2021-03-02 op static void handle_imsg_eof(struct imsg*, size_t);
26 0972d8b2 2021-03-02 op static void load_page_from_str(struct tab*, const char*);
28 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
29 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
30 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
31 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
32 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
33 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
34 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
37 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
40 5e11c00c 2021-03-02 op abort(); /* TODO */
43 5e11c00c 2021-03-02 op static struct tab *
44 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
46 5e11c00c 2021-03-02 op struct tab *t;
48 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
49 5e11c00c 2021-03-02 op if (t->id == id)
57 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
59 5e11c00c 2021-03-02 op /* write(2, imsg->data, datalen); */
60 5e11c00c 2021-03-02 op /* fprintf(stderr, "\nEOF\n"); */
61 5e11c00c 2021-03-02 op /* event_loopbreak(); */
65 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
67 5e11c00c 2021-03-02 op int tofu_res = 1;
69 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
70 5e11c00c 2021-03-02 op imsg_flush(ibuf);
74 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
76 0972d8b2 2021-03-02 op const char *errpage;
77 0972d8b2 2021-03-02 op struct tab *tab;
79 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
81 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
83 0972d8b2 2021-03-02 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
85 0972d8b2 2021-03-02 op if (tab->code < 20) {
86 0972d8b2 2021-03-02 op if (tab->code != 10 && tab->code != 11)
87 0972d8b2 2021-03-02 op tab->code = 10;
88 0972d8b2 2021-03-02 op } else if (tab->code < 30)
89 0972d8b2 2021-03-02 op tab->code = 20;
90 0972d8b2 2021-03-02 op else if (tab->code < 40)
91 0972d8b2 2021-03-02 op tab->code = 30;
92 0972d8b2 2021-03-02 op else if (tab->code < 50)
93 0972d8b2 2021-03-02 op tab->code = 40;
94 0972d8b2 2021-03-02 op else if (tab->code < 60)
95 0972d8b2 2021-03-02 op tab->code = 50;
97 0972d8b2 2021-03-02 op tab->code = 60;
99 0972d8b2 2021-03-02 op if (tab->code != 30)
100 0972d8b2 2021-03-02 op tab->redirect_count = 0;
104 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
106 0972d8b2 2021-03-02 op struct tab *tab;
108 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
110 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
112 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
114 0972d8b2 2021-03-02 op /* TODO: parse the MIME type if it's 20 */
115 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
117 0972d8b2 2021-03-02 op if (tab->code == 20) {
118 0972d8b2 2021-03-02 op imsg_compose(ibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
119 0972d8b2 2021-03-02 op imsg_flush(ibuf);
123 0972d8b2 2021-03-02 op if (tab->code == 30)
126 0972d8b2 2021-03-02 op /* 4x, 5x or 6x */
128 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, err_pages[tab->code],
129 0972d8b2 2021-03-02 op strlen(err_pages[tab->code])))
131 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
134 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
138 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
140 0972d8b2 2021-03-02 op struct tab *tab;
142 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
144 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
147 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
151 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
153 a5c3e03d 2021-03-02 op struct tab *t;
155 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
156 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
159 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
160 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
164 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
166 5e11c00c 2021-03-02 op struct imsg imsg;
167 5e11c00c 2021-03-02 op size_t datalen;
170 5e11c00c 2021-03-02 op if ((n = imsg_read(ibuf)) == -1) {
171 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
176 5e11c00c 2021-03-02 op if (n == 0) {
177 5e11c00c 2021-03-02 op fprintf(stderr, "other side is dead\n");
182 5e11c00c 2021-03-02 op if ((n = imsg_get(ibuf, &imsg)) == -1)
186 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
187 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
188 5e11c00c 2021-03-02 op imsg_free(&imsg);
193 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
195 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
196 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, about_new, strlen(about_new)))
198 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
200 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
201 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
205 0972d8b2 2021-03-02 op load_url(struct tab *tab, const char *url)
207 8af5e5ed 2021-03-08 op strlcpy(tab->url, url, sizeof(tab->url));
209 0972d8b2 2021-03-02 op if (!strcmp(url, "about:new")) {
210 0972d8b2 2021-03-02 op load_page_from_str(tab, about_new);
214 0972d8b2 2021-03-02 op imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1, url, strlen(url)+1);
215 0972d8b2 2021-03-02 op imsg_flush(ibuf);
221 5e11c00c 2021-03-02 op struct imsgbuf main_ibuf, network_ibuf;
222 5e11c00c 2021-03-02 op int imsg_fds[2];
224 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
226 5e11c00c 2021-03-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
227 5e11c00c 2021-03-02 op err(1, "socketpair");
229 5e11c00c 2021-03-02 op switch (fork()) {
231 5e11c00c 2021-03-02 op err(1, "fork");
234 5e11c00c 2021-03-02 op setproctitle("client");
235 5e11c00c 2021-03-02 op close(imsg_fds[0]);
236 5e11c00c 2021-03-02 op imsg_init(&network_ibuf, imsg_fds[1]);
237 5e11c00c 2021-03-02 op exit(client_main(&network_ibuf));
240 5e11c00c 2021-03-02 op close(imsg_fds[1]);
241 5e11c00c 2021-03-02 op imsg_init(&main_ibuf, imsg_fds[0]);
242 5e11c00c 2021-03-02 op ibuf = &main_ibuf;
244 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
246 5e11c00c 2021-03-02 op event_init();
248 5e11c00c 2021-03-02 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
249 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
253 5e11c00c 2021-03-02 op event_dispatch();
255 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
256 5e11c00c 2021-03-02 op imsg_flush(ibuf);