/* 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 . */ #include #include #include #include #include #include #include "archive.h" static int htmlAddress(FILE *file, const char *class, struct Address addr) { const char *template; if (addr.host) { template = TEMPLATE(
  • [name]
  • ); } else if (addr.mailbox) { template = TEMPLATE(
  • [mailbox]
      ); } else { template = TEMPLATE(
  • ); } 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(
      ); 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(
    ), NULL, NULL); } static char *htmlMailto(const struct Envelope *envelope) { const char *template = { "mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]" }; struct Variable vars[] = { { "mailbox", envelope->from.mailbox }, { "host", envelope->from.host }, { "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(), NULL, NULL); } int htmlMessageOpen(FILE *file, const struct Envelope *envelope) { // TODO: Conditionally include mailto: link. const char *template = TEMPLATE(

    [subject]

    [from]
    ); char *fragment = htmlFragment(envelope->messageID); char *mailto = htmlMailto(envelope); char utc[sizeof("0000-00-00T00:00:00Z")]; strftime(utc, sizeof(utc), "%FT%TZ", gmtime(&envelope->time)); struct Variable vars[] = { { "messageID", envelope->messageID }, { "fragment", fragment }, { "subject", envelope->subject }, { "mailto", mailto }, { "from", addressName(envelope->from) }, { "utc", utc }, { "date", envelope->date }, {0}, }; int error = 0 || templateRender(file, template, vars, escapeXML) || htmlMessageNav(file, envelope) || htmlAddressList(file, "to", envelope->to) || htmlAddressList(file, "cc", envelope->cc) || templateRender(file, TEMPLATE(
    ), NULL, NULL); free(mailto); free(fragment); return error; } static int htmlInlineAttrs(FILE *file, const struct BodyPart *part) { const char *template = " [attr]=\"[value]\""; if (part->id) { struct Variable vars[] = { { "attr", "id" }, { "value", part->id }, {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 bool isPatch(const char *content) { return !strncmp(content, "---\n", 4) || strstr(content, "\n---\n"); } static int htmlPatch(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[1] - 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, "---\n")) { patch = true; } else if (patch && !strcmp(buf, "-- \n")) { patch = false; } if (patch && !strncmp(buf, "@@", 2)) { error = templateRender( file, TEMPLATE([line]), vars, escapeXML ); } else if (patch && buf[0] == '-' && strncmp(buf, "---", 3)) { error = templateRender( file, TEMPLATE([line]), vars, escapeXML ); } else if (patch && buf[0] == '+' && strncmp(buf, "+++", 3)) { error = templateRender( file, TEMPLATE([line]), vars, escapeXML ); } else { error = escapeXML(file, buf); } if (error) break; } free(buf); return error; } int htmlInline(FILE *file, const struct BodyPart *part, const char *content) { int error = 0 || templateRender(file, TEMPLATE(), NULL, NULL); if (error) return error; if (isPatch(content)) { error = htmlPatch(file, content); } else { error = escapeXML(file, content); } return error || templateRender(file, TEMPLATE(), NULL, NULL); } int htmlAttachmentOpen(FILE *file) { return templateRender(file, TEMPLATE(
      ), NULL, NULL); } int htmlAttachment( FILE *file, const struct BodyPart *part, const struct Variable *path ) { const char *template = TEMPLATE(
    • [name][type][/][subtype]
    • ); char *url = templateURL("../" PATH_ATTACHMENT, path); const char *name = paramGet(part->disposition.params, "filename"); 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(
    ), NULL, NULL); } int htmlMessageClose(FILE *file) { return templateRender(file, TEMPLATE(
    ), NULL, NULL); } const char *htmlTitle; 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( [subject] · [title] ); char *atom = htmlThreadURL(envelope, "atom"); char *mbox = htmlThreadURL(envelope, "mbox"); struct Variable vars[] = { { "subject", envelope->subject }, { "title", htmlTitle }, { "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(

    [subject]

    ); 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(
    [replies] repl[ies] ); 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(
    ), NULL, NULL); } int htmlThreadClose(FILE *file) { const char *template = TEMPLATE(
    ); time_t now = time(NULL); char time[sizeof("0000-00-00T00:00:00Z")]; strftime(time, sizeof(time), "%FT%TZ", gmtime(&now)); struct Variable vars[] = { { "time", time }, {0}, }; return templateRender(file, template, vars, escapeXML); }