summaryrefslogtreecommitdiff
path: root/sfeed_frames.c
blob: 6fd45ae2b1e9a8a482ca5305b9bfeb7a90edd7f7 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#include <errno.h>

#include "util.h"

static unsigned int showsidebar = 1; /* show sidebar ? */

static FILE *fpindex = NULL, *fpitems = NULL, *fpmenu = NULL, *fpcontent = NULL;
static char *line = NULL;
static struct feed *feeds = NULL; /* start of feeds linked-list. */

static void /* print error message to stderr */
die(const char *s) {
	fputs("sfeed_frames: ", stderr);
	fputs(s, stderr);
	fputc('\n', stderr);
	exit(EXIT_FAILURE);
}

static void
cleanup(void) {
	if(fpmenu)
		fclose(fpmenu);
	if(fpitems)
		fclose(fpitems);
	if(fpindex)
		fclose(fpindex);
	if(fpcontent)
		fclose(fpcontent);
	free(line); /* free line */
	feedsfree(feeds); /* free feeds linked-list */
}

/* print text, ignore tabs, newline and carriage return etc
 * print some HTML 2.0 / XML 1.0 as normal text */
static void
printcontent(const char *s, FILE *fp) {
	const char *p;
	int len = 0;
	for(p = s; *p; p++) {
		if(*p == '\\') {
			p++;
			if(*p == '\\')
				fputc('\\', fp);
			else if(*p == 't' && len)
				fputc('\t', fp);
			else if(*p == 'n' && len)
				fputc('\n', fp);
		} else {
			fputc(*p, fp);
			len++;
		}
	}
}

/* TODO: bufsiz - 1 ? */
static size_t
makepathname(char *buffer, size_t bufsiz, const char *path) {
	const char *p = path;
	size_t i = 0, r = 0;
	for(; *p && i < bufsiz; p++) {
		if(isalpha((int)*p) || isdigit((int)*p)) {
			buffer[i++] = tolower((int)*p);
			r = 0;
		} else {
			if(!r) /* don't repeat '-'. */
				buffer[i++] = '-';
			r++;
		}
	}
	buffer[i] = '\0';
	/* remove trailing - */
	for(; i > 0 && (buffer[i] == '-' || buffer[i] == '\0'); i--)
		buffer[i] = '\0';
	return i;
}

static int
fileexists(const char *path) {
	return (!access(path, F_OK));
}

int
main(int argc, char **argv) {
	char *fields[FieldLast];
	char name[256]; /* TODO: bigger size? */
	char *basepath = ".";
	 /* TODO: max path size? */
	char dirpath[1024], filepath[1024], reldirpath[1024], relfilepath[1024];
	unsigned long totalfeeds = 0, totalnew = 0;
	unsigned int isnew;
	struct feed *f, *feedcurrent = NULL;
	time_t parsedtime, comparetime;
	size_t size = 0, namelen = 0, basepathlen = 0;
	struct stat st;
	struct utimbuf contenttime;

	atexit(cleanup);
	memset(&contenttime, 0, sizeof(contenttime));

	if(argc > 1 && argv[1][0] != '\0')
		basepath = argv[1];

	comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */
	basepathlen = strlen(basepath);
	if(basepathlen > 0)
		mkdir(basepath, S_IRWXU);

	/* write main index page */
	if(basepathlen + strlen("/index.html") < sizeof(dirpath) - 1)
		sprintf(dirpath, "%s/index.html", basepath);
	if(!(fpindex = fopen(dirpath, "w+b")))
		die("can't write index.html");
	if(basepathlen + strlen("/menu.html") < sizeof(dirpath) - 1)
		sprintf(dirpath, "%s/menu.html", basepath);
	if(!(fpmenu = fopen(dirpath, "w+b")))
		die("can't write menu.html");
	if(basepathlen + strlen("/items.html") < sizeof(dirpath) - 1)
		sprintf(dirpath, "%s/items.html", basepath);
	if(!(fpitems = fopen(dirpath, "w+b")))
		die("can't write items.html");
	fputs("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\" /></head>"
	      "<body class=\"frame\"><div id=\"items\">", fpitems);
	while(parseline(&line, &size, fields, FieldLast, '\t', stdin) > 0) {


/*

		dirpath[0] = '\0';
		filepath[0] = '\0';
		reldirpath[0] = '\0';
		relfilepath[0] = '\0';


*/


		/* first of feed section or new feed section. */
		if(!totalfeeds || (feedcurrent && strcmp(feedcurrent->name, fields[FieldFeedName]))) {


			/* TODO: makepathname isnt necesary if fields[FieldFeedName] is the same as the previous line */
			/* TODO: move this part below where FieldFeedName is checked if its different ? */

			/* make directory for feedname */
			if(!(namelen = makepathname(name, sizeof(name) - 1, fields[FieldFeedName])))
				continue;

			if(basepathlen + namelen + 1 < sizeof(dirpath) - 1)
				sprintf(dirpath, "%s/%s", basepath, name);
			/* TODO: handle error. */
			if(stat(dirpath, &st) == -1) {
				if(mkdir(dirpath, S_IRWXU) == -1) {
					fprintf(stderr, "sfeed_frames: can't make directory '%s': %s\n", dirpath, strerror(errno));
					exit(EXIT_FAILURE);
				}
			}
			/* TODO: test, replaces strncpy (strncpy is slow) */
			reldirpath[0] = '\0';
			if(namelen < sizeof(reldirpath) - 2) {
				memcpy(reldirpath, name, namelen + 1); /* copy including nul byte */
	/*			reldirpath[namelen] = '\0';*/
			}
	/*		strncpy(reldirpath, name, sizeof(reldirpath) - 1);*/





			if(!(f = calloc(1, sizeof(struct feed))))
				die("can't allocate enough memory");



			if(totalfeeds) { /* end previous one. */
				fputs("</table>\n", fpitems);


				feedcurrent->next = f;
				feedcurrent = feedcurrent->next;



			} else {
				/* first item. */
				feedcurrent = f;

				feeds = feedcurrent;
				/* assume single feed (hide sidebar) */
				if(fields[FieldFeedName][0] == '\0')
					showsidebar = 0;
			}
			/* write menu link if new. */
			if(!(feedcurrent->name = strdup(fields[FieldFeedName])))
				die("can't allocate enough memory");
			if(fields[FieldFeedName][0] != '\0') {
				fputs("<h2 id=\"", fpitems);
				printfeednameid(feedcurrent->name, fpitems);
				fputs("\"><a href=\"#", fpitems);
				printfeednameid(feedcurrent->name, fpitems);
				fputs("\">", fpitems);
				fputs(feedcurrent->name, fpitems);
				fputs("</a></h2>\n", fpitems);
			}
			fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", fpitems);
			totalfeeds++;
		}

		/* write content */
		if(!(namelen = makepathname(name, sizeof(name), fields[FieldTitle])))
			continue;
		if(strlen(dirpath) + namelen + strlen("/.html") < sizeof(filepath) - 1)
			sprintf(filepath, "%s/%s.html", dirpath, name);
		if(strlen(reldirpath) + namelen + strlen("/.html") < sizeof(relfilepath) - 1)
			sprintf(relfilepath, "%s/%s.html", reldirpath, name);
		if(!fileexists(filepath) && (fpcontent = fopen(filepath, "w+b"))) {
			fputs("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"../../style.css\" /></head>"
			      "<body class=\"frame\"><div class=\"content\">"
			      "<h2><a href=\"", fpcontent);
			if(fields[FieldBaseSiteUrl][0] != '\0')
				printlink(fields[FieldLink], fields[FieldBaseSiteUrl], fpcontent);
			else
				printlink(fields[FieldLink], fields[FieldFeedUrl], fpcontent);
			fputs("\">", fpcontent);
			printhtmlencoded(fields[FieldTitle], fpcontent);
			fputs("</a></h2>", fpcontent);
			printcontent(fields[FieldContent], fpcontent);
			fputs("</div></body></html>", fpcontent);
			fclose(fpcontent);
			fpcontent = NULL;
		}

		/* write item. */
		parsedtime = (time_t)strtol(fields[FieldUnixTimestamp], NULL, 10);
		/* set modified and access time of file to time of item. */
		contenttime.actime = parsedtime;
		contenttime.modtime = parsedtime;
		utime(filepath, &contenttime);

		isnew = (parsedtime >= comparetime);
		totalnew += isnew;
		feedcurrent->totalnew += isnew;
		feedcurrent->total++;
		if(isnew)
			fputs("<tr class=\"n\">", fpitems);
		else
			fputs("<tr>", fpitems);
		fputs("<td nowrap valign=\"top\">", fpitems);
		fputs(fields[FieldTimeFormatted], fpitems);
		fputs("</td><td nowrap valign=\"top\">", fpitems);
		if(isnew)
			fputs("<b><u>", fpitems);
		fputs("<a href=\"", fpitems);
		fputs(relfilepath, fpitems);
		fputs("\" target=\"content\">", fpitems);
		printhtmlencoded(fields[FieldTitle], fpitems);
		fputs("</a>", fpitems);
		if(isnew)
			fputs("</u></b>", fpitems);
		fputs("</td></tr>\n", fpitems);
	}
	if(totalfeeds) {
		fputs("</table>\n", fpitems);
	}
	fputs("\n</div></body>\n</html>", fpitems); /* div items */
	if(showsidebar) {
		fputs("<html><head>"
		      "<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\" />"
		      "</head><body class=\"frame\"><div id=\"sidebar\">", fpmenu);
		for(feedcurrent = feeds; feedcurrent; feedcurrent = feedcurrent->next) {
			if(!feedcurrent->name || feedcurrent->name[0] == '\0')
				continue;
			if(feedcurrent->totalnew)
				fputs("<a class=\"n\" href=\"items.html#", fpmenu);
			else
				fputs("<a href=\"items.html#", fpmenu);
			printfeednameid(feedcurrent->name, fpmenu);
			fputs("\" target=\"items\">", fpmenu);
			if(feedcurrent->totalnew > 0)
				fputs("<b><u>", fpmenu);
			fputs(feedcurrent->name, fpmenu);
			fprintf(fpmenu, " (%lu)", feedcurrent->totalnew);
			if(feedcurrent->totalnew > 0)
				fputs("</u></b>", fpmenu);
			fputs("</a><br/>\n", fpmenu);
		}
		fputs("</div></body></html>", fpmenu);
	}
	fputs("<!DOCTYPE html><html><head>\n\t<title>Newsfeed (", fpindex);
	fprintf(fpindex, "%lu", totalnew);
	fputs(")</title>\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\" />\n"
	      "</head>\n", fpindex);
	if(showsidebar) {
		fputs(
			"<frameset framespacing=\"0\" cols=\"200,*\" frameborder=\"1\">\n"
			"	<frame name=\"menu\" src=\"menu.html\" target=\"menu\">\n", fpindex);
	} else {
		fputs("<frameset framespacing=\"0\" cols=\"*\" frameborder=\"1\">\n", fpindex);
	}
	fputs(
		"	<frameset id=\"frameset\" framespacing=\"0\" cols=\"50%,50%\" frameborder=\"1\">\n"
		"		<frame name=\"items\" src=\"items.html\" target=\"items\">\n"
		"		<frame name=\"content\" target=\"content\">\n"
		"	</frameset>\n"
		"</frameset>\n"
		"</html>", fpindex);

	return EXIT_SUCCESS;
}