2 81839fee 2021-07-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 81839fee 2021-07-25 op * Permission to use, copy, modify, and distribute this software for any
5 81839fee 2021-07-25 op * purpose with or without fee is hereby granted, provided that the above
6 81839fee 2021-07-25 op * copyright notice and this permission notice appear in all copies.
8 81839fee 2021-07-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 81839fee 2021-07-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 81839fee 2021-07-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 81839fee 2021-07-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 81839fee 2021-07-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 81839fee 2021-07-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 81839fee 2021-07-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 81839fee 2021-07-25 op #include "compat.h"
19 81839fee 2021-07-25 op #include <stdio.h>
20 81839fee 2021-07-25 op #include <stdlib.h>
21 81839fee 2021-07-25 op #include <string.h>
23 81839fee 2021-07-25 op #include "parser.h"
24 9d65b1d9 2022-01-11 op #include "utils.h"
26 069c9a92 2022-01-19 op #define evap evbuffer_add_printf
28 81839fee 2021-07-25 op struct gm_selector {
30 81839fee 2021-07-25 op const char *ds;
31 81839fee 2021-07-25 op const char *selector;
32 81839fee 2021-07-25 op const char *addr;
33 81839fee 2021-07-25 op const char *port;
36 81839fee 2021-07-25 op static void gm_parse_selector(char *, struct gm_selector *);
38 81839fee 2021-07-25 op static int gm_parse(struct parser *, const char *, size_t);
39 81839fee 2021-07-25 op static int gm_foreach_line(struct parser *, const char *, size_t);
40 81839fee 2021-07-25 op static int gm_free(struct parser *);
41 069c9a92 2022-01-19 op static int gm_serialize(struct parser *, struct evbuffer *);
44 81839fee 2021-07-25 op gophermap_initparser(struct parser *p)
46 81839fee 2021-07-25 op memset(p, 0, sizeof(*p));
48 81839fee 2021-07-25 op p->name = "gophermap";
49 81839fee 2021-07-25 op p->parse = &gm_parse;
50 81839fee 2021-07-25 op p->free = &gm_free;
51 069c9a92 2022-01-19 op p->serialize = &gm_serialize;
53 78894e73 2021-08-12 op TAILQ_INIT(&p->head);
57 81839fee 2021-07-25 op gm_parse_selector(char *line, struct gm_selector *s)
59 81839fee 2021-07-25 op s->type = *line++;
61 6c739557 2021-08-13 op s->selector = "";
65 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
67 95a8c791 2021-08-26 op *line++ = '\0';
68 81839fee 2021-07-25 op s->selector = line;
70 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
72 81839fee 2021-07-25 op *line++ = '\0';
73 81839fee 2021-07-25 op s->addr = line;
75 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
77 81839fee 2021-07-25 op *line++ = '\0';
78 81839fee 2021-07-25 op s->port = line;
82 81839fee 2021-07-25 op gm_parse(struct parser *p, const char *buf, size_t size)
84 81839fee 2021-07-25 op return parser_foreach_line(p, buf, size, gm_foreach_line);
87 81839fee 2021-07-25 op static inline int
88 81839fee 2021-07-25 op emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
90 81839fee 2021-07-25 op struct line *l;
91 81839fee 2021-07-25 op char buf[LINE_MAX], b[2] = {0};
93 81839fee 2021-07-25 op if ((l = calloc(1, sizeof(*l))) == NULL)
96 81839fee 2021-07-25 op if ((l->line = strdup(s->ds)) == NULL)
99 81839fee 2021-07-25 op switch (l->type = type) {
100 81839fee 2021-07-25 op case LINE_LINK:
101 81839fee 2021-07-25 op if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
102 81839fee 2021-07-25 op strlcpy(buf, s->selector+4, sizeof(buf));
104 81839fee 2021-07-25 op strlcpy(buf, "gopher://", sizeof(buf));
105 81839fee 2021-07-25 op strlcat(buf, s->addr, sizeof(buf));
106 81839fee 2021-07-25 op strlcat(buf, ":", sizeof(buf));
107 81839fee 2021-07-25 op strlcat(buf, s->port, sizeof(buf));
108 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
109 81839fee 2021-07-25 op b[0] = s->type;
110 81839fee 2021-07-25 op strlcat(buf, b, sizeof(buf));
111 81839fee 2021-07-25 op if (*s->selector != '/')
112 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
113 81839fee 2021-07-25 op strlcat(buf, s->selector, sizeof(buf));
116 81839fee 2021-07-25 op if ((l->alt = strdup(buf)) == NULL)
124 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
129 81839fee 2021-07-25 op if (l != NULL) {
130 81839fee 2021-07-25 op free(l->line);
131 81839fee 2021-07-25 op free(l->alt);
138 81839fee 2021-07-25 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
140 81839fee 2021-07-25 op char buf[LINE_MAX] = {0};
141 81839fee 2021-07-25 op struct gm_selector s = {0};
143 81839fee 2021-07-25 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
144 81839fee 2021-07-25 op gm_parse_selector(buf, &s);
146 81839fee 2021-07-25 op switch (s.type) {
147 81839fee 2021-07-25 op case '0': /* text file */
148 81839fee 2021-07-25 op case '1': /* gopher submenu */
149 81839fee 2021-07-25 op case '2': /* CCSO nameserver */
150 81839fee 2021-07-25 op case '4': /* binhex-encoded file */
151 81839fee 2021-07-25 op case '5': /* DOS file */
152 81839fee 2021-07-25 op case '6': /* uuencoded file */
153 81839fee 2021-07-25 op case '7': /* full-text search */
154 81839fee 2021-07-25 op case '8': /* telnet */
155 81839fee 2021-07-25 op case '9': /* binary file */
156 81839fee 2021-07-25 op case '+': /* mirror or alternate server */
157 81839fee 2021-07-25 op case 'g': /* gif */
158 81839fee 2021-07-25 op case 'I': /* image */
159 81839fee 2021-07-25 op case 'T': /* telnet 3270 */
160 81839fee 2021-07-25 op case ':': /* gopher+: bitmap image */
161 81839fee 2021-07-25 op case ';': /* gopher+: movie file */
162 81839fee 2021-07-25 op case 'd': /* non-canonical: doc */
163 81839fee 2021-07-25 op case 'h': /* non-canonical: html file */
164 81839fee 2021-07-25 op case 's': /* non-canonical: sound file */
165 81839fee 2021-07-25 op if (!emit_line(p, LINE_LINK, &s))
171 81839fee 2021-07-25 op case 'i': /* non-canonical: message */
172 81839fee 2021-07-25 op if (!emit_line(p, LINE_TEXT, &s))
176 81839fee 2021-07-25 op case '3': /* error code */
177 81839fee 2021-07-25 op if (!emit_line(p, LINE_QUOTE, &s))
186 81839fee 2021-07-25 op gm_free(struct parser *p)
188 81839fee 2021-07-25 op /* flush the buffer */
189 81839fee 2021-07-25 op if (p->len != 0)
190 81839fee 2021-07-25 op gm_foreach_line(p, p->buf, p->len);
192 81839fee 2021-07-25 op free(p->buf);
197 069c9a92 2022-01-19 op static inline const char *
198 069c9a92 2022-01-19 op gopher_skip_selector(const char *path, int *ret_type)
200 069c9a92 2022-01-19 op *ret_type = 0;
202 069c9a92 2022-01-19 op if (!strcmp(path, "/") || *path == '\0') {
203 069c9a92 2022-01-19 op *ret_type = '1';
207 069c9a92 2022-01-19 op if (*path != '/')
211 069c9a92 2022-01-19 op switch (*ret_type = *path) {
218 069c9a92 2022-01-19 op *ret_type = 0;
223 069c9a92 2022-01-19 op return ++path;
227 069c9a92 2022-01-19 op serialize_link(struct line *line, const char *text, struct evbuffer *evb)
229 069c9a92 2022-01-19 op size_t portlen = 0;
231 069c9a92 2022-01-19 op const char *uri, *endhost, *port, *path, *colon;
233 069c9a92 2022-01-19 op if ((uri = line->alt) == NULL)
236 069c9a92 2022-01-19 op if (!has_prefix(uri, "gopher://"))
237 069c9a92 2022-01-19 op return evap(evb, "h%s\tURL:%s\terror.host\t1\n",
238 069c9a92 2022-01-19 op text, line->alt);
240 069c9a92 2022-01-19 op uri += 9; /* skip gopher:// */
242 069c9a92 2022-01-19 op path = strchr(uri, '/');
243 069c9a92 2022-01-19 op colon = strchr(uri, ':');
245 069c9a92 2022-01-19 op if (path != NULL && colon > path)
246 069c9a92 2022-01-19 op colon = NULL;
248 069c9a92 2022-01-19 op if ((endhost = colon) == NULL &&
249 069c9a92 2022-01-19 op (endhost = path) == NULL)
250 069c9a92 2022-01-19 op endhost = strchr(path, '\0');
252 069c9a92 2022-01-19 op if (colon != NULL) {
253 069c9a92 2022-01-19 op for (port = colon+1; *port && *port != '/'; ++port)
255 069c9a92 2022-01-19 op port = colon+1;
261 069c9a92 2022-01-19 op if (path == NULL) {
265 069c9a92 2022-01-19 op path = gopher_skip_selector(path, &type);
267 069c9a92 2022-01-19 op return evap(evb, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
268 069c9a92 2022-01-19 op path, (int)(endhost - uri), uri, (int)portlen, port);
272 069c9a92 2022-01-19 op gm_serialize(struct parser *p, struct evbuffer *evb)
274 069c9a92 2022-01-19 op struct line *line;
275 069c9a92 2022-01-19 op const char *text;
278 069c9a92 2022-01-19 op TAILQ_FOREACH(line, &p->head, lines) {
279 069c9a92 2022-01-19 op if ((text = line->line) == NULL)
282 069c9a92 2022-01-19 op switch (line->type) {
283 069c9a92 2022-01-19 op case LINE_LINK:
284 069c9a92 2022-01-19 op r = serialize_link(line, text, evb);
287 069c9a92 2022-01-19 op case LINE_TEXT:
288 069c9a92 2022-01-19 op r = evap(evb, "i%s\t\terror.host\t1\n",
292 069c9a92 2022-01-19 op case LINE_QUOTE:
293 069c9a92 2022-01-19 op r = evap(evb, "3%s\t\terror.host\t1\n",
298 069c9a92 2022-01-19 op /* unreachable */