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 struct imsgev {
32 struct imsgbuf ibuf;
33 void (*handler)(int, short, void *);
34 struct event ev;
35 short events;
36 };
38 enum imsg_type {
39 /* ui <-> client/fs */
40 IMSG_GET, /* data is URL, peerid the tab id */
41 IMSG_GET_RAW, /* get but with an explicit req str */
42 IMSG_ERR,
43 IMSG_CHECK_CERT,
44 IMSG_CERT_STATUS,
45 IMSG_GOT_CODE,
46 IMSG_GOT_META,
47 IMSG_PROCEED,
48 IMSG_STOP,
49 IMSG_BUF,
50 IMSG_EOF,
51 IMSG_QUIT,
53 /* ui <-> fs */
54 IMSG_BOOKMARK_PAGE,
55 IMSG_BOOKMARK_OK,
56 IMSG_SAVE_CERT,
57 IMSG_SAVE_CERT_OK,
58 IMSG_UPDATE_CERT,
59 IMSG_UPDATE_CERT_OK,
61 IMSG_FILE_OPEN,
62 IMSG_FILE_OPENED,
64 IMSG_SESSION_START,
65 IMSG_SESSION_TAB,
66 IMSG_SESSION_END,
67 };
69 extern char *new_tab_url;
70 extern int fill_column;
71 extern int olivetti_mode;
72 extern int enable_colors;
73 extern int hide_pre_context;
74 extern int hide_pre_blocks;
76 struct lineprefix {
77 const char *prfx1;
78 const char *prfx2;
79 };
80 extern struct lineprefix line_prefixes[];
82 struct line_face {
83 int prfx_pair, pair, trail_pair;
84 int prfx_bg, bg, trail_bg;
85 int prfx_fg, fg, trail_fg;
86 int prfx_attr, attr, trail_attr;
88 int prefix, text, trail;
89 };
90 extern struct line_face line_faces[];
92 struct tab_face {
93 int bg_attr, bg_bg, bg_fg;
94 int t_attr, t_bg, t_fg;
95 int c_attr, c_bg, c_fg;
97 int background, tab, current;
98 };
99 extern struct tab_face tab_face;
101 struct body_face {
102 int lbg, lfg;
103 int bg, fg;
104 int rbg, rfg;
106 int left, body, right;
107 };
108 extern struct body_face body_face;
110 struct modeline_face {
111 int bg, fg, attr;
112 int background;
113 };
114 extern struct modeline_face modeline_face;
116 struct minibuffer_face {
117 int bg, fg, attr;
118 int background;
119 };
120 extern struct minibuffer_face minibuffer_face;
122 enum line_type {
123 LINE_TEXT,
124 LINE_LINK,
125 LINE_TITLE_1,
126 LINE_TITLE_2,
127 LINE_TITLE_3,
128 LINE_ITEM,
129 LINE_QUOTE,
130 LINE_PRE_START,
131 LINE_PRE_CONTENT,
132 LINE_PRE_END,
133 };
135 /* for lines: mark as hidden */
136 #define L_HIDDEN 1
138 /* for vlines: mark as continuation */
139 #define L_CONTINUATION 2
141 struct line {
142 enum line_type type;
143 char *line;
144 char *alt;
145 int flags;
146 TAILQ_ENTRY(line) lines;
147 };
149 struct vline {
150 const struct line *parent;
151 char *line;
152 int flags;
153 TAILQ_ENTRY(vline) vlines;
154 };
156 struct parser;
157 struct page;
159 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
160 typedef int (*parserfreefn)(struct parser*);
162 typedef void (imsg_handlerfn)(struct imsg*, size_t);
164 struct parser {
165 const char *name;
166 char title[32+1];
167 char *buf;
168 size_t len;
169 size_t cap;
171 #define PARSER_IN_BODY 1
172 #define PARSER_IN_PRE 2
173 int flags;
174 parsechunkfn parse;
175 parserfreefn free;
177 TAILQ_HEAD(, line) head;
178 };
180 /*
181 * differnt types of trust for a certificate. Following
182 * gemini://thfr.info/gemini/modified-trust-verify.gmi
183 */
184 enum trust_state {
185 TS_UNKNOWN,
186 TS_UNTRUSTED,
187 TS_TEMP_TRUSTED,
188 TS_TRUSTED,
189 TS_VERIFIED,
190 };
192 struct tofu_entry {
193 char domain[GEMINI_URL_LEN];
195 /*
196 * enough space for ``PROTO:HASH''. probably isn't a good
197 * idea tho.
198 */
199 char hash[128+1];
200 int verified;
201 };
203 struct histhead {
204 TAILQ_HEAD(mhisthead, hist) head;
205 size_t len;
206 };
207 struct hist {
208 char h[1025];
209 TAILQ_ENTRY(hist) entries;
210 };
212 struct buffer {
213 struct parser page;
215 size_t last_line_off;
216 int force_redraw;
218 int curs_x;
219 int curs_y;
220 size_t line_off;
221 size_t line_max;
222 struct vline *top_line;
223 struct vline *current_line;
224 size_t cpoff;
225 TAILQ_HEAD(vhead, vline) head;
226 };
228 #define TAB_CURRENT 0x1
229 #define TAB_URGENT 0x2
231 #define NEW_TAB_URL "about:new"
233 extern TAILQ_HEAD(tabshead, tab) tabshead;
234 struct tab {
235 TAILQ_ENTRY(tab) tabs;
236 uint32_t id;
237 uint32_t flags;
239 char *cert;
240 enum trust_state trust;
241 struct proxy *proxy;
242 struct phos_uri uri;
243 struct histhead hist;
244 struct hist *hist_cur;
245 size_t hist_off;
247 int code;
248 char meta[GEMINI_URL_LEN];
249 int redirect_count;
251 struct buffer buffer;
253 short loading_anim;
254 short loading_anim_step;
255 struct event loadingev;
257 int fd;
258 size_t bytes;
259 char *path;
260 };
262 struct proto {
263 const char *schema;
265 /*
266 * should load the given url in the tab. Optionally, it may
267 * consider the given url as relative to the one already
268 * present in tab. It must set tab->urlstr to a serialized
269 * human-friendly URL.
270 */
271 void (*loadfn)(struct tab*, const char*);
272 };
274 extern TAILQ_HEAD(proxylist, proxy) proxies;
275 struct proxy {
276 char *match_proto;
278 char *host;
279 char *port;
280 int proto;
282 TAILQ_ENTRY(proxy) proxies;
283 };
285 enum {
286 PROTO_GEMINI,
287 /* ... */
288 };
290 struct get_req {
291 int proto;
292 char host[254];
293 char port[16];
294 char req[1027];
295 };
297 struct kmap {
298 TAILQ_HEAD(map, keymap) m;
299 void (*unhandled_input)(void);
300 };
301 extern struct kmap global_map, minibuffer_map;
303 typedef void(interactivefn)(struct buffer *);
305 struct keymap {
306 int meta;
307 int key;
308 struct kmap map;
309 interactivefn *fn;
311 TAILQ_ENTRY(keymap) keymaps;
312 };
314 struct cmd {
315 const char *cmd;
316 void (*fn)(struct buffer *);
317 };
318 extern struct cmd cmds[];
320 /* defaults.c */
321 void config_init(void);
322 int config_setprfx(const char *, const char *, const char *);
323 int config_setvari(const char *, int);
324 int config_setvars(const char *, char *);
325 int config_setcolor(int, const char *, int, int, int);
326 int config_setattr(const char *, int, int, int);
327 void config_apply_style(void);
329 /* fs.c */
330 int fs_init(void);
331 int fs_main(void);
332 int load_certs(struct ohash*);
333 int load_last_session(void(*)(const char*));
335 /* gemini.c */
336 int client_main(void);
338 /* hist.c */
339 void hist_clear_forward(struct histhead*, struct hist*);
340 void hist_push(struct histhead*, struct hist*);
342 /* keymap.c */
343 int kbd(const char*);
344 const char *unkbd(int);
345 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
347 /* mime.c */
348 int setup_parser_for(struct tab*);
350 /* pages.c */
351 extern const char *about_about;
352 extern const char *about_blank;
353 extern const char *about_help;
354 extern const char *about_new;
356 #define CANNOT_FETCH 0
357 #define TOO_MUCH_REDIRECTS 1
358 #define MALFORMED_RESPONSE 2
359 #define UNKNOWN_TYPE_OR_CSET 3
360 extern const char *err_pages[70];
362 /* parse.y */
363 void parseconfig(const char *, int);
365 /* sandbox.c */
366 void sandbox_net_process(void);
367 void sandbox_ui_process(void);
368 void sandbox_fs_process(void);
370 /* telescope.c */
371 void load_about_url(struct tab*, const char*);
372 void load_gemini_url(struct tab*, const char*);
373 void load_via_proxy(struct tab *, const char *, struct proxy *);
374 void load_url(struct tab*, const char*);
375 int load_previous_page(struct tab*);
376 int load_next_page(struct tab*);
377 void stop_tab(struct tab*);
378 void add_to_bookmarks(const char*);
379 void save_session(void);
381 /* tofu.c */
382 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
383 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
384 void tofu_add(struct ohash*, struct tofu_entry*);
385 void tofu_update(struct ohash*, struct tofu_entry*);
386 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
388 /* util.c */
389 int mark_nonblock(int);
390 int has_prefix(const char*, const char*);
391 int unicode_isspace(uint32_t);
392 int unicode_isgraph(uint32_t);
393 void dispatch_imsg(struct imsgev*, short, imsg_handlerfn**, size_t);
394 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t, int, const void *, uint16_t);
396 /* wrap.c */
397 void erase_buffer(struct buffer *);
398 void empty_linelist(struct buffer*);
399 void empty_vlist(struct buffer*);
400 int wrap_text(struct buffer*, const char*, struct line*, size_t);
401 int hardwrap_text(struct buffer*, struct line*, size_t);
403 #endif /* TELESCOPE_H */