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