Blob


1 /*
2 * Copyright (c) 2021 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 #include "compat.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "parser.h"
24 #include "utils.h"
26 #define evap evbuffer_add_printf
28 struct gm_selector {
29 char type;
30 const char *ds;
31 const char *selector;
32 const char *addr;
33 const char *port;
34 };
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 *);
43 void
44 gophermap_initparser(struct parser *p)
45 {
46 memset(p, 0, sizeof(*p));
48 p->name = "gophermap";
49 p->parse = &gm_parse;
50 p->free = &gm_free;
51 p->serialize = &gm_serialize;
53 TAILQ_INIT(&p->head);
54 }
56 static void
57 gm_parse_selector(char *line, struct gm_selector *s)
58 {
59 s->type = *line++;
60 s->ds = line;
61 s->selector = "";
62 s->addr = "";
63 s->port = "";
65 if ((line = strchr(line, '\t')) == NULL)
66 return;
67 *line++ = '\0';
68 s->selector = line;
70 if ((line = strchr(line, '\t')) == NULL)
71 return;
72 *line++ = '\0';
73 s->addr = line;
75 if ((line = strchr(line, '\t')) == NULL)
76 return;
77 *line++ = '\0';
78 s->port = line;
79 }
81 static int
82 gm_parse(struct parser *p, const char *buf, size_t size)
83 {
84 return parser_foreach_line(p, buf, size, gm_foreach_line);
85 }
87 static inline int
88 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
89 {
90 struct line *l;
91 char buf[LINE_MAX], b[2] = {0};
93 if ((l = calloc(1, sizeof(*l))) == NULL)
94 goto err;
96 if ((l->line = strdup(s->ds)) == NULL)
97 goto err;
99 switch (l->type = type) {
100 case LINE_LINK:
101 if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
102 strlcpy(buf, s->selector+4, sizeof(buf));
103 } else {
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));
109 b[0] = s->type;
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)
117 goto err;
118 break;
120 default:
121 break;
124 TAILQ_INSERT_TAIL(&p->head, l, lines);
126 return 1;
128 err:
129 if (l != NULL) {
130 free(l->line);
131 free(l->alt);
132 free(l);
134 return 0;
137 static int
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);
146 switch (s.type) {
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 */
157 case 'g': /* gif */
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))
166 return 0;
167 break;
169 break;
171 case 'i': /* non-canonical: message */
172 if (!emit_line(p, LINE_TEXT, &s))
173 return 0;
174 break;
176 case '3': /* error code */
177 if (!emit_line(p, LINE_QUOTE, &s))
178 return 0;
179 break;
182 return 1;
185 static int
186 gm_free(struct parser *p)
188 /* flush the buffer */
189 if (p->len != 0)
190 gm_foreach_line(p, p->buf, p->len);
192 free(p->buf);
194 return 1;
197 static inline const char *
198 gopher_skip_selector(const char *path, int *ret_type)
200 *ret_type = 0;
202 if (!strcmp(path, "/") || *path == '\0') {
203 *ret_type = '1';
204 return path;
207 if (*path != '/')
208 return path;
209 path++;
211 switch (*ret_type = *path) {
212 case '0':
213 case '1':
214 case '7':
215 break;
217 default:
218 *ret_type = 0;
219 path -= 1;
220 return path;
223 return ++path;
226 static int
227 serialize_link(struct line *line, const char *text, struct evbuffer *evb)
229 size_t portlen = 0;
230 int type;
231 const char *uri, *endhost, *port, *path, *colon;
233 if ((uri = line->alt) == NULL)
234 return -1;
236 if (!has_prefix(uri, "gopher://"))
237 return evap(evb, "h%s\tURL:%s\terror.host\t1\n",
238 text, line->alt);
240 uri += 9; /* skip gopher:// */
242 path = strchr(uri, '/');
243 colon = strchr(uri, ':');
245 if (path != NULL && colon > path)
246 colon = NULL;
248 if ((endhost = colon) == NULL &&
249 (endhost = path) == NULL)
250 endhost = strchr(path, '\0');
252 if (colon != NULL) {
253 for (port = colon+1; *port && *port != '/'; ++port)
254 ++portlen;
255 port = colon+1;
256 } else {
257 port = "70";
258 portlen = 2;
261 if (path == NULL) {
262 type = '1';
263 path = "";
264 } else
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);
271 static int
272 gm_serialize(struct parser *p, struct evbuffer *evb)
274 struct line *line;
275 const char *text;
276 int r;
278 TAILQ_FOREACH(line, &p->head, lines) {
279 if ((text = line->line) == NULL)
280 text = "";
282 switch (line->type) {
283 case LINE_LINK:
284 r = serialize_link(line, text, evb);
285 break;
287 case LINE_TEXT:
288 r = evap(evb, "i%s\t\terror.host\t1\n",
289 text);
290 break;
292 case LINE_QUOTE:
293 r = evap(evb, "3%s\t\terror.host\t1\n",
294 text);
295 break;
297 default:
298 /* unreachable */
299 abort();
302 if (r == -1)
303 return 0;
306 return 1;