2 62c0f697 2024-02-21 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
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.
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.
17 1ac119fb 2024-01-23 op #include "compat.h"
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>
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"
29 e5e04904 2024-02-06 op #ifndef LINE_MAX
30 e5e04904 2024-02-06 op #define LINE_MAX 2048
33 1ac119fb 2024-01-23 op struct gm_selector {
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;
41 1ac119fb 2024-01-23 op static void gm_parse_selector(char *, struct gm_selector *);
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 *);
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,
53 1ac119fb 2024-01-23 op gm_parse_selector(char *line, struct gm_selector *s)
55 1ac119fb 2024-01-23 op s->type = *line++;
57 1ac119fb 2024-01-23 op s->selector = "";
61 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
63 1ac119fb 2024-01-23 op *line++ = '\0';
64 1ac119fb 2024-01-23 op s->selector = line;
66 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
68 1ac119fb 2024-01-23 op *line++ = '\0';
69 1ac119fb 2024-01-23 op s->addr = line;
71 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
73 1ac119fb 2024-01-23 op *line++ = '\0';
74 1ac119fb 2024-01-23 op s->port = line;
78 62c0f697 2024-02-21 op selector2uri(struct gm_selector *s, char *buf, size_t len)
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)
89 62c0f697 2024-02-21 op return (iri_urlescape(s->selector, buf, len));
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)
95 1ac119fb 2024-01-23 op struct line *l;
96 62c0f697 2024-02-21 op char buf[LINE_MAX];
98 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
100 3d89457c 2024-06-18 thomas.ad l->line = xstrdup(s->ds);
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)
109 3d89457c 2024-06-18 thomas.ad l->alt = xstrdup(buf);
116 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
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);
130 c1d27b0e 2024-06-14 op gm_parse_line(struct buffer *b, const char *line, size_t linelen)
132 1ac119fb 2024-01-23 op char buf[LINE_MAX] = {0};
133 1ac119fb 2024-01-23 op struct gm_selector s = {0};
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);
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))
161 1ac119fb 2024-01-23 op case 'i': /* non-canonical: message */
162 c1d27b0e 2024-06-14 op if (!emit_line(b, LINE_TEXT, &s))
166 1ac119fb 2024-01-23 op case '3': /* error code */
167 c1d27b0e 2024-06-14 op if (!emit_line(b, LINE_QUOTE, &s))
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)
178 1ac119fb 2024-01-23 op *ret_type = 0;
180 1ac119fb 2024-01-23 op if (!strcmp(path, "/") || *path == '\0') {
181 1ac119fb 2024-01-23 op *ret_type = '1';
185 1ac119fb 2024-01-23 op if (*path != '/')
189 1ac119fb 2024-01-23 op switch (*ret_type = *path) {
196 1ac119fb 2024-01-23 op *ret_type = 0;
201 1ac119fb 2024-01-23 op return ++path;
205 1ac119fb 2024-01-23 op serialize_link(struct line *line, const char *text, FILE *fp)
207 1ac119fb 2024-01-23 op size_t portlen = 0;
209 1ac119fb 2024-01-23 op const char *uri, *endhost, *port, *path, *colon;
211 1ac119fb 2024-01-23 op if ((uri = line->alt) == NULL)
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);
218 1ac119fb 2024-01-23 op uri += 9; /* skip gopher:// */
220 1ac119fb 2024-01-23 op path = strchr(uri, '/');
221 1ac119fb 2024-01-23 op colon = strchr(uri, ':');
223 1ac119fb 2024-01-23 op if (path != NULL && colon > path)
224 1ac119fb 2024-01-23 op colon = NULL;
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');
230 1ac119fb 2024-01-23 op if (colon != NULL) {
231 1ac119fb 2024-01-23 op for (port = colon+1; *port && *port != '/'; ++port)
233 1ac119fb 2024-01-23 op port = colon+1;
239 1ac119fb 2024-01-23 op if (path == NULL) {
243 1ac119fb 2024-01-23 op path = gopher_skip_selector(path, &type);
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);
250 c1d27b0e 2024-06-14 op gm_serialize(struct buffer *b, FILE *fp)
252 1ac119fb 2024-01-23 op struct line *line;
253 1ac119fb 2024-01-23 op const char *text;
256 c1d27b0e 2024-06-14 op TAILQ_FOREACH(line, &b->head, lines) {
257 1ac119fb 2024-01-23 op if ((text = line->line) == NULL)
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);
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);
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);
274 1ac119fb 2024-01-23 op /* unreachable */