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 #define SIDE_WINDOW_LEFT 0x1
32 #define SIDE_WINDOW_BOTTOM 0x2
34 struct imsgev {
35 struct imsgbuf ibuf;
36 void (*handler)(int, short, void *);
37 struct event ev;
38 short events;
39 };
41 enum imsg_type {
42 /* ui <-> client/fs */
43 IMSG_GET, /* data is URL, peerid the tab id */
44 IMSG_GET_FILE, /* data is path, without \r\n or such */
45 IMSG_GET_RAW, /* get but with an explicit req str */
46 IMSG_ERR,
47 IMSG_CHECK_CERT,
48 IMSG_CERT_STATUS,
49 IMSG_GOT_CODE,
50 IMSG_GOT_META,
51 IMSG_PROCEED,
52 IMSG_STOP,
53 IMSG_BUF,
54 IMSG_EOF,
55 IMSG_QUIT,
57 /* ui <-> fs */
58 IMSG_BOOKMARK_PAGE,
59 IMSG_BOOKMARK_OK,
60 IMSG_SAVE_CERT,
61 IMSG_SAVE_CERT_OK,
62 IMSG_UPDATE_CERT,
63 IMSG_UPDATE_CERT_OK,
65 IMSG_FILE_OPEN,
66 IMSG_FILE_OPENED,
68 IMSG_SESSION_START,
69 IMSG_SESSION_TAB,
70 IMSG_SESSION_TAB_TITLE,
71 IMSG_SESSION_END,
72 };
74 enum line_type {
75 /* text/gemini */
76 LINE_TEXT,
77 LINE_LINK,
78 LINE_TITLE_1,
79 LINE_TITLE_2,
80 LINE_TITLE_3,
81 LINE_ITEM,
82 LINE_QUOTE,
83 LINE_PRE_START,
84 LINE_PRE_CONTENT,
85 LINE_PRE_END,
87 /* text/x-patch */
88 LINE_PATCH,
89 LINE_PATCH_HDR,
90 LINE_PATCH_HUNK_HDR,
91 LINE_PATCH_ADD,
92 LINE_PATCH_DEL,
94 /* minibuffer */
95 LINE_COMPL,
96 LINE_COMPL_CURRENT,
98 /* help */
99 LINE_HELP,
101 /* download */
102 LINE_DOWNLOAD,
103 LINE_DOWNLOAD_DONE,
104 LINE_DOWNLOAD_INFO,
105 };
107 /* for lines: mark as hidden */
108 #define L_HIDDEN 1
110 /* for vlines: mark as continuation */
111 #define L_CONTINUATION 2
113 struct line {
114 enum line_type type;
115 char *line;
116 char *alt;
117 void *data;
118 int flags;
119 TAILQ_ENTRY(line) lines;
120 };
122 struct vline {
123 struct line *parent;
124 char *line;
125 int flags;
126 TAILQ_ENTRY(vline) vlines;
127 };
129 struct parser;
130 struct page;
132 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
133 typedef int (*parserfreefn)(struct parser*);
135 typedef void (imsg_handlerfn)(struct imsg*, size_t);
137 struct parser {
138 const char *name;
139 char title[128+1];
140 char *buf;
141 size_t len;
142 size_t cap;
144 #define PARSER_IN_BODY 1
145 #define PARSER_IN_PRE 2
146 #define PARSER_IN_PATCH_HDR 4
147 int flags;
148 parsechunkfn parse;
149 parserfreefn free;
151 TAILQ_HEAD(, line) head;
152 };
154 /*
155 * differnt types of trust for a certificate. Following
156 * gemini://thfr.info/gemini/modified-trust-verify.gmi
157 */
158 enum trust_state {
159 TS_UNKNOWN,
160 TS_UNTRUSTED,
161 TS_TEMP_TRUSTED,
162 TS_TRUSTED,
163 TS_VERIFIED,
164 };
166 struct tofu_entry {
167 char domain[GEMINI_URL_LEN];
169 /*
170 * enough space for ``PROTO:HASH''. probably isn't a good
171 * idea tho.
172 */
173 char hash[128+1];
174 int verified;
175 };
177 struct histhead {
178 TAILQ_HEAD(mhisthead, hist) head;
179 size_t len;
180 };
181 struct hist {
182 char h[1025];
183 TAILQ_ENTRY(hist) entries;
184 };
186 struct buffer {
187 struct parser page;
189 size_t last_line_off;
190 int force_redraw;
192 int curs_x;
193 int curs_y;
194 size_t line_off;
195 size_t line_max;
196 struct vline *top_line;
197 struct vline *current_line;
198 size_t cpoff;
199 TAILQ_HEAD(vhead, vline) head;
200 };
202 #define TAB_CURRENT 0x1 /* only for save_session */
203 #define TAB_URGENT 0x2
204 #define TAB_LAZY 0x4 /* to lazy load tabs */
206 #define NEW_TAB_URL "about:new"
208 extern TAILQ_HEAD(tabshead, tab) tabshead;
209 struct tab {
210 TAILQ_ENTRY(tab) tabs;
211 uint32_t id;
212 uint32_t flags;
214 char *cert;
215 enum trust_state trust;
216 struct proxy *proxy;
217 struct phos_uri uri;
218 struct histhead hist;
219 struct hist *hist_cur;
220 size_t hist_off;
222 int code;
223 char meta[GEMINI_URL_LEN];
224 int redirect_count;
226 struct buffer buffer;
228 short loading_anim;
229 short loading_anim_step;
230 struct event loadingev;
232 int fd;
233 size_t bytes;
234 char *path;
235 };
237 extern TAILQ_HEAD(proxylist, proxy) proxies;
238 struct proxy {
239 char *match_proto;
241 char *host;
242 char *port;
243 int proto;
245 TAILQ_ENTRY(proxy) proxies;
246 };
248 enum {
249 PROTO_FINGER,
250 PROTO_GEMINI,
251 PROTO_GOPHER,
252 /* ... */
253 };
255 struct get_req {
256 int proto;
257 char host[254];
258 char port[16];
259 char req[1027];
260 };
262 struct kmap {
263 TAILQ_HEAD(map, keymap) m;
264 void (*unhandled_input)(void);
265 };
266 extern struct kmap global_map, minibuffer_map;
268 typedef void(interactivefn)(struct buffer *);
270 struct keymap {
271 int meta;
272 int key;
273 struct kmap map;
274 interactivefn *fn;
276 TAILQ_ENTRY(keymap) keymaps;
277 };
279 struct cmd {
280 const char *cmd;
281 void (*fn)(struct buffer *);
282 const char *descr;
283 };
284 extern struct cmd cmds[];
286 /* defaults.c */
287 void config_init(void);
288 int config_setprfx(const char *, const char *, const char *);
289 int config_setvari(const char *, int);
290 int config_setvars(const char *, char *);
291 int config_setcolor(int, const char *, int, int, int);
292 int config_setattr(const char *, int, int, int);
293 void config_apply_style(void);
295 /* downloads.c */
296 extern STAILQ_HEAD(downloads, download) downloads;
297 struct download {
298 int fd;
299 size_t bytes;
300 char *path;
301 STAILQ_ENTRY(download) entries;
302 };
304 void recompute_downloads(void);
306 /* help.c */
307 void recompute_help(void);
309 /* hist.c */
310 void hist_clear_forward(struct histhead*, struct hist*);
311 void hist_push(struct histhead*, struct hist*);
313 /* keymap.c */
314 int kbd(const char*);
315 const char *unkbd(int);
316 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
318 /* mime.c */
319 int setup_parser_for(struct tab*);
321 /* net.c */
322 int net_main(void);
324 /* parse.y */
325 void parseconfig(const char *, int);
327 /* sandbox.c */
328 void sandbox_net_process(void);
329 void sandbox_ui_process(void);
330 void sandbox_fs_process(void);
332 /* telescope.c */
333 extern int operating;
334 extern int safe_mode;
336 void gopher_send_search_req(struct tab *, const char *);
337 void load_url(struct tab *, const char *, const char *, int);
338 void load_url_in_tab(struct tab *, const char *, const char *, int);
339 int load_previous_page(struct tab*);
340 int load_next_page(struct tab*);
341 void add_to_bookmarks(const char*);
342 void humanify_url(const char *, char *, size_t);
343 int ui_send_net(int, uint32_t, const void *, uint16_t);
344 int ui_send_fs(int, uint32_t, const void *, uint16_t);
346 /* tofu.c */
347 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
348 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
349 void tofu_add(struct ohash*, struct tofu_entry*);
350 void tofu_update(struct ohash*, struct tofu_entry*);
351 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
353 /* util.c */
354 int mark_nonblock(int);
355 int has_prefix(const char*, const char*);
356 int has_suffix(const char *, const char *);
357 int unicode_isspace(uint32_t);
358 int unicode_isgraph(uint32_t);
359 int dispatch_imsg(struct imsgev *, short, imsg_handlerfn **, size_t);
360 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t, int, const void *, uint16_t);
362 /* wrap.c */
363 void erase_buffer(struct buffer *);
364 void empty_linelist(struct buffer*);
365 void empty_vlist(struct buffer*);
366 int wrap_one(struct buffer *, const char *, struct line *, size_t);
367 int wrap_text(struct buffer*, const char*, struct line*, size_t);
368 int hardwrap_text(struct buffer*, struct line*, size_t);
369 int wrap_page(struct buffer *, int width);
371 #endif /* TELESCOPE_H */