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/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 enum imsg_type {
44 /* ui <-> client/fs */
45 IMSG_GET, /* data is URL, peerid the tab id */
46 IMSG_GET_FILE, /* data is path, without \r\n or such */
47 IMSG_GET_RAW, /* get but with an explicit req str */
48 IMSG_ERR,
49 IMSG_CHECK_CERT,
50 IMSG_CERT_STATUS,
51 IMSG_GOT_CODE,
52 IMSG_GOT_META,
53 IMSG_PROCEED,
54 IMSG_STOP,
55 IMSG_BUF,
56 IMSG_EOF,
57 IMSG_QUIT,
59 /* ui <-> fs */
60 IMSG_INIT,
61 IMSG_TOFU,
62 IMSG_BOOKMARK_PAGE,
63 IMSG_BOOKMARK_OK,
64 IMSG_SAVE_CERT,
65 IMSG_SAVE_CERT_OK,
66 IMSG_UPDATE_CERT,
67 IMSG_UPDATE_CERT_OK,
69 IMSG_FILE_OPEN,
70 IMSG_FILE_OPENED,
72 IMSG_SESSION_START,
73 IMSG_SESSION_TAB,
74 IMSG_SESSION_TAB_HIST,
75 IMSG_SESSION_END,
76 };
78 enum line_type {
79 /* text/gemini */
80 LINE_TEXT,
81 LINE_LINK,
82 LINE_TITLE_1,
83 LINE_TITLE_2,
84 LINE_TITLE_3,
85 LINE_ITEM,
86 LINE_QUOTE,
87 LINE_PRE_START,
88 LINE_PRE_CONTENT,
89 LINE_PRE_END,
91 /* text/x-patch */
92 LINE_PATCH,
93 LINE_PATCH_HDR,
94 LINE_PATCH_HUNK_HDR,
95 LINE_PATCH_ADD,
96 LINE_PATCH_DEL,
98 /* minibuffer */
99 LINE_COMPL,
100 LINE_COMPL_CURRENT,
102 /* help */
103 LINE_HELP,
105 /* download */
106 LINE_DOWNLOAD,
107 LINE_DOWNLOAD_DONE,
108 LINE_DOWNLOAD_INFO,
110 /* misc ui */
111 LINE_FRINGE,
112 };
114 /* for lines: mark as hidden */
115 #define L_HIDDEN 1
117 /* for vlines: mark as continuation */
118 #define L_CONTINUATION 2
120 struct line {
121 enum line_type type;
122 char *line;
123 char *alt;
124 void *data;
125 int flags;
126 TAILQ_ENTRY(line) lines;
127 };
129 struct vline {
130 struct line *parent;
131 char *line;
132 int flags;
133 TAILQ_ENTRY(vline) vlines;
134 };
136 struct parser;
138 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
139 typedef int (*parserfreefn)(struct parser*);
141 typedef void (imsg_handlerfn)(struct imsg*, size_t);
143 struct parser {
144 const char *name;
145 char title[128+1];
146 char *buf;
147 size_t len;
148 size_t cap;
150 #define PARSER_IN_BODY 1
151 #define PARSER_IN_PRE 2
152 #define PARSER_IN_PATCH_HDR 4
153 int flags;
154 parsechunkfn parse;
155 parserfreefn free;
157 TAILQ_HEAD(, line) head;
158 };
160 /*
161 * differnt types of trust for a certificate. Following
162 * gemini://thfr.info/gemini/modified-trust-verify.gmi
163 */
164 enum trust_state {
165 TS_UNKNOWN,
166 TS_UNTRUSTED,
167 TS_TEMP_TRUSTED,
168 TS_TRUSTED,
169 TS_VERIFIED,
170 };
172 struct tofu_entry {
173 char domain[GEMINI_URL_LEN];
175 /*
176 * enough space for ``PROTO:HASH''. probably isn't a good
177 * idea tho.
178 */
179 char hash[128+1];
180 int verified;
181 };
183 struct histhead {
184 TAILQ_HEAD(mhisthead, hist) head;
185 size_t len;
186 };
187 struct hist {
188 char h[1025];
189 size_t line_off;
190 size_t current_off;
191 TAILQ_ENTRY(hist) entries;
192 };
194 struct buffer {
195 struct parser page;
197 size_t last_line_off;
198 int force_redraw;
200 int curs_x;
201 int curs_y;
202 size_t line_off;
203 size_t line_max;
204 struct vline *top_line;
205 struct vline *current_line;
206 size_t cpoff;
207 TAILQ_HEAD(vhead, vline) head;
208 };
210 #define TAB_CURRENT 0x1 /* only for save_session */
211 #define TAB_KILLED 0x2 /* only for save_session */
212 #define TAB_URGENT 0x4
213 #define TAB_LAZY 0x8 /* to lazy load tabs */
215 #define NEW_TAB_URL "about:new"
217 TAILQ_HEAD(tabshead, tab);
218 extern struct tabshead tabshead;
219 extern struct tabshead ktabshead;
220 struct tab {
221 TAILQ_ENTRY(tab) tabs;
222 uint32_t id;
223 uint32_t flags;
225 char *cert;
226 enum trust_state trust;
227 struct proxy *proxy;
228 struct phos_uri uri;
229 struct histhead hist;
230 struct hist *hist_cur;
231 size_t hist_off;
233 int code;
234 char meta[GEMINI_URL_LEN];
235 int redirect_count;
237 struct buffer buffer;
239 short loading_anim;
240 short loading_anim_step;
241 struct event loadingev;
242 };
244 extern TAILQ_HEAD(proxylist, proxy) proxies;
245 struct proxy {
246 char *match_proto;
248 char *host;
249 char *port;
250 int proto;
252 TAILQ_ENTRY(proxy) proxies;
253 };
255 enum {
256 PROTO_FINGER,
257 PROTO_GEMINI,
258 PROTO_GOPHER,
259 /* ... */
260 };
262 struct get_req {
263 int proto;
264 char host[254];
265 char port[16];
266 char req[1027];
267 };
269 struct cmd {
270 const char *cmd;
271 void (*fn)(struct buffer *);
272 const char *descr;
273 };
274 extern struct cmd cmds[];
276 /* defaults.c */
277 void config_init(void);
278 int config_setprfx(const char *, const char *, const char *);
279 int config_setvari(const char *, int);
280 int config_setvars(const char *, char *);
281 int config_setcolor(int, const char *, int, int, int);
282 int config_setattr(const char *, int, int, int);
283 void config_apply_style(void);
285 /* downloads.c */
286 extern STAILQ_HEAD(downloads, download) downloads;
287 struct download {
288 uint32_t id;
289 int fd;
290 size_t bytes;
291 char *path;
292 STAILQ_ENTRY(download) entries;
293 };
295 void recompute_downloads(void);
296 void enqueue_download(uint32_t, const char *);
297 struct download *download_by_id(uint32_t);
299 /* help.c */
300 void recompute_help(void);
302 /* hist.c */
303 void hist_clear(struct histhead *);
304 void hist_clear_forward(struct histhead*, struct hist*);
305 void hist_push(struct histhead*, struct hist*);
306 void hist_add_before(struct histhead *, struct hist *, struct hist *);
307 struct hist *hist_pop(struct histhead *);
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 #define LU_MODE_NONE 0x0
328 #define LU_MODE_NOHIST 0x1
329 #define LU_MODE_NOCACHE 0x2
331 void gopher_send_search_req(struct tab *, const char *);
332 void load_url(struct tab *, const char *, const char *, int);
333 void load_url_in_tab(struct tab *, const char *, const char *, int);
334 int load_previous_page(struct tab*);
335 int load_next_page(struct tab*);
336 void add_to_bookmarks(const char*);
337 void humanify_url(const char *, char *, size_t);
338 int ui_send_net(int, uint32_t, const void *, uint16_t);
339 int ui_send_fs(int, uint32_t, const void *, uint16_t);
341 /* tofu.c */
342 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
343 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
344 void tofu_add(struct ohash*, struct tofu_entry*);
345 void tofu_update(struct ohash*, struct tofu_entry*);
346 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
348 /* util.c */
350 /* wrap.c */
351 void erase_buffer(struct buffer *);
352 void empty_linelist(struct buffer*);
353 void empty_vlist(struct buffer*);
354 int wrap_one(struct buffer *, const char *, struct line *, size_t);
355 int wrap_text(struct buffer*, const char*, struct line*, size_t);
356 int hardwrap_text(struct buffer*, struct line*, size_t);
357 int wrap_page(struct buffer *, int width);
359 #endif /* TELESCOPE_H */