about summary refs log tree commit diff
path: root/atom.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-04-09 21:59:21 -0400
committerJune McEnroe <june@causal.agency>2020-04-09 21:59:48 -0400
commit84aae6ed294e369c5edf755ef60fb963ce3bd7f7 (patch)
treec52743cb1cda5b6018b5ecf77c1e0dbeaf6276dc /atom.c
parentFactor out templateURL (diff)
downloadbubger-84aae6ed294e369c5edf755ef60fb963ce3bd7f7.tar.gz
bubger-84aae6ed294e369c5edf755ef60fb963ce3bd7f7.zip
Render Atom envelopes
Diffstat (limited to 'atom.c')
-rw-r--r--atom.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/atom.c b/atom.c
new file mode 100644
index 0000000..8991ca7
--- /dev/null
+++ b/atom.c
@@ -0,0 +1,61 @@
+/* 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 <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysexits.h>
+#include <time.h>
+
+#include "archive.h"
+
+static const char *Envelope = TEMPLATE(
+	<entry>
+	<title>[subject]</title>
+	<author>
+		<name>[from.name]</name>
+		<email>[from.mailbox]@[from.host]</email>
+	</author>
+	<updated>[date]</updated>
+	<id>[id]</id>
+);
+
+int atomEnvelope(FILE *file, const struct Envelope *envelope) {
+	const char *from = envelope->from.name;
+	if (!from) from = envelope->from.mailbox;
+
+	char date[sizeof("0000-00-00T00:00:00Z")];
+	strftime(date, sizeof(date), "%FT%TZ", gmtime(&envelope->utc));
+
+	struct Variable idVars[] = {
+		{ "messageID", envelope->messageID },
+		{0},
+	};
+	char *id = templateURL("mailto:?In-Reply-To=[messageID]", idVars);
+
+	struct Variable vars[] = {
+		{ "subject", envelope->subject },
+		{ "from.name", from },
+		{ "from.mailbox", envelope->from.mailbox },
+		{ "from.host", envelope->from.host },
+		{ "date", date },
+		{ "id", id },
+		{0},
+	};
+	int error = templateRender(file, Envelope, vars, escapeXML);
+	free(id);
+	return error;
+}