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.
18 * Handles the data in ~/.telescope
21 #include "telescope.h"
30 static void die(void) __attribute__((__noreturn__));
31 static void serve_bookmarks(uint32_t);
32 static void handle_get(struct imsg*, size_t);
33 static void handle_quit(struct imsg*, size_t);
34 static void handle_bookmark_page(struct imsg*, size_t);
35 static void handle_dispatch_imsg(int, short, void*);
37 static struct event imsgev;
38 static struct imsgbuf *ibuf;
40 static char bookmark_file[PATH_MAX];
42 static imsg_handlerfn *handlers[] = {
43 [IMSG_GET] = handle_get,
44 [IMSG_QUIT] = handle_quit,
45 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
48 static void __attribute__((__noreturn__))
55 serve_bookmarks(uint32_t peerid)
62 if ((f = fopen(bookmark_file, "r")) == NULL) {
63 t = "# error\n\nCan't open bookmarks\n";
64 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
65 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
71 r = fread(buf, 1, sizeof(buf), f);
72 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
78 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
85 handle_get(struct imsg *imsg, size_t datalen)
92 if (data[datalen-1] != '\0')
95 if (!strcmp(data, "about:new")) {
96 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
97 about_new, strlen(about_new));
98 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
100 } else if (!strcmp(data, "about:bookmarks")) {
101 serve_bookmarks(imsg->hdr.peerid);
103 p = "# not found!\n";
104 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
105 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
111 handle_quit(struct imsg *imsg, size_t datalen)
117 handle_bookmark_page(struct imsg *imsg, size_t datalen)
124 if (data[datalen-1] != '\0')
127 if ((f = fopen(bookmark_file, "a")) == NULL) {
131 fprintf(f, "=> %s\n", data);
136 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
141 handle_dispatch_imsg(int fd, short ev, void *d)
143 struct imsgbuf *ibuf = d;
144 dispatch_imsg(ibuf, handlers, sizeof(handlers));
148 fs_main(struct imsgbuf *b)
152 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
153 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
157 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
158 event_add(&imsgev, NULL);
160 sandbox_fs_process();