Files
wekan/server/lib/inboundEmailQuoteStrip.js
T
Lauri Ojansivu 0c7524db05 Add reply-by-email: replying to a notification email comments on the card.
WeKan only ever sent email; there was no way to receive it, so replying to a
notification email went nowhere. This adds the webhook-based half of #2414 -
not an IMAP-polling mail client, which would need a running mailbox,
credentials and a polling loop the self-hoster would have to operate. Instead
a self-hoster points a mail provider's inbound-parse webhook (Mailgun Routes,
SendGrid Inbound Parse, Postmark inbound - documented in
docs/Features/Email/Reply-By-Email.md) at a new POST /api/inbound-email.

Every outbound notification email's Reply-To now carries
reply+<cardId>-<hmac>@<domain> (server/lib/inboundEmailReplyToken.js), the
HMAC keyed by INBOUND_EMAIL_HMAC_SECRET so it cannot be forged or guessed.
The webhook verifies that token, resolves the card, strips the reply's quoted
text (server/lib/inboundEmailQuoteStrip.js), matches the sender's address to
an existing WeKan user (server/lib/inboundEmailUserMatch.js - no match is
rejected, never an anonymous comment), and inserts a CardComments document.

Unauthenticated by design, so every rejection (bad/forged token, unmatched
sender) is logged via securityLog under the new authn.inbound-email catalog
key, wrapped so logging can never break the guard.

Both INBOUND_EMAIL_HMAC_SECRET and INBOUND_EMAIL_DOMAIN are opt-in; unset
(the default), no Reply-To is added and existing installs are unchanged.

Thanks to vasyugan and xet7 !

Closes #2414,
2026-09-10 20:51:52 +03:00

43 lines
1.6 KiB
JavaScript

'use strict';
// Strip the quoted reply text from an inbound plain-text email reply, keeping
// only what the person actually typed above it - used by
// server/routes/inboundEmail.js (#2414). Pure string function, no I/O, so it
// is unit-testable on its own (tests/inboundEmailQuoteStrip.test.cjs).
//
// Heuristic, same shape most reply-by-email systems (GitHub, Trello, mailing
// list tools) use: cut the text at the FIRST line that looks like the start
// of a quoted reply, then trim trailing whitespace. Recognized cut markers:
// - "On ... wrote:" (most mail clients' quote header)
// - a line starting with ">" (quoted/plain-text quoting)
// - "-----Original Message-----" (Outlook-style)
// - "From: ...") (Outlook-style header block), but
// only when it looks like a header line (short, no other prose before it)
// A body with none of these markers is returned trimmed and otherwise intact.
const ON_WROTE_RE = /^On .{0,120} wrote:\s*$/i;
const ORIGINAL_MESSAGE_RE = /^-{2,}\s*Original Message\s*-{2,}\s*$/i;
const QUOTE_LINE_RE = /^\s*>/;
const FROM_HEADER_RE = /^From:\s*.+/i;
function stripQuotedReply(body) {
if (typeof body !== 'string') return '';
const lines = body.replace(/\r\n/g, '\n').split('\n');
const kept = [];
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (
ON_WROTE_RE.test(line) ||
ORIGINAL_MESSAGE_RE.test(line) ||
QUOTE_LINE_RE.test(line) ||
FROM_HEADER_RE.test(line)
) {
break;
}
kept.push(line);
}
return kept.join('\n').trim();
}
module.exports = { stripQuotedReply };