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 /*
18 * Handles the data in ~/.telescope
19 */
21 #include "telescope.h"
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.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,
46 };
48 static void __attribute__((__noreturn__))
49 die(void)
50 {
51 abort(); /* TODO */
52 }
54 static void
55 serve_bookmarks(uint32_t peerid)
56 {
57 const char *t;
58 char buf[BUFSIZ];
59 size_t r;
60 FILE *f;
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);
66 imsg_flush(ibuf);
67 return;
68 }
70 for (;;) {
71 r = fread(buf, 1, sizeof(buf), f);
72 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
73 imsg_flush(ibuf);
74 if (r != sizeof(buf))
75 break;
76 }
78 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
79 imsg_flush(ibuf);
81 fclose(f);
82 }
84 static void
85 handle_get(struct imsg *imsg, size_t datalen)
86 {
87 char *data;
88 const char *p;
90 data = imsg->data;
92 if (data[datalen-1] != '\0')
93 die();
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);
99 imsg_flush(ibuf);
100 } else if (!strcmp(data, "about:bookmarks")) {
101 serve_bookmarks(imsg->hdr.peerid);
102 } else {
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);
106 imsg_flush(ibuf);
110 static void
111 handle_quit(struct imsg *imsg, size_t datalen)
113 event_loopbreak();
116 static void
117 handle_bookmark_page(struct imsg *imsg, size_t datalen)
119 char *data;
120 int res;
121 FILE *f;
123 data = imsg->data;
124 if (data[datalen-1] != '\0')
125 die();
127 if ((f = fopen(bookmark_file, "a")) == NULL) {
128 res = errno;
129 goto end;
131 fprintf(f, "=> %s\n", data);
132 fclose(f);
134 res = 0;
135 end:
136 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
137 imsg_flush(ibuf);
140 static void
141 handle_dispatch_imsg(int fd, short ev, void *d)
143 struct imsgbuf *ibuf = d;
144 dispatch_imsg(ibuf, handlers, sizeof(handlers));
147 int
148 fs_main(struct imsgbuf *b)
150 ibuf = b;
152 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
153 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
155 event_init();
157 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
158 event_add(&imsgev, NULL);
160 sandbox_fs_process();
162 event_dispatch();
163 return 0;