From 8ccc849c397b5193b847c45b9efe18b5eac7c28b Mon Sep 17 00:00:00 2001 From: cheetah Date: Wed, 7 Apr 2021 21:00:13 +0000 Subject: [PATCH] added new features, config layout changed --- config.json | 21 ++++++++++++++------- index.js | 34 ++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/config.json b/config.json index 939ef8f..dd2c3ec 100644 --- a/config.json +++ b/config.json @@ -2,11 +2,15 @@ "pager": { "url": "http://127.0.0.1:3000/api/message/advanced", "params": { - "type": "simple", + "type": "duplex", "routing": { - "device": "generic", + "device": "birdyslim", "connectors": [ - [ "pocsag", "133701D" ] + [ "dapnet", "dl-all#DXxxx" ], + [ "ecityruf", "2900000" ], + [ "dapnet", "dl-all#DLxxx" ], + [ "pocsag", "133701D" ], + [ "dummy", "works" ] ] } } @@ -14,8 +18,11 @@ "handling": { "onlySimpleCounter": false, "onlySimpleCounterFormat": "[unreadCount] ongelezen emails", - "processing": { - "default": "[subject] - [from]" - } + "processing": [ + { "subjectContains": "data transferred" , "format": "[subject] - CF" }, + { "mxdomainExact": "spamdomein.tld" , "ignore": true }, + { "mxdomainContains": "paypal.nl" , "format": "PP: [subject]" }, + { "ignore": true } + ] } -} \ No newline at end of file +} diff --git a/index.js b/index.js index 8acd991..202aff5 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ let $ImapConnection const config = require('./config.json') config.imap = require('./credentials.json') -config.onmail = () => ScanUnread() +//config.onmail = () => ScanUnread() const axios = require('axios') @@ -24,27 +24,41 @@ function main() { .then(ScanUnread) } async function sendPage(payload) { + console.log(payload) await axios.post(config.pager.url, Object.assign({ ...config.pager.params }, { payload })) } +function checkMatch(mail, cmd) { + if (!!cmd.subjectContains && !(mail.subject.indexOf(cmd.subjectContains) > -1)) return 0 // subjectContains does not match + if (!!cmd.mxdomainExact && !(mail.from.value[0].address)) return 0 + if (!!cmd.mxdomainContains && !(mail.from.value[0].address.indexOf(cmd.mxdomainContains) > -1)) return 0 // mxdomainContains does not match + + return !!cmd.ignore ? 1 : 2 // if ignore is set, lets break with 1, otherwise send page with 2 +} async function processMail(mail) { - 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) + for (processCommand of config.handling.processing) { + let res = checkMatch(mail, processCommand) + if (res) { + if (!!processCommand.format && !processCommand.ignore) { + let payload = processCommand.format + payload = payload.replace("[subject]", mail.subject) + payload = payload.replace("[from]", mail.from.text) + payload = payload.replace("[fromName]", mail.from.value[0].name || mail.from.value[0].address) + payload = payload.replace("[fromAddress]", mail.from.value[0].address) + sendPage(payload) + } + } + } } function ScanUnread() { return $ImapConnection.openBox('INBOX') .then(() => $ImapConnection.search(searchCriteria, fetchOptions)) .then((emails) => { console.log('unreadCount:', emails.length) - if (config.handling.onlySimpleCounter === true) { + if (emails.length > 0 && 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 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)