Blame


1 1ac119fb 2024-01-23 op /*
2 62c0f697 2024-02-21 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 1ac119fb 2024-01-23 op *
4 1ac119fb 2024-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 1ac119fb 2024-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 1ac119fb 2024-01-23 op * copyright notice and this permission notice appear in all copies.
7 1ac119fb 2024-01-23 op *
8 1ac119fb 2024-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1ac119fb 2024-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1ac119fb 2024-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1ac119fb 2024-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1ac119fb 2024-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1ac119fb 2024-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1ac119fb 2024-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 1ac119fb 2024-01-23 op */
16 1ac119fb 2024-01-23 op
17 1ac119fb 2024-01-23 op #include "compat.h"
18 1ac119fb 2024-01-23 op
19 1ac119fb 2024-01-23 op #include <stdio.h>
20 1ac119fb 2024-01-23 op #include <stdlib.h>
21 1ac119fb 2024-01-23 op #include <string.h>
22 1ac119fb 2024-01-23 op
23 62c0f697 2024-02-21 op #include "iri.h"
24 c1d27b0e 2024-06-14 op #include "parser.h"
25 c1d27b0e 2024-06-14 op #include "telescope.h"
26 1ac119fb 2024-01-23 op #include "utils.h"
27 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
28 e5e04904 2024-02-06 op
29 e5e04904 2024-02-06 op #ifndef LINE_MAX
30 e5e04904 2024-02-06 op #define LINE_MAX 2048
31 e5e04904 2024-02-06 op #endif
32 1ac119fb 2024-01-23 op
33 1ac119fb 2024-01-23 op struct gm_selector {
34 1ac119fb 2024-01-23 op char type;
35 1ac119fb 2024-01-23 op const char *ds;
36 1ac119fb 2024-01-23 op const char *selector;
37 1ac119fb 2024-01-23 op const char *addr;
38 1ac119fb 2024-01-23 op const char *port;
39 1ac119fb 2024-01-23 op };
40 1ac119fb 2024-01-23 op
41 1ac119fb 2024-01-23 op static void gm_parse_selector(char *, struct gm_selector *);
42 1ac119fb 2024-01-23 op
43 c1d27b0e 2024-06-14 op static int gm_parse_line(struct buffer *, const char *, size_t);
44 c1d27b0e 2024-06-14 op static int gm_serialize(struct buffer *, FILE *);
45 1ac119fb 2024-01-23 op
46 fd1c80ce 2024-06-14 op const struct parser gophermap_parser = {
47 c1d27b0e 2024-06-14 op .name = "gophermap",
48 c1d27b0e 2024-06-14 op .parseline = &gm_parse_line,
49 c1d27b0e 2024-06-14 op .serialize = &gm_serialize,
50 c1d27b0e 2024-06-14 op };
51 1ac119fb 2024-01-23 op
52 1ac119fb 2024-01-23 op static void
53 1ac119fb 2024-01-23 op gm_parse_selector(char *line, struct gm_selector *s)
54 1ac119fb 2024-01-23 op {
55 1ac119fb 2024-01-23 op s->type = *line++;
56 1ac119fb 2024-01-23 op s->ds = line;
57 1ac119fb 2024-01-23 op s->selector = "";
58 1ac119fb 2024-01-23 op s->addr = "";
59 1ac119fb 2024-01-23 op s->port = "";
60 1ac119fb 2024-01-23 op
61 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
62 1ac119fb 2024-01-23 op return;
63 1ac119fb 2024-01-23 op *line++ = '\0';
64 1ac119fb 2024-01-23 op s->selector = line;
65 1ac119fb 2024-01-23 op
66 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
67 1ac119fb 2024-01-23 op return;
68 1ac119fb 2024-01-23 op *line++ = '\0';
69 1ac119fb 2024-01-23 op s->addr = line;
70 1ac119fb 2024-01-23 op
71 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
72 1ac119fb 2024-01-23 op return;
73 1ac119fb 2024-01-23 op *line++ = '\0';
74 1ac119fb 2024-01-23 op s->port = line;
75 1ac119fb 2024-01-23 op }
76 1ac119fb 2024-01-23 op
77 1ac119fb 2024-01-23 op static int
78 62c0f697 2024-02-21 op selector2uri(struct gm_selector *s, char *buf, size_t len)
79 62c0f697 2024-02-21 op {
80 62c0f697 2024-02-21 op int r;
81 62c0f697 2024-02-21 op
82 62c0f697 2024-02-21 op r = snprintf(buf, len, "gopher://%s:%s/%c%s",
83 62c0f697 2024-02-21 op s->addr, s->port, s->type, *s->selector != '/' ? "/" : "");
84 62c0f697 2024-02-21 op if (r < 0 || (size_t)r >= len)
85 62c0f697 2024-02-21 op return (-1);
86 62c0f697 2024-02-21 op
87 62c0f697 2024-02-21 op buf += r;
88 62c0f697 2024-02-21 op len -= r;
89 62c0f697 2024-02-21 op return (iri_urlescape(s->selector, buf, len));
90 1ac119fb 2024-01-23 op }
91 1ac119fb 2024-01-23 op
92 1ac119fb 2024-01-23 op static inline int
93 c1d27b0e 2024-06-14 op emit_line(struct buffer *b, enum line_type type, struct gm_selector *s)
94 1ac119fb 2024-01-23 op {
95 1ac119fb 2024-01-23 op struct line *l;
96 62c0f697 2024-02-21 op char buf[LINE_MAX];
97 1ac119fb 2024-01-23 op
98 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
99 1ac119fb 2024-01-23 op
100 3d89457c 2024-06-18 thomas.ad l->line = xstrdup(s->ds);
101 1ac119fb 2024-01-23 op
102 1ac119fb 2024-01-23 op switch (l->type = type) {
103 1ac119fb 2024-01-23 op case LINE_LINK:
104 1ac119fb 2024-01-23 op if (s->type == 'h' && !strncmp(s->selector, "URL:", 4)) {
105 1ac119fb 2024-01-23 op strlcpy(buf, s->selector+4, sizeof(buf));
106 62c0f697 2024-02-21 op } else if (selector2uri(s, buf, sizeof(buf)) == -1)
107 62c0f697 2024-02-21 op goto err;
108 1ac119fb 2024-01-23 op
109 3d89457c 2024-06-18 thomas.ad l->alt = xstrdup(buf);
110 1ac119fb 2024-01-23 op break;
111 1ac119fb 2024-01-23 op
112 1ac119fb 2024-01-23 op default:
113 1ac119fb 2024-01-23 op break;
114 1ac119fb 2024-01-23 op }
115 1ac119fb 2024-01-23 op
116 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
117 1ac119fb 2024-01-23 op
118 1ac119fb 2024-01-23 op return 1;
119 1ac119fb 2024-01-23 op
120 1ac119fb 2024-01-23 op err:
121 1ac119fb 2024-01-23 op if (l != NULL) {
122 1ac119fb 2024-01-23 op free(l->line);
123 1ac119fb 2024-01-23 op free(l->alt);
124 1ac119fb 2024-01-23 op free(l);
125 1ac119fb 2024-01-23 op }
126 1ac119fb 2024-01-23 op return 0;
127 1ac119fb 2024-01-23 op }
128 1ac119fb 2024-01-23 op
129 1ac119fb 2024-01-23 op static int
130 c1d27b0e 2024-06-14 op gm_parse_line(struct buffer *b, const char *line, size_t linelen)
131 1ac119fb 2024-01-23 op {
132 1ac119fb 2024-01-23 op char buf[LINE_MAX] = {0};
133 1ac119fb 2024-01-23 op struct gm_selector s = {0};
134 1ac119fb 2024-01-23 op
135 1ac119fb 2024-01-23 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
136 1ac119fb 2024-01-23 op gm_parse_selector(buf, &s);
137 1ac119fb 2024-01-23 op
138 1ac119fb 2024-01-23 op switch (s.type) {
139 1ac119fb 2024-01-23 op case '0': /* text file */
140 1ac119fb 2024-01-23 op case '1': /* gopher submenu */
141 1ac119fb 2024-01-23 op case '2': /* CCSO nameserver */
142 1ac119fb 2024-01-23 op case '4': /* binhex-encoded file */
143 1ac119fb 2024-01-23 op case '5': /* DOS file */
144 1ac119fb 2024-01-23 op case '6': /* uuencoded file */
145 1ac119fb 2024-01-23 op case '7': /* full-text search */
146 1ac119fb 2024-01-23 op case '8': /* telnet */
147 1ac119fb 2024-01-23 op case '9': /* binary file */
148 1ac119fb 2024-01-23 op case '+': /* mirror or alternate server */
149 1ac119fb 2024-01-23 op case 'g': /* gif */
150 1ac119fb 2024-01-23 op case 'I': /* image */
151 1ac119fb 2024-01-23 op case 'T': /* telnet 3270 */
152 1ac119fb 2024-01-23 op case ':': /* gopher+: bitmap image */
153 1ac119fb 2024-01-23 op case ';': /* gopher+: movie file */
154 1ac119fb 2024-01-23 op case 'd': /* non-canonical: doc */
155 1ac119fb 2024-01-23 op case 'h': /* non-canonical: html file */
156 1ac119fb 2024-01-23 op case 's': /* non-canonical: sound file */
157 c1d27b0e 2024-06-14 op if (!emit_line(b, LINE_LINK, &s))
158 1ac119fb 2024-01-23 op return 0;
159 1ac119fb 2024-01-23 op break;
160 1ac119fb 2024-01-23 op
161 1ac119fb 2024-01-23 op case 'i': /* non-canonical: message */
162 c1d27b0e 2024-06-14 op if (!emit_line(b, LINE_TEXT, &s))
163 1ac119fb 2024-01-23 op return 0;
164 1ac119fb 2024-01-23 op break;
165 1ac119fb 2024-01-23 op
166 1ac119fb 2024-01-23 op case '3': /* error code */
167 c1d27b0e 2024-06-14 op if (!emit_line(b, LINE_QUOTE, &s))
168 1ac119fb 2024-01-23 op return 0;
169 1ac119fb 2024-01-23 op break;
170 1ac119fb 2024-01-23 op }
171 1ac119fb 2024-01-23 op
172 1ac119fb 2024-01-23 op return 1;
173 1ac119fb 2024-01-23 op }
174 1ac119fb 2024-01-23 op
175 1ac119fb 2024-01-23 op static inline const char *
176 1ac119fb 2024-01-23 op gopher_skip_selector(const char *path, int *ret_type)
177 1ac119fb 2024-01-23 op {
178 1ac119fb 2024-01-23 op *ret_type = 0;
179 1ac119fb 2024-01-23 op
180 1ac119fb 2024-01-23 op if (!strcmp(path, "/") || *path == '\0') {
181 1ac119fb 2024-01-23 op *ret_type = '1';
182 1ac119fb 2024-01-23 op return path;
183 1ac119fb 2024-01-23 op }
184 1ac119fb 2024-01-23 op
185 1ac119fb 2024-01-23 op if (*path != '/')
186 1ac119fb 2024-01-23 op return path;
187 1ac119fb 2024-01-23 op path++;
188 1ac119fb 2024-01-23 op
189 1ac119fb 2024-01-23 op switch (*ret_type = *path) {
190 1ac119fb 2024-01-23 op case '0':
191 1ac119fb 2024-01-23 op case '1':
192 1ac119fb 2024-01-23 op case '7':
193 1ac119fb 2024-01-23 op break;
194 1ac119fb 2024-01-23 op
195 1ac119fb 2024-01-23 op default:
196 1ac119fb 2024-01-23 op *ret_type = 0;
197 1ac119fb 2024-01-23 op path -= 1;
198 1ac119fb 2024-01-23 op return path;
199 1ac119fb 2024-01-23 op }
200 1ac119fb 2024-01-23 op
201 1ac119fb 2024-01-23 op return ++path;
202 1ac119fb 2024-01-23 op }
203 1ac119fb 2024-01-23 op
204 1ac119fb 2024-01-23 op static int
205 1ac119fb 2024-01-23 op serialize_link(struct line *line, const char *text, FILE *fp)
206 1ac119fb 2024-01-23 op {
207 1ac119fb 2024-01-23 op size_t portlen = 0;
208 1ac119fb 2024-01-23 op int type;
209 1ac119fb 2024-01-23 op const char *uri, *endhost, *port, *path, *colon;
210 1ac119fb 2024-01-23 op
211 1ac119fb 2024-01-23 op if ((uri = line->alt) == NULL)
212 1ac119fb 2024-01-23 op return -1;
213 1ac119fb 2024-01-23 op
214 1ac119fb 2024-01-23 op if (strncmp(uri, "gopher://", 9) != 0)
215 1ac119fb 2024-01-23 op return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
216 1ac119fb 2024-01-23 op text, line->alt);
217 1ac119fb 2024-01-23 op
218 1ac119fb 2024-01-23 op uri += 9; /* skip gopher:// */
219 1ac119fb 2024-01-23 op
220 1ac119fb 2024-01-23 op path = strchr(uri, '/');
221 1ac119fb 2024-01-23 op colon = strchr(uri, ':');
222 1ac119fb 2024-01-23 op
223 1ac119fb 2024-01-23 op if (path != NULL && colon > path)
224 1ac119fb 2024-01-23 op colon = NULL;
225 1ac119fb 2024-01-23 op
226 1ac119fb 2024-01-23 op if ((endhost = colon) == NULL &&
227 1ac119fb 2024-01-23 op (endhost = path) == NULL)
228 1ac119fb 2024-01-23 op endhost = strchr(uri, '\0');
229 1ac119fb 2024-01-23 op
230 1ac119fb 2024-01-23 op if (colon != NULL) {
231 1ac119fb 2024-01-23 op for (port = colon+1; *port && *port != '/'; ++port)
232 1ac119fb 2024-01-23 op ++portlen;
233 1ac119fb 2024-01-23 op port = colon+1;
234 1ac119fb 2024-01-23 op } else {
235 1ac119fb 2024-01-23 op port = "70";
236 1ac119fb 2024-01-23 op portlen = 2;
237 1ac119fb 2024-01-23 op }
238 1ac119fb 2024-01-23 op
239 1ac119fb 2024-01-23 op if (path == NULL) {
240 1ac119fb 2024-01-23 op type = '1';
241 1ac119fb 2024-01-23 op path = "";
242 1ac119fb 2024-01-23 op } else
243 1ac119fb 2024-01-23 op path = gopher_skip_selector(path, &type);
244 1ac119fb 2024-01-23 op
245 1ac119fb 2024-01-23 op return fprintf(fp, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
246 1ac119fb 2024-01-23 op path, (int)(endhost - uri), uri, (int)portlen, port);
247 1ac119fb 2024-01-23 op }
248 1ac119fb 2024-01-23 op
249 1ac119fb 2024-01-23 op static int
250 c1d27b0e 2024-06-14 op gm_serialize(struct buffer *b, FILE *fp)
251 1ac119fb 2024-01-23 op {
252 1ac119fb 2024-01-23 op struct line *line;
253 1ac119fb 2024-01-23 op const char *text;
254 1ac119fb 2024-01-23 op int r;
255 1ac119fb 2024-01-23 op
256 c1d27b0e 2024-06-14 op TAILQ_FOREACH(line, &b->head, lines) {
257 1ac119fb 2024-01-23 op if ((text = line->line) == NULL)
258 1ac119fb 2024-01-23 op text = "";
259 1ac119fb 2024-01-23 op
260 1ac119fb 2024-01-23 op switch (line->type) {
261 1ac119fb 2024-01-23 op case LINE_LINK:
262 1ac119fb 2024-01-23 op r = serialize_link(line, text, fp);
263 1ac119fb 2024-01-23 op break;
264 1ac119fb 2024-01-23 op
265 1ac119fb 2024-01-23 op case LINE_TEXT:
266 1ac119fb 2024-01-23 op r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
267 1ac119fb 2024-01-23 op break;
268 1ac119fb 2024-01-23 op
269 1ac119fb 2024-01-23 op case LINE_QUOTE:
270 1ac119fb 2024-01-23 op r = fprintf(fp, "3%s\t\terror.host\t1\n", text);
271 1ac119fb 2024-01-23 op break;
272 1ac119fb 2024-01-23 op
273 1ac119fb 2024-01-23 op default:
274 1ac119fb 2024-01-23 op /* unreachable */
275 1ac119fb 2024-01-23 op abort();
276 1ac119fb 2024-01-23 op }
277 1ac119fb 2024-01-23 op
278 1ac119fb 2024-01-23 op if (r == -1)
279 1ac119fb 2024-01-23 op return 0;
280 1ac119fb 2024-01-23 op }
281 1ac119fb 2024-01-23 op
282 1ac119fb 2024-01-23 op return 1;
283 1ac119fb 2024-01-23 op }