added simpler mode, which just counts unread emails

master
cheetah 4 years ago
parent 03864cdc55
commit 25e2cb5777

@ -6,12 +6,16 @@
"routing": { "routing": {
"device": "generic", "device": "generic",
"connectors": [ "connectors": [
[ "pocsag", "77174A" ] [ "pocsag", "133701D" ]
] ]
} }
} }
}, },
"handling": { "handling": {
"default": "[subject] - [from]" "onlySimpleCounter": false,
"onlySimpleCounterFormat": "[unreadCount] ongelezen emails",
"processing": {
"default": "[subject] - [from]"
}
} }
} }

@ -10,43 +10,47 @@ const axios = require('axios')
const searchCriteria = ['UNSEEN'] const searchCriteria = ['UNSEEN']
const fetchOptions = { const fetchOptions = {
bodies: ['HEADER', 'TEXT', ''], bodies: ['HEADER', 'TEXT', ''],
markSeen: false, markSeen: false,
struct: true, struct: true,
} }
const ignoreImapIDs = [] const ignoreImapIDs = []
function main() { function main() {
ImapSimple ImapSimple
.connect(config) .connect(config)
.then((connection) => { .then((connection) => {
$ImapConnection = connection $ImapConnection = connection
}) })
.then(ScanUnread) .then(ScanUnread)
} }
async function sendPage(payload) { 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) { async function processMail(mail) {
let handling = "default" let handling = "default"
let payload = config.handling[handling] let payload = config.handling.processing[handling]
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.name || mail.from.value.address)
payload = payload.replace("[fromAddress]", mail.from.value.address) payload = payload.replace("[fromAddress]", mail.from.value.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)
for (let mail of emails) { if (config.handling.onlySimpleCounter === true) {
const all = mail.parts.filter(x=>x.which=='')[0] sendPage(config.handling.onlySimpleCounterFormat.replace("[unreadCount]", emails.length))
const idHeader = `Imap-Id: ${ mail.attributes.uid }\r\n` } else {
if (ignoreImapIDs.indexOf(mail.attributes.uid) > -1) continue // Skip already notified mails for (let mail of emails) {
ignoreImapIDs.push(mail.attributes.uid) const all = mail.parts.filter(x => x.which == '')[0]
mailparser.simpleParser(idHeader + all.body).then(processMail) 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() main()
Loading…
Cancel
Save