Dev #1

Merged
redrockjs merged 8 commits from dev into main 2024-05-12 10:57:25 +00:00
2 changed files with 6 additions and 58 deletions
Showing only changes of commit db6f3e95fd - Show all commits

View File

@@ -1,6 +1,6 @@
{ {
"name": "requestbot", "name": "requestbot",
"version": "1.0.0", "version": "0.1.0",
"description": "", "description": "",
"main": "/src/index.ts", "main": "/src/index.ts",
"scripts": { "scripts": {

View File

@@ -1,6 +1,5 @@
const {ImapFlow} = require('imapflow'); const {ImapFlow} = require('imapflow');
const simpleParser = require('mailparser').simpleParser const simpleParser = require('mailparser').simpleParser
const dotenv = require('dotenv') const dotenv = require('dotenv')
require('dotenv').config() require('dotenv').config()
@@ -19,76 +18,25 @@ const client = new ImapFlow({
const markString = 'test' const markString = 'test'
const concat_RS = (stream: any) => new Promise((res, rej) => {
let buffers: any = [];
stream.on("data", (data: any) => {
buffers.push(data);
});
stream.on("end", () => {
res(Buffer.concat(buffers));
});
});
const main = async () => { const main = async () => {
await client.connect(); // Wait until client connects and authorizes await client.connect(); // Wait until client connects and authorizes
await client.mailboxOpen('INBOX'); // Open mailbox
//let lock = await client.getMailboxLock('INBOX'); // Select and lock a mailbox. Throws if mailbox does not exist
let mailbox = await client.mailboxOpen('INBOX');
try { try {
// // fetch latest message source
// // client.mailbox includes information about currently selected mailbox
// // "exists" value is also the largest sequence number available in the mailbox
// let message = await client.fetchOne(client.mailbox.exists, { source: true });
// console.log(message.source.toString());
//
// // list subjects for all messages
// // uid value is always included in FETCH response, envelope strings are in unicode.
// for await (let message of client.fetch('1:*', { envelope: true })) {
// console.log(`${message.uid}: ${message.envelope.subject}`);
// }
// await client.messageFlagsRemove({seen: false}, ["\\Seen"], {uid:true}); // mark all seen messages as unseen by removing Seen flag let listSeenMsgId = await client.search({seen: false}); // search only unseen messages
// await client.messageFlagsSet({seen: false}, ["\\Seen"]); // mark all unseen messages as seen (and remove other flags)
//let list = await client.search({seen: false}); // find all unseen messages
//let lastMsg = await client.fetchOne('*', {uid: true}) // fetch one message
let listSeenMsgId = await client.search({seen: false});
console.log('📌', 'LIST:', listSeenMsgId) console.log('📌', 'LIST:', listSeenMsgId)
// for await (let msg of client.fetch(listSeenMsgId, {uid: false})){
// console.log(msg.envelope.subject);
// }
for await (let message of client.fetch(listSeenMsgId, for await (let message of client.fetch(listSeenMsgId,
{ {
envelope: true, envelope: true,
//bodyParts: true, bodyParts: true,
bodyStructure: true bodyStructure: true
})) { })) {
if (message.envelope.subject.includes(markString)) {
console.log('📌', message.envelope.subject, message.seq, message.bodyStructure);
let bs;
if (message.bodyStructure.childNodes) {
let nodeHTML = message.bodyStructure?.childNodes?.find((node: any) => node.type?.toLowerCase() === 'text/html');
let nodePlainText = message.bodyStructure?.childNodes?.find((node: any) => node.type?.toLowerCase() === 'text/plain');
bs = nodePlainText ? nodePlainText : nodeHTML;
} else {
if (message.bodyStructure.type === 'text/plain') {
bs = {
part: "1",
type: "text/plain"
}
}
}
}
} }
} finally { } catch (e) {
console.log(e)
//lock.release(); // Make sure lock is released, otherwise next `getMailboxLock()` never returns
} }
await client.logout(); // log out and close connection await client.logout(); // log out and close connection