From 871dc8406bc749989201fdd5084008cfce1d608c Mon Sep 17 00:00:00 2001 From: AyrisAI Date: Thu, 14 May 2026 19:04:27 +0300 Subject: [PATCH] Add RFC 2047 MIME decoding for mail subjects --- app/api/webhooks/mail-signal/route.ts | 30 ++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/api/webhooks/mail-signal/route.ts b/app/api/webhooks/mail-signal/route.ts index 0a2fe58..fc5ad06 100644 --- a/app/api/webhooks/mail-signal/route.ts +++ b/app/api/webhooks/mail-signal/route.ts @@ -7,6 +7,29 @@ import { sendTelegramNotification } from '@/lib/notifications'; const WEBHOOK_SECRET = 'besiktasK1903*'; const IMAP_PASSWORD = process.env.MAILCOW_MASTER_PASSWORD || ''; // Dovecot Master Password tavsiye edilir +// RFC 2047 Decode Fonksiyonu +function decodeMimeText(text: string) { + if (!text) return text; + + // Eğer metin =? ile başlıyorsa decode etmeye çalış + return text.replace(/=\?([^?]+)\?([QB])\?([^?]+)\?=/gi, (match, charset, encoding, data) => { + try { + if (encoding.toUpperCase() === 'Q') { + // Quoted-Printable decode + return data + .replace(/_/g, ' ') + .replace(/=([0-9A-F]{2})/gi, (_: any, hex: string) => String.fromCharCode(parseInt(hex, 16))); + } else if (encoding.toUpperCase() === 'B') { + // Base64 decode + return Buffer.from(data, 'base64').toString(charset.toLowerCase() === 'utf-8' ? 'utf8' : 'binary'); + } + } catch (e) { + return match; + } + return match; + }); +} + export async function POST(request: Request) { const secret = request.headers.get('x-ayristech-secret'); @@ -17,7 +40,8 @@ export async function POST(request: Request) { const body = await request.json(); const { to, event, subject: incomingSubject, snippet: incomingSnippet, from: incomingFrom } = body; - console.log(`📩 Webhook Sinyali Alındı! Alıcı: ${to}`); + const subject = decodeMimeText(incomingSubject || ""); + console.log(`📩 Webhook Sinyali Alındı! Alıcı: ${to} | Konu: ${subject}`); try { // 1. Mailbox Mapping kontrolü @@ -35,9 +59,9 @@ export async function POST(request: Request) { let mailData = null; if (incomingSubject || incomingSnippet) { - console.log(`[Signal] İçerik worker'dan hazır geldi: ${incomingSubject}`); + console.log(`[Signal] İçerik worker'dan hazır geldi: ${subject}`); mailData = { - subject: incomingSubject || "(Konu Yok)", + subject: subject || "(Konu Yok)", text: incomingSnippet || "", from: incomingFrom || "Bilinmiyor" };