diff --git a/inbound.js b/inbound.js index 4f6e361..c24f5a5 100644 --- a/inbound.js +++ b/inbound.js @@ -4,8 +4,8 @@ // auto-replies, bulk mail and our own messages, so two autoresponders can never ping-pong. The URL carries a secret key // (env SUPPORT_INBOUND_KEY); SendGrid retries anything that is not 2xx, so the route always answers 200 once the key passes. 'use strict'; -let tickets = null; -function init(d) { tickets = d.tickets; } +let tickets = null, accounts = null; +function init(d) { tickets = d.tickets; accounts = d.accounts || null; } const key = () => String(process.env.SUPPORT_INBOUND_KEY || '').trim(); // a small multipart reader: text fields only, files skipped (attachments are not kept yet) @@ -63,6 +63,8 @@ async function handle(buf, contentType) { if (plus) { const ref = plus[1].toUpperCase(); if (!subject.toUpperCase().includes(ref)) subject = '[' + ref + '] ' + subject; } const body = stripQuoted(f.text || htmlToText(f.html)) || '(empty message)'; const r = await tickets.ingestEmail({ from, name: nameOf(f.from), subject, text: body, platform, confirm: true, + // new tickets only from member emails (Marty 2026-10-24); LinkSpin-first members are InstantAdPay accounts too (lsinbound) + canOpen: accounts ? async addr => { try { return !!(await accounts.byEmail(addr)); } catch (e) { return true; } } : null, messageId: header(raw, 'Message-ID'), inReplyTo: header(raw, 'In-Reply-To'), references: header(raw, 'References') }); if (r && r.error) return 'refused: ' + r.error; return (r.created ? 'new ticket ' : 'reply on ') + (r.ticket && r.ticket.ref) + ' from ' + from; diff --git a/server.js b/server.js index 741576c..bc9d296 100644 --- a/server.js +++ b/server.js @@ -559,7 +559,7 @@ async function boot() { pipeline.init({ dataDir: DATA_DIR, accounts, coach, chain }); translate.init({ dataDir: DATA_DIR }); tickets.init({ dataDir: DATA_DIR, mailer, messages, accounts, adminEmail: ADMIN_EMAIL, telegram: supportTelegram, translate }); - inbound.init({ tickets }); + inbound.init({ tickets, accounts }); supportbot.init({ tickets, chatId: () => String(siteConfig().supportTelegramChat || '1289244227').trim(), site: 'https://instantadpay.com' }); badge.init({ publicDir: PUBLIC_DIR }); gamestats.init({ dataDir: DATA_DIR }); @@ -1115,6 +1115,10 @@ const supportGlobal = { minute: 0, n: 0 }; const supportRecent = (k, ms) => { const now = Date.now(); const t = (supportHits.get(k) || []).filter(ts => now - ts < 86400000); supportHits.set(k, t); return t.filter(ts => now - ts < ms).length; }; const supportMark = k => { const t = supportHits.get(k) || []; t.push(Date.now()); supportHits.set(k, t); if (supportHits.size > 5000) for (const [kk, v] of supportHits) if (!v.length || Date.now() - v[v.length - 1] > 86400000) supportHits.delete(kk); }; const supportFlood = (k, n, ms) => { if (supportRecent(k, ms) >= n) return true; supportMark(k); return false; }; // signed-in and private-link replies: a plain cap +// tickets are open to platform members only (Marty, 2026-10-24, after a throwaway-address 'pentest' ticket): +// a signed-out sender's email must belong to an account on this site +const MEMBER_ONLY_TICKET_MSG = "Support tickets are for InstantAdPay members. Use the email on your InstantAdPay account, or sign in first. Not a member yet? Join free at https://instantadpay.com/join and ask from inside."; +async function memberOnlyTicket(email) { try { return (await accounts.byEmail(email)) ? null : { status: 403, body: { error: MEMBER_ONLY_TICKET_MSG, membersOnly: true } }; } catch (e) { return null; } } function supportGuard(req, b, email) { const now = Date.now(); const hpRaw = String(b.hp_field_x9 || '').trim(); @@ -2330,6 +2334,7 @@ const server = http.createServer(async (req, res) => { const email = acct ? acct.email : String(b.email || '').trim().toLowerCase(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) return json(res, 400, { error: 'That email address does not look right.' }); if (!acct) { const iss = await newEmailIssue(email); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }); } // replies must reach them + if (!acct) { const mo = await memberOnlyTicket(email); if (mo) return json(res, mo.status, mo.body); } const guard = supportGuard(req, b, email); if (guard) return json(res, guard.status, guard.body); const r = await tickets.create({ source: 'public', email, name: b.name || (acct && acct.username) || '', username: acct && acct.username, subject: b.subject, category: b.category, body: b.body }); @@ -2353,7 +2358,7 @@ const server = http.createServer(async (req, res) => { const email = acct ? acct.email : String(b.email || '').trim().toLowerCase(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) return json(res, 400, { error: 'Add your email so Marty can reply.', needEmail: true }); if (acct) { if (supportFlood('em:' + email, 5, 3600000)) return json(res, 429, { error: 'You have sent several tickets in the last hour. Open Help in your dashboard to add to one.' }); } - else { const guard = supportGuard(req, b, email); if (guard) return json(res, guard.status, guard.body); } + else { const mo = await memberOnlyTicket(email); if (mo) return json(res, mo.status, mo.body); const guard = supportGuard(req, b, email); if (guard) return json(res, guard.status, guard.body); } const tr = Array.isArray(b.transcript) ? b.transcript : []; const turns = tr.slice(-8).map(x => ((x && x.who === 'me') ? 'Me: ' : 'Assistant: ') + String((x && x.text) || '').replace(/\s+/g, ' ').trim().slice(0, 600)).filter(x => x.length > 6); const firstQ = tr.find(x => x && x.who === 'me' && String(x.text || '').trim()); diff --git a/tickets.js b/tickets.js index 4e69bde..1a9ac83 100644 --- a/tickets.js +++ b/tickets.js @@ -362,6 +362,7 @@ async function ingestEmail(e) { if (!ticketId) { const r = parseRef(e.subject); if (r) { const t = await byRef(r.ref); if (t && t.email === from) ticketId = t.id; } } const body = clean(e.text, 5000); if (ticketId) { const r = await addMessage(ticketId, { author: 'member', body, emailMessageId: mid, via: 'email' }); return Object.assign(r, { created: false }); } + if (e.canOpen && !(await e.canOpen(from))) return { error: 'not a member (new tickets need a platform account)', created: false }; // replies above still land const r = await create({ platform: e.platform || 'iap', source: 'email', email: from, name: e.name, subject: oneLine(e.subject, 140) || '(no subject)', category: 'Other', body, emailMessageId: mid, via: 'email', confirm: !!e.confirm }); return Object.assign(r, { created: true }); }