2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 740f578b 2021-03-15 op * Handles the data in ~/.telescope
21 35e1f40a 2021-03-14 op #include "telescope.h"
23 35e1f40a 2021-03-14 op #include <errno.h>
24 35e1f40a 2021-03-14 op #include <limits.h>
25 35e1f40a 2021-03-14 op #include <stdio.h>
26 35e1f40a 2021-03-14 op #include <stdlib.h>
27 35e1f40a 2021-03-14 op #include <string.h>
28 35e1f40a 2021-03-14 op #include <unistd.h>
30 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
31 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
32 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
33 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
34 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
35 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
37 35e1f40a 2021-03-14 op static struct event imsgev;
38 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
40 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
42 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
43 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
44 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
45 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
48 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
51 35e1f40a 2021-03-14 op abort(); /* TODO */
55 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
57 35e1f40a 2021-03-14 op const char *t;
58 740f578b 2021-03-15 op char buf[BUFSIZ];
62 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
63 740f578b 2021-03-15 op t = "# error\n\nCan't open bookmarks\n";
64 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
65 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
66 35e1f40a 2021-03-14 op imsg_flush(ibuf);
71 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
72 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
73 35e1f40a 2021-03-14 op imsg_flush(ibuf);
74 35e1f40a 2021-03-14 op if (r != sizeof(buf))
78 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
79 35e1f40a 2021-03-14 op imsg_flush(ibuf);
85 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
88 35e1f40a 2021-03-14 op const char *p;
90 35e1f40a 2021-03-14 op data = imsg->data;
92 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
95 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
96 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
97 35e1f40a 2021-03-14 op about_new, strlen(about_new));
98 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
99 35e1f40a 2021-03-14 op imsg_flush(ibuf);
100 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
101 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
103 35e1f40a 2021-03-14 op p = "# not found!\n";
104 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
105 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
106 35e1f40a 2021-03-14 op imsg_flush(ibuf);
111 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
113 35e1f40a 2021-03-14 op event_loopbreak();
117 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
123 740f578b 2021-03-15 op data = imsg->data;
124 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
127 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
131 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
136 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
137 740f578b 2021-03-15 op imsg_flush(ibuf);
141 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
143 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
144 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
148 35e1f40a 2021-03-14 op fs_main(struct imsgbuf *b)
152 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
153 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
155 35e1f40a 2021-03-14 op event_init();
157 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
158 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
160 35e1f40a 2021-03-14 op sandbox_fs_process();
162 35e1f40a 2021-03-14 op event_dispatch();