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 <event.h>
23 #include <phos/phos.h>
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #define MAX(a, b) ((a) > (b) ? (a) : (b))
28 #define GEMINI_URL_LEN 1024
30 enum imsg_type {
31 /* ui <-> client/fs */
32 IMSG_GET, /* data is URL, peerid the tab id */
33 IMSG_ERR,
34 IMSG_CHECK_CERT,
35 IMSG_CERT_STATUS,
36 IMSG_GOT_CODE,
37 IMSG_GOT_META,
38 IMSG_PROCEED,
39 IMSG_STOP,
40 IMSG_BUF,
41 IMSG_EOF,
42 IMSG_QUIT,
44 /* ui <-> fs */
45 IMSG_BOOKMARK_PAGE,
46 IMSG_BOOKMARK_OK,
47 IMSG_SAVE_CERT,
48 IMSG_SAVE_CERT_OK,
49 IMSG_UPDATE_CERT,
50 IMSG_UPDATE_CERT_OK,
52 IMSG_SESSION_START,
53 IMSG_SESSION_TAB,
54 IMSG_SESSION_END,
55 };
57 enum line_type {
58 LINE_TEXT,
59 LINE_LINK,
60 LINE_TITLE_1,
61 LINE_TITLE_2,
62 LINE_TITLE_3,
63 LINE_ITEM,
64 LINE_QUOTE,
65 LINE_PRE_START,
66 LINE_PRE_CONTENT,
67 LINE_PRE_END,
68 };
70 struct line {
71 enum line_type type;
72 char *line;
73 char *alt;
74 int flags;
75 TAILQ_ENTRY(line) lines;
76 };
78 struct vline {
79 const struct line *parent;
80 char *line;
81 int flags;
82 TAILQ_ENTRY(vline) vlines;
83 };
85 struct parser;
86 struct page;
88 /* typedef void (*initparserfn)(struct parser*); */
90 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
91 typedef int (*parserfreefn)(struct parser*);
93 typedef void (imsg_handlerfn)(struct imsg*, size_t);
95 struct parser {
96 const char *name;
97 char title[32+1];
98 char *buf;
99 size_t len;
100 size_t cap;
101 int flags;
102 parsechunkfn parse;
103 parserfreefn free;
105 TAILQ_HEAD(, line) head;
106 };
108 /*
109 * differnt types of trust for a certificate. Following
110 * gemini://thfr.info/gemini/modified-trust-verify.gmi
111 */
112 enum trust_state {
113 TS_UNKNOWN,
114 TS_UNTRUSTED,
115 TS_TRUSTED,
116 TS_VERIFIED,
117 };
119 struct tofu_entry {
120 char domain[GEMINI_URL_LEN];
121 /* enough space for ``PROTO:HASH''. probably isn't a good
122 * idea tho. */
123 char hash[128+1];
124 int verified;
125 };
127 struct histhead {
128 TAILQ_HEAD(mhisthead, hist) head;
129 size_t len;
130 };
131 struct hist {
132 char h[1025];
133 TAILQ_ENTRY(hist) entries;
134 };
136 struct window {
137 struct parser page;
138 int curs_x;
139 int curs_y;
140 size_t line_off;
141 size_t line_max;
142 struct vline *current_line;
143 size_t cpoff;
144 TAILQ_HEAD(vhead, vline) head;
145 };
147 extern TAILQ_HEAD(tabshead, tab) tabshead;
148 struct tab {
149 TAILQ_ENTRY(tab) tabs;
150 uint32_t id;
151 uint32_t flags;
153 char *cert;
154 enum trust_state trust;
155 struct phos_uri uri;
156 struct histhead hist;
157 struct hist *hist_cur;
158 size_t hist_off;
160 int code;
161 char meta[GEMINI_URL_LEN];
162 int redirect_count;
164 struct window window;
166 short loading_anim;
167 short loading_anim_step;
168 struct event loadingev;
169 };
171 struct proto {
172 const char *schema;
174 /* should load the given url in the tab. Optionally, it may
175 * consider the given url as relative to the one already
176 * present in tab. It must set tab->urlstr to a serialized
177 * human-friendly URL. */
178 void (*loadfn)(struct tab*, const char*);
179 };
181 struct kmap {
182 TAILQ_HEAD(map, keymap) m;
183 void (*unhandled_input)(void);
184 };
186 struct keymap {
187 int meta;
188 int key;
189 struct kmap map;
190 void (*fn)(struct window*);
192 TAILQ_ENTRY(keymap) keymaps;
193 };
195 /* fs.c */
196 int fs_init(void);
197 int fs_main(struct imsgbuf*);
198 int load_certs(struct ohash*);
199 int load_last_session(void(*)(const char*));
201 /* gemini.c */
202 int client_main(struct imsgbuf*);
204 /* gemtext.c */
205 void gemtext_initparser(struct parser*);
207 /* hist.c */
208 void hist_clear_forward(struct histhead*, struct hist*);
209 void hist_push(struct histhead*, struct hist*);
211 /* keymap.c */
212 int kbd(const char*);
213 const char *unkbd(int);
214 int kmap_define_key(struct kmap*, const char*, void(*)(struct window*));
216 /* mime.c */
217 int setup_parser_for(struct tab*);
219 /* pages.c */
220 extern const char *about_new;
222 #define CANNOT_FETCH 0
223 #define TOO_MUCH_REDIRECTS 1
224 #define MALFORMED_RESPONSE 2
225 #define UNKNOWN_TYPE_OR_CSET 3
226 extern const char *err_pages[70];
228 /* parser.c */
229 int parser_append(struct parser*, const char*, size_t);
230 int parser_set_buf(struct parser*, const char*, size_t);
231 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
233 /* sandbox.c */
234 void sandbox_network_process(void);
235 void sandbox_ui_process(void);
236 void sandbox_fs_process(void);
238 /* telescope.c */
239 void load_about_url(struct tab*, const char*);
240 void load_gemini_url(struct tab*, const char*);
241 void load_url(struct tab*, const char*);
242 int load_previous_page(struct tab*);
243 int load_next_page(struct tab*);
244 void stop_tab(struct tab*);
245 void add_to_bookmarks(const char*);
246 void save_session(void);
248 /* textplain.c */
249 void textplain_initparser(struct parser*);
251 /* tofu.c */
252 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
253 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
254 void tofu_add(struct ohash*, struct tofu_entry*);
255 void tofu_update(struct ohash*, struct tofu_entry*);
257 /* ui.c */
258 unsigned int tab_new_id(void);
259 int ui_init(int, char * const*);
260 void ui_on_tab_loaded(struct tab*);
261 void ui_on_tab_refresh(struct tab*);
262 void ui_require_input(struct tab*, int);
263 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
264 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
265 void ui_end(void);
267 /* utf.8 */
268 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
269 size_t utf8_encode(uint32_t, char*);
270 char *utf8_nth(char*, size_t);
271 size_t utf8_cplen(char*);
272 size_t utf8_chwidth(uint32_t);
273 size_t utf8_snwidth(const char*, size_t);
274 size_t utf8_swidth(const char*);
275 size_t utf8_swidth_between(const char*, const char*);
276 char *utf8_next_cp(const char*);
277 char *utf8_prev_cp(const char*, const char*);
279 /* util.c */
280 int mark_nonblock(int);
281 int has_prefix(const char*, const char*);
282 int unicode_isspace(uint32_t);
283 int unicode_isgraph(uint32_t);
284 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
286 /* wrap.c */
287 void empty_linelist(struct window*);
288 void empty_vlist(struct window*);
289 int wrap_text(struct window*, const char*, struct line*, size_t);
290 int hardwrap_text(struct window*, struct line*, size_t);
292 #endif /* TELESCOPE_H */