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;
18 5e11c00c 2021-03-02 op static uint32_t tab_counter;
20 5e11c00c 2021-03-02 op static void handle_imsg_err(struct imsg*, size_t);
21 5e11c00c 2021-03-02 op static void handle_imsg_check_cert(struct imsg*, size_t);
22 5e11c00c 2021-03-02 op static void handle_imsg_got_code(struct imsg*, size_t);
23 5e11c00c 2021-03-02 op static void handle_imsg_got_meta(struct imsg*, size_t);
24 5e11c00c 2021-03-02 op static void handle_imsg_buf(struct imsg*, size_t);
25 5e11c00c 2021-03-02 op static void handle_imsg_eof(struct imsg*, size_t);
27 0972d8b2 2021-03-02 op static void load_page_from_str(struct tab*, const char*);
28 0972d8b2 2021-03-02 op static void load_url(struct tab*, const char*);
30 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
31 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
32 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
33 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
34 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
35 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
36 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
39 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
42 5e11c00c 2021-03-02 op abort(); /* TODO */
45 5e11c00c 2021-03-02 op static struct tab *
46 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
48 5e11c00c 2021-03-02 op struct tab *t;
50 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
51 5e11c00c 2021-03-02 op if (t->id == id)
59 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
61 5e11c00c 2021-03-02 op /* write(2, imsg->data, datalen); */
62 5e11c00c 2021-03-02 op /* fprintf(stderr, "\nEOF\n"); */
63 5e11c00c 2021-03-02 op /* event_loopbreak(); */
67 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
69 5e11c00c 2021-03-02 op int tofu_res = 1;
71 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
72 5e11c00c 2021-03-02 op imsg_flush(ibuf);
76 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
78 0972d8b2 2021-03-02 op const char *errpage;
79 0972d8b2 2021-03-02 op struct tab *tab;
81 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
83 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
85 0972d8b2 2021-03-02 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
87 0972d8b2 2021-03-02 op if (tab->code < 20) {
88 0972d8b2 2021-03-02 op if (tab->code != 10 && tab->code != 11)
89 0972d8b2 2021-03-02 op tab->code = 10;
90 0972d8b2 2021-03-02 op } else if (tab->code < 30)
91 0972d8b2 2021-03-02 op tab->code = 20;
92 0972d8b2 2021-03-02 op else if (tab->code < 40)
93 0972d8b2 2021-03-02 op tab->code = 30;
94 0972d8b2 2021-03-02 op else if (tab->code < 50)
95 0972d8b2 2021-03-02 op tab->code = 40;
96 0972d8b2 2021-03-02 op else if (tab->code < 60)
97 0972d8b2 2021-03-02 op tab->code = 50;
99 0972d8b2 2021-03-02 op tab->code = 60;
101 0972d8b2 2021-03-02 op if (tab->code != 30)
102 0972d8b2 2021-03-02 op tab->redirect_count = 0;
106 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
108 0972d8b2 2021-03-02 op struct tab *tab;
110 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
112 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
114 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
116 0972d8b2 2021-03-02 op /* TODO: parse the MIME type if it's 20 */
117 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
119 0972d8b2 2021-03-02 op if (tab->code == 20) {
120 0972d8b2 2021-03-02 op imsg_compose(ibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
121 0972d8b2 2021-03-02 op imsg_flush(ibuf);
125 0972d8b2 2021-03-02 op if (tab->code == 30)
128 0972d8b2 2021-03-02 op /* 4x, 5x or 6x */
130 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, err_pages[tab->code],
131 0972d8b2 2021-03-02 op strlen(err_pages[tab->code])))
133 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
136 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
140 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
142 0972d8b2 2021-03-02 op struct tab *tab;
144 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
146 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
149 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
153 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
155 a5c3e03d 2021-03-02 op struct tab *t;
157 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
158 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
161 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
165 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
167 5e11c00c 2021-03-02 op struct imsg imsg;
168 5e11c00c 2021-03-02 op size_t datalen;
171 5e11c00c 2021-03-02 op if ((n = imsg_read(ibuf)) == -1) {
172 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
177 5e11c00c 2021-03-02 op if (n == 0) {
178 5e11c00c 2021-03-02 op fprintf(stderr, "other side is dead\n");
183 5e11c00c 2021-03-02 op if ((n = imsg_get(ibuf, &imsg)) == -1)
187 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
188 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
189 5e11c00c 2021-03-02 op imsg_free(&imsg);
194 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
196 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
197 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, about_new, strlen(about_new)))
199 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
201 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
205 0972d8b2 2021-03-02 op load_url(struct tab *tab, const char *url)
207 0972d8b2 2021-03-02 op if (!strcmp(url, "about:new")) {
208 0972d8b2 2021-03-02 op load_page_from_str(tab, about_new);
212 0972d8b2 2021-03-02 op imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1, url, strlen(url)+1);
213 0972d8b2 2021-03-02 op imsg_flush(ibuf);
217 5e11c00c 2021-03-02 op new_tab(void)
219 5e11c00c 2021-03-02 op struct tab *tab;
220 1d08c280 2021-03-06 op /* const char *url = "about:new"; */
221 1d08c280 2021-03-06 op /* const char *url = "gemini://localhost.it/scroll.txt"; */
222 1d08c280 2021-03-06 op const char *url = "gemini://localhost.it/test.gmi";
224 5e11c00c 2021-03-02 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
227 5e11c00c 2021-03-02 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
229 5e11c00c 2021-03-02 op tab->id = tab_counter++;
230 5e11c00c 2021-03-02 op TAILQ_INIT(&tab->page.head);
232 5e11c00c 2021-03-02 op ui_on_new_tab(tab);
234 0972d8b2 2021-03-02 op load_url(tab, url);
240 5e11c00c 2021-03-02 op struct imsgbuf main_ibuf, network_ibuf;
241 5e11c00c 2021-03-02 op int imsg_fds[2];
243 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
245 5e11c00c 2021-03-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
246 5e11c00c 2021-03-02 op err(1, "socketpair");
248 5e11c00c 2021-03-02 op switch (fork()) {
250 5e11c00c 2021-03-02 op err(1, "fork");
253 5e11c00c 2021-03-02 op setproctitle("client");
254 5e11c00c 2021-03-02 op close(imsg_fds[0]);
255 5e11c00c 2021-03-02 op imsg_init(&network_ibuf, imsg_fds[1]);
256 5e11c00c 2021-03-02 op exit(client_main(&network_ibuf));
259 5e11c00c 2021-03-02 op close(imsg_fds[1]);
260 5e11c00c 2021-03-02 op imsg_init(&main_ibuf, imsg_fds[0]);
261 5e11c00c 2021-03-02 op ibuf = &main_ibuf;
263 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
265 5e11c00c 2021-03-02 op event_init();
267 5e11c00c 2021-03-02 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
268 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
274 5e11c00c 2021-03-02 op event_dispatch();
276 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
277 5e11c00c 2021-03-02 op imsg_flush(ibuf);