File: sfeed_gopher.c

package info (click to toggle)
sfeed 2.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 412 kB
  • sloc: ansic: 5,176; sh: 275; makefile: 126; xml: 58
file content (229 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "util.h"

static char *prefixpath = "/", *host = "127.0.0.1", *port = "70"; /* default */
static char *line;
static size_t linesize;
static time_t comparetime;
static struct feed *feeds;

/* Escape characters in Gopher, CR and LF are ignored */
static void
gophertext(FILE *fp, const char *s)
{
	for (; *s; s++) {
		switch (*s) {
		case '\r': /* ignore CR */
		case '\n': /* ignore LF */
			break;
		case '\t':
			fputs("        ", fp);
			break;
		default:
			putc(*s, fp);
			break;
		}
	}
}

static void
printfeed(FILE *fpitems, FILE *fpin, struct feed *f)
{
	struct uri u;
	char *fields[FieldLast];
	char *itemhost, *itemport, *itempath, *itemquery, *itemfragment;
	ssize_t linelen;
	unsigned int isnew;
	struct tm rtm, *tm;
	time_t parsedtime;
	int itemtype;

	if (f->name[0]) {
		fprintf(fpitems, "i%s\t\t%s\t%s\r\n", f->name, host, port);
		fprintf(fpitems, "i\t\t%s\t%s\r\n", host, port);
	}

	while ((linelen = getline(&line, &linesize, fpin)) > 0 &&
	       !ferror(fpitems)) {
		if (line[linelen - 1] == '\n')
			line[--linelen] = '\0';
		parseline(line, fields);

		itemhost = host;
		itemport = port;
		itemtype = 'i'; /* i type (extension): informational message */
		itempath = fields[FieldLink];
		itemquery = "";
		itemfragment = "";

		if (fields[FieldLink][0]) {
			itemtype = 'h'; /* h type (extension): HTML or external URL */
			/* if it is a Gopher URL then change it into a DirEntity */
			if (!strncmp(fields[FieldLink], "gopher://", 9) &&
			    uri_parse(fields[FieldLink], &u) != -1) {
				itemhost = u.host;
				itemport = u.port[0] ? u.port : "70";
				itemtype = '1'; /* directory */
				itempath = u.path;
				itemquery = u.query;
				itemfragment = u.fragment;
				/* use the Gopher type from the path if it is set */
				if (itempath[0] == '/') {
					itempath++;
					if (*itempath) {
						itemtype = *itempath;
						itempath++;
					}
				}
			}
		}

		parsedtime = 0;
		if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
		    (tm = localtime_r(&parsedtime, &rtm))) {
			isnew = (parsedtime >= comparetime) ? 1 : 0;
			f->totalnew += isnew;

			fprintf(fpitems, "%c%c %04d-%02d-%02d %02d:%02d ",
			        itemtype,
			        isnew ? 'N' : ' ',
			        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
			        tm->tm_hour, tm->tm_min);
		} else {
			fprintf(fpitems, "%c                   ", itemtype);
		}
		f->total++;

		gophertext(fpitems, fields[FieldTitle]);
		fputs("\t", fpitems);
		/* Prefix non-Gopher URLs with "URL:" using the h type.
		   This Gopher extension is commonly used to link to external URLs */
		if (itemtype == 'h' && fields[FieldLink] == itempath) {
			fputs("URL:", fpitems);
			gophertext(fpitems, fields[FieldLink]);
		} else {
			gophertext(fpitems, itempath);
			if (itemquery[0]) {
				fputs("?", fpitems);
				gophertext(fpitems, itemquery);
			}
			if (itemfragment[0]) {
				fputs("#", fpitems);
				gophertext(fpitems, itemfragment);
			}
		}
		fprintf(fpitems, "\t%s\t%s\r\n", itemhost, itemport);
	}
	fputs(".\r\n", fpitems);
}

int
main(int argc, char *argv[])
{
	struct feed *f;
	FILE *fpitems, *fpindex, *fp;
	char buf[64], *name, *p;
	size_t maxcountlen = 0, maxnamelen = 0, len;
	int i;

	if (argc <= 1) {
		if (pledge("stdio", NULL) == -1)
			err(1, "pledge");
	} else {
		if (unveil("/", "r") == -1)
			err(1, "unveil: /");
		if (unveil(".", "rwc") == -1)
			err(1, "unveil: .");
		if (pledge("stdio rpath wpath cpath", NULL) == -1)
			err(1, "pledge");

		setlocale(LC_CTYPE, "");
	}

	if ((comparetime = getcomparetime()) == (time_t)-1)
		errx(1, "getcomparetime");

	if ((p = getenv("SFEED_GOPHER_HOST")))
		host = p;
	if ((p = getenv("SFEED_GOPHER_PORT")))
		port = p;
	if ((p = getenv("SFEED_GOPHER_PATH")))
		prefixpath = p;

	if (!(feeds = calloc(argc <= 1 ? 1 : argc, sizeof(struct feed))))
		err(1, "calloc");

	if (argc <= 1) {
		feeds[0].name = "";
		printfeed(stdout, stdin, &feeds[0]);
		checkfileerror(stdin, "<stdin>", 'r');
		checkfileerror(stdout, "<stdout>", 'w');
	} else {
		for (i = 1; i < argc; i++) {
			name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
			f = &feeds[i - 1];
			f->name = name;

			if (!(fp = fopen(argv[i], "r")))
				err(1, "fopen: %s", argv[i]);
			if (!(fpitems = fopen(name, "wb")))
				err(1, "fopen");
			printfeed(fpitems, fp, f);
			checkfileerror(fp, argv[i], 'r');
			checkfileerror(fpitems, name, 'w');
			fclose(fp);
			fclose(fpitems);

			/* count max length: used for aligning the feed names */
			len = colw(name);
			if (len > maxnamelen)
				maxnamelen = len;

			/* count max length: used for aligning the feed counts to the right */
			len = snprintf(NULL, 0, " (%lu/%lu)", f->totalnew, f->total);
			if (len > maxcountlen)
				maxcountlen = len;
		}
	}

	/* write index file */
	if (argc > 1) {
		if (!(fpindex = fopen("index", "wb")))
			err(1, "fopen: index");

		for (i = 0; i < argc - 1; i++) {
			f = &feeds[i];

			/* append directory item to index */
			fputs("1", fpindex);
			fputs(f->totalnew ? "N " : "  ", fpindex);

			/* left align feed names and pad with spaces */
			len = colw(f->name);
			gophertext(fpindex, f->name);
			for (; len < maxnamelen; len++)
				fputs(" ", fpindex);

			/* right align the item counts by padding with spaces */
			snprintf(buf, sizeof(buf), " (%lu/%lu)", f->totalnew, f->total);
			len = strlen(buf);
			for (; len < maxcountlen; len++)
				fputs(" ", fpindex);
			fprintf(fpindex, "%s\t", buf);

			gophertext(fpindex, prefixpath);
			gophertext(fpindex, f->name);
			fprintf(fpindex, "\t%s\t%s\r\n", host, port);
		}
		fputs(".\r\n", fpindex);
		checkfileerror(fpindex, "index", 'w');
		fclose(fpindex);
	}

	return 0;
}