first commit

master
cheetah 4 years ago
commit b6d5238bd5

3
.gitignore vendored

@ -0,0 +1,3 @@
credentials.json
node_modules
package-lock.json

@ -0,0 +1 @@
# msg-email

@ -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]"
}
}

@ -0,0 +1,8 @@
{
"user": "mail@domein.tld",
"password": "wachtwoord",
"host": "imap.domein.tld",
"port": 993,
"tls": true,
"authTimeout": 3000
}

@ -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()

@ -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"
}
}
Loading…
Cancel
Save