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 struct modeline_face {
79 int background;
80 };
81 extern struct modeline_face modeline_face;
83 struct minibuffer_face {
84 int background;
85 };
86 extern struct minibuffer_face minibuffer_face;
88 enum line_type {
89 LINE_TEXT,
90 LINE_LINK,
91 LINE_TITLE_1,
92 LINE_TITLE_2,
93 LINE_TITLE_3,
94 LINE_ITEM,
95 LINE_QUOTE,
96 LINE_PRE_START,
97 LINE_PRE_CONTENT,
98 LINE_PRE_END,
99 };
101 struct line {
102 enum line_type type;
103 char *line;
104 char *alt;
105 int flags;
106 TAILQ_ENTRY(line) lines;
107 };
109 struct vline {
110 const struct line *parent;
111 char *line;
112 int flags;
113 TAILQ_ENTRY(vline) vlines;
114 };
116 struct parser;
117 struct page;
119 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
120 typedef int (*parserfreefn)(struct parser*);
122 typedef void (imsg_handlerfn)(struct imsg*, size_t);
124 struct parser {
125 const char *name;
126 char title[32+1];
127 char *buf;
128 size_t len;
129 size_t cap;
130 int flags;
131 parsechunkfn parse;
132 parserfreefn free;
134 TAILQ_HEAD(, line) head;
135 };
137 /*
138 * differnt types of trust for a certificate. Following
139 * gemini://thfr.info/gemini/modified-trust-verify.gmi
140 */
141 enum trust_state {
142 TS_UNKNOWN,
143 TS_UNTRUSTED,
144 TS_TRUSTED,
145 TS_VERIFIED,
146 };
148 struct tofu_entry {
149 char domain[GEMINI_URL_LEN];
151 /*
152 * enough space for ``PROTO:HASH''. probably isn't a good
153 * idea tho.
154 */
155 char hash[128+1];
156 int verified;
157 };
159 struct histhead {
160 TAILQ_HEAD(mhisthead, hist) head;
161 size_t len;
162 };
163 struct hist {
164 char h[1025];
165 TAILQ_ENTRY(hist) entries;
166 };
168 struct buffer {
169 struct parser page;
170 int curs_x;
171 int curs_y;
172 size_t line_off;
173 size_t line_max;
174 struct vline *current_line;
175 size_t cpoff;
176 TAILQ_HEAD(vhead, vline) head;
177 };
179 extern TAILQ_HEAD(tabshead, tab) tabshead;
180 struct tab {
181 TAILQ_ENTRY(tab) tabs;
182 uint32_t id;
183 uint32_t flags;
185 char *cert;
186 enum trust_state trust;
187 struct phos_uri uri;
188 struct histhead hist;
189 struct hist *hist_cur;
190 size_t hist_off;
192 int code;
193 char meta[GEMINI_URL_LEN];
194 int redirect_count;
196 struct buffer buffer;
198 short loading_anim;
199 short loading_anim_step;
200 struct event loadingev;
202 int fd;
203 size_t bytes;
204 char *path;
205 };
207 struct proto {
208 const char *schema;
210 /*
211 * should load the given url in the tab. Optionally, it may
212 * consider the given url as relative to the one already
213 * present in tab. It must set tab->urlstr to a serialized
214 * human-friendly URL.
215 */
216 void (*loadfn)(struct tab*, const char*);
217 };
219 struct kmap {
220 TAILQ_HEAD(map, keymap) m;
221 void (*unhandled_input)(void);
222 };
224 struct keymap {
225 int meta;
226 int key;
227 struct kmap map;
228 void (*fn)(struct buffer*);
230 TAILQ_ENTRY(keymap) keymaps;
231 };
233 /* defaults.c */
234 int config_setprfx(const char *, int, const char *);
236 /* fs.c */
237 int fs_init(void);
238 int fs_main(struct imsgbuf*);
239 int load_certs(struct ohash*);
240 int load_last_session(void(*)(const char*));
242 /* gemini.c */
243 int client_main(struct imsgbuf*);
245 /* gemtext.c */
246 void gemtext_initparser(struct parser*);
248 /* hist.c */
249 void hist_clear_forward(struct histhead*, struct hist*);
250 void hist_push(struct histhead*, struct hist*);
252 /* keymap.c */
253 int kbd(const char*);
254 const char *unkbd(int);
255 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
257 /* mime.c */
258 int setup_parser_for(struct tab*);
260 /* pages.c */
261 extern const char *about_new;
263 #define CANNOT_FETCH 0
264 #define TOO_MUCH_REDIRECTS 1
265 #define MALFORMED_RESPONSE 2
266 #define UNKNOWN_TYPE_OR_CSET 3
267 extern const char *err_pages[70];
269 /* parse.y */
270 void parseconfig(const char *, int);
272 /* parser.c */
273 int parser_append(struct parser*, const char*, size_t);
274 int parser_set_buf(struct parser*, const char*, size_t);
275 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
277 /* sandbox.c */
278 void sandbox_network_process(void);
279 void sandbox_ui_process(void);
280 void sandbox_fs_process(void);
282 /* telescope.c */
283 void load_about_url(struct tab*, const char*);
284 void load_gemini_url(struct tab*, const char*);
285 void load_url(struct tab*, const char*);
286 int load_previous_page(struct tab*);
287 int load_next_page(struct tab*);
288 void stop_tab(struct tab*);
289 void add_to_bookmarks(const char*);
290 void save_session(void);
292 /* textplain.c */
293 void textplain_initparser(struct parser*);
295 /* tofu.c */
296 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
297 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
298 void tofu_add(struct ohash*, struct tofu_entry*);
299 void tofu_update(struct ohash*, struct tofu_entry*);
301 /* ui.c */
302 unsigned int tab_new_id(void);
303 int ui_init(int, char * const*);
304 void ui_on_tab_loaded(struct tab*);
305 void ui_on_tab_refresh(struct tab*);
306 void ui_require_input(struct tab*, int);
307 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
308 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
309 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
310 void ui_end(void);
312 /* utf.8 */
313 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
314 size_t utf8_encode(uint32_t, char*);
315 char *utf8_nth(char*, size_t);
316 size_t utf8_cplen(char*);
317 size_t utf8_chwidth(uint32_t);
318 size_t utf8_snwidth(const char*, size_t);
319 size_t utf8_swidth(const char*);
320 size_t utf8_swidth_between(const char*, const char*);
321 char *utf8_next_cp(const char*);
322 char *utf8_prev_cp(const char*, const char*);
324 /* util.c */
325 int mark_nonblock(int);
326 int has_prefix(const char*, const char*);
327 int unicode_isspace(uint32_t);
328 int unicode_isgraph(uint32_t);
329 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
331 /* wrap.c */
332 void empty_linelist(struct buffer*);
333 void empty_vlist(struct buffer*);
334 int wrap_text(struct buffer*, const char*, struct line*, size_t);
335 int hardwrap_text(struct buffer*, struct line*, size_t);
337 #endif /* TELESCOPE_H */