- Reset master to upstream/main (16,697 commits) - Overlay 2,271 local-only files (skills, tools, workspace, configs, apps) - Restore IDENTITY.md and USER.md templates - Build verified, gateway running, Discord working Co-Authored-By: Claude Opus 4.6 <[email protected]>
24 lines
2.2 KiB
JavaScript
24 lines
2.2 KiB
JavaScript
#!/usr/bin/env node
|
|
const fs=require('fs');const https=require('https');const {homedir}=require('os');
|
|
function httpsRequest(url, options={}){return new Promise((res,rej)=>{const req=https.request(url,options,r=>{let d='';r.on('data',c=>d+=c);r.on('end',()=>{if(r.statusCode>=400)rej(new Error(`HTTP ${r.statusCode}: ${d}`));else try{res(JSON.parse(d))}catch(e){res(d)};});});req.on('error',rej);if(options.body)req.write(options.body);req.end();});}
|
|
function base64urlEncode(str){return Buffer.from(str).toString('base64').replace(/\+/g,'-').replace(/\//g,'_').replace(/=+$/,'');}
|
|
(async()=>{
|
|
try{
|
|
const id=process.argv[2]; const dest=process.argv[3]; if(!id||!dest) throw new Error('Usage: forward_gmail.cjs <message-id> <dest-email>');
|
|
const credsPath=require('path').join(homedir(),'.openclaw/secrets/google-oauth.json');
|
|
const creds=JSON.parse(fs.readFileSync(credsPath,'utf8'));
|
|
const body=new URLSearchParams({client_id:creds.client_id,client_secret:creds.client_secret,refresh_token:creds.refresh_token,grant_type:'refresh_token'}).toString();
|
|
const tokenResp=await httpsRequest('https://oauth2.googleapis.com/token',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','Content-Length':Buffer.byteLength(body)},body});
|
|
const token=tokenResp.access_token;
|
|
// fetch original message
|
|
const msg=await httpsRequest(`https://gmail.googleapis.com/gmail/v1/users/me/messages/${id}?format=full`,{headers:{Authorization:`Bearer ${token}`}});
|
|
// build a simple forward: include subject and snippet
|
|
const subject = msg.payload.headers.find(h=>h.name.toLowerCase()==='subject')?.value || '(no subject)';
|
|
const from = msg.payload.headers.find(h=>h.name.toLowerCase()==='from')?.value || '';
|
|
const snippet = msg.snippet || '';
|
|
const raw = `From: ${from}\nTo: ${dest}\nSubject: Fwd: ${subject}\nContent-Type: text/plain; charset=utf-8\n\n---- Forwarded message ----\n${snippet}\n`;
|
|
const encoded = base64urlEncode(raw);
|
|
await httpsRequest('https://gmail.googleapis.com/gmail/v1/users/me/messages/send',{method:'POST',headers:{Authorization:`Bearer ${token}`,'Content-Type':'application/json'},body:JSON.stringify({raw:encoded})});
|
|
console.log('Forwarded',id,'->',dest);
|
|
}catch(err){console.error('Error:',err.message);process.exit(1);} })();
|