diff --git a/chat.mjs b/chat.mjs index 8904c1b..b8f605e 100644 --- a/chat.mjs +++ b/chat.mjs @@ -21,7 +21,7 @@ export class Chat { if(line==='Active Topics are:'){this.topics=[];this.section='topics';return;} if(/^\d+ Node\(s\)|^Here |^User [A-Z0-9-]+|^Links Defined:/.test(line)){if(line==='Links Defined:'){this.section='links';this.links=[]}else this.section='';return;} if(this.section==='topics'){if(/^[A-Za-z0-9_-]+$/.test(line)&&line!==this.call){this.topics.push(line);}return;} - if(this.section==='links'){const m=line.match(/^([A-Z0-9]+-\d+)\s+(Open|Connect failed|Connecting|Closed)(.*)$/i);if(m){this.links.push({call:m[1],state:m[2],detail:m[3].trim()});return;}this.section='';} + if(this.section==='links'){const m=line.match(/^([A-Z0-9]+(?:-\d+)?)\s+(Open|Connect failed|Connecting|Closed|Idle)(.*)$/i);if(m){this.links.push({call:m[1],state:m[2],detail:m[3].trim()});return;}this.section='';} if(/^(Nodes via|\s*[A-Z0-9#]{3,6}(?:\s+[A-Z0-9#]{3,6}){2,})/.test(line))return; if(/^[A-Z0-9-]+ Chat Server|^Type \/h|^Bringing up links|^This may take|^The \/p|^[A-Z0-9]{1,6}:/.test(line))return; const message=line.match(/^(?:\d{2}:\d{2}(?::\d{2})?\s+)?(?:<|\[)?([A-Z0-9]*\d[A-Z0-9]*(?:-\d+)?)(?:>|\])?\s*(?:\([^)]*\))?[: ]+(.+)$/); diff --git a/public/enhancements.js b/public/enhancements.js index ed1e22d..0b22d2b 100644 --- a/public/enhancements.js +++ b/public/enhancements.js @@ -35,7 +35,7 @@ function renderChat(data){ $$('.chat-user').forEach(b=>b.onclick=()=>{$('#chat-recipient').value=b.dataset.call===station.call?'':b.dataset.call;$('#chat-input').placeholder=b.dataset.call===station.call?'Scrivi alla stanza…':'Messaggio privato a '+b.dataset.call+'…';$('#chat-input').focus();}); $('#chat-topics').innerHTML=[...new Set(['General',...data.topics,data.topic])].map(t=>``).join(''); $$('[data-topic]').forEach(b=>b.onclick=async()=>{try{await api('/chat/topic',{method:'POST',body:JSON.stringify({topic:b.dataset.topic})});chatFollowing=true;chatUnread=0;pollChat();}catch(e){toast(e.message);}}); - $('#chat-links').innerHTML=online?data.links.map(l=>``).join('')||'':''; + $('#chat-links').innerHTML=online?data.links.map(l=>``).join('')||'':''; const events=data.events.filter(e=>e.id>chatLastId),initial=chatLastId===0; if(events.length){if(chatArea.querySelector('.chat-empty'))chatArea.replaceChildren();const fragment=document.createDocumentFragment();let added=0; for(const event of events){chatLastId=Math.max(chatLastId,event.id);if(event.type==='system'&&/^[A-Z0-9]{3,6}(?:\s+[A-Z0-9]{3,6})*$/.test(event.text))continue; diff --git a/test.mjs b/test.mjs index 7ddfcb7..243604f 100644 --- a/test.mjs +++ b/test.mjs @@ -90,3 +90,18 @@ test('7plus decodes a known binary line and verifies both CRC fields',async()=>{ test('7plus combines out-of-order parts and refuses incomplete or damaged groups',async()=>{const {scanSevenPlus,assembleSevenPlus}=await import('./sevenplus.mjs');const first=scanSevenPlus(sevenFixture(1),1),second=scanSevenPlus(sevenFixture(2),2);assert.equal(first[0].issue,null);assert.equal(second[0].issue,null);const incomplete=assembleSevenPlus(first);assert.equal(incomplete.metadata.state,'incomplete');assert.deepEqual(incomplete.metadata.missingParts,[2]);assert.equal(incomplete.file,null);const done=assembleSevenPlus([...second,...first]);assert.equal(done.metadata.state,'complete');assert.equal(done.file.size,100);assert.deepEqual(done.file.data,Buffer.concat([Buffer.from(sevenDecodedHex,'hex'),Buffer.from(sevenDecodedHex,'hex')]).subarray(0,100));const damaged=sevenFixture(2);damaged[damaged.indexOf(Buffer.from(sevenLineHex,'hex'))]^=1;const failed=assembleSevenPlus([...first,...scanSevenPlus(damaged,2)]);assert.equal(failed.metadata.state,'damaged');assert.deepEqual(failed.metadata.damagedParts,[2]);assert.equal(failed.file,null);}); test('7plus isolates senders and rejects unsafe metadata before allocating data',async()=>{const {scanSevenPlus,assembleSevenPlus}=await import('./sevenplus.mjs');const a=scanSevenPlus(sevenFixture(1,'N0TEST'),1),b=scanSevenPlus(sevenFixture(2,'N1OTHER'),2);assert.notEqual(a[0].key,b[0].key);assert.equal(assembleSevenPlus([...a,...b]).file,null);const bad={...a[0],total:999999999,size:1e15};assert.equal(assembleSevenPlus([bad]).metadata.state,'damaged');const damagedHeader=sevenFixture(1);damagedHeader[damagedHeader.indexOf(Buffer.from('PAYLOAD.BIN'))]^=1;assert.ok(scanSevenPlus(damagedHeader,1)[0].issue);}); test('7plus includes only currently visible messages and invalidates a removed part',async()=>{const {SevenPlus}=await import('./sevenplus.mjs');const fs=await import('node:fs/promises'),path=await import('node:path'),os=await import('node:os');const tmp=await fs.mkdtemp(path.join(os.tmpdir(),'bpq-sevenplus-test-'));try{await fs.mkdir(path.join(tmp,'BPQMailChat','Mail'),{recursive:true});await fs.writeFile(path.join(tmp,'BPQMailChat','Mail','m_000001.mes'),sevenFixture(1));await fs.writeFile(path.join(tmp,'BPQMailChat','Mail','m_000002.mes'),sevenFixture(2));const codec=new SevenPlus(tmp);const first=await codec.forMessage(1,[{id:1}]);assert.equal(first.file,null);const full=await codec.forMessage(1,[{id:1},{id:2}]);assert.equal(full.file.size,100);const hidden=await codec.forMessage(1,[{id:1}]);assert.equal(hidden.file,null);assert.equal(hidden.metadata.state,'incomplete');}finally{await fs.rm(tmp,{recursive:true,force:true})}}); + + +test('idle chat links stay in the roster, without conversation messages',()=>{ + const c=new Chat(async()=>({})); + try { + for(let i=0;i<2;i++){ + c.line('Links Defined:');c.line('HB9ON-2 Idle');c.line('IW2OHX-5 Open RTT 1');c.line('N0TEST Connecting'); + assert.deepEqual(c.links,[{call:'HB9ON-2',state:'Idle',detail:''},{call:'IW2OHX-5',state:'Open',detail:'RTT 1'},{call:'N0TEST',state:'Connecting',detail:''}]); + assert.equal(c.events.length,0); + } + c.line('K1ABC I am idle today'); + assert.equal(c.events.at(-1).text,'I am idle today'); + assert.equal(c.events.at(-1).type,'message'); + } finally {clearInterval(c.timer);} +});