2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
26 #define evap evbuffer_add_printf
36 static void gm_parse_selector(char *, struct gm_selector *);
38 static int gm_parse(struct parser *, const char *, size_t);
39 static int gm_foreach_line(struct parser *, const char *, size_t);
40 static int gm_free(struct parser *);
41 static int gm_serialize(struct parser *, struct evbuffer *);
44 gophermap_initparser(struct parser *p)
46 memset(p, 0, sizeof(*p));
48 p->name = "gophermap";
51 p->serialize = &gm_serialize;
57 gm_parse_selector(char *line, struct gm_selector *s)
65 if ((line = strchr(line, '\t')) == NULL)
70 if ((line = strchr(line, '\t')) == NULL)
75 if ((line = strchr(line, '\t')) == NULL)
82 gm_parse(struct parser *p, const char *buf, size_t size)
84 return parser_foreach_line(p, buf, size, gm_foreach_line);
88 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
91 char buf[LINE_MAX], b[2] = {0};
93 if ((l = calloc(1, sizeof(*l))) == NULL)
96 if ((l->line = strdup(s->ds)) == NULL)
99 switch (l->type = type) {
101 if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
102 strlcpy(buf, s->selector+4, sizeof(buf));
104 strlcpy(buf, "gopher://", sizeof(buf));
105 strlcat(buf, s->addr, sizeof(buf));
106 strlcat(buf, ":", sizeof(buf));
107 strlcat(buf, s->port, sizeof(buf));
108 strlcat(buf, "/", sizeof(buf));
110 strlcat(buf, b, sizeof(buf));
111 if (*s->selector != '/')
112 strlcat(buf, "/", sizeof(buf));
113 strlcat(buf, s->selector, sizeof(buf));
116 if ((l->alt = strdup(buf)) == NULL)
124 TAILQ_INSERT_TAIL(&p->head, l, lines);
138 gm_foreach_line(struct parser *p, const char *line, size_t linelen)
140 char buf[LINE_MAX] = {0};
141 struct gm_selector s = {0};
143 memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
144 gm_parse_selector(buf, &s);
147 case '0': /* text file */
148 case '1': /* gopher submenu */
149 case '2': /* CCSO nameserver */
150 case '4': /* binhex-encoded file */
151 case '5': /* DOS file */
152 case '6': /* uuencoded file */
153 case '7': /* full-text search */
154 case '8': /* telnet */
155 case '9': /* binary file */
156 case '+': /* mirror or alternate server */
158 case 'I': /* image */
159 case 'T': /* telnet 3270 */
160 case ':': /* gopher+: bitmap image */
161 case ';': /* gopher+: movie file */
162 case 'd': /* non-canonical: doc */
163 case 'h': /* non-canonical: html file */
164 case 's': /* non-canonical: sound file */
165 if (!emit_line(p, LINE_LINK, &s))
171 case 'i': /* non-canonical: message */
172 if (!emit_line(p, LINE_TEXT, &s))
176 case '3': /* error code */
177 if (!emit_line(p, LINE_QUOTE, &s))
186 gm_free(struct parser *p)
188 /* flush the buffer */
190 gm_foreach_line(p, p->buf, p->len);
197 static inline const char *
198 gopher_skip_selector(const char *path, int *ret_type)
202 if (!strcmp(path, "/") || *path == '\0') {
211 switch (*ret_type = *path) {
227 serialize_link(struct line *line, const char *text, struct evbuffer *evb)
231 const char *uri, *endhost, *port, *path, *colon;
233 if ((uri = line->alt) == NULL)
236 if (!has_prefix(uri, "gopher://"))
237 return evap(evb, "h%s\tURL:%s\terror.host\t1\n",
240 uri += 9; /* skip gopher:// */
242 path = strchr(uri, '/');
243 colon = strchr(uri, ':');
245 if (path != NULL && colon > path)
248 if ((endhost = colon) == NULL &&
249 (endhost = path) == NULL)
250 endhost = strchr(path, '\0');
253 for (port = colon+1; *port && *port != '/'; ++port)
265 path = gopher_skip_selector(path, &type);
267 return evap(evb, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
268 path, (int)(endhost - uri), uri, (int)portlen, port);
272 gm_serialize(struct parser *p, struct evbuffer *evb)
278 TAILQ_FOREACH(line, &p->head, lines) {
279 if ((text = line->line) == NULL)
282 switch (line->type) {
284 r = serialize_link(line, text, evb);
288 r = evap(evb, "i%s\t\terror.host\t1\n",
293 r = evap(evb, "3%s\t\terror.host\t1\n",