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
20 * TODO: add some form of locking on the files
23 #include "telescope.h"
34 static void die(void) __attribute__((__noreturn__));
35 static void serve_bookmarks(uint32_t);
36 static void handle_get(struct imsg*, size_t);
37 static void handle_quit(struct imsg*, size_t);
38 static void handle_bookmark_page(struct imsg*, size_t);
39 static void handle_save_cert(struct imsg*, size_t);
40 static void handle_session_start(struct imsg*, size_t);
41 static void handle_session_tab(struct imsg*, size_t);
42 static void handle_session_end(struct imsg*, size_t);
43 static void handle_dispatch_imsg(int, short, void*);
45 static struct event imsgev;
46 static struct imsgbuf *ibuf;
50 static char bookmark_file[PATH_MAX];
51 static char known_hosts_file[PATH_MAX];
52 static char session_file[PATH_MAX];
54 static imsg_handlerfn *handlers[] = {
55 [IMSG_GET] = handle_get,
56 [IMSG_QUIT] = handle_quit,
57 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
58 [IMSG_SAVE_CERT] = handle_save_cert,
59 [IMSG_SESSION_START] = handle_session_start,
60 [IMSG_SESSION_TAB] = handle_session_tab,
61 [IMSG_SESSION_END] = handle_session_end,
64 static void __attribute__((__noreturn__))
71 serve_bookmarks(uint32_t peerid)
78 if ((f = fopen(bookmark_file, "r")) == NULL) {
81 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
82 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
83 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
89 r = fread(buf, 1, sizeof(buf), f);
90 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
96 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
103 handle_get(struct imsg *imsg, size_t datalen)
110 if (data[datalen-1] != '\0')
113 if (!strcmp(data, "about:new")) {
114 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
115 about_new, strlen(about_new));
116 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
118 } else if (!strcmp(data, "about:bookmarks")) {
119 serve_bookmarks(imsg->hdr.peerid);
121 p = "# not found!\n";
122 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
123 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
129 handle_quit(struct imsg *imsg, size_t datalen)
135 handle_bookmark_page(struct imsg *imsg, size_t datalen)
142 if (data[datalen-1] != '\0')
145 if ((f = fopen(bookmark_file, "a")) == NULL) {
149 fprintf(f, "=> %s\n", data);
154 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
159 handle_save_cert(struct imsg *imsg, size_t datalen)
165 /* TODO: traverse the file to avoid duplications? */
167 if (datalen != sizeof(e))
169 memcpy(&e, imsg->data, datalen);
171 if ((f = fopen(known_hosts_file, "a")) == NULL) {
175 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
180 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
186 handle_session_start(struct imsg *imsg, size_t datalen)
191 if ((session = fopen(session_file, "w")) == NULL)
196 handle_session_tab(struct imsg *imsg, size_t datalen)
204 /* skip the NUL-terminator */
205 fwrite(imsg->data, 1, datalen-1, session);
207 fprintf(session, "\n");
211 handle_session_end(struct imsg *imsg, size_t datalen)
220 handle_dispatch_imsg(int fd, short ev, void *d)
222 struct imsgbuf *ibuf = d;
223 dispatch_imsg(ibuf, handlers, sizeof(handlers));
231 strlcpy(dir, getenv("HOME"), sizeof(dir));
232 strlcat(dir, "/.telescope", sizeof(dir));
235 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
236 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
238 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
239 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
241 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
242 strlcat(session_file, "/.telescope/session", sizeof(session_file));
248 fs_main(struct imsgbuf *b)
254 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
255 event_add(&imsgev, NULL);
257 sandbox_fs_process();
266 load_certs(struct ohash *h)
268 char *p, *last, *el, *line = NULL;
274 struct tofu_entry *e;
276 if ((f = fopen(known_hosts_file, "r")) == NULL)
279 while ((linelen = getline(&line, &linesize, f)) != -1) {
280 if ((e = calloc(1, sizeof(*e))) == NULL)
284 for ((p = strtok_r(line, " ", &last)); p;
285 (p = strtok_r(NULL, " ", &last))) {
293 strlcpy(e->domain, p, sizeof(e->domain));
296 strlcpy(e->hash, p, sizeof(e->hash));
299 if ((el = strchr(p, '\n')) == NULL)
303 /* 0 <= verified <= 1 */
304 e->verified = strtonum(p, -1, 2, &errstr);
306 errx(1, "verification for %s is %s: %s",
307 e->domain, errstr, p);
315 if (i != 0 && i != 3)
319 telescope_ohash_insert(h, e);
327 load_last_session(void (*cb)(const char*))
329 char *nl, *line = NULL;
335 if ((session = fopen(session_file, "r")) == NULL)
338 while ((linelen = getline(&line, &linesize, session)) != -1) {
339 if ((nl = strchr(line, '\n')) != NULL)