about summary refs log tree commit diff
path: root/html.c
blob: ee9e9663535fb0533d073e14c15c6febb80da89f (plain) (blame)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>

#include "archive.h"

static int htmlAddress(FILE *file, const char *class, struct Address addr) {
	const char *template;
	if (addr.host) {
		template = TEMPLATE(
			<li><address class="[class]">[name]</address></li>
		);
	} else if (addr.mailbox) {
		template = TEMPLATE(
			<li class="group">
				<address class="[class]">[mailbox]</address>
				<ul>
		);
	} else {
		template = TEMPLATE(
				</ul>
			</li>
		);
	}
	struct Variable vars[] = {
		{ "class", class },
		{ "name", addressName(addr) },
		{ "mailbox", addr.mailbox },
		{0},
	};
	return templateRender(file, template, vars, escapeXML);
}

static int
htmlAddressList(FILE *file, const char *class, struct AddressList list) {
	if (!list.len) return 0;
	const char *template = TEMPLATE(
		<ul class="recipient [class]">
	);
	struct Variable vars[] = {
		{ "class", class },
		{0},
	};
	int error = templateRender(file, template, vars, escapeXML);
	if (error) return error;
	for (size_t i = 0; i < list.len; ++i) {
		error = htmlAddress(file, class, list.addrs[i]);
		if (error) return error;
	}
	return templateRender(file, TEMPLATE(</ul>), NULL, NULL);
}

static char *htmlReply(const struct Envelope *envelope) {
	const char *template = {
		"mailto:[mailbox]@[host]"
		"?cc=[cc]"
		"&subject=[re][subject]"
		"&In-Reply-To=[<][messageID][>]"
	};
	struct Variable vars[] = {
		{ "mailbox", envelope->replyTo.mailbox },
		{ "host", envelope->replyTo.host },
		{ "cc", baseAddress },
		{ "re", (strncmp(envelope->subject, "Re: ", 4) ? "Re: " : "") },
		{ "subject", envelope->subject },
		{ "messageID", envelope->messageID },
		{ "<", "<" },
		{ ">", ">" },
		{0},
	};
	return templateURL(template, vars);
}

static char *htmlFragment(const char *messageID) {
	struct Variable vars[] = {
		{ "messageID", messageID },
		{0},
	};
	return templateURL("#[messageID]", vars);
}

static char *htmlMbox(const char *messageID) {
	struct Variable vars[] = {
		{ "messageID", messageID },
		{ "type", "mbox" },
		{0},
	};
	return templateURL("../" PATH_MESSAGE, vars);
}

int htmlMessageNav(FILE *file, const struct Envelope *envelope) {
	int error = templateRender(file, TEMPLATE(<nav><ul>), NULL, NULL);
	if (error) return error;
	if (envelope->inReplyTo) {
		const char *template = TEMPLATE(
			<li><a href="[fragment]">parent</a></li>
		);
		char *fragment = htmlFragment(envelope->inReplyTo);
		struct Variable vars[] = {
			{ "fragment", fragment },
			{0},
		};
		error = templateRender(file, template, vars, escapeXML);
		free(fragment);
		if (error) return error;
	}
	const char *template = TEMPLATE(
		<li><a href="[mbox]">download</a></li>
	);
	char *mbox = htmlMbox(envelope->messageID);
	struct Variable vars[] = {
		{ "mbox", mbox },
		{0},
	};
	error = templateRender(file, template, vars, escapeXML);
	free(mbox);
	if (error) return error;
	return templateRender(file, TEMPLATE(</ul></nav>), NULL, NULL);
}

static const char *htmlUTC(time_t time) {
	static char buf[sizeof("0000-00-00T00:00:00Z")];
	strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&time));
	return buf;
}

int htmlMessageOpen(FILE *file, const struct Envelope *envelope) {
	const char *template = TEMPLATE(
		<article class="message" id="[messageID]">
		<header>
			<h2 class="subject"><a href="[fragment]">[subject]</a></h2>
			<address class="from">
				<a href="[reply]">[from]</a>
			</address>
			<time datetime="[utc]">[date]</time>
	);
	char *fragment = htmlFragment(envelope->messageID);
	char *reply = htmlReply(envelope);
	struct Variable vars[] = {
		{ "messageID", envelope->messageID },
		{ "fragment", fragment },
		{ "subject", envelope->subject },
		{ "reply", reply },
		{ "from", addressName(envelope->from) },
		{ "utc", htmlUTC(envelope->time) },
		{ "date", envelope->date },
		{0},
	};
	int error = 0
		|| templateRender(file, template, vars, escapeXML)
		|| htmlAddressList(file, "to", envelope->to)
		|| htmlAddressList(file, "cc", envelope->cc)
		|| htmlMessageNav(file, envelope)
		|| templateRender(file, TEMPLATE(</header>), NULL, NULL);
	free(reply);
	free(fragment);
	return error;
}

static int htmlInlineAttrs(FILE *file, const struct BodyPart *part) {
	const char *template = " " TEMPLATE([attr]="[value]");
	if (part->contentID) {
		struct Variable vars[] = {
			{ "attr", "id" },
			{ "value", part->contentID },
			{0},
		};
		int error = templateRender(file, template, vars, escapeXML);
		if (error) return error;
	}
	if (part->description) {
		struct Variable vars[] = {
			{ "attr", "title" },
			{ "value", part->description },
			{0},
		};
		int error = templateRender(file, template, vars, escapeXML);
		if (error) return error;
	}
	const char *language = NULL;
	if (part->language.type == String) language = part->language.string;
	if (part->language.type == List && part->language.list.len == 1) {
		language = dataCheck(part->language.list.ptr[0], String).string;
	}
	if (language) {
		struct Variable vars[] = {
			{ "attr", "lang" },
			{ "value", language },
			{0},
		};
		int error = templateRender(file, template, vars, escapeXML);
		if (error) return error;
	}
	return 0;
}

static void swap(char *a, char *b) {
	char ch = *a;
	*a = *b;
	*b = ch;
}

static int htmlMarkupURLs(FILE *file, char *buf) {
	static const char *Pattern = "(^|[[:space:]<])(https?:[^[:space:]>]+)(.|$)";
	static regex_t regex;
	if (!regex.re_nsub) {
		int error = regcomp(&regex, Pattern, REG_EXTENDED);
		assert(!error);
	}

	int error;
	char *ptr;
	regmatch_t match[4];
	for (ptr = buf; !regexec(&regex, ptr, 4, match, 0); ptr += match[2].rm_eo) {
		char nul = '\0';

		swap(&ptr[match[2].rm_so], &nul);
		error = escapeXML(file, ptr);
		if (error) return error;
		swap(&ptr[match[2].rm_so], &nul);

		const char *template = TEMPLATE(<a href="[url]">[url]</a>);
		swap(&ptr[match[3].rm_so], &nul);
		struct Variable vars[] = {
			{ "url", &ptr[match[2].rm_so] },
			{0},
		};
		error = templateRender(file, template, vars, escapeXML);
		if (error) return error;
		swap(&ptr[match[3].rm_so], &nul);
	}
	return escapeXML(file, ptr);
}

static int htmlMarkup(FILE *file, const char *content) {
	int error = 0;
	size_t cap = 0;
	char *buf = NULL;
	bool patch = false;
	for (const char *nl; (nl = strchr(content, '\n')); content = &nl[1]) {
		size_t len = nl - content;
		if (cap < len + 1) {
			cap = len + 1;
			buf = realloc(buf, cap);
			if (!buf) err(EX_OSERR, "realloc");
		}
		memcpy(buf, content, len);
		buf[len] = '\0';
		struct Variable vars[] = {
			{ "line", buf },
			{0},
		};

		if (!strcmp(buf, "---")) {
			patch = true;
		} else if (patch && !strcmp(buf, "-- ")) {
			patch = false;
		}

		static const char *Pattern = "^(diff|index|---|[+]{3}|@@) ";
		static regex_t regex;
		if (!regex.re_nsub) {
			error = regcomp(&regex, Pattern, REG_EXTENDED);
			assert(!error);
		}
		if (patch && !regexec(&regex, buf, 0, NULL, 0)) {
			error = templateRender(
				file, TEMPLATE(<b>[line]</b>), vars, escapeXML
			);
		} else if (patch && buf[0] == '-' && strcmp(buf, "---")) {
			error = templateRender(
				file, TEMPLATE(<del>[line]</del>), vars, escapeXML
			);
		} else if (patch && buf[0] == '+') {
			error = templateRender(
				file, TEMPLATE(<ins>[line]</ins>), vars, escapeXML
			);
		} else if (patch) {
			error = escapeXML(file, buf);
		} else if (buf[0] == '>') {
			error = 0
				|| templateRender(file, TEMPLATE(<q>), NULL, NULL)
				|| htmlMarkupURLs(file, buf)
				|| templateRender(file, TEMPLATE(</q>), NULL, NULL);
		} else {
			error = htmlMarkupURLs(file, buf);
		}
		if (error) break;

		error = templateRender(file, "\n", NULL, NULL);
		if (error) break;
	}
	free(buf);
	return error;
}

int htmlInline(FILE *file, const struct BodyPart *part, const char *content) {
	return 0
		|| templateRender(file, TEMPLATE(<pre), NULL, NULL)
		|| htmlInlineAttrs(file, part)
		|| templateRender(file, TEMPLATE(>), NULL, NULL)
		|| htmlMarkup(file, content)
		|| templateRender(file, TEMPLATE(</pre>), NULL, NULL);
}

int htmlAttachmentOpen(FILE *file) {
	return templateRender(file, TEMPLATE(<ul class="attachment">), NULL, NULL);
}

int htmlAttachment(
	FILE *file, const struct BodyPart *part, const struct Variable *path
) {
	const char *template = TEMPLATE(
		<li><a href="[url]">[name][type][/][subtype]</a></li>
	);
	char *url = templateURL("../" PATH_ATTACHMENT, path);
	const char *name = paramGet(part->disposition.params, "filename");
	if (!name) name = paramGet(part->params, "name");
	struct Variable vars[] = {
		{ "url", url },
		{ "name", (name ? name : "") },
		{ "type", (name ? "" : part->type) },
		{ "/", (name ? "" : "/") },
		{ "subtype", (name ? "" : part->subtype) },
		{0},
	};
	int error = templateRender(file, template, vars, escapeXML);
	free(url);
	return error;
}

int htmlAttachmentClose(FILE *file) {
	return templateRender(file, TEMPLATE(</ul>), NULL, NULL);
}

int htmlMessageClose(FILE *file) {
	return templateRender(file, TEMPLATE(</article>), NULL, NULL);
}

static char *htmlThreadURL(const struct Envelope *envelope, const char *type) {
	struct Variable vars[] = {
		{ "messageID", envelope->messageID },
		{ "type", type },
		{0},
	};
	return templateURL("../" PATH_THREAD, vars);
}

int htmlThreadHead(FILE *file, const struct Envelope *envelope) {
	const char *template = TEMPLATE(
		<!DOCTYPE html>
		<meta charset="utf-8">
		<title>[subject] &middot; [title]</title>
		<link rel="alternate" type="application/atom+xml" href="[atom]">
		<link rel="alternate" type="application/mbox" href="[mbox]">
	);
	char *atom = htmlThreadURL(envelope, "atom");
	char *mbox = htmlThreadURL(envelope, "mbox");
	struct Variable vars[] = {
		{ "subject", envelope->subject },
		{ "title", baseTitle },
		{ "atom", atom },
		{ "mbox", mbox },
		{0},
	};
	int error = templateRender(file, template, vars, escapeXML);
	free(atom);
	free(mbox);
	return error;
}

int htmlThreadOpen(FILE *file, const struct Envelope *envelope) {
	const char *template = TEMPLATE(
		<header class="thread">
			<h1>[subject]</h1>
			<nav>
				<ul>
					<li><a href="../index.html">index</a></li>
					<li><a href="[atom]">follow</a></li>
					<li><a href="[mbox]">download</a></li>
				</ul>
			</nav>
		</header>
		<main class="thread">
	);
	char *atom = htmlThreadURL(envelope, "atom");
	char *mbox = htmlThreadURL(envelope, "mbox");
	struct Variable vars[] = {
		{ "subject", envelope->subject },
		{ "atom", atom },
		{ "mbox", mbox },
		{0},
	};
	int error = templateRender(file, template, vars, escapeXML);
	free(atom);
	free(mbox);
	return error;
}

static size_t threadCount(struct List thread) {
	size_t count = 0;
	for (size_t i = 0; i < thread.len; ++i) {
		if (thread.ptr[i].type == List) {
			count += threadCount(thread.ptr[i].list);
		} else {
			count++;
		}
	}
	return count;
}

int htmlSubthreadOpen(FILE *file, struct List thread) {
	const char *template = {
		TEMPLATE(
			<details class="subthread" open>
			<summary>
		)
		TEMPLATE (
			<data class="replies" value="[replies]">[replies] repl[ies]</data>
		)
		TEMPLATE(
			</summary>
		)
	};
	size_t count = threadCount(thread);
	char replies[32];
	snprintf(replies, sizeof(replies), "%zu", count);
	struct Variable vars[] = {
		{ "replies", replies },
		{ "ies", (count > 1 ? "ies" : "y") },
		{0},
	};
	return templateRender(file, template, vars, escapeXML);
}

int htmlSubthreadClose(FILE *file) {
	return templateRender(file, TEMPLATE(</details>), NULL, NULL);
}

static int htmlFooter(FILE *file) {
	const char *template = TEMPLATE(
		<footer>generated <time datetime="[time]">[time]</time></footer>
	);
	struct Variable vars[] = {
		{ "time", htmlUTC(time(NULL)) },
		{0},
	};
	return templateRender(file, template, vars, escapeXML);
}

int htmlThreadClose(FILE *file) {
	return 0
		|| templateRender(file, TEMPLATE(</main>), NULL, NULL)
		|| htmlFooter(file);
}

int htmlIndexHead(FILE *file) {
	const char *template = TEMPLATE(
		<!DOCTYPE html>
		<meta charset="utf-8">
		<title>[title]</title>
		<link rel="alternate" type="application/atom+xml" href="index.atom">
	);
	struct Variable vars[] = {
		{ "title", baseTitle },
		{0},
	};
	return templateRender(file, template, vars, escapeXML);
}

int htmlIndexOpen(FILE *file) {
	const char *template = TEMPLATE(
		<header class="index">
			<h1>[title]</h1>
			<nav>
				<ul>
					<li><a href="index.atom">follow</a></li>
					<li><a href="mailto:[addr]">write</a></li>
				</ul>
			</nav>
		</header>
		<main class="index">
			<ol>
	);
	struct Variable vars[] = {
		{ "title", baseTitle },
		{ "addr", baseAddress },
		{0},
	};
	return templateRender(file, template, vars, escapeXML);
}

static char *htmlIndexURL(const struct Envelope *envelope) {
	struct Variable vars[] = {
		{ "messageID", envelope->messageID },
		{ "type", "html" },
		{0},
	};
	return templateURL(PATH_THREAD, vars);
}

int htmlIndexThread(
	FILE *file, const struct Envelope *envelope, struct List thread
) {
	const char *template = TEMPLATE(
		<li>
			<h2 class="subject"><a href="[url]">[subject]</a></h2>
			<address class="from">[from]</address>
			<time datetime="[utc]">[date]</time>
			<data class="replies" value="[replies]">[replies] repl[ies]</data>
		</li>
	);
	char *url = htmlIndexURL(envelope);
	size_t count = threadCount(thread) - 1;
	char replies[32];
	snprintf(replies, sizeof(replies), "%zu", count);
	struct Variable vars[] = {
		{ "url", url },
		{ "subject", envelope->subject },
		{ "from", addressName(envelope->from) },
		{ "utc", htmlUTC(envelope->time) },
		{ "date", envelope->date },
		{ "replies", replies },
		{ "ies", (count == 1 ? "y" : "ies") },
		{0},
	};
	int error = templateRender(file, template, vars, escapeXML);
	free(url);
	return error;
}

int htmlIndexClose(FILE *file) {
	return 0
		|| templateRender(file, TEMPLATE(</ol></main>), NULL, NULL)
		|| htmlFooter(file);
}