added new features, config layout changed

master
cheetah 4 years ago
parent 25e2cb5777
commit 8ccc849c39

@ -2,11 +2,15 @@
"pager": { "pager": {
"url": "http://127.0.0.1:3000/api/message/advanced", "url": "http://127.0.0.1:3000/api/message/advanced",
"params": { "params": {
"type": "simple", "type": "duplex",
"routing": { "routing": {
"device": "generic", "device": "birdyslim",
"connectors": [ "connectors": [
[ "pocsag", "133701D" ] [ "dapnet", "dl-all#DXxxx" ],
[ "ecityruf", "2900000" ],
[ "dapnet", "dl-all#DLxxx" ],
[ "pocsag", "133701D" ],
[ "dummy", "works" ]
] ]
} }
} }
@ -14,8 +18,11 @@
"handling": { "handling": {
"onlySimpleCounter": false, "onlySimpleCounter": false,
"onlySimpleCounterFormat": "[unreadCount] ongelezen emails", "onlySimpleCounterFormat": "[unreadCount] ongelezen emails",
"processing": { "processing": [
"default": "[subject] - [from]" { "subjectContains": "data transferred" , "format": "[subject] - CF" },
} { "mxdomainExact": "spamdomein.tld" , "ignore": true },
{ "mxdomainContains": "paypal.nl" , "format": "PP: [subject]" },
{ "ignore": true }
]
} }
} }

@ -4,7 +4,7 @@ let $ImapConnection
const config = require('./config.json') const config = require('./config.json')
config.imap = require('./credentials.json') config.imap = require('./credentials.json')
config.onmail = () => ScanUnread() //config.onmail = () => ScanUnread()
const axios = require('axios') const axios = require('axios')
@ -24,27 +24,41 @@ function main() {
.then(ScanUnread) .then(ScanUnread)
} }
async function sendPage(payload) { async function sendPage(payload) {
console.log(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 }))
} }
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) { async function processMail(mail) {
let handling = "default" for (processCommand of config.handling.processing) {
let payload = config.handling.processing[handling] 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("[subject]", mail.subject)
payload = payload.replace("[from]", mail.from.text) payload = payload.replace("[from]", mail.from.text)
payload = payload.replace("[fromName]", mail.from.value.name || mail.from.value.address) payload = payload.replace("[fromName]", mail.from.value[0].name || mail.from.value[0].address)
payload = payload.replace("[fromAddress]", mail.from.value.address) payload = payload.replace("[fromAddress]", mail.from.value[0].address)
sendPage(payload) sendPage(payload)
}
}
}
} }
function ScanUnread() { function ScanUnread() {
return $ImapConnection.openBox('INBOX') return $ImapConnection.openBox('INBOX')
.then(() => $ImapConnection.search(searchCriteria, fetchOptions)) .then(() => $ImapConnection.search(searchCriteria, fetchOptions))
.then((emails) => { .then((emails) => {
console.log('unreadCount:', emails.length) 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)) sendPage(config.handling.onlySimpleCounterFormat.replace("[unreadCount]", emails.length))
} else { } else {
for (let mail of emails) { 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` const idHeader = `Imap-Id: ${mail.attributes.uid}\r\n`
if (ignoreImapIDs.indexOf(mail.attributes.uid) > -1) continue // Skip already notified mails if (ignoreImapIDs.indexOf(mail.attributes.uid) > -1) continue // Skip already notified mails
ignoreImapIDs.push(mail.attributes.uid) ignoreImapIDs.push(mail.attributes.uid)

Loading…
Cancel
Save