/* Copyright (C) 2020 C. McEnroe * * 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 . * * Additional permission under GNU GPL version 3 section 7: * * If you modify this Program, or any covered work, by linking or * combining it with OpenSSL (or a modified version of that library), * containing parts covered by the terms of the OpenSSL License and the * original SSLeay license, the licensors of this Program grant you * additional permission to convey the resulting work. Corresponding * Source for a non-source form of such a combination shall include the * source code for the parts of OpenSSL used as well as that of the * covered work. */ #include #include #include #include #include #include #include #include #include #include "archive.h" static char *htmlMailto(struct Address addr) { struct Variable vars[] = { { "mailbox", addr.mailbox }, { "host", addr.host }, {0}, }; return templateString("mailto:[mailbox]@[host]", vars, escapeURL); } static int htmlAddress(FILE *file, struct Address addr, bool comma) { char *mailto = htmlMailto(addr); const char *template; if (addr.host) { template = Q([,][name]); } else if (addr.mailbox) { template = "[mailbox]: "; } else { template = ";"; } struct Variable vars[] = { { "mailto", mailto }, { "name", addressName(addr) }, { "mailbox", addr.mailbox }, { ",", (comma ? ", " : " ") }, {0}, }; int error = templateRender(file, template, vars, escapeXML); free(mailto); return error; } static int htmlAddressList(FILE *file, const char *name, struct AddressList list) { if (!list.len) return 0; const char *template = Q(
[name]: ); struct Variable vars[] = { { "name", name }, {0}, }; int error = templateRender(file, template, vars, escapeXML); if (error) return error; for (size_t i = 0; i < list.len; ++i) { error = htmlAddress( file, list.addrs[i], i > 0 && list.addrs[i - 1].host ); if (error) return error; } return templateRender(file, Q(
), NULL, NULL); } static char *htmlFragment(const char *messageID) { struct Variable vars[] = { { "messageID", messageID }, {0}, }; return templateString("#[messageID]", vars, escapeURL); } static char *htmlMbox(const char *messageID) { struct Variable vars[] = { { "messageID", messageID }, { "type", "mbox" }, {0}, }; return templateString("../" PATH_MESSAGE, vars, escapeURL); } 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", baseMailto }, { "re", (strncmp(envelope->subject, "Re: ", 4) ? "Re: " : "") }, { "subject", envelope->subject }, { "messageID", envelope->messageID }, { "<", "<" }, { ">", ">" }, {0}, }; return templateString(template, vars, escapeURL); } int htmlMessageNav(FILE *file, const struct Envelope *envelope) { char *parent = envelope->inReplyTo ? htmlFragment(envelope->inReplyTo) : NULL; char *mbox = htmlMbox(envelope->messageID); char *reply = htmlReply(envelope); const char *template = Q( ); struct Variable vars[] = { { "parent", parent }, { "mbox", mbox }, { "reply", reply }, {0}, }; int error = templateRender(file, template, vars, escapeXML); free(parent); free(mbox); free(reply); return error; } int htmlMessageOpen(FILE *file, const struct Envelope *envelope) { char *fragment = htmlFragment(envelope->messageID); char *mailto = htmlMailto(envelope->from); const char *template = Q(

[subject]

From: [from]
); struct Variable vars[] = { { "messageID", envelope->messageID }, { "fragment", fragment }, { "subject", envelope->subject }, { "mailto", mailto }, { "from", addressName(envelope->from) }, { "utc", iso8601(envelope->time).s }, { "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, Q(
), NULL, NULL); free(fragment); free(mailto); return error; } 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(®ex, Pattern, REG_EXTENDED); assert(!error); } int error; char *ptr; regmatch_t match[4]; for (ptr = buf; !regexec(®ex, 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 = Q([url]); 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 htmlMarkupQuote(FILE *file, char *buf) { uint32_t level = 0; for (char *ch = buf; *ch == '>' || *ch == ' '; level += (*ch++ == '>')); const char *template = Q(); struct Variable vars[] = { { "level", u32(level).s }, {0}, }; return 0 || templateRender(file, template, vars, escapeXML) || htmlMarkupURLs(file, buf) || templateRender(file, Q(), NULL, NULL); } 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(®ex, Pattern, REG_EXTENDED); assert(!error); } if (patch && !regexec(®ex, buf, 0, NULL, 0)) { error = templateRender( file, Q([line]), vars, escapeXML ); } else if (patch && buf[0] == '@' && buf[1] == '@') { error = templateRender( file, Q([line]), vars, escapeXML ); } else if (patch && buf[0] == '-' && strcmp(buf, "---")) { error = templateRender( file, Q([line]), vars, escapeXML ); } else if (patch && buf[0] == '+') { error = templateRender( file, Q([line]), vars, escapeXML ); } else if (patch) { error = escapeXML(file, buf); } else if (buf[0] == '>') { error = htmlMarkupQuote(file, buf); } 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) { const char *template = Q(
	);
	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;
	}
	struct Variable vars[] = {
		{ "contentID", part->contentID },
		{ "description", part->description },
		{ "language",  language },
		{0},
	};
	return 0
		|| templateRender(file, template, vars, escapeXML)
		|| htmlMarkup(file, content)
		|| templateRender(file, Q(
), NULL, NULL); } int htmlAttachmentOpen(FILE *file) { return templateRender(file, Q(
    ), NULL, NULL); } int htmlAttachment( FILE *file, const struct BodyPart *part, const struct Variable *path ) { char *url = templateString("../" PATH_ATTACHMENT, path, escapeURL); const char *name = paramGet(part->disposition.params, "filename"); if (!name) name = paramGet(part->params, "name"); const char *template = Q(
  • [name][type][/][subtype]
  • ); 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, Q(
), NULL, NULL); } int htmlMessageClose(FILE *file) { return templateRender(file, Q(
), NULL, NULL); } static int htmlStylesheet(FILE *file) { if (baseStylesheet) { const char *template = Q(); struct Variable vars[] = { { "href", baseStylesheet }, {0}, }; return templateRender(file, template, vars, escapeXML); } else { const char *template = Q(); struct Variable vars[] = { { "style", Stylesheet }, {0}, }; return templateRender(file, template, vars, NULL); } } static char *htmlThreadURL(const struct Envelope *envelope, const char *type) { struct Variable vars[] = { { "messageID", envelope->messageID }, { "type", type }, {0}, }; return templateString("../" PATH_THREAD, vars, escapeURL); } int htmlThreadHead(FILE *file, const struct Envelope *envelope) { char *atom = htmlThreadURL(envelope, "atom"); char *mbox = htmlThreadURL(envelope, "mbox"); const char *template = Q( [subject] ); struct Variable vars[] = { { "generator", GENERATOR_URL }, { "subject", envelope->subject }, { "atom", atom }, { "mbox", mbox }, {0}, }; int error = 0 || templateRender(file, template, vars, escapeXML) || htmlStylesheet(file); free(atom); free(mbox); return error; } int htmlThreadOpen(FILE *file, const struct Envelope *envelope) { char *atom = htmlThreadURL(envelope, "atom"); char *mbox = htmlThreadURL(envelope, "mbox"); const char *template = Q(

[subject]

); 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 uint32_t threadCount(struct List thread) { uint32_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; } static int htmlReplies(FILE *file, uint32_t replies) { const char *template = Q( [replies] repl[ies] ); struct Variable vars[] = { { "replies", u32(replies).s }, { "ies", (replies != 1 ? "ies" : "y") }, {0}, }; return templateRender(file, template, vars, escapeXML); } int htmlSubthreadOpen(FILE *file, struct List thread) { const char *template = Q(
); return 0 || templateRender(file, template, NULL, NULL) || htmlReplies(file, threadCount(thread)) || templateRender(file, Q(), NULL, NULL); } int htmlSubthreadClose(FILE *file) { return templateRender(file, Q(
), NULL, NULL); } static int htmlFooter(FILE *file) { const char *template = Q( ); struct Variable vars[] = { { "generator", GENERATOR_URL }, { "time", iso8601(time(NULL)).s }, {0}, }; return templateRender(file, template, vars, escapeXML); } int htmlThreadClose(FILE *file) { return 0 || templateRender(file, Q(
), NULL, NULL) || htmlFooter(file); } int htmlIndexHead(FILE *file) { const char *template = Q( [title] ); struct Variable vars[] = { { "generator", GENERATOR_URL }, { "title", baseTitle }, {0}, }; return 0 || templateRender(file, template, vars, escapeXML) || htmlStylesheet(file); } int htmlIndexOpen(FILE *file) { const char *template = Q(

[title]

    ); struct Variable vars[] = { { "title", baseTitle }, { "subscribe", baseSubscribe }, { "mailto", baseMailto }, {0}, }; return templateRender(file, template, vars, escapeXML); } static char *htmlIndexURL(const struct Envelope *envelope) { struct Variable vars[] = { { "messageID", envelope->messageID }, { "type", "html" }, {0}, }; return templateString(PATH_THREAD, vars, escapeURL); } int htmlIndexThread( FILE *file, const struct Envelope *envelope, struct List thread ) { char *url = htmlIndexURL(envelope); const char *template = Q(
  1. [subject]

    From: [from]
    ); struct Variable vars[] = { { "url", url }, { "subject", envelope->subject }, { "from", addressName(envelope->from) }, { "utc", iso8601(envelope->time).s }, { "date", envelope->date }, {0}, }; int error = 0 || templateRender(file, template, vars, escapeXML) || htmlReplies(file, threadCount(thread) - 1) || templateRender(file, Q(
  2. ), NULL, NULL); free(url); return error; } int htmlIndexClose(FILE *file) { return 0 || templateRender(file, Q(
), NULL, NULL) || htmlFooter(file); }