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,
84 /* text/x-patch */
85 LINE_PATCH,
86 LINE_PATCH_HDR,
87 LINE_PATCH_HUNK_HDR,
88 LINE_PATCH_ADD,
89 LINE_PATCH_DEL,
91 /* minibuffer */
92 LINE_COMPL,
93 LINE_COMPL_CURRENT,
95 /* help */
96 LINE_HELP,
97 };
99 /* for lines: mark as hidden */
100 #define L_HIDDEN 1
102 /* for vlines: mark as continuation */
103 #define L_CONTINUATION 2
105 struct line {
106 enum line_type type;
107 char *line;
108 char *alt;
109 void *data;
110 int flags;
111 TAILQ_ENTRY(line) lines;
112 };
114 struct vline {
115 struct line *parent;
116 char *line;
117 int flags;
118 TAILQ_ENTRY(vline) vlines;
119 };
121 struct parser;
122 struct page;
124 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
125 typedef int (*parserfreefn)(struct parser*);
127 typedef void (imsg_handlerfn)(struct imsg*, size_t);
129 struct parser {
130 const char *name;
131 char title[128+1];
132 char *buf;
133 size_t len;
134 size_t cap;
136 #define PARSER_IN_BODY 1
137 #define PARSER_IN_PRE 2
138 #define PARSER_IN_PATCH_HDR 4
139 int flags;
140 parsechunkfn parse;
141 parserfreefn free;
143 TAILQ_HEAD(, line) head;
144 };
146 /*
147 * differnt types of trust for a certificate. Following
148 * gemini://thfr.info/gemini/modified-trust-verify.gmi
149 */
150 enum trust_state {
151 TS_UNKNOWN,
152 TS_UNTRUSTED,
153 TS_TEMP_TRUSTED,
154 TS_TRUSTED,
155 TS_VERIFIED,
156 };
158 struct tofu_entry {
159 char domain[GEMINI_URL_LEN];
161 /*
162 * enough space for ``PROTO:HASH''. probably isn't a good
163 * idea tho.
164 */
165 char hash[128+1];
166 int verified;
167 };
169 struct histhead {
170 TAILQ_HEAD(mhisthead, hist) head;
171 size_t len;
172 };
173 struct hist {
174 char h[1025];
175 TAILQ_ENTRY(hist) entries;
176 };
178 struct buffer {
179 struct parser page;
181 size_t last_line_off;
182 int force_redraw;
184 int curs_x;
185 int curs_y;
186 size_t line_off;
187 size_t line_max;
188 struct vline *top_line;
189 struct vline *current_line;
190 size_t cpoff;
191 TAILQ_HEAD(vhead, vline) head;
192 };
194 #define TAB_CURRENT 0x1 /* only for save_session */
195 #define TAB_URGENT 0x2
196 #define TAB_LAZY 0x4 /* to lazy load tabs */
198 #define NEW_TAB_URL "about:new"
200 extern TAILQ_HEAD(tabshead, tab) tabshead;
201 struct tab {
202 TAILQ_ENTRY(tab) tabs;
203 uint32_t id;
204 uint32_t flags;
206 char *cert;
207 enum trust_state trust;
208 struct proxy *proxy;
209 struct phos_uri uri;
210 struct histhead hist;
211 struct hist *hist_cur;
212 size_t hist_off;
214 int code;
215 char meta[GEMINI_URL_LEN];
216 int redirect_count;
218 struct buffer buffer;
220 short loading_anim;
221 short loading_anim_step;
222 struct event loadingev;
224 int fd;
225 size_t bytes;
226 char *path;
227 };
229 extern TAILQ_HEAD(proxylist, proxy) proxies;
230 struct proxy {
231 char *match_proto;
233 char *host;
234 char *port;
235 int proto;
237 TAILQ_ENTRY(proxy) proxies;
238 };
240 enum {
241 PROTO_FINGER,
242 PROTO_GEMINI,
243 PROTO_GOPHER,
244 /* ... */
245 };
247 struct get_req {
248 int proto;
249 char host[254];
250 char port[16];
251 char req[1027];
252 };
254 struct kmap {
255 TAILQ_HEAD(map, keymap) m;
256 void (*unhandled_input)(void);
257 };
258 extern struct kmap global_map, minibuffer_map;
260 typedef void(interactivefn)(struct buffer *);
262 struct keymap {
263 int meta;
264 int key;
265 struct kmap map;
266 interactivefn *fn;
268 TAILQ_ENTRY(keymap) keymaps;
269 };
271 struct cmd {
272 const char *cmd;
273 void (*fn)(struct buffer *);
274 const char *descr;
275 };
276 extern struct cmd cmds[];
278 /* defaults.c */
279 void config_init(void);
280 int config_setprfx(const char *, const char *, const char *);
281 int config_setvari(const char *, int);
282 int config_setvars(const char *, char *);
283 int config_setcolor(int, const char *, int, int, int);
284 int config_setattr(const char *, int, int, int);
285 void config_apply_style(void);
287 /* fs.c */
288 extern char session_file[PATH_MAX];
290 int fs_init(void);
291 int fs_main(void);
292 int last_time_crashed(void);
293 int lock_session(void);
294 int load_certs(struct ohash*);
296 /* help.c */
297 void recompute_help(void);
299 /* hist.c */
300 void hist_clear_forward(struct histhead*, struct hist*);
301 void hist_push(struct histhead*, struct hist*);
303 /* keymap.c */
304 int kbd(const char*);
305 const char *unkbd(int);
306 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
308 /* mime.c */
309 int setup_parser_for(struct tab*);
311 /* net.c */
312 int net_main(void);
314 /* parse.y */
315 void parseconfig(const char *, int);
317 /* sandbox.c */
318 void sandbox_net_process(void);
319 void sandbox_ui_process(void);
320 void sandbox_fs_process(void);
322 /* telescope.c */
323 extern int operating;
325 void gopher_send_search_req(struct tab *, const char *);
326 void load_url(struct tab *, const char *, const char *, int);
327 void load_url_in_tab(struct tab *, const char *, const char *, int);
328 int load_previous_page(struct tab*);
329 int load_next_page(struct tab*);
330 void add_to_bookmarks(const char*);
331 void humanify_url(const char *, char *, size_t);
332 int ui_send_net(int, uint32_t, const void *, uint16_t);
333 int ui_send_fs(int, uint32_t, const void *, uint16_t);
335 /* tofu.c */
336 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
337 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
338 void tofu_add(struct ohash*, struct tofu_entry*);
339 void tofu_update(struct ohash*, struct tofu_entry*);
340 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
342 /* util.c */
343 int mark_nonblock(int);
344 int has_prefix(const char*, const char*);
345 int has_suffix(const char *, const char *);
346 int unicode_isspace(uint32_t);
347 int unicode_isgraph(uint32_t);
348 int dispatch_imsg(struct imsgev *, short, imsg_handlerfn **, size_t);
349 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t, int, const void *, uint16_t);
351 /* wrap.c */
352 void erase_buffer(struct buffer *);
353 void empty_linelist(struct buffer*);
354 void empty_vlist(struct buffer*);
355 int wrap_one(struct buffer *, const char *, struct line *, size_t);
356 int wrap_text(struct buffer*, const char*, struct line*, size_t);
357 int hardwrap_text(struct buffer*, struct line*, size_t);
358 int wrap_page(struct buffer *, int width);
360 #endif /* TELESCOPE_H */