Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "telescope.h"
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 static void die(void) __attribute__((__noreturn__));
27 static void serve_bookmarks(uint32_t);
28 static void handle_get(struct imsg*, size_t);
29 static void handle_quit(struct imsg*, size_t);
30 static void dispatch_imsg(int, short, void*);
32 static struct event imsgev;
33 static struct imsgbuf *ibuf;
35 static imsg_handlerfn *handlers[] = {
36 [IMSG_GET] = handle_get,
37 [IMSG_QUIT] = handle_quit,
38 };
40 static void __attribute__((__noreturn__))
41 die(void)
42 {
43 abort(); /* TODO */
44 }
46 static void
47 serve_bookmarks(uint32_t peerid)
48 {
49 const char *t;
50 char path[PATH_MAX], buf[BUFSIZ];
51 size_t r;
52 FILE *f;
54 strlcpy(path, getenv("HOME"), sizeof(path));
55 strlcat(path, "/.telescope/bookmarks.gmi", sizeof(path));
57 if ((f = fopen(path, "r")) == NULL) {
58 t = "# error\n\nCan't open ~/.telescope/bookmarks.gmi";
59 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
60 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
61 imsg_flush(ibuf);
62 return;
63 }
65 for (;;) {
66 r = fread(buf, 1, sizeof(buf), f);
67 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
68 imsg_flush(ibuf);
69 if (r != sizeof(buf))
70 break;
71 }
73 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
74 imsg_flush(ibuf);
76 fclose(f);
77 }
79 static void
80 handle_get(struct imsg *imsg, size_t datalen)
81 {
82 char *data;
83 const char *p;
85 data = imsg->data;
87 if (data[datalen-1] != '\0')
88 die();
90 if (!strcmp(data, "about:new")) {
91 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
92 about_new, strlen(about_new));
93 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
94 imsg_flush(ibuf);
95 } else if (!strcmp(data, "about:bookmarks")) {
96 serve_bookmarks(imsg->hdr.peerid);
97 } else {
98 p = "# not found!\n";
99 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
100 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
101 imsg_flush(ibuf);
105 static void
106 handle_quit(struct imsg *imsg, size_t datalen)
108 event_loopbreak();
111 static void
112 dispatch_imsg(int fd, short ev, void *d)
114 struct imsgbuf *ibuf = d;
115 struct imsg imsg;
116 ssize_t n;
117 size_t datalen;
119 if ((n = imsg_read(ibuf)) == -1) {
120 if (errno == EAGAIN || errno == EWOULDBLOCK)
121 return;
122 _exit(1);
125 if (n == 0)
126 _exit(1);
128 for (;;) {
129 if ((n = imsg_get(ibuf, &imsg)) == -1)
130 _exit(1);
131 if (n == 0)
132 return;
133 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
134 handlers[imsg.hdr.type](&imsg, datalen);
135 imsg_free(&imsg);
139 int
140 fs_main(struct imsgbuf *b)
142 ibuf = b;
144 event_init();
146 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
147 event_add(&imsgev, NULL);
149 sandbox_fs_process();
151 event_dispatch();
152 return 0;