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 <limits.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_FILE, /* data is path, without \r\n or such */
42 IMSG_GET_RAW, /* get but with an explicit req str */
43 IMSG_ERR,
44 IMSG_CHECK_CERT,
45 IMSG_CERT_STATUS,
46 IMSG_GOT_CODE,
47 IMSG_GOT_META,
48 IMSG_PROCEED,
49 IMSG_STOP,
50 IMSG_BUF,
51 IMSG_EOF,
52 IMSG_QUIT,
54 /* ui <-> fs */
55 IMSG_BOOKMARK_PAGE,
56 IMSG_BOOKMARK_OK,
57 IMSG_SAVE_CERT,
58 IMSG_SAVE_CERT_OK,
59 IMSG_UPDATE_CERT,
60 IMSG_UPDATE_CERT_OK,
62 IMSG_FILE_OPEN,
63 IMSG_FILE_OPENED,
65 IMSG_SESSION_START,
66 IMSG_SESSION_TAB,
67 IMSG_SESSION_TAB_TITLE,
68 IMSG_SESSION_END,
69 };
71 enum line_type {
72 /* text/gemini */
73 LINE_TEXT,
74 LINE_LINK,
75 LINE_TITLE_1,
76 LINE_TITLE_2,
77 LINE_TITLE_3,
78 LINE_ITEM,
79 LINE_QUOTE,
80 LINE_PRE_START,
81 LINE_PRE_CONTENT,
82 LINE_PRE_END,
83 LINE_BREAK,
85 /* text/x-patch */
86 LINE_PATCH,
87 LINE_PATCH_HDR,
88 LINE_PATCH_HUNK_HDR,
89 LINE_PATCH_ADD,
90 LINE_PATCH_DEL,
92 /* minibuffer */
93 LINE_COMPL,
94 LINE_COMPL_CURRENT,
96 /* help */
97 LINE_HELP,
98 };
100 /* for lines: mark as hidden */
101 #define L_HIDDEN 1
103 /* for vlines: mark as continuation */
104 #define L_CONTINUATION 2
106 struct line {
107 enum line_type type;
108 char *line;
109 char *alt;
110 void *data;
111 int flags;
112 TAILQ_ENTRY(line) lines;
113 };
115 struct vline {
116 struct line *parent;
117 char *line;
118 int flags;
119 TAILQ_ENTRY(vline) vlines;
120 };
122 struct parser;
123 struct page;
125 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
126 typedef int (*parserfreefn)(struct parser*);
128 typedef void (imsg_handlerfn)(struct imsg*, size_t);
130 struct parser {
131 const char *name;
132 char title[128+1];
133 char *buf;
134 size_t len;
135 size_t cap;
137 #define PARSER_IN_BODY 1
138 #define PARSER_IN_PRE 2
139 #define PARSER_IN_PATCH_HDR 4
140 int flags;
141 parsechunkfn parse;
142 parserfreefn free;
144 TAILQ_HEAD(, line) head;
145 };
147 /*
148 * differnt types of trust for a certificate. Following
149 * gemini://thfr.info/gemini/modified-trust-verify.gmi
150 */
151 enum trust_state {
152 TS_UNKNOWN,
153 TS_UNTRUSTED,
154 TS_TEMP_TRUSTED,
155 TS_TRUSTED,
156 TS_VERIFIED,
157 };
159 struct tofu_entry {
160 char domain[GEMINI_URL_LEN];
162 /*
163 * enough space for ``PROTO:HASH''. probably isn't a good
164 * idea tho.
165 */
166 char hash[128+1];
167 int verified;
168 };
170 struct histhead {
171 TAILQ_HEAD(mhisthead, hist) head;
172 size_t len;
173 };
174 struct hist {
175 char h[1025];
176 TAILQ_ENTRY(hist) entries;
177 };
179 struct buffer {
180 struct parser page;
182 size_t last_line_off;
183 int force_redraw;
185 int curs_x;
186 int curs_y;
187 size_t line_off;
188 size_t line_max;
189 struct vline *top_line;
190 struct vline *current_line;
191 size_t cpoff;
192 TAILQ_HEAD(vhead, vline) head;
193 };
195 #define TAB_CURRENT 0x1 /* only for save_session */
196 #define TAB_URGENT 0x2
197 #define TAB_LAZY 0x4 /* to lazy load tabs */
199 #define NEW_TAB_URL "about:new"
201 extern TAILQ_HEAD(tabshead, tab) tabshead;
202 struct tab {
203 TAILQ_ENTRY(tab) tabs;
204 uint32_t id;
205 uint32_t flags;
207 char *cert;
208 enum trust_state trust;
209 struct proxy *proxy;
210 struct phos_uri uri;
211 struct histhead hist;
212 struct hist *hist_cur;
213 size_t hist_off;
215 int code;
216 char meta[GEMINI_URL_LEN];
217 int redirect_count;
219 struct buffer buffer;
221 short loading_anim;
222 short loading_anim_step;
223 struct event loadingev;
225 int fd;
226 size_t bytes;
227 char *path;
228 };
230 extern TAILQ_HEAD(proxylist, proxy) proxies;
231 struct proxy {
232 char *match_proto;
234 char *host;
235 char *port;
236 int proto;
238 TAILQ_ENTRY(proxy) proxies;
239 };
241 enum {
242 PROTO_FINGER,
243 PROTO_GEMINI,
244 PROTO_GOPHER,
245 /* ... */
246 };
248 struct get_req {
249 int proto;
250 char host[254];
251 char port[16];
252 char req[1027];
253 };
255 struct kmap {
256 TAILQ_HEAD(map, keymap) m;
257 void (*unhandled_input)(void);
258 };
259 extern struct kmap global_map, minibuffer_map;
261 typedef void(interactivefn)(struct buffer *);
263 struct keymap {
264 int meta;
265 int key;
266 struct kmap map;
267 interactivefn *fn;
269 TAILQ_ENTRY(keymap) keymaps;
270 };
272 struct cmd {
273 const char *cmd;
274 void (*fn)(struct buffer *);
275 const char *descr;
276 };
277 extern struct cmd cmds[];
279 /* defaults.c */
280 void config_init(void);
281 int config_setprfx(const char *, const char *, const char *);
282 int config_setvari(const char *, int);
283 int config_setvars(const char *, char *);
284 int config_setcolor(int, const char *, int, int, int);
285 int config_setattr(const char *, int, int, int);
286 void config_apply_style(void);
288 /* fs.c */
289 extern char session_file[PATH_MAX];
291 int fs_init(void);
292 int fs_main(void);
293 int last_time_crashed(void);
294 int lock_session(void);
295 int load_certs(struct ohash*);
297 /* help.c */
298 void recompute_help(void);
300 /* hist.c */
301 void hist_clear_forward(struct histhead*, struct hist*);
302 void hist_push(struct histhead*, struct hist*);
304 /* keymap.c */
305 int kbd(const char*);
306 const char *unkbd(int);
307 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
309 /* mime.c */
310 int setup_parser_for(struct tab*);
312 /* net.c */
313 int net_main(void);
315 /* parse.y */
316 void parseconfig(const char *, int);
318 /* sandbox.c */
319 void sandbox_net_process(void);
320 void sandbox_ui_process(void);
321 void sandbox_fs_process(void);
323 /* telescope.c */
324 extern int operating;
325 extern int safe_mode;
327 void gopher_send_search_req(struct tab *, const char *);
328 void load_url(struct tab *, const char *, const char *, int);
329 void load_url_in_tab(struct tab *, const char *, const char *, int);
330 int load_previous_page(struct tab*);
331 int load_next_page(struct tab*);
332 void add_to_bookmarks(const char*);
333 void humanify_url(const char *, char *, size_t);
334 int ui_send_net(int, uint32_t, const void *, uint16_t);
335 int ui_send_fs(int, uint32_t, const void *, uint16_t);
337 /* tofu.c */
338 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
339 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
340 void tofu_add(struct ohash*, struct tofu_entry*);
341 void tofu_update(struct ohash*, struct tofu_entry*);
342 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
344 /* util.c */
345 int mark_nonblock(int);
346 int has_prefix(const char*, const char*);
347 int has_suffix(const char *, const char *);
348 int unicode_isspace(uint32_t);
349 int unicode_isgraph(uint32_t);
350 int dispatch_imsg(struct imsgev *, short, imsg_handlerfn **, size_t);
351 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t, int, const void *, uint16_t);
353 /* wrap.c */
354 void erase_buffer(struct buffer *);
355 void empty_linelist(struct buffer*);
356 void empty_vlist(struct buffer*);
357 int wrap_one(struct buffer *, const char *, struct line *, size_t);
358 int wrap_text(struct buffer*, const char*, struct line*, size_t);
359 int hardwrap_text(struct buffer*, struct line*, size_t);
360 int wrap_page(struct buffer *, int width);
362 #endif /* TELESCOPE_H */