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 35e1f40a 2021-03-14 op #include "telescope.h"
18 35e1f40a 2021-03-14 op
19 35e1f40a 2021-03-14 op #include <errno.h>
20 35e1f40a 2021-03-14 op #include <limits.h>
21 35e1f40a 2021-03-14 op #include <stdio.h>
22 35e1f40a 2021-03-14 op #include <stdlib.h>
23 35e1f40a 2021-03-14 op #include <string.h>
24 35e1f40a 2021-03-14 op #include <unistd.h>
25 35e1f40a 2021-03-14 op
26 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
27 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
28 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
29 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
30 35e1f40a 2021-03-14 op static void dispatch_imsg(int, short, void*);
31 35e1f40a 2021-03-14 op
32 35e1f40a 2021-03-14 op static struct event imsgev;
33 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
34 35e1f40a 2021-03-14 op
35 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
36 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
37 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
38 35e1f40a 2021-03-14 op };
39 35e1f40a 2021-03-14 op
40 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
41 35e1f40a 2021-03-14 op die(void)
42 35e1f40a 2021-03-14 op {
43 35e1f40a 2021-03-14 op abort(); /* TODO */
44 35e1f40a 2021-03-14 op }
45 35e1f40a 2021-03-14 op
46 35e1f40a 2021-03-14 op static void
47 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
48 35e1f40a 2021-03-14 op {
49 35e1f40a 2021-03-14 op const char *t;
50 35e1f40a 2021-03-14 op char path[PATH_MAX], buf[BUFSIZ];
51 35e1f40a 2021-03-14 op size_t r;
52 35e1f40a 2021-03-14 op FILE *f;
53 35e1f40a 2021-03-14 op
54 35e1f40a 2021-03-14 op strlcpy(path, getenv("HOME"), sizeof(path));
55 35e1f40a 2021-03-14 op strlcat(path, "/.telescope/bookmarks.gmi", sizeof(path));
56 35e1f40a 2021-03-14 op
57 35e1f40a 2021-03-14 op if ((f = fopen(path, "r")) == NULL) {
58 35e1f40a 2021-03-14 op t = "# error\n\nCan't open ~/.telescope/bookmarks.gmi";
59 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
60 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
61 35e1f40a 2021-03-14 op imsg_flush(ibuf);
62 35e1f40a 2021-03-14 op return;
63 35e1f40a 2021-03-14 op }
64 35e1f40a 2021-03-14 op
65 35e1f40a 2021-03-14 op for (;;) {
66 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
67 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
68 35e1f40a 2021-03-14 op imsg_flush(ibuf);
69 35e1f40a 2021-03-14 op if (r != sizeof(buf))
70 35e1f40a 2021-03-14 op break;
71 35e1f40a 2021-03-14 op }
72 35e1f40a 2021-03-14 op
73 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
74 35e1f40a 2021-03-14 op imsg_flush(ibuf);
75 35e1f40a 2021-03-14 op
76 35e1f40a 2021-03-14 op fclose(f);
77 35e1f40a 2021-03-14 op }
78 35e1f40a 2021-03-14 op
79 35e1f40a 2021-03-14 op static void
80 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
81 35e1f40a 2021-03-14 op {
82 35e1f40a 2021-03-14 op char *data;
83 35e1f40a 2021-03-14 op const char *p;
84 35e1f40a 2021-03-14 op
85 35e1f40a 2021-03-14 op data = imsg->data;
86 35e1f40a 2021-03-14 op
87 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
88 35e1f40a 2021-03-14 op die();
89 35e1f40a 2021-03-14 op
90 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
91 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
92 35e1f40a 2021-03-14 op about_new, strlen(about_new));
93 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
94 35e1f40a 2021-03-14 op imsg_flush(ibuf);
95 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
96 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
97 35e1f40a 2021-03-14 op } else {
98 35e1f40a 2021-03-14 op p = "# not found!\n";
99 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
100 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
101 35e1f40a 2021-03-14 op imsg_flush(ibuf);
102 35e1f40a 2021-03-14 op }
103 35e1f40a 2021-03-14 op }
104 35e1f40a 2021-03-14 op
105 35e1f40a 2021-03-14 op static void
106 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
107 35e1f40a 2021-03-14 op {
108 35e1f40a 2021-03-14 op event_loopbreak();
109 35e1f40a 2021-03-14 op }
110 35e1f40a 2021-03-14 op
111 35e1f40a 2021-03-14 op static void
112 35e1f40a 2021-03-14 op dispatch_imsg(int fd, short ev, void *d)
113 35e1f40a 2021-03-14 op {
114 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
115 35e1f40a 2021-03-14 op struct imsg imsg;
116 35e1f40a 2021-03-14 op ssize_t n;
117 35e1f40a 2021-03-14 op size_t datalen;
118 35e1f40a 2021-03-14 op
119 35e1f40a 2021-03-14 op if ((n = imsg_read(ibuf)) == -1) {
120 35e1f40a 2021-03-14 op if (errno == EAGAIN || errno == EWOULDBLOCK)
121 35e1f40a 2021-03-14 op return;
122 35e1f40a 2021-03-14 op _exit(1);
123 35e1f40a 2021-03-14 op }
124 35e1f40a 2021-03-14 op
125 35e1f40a 2021-03-14 op if (n == 0)
126 35e1f40a 2021-03-14 op _exit(1);
127 35e1f40a 2021-03-14 op
128 35e1f40a 2021-03-14 op for (;;) {
129 35e1f40a 2021-03-14 op if ((n = imsg_get(ibuf, &imsg)) == -1)
130 35e1f40a 2021-03-14 op _exit(1);
131 35e1f40a 2021-03-14 op if (n == 0)
132 35e1f40a 2021-03-14 op return;
133 35e1f40a 2021-03-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
134 35e1f40a 2021-03-14 op handlers[imsg.hdr.type](&imsg, datalen);
135 35e1f40a 2021-03-14 op imsg_free(&imsg);
136 35e1f40a 2021-03-14 op }
137 35e1f40a 2021-03-14 op }
138 35e1f40a 2021-03-14 op
139 35e1f40a 2021-03-14 op int
140 35e1f40a 2021-03-14 op fs_main(struct imsgbuf *b)
141 35e1f40a 2021-03-14 op {
142 35e1f40a 2021-03-14 op ibuf = b;
143 35e1f40a 2021-03-14 op
144 35e1f40a 2021-03-14 op event_init();
145 35e1f40a 2021-03-14 op
146 35e1f40a 2021-03-14 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
147 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
148 35e1f40a 2021-03-14 op
149 35e1f40a 2021-03-14 op sandbox_fs_process();
150 35e1f40a 2021-03-14 op
151 35e1f40a 2021-03-14 op event_dispatch();
152 35e1f40a 2021-03-14 op return 0;
153 35e1f40a 2021-03-14 op }