← back to Sample Followup Sweep

scripts/contact-research.mjs

49 lines

'use strict';
// READ-ONLY Gmail domain research for skipped sample-followup vendor vids.
// Searches info@ mailbox by vendor DOMAIN (not name), returns recent human counterparts.
import http from 'node:http';
import { readFileSync } from 'node:fs';

const GEORGE = { host: '127.0.0.1', port: 9850 };
function georgeAuth() {
  const files = ['/Users/macstudio3/Projects/Designer-Wallcoverings/DW-MCP/.env', '/Users/macstudio3/Projects/george-gmail/.env'];
  const genv = (k) => { for (const f of files) { try { const m = readFileSync(f, 'utf8').match(new RegExp('^' + k + '=(.+)$', 'm')); if (m) return m[1].trim(); } catch {} } return ''; };
  let a = genv('GEORGE_BASIC_AUTH'); if (!a.includes(':')) a = 'admin:' + genv('GEORGE_BASIC_AUTH_PASS');
  return 'Basic ' + Buffer.from(a).toString('base64');
}
function gGet(path) {
  return new Promise((resolve, reject) => {
    const req = http.request({ ...GEORGE, path, method: 'GET', headers: { Authorization: georgeAuth() } }, (r) => {
      let b = ''; r.on('data', (d) => b += d); r.on('end', () => { try { resolve(JSON.parse(b)); } catch { resolve(null); } });
    });
    req.on('error', reject); req.end();
  });
}
async function search(q) {
  const res = await gGet(`/api/info/search?q=${encodeURIComponent(q)}`).catch(() => null);
  const msgs = res && (res.messages || res.results || (Array.isArray(res) ? res : null));
  return Array.isArray(msgs) ? msgs : [];
}

// vid -> {vendor, domain(s)}  (domain from vendor_registry / known DW mappings)
const MAP = JSON.parse(process.argv[2] || '{}');

const run = async () => {
  const out = {};
  for (const [vid, info] of Object.entries(MAP)) {
    const domains = info.domains || [];
    const rec = { vendor: info.vendor, domains, from_hits: [], to_hits: [] };
    for (const d of domains) {
      // messages we RECEIVED from anyone @domain (who they route through)
      const froms = await search(`from:@${d}`);
      for (const m of froms.slice(0, 5)) rec.from_hits.push({ from: m.from || m.sender || '', subject: (m.subject || '').slice(0, 90), date: m.date || m.internalDate || '', snippet: (m.snippet || '').slice(0, 120) });
      // messages we SENT to anyone @domain (who we chase)
      const tos = await search(`to:@${d}`);
      for (const m of tos.slice(0, 5)) rec.to_hits.push({ to: m.to || '', subject: (m.subject || '').slice(0, 90), date: m.date || m.internalDate || '' });
    }
    out[vid] = rec;
  }
  console.log(JSON.stringify(out, null, 2));
};
run().catch((e) => { console.error('research error:', e.message); process.exit(1); });