MC/website/node/mail-broker.mjs
2026-11-27 23:43:05 +00:00

37 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {mailBrokerCommand} from './mail-broker-transport.mjs';
import {spawn} from 'node:child_process';
// Administrator settings select the local JSON-only broker or restricted SSH broker.
// Request validation and mailbox ownership remain independent of that transport.
export async function callMailBroker(payload){
if(!payload||!['ensure','status','set_password'].includes(payload.action)||!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(payload.userId||''))throw Error('Invalid mailbox request');
const request={action:payload.action,userId:payload.userId};
if(payload.action==='ensure'){
if(!/^[a-z0-9_]{3,32}$/.test(payload.username||''))throw Error('Invalid staff username');
request.username=payload.username;
}
if(payload.action==='set_password'){
if(typeof payload.password!=='string'||payload.password.length<12||payload.password.length>256||/[\r\n\0]/.test(payload.password))throw Error('Use a password of 12–256 characters without line breaks');
request.password=payload.password;
}
return new Promise((resolve,reject)=>{
const {command,args}=mailBrokerCommand();const child=spawn(command,args,{stdio:['pipe','pipe','ignore']});
let output='',finished=false;
const fail=message=>{if(finished)return;finished=true;clearTimeout(timer);child.kill();reject(Error(message));};
const timer=setTimeout(()=>fail('Mailbox service temporarily unavailable'),15000);
child.on('error',()=>fail('Mailbox service temporarily unavailable'));
child.stdin.on('error',()=>fail('Mailbox service temporarily unavailable'));
child.stdout.on('data',data=>{output+=data;if(output.length>8192)fail('Invalid mailbox service response');});
child.on('close',code=>{
if(finished)return;
finished=true;clearTimeout(timer);
try{
const result=JSON.parse(output);
if(code!==0||result.error)throw Error(result.error||'Mailbox service temporarily unavailable');
if(typeof result.active!=='boolean'||typeof result.needsPasswordSetup!=='boolean'||(result.configured&&!/^[a-z0-9_]+@dunerealms\.com$/.test(result.address||'')))throw Error('Invalid mailbox service response');
resolve(result);
}catch(error){reject(Error(error.message==='Unexpected end of JSON input'?'Mailbox service temporarily unavailable':error.message));}
});
child.stdin.end(JSON.stringify(request)+'\n');
});
}