feat: unify SMTP and optional IMAP account settings
This commit is contained in:
1 parent
39afafefa7
commit
7104b6745c
8 files changed
+56
-14
No files matched your search
@@ -32,19 +32,23 @@ class MailStore{
|
||||
encode(id,value){try{return this.vault.encode(id,value);}catch{fail('邮件加密存储不可用,请联系管理员。',503);}}
|
||||
decode(id,value){try{return this.vault.decode(id,value);}catch{fail('邮件加密存储不可用,请联系管理员。',503);}}
|
||||
row(id){const row=this.db.prepare('SELECT * FROM channels WHERE id=?').get(id);if(!row)fail('邮箱不存在。',404);return row;}
|
||||
channelSecret(id){const row=this.row(id);return {...this.decode(id,row.payload),id,version:row.version};}
|
||||
publicChannel(id){const c=this.channelSecret(id),state=JSON.parse(this.row(id).state);return {...c,smtp:{...c.smtp,password:undefined,hasPassword:!!c.smtp?.password},imap:{...c.imap,password:undefined,hasPassword:!!c.imap.password},state};}
|
||||
channelSecret(id){const row=this.row(id);return {receiveEnabled:true,...this.decode(id,row.payload),id,version:row.version};}
|
||||
publicChannel(id){const c=this.channelSecret(id),state=JSON.parse(this.row(id).state);return {...c,smtp:{...c.smtp,password:undefined,hasPassword:!!c.smtp?.password},imap:{...c.imap,password:undefined,hasPassword:!!c.imap?.password},state};}
|
||||
channels(){return this.db.prepare('SELECT id FROM channels ORDER BY rowid').all().map(r=>this.publicChannel(r.id));}
|
||||
saveChannel(input){return this.transaction(()=>{
|
||||
if(!input||typeof input!=='object')fail('邮箱配置无效。');const old=input.id?this.channelSecret(input.id):null;if(old&&old.version!==input.version)fail('邮箱已被修改,请刷新后重试。',409);
|
||||
const id=old?.id||crypto.randomUUID(),address=email(input.email);const existing=this.db.prepare('SELECT id FROM channels WHERE email=?').get(address);if(existing&&existing.id!==id)fail('此邮箱已托管,请使用已有邮箱。',409);
|
||||
if(input.enabled!==undefined&&typeof input.enabled!=='boolean')fail('模板收信开关必须是布尔值。');
|
||||
const readOnly=input.readOnly===true;
|
||||
if(!readOnly&&(typeof input.enabled!=='boolean'||!Array.isArray(input.senders)||!input.senders.length||input.senders.length>100))fail('请填写模板发件人名单,并选择是否启用收信。');
|
||||
const receiveEnabled=input.receiveEnabled===undefined?(old?.receiveEnabled??true):input.receiveEnabled;if(typeof receiveEnabled!=='boolean')fail('请选择是否启用 IMAP 收信。');
|
||||
input=require('./account-config.cjs').expandAccount(input,old,address);
|
||||
const enabled=!readOnly&&receiveEnabled&&input.enabled===true;
|
||||
if(!readOnly&&(!Array.isArray(input.senders)||input.senders.length>100||(enabled&&!input.senders.length)))fail('请填写模板发件人名单,并选择是否启用收信。');
|
||||
const endpoint=kind=>{const v=input[kind];if(!v||!['tls','starttls'].includes(v.security)||!Number.isInteger(v.port)||v.port<1||v.port>65535)fail(kind.toUpperCase()+' 端口或加密方式无效。');const host=text(v.host,'服务器地址',253);if(!/^[a-zA-Z0-9.:-]+$/.test(host)||host.startsWith('-'))fail('服务器地址只填写主机名或 IP。');const password=v.password===''&&old?old[kind]?.password:v.password;if(typeof password!=='string'||!password||password.length>2048)fail('请输入 '+kind.toUpperCase()+' 密码或授权码。');const user=text(v.user,'登录账号',254);if(old&&v.password===''&&(old.email!==address||old[kind].user!==user||old[kind].host!==host))fail('更换邮箱、服务器或登录账号时,请重新填写密码。');return {host,port:v.port,security:v.security,user,password};};
|
||||
const saved={name:text(input.name,'邮箱名称',80),email:address,readOnly,smtp:readOnly?{}:endpoint('smtp'),imap:endpoint('imap'),senders:readOnly?[]:[...new Set(input.senders.map(email))],enabled:readOnly?false:input.enabled,createdAt:old?.createdAt||at(),updatedAt:at()};
|
||||
const saved={name:text(input.name,'邮箱名称',80),email:address,readOnly,receiveEnabled,smtp:readOnly?{}:endpoint('smtp'),imap:receiveEnabled?endpoint('imap'):(old?.imap||{}),senders:readOnly?[]:[...new Set(input.senders.map(email))],enabled,createdAt:old?.createdAt||at(),updatedAt:at()};
|
||||
if(input.signature!==undefined)saved.signature=require('./content.cjs').normalizeSignature(input.signature);else if(old?.signature)saved.signature=old.signature;
|
||||
const reset=!old||old.email!==address||JSON.stringify(old.imap)!==JSON.stringify(saved.imap);const oldrow=old?this.row(id):null;
|
||||
const changed=reset||JSON.stringify(old?.smtp)!==JSON.stringify(saved.smtp);const state=changed?{checkedAt:null,smtp:false,imap:false,pollAt:null,message:'待检查连接'}:JSON.parse(oldrow.state);
|
||||
const changed=reset||old?.receiveEnabled!==receiveEnabled||JSON.stringify(old?.smtp)!==JSON.stringify(saved.smtp);const state=changed?{checkedAt:null,smtp:false,imap:false,pollAt:null,message:'待检查连接'}:JSON.parse(oldrow.state);
|
||||
this.db.prepare('INSERT INTO channels VALUES(?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET email=excluded.email,version=excluded.version,payload=excluded.payload,state=excluded.state,cursor=excluded.cursor').run(id,address,(old?.version||0)+1,this.encode(id,saved),JSON.stringify(state),reset?'{}':oldrow.cursor);return this.publicChannel(id);
|
||||
});}
|
||||
guard(id,version){if(this.row(id).version!==version)fail('邮箱配置已变化,请重试。',409);}
|
||||
|
||||
Reference in new issue
Block a user