Impeccable
This commit is contained in:
49
read-inbox.mjs
Normal file
49
read-inbox.mjs
Normal file
@@ -0,0 +1,49 @@
|
||||
import tls from 'node:tls';
|
||||
|
||||
function readMessages(seqs) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = tls.connect({ host: 'imap.qrmaster.net', port: 993 }, () => {
|
||||
let buf = '';
|
||||
let step = 0;
|
||||
const results = {};
|
||||
|
||||
socket.on('data', (chunk) => {
|
||||
buf += chunk.toString();
|
||||
const lines = buf.split('\r\n');
|
||||
buf = lines.pop();
|
||||
|
||||
for (const line of lines) {
|
||||
if (step === 0 && line.includes('* OK')) {
|
||||
socket.write(`A1 LOGIN timo@qrmaster.net fiesta\r\n`);
|
||||
step = 1;
|
||||
} else if (step === 1 && line.startsWith('A1 OK')) {
|
||||
socket.write(`A2 SELECT INBOX\r\n`);
|
||||
step = 2;
|
||||
} else if (step === 2 && line.startsWith('A2 OK')) {
|
||||
socket.write(`A3 FETCH ${seqs.join(',')} (BODY.PEEK[1])\r\n`);
|
||||
step = 3;
|
||||
} else if (step === 3) {
|
||||
if (line.startsWith('A3 OK')) {
|
||||
socket.write(`A4 LOGOUT\r\n`);
|
||||
resolve(results);
|
||||
}
|
||||
const m = line.match(/^\* (\d+) FETCH/);
|
||||
if (m) results[m[1]] = { body: '' };
|
||||
const curr = Object.keys(results).at(-1);
|
||||
if (curr && line && !line.match(/^\* \d+ FETCH/) && !line.startsWith('A3') && line !== ')') {
|
||||
results[curr].body += line + '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('error', reject);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const r = await readMessages([954, 990, 997]);
|
||||
for (const [seq, msg] of Object.entries(r)) {
|
||||
console.log(`\n=== SEQ ${seq} ===`);
|
||||
console.log(msg.body.slice(0, 1500));
|
||||
}
|
||||
Reference in New Issue
Block a user