Blob


1 /*
2 * Copyright (c) 2021, 2024 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 <limits.h>
21 #include <stdio.h> /* XXX: for parsers.h */
23 #include "iri.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
29 #define TITLE_MAX 128+1 /* account for NUL too */
31 #define SIDE_WINDOW_LEFT 0x1
32 #define SIDE_WINDOW_BOTTOM 0x2
34 enum line_type {
35 /* text/gemini */
36 LINE_TEXT,
37 LINE_LINK,
38 LINE_TITLE_1,
39 LINE_TITLE_2,
40 LINE_TITLE_3,
41 LINE_ITEM,
42 LINE_QUOTE,
43 LINE_PRE_START,
44 LINE_PRE_CONTENT,
45 LINE_PRE_END,
47 /* text/x-patch */
48 LINE_PATCH,
49 LINE_PATCH_HDR,
50 LINE_PATCH_HUNK_HDR,
51 LINE_PATCH_ADD,
52 LINE_PATCH_DEL,
54 /* minibuffer */
55 LINE_COMPL,
56 LINE_COMPL_CURRENT,
58 /* help */
59 LINE_HELP,
61 /* download */
62 LINE_DOWNLOAD,
63 LINE_DOWNLOAD_DONE,
64 LINE_DOWNLOAD_INFO,
66 /* misc ui */
67 LINE_FRINGE,
68 };
70 struct line {
71 enum line_type type;
72 char *line;
73 char *alt;
74 void *data;
76 #define L_HIDDEN 0x1
77 int flags;
78 TAILQ_ENTRY(line) lines;
79 };
81 struct vline {
82 struct line *parent;
83 size_t from;
84 size_t len;
86 #define L_CONTINUATION 0x2
87 int flags;
88 TAILQ_ENTRY(vline) vlines;
89 };
91 /*
92 * different types of trust for a certificate. Following
93 * gemini://thfr.info/gemini/modified-trust-verify.gmi
94 */
95 enum trust_state {
96 TS_UNKNOWN,
97 TS_UNTRUSTED,
98 TS_TEMP_TRUSTED,
99 TS_TRUSTED,
100 TS_VERIFIED,
101 };
103 struct parser;
105 struct buffer {
106 char title[128 + 1];
107 const char *mode;
108 char *buf;
109 size_t len;
110 size_t cap;
112 #define PARSER_IN_BODY 1
113 #define PARSER_IN_PRE 2
114 #define PARSER_IN_PATCH_HDR 4
115 int parser_flags;
116 const struct parser *parser;
118 size_t last_line_off;
119 int force_redraw;
121 int curs_x;
122 int curs_y;
123 size_t line_off;
124 size_t line_max;
125 struct vline *top_line;
126 struct vline *current_line;
127 size_t point_offset;
129 TAILQ_HEAD(, line) head;
130 TAILQ_HEAD(vhead, vline) vhead;
131 };
133 #define TAB_CURRENT 0x1 /* only for save_session */
134 #define TAB_KILLED 0x2 /* only for save_session */
135 #define TAB_URGENT 0x4
136 #define TAB_LAZY 0x8 /* to lazy load tabs */
138 #define NEW_TAB_URL "about:new"
140 struct hist;
142 TAILQ_HEAD(tabshead, tab);
143 extern struct tabshead tabshead;
144 extern struct tabshead ktabshead;
145 struct tab {
146 TAILQ_ENTRY(tab) tabs;
147 uint32_t id;
148 uint32_t flags;
150 char *cert;
151 enum trust_state trust;
152 int faulty_gemserver;
153 const char *client_cert;
154 int client_cert_temp;
155 struct proxy *proxy;
156 struct iri iri;
157 struct hist *hist;
158 char *last_input_url;
160 int code;
161 char meta[GEMINI_URL_LEN];
162 int redirect_count;
164 struct buffer buffer;
166 short loading_anim;
167 short loading_anim_step;
168 unsigned long loading_timer;
169 };
171 extern TAILQ_HEAD(proxylist, proxy) proxies;
172 struct proxy {
173 char *match_proto;
175 char *host;
176 char *port;
177 int proto;
179 TAILQ_ENTRY(proxy) proxies;
180 };
182 enum {
183 PROTO_FINGER,
184 PROTO_GEMINI,
185 PROTO_GOPHER,
186 /* ... */
187 };
189 struct get_req {
190 int proto;
191 char host[254];
192 char port[16];
193 char req[1027];
194 };
196 /* downloads.c */
197 extern STAILQ_HEAD(downloads, download) downloads;
198 struct download {
199 uint32_t id;
200 int fd;
201 size_t bytes;
202 char *mime_type;
203 char *path;
204 STAILQ_ENTRY(download) entries;
205 };
207 void recompute_downloads(void);
208 struct download *enqueue_download(uint32_t, const char *, const char *);
209 struct download *download_by_id(uint32_t);
210 void download_finished(struct download *);
212 /* help.c */
213 void recompute_help(void);
215 /* mime.c */
216 int setup_parser_for(struct tab*);
218 /* net.c */
219 int net_main(void);
221 /* parse.y */
222 void parseconfig(const char *, int);
224 /* sandbox.c */
225 void sandbox_net_process(void);
226 void sandbox_ui_process(void);
228 /* telescope.c */
229 extern int operating;
230 extern int safe_mode;
232 #define LU_MODE_NONE 0x0
233 #define LU_MODE_NOHIST 0x1
234 #define LU_MODE_NOCACHE 0x2
236 void gopher_send_search_req(struct tab *, const char *);
237 void load_page_from_str(struct tab *, const char *);
238 void load_url(struct tab *, const char *, const char *, int);
239 void load_url_in_tab(struct tab *, const char *, const char *, int);
240 int load_previous_page(struct tab*);
241 int load_next_page(struct tab*);
242 void write_buffer(const char *, struct tab *);
243 void humanify_url(const char *, const char *, char *, size_t);
244 int bookmark_page(const char *);
245 int ui_send_net(int, uint32_t, int, const void *, uint16_t);
247 /* wrap.c */
248 void erase_buffer(struct buffer *);
249 void empty_linelist(struct buffer*);
250 void empty_vlist(struct buffer*);
251 int wrap_text(struct buffer*, const char*, struct line*, size_t, int, int);
252 int wrap_page(struct buffer *, int, int);
254 #endif /* TELESCOPE_H */