43 lines
4.4 KiB
JavaScript
43 lines
4.4 KiB
JavaScript
'use strict';
|
|
const {secureBpmTransport}=require('../bpm/api.cjs');const {escape}=require('./parser.cjs');
|
|
const fail=(message,status)=>{throw Object.assign(new Error(message),{status});};
|
|
async function mailApi({mail,workspace,user,route,method,body,request,response,reply,trustedProxy}){
|
|
if(!route.startsWith('/api/mail'))return false;if(!mail)fail('邮件服务暂不可用。',503);const store=mail.store;
|
|
if(method==='POST'&&user.role!=='admin')fail('仅管理员可以维护邮箱和确认模板。',403);
|
|
if(route==='/api/mail'&&method==='GET'){reply(200,{historyOnly:mail.historyOnly===true,channels:store.channels(),templates:store.templates(),receipts:store.receipts(),sends:store.sends()});return true;}
|
|
const audit=(action,detail)=>workspace?.record(user,action,detail);
|
|
if(route==='/api/mail/templates/categories'){
|
|
if(method==='GET'){reply(200,{categories:require('../order-rules/catalog.cjs').seedRules().map(({id,name})=>({id,name})),bindings:store.categoryBindings()});return true;}
|
|
if(method==='POST')fail('旧类别模板关联已改为只读,请在分类通知规则选择模板。',410);
|
|
}
|
|
if(route==='/api/mail/channels'&&method==='POST'){
|
|
if(!secureBpmTransport(request,trustedProxy))fail('请在部署机本地或 HTTPS 页面维护邮箱账号。',403);
|
|
if(mail.historyOnly&&body?.readOnly!==true)fail('此空间只允许配置只读 IMAP 邮箱。',409);
|
|
if(body?.id&&mail.busy(body.id))fail('此邮箱正在执行操作,请稍后保存。',409);const saved=store.saveChannel(body);audit('mail.channel.save',saved.name);reply(body.id?200:201,saved);return true;
|
|
}
|
|
let match=/^\/api\/mail\/channels\/([\w-]+)\/history$/.exec(route);
|
|
if(match&&method==='POST'){
|
|
const result=await mail.history(match[1],body);audit('mail.history.read',store.publicChannel(match[1]).name+' · '+String(body.action));reply(200,result);return true;
|
|
}
|
|
if(mail.historyOnly&&method==='POST')fail('此空间仅允许查询历史邮件,不能发信或收取模板。',409);
|
|
match=/^\/api\/mail\/channels\/([\w-]+)\/(verify|poll|test)$/.exec(route);
|
|
if(match&&method==='POST'){const [,id,action]=match;let result;if(action==='test'){if(typeof body.templateId!=='string')fail('请选择已生效模板。',400);result=await mail.sendTest(id,body.templateId);}else result=await mail[action](id);audit('mail.'+action,store.publicChannel(id).name);reply(200,result);return true;}
|
|
match=/^\/api\/mail\/templates\/([\w-]+)(?:\/(confirm|reject|preview|source|attachments\/([0-9]+)))?$/.exec(route);
|
|
if(match){const [,id,action,index]=match;const template=store.template(id);
|
|
if(method==='GET'&&!action){const {raw,attachments,...detail}=template;reply(200,{...detail,attachments:attachments.map(({content,...a})=>a)});return true;}
|
|
if(method==='GET'&&action==='preview'){
|
|
response.setHeader('Content-Security-Policy',"default-src 'none'; img-src data:; style-src 'unsafe-inline'; sandbox; base-uri 'none'; form-action 'none'");
|
|
response.writeHead(200,{'Content-Type':'text/html; charset=utf-8'});response.end('<!doctype html><html lang="zh-CN"><meta charset="utf-8"><style>body{font:14px/1.7 system-ui,sans-serif;padding:16px;color:#182433;overflow-wrap:anywhere}img{max-width:100%;height:auto}table{border-collapse:collapse;max-width:100%}td,th{padding:6px;border:1px solid #ddd}pre{white-space:pre-wrap}</style><body>'+(template.html||'<pre>'+escape(template.text)+'</pre>')+'</body></html>');return true;
|
|
}
|
|
if(method==='GET'&&(action==='source'||action?.startsWith('attachments/'))){
|
|
if(user.role!=='admin')fail('仅管理员可以下载原邮件和附件。',403);let bytes,name;
|
|
if(action==='source'){bytes=store.source(id);name='template.eml';}else{const a=template.attachments[Number(index)];if(!a)fail('附件不存在。',404);bytes=Buffer.from(a.content,'base64');name=a.filename;}
|
|
response.writeHead(200,{'Content-Type':'application/octet-stream','Content-Disposition':"attachment; filename*=UTF-8''"+encodeURIComponent(name)});response.end(bytes);return true;
|
|
}
|
|
if(method==='POST'&&action==='confirm'){const saved=store.confirm(id,body,user.username);audit('mail.template.confirm',saved.subject);reply(200,{id:saved.id,status:saved.status,version:saved.version});return true;}
|
|
if(method==='POST'&&action==='reject'){store.reject(id,body.version);audit('mail.template.reject',template.subject);reply(200,{ok:true});return true;}
|
|
}
|
|
fail('没有找到此邮件操作。',404);
|
|
}
|
|
module.exports={mailApi};
|