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>
24 #include "url.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,
51 IMSG_SESSION_START,
52 IMSG_SESSION_TAB,
53 IMSG_SESSION_END,
54 };
56 enum line_type {
57 LINE_TEXT,
58 LINE_LINK,
59 LINE_TITLE_1,
60 LINE_TITLE_2,
61 LINE_TITLE_3,
62 LINE_ITEM,
63 LINE_QUOTE,
64 LINE_PRE_START,
65 LINE_PRE_CONTENT,
66 LINE_PRE_END,
67 };
69 struct line {
70 enum line_type type;
71 char *line;
72 char *alt;
73 int flags;
74 TAILQ_ENTRY(line) lines;
75 };
77 struct vline {
78 const struct line *parent;
79 char *line;
80 int flags;
81 TAILQ_ENTRY(vline) vlines;
82 };
84 struct parser;
85 struct page;
87 /* typedef void (*initparserfn)(struct parser*); */
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];
120 /* enough space for ``PROTO:HASH''. probably isn't a good
121 * idea thou. */
122 char hash[128+1];
123 int verified;
124 };
126 struct histhead {
127 TAILQ_HEAD(mhisthead, hist) head;
128 size_t len;
129 };
130 struct hist {
131 char h[1025];
132 TAILQ_ENTRY(hist) entries;
133 };
135 struct window {
136 struct parser page;
137 int curs_x;
138 int curs_y;
139 size_t line_off;
140 size_t line_max;
141 struct vline *current_line;
142 size_t cpoff;
143 TAILQ_HEAD(vhead, vline) head;
144 };
146 extern TAILQ_HEAD(tabshead, tab) tabshead;
147 struct tab {
148 TAILQ_ENTRY(tab) tabs;
149 uint32_t id;
150 uint32_t flags;
152 enum trust_state trust;
153 struct url url;
154 struct histhead hist;
155 struct hist *hist_cur;
156 size_t hist_off;
158 int code;
159 char meta[GEMINI_URL_LEN];
160 int redirect_count;
162 struct window window;
164 short loading_anim;
165 short loading_anim_step;
166 struct event loadingev;
167 };
169 struct proto {
170 const char *schema;
172 /* should load the given url in the tab. Optionally, it may
173 * consider the given url as relative to the one already
174 * present in tab. It must set tab->urlstr to a serialized
175 * human-friendly URL. */
176 void (*loadfn)(struct tab*, const char*);
177 };
179 struct kmap {
180 TAILQ_HEAD(map, keymap) m;
181 void (*unhandled_input)(void);
182 };
184 struct keymap {
185 int meta;
186 int key;
187 struct kmap map;
188 void (*fn)(struct window*);
190 TAILQ_ENTRY(keymap) keymaps;
191 };
193 /* fs.c */
194 int fs_init(void);
195 int fs_main(struct imsgbuf*);
196 int load_certs(struct ohash*);
197 int load_last_session(void(*)(const char*));
199 /* gemini.c */
200 int client_main(struct imsgbuf*);
202 /* gemtext.c */
203 void gemtext_initparser(struct parser*);
205 /* hash.c */
206 void telescope_ohash_init(struct ohash*, unsigned int, ptrdiff_t);
207 struct tofu_entry *telescope_lookup_tofu(struct ohash*, const char*);
208 void telescope_ohash_insert(struct ohash*, struct tofu_entry*);
210 /* hist.c */
211 void hist_clear_forward(struct histhead*, struct hist*);
212 void hist_push(struct histhead*, struct hist*);
214 /* keymap.c */
215 int kbd(const char*);
216 const char *unkbd(int);
217 int kmap_define_key(struct kmap*, const char*, void(*)(struct window*));
219 /* mime.c */
220 int setup_parser_for(struct tab*);
222 /* pages.c */
223 extern const char *about_new;
225 #define CANNOT_FETCH 0
226 #define TOO_MUCH_REDIRECTS 1
227 #define MALFORMED_RESPONSE 2
228 #define UNKNOWN_TYPE_OR_CSET 3
229 extern const char *err_pages[70];
231 /* parser.c */
232 int parser_append(struct parser*, const char*, size_t);
233 int parser_set_buf(struct parser*, const char*, size_t);
234 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
236 /* sandbox.c */
237 void sandbox_network_process(void);
238 void sandbox_ui_process(void);
239 void sandbox_fs_process(void);
241 /* telescope.c */
242 void load_about_url(struct tab*, const char*);
243 void load_gemini_url(struct tab*, const char*);
244 void load_url(struct tab*, const char*);
245 int load_previous_page(struct tab*);
246 int load_next_page(struct tab*);
247 void stop_tab(struct tab*);
248 void add_to_bookmarks(const char*);
249 void save_session(void);
251 /* textplain.c */
252 void textplain_initparser(struct parser*);
254 /* ui.c */
255 int ui_init(int, char * const*);
256 void ui_on_tab_loaded(struct tab*);
257 void ui_on_tab_refresh(struct tab*);
258 void ui_require_input(struct tab*, int);
259 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
260 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
261 void ui_end(void);
263 /* utf.8 */
264 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
265 size_t utf8_encode(uint32_t, char*);
266 char *utf8_nth(char*, size_t);
267 size_t utf8_cplen(char*);
268 size_t utf8_chwidth(uint32_t);
269 size_t utf8_snwidth(const char*, size_t);
270 size_t utf8_swidth(const char*);
271 size_t utf8_swidth_between(const char*, const char*);
272 char *utf8_next_cp(const char*);
273 char *utf8_prev_cp(const char*, const char*);
275 /* util.c */
276 int mark_nonblock(int);
277 int has_prefix(const char*, const char*);
278 int unicode_isspace(uint32_t);
279 int unicode_isgraph(uint32_t);
280 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
282 /* wrap.c */
283 void empty_linelist(struct window*);
284 void empty_vlist(struct window*);
285 int wrap_text(struct window*, const char*, struct line*, size_t);
286 int hardwrap_text(struct window*, struct line*, size_t);
288 #endif /* TELESCOPE_H */