From b6d5238bd5da3aa83392ea87ef9b543b122fbfe9 Mon Sep 17 00:00:00 2001 From: cheetah Date: Fri, 19 Mar 2021 19:41:41 +0000 Subject: [PATCH] first commit --- .gitignore | 3 +++ README.md | 1 + config.json | 17 ++++++++++++++ credentials.default.json | 8 +++++++ index.js | 51 ++++++++++++++++++++++++++++++++++++++++ package.json | 25 ++++++++++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.json create mode 100644 credentials.default.json create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e78de71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +credentials.json +node_modules +package-lock.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..92b83e4 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# msg-email diff --git a/config.json b/config.json new file mode 100644 index 0000000..70cc629 --- /dev/null +++ b/config.json @@ -0,0 +1,17 @@ +{ + "pager": { + "url": "http://127.0.0.1:3000/api/message/advanced", + "params": { + "type": "simple", + "routing": { + "device": "generic", + "connectors": [ + [ "pocsag", "77174D" ] + ] + } + } + }, + "handling": { + "default": "[subject] - [from]" + } +} \ No newline at end of file diff --git a/credentials.default.json b/credentials.default.json new file mode 100644 index 0000000..db1ce59 --- /dev/null +++ b/credentials.default.json @@ -0,0 +1,8 @@ +{ + "user": "mail@domein.tld", + "password": "wachtwoord", + "host": "imap.domein.tld", + "port": 993, + "tls": true, + "authTimeout": 3000 +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..17e96a4 --- /dev/null +++ b/index.js @@ -0,0 +1,51 @@ +const ImapSimple = require('imap-simple') +const mailparser = require('mailparser') +let $ImapConnection + +const config = require('./config.json') +config.imap = require('./credentials.json') +config.onmail = () => ScanUnread() + +const axios = require('axios') + +const searchCriteria = ['UNSEEN'] +const fetchOptions = { + bodies: ['HEADER', 'TEXT', ''], + markSeen: false, + struct: true, +} + +function main() { + 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 })) +} +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) + payload = payload.replace("[fromAddress]", mail.from.value.address) + console.log(mail.from.value) + 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` + mailparser.simpleParser(idHeader + all.body).then(processMail) + } + }) +} +main() \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..96fb161 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "msg-email", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/smartpager-network/msg-email.git" + }, + "author": "catSIXe", + "license": "ISC", + "bugs": { + "url": "https://github.com/smartpager-network/msg-email/issues" + }, + "homepage": "https://github.com/smartpager-network/msg-email#readme", + "dependencies": { + "axios": "^0.21.1", + "imap-simple": "^5.0.0", + "mailparser": "^3.1.0", + "node-imap": "^0.9.6" + } +}