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