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 };
50 enum line_type {
51 LINE_TEXT,
52 LINE_LINK,
53 LINE_TITLE_1,
54 LINE_TITLE_2,
55 LINE_TITLE_3,
56 LINE_ITEM,
57 LINE_QUOTE,
58 LINE_PRE_START,
59 LINE_PRE_CONTENT,
60 LINE_PRE_END,
61 };
63 struct line {
64 enum line_type type;
65 char *line;
66 char *alt;
67 int flags;
68 TAILQ_ENTRY(line) lines;
69 };
71 struct vline {
72 const struct line *parent;
73 char *line;
74 int flags;
75 TAILQ_ENTRY(vline) vlines;
76 };
78 struct parser;
79 struct page;
81 /* typedef void (*initparserfn)(struct parser*); */
83 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
84 typedef int (*parserfreefn)(struct parser*);
86 typedef void (imsg_handlerfn)(struct imsg*, size_t);
88 struct parser {
89 const char *name;
90 char title[32+1];
91 char *buf;
92 size_t len;
93 size_t cap;
94 int flags;
95 parsechunkfn parse;
96 parserfreefn free;
98 TAILQ_HEAD(, line) head;
99 };
101 struct histhead {
102 TAILQ_HEAD(mhisthead, hist) head;
103 size_t len;
104 };
105 struct hist {
106 char h[1025];
107 TAILQ_ENTRY(hist) entries;
108 };
110 struct ui_state {
111 int curs_x;
112 int curs_y;
113 size_t line_off;
114 size_t line_max;
115 struct vline *current_line;
116 size_t line_x;
118 short loading_anim;
119 short loading_anim_step;
120 struct event loadingev;
122 TAILQ_HEAD(vhead, vline) head;
123 };
125 /*
126 * differnt types of trust for a certificate. Following
127 * gemini://thfr.info/gemini/modified-trust-verify.gmi
128 */
129 enum trust_state {
130 TS_UNKNOWN,
131 TS_UNTRUSTED,
132 TS_TRUSTED,
133 TS_VERIFIED,
134 };
136 struct tofu_entry {
137 char domain[GEMINI_URL_LEN];
138 /* enough space for ``PROTO:HASH''. probably isn't a good
139 * idea thou. */
140 char hash[128+1];
141 int verified;
142 };
144 extern TAILQ_HEAD(tabshead, tab) tabshead;
145 struct tab {
146 struct parser page;
147 TAILQ_ENTRY(tab) tabs;
148 uint32_t id;
149 uint32_t flags;
151 enum trust_state trust;
152 struct url url;
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 ui_state s;
162 };
164 struct proto {
165 const char *schema;
167 /* should load the given url in the tab. Optionally, it may
168 * consider the given url as relative to the one already
169 * present in tab. It must set tab->urlstr to a serialized
170 * human-friendly URL. */
171 void (*loadfn)(struct tab*, const char*);
172 };
174 struct kmap {
175 TAILQ_HEAD(map, keymap) m;
176 void (*unhandled_input)(void);
177 };
179 struct keymap {
180 int meta;
181 int key;
182 struct kmap map;
183 void (*fn)(struct tab*);
185 TAILQ_ENTRY(keymap) keymaps;
186 };
188 /* fs.c */
189 int fs_main(struct imsgbuf*);
191 /* gemini.c */
192 int client_main(struct imsgbuf*);
194 /* gemtext.c */
195 void gemtext_initparser(struct parser*);
197 /* hash.c */
198 void telescope_ohash_init(struct ohash*, unsigned int, ptrdiff_t);
199 struct tofu_entry *telescope_lookup_tofu(struct ohash*, const char*);
200 void telescope_ohash_insert(struct ohash*, struct tofu_entry*);
202 /* hist.c */
203 void hist_clear_forward(struct histhead*, struct hist*);
204 void hist_push(struct histhead*, struct hist*);
206 /* keymap.c */
207 int kbd(const char*);
208 const char *unkbd(int);
209 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
211 /* mime.c */
212 int setup_parser_for(struct tab*);
214 /* pages.c */
215 extern const char *about_new;
217 #define CANNOT_FETCH 0
218 #define TOO_MUCH_REDIRECTS 1
219 #define MALFORMED_RESPONSE 2
220 #define UNKNOWN_TYPE_OR_CSET 3
221 extern const char *err_pages[70];
223 /* parser.c */
224 int parser_append(struct parser*, const char*, size_t);
225 int parser_set_buf(struct parser*, const char*, size_t);
227 /* sandbox.c */
228 void sandbox_network_process(void);
229 void sandbox_ui_process(void);
230 void sandbox_fs_process(void);
232 /* telescope.c */
233 void load_about_url(struct tab*, const char*);
234 void load_gemini_url(struct tab*, const char*);
235 void load_url(struct tab*, const char*);
236 int load_previous_page(struct tab*);
237 int load_next_page(struct tab*);
238 void stop_tab(struct tab*);
239 void add_to_bookmarks(const char*);
241 /* textplain.c */
242 void textplain_initparser(struct parser*);
244 /* ui.c */
245 int ui_init(void);
246 void ui_on_tab_loaded(struct tab*);
247 void ui_on_tab_refresh(struct tab*);
248 void ui_require_input(struct tab*, int);
249 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
250 void ui_end(void);
252 /* util.c */
253 int mark_nonblock(int);
254 int has_prefix(const char*, const char*);
255 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
257 /* wrap.c */
258 void wrap_text(struct tab*, const char*, struct line*, size_t);
259 int hardwrap_text(struct tab*, struct line*, size_t);
261 #endif /* TELESCOPE_H */