2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 #include "telescope.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,
40 static void __attribute__((__noreturn__))
47 serve_bookmarks(uint32_t peerid)
50 char path[PATH_MAX], buf[BUFSIZ];
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);
66 r = fread(buf, 1, sizeof(buf), f);
67 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
73 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
80 handle_get(struct imsg *imsg, size_t datalen)
87 if (data[datalen-1] != '\0')
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);
95 } else if (!strcmp(data, "about:bookmarks")) {
96 serve_bookmarks(imsg->hdr.peerid);
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);
106 handle_quit(struct imsg *imsg, size_t datalen)
112 dispatch_imsg(int fd, short ev, void *d)
114 struct imsgbuf *ibuf = d;
119 if ((n = imsg_read(ibuf)) == -1) {
120 if (errno == EAGAIN || errno == EWOULDBLOCK)
129 if ((n = imsg_get(ibuf, &imsg)) == -1)
133 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
134 handlers[imsg.hdr.type](&imsg, datalen);
140 fs_main(struct imsgbuf *b)
146 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
147 event_add(&imsgev, NULL);
149 sandbox_fs_process();