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 #ifndef TELESCOPE_H
18 #define TELESCOPE_H
20 #include "cmd.h"
21 #include "compat.h"
22 #include "phos/phos.h"
24 #include <event.h>
26 #define MIN(a, b) ((a) < (b) ? (a) : (b))
27 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 #define GEMINI_URL_LEN 1024
31 enum imsg_type {
32 /* ui <-> client/fs */
33 IMSG_GET, /* data is URL, peerid the tab id */
34 IMSG_ERR,
35 IMSG_CHECK_CERT,
36 IMSG_CERT_STATUS,
37 IMSG_GOT_CODE,
38 IMSG_GOT_META,
39 IMSG_PROCEED,
40 IMSG_STOP,
41 IMSG_BUF,
42 IMSG_EOF,
43 IMSG_QUIT,
45 /* ui <-> fs */
46 IMSG_BOOKMARK_PAGE,
47 IMSG_BOOKMARK_OK,
48 IMSG_SAVE_CERT,
49 IMSG_SAVE_CERT_OK,
50 IMSG_UPDATE_CERT,
51 IMSG_UPDATE_CERT_OK,
53 IMSG_FILE_OPEN,
54 IMSG_FILE_OPENED,
56 IMSG_SESSION_START,
57 IMSG_SESSION_TAB,
58 IMSG_SESSION_END,
59 };
61 enum line_type {
62 LINE_TEXT,
63 LINE_LINK,
64 LINE_TITLE_1,
65 LINE_TITLE_2,
66 LINE_TITLE_3,
67 LINE_ITEM,
68 LINE_QUOTE,
69 LINE_PRE_START,
70 LINE_PRE_CONTENT,
71 LINE_PRE_END,
72 };
74 struct line {
75 enum line_type type;
76 char *line;
77 char *alt;
78 int flags;
79 TAILQ_ENTRY(line) lines;
80 };
82 struct vline {
83 const struct line *parent;
84 char *line;
85 int flags;
86 TAILQ_ENTRY(vline) vlines;
87 };
89 struct parser;
90 struct page;
92 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
93 typedef int (*parserfreefn)(struct parser*);
95 typedef void (imsg_handlerfn)(struct imsg*, size_t);
97 struct parser {
98 const char *name;
99 char title[32+1];
100 char *buf;
101 size_t len;
102 size_t cap;
103 int flags;
104 parsechunkfn parse;
105 parserfreefn free;
107 TAILQ_HEAD(, line) head;
108 };
110 /*
111 * differnt types of trust for a certificate. Following
112 * gemini://thfr.info/gemini/modified-trust-verify.gmi
113 */
114 enum trust_state {
115 TS_UNKNOWN,
116 TS_UNTRUSTED,
117 TS_TRUSTED,
118 TS_VERIFIED,
119 };
121 struct tofu_entry {
122 char domain[GEMINI_URL_LEN];
124 /*
125 * enough space for ``PROTO:HASH''. probably isn't a good
126 * idea tho.
127 */
128 char hash[128+1];
129 int verified;
130 };
132 struct histhead {
133 TAILQ_HEAD(mhisthead, hist) head;
134 size_t len;
135 };
136 struct hist {
137 char h[1025];
138 TAILQ_ENTRY(hist) entries;
139 };
141 struct buffer {
142 struct parser page;
143 int curs_x;
144 int curs_y;
145 size_t line_off;
146 size_t line_max;
147 struct vline *current_line;
148 size_t cpoff;
149 TAILQ_HEAD(vhead, vline) head;
150 };
152 extern TAILQ_HEAD(tabshead, tab) tabshead;
153 struct tab {
154 TAILQ_ENTRY(tab) tabs;
155 uint32_t id;
156 uint32_t flags;
158 char *cert;
159 enum trust_state trust;
160 struct phos_uri uri;
161 struct histhead hist;
162 struct hist *hist_cur;
163 size_t hist_off;
165 int code;
166 char meta[GEMINI_URL_LEN];
167 int redirect_count;
169 struct buffer buffer;
171 short loading_anim;
172 short loading_anim_step;
173 struct event loadingev;
175 int fd;
176 size_t bytes;
177 char *path;
178 };
180 struct proto {
181 const char *schema;
183 /*
184 * should load the given url in the tab. Optionally, it may
185 * consider the given url as relative to the one already
186 * present in tab. It must set tab->urlstr to a serialized
187 * human-friendly URL.
188 */
189 void (*loadfn)(struct tab*, const char*);
190 };
192 struct kmap {
193 TAILQ_HEAD(map, keymap) m;
194 void (*unhandled_input)(void);
195 };
197 struct keymap {
198 int meta;
199 int key;
200 struct kmap map;
201 void (*fn)(struct buffer*);
203 TAILQ_ENTRY(keymap) keymaps;
204 };
206 /* fs.c */
207 int fs_init(void);
208 int fs_main(struct imsgbuf*);
209 int load_certs(struct ohash*);
210 int load_last_session(void(*)(const char*));
212 /* gemini.c */
213 int client_main(struct imsgbuf*);
215 /* gemtext.c */
216 void gemtext_initparser(struct parser*);
218 /* hist.c */
219 void hist_clear_forward(struct histhead*, struct hist*);
220 void hist_push(struct histhead*, struct hist*);
222 /* keymap.c */
223 int kbd(const char*);
224 const char *unkbd(int);
225 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
227 /* mime.c */
228 int setup_parser_for(struct tab*);
230 /* pages.c */
231 extern const char *about_new;
233 #define CANNOT_FETCH 0
234 #define TOO_MUCH_REDIRECTS 1
235 #define MALFORMED_RESPONSE 2
236 #define UNKNOWN_TYPE_OR_CSET 3
237 extern const char *err_pages[70];
239 /* parser.c */
240 int parser_append(struct parser*, const char*, size_t);
241 int parser_set_buf(struct parser*, const char*, size_t);
242 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
244 /* sandbox.c */
245 void sandbox_network_process(void);
246 void sandbox_ui_process(void);
247 void sandbox_fs_process(void);
249 /* telescope.c */
250 void load_about_url(struct tab*, const char*);
251 void load_gemini_url(struct tab*, const char*);
252 void load_url(struct tab*, const char*);
253 int load_previous_page(struct tab*);
254 int load_next_page(struct tab*);
255 void stop_tab(struct tab*);
256 void add_to_bookmarks(const char*);
257 void save_session(void);
259 /* textplain.c */
260 void textplain_initparser(struct parser*);
262 /* tofu.c */
263 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
264 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
265 void tofu_add(struct ohash*, struct tofu_entry*);
266 void tofu_update(struct ohash*, struct tofu_entry*);
268 /* ui.c */
269 unsigned int tab_new_id(void);
270 int ui_init(int, char * const*);
271 void ui_on_tab_loaded(struct tab*);
272 void ui_on_tab_refresh(struct tab*);
273 void ui_require_input(struct tab*, int);
274 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
275 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
276 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
277 void ui_end(void);
279 /* utf.8 */
280 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
281 size_t utf8_encode(uint32_t, char*);
282 char *utf8_nth(char*, size_t);
283 size_t utf8_cplen(char*);
284 size_t utf8_chwidth(uint32_t);
285 size_t utf8_snwidth(const char*, size_t);
286 size_t utf8_swidth(const char*);
287 size_t utf8_swidth_between(const char*, const char*);
288 char *utf8_next_cp(const char*);
289 char *utf8_prev_cp(const char*, const char*);
291 /* util.c */
292 int mark_nonblock(int);
293 int has_prefix(const char*, const char*);
294 int unicode_isspace(uint32_t);
295 int unicode_isgraph(uint32_t);
296 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
298 /* wrap.c */
299 void empty_linelist(struct buffer*);
300 void empty_vlist(struct buffer*);
301 int wrap_text(struct buffer*, const char*, struct line*, size_t);
302 int hardwrap_text(struct buffer*, struct line*, size_t);
304 #endif /* TELESCOPE_H */