From 25e2cb5777c78b96e80d1f8f13008ee54cd02c1e Mon Sep 17 00:00:00 2001 From: cheetah Date: Sat, 20 Mar 2021 14:21:45 +0000 Subject: [PATCH] added simpler mode, which just counts unread emails --- config.json | 8 +++++-- index.js | 62 ++++++++++++++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/config.json b/config.json index f4d8535..939ef8f 100644 --- a/config.json +++ b/config.json @@ -6,12 +6,16 @@ "routing": { "device": "generic", "connectors": [ - [ "pocsag", "77174A" ] + [ "pocsag", "133701D" ] ] } } }, "handling": { - "default": "[subject] - [from]" + "onlySimpleCounter": false, + "onlySimpleCounterFormat": "[unreadCount] ongelezen emails", + "processing": { + "default": "[subject] - [from]" + } } } \ No newline at end of file diff --git a/index.js b/index.js index 01dfcf1..8acd991 100644 --- a/index.js +++ b/index.js @@ -10,43 +10,47 @@ const axios = require('axios') const searchCriteria = ['UNSEEN'] const fetchOptions = { - bodies: ['HEADER', 'TEXT', ''], - markSeen: false, - struct: true, + bodies: ['HEADER', 'TEXT', ''], + markSeen: false, + struct: true, } const ignoreImapIDs = [] function main() { - ImapSimple - .connect(config) - .then((connection) => { - $ImapConnection = connection - }) - .then(ScanUnread) + ImapSimple + .connect(config) + .then((connection) => { + $ImapConnection = connection + }) + .then(ScanUnread) } async function sendPage(payload) { - await axios.post(config.pager.url, Object.assign({...config.pager.params}, { payload })) + await axios.post(config.pager.url, Object.assign({ ...config.pager.params }, { payload })) } async function processMail(mail) { - let handling = "default" - let payload = config.handling[handling] - payload = payload.replace("[subject]", mail.subject) - payload = payload.replace("[from]", mail.from.text) - payload = payload.replace("[fromName]", mail.from.value.name || mail.from.value.address) - payload = payload.replace("[fromAddress]", mail.from.value.address) - sendPage(payload) + let handling = "default" + let payload = config.handling.processing[handling] + payload = payload.replace("[subject]", mail.subject) + payload = payload.replace("[from]", mail.from.text) + payload = payload.replace("[fromName]", mail.from.value.name || mail.from.value.address) + payload = payload.replace("[fromAddress]", mail.from.value.address) + sendPage(payload) } function ScanUnread() { - return $ImapConnection.openBox('INBOX') - .then(() => $ImapConnection.search(searchCriteria, fetchOptions)) - .then((emails) => { - console.log('unreadCount:', emails.length) - for (let mail of emails) { - const all = mail.parts.filter(x=>x.which=='')[0] - const idHeader = `Imap-Id: ${ mail.attributes.uid }\r\n` - if (ignoreImapIDs.indexOf(mail.attributes.uid) > -1) continue // Skip already notified mails - ignoreImapIDs.push(mail.attributes.uid) - mailparser.simpleParser(idHeader + all.body).then(processMail) - } - }) + return $ImapConnection.openBox('INBOX') + .then(() => $ImapConnection.search(searchCriteria, fetchOptions)) + .then((emails) => { + console.log('unreadCount:', emails.length) + if (config.handling.onlySimpleCounter === true) { + sendPage(config.handling.onlySimpleCounterFormat.replace("[unreadCount]", emails.length)) + } else { + for (let mail of emails) { + const all = mail.parts.filter(x => x.which == '')[0] + const idHeader = `Imap-Id: ${mail.attributes.uid}\r\n` + if (ignoreImapIDs.indexOf(mail.attributes.uid) > -1) continue // Skip already notified mails + ignoreImapIDs.push(mail.attributes.uid) + mailparser.simpleParser(idHeader + all.body).then(processMail) + } + } + }) } main() \ No newline at end of file