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 "compat.h"
22 #include <limits.h>
24 #include "cmd.h"
25 #include "phos.h"
27 #define MIN(a, b) ((a) < (b) ? (a) : (b))
28 #define MAX(a, b) ((a) > (b) ? (a) : (b))
30 #define GEMINI_URL_LEN 1024
31 #define TITLE_MAX 128+1 /* account for NUL too */
33 #define SIDE_WINDOW_LEFT 0x1
34 #define SIDE_WINDOW_BOTTOM 0x2
36 struct imsgev {
37 struct imsgbuf ibuf;
38 void (*handler)(int, short, void *);
39 struct event ev;
40 short events;
41 };
43 #define IMSG_DATA_SIZE(imsg) ((imsg).hdr.len - IMSG_HEADER_SIZE)
45 enum imsg_type {
46 /* ui <-> client/fs */
47 IMSG_GET, /* data is URL, peerid the tab id */
48 IMSG_GET_FILE, /* data is path, without \r\n or such */
49 IMSG_GET_RAW, /* get but with an explicit req str */
50 IMSG_ERR,
51 IMSG_CHECK_CERT,
52 IMSG_CERT_STATUS,
53 IMSG_GOT_CODE,
54 IMSG_GOT_META,
55 IMSG_PROCEED,
56 IMSG_STOP,
57 IMSG_BUF,
58 IMSG_EOF,
59 IMSG_QUIT,
61 /* ui <-> fs */
62 IMSG_INIT,
63 IMSG_TOFU,
64 IMSG_BOOKMARK_PAGE,
65 IMSG_BOOKMARK_OK,
66 IMSG_SAVE_CERT,
67 IMSG_SAVE_CERT_OK,
68 IMSG_UPDATE_CERT,
69 IMSG_UPDATE_CERT_OK,
71 IMSG_FILE_OPEN,
72 IMSG_FILE_OPENED,
74 IMSG_SESSION_START,
75 IMSG_SESSION_TAB,
76 IMSG_SESSION_TAB_HIST,
77 IMSG_SESSION_END,
78 };
80 enum line_type {
81 /* text/gemini */
82 LINE_TEXT,
83 LINE_LINK,
84 LINE_TITLE_1,
85 LINE_TITLE_2,
86 LINE_TITLE_3,
87 LINE_ITEM,
88 LINE_QUOTE,
89 LINE_PRE_START,
90 LINE_PRE_CONTENT,
91 LINE_PRE_END,
93 /* text/x-patch */
94 LINE_PATCH,
95 LINE_PATCH_HDR,
96 LINE_PATCH_HUNK_HDR,
97 LINE_PATCH_ADD,
98 LINE_PATCH_DEL,
100 /* minibuffer */
101 LINE_COMPL,
102 LINE_COMPL_CURRENT,
104 /* help */
105 LINE_HELP,
107 /* download */
108 LINE_DOWNLOAD,
109 LINE_DOWNLOAD_DONE,
110 LINE_DOWNLOAD_INFO,
112 /* misc ui */
113 LINE_FRINGE,
114 };
116 /* for lines: mark as hidden */
117 #define L_HIDDEN 1
119 /* for vlines: mark as continuation */
120 #define L_CONTINUATION 2
122 struct line {
123 enum line_type type;
124 char *line;
125 char *alt;
126 void *data;
127 int flags;
128 TAILQ_ENTRY(line) lines;
129 };
131 struct vline {
132 struct line *parent;
133 char *line;
134 int flags;
135 TAILQ_ENTRY(vline) vlines;
136 };
138 struct parser;
140 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
141 typedef int (*parserfreefn)(struct parser*);
142 typedef int (*parserserial)(struct parser*, struct evbuffer *);
144 typedef void (imsg_handlerfn)(struct imsg*, size_t);
146 struct parser {
147 const char *name;
148 char title[128+1];
149 char *buf;
150 size_t len;
151 size_t cap;
153 #define PARSER_IN_BODY 1
154 #define PARSER_IN_PRE 2
155 #define PARSER_IN_PATCH_HDR 4
156 int flags;
157 void (*init)(struct parser *);
158 parsechunkfn parse;
159 parserfreefn free;
160 parserserial serialize;
162 TAILQ_HEAD(, line) head;
163 };
165 /*
166 * differnt types of trust for a certificate. Following
167 * gemini://thfr.info/gemini/modified-trust-verify.gmi
168 */
169 enum trust_state {
170 TS_UNKNOWN,
171 TS_UNTRUSTED,
172 TS_TEMP_TRUSTED,
173 TS_TRUSTED,
174 TS_VERIFIED,
175 };
177 struct tofu_entry {
178 char domain[GEMINI_URL_LEN];
180 /*
181 * enough space for ``PROTO:HASH''. probably isn't a good
182 * idea tho.
183 */
184 char hash[128+1];
185 int verified;
186 };
188 struct histhead {
189 TAILQ_HEAD(mhisthead, hist) head;
190 size_t len;
191 };
192 struct hist {
193 char h[1025];
194 size_t line_off;
195 size_t current_off;
196 TAILQ_ENTRY(hist) entries;
197 };
199 struct buffer {
200 struct parser page;
202 size_t last_line_off;
203 int force_redraw;
205 int curs_x;
206 int curs_y;
207 size_t line_off;
208 size_t line_max;
209 struct vline *top_line;
210 struct vline *current_line;
211 size_t cpoff;
212 TAILQ_HEAD(vhead, vline) head;
213 };
215 #define TAB_CURRENT 0x1 /* only for save_session */
216 #define TAB_KILLED 0x2 /* only for save_session */
217 #define TAB_URGENT 0x4
218 #define TAB_LAZY 0x8 /* to lazy load tabs */
220 #define NEW_TAB_URL "about:new"
222 TAILQ_HEAD(tabshead, tab);
223 extern struct tabshead tabshead;
224 extern struct tabshead ktabshead;
225 struct tab {
226 TAILQ_ENTRY(tab) tabs;
227 uint32_t id;
228 uint32_t flags;
230 char *cert;
231 enum trust_state trust;
232 struct proxy *proxy;
233 struct phos_uri uri;
234 struct histhead hist;
235 struct hist *hist_cur;
236 size_t hist_off;
237 char *last_input_url;
239 int code;
240 char meta[GEMINI_URL_LEN];
241 int redirect_count;
243 struct buffer buffer;
245 short loading_anim;
246 short loading_anim_step;
247 struct event loadingev;
248 };
250 extern TAILQ_HEAD(proxylist, proxy) proxies;
251 struct proxy {
252 char *match_proto;
254 char *host;
255 char *port;
256 int proto;
258 TAILQ_ENTRY(proxy) proxies;
259 };
261 enum {
262 PROTO_FINGER,
263 PROTO_GEMINI,
264 PROTO_GOPHER,
265 /* ... */
266 };
268 struct get_req {
269 int proto;
270 char host[254];
271 char port[16];
272 char req[1027];
273 };
275 struct cmd {
276 const char *cmd;
277 void (*fn)(struct buffer *);
278 const char *descr;
279 };
280 extern struct cmd cmds[];
282 /* defaults.c */
283 void config_init(void);
284 int config_setprfx(const char *, const char *, const char *);
285 int config_setvari(const char *, int);
286 int config_setvars(const char *, char *);
287 int config_setcolor(int, const char *, int, int, int);
288 int config_setattr(const char *, int, int, int);
289 void config_apply_style(void);
291 /* downloads.c */
292 extern STAILQ_HEAD(downloads, download) downloads;
293 struct download {
294 uint32_t id;
295 int fd;
296 size_t bytes;
297 char *path;
298 STAILQ_ENTRY(download) entries;
299 };
301 void recompute_downloads(void);
302 void enqueue_download(uint32_t, const char *);
303 struct download *download_by_id(uint32_t);
305 /* help.c */
306 void recompute_help(void);
308 /* hist.c */
309 void hist_clear(struct histhead *);
310 void hist_clear_forward(struct histhead*, struct hist*);
311 void hist_push(struct histhead*, struct hist*);
312 void hist_add_before(struct histhead *, struct hist *, struct hist *);
313 struct hist *hist_pop(struct histhead *);
315 /* mime.c */
316 int setup_parser_for(struct tab*);
318 /* net.c */
319 int net_main(void);
321 /* parse.y */
322 void parseconfig(const char *, int);
324 /* sandbox.c */
325 void sandbox_net_process(void);
326 void sandbox_ui_process(void);
327 void sandbox_fs_process(void);
329 /* telescope.c */
330 extern int operating;
331 extern int safe_mode;
333 #define LU_MODE_NONE 0x0
334 #define LU_MODE_NOHIST 0x1
335 #define LU_MODE_NOCACHE 0x2
337 void gopher_send_search_req(struct tab *, const char *);
338 int load_page_from_str(struct tab *, const char *);
339 void load_url(struct tab *, const char *, const char *, int);
340 void load_url_in_tab(struct tab *, const char *, const char *, int);
341 int load_previous_page(struct tab*);
342 int load_next_page(struct tab*);
343 void add_to_bookmarks(const char*);
344 void humanify_url(const char *, char *, size_t);
345 int ui_send_net(int, uint32_t, const void *, uint16_t);
346 int ui_send_fs(int, uint32_t, const void *, uint16_t);
348 /* tofu.c */
349 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
350 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
351 void tofu_add(struct ohash*, struct tofu_entry*);
352 void tofu_update(struct ohash*, struct tofu_entry*);
353 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
355 /* wrap.c */
356 void erase_buffer(struct buffer *);
357 void empty_linelist(struct buffer*);
358 void empty_vlist(struct buffer*);
359 int wrap_one(struct buffer *, const char *, struct line *, size_t);
360 int wrap_text(struct buffer*, const char*, struct line*, size_t);
361 int hardwrap_text(struct buffer*, struct line*, size_t);
362 int wrap_page(struct buffer *, int width);
364 #endif /* TELESCOPE_H */