Blame


1 35e1f40a 2021-03-14 op /*
2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 35e1f40a 2021-03-14 op *
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.
7 35e1f40a 2021-03-14 op *
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.
15 35e1f40a 2021-03-14 op */
16 35e1f40a 2021-03-14 op
17 740f578b 2021-03-15 op /*
18 740f578b 2021-03-15 op * Handles the data in ~/.telescope
19 740f578b 2021-03-15 op */
20 740f578b 2021-03-15 op
21 35e1f40a 2021-03-14 op #include "telescope.h"
22 35e1f40a 2021-03-14 op
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>
29 35e1f40a 2021-03-14 op
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*);
36 35e1f40a 2021-03-14 op
37 35e1f40a 2021-03-14 op static struct event imsgev;
38 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
39 35e1f40a 2021-03-14 op
40 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
41 740f578b 2021-03-15 op
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,
46 35e1f40a 2021-03-14 op };
47 35e1f40a 2021-03-14 op
48 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
49 35e1f40a 2021-03-14 op die(void)
50 35e1f40a 2021-03-14 op {
51 35e1f40a 2021-03-14 op abort(); /* TODO */
52 35e1f40a 2021-03-14 op }
53 35e1f40a 2021-03-14 op
54 35e1f40a 2021-03-14 op static void
55 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
56 35e1f40a 2021-03-14 op {
57 35e1f40a 2021-03-14 op const char *t;
58 740f578b 2021-03-15 op char buf[BUFSIZ];
59 35e1f40a 2021-03-14 op size_t r;
60 35e1f40a 2021-03-14 op FILE *f;
61 35e1f40a 2021-03-14 op
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);
67 35e1f40a 2021-03-14 op return;
68 35e1f40a 2021-03-14 op }
69 35e1f40a 2021-03-14 op
70 35e1f40a 2021-03-14 op for (;;) {
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))
75 35e1f40a 2021-03-14 op break;
76 35e1f40a 2021-03-14 op }
77 35e1f40a 2021-03-14 op
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);
80 35e1f40a 2021-03-14 op
81 35e1f40a 2021-03-14 op fclose(f);
82 35e1f40a 2021-03-14 op }
83 35e1f40a 2021-03-14 op
84 35e1f40a 2021-03-14 op static void
85 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
86 35e1f40a 2021-03-14 op {
87 35e1f40a 2021-03-14 op char *data;
88 35e1f40a 2021-03-14 op const char *p;
89 35e1f40a 2021-03-14 op
90 35e1f40a 2021-03-14 op data = imsg->data;
91 35e1f40a 2021-03-14 op
92 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
93 35e1f40a 2021-03-14 op die();
94 35e1f40a 2021-03-14 op
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);
102 35e1f40a 2021-03-14 op } else {
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);
107 35e1f40a 2021-03-14 op }
108 35e1f40a 2021-03-14 op }
109 35e1f40a 2021-03-14 op
110 35e1f40a 2021-03-14 op static void
111 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
112 35e1f40a 2021-03-14 op {
113 35e1f40a 2021-03-14 op event_loopbreak();
114 35e1f40a 2021-03-14 op }
115 35e1f40a 2021-03-14 op
116 35e1f40a 2021-03-14 op static void
117 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
118 740f578b 2021-03-15 op {
119 740f578b 2021-03-15 op char *data;
120 740f578b 2021-03-15 op int res;
121 740f578b 2021-03-15 op FILE *f;
122 740f578b 2021-03-15 op
123 740f578b 2021-03-15 op data = imsg->data;
124 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
125 740f578b 2021-03-15 op die();
126 740f578b 2021-03-15 op
127 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
128 740f578b 2021-03-15 op res = errno;
129 740f578b 2021-03-15 op goto end;
130 740f578b 2021-03-15 op }
131 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
132 740f578b 2021-03-15 op fclose(f);
133 740f578b 2021-03-15 op
134 740f578b 2021-03-15 op res = 0;
135 740f578b 2021-03-15 op end:
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);
138 740f578b 2021-03-15 op }
139 740f578b 2021-03-15 op
140 740f578b 2021-03-15 op static void
141 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
142 35e1f40a 2021-03-14 op {
143 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
144 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
145 35e1f40a 2021-03-14 op }
146 35e1f40a 2021-03-14 op
147 35e1f40a 2021-03-14 op int
148 35e1f40a 2021-03-14 op fs_main(struct imsgbuf *b)
149 35e1f40a 2021-03-14 op {
150 35e1f40a 2021-03-14 op ibuf = b;
151 35e1f40a 2021-03-14 op
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));
154 740f578b 2021-03-15 op
155 35e1f40a 2021-03-14 op event_init();
156 35e1f40a 2021-03-14 op
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);
159 35e1f40a 2021-03-14 op
160 35e1f40a 2021-03-14 op sandbox_fs_process();
161 35e1f40a 2021-03-14 op
162 35e1f40a 2021-03-14 op event_dispatch();
163 35e1f40a 2021-03-14 op return 0;
164 35e1f40a 2021-03-14 op }