about summary refs log tree commit diff
path: root/html.c
diff options
context:
space:
mode:
Diffstat (limited to 'html.c')
-rw-r--r--html.c110
1 files changed, 52 insertions, 58 deletions
diff --git a/html.c b/html.c
index f078b75..bc6035e 100644
--- a/html.c
+++ b/html.c
@@ -23,31 +23,40 @@
 
 #include "archive.h"
 
-static const char *Mailto = {
-	"mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]"
-};
-
-static const char *Summary = TEMPLATE(
-	<details class="message" id="[messageID]">
-	<summary>
-		<h1 class="subject"><a href="[fragment]">[subject]</a></h1>
-		<address class="from"><a href="[mailto]">[from]</a></address>
-		<time datetime="[utc]">[date]</time>
-	</summary>
-);
-
-static const char *Address = TEMPLATE(
-	<li><address class="[class]">[name]</address></li>
-);
+static const char *addrName(struct Address addr) {
+	return (addr.name ? addr.name : addr.mailbox);
+}
 
-int htmlEnvelope(FILE *file, const struct Envelope *envelope) {
-	struct Variable fragmentVars[] = {
-		{ "messageID", envelope->messageID },
+static int
+htmlAddressList(FILE *file, const char *class, struct AddressList list) {
+	if (!list.len) return 0;
+	struct Variable vars[] = {
+		{ "class", class },
 		{0},
 	};
-	char *fragment = templateURL("#[messageID]", fragmentVars);
+	int error = templateRender(
+		file, TEMPLATE(<ul class="[class]">), vars, escapeXML
+	);
+	if (error) return error;
+	for (size_t i = 0; i < list.len; ++i) {
+		struct Variable vars[] = {
+			{ "class", class },
+			{ "name", addrName(list.addrs[i]) },
+			{0},
+		};
+		error = templateRender(
+			file,
+			TEMPLATE(<li><address class="[class]">[name]</address></li>),
+			vars,
+			escapeXML
+		);
+		if (error) return error;
+	}
+	return templateRender(file, TEMPLATE(</ul>), vars, escapeXML);
+}
 
-	struct Variable mailtoVars[] = {
+int htmlEnvelope(FILE *file, const struct Envelope *envelope) {
+	struct Variable urlVars[] = {
 		{ "mailbox", envelope->replyTo.mailbox },
 		{ "host", envelope->replyTo.host },
 		{ "re", (strncmp(envelope->subject, "Re: ", 4) ? "Re: " : "") },
@@ -55,55 +64,40 @@ int htmlEnvelope(FILE *file, const struct Envelope *envelope) {
 		{ "messageID", envelope->messageID },
 		{0},
 	};
-	char *mailto = templateURL(Mailto, mailtoVars);
-
-	const char *from = envelope->from.name;
-	if (!from) from = envelope->from.mailbox;
-
-	char utc[sizeof("0000-00-00T00:00:00Z")];
-	strftime(utc, sizeof(utc), "%FT%TZ", gmtime(&envelope->utc));
+	char *fragment = templateURL("#[messageID]", urlVars);
+	char *mailto = templateURL(
+		"mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]",
+		urlVars
+	);
 
 	char date[256];
+	char utc[sizeof("0000-00-00T00:00:00Z")];
 	strftime(date, sizeof(date), "%c %z", &envelope->date);
-
-	struct Variable summaryVars[] = {
+	strftime(utc, sizeof(utc), "%FT%TZ", gmtime(&envelope->utc));
+	struct Variable vars[] = {
 		{ "messageID", envelope->messageID },
 		{ "fragment", fragment },
 		{ "subject", envelope->subject },
 		{ "mailto", mailto },
-		{ "from", from },
-		{ "utc", utc },
+		{ "from", addrName(envelope->from) },
 		{ "date", date },
+		{ "utc", utc },
 		{0},
 	};
-	int error = templateRender(file, Summary, summaryVars, escapeXML);
+	const char *Summary = TEMPLATE(
+		<details class="message" id="[messageID]">
+		<summary>
+			<h1 class="subject"><a href="[fragment]">[subject]</a></h1>
+			<address class="from"><a href="[mailto]">[from]</a></address>
+			<time datetime="[utc]">[date]</time>
+		</summary>
+	);
+	int error = templateRender(file, Summary, vars, escapeXML);
 	free(fragment);
 	free(mailto);
 	if (error) return error;
 
-	if (0 > fprintf(file, "<dl><dt>To:</dt><dd><ul>")) return -1;
-	for (size_t i = 0; i < envelope->to.len; ++i) {
-		struct Address addr = envelope->to.addrs[i];
-		struct Variable vars[] = {
-			{ "class", "to" },
-			{ "name", (addr.name ? addr.name : addr.mailbox) },
-			{0},
-		};
-		error = templateRender(file, Address, vars, escapeXML);
-		if (error) return error;
-	}
-	if (0 > fprintf(file, "</ul></dd><dt>Cc:</dt><dd><ul>")) return -1;
-	for (size_t i = 0; i < envelope->cc.len; ++i) {
-		struct Address addr = envelope->cc.addrs[i];
-		struct Variable vars[] = {
-			{ "class", "cc" },
-			{ "name", (addr.name ? addr.name : addr.mailbox) },
-			{0},
-		};
-		error = templateRender(file, Address, vars, escapeXML);
-		if (error) return error;
-	}
-	if (0 > fprintf(file, "</ul></dd></dl>")) return -1;
-
-	return 0;
+	return 0
+		|| htmlAddressList(file, "to", envelope->to)
+		|| htmlAddressList(file, "cc", envelope->cc);
 }