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,
50 IMSG_SESSION_START,
51 IMSG_SESSION_TAB,
52 IMSG_SESSION_END,
53 };
55 enum line_type {
56 LINE_TEXT,
57 LINE_LINK,
58 LINE_TITLE_1,
59 LINE_TITLE_2,
60 LINE_TITLE_3,
61 LINE_ITEM,
62 LINE_QUOTE,
63 LINE_PRE_START,
64 LINE_PRE_CONTENT,
65 LINE_PRE_END,
66 };
68 struct line {
69 enum line_type type;
70 char *line;
71 char *alt;
72 int flags;
73 TAILQ_ENTRY(line) lines;
74 };
76 struct vline {
77 const struct line *parent;
78 char *line;
79 int flags;
80 TAILQ_ENTRY(vline) vlines;
81 };
83 struct parser;
84 struct page;
86 /* typedef void (*initparserfn)(struct parser*); */
88 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
89 typedef int (*parserfreefn)(struct parser*);
91 typedef void (imsg_handlerfn)(struct imsg*, size_t);
93 struct parser {
94 const char *name;
95 char title[32+1];
96 char *buf;
97 size_t len;
98 size_t cap;
99 int flags;
100 parsechunkfn parse;
101 parserfreefn free;
103 TAILQ_HEAD(, line) head;
104 };
106 /*
107 * differnt types of trust for a certificate. Following
108 * gemini://thfr.info/gemini/modified-trust-verify.gmi
109 */
110 enum trust_state {
111 TS_UNKNOWN,
112 TS_UNTRUSTED,
113 TS_TRUSTED,
114 TS_VERIFIED,
115 };
117 struct tofu_entry {
118 char domain[GEMINI_URL_LEN];
119 /* enough space for ``PROTO:HASH''. probably isn't a good
120 * idea thou. */
121 char hash[128+1];
122 int verified;
123 };
125 struct histhead {
126 TAILQ_HEAD(mhisthead, hist) head;
127 size_t len;
128 };
129 struct hist {
130 char h[1025];
131 TAILQ_ENTRY(hist) entries;
132 };
134 struct window {
135 struct parser page;
136 int curs_x;
137 int curs_y;
138 size_t line_off;
139 size_t line_max;
140 struct vline *current_line;
141 size_t cpoff;
142 TAILQ_HEAD(vhead, vline) head;
143 };
145 extern TAILQ_HEAD(tabshead, tab) tabshead;
146 struct tab {
147 TAILQ_ENTRY(tab) tabs;
148 uint32_t id;
149 uint32_t flags;
151 enum trust_state trust;
152 struct phos_uri uri;
153 struct histhead hist;
154 struct hist *hist_cur;
155 size_t hist_off;
157 int code;
158 char meta[GEMINI_URL_LEN];
159 int redirect_count;
161 struct window window;
163 short loading_anim;
164 short loading_anim_step;
165 struct event loadingev;
166 };
168 struct proto {
169 const char *schema;
171 /* should load the given url in the tab. Optionally, it may
172 * consider the given url as relative to the one already
173 * present in tab. It must set tab->urlstr to a serialized
174 * human-friendly URL. */
175 void (*loadfn)(struct tab*, const char*);
176 };
178 struct kmap {
179 TAILQ_HEAD(map, keymap) m;
180 void (*unhandled_input)(void);
181 };
183 struct keymap {
184 int meta;
185 int key;
186 struct kmap map;
187 void (*fn)(struct window*);
189 TAILQ_ENTRY(keymap) keymaps;
190 };
192 /* fs.c */
193 int fs_init(void);
194 int fs_main(struct imsgbuf*);
195 int load_certs(struct ohash*);
196 int load_last_session(void(*)(const char*));
198 /* gemini.c */
199 int client_main(struct imsgbuf*);
201 /* gemtext.c */
202 void gemtext_initparser(struct parser*);
204 /* hash.c */
205 void telescope_ohash_init(struct ohash*, unsigned int, ptrdiff_t);
206 struct tofu_entry *telescope_lookup_tofu(struct ohash*, const char*);
207 void telescope_ohash_insert(struct ohash*, struct tofu_entry*);
209 /* hist.c */
210 void hist_clear_forward(struct histhead*, struct hist*);
211 void hist_push(struct histhead*, struct hist*);
213 /* keymap.c */
214 int kbd(const char*);
215 const char *unkbd(int);
216 int kmap_define_key(struct kmap*, const char*, void(*)(struct window*));
218 /* mime.c */
219 int setup_parser_for(struct tab*);
221 /* pages.c */
222 extern const char *about_new;
224 #define CANNOT_FETCH 0
225 #define TOO_MUCH_REDIRECTS 1
226 #define MALFORMED_RESPONSE 2
227 #define UNKNOWN_TYPE_OR_CSET 3
228 extern const char *err_pages[70];
230 /* parser.c */
231 int parser_append(struct parser*, const char*, size_t);
232 int parser_set_buf(struct parser*, const char*, size_t);
233 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
235 /* sandbox.c */
236 void sandbox_network_process(void);
237 void sandbox_ui_process(void);
238 void sandbox_fs_process(void);
240 /* telescope.c */
241 void load_about_url(struct tab*, const char*);
242 void load_gemini_url(struct tab*, const char*);
243 void load_url(struct tab*, const char*);
244 int load_previous_page(struct tab*);
245 int load_next_page(struct tab*);
246 void stop_tab(struct tab*);
247 void add_to_bookmarks(const char*);
248 void save_session(void);
250 /* textplain.c */
251 void textplain_initparser(struct parser*);
253 /* ui.c */
254 int ui_init(int, char * const*);
255 void ui_on_tab_loaded(struct tab*);
256 void ui_on_tab_refresh(struct tab*);
257 void ui_require_input(struct tab*, int);
258 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
259 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
260 void ui_end(void);
262 /* utf.8 */
263 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
264 size_t utf8_encode(uint32_t, char*);
265 char *utf8_nth(char*, size_t);
266 size_t utf8_cplen(char*);
267 size_t utf8_chwidth(uint32_t);
268 size_t utf8_snwidth(const char*, size_t);
269 size_t utf8_swidth(const char*);
270 size_t utf8_swidth_between(const char*, const char*);
271 char *utf8_next_cp(const char*);
272 char *utf8_prev_cp(const char*, const char*);
274 /* util.c */
275 int mark_nonblock(int);
276 int has_prefix(const char*, const char*);
277 int unicode_isspace(uint32_t);
278 int unicode_isgraph(uint32_t);
279 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
281 /* wrap.c */
282 void empty_linelist(struct window*);
283 void empty_vlist(struct window*);
284 int wrap_text(struct window*, const char*, struct line*, size_t);
285 int hardwrap_text(struct window*, struct line*, size_t);
287 #endif /* TELESCOPE_H */