import {readFile} from 'node:fs/promises'; import path from 'node:path'; import os from 'node:os'; import {fileURLToPath} from 'node:url'; export function parseConfig(text){ const lines=text.split(/\r?\n/).map(l=>l.split(';')[0].trim()).filter(Boolean); const value=(name)=>lines.find(l=>new RegExp('^'+name+'\\s*=','i').test(l))?.split('=').slice(1).join('=').trim()||''; const users=lines.flatMap(l=>{const m=l.match(/^USER=([^,]+),([^,]+),([^,]+),(.*)$/i);return m&&/\bsysop\b/i.test(m[4])?[{user:m[1].trim(),password:m[2],call:m[3].trim().toUpperCase()}]:[]}); const apps=lines.flatMap(l=>{const m=l.match(/^APPLICATION\s+\d+,([^,]+),([^,]*),([^,]*),([^,]*)/i);return m?[{command:m[1],call:m[3],alias:m[4]}]:[]}); let ports=[],current=null; for(const l of lines){if(/^PORT$/i.test(l)){current={id:ports.length+1,name:'',driver:''};continue;}if(/^ENDPORT$/i.test(l)){if(current){if(!current.driver)current.driver=current.protocol||current.type||'';ports.push(current)};current=null;continue;}if(current){const m=l.match(/^(PORTNUM|ID|DRIVER|TYPE|PROTOCOL)=(.*)$/i);if(m)current[({PORTNUM:'id',ID:'name',DRIVER:'driver',TYPE:'type',PROTOCOL:'protocol'})[m[1].toUpperCase()]]=m[1].toUpperCase()==='PORTNUM'?Number(m[2]):m[2];}} const radioPorts=ports.filter(p=>!/TELNET|AXIP|LOOPBACK/i.test(p.driver)&&p.driver); const nodeCall=value('NODECALL'),sysop=users[0]; return {nodeCall,call:sysop?.call||nodeCall.replace(/-\d+$/,''),alias:value('NODEALIAS'),locator:value('LOCATOR'),apps,ports,radioPorts,users,httpPort:Number(value('HTTPPORT'))||null,telnetPort:Number(value('TCPPORT'))||null,monitorPort:Number(value('FBBPORT'))||null,winlink:value('CMS')==='1'}; } export async function loadConfig(){ const dir=path.dirname(fileURLToPath(import.meta.url));let saved={}; try{saved=JSON.parse(await readFile(process.env.BPQ_CONSOLE_CONFIG||path.join(dir,'console.config.json'),'utf8'));}catch(e){if(e.code!=='ENOENT')throw e;} const root=process.env.BPQ_ROOT||saved.bpqRoot||path.join(process.env.APPDATA||path.join(os.homedir(),'AppData','Roaming'),'BPQ32'); const detected=parseConfig(await readFile(path.join(root,'BPQ32.cfg'),'utf8')); if(!detected.users.length)throw new Error('Nessun utente Telnet sysop in BPQ32.cfg'); const radio=detected.radioPorts.find(p=>p.id===saved.radioPort)||detected.radioPorts[0]||null; const httpPort=saved.httpPort||detected.httpPort;if(!httpPort)throw new Error('HTTPPORT BPQ non rilevata: eseguire Install.ps1'); const features={terminal:!!detected.telnetPort,bbs:detected.apps.some(a=>a.command==='BBS'),chat:detected.apps.some(a=>a.command==='CHAT')&&!!detected.telnetPort,monitor:!!detected.monitorPort,winlink:detected.winlink&&detected.apps.some(a=>a.command==='BBS'),vara:!!radio&&/^VARA/i.test(radio.driver),radio:!!radio}; return {...detected,...saved,root,httpPort,radio,features,port:Number(process.env.BPQ_CONSOLE_PORT||saved.port||8090),sysop:detected.users.find(u=>u.user===saved.sysopUser)||detected.users[0]}; } export function publicConfig(c){return {call:c.sysop.call,nodeCall:c.nodeCall,alias:c.alias,locator:c.locator,apps:c.apps,radio:c.radio,ports:c.ports,features:c.features,forwardPeer:c.forwardPeer||'',cmsPeer:c.cmsPeer||'RMS'};} export function ownCall(sender,call){return !!call&&String(sender||'').replace(/^RMS[/:]/i,'').toUpperCase().split('@')[0].replace(/-\d+$/,'')===String(call).toUpperCase().split('@')[0].replace(/-\d+$/,'');}