|
|
|
@ -1,32 +1,98 @@
|
|
|
|
|
const MailListener = require("mail-listener2")
|
|
|
|
|
const ImapSimple = require('imap-simple')
|
|
|
|
|
const mailparser = require('mailparser')
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
if (!fs.existsSync('./credentials.json')) fs.copyFileSync('./credentials.default.json', './credentials.json')
|
|
|
|
|
let $ImapConnection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const config = require('./config.json')
|
|
|
|
|
config.imap = require('./credentials.json')
|
|
|
|
|
const credentials = require('./credentials.json')
|
|
|
|
|
|
|
|
|
|
const nodemailer = require("nodemailer")
|
|
|
|
|
config.smtp = Object.assign({}, credentials, credentials.smtp)
|
|
|
|
|
let smtpTransporter = nodemailer.createTransport({
|
|
|
|
|
host: config.smtp.host,
|
|
|
|
|
port: config.smtp.port,
|
|
|
|
|
secure: config.smtp.tls,
|
|
|
|
|
auth: {
|
|
|
|
|
user: config.smtp.user, // generated ethereal user
|
|
|
|
|
pass: config.smtp.password, // generated ethereal password
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
config.imap = Object.assign({}, credentials, credentials.imap)
|
|
|
|
|
|
|
|
|
|
config.imap.username = config.imap.user
|
|
|
|
|
//config.onmail = () => ScanUnread()
|
|
|
|
|
const io = require("socket.io-client")
|
|
|
|
|
const axios = require('axios')
|
|
|
|
|
const socket = io(new URL(config.pager.url).origin)
|
|
|
|
|
|
|
|
|
|
const mailListener = new MailListener({
|
|
|
|
|
...config.imap,
|
|
|
|
|
connTimeout: 10000,
|
|
|
|
|
authTimeout: 10000,
|
|
|
|
|
//debug: console.log,
|
|
|
|
|
tlsOptions: { rejectUnauthorized: false },
|
|
|
|
|
mailbox: "INBOX",
|
|
|
|
|
searchFilter: ["UNSEEN"],
|
|
|
|
|
markSeen: true,
|
|
|
|
|
fetchUnreadOnStart: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const searchCriteria = ['UNSEEN']
|
|
|
|
|
const fetchOptions = {
|
|
|
|
|
bodies: ['HEADER', 'TEXT', ''],
|
|
|
|
|
markSeen: false,
|
|
|
|
|
markSeen: true,
|
|
|
|
|
struct: true,
|
|
|
|
|
}
|
|
|
|
|
const ignoreImapIDs = []
|
|
|
|
|
function main() {
|
|
|
|
|
ImapSimple
|
|
|
|
|
/* ImapSimple
|
|
|
|
|
.connect(config)
|
|
|
|
|
.then((connection) => {
|
|
|
|
|
$ImapConnection = connection
|
|
|
|
|
})
|
|
|
|
|
.then(ScanUnread)
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ingressMap = {}
|
|
|
|
|
socket.on('msgmgr:event', async (eventType, eventData) => {
|
|
|
|
|
console.log(eventType, eventData)
|
|
|
|
|
switch (eventType) {
|
|
|
|
|
case 'status': {
|
|
|
|
|
const [msgId, uuid, status] = eventData
|
|
|
|
|
ingressMap[ msgId ][2].push(`${ uuid } is ${ status }`)
|
|
|
|
|
} break;
|
|
|
|
|
case 'read':
|
|
|
|
|
ingressMap[ eventData ][2].push('read')
|
|
|
|
|
break;
|
|
|
|
|
case 'response': {
|
|
|
|
|
const [msgId, response] = eventData
|
|
|
|
|
if (!!ingressMap[msgId]) {
|
|
|
|
|
ingressMap[ msgId ][2].push(`${ uuid } response ${ response }`)
|
|
|
|
|
//const [ replyTo, subject, logArray ] = ingressMap[msgId]
|
|
|
|
|
let info = await smtpTransporter.sendMail({
|
|
|
|
|
from: `"msg-email--smartpager 👻" <${ config.smtp.user }>`,
|
|
|
|
|
to: ingressMap[msgId][ 0 ],
|
|
|
|
|
subject: `Response [${ ingressMap[msgId][ 1 ] }] ${ response.toString() }`,
|
|
|
|
|
text: `Response: ${ response.toString() }\n${ ingressMap[msgId][ 2 ].join('\n') }`,
|
|
|
|
|
inReplyTo: ingressMap[msgId][ 3 ]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log("Message sent: %s", info.messageId, ingressMap[ msgId ]);
|
|
|
|
|
}
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function sendPage(payload) {
|
|
|
|
|
console.log(payload)
|
|
|
|
|
await axios.post(config.pager.url, Object.assign({ ...config.pager.params }, { payload }))
|
|
|
|
|
return (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
|
|
|
|
@ -40,16 +106,30 @@ async function processMail(mail) {
|
|
|
|
|
let res = checkMatch(mail, processCommand)
|
|
|
|
|
if (res) {
|
|
|
|
|
if (!!processCommand.format && !processCommand.ignore) {
|
|
|
|
|
console.log(mail)
|
|
|
|
|
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)
|
|
|
|
|
payload = payload.replace("[fromName]", mail.from[0].name || mail.from[0].address)
|
|
|
|
|
payload = payload.replace("[fromAddress]", mail.from[0].address)
|
|
|
|
|
sendPage(payload)
|
|
|
|
|
.then(result => {
|
|
|
|
|
if (!!processCommand.duplexReply) {
|
|
|
|
|
ingressMap[ result.data ] = [
|
|
|
|
|
mail.from[0].address,
|
|
|
|
|
mail.subject,
|
|
|
|
|
[],
|
|
|
|
|
mail.messageId
|
|
|
|
|
]
|
|
|
|
|
console.log('Duplex Handling Active', result.data, mail.from[0].address)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
function ScanUnread() {
|
|
|
|
|
return $ImapConnection.openBox('INBOX')
|
|
|
|
|
.then(() => $ImapConnection.search(searchCriteria, fetchOptions))
|
|
|
|
@ -68,6 +148,28 @@ function ScanUnread() {
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
mailListener.on("server:connected", () => {
|
|
|
|
|
console.log("imapConnected")
|
|
|
|
|
})
|
|
|
|
|
mailListener.on("server:disconnected", () => {
|
|
|
|
|
console.log("imapDisconnected")
|
|
|
|
|
require('process').exit()
|
|
|
|
|
})
|
|
|
|
|
mailListener.on("error", (err) => {
|
|
|
|
|
console.log(err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mailListener.on("mail", async (mail, seqno, attributes) => {
|
|
|
|
|
//if (mail.headers.from == "eBay Kleinanzeigen <noreply@ebay-kleinanzeigen.de>" && mail.headers.subject == "Aktiviere jetzt dein eBay Kleinanzeigen Konto") {
|
|
|
|
|
console.log("emailParsed", mail.headers.subject, mail.headers.to)
|
|
|
|
|
processMail(mail)
|
|
|
|
|
//await fuckEcho(mail.html, mail.headers.to)
|
|
|
|
|
// await registrator.handleMail(mail.html, mail.headers.to)
|
|
|
|
|
//}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mailListener.start()
|
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -105,4 +207,4 @@ appConfig.post('/restart', (req, res) => {
|
|
|
|
|
process.exit(1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
appConfig.listen(3010)
|
|
|
|
|
appConfig.listen(3010, '0.0.0.0' || config.host || '127.0.0.1')
|
|
|
|
|