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 struct lineprefix {
62 const char *prfx1;
63 const char *prfx2;
64 };
65 extern struct lineprefix line_prefixes[];
67 struct line_face {
68 int prefix_prop;
69 int text_prop;
70 };
71 extern struct line_face line_faces[];
73 struct tab_face {
74 int background, tab, current_tab;
75 };
76 extern struct tab_face tab_face;
78 enum line_type {
79 LINE_TEXT,
80 LINE_LINK,
81 LINE_TITLE_1,
82 LINE_TITLE_2,
83 LINE_TITLE_3,
84 LINE_ITEM,
85 LINE_QUOTE,
86 LINE_PRE_START,
87 LINE_PRE_CONTENT,
88 LINE_PRE_END,
89 };
91 struct line {
92 enum line_type type;
93 char *line;
94 char *alt;
95 int flags;
96 TAILQ_ENTRY(line) lines;
97 };
99 struct vline {
100 const struct line *parent;
101 char *line;
102 int flags;
103 TAILQ_ENTRY(vline) vlines;
104 };
106 struct parser;
107 struct page;
109 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
110 typedef int (*parserfreefn)(struct parser*);
112 typedef void (imsg_handlerfn)(struct imsg*, size_t);
114 struct parser {
115 const char *name;
116 char title[32+1];
117 char *buf;
118 size_t len;
119 size_t cap;
120 int flags;
121 parsechunkfn parse;
122 parserfreefn free;
124 TAILQ_HEAD(, line) head;
125 };
127 /*
128 * differnt types of trust for a certificate. Following
129 * gemini://thfr.info/gemini/modified-trust-verify.gmi
130 */
131 enum trust_state {
132 TS_UNKNOWN,
133 TS_UNTRUSTED,
134 TS_TRUSTED,
135 TS_VERIFIED,
136 };
138 struct tofu_entry {
139 char domain[GEMINI_URL_LEN];
141 /*
142 * enough space for ``PROTO:HASH''. probably isn't a good
143 * idea tho.
144 */
145 char hash[128+1];
146 int verified;
147 };
149 struct histhead {
150 TAILQ_HEAD(mhisthead, hist) head;
151 size_t len;
152 };
153 struct hist {
154 char h[1025];
155 TAILQ_ENTRY(hist) entries;
156 };
158 struct buffer {
159 struct parser page;
160 int curs_x;
161 int curs_y;
162 size_t line_off;
163 size_t line_max;
164 struct vline *current_line;
165 size_t cpoff;
166 TAILQ_HEAD(vhead, vline) head;
167 };
169 extern TAILQ_HEAD(tabshead, tab) tabshead;
170 struct tab {
171 TAILQ_ENTRY(tab) tabs;
172 uint32_t id;
173 uint32_t flags;
175 char *cert;
176 enum trust_state trust;
177 struct phos_uri uri;
178 struct histhead hist;
179 struct hist *hist_cur;
180 size_t hist_off;
182 int code;
183 char meta[GEMINI_URL_LEN];
184 int redirect_count;
186 struct buffer buffer;
188 short loading_anim;
189 short loading_anim_step;
190 struct event loadingev;
192 int fd;
193 size_t bytes;
194 char *path;
195 };
197 struct proto {
198 const char *schema;
200 /*
201 * should load the given url in the tab. Optionally, it may
202 * consider the given url as relative to the one already
203 * present in tab. It must set tab->urlstr to a serialized
204 * human-friendly URL.
205 */
206 void (*loadfn)(struct tab*, const char*);
207 };
209 struct kmap {
210 TAILQ_HEAD(map, keymap) m;
211 void (*unhandled_input)(void);
212 };
214 struct keymap {
215 int meta;
216 int key;
217 struct kmap map;
218 void (*fn)(struct buffer*);
220 TAILQ_ENTRY(keymap) keymaps;
221 };
223 /* fs.c */
224 int fs_init(void);
225 int fs_main(struct imsgbuf*);
226 int load_certs(struct ohash*);
227 int load_last_session(void(*)(const char*));
229 /* gemini.c */
230 int client_main(struct imsgbuf*);
232 /* gemtext.c */
233 void gemtext_initparser(struct parser*);
235 /* hist.c */
236 void hist_clear_forward(struct histhead*, struct hist*);
237 void hist_push(struct histhead*, struct hist*);
239 /* keymap.c */
240 int kbd(const char*);
241 const char *unkbd(int);
242 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
244 /* mime.c */
245 int setup_parser_for(struct tab*);
247 /* pages.c */
248 extern const char *about_new;
250 #define CANNOT_FETCH 0
251 #define TOO_MUCH_REDIRECTS 1
252 #define MALFORMED_RESPONSE 2
253 #define UNKNOWN_TYPE_OR_CSET 3
254 extern const char *err_pages[70];
256 /* parse.y */
257 void parseconfig(const char *, int);
259 /* parser.c */
260 int parser_append(struct parser*, const char*, size_t);
261 int parser_set_buf(struct parser*, const char*, size_t);
262 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
264 /* sandbox.c */
265 void sandbox_network_process(void);
266 void sandbox_ui_process(void);
267 void sandbox_fs_process(void);
269 /* telescope.c */
270 void load_about_url(struct tab*, const char*);
271 void load_gemini_url(struct tab*, const char*);
272 void load_url(struct tab*, const char*);
273 int load_previous_page(struct tab*);
274 int load_next_page(struct tab*);
275 void stop_tab(struct tab*);
276 void add_to_bookmarks(const char*);
277 void save_session(void);
279 /* textplain.c */
280 void textplain_initparser(struct parser*);
282 /* tofu.c */
283 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
284 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
285 void tofu_add(struct ohash*, struct tofu_entry*);
286 void tofu_update(struct ohash*, struct tofu_entry*);
288 /* ui.c */
289 unsigned int tab_new_id(void);
290 int ui_init(int, char * const*);
291 void ui_on_tab_loaded(struct tab*);
292 void ui_on_tab_refresh(struct tab*);
293 void ui_require_input(struct tab*, int);
294 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
295 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
296 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
297 void ui_end(void);
299 /* utf.8 */
300 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
301 size_t utf8_encode(uint32_t, char*);
302 char *utf8_nth(char*, size_t);
303 size_t utf8_cplen(char*);
304 size_t utf8_chwidth(uint32_t);
305 size_t utf8_snwidth(const char*, size_t);
306 size_t utf8_swidth(const char*);
307 size_t utf8_swidth_between(const char*, const char*);
308 char *utf8_next_cp(const char*);
309 char *utf8_prev_cp(const char*, const char*);
311 /* util.c */
312 int mark_nonblock(int);
313 int has_prefix(const char*, const char*);
314 int unicode_isspace(uint32_t);
315 int unicode_isgraph(uint32_t);
316 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
318 /* wrap.c */
319 void empty_linelist(struct buffer*);
320 void empty_vlist(struct buffer*);
321 int wrap_text(struct buffer*, const char*, struct line*, size_t);
322 int hardwrap_text(struct buffer*, struct line*, size_t);
324 #endif /* TELESCOPE_H */