2013-12-08 03:56:03 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-09-14 07:51:51 +03:00
|
|
|
/* eslint-disable no-console */
|
2015-08-17 19:48:34 +03:00
|
|
|
var config = require('../config').getProperties()
|
2013-12-08 03:56:03 +04:00
|
|
|
|
|
|
|
// SMTP half
|
|
|
|
|
2014-07-20 23:18:44 +04:00
|
|
|
var MailParser = require('mailparser').MailParser
|
2013-12-17 09:36:42 +04:00
|
|
|
var users = {}
|
|
|
|
|
|
|
|
function emailName(emailAddress) {
|
2017-02-04 00:29:46 +03:00
|
|
|
var utf8Address = Buffer.from(emailAddress, 'binary').toString('utf-8')
|
|
|
|
return utf8Address.split('@')[0]
|
2013-12-17 09:36:42 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 23:18:44 +04:00
|
|
|
require('simplesmtp').createSimpleServer(
|
|
|
|
{
|
2015-06-19 02:12:42 +03:00
|
|
|
SMTPBanner: 'FXATEST'
|
2014-07-20 23:18:44 +04:00
|
|
|
},
|
|
|
|
function (req) {
|
2017-02-04 00:29:46 +03:00
|
|
|
var mp = new MailParser({ defaultCharset: 'utf-8' })
|
2014-07-20 23:18:44 +04:00
|
|
|
mp.on('end',
|
2013-12-17 09:36:42 +04:00
|
|
|
function (mail) {
|
|
|
|
var link = mail.headers['x-link']
|
|
|
|
var rc = mail.headers['x-recovery-code']
|
2016-10-12 03:26:37 +03:00
|
|
|
var rul = mail.headers['x-report-signin-link']
|
|
|
|
var uc = mail.headers['x-unblock-code']
|
2013-12-17 09:36:42 +04:00
|
|
|
var vc = mail.headers['x-verify-code']
|
2014-01-14 23:07:59 +04:00
|
|
|
var name = emailName(mail.headers.to)
|
2013-12-17 09:36:42 +04:00
|
|
|
if (vc) {
|
2014-01-10 00:09:49 +04:00
|
|
|
console.log('\x1B[32m', link, '\x1B[39m')
|
2013-12-17 09:36:42 +04:00
|
|
|
}
|
|
|
|
else if (rc) {
|
2014-07-20 23:18:44 +04:00
|
|
|
console.log('\x1B[34m', link, '\x1B[39m')
|
2013-12-17 09:36:42 +04:00
|
|
|
}
|
2016-10-12 03:26:37 +03:00
|
|
|
else if (uc) {
|
|
|
|
console.log('\x1B[36mUnblock code:', uc, '\x1B[39m')
|
|
|
|
console.log('\x1B[36mReport link:', rul, '\x1B[39m')
|
|
|
|
|
|
|
|
}
|
2013-12-17 09:36:42 +04:00
|
|
|
else {
|
2014-01-10 00:09:49 +04:00
|
|
|
console.error('\x1B[31mNo verify code match\x1B[39m')
|
2014-07-20 23:18:44 +04:00
|
|
|
console.error(mail)
|
2013-12-17 09:36:42 +04:00
|
|
|
}
|
2014-01-14 23:07:59 +04:00
|
|
|
if (users[name]) {
|
|
|
|
users[name].push(mail)
|
|
|
|
} else {
|
|
|
|
users[name] = [mail]
|
|
|
|
}
|
2013-12-08 04:54:15 +04:00
|
|
|
}
|
2013-12-17 09:36:42 +04:00
|
|
|
)
|
2014-07-20 23:18:44 +04:00
|
|
|
req.pipe(mp)
|
|
|
|
req.accept()
|
2013-12-08 03:56:03 +04:00
|
|
|
}
|
2014-07-20 23:18:44 +04:00
|
|
|
).listen(config.smtp.port, config.smtp.host)
|
2013-12-08 03:56:03 +04:00
|
|
|
|
|
|
|
// HTTP half
|
|
|
|
|
2014-07-20 23:18:44 +04:00
|
|
|
var hapi = require('hapi')
|
2015-03-04 02:13:21 +03:00
|
|
|
var api = new hapi.Server()
|
|
|
|
api.connection({
|
|
|
|
host: config.smtp.api.host,
|
|
|
|
port: config.smtp.api.port
|
|
|
|
})
|
2013-12-08 03:56:03 +04:00
|
|
|
|
|
|
|
function loop(email, cb) {
|
2013-12-17 09:36:42 +04:00
|
|
|
var mail = users[email]
|
2017-02-08 18:39:56 +03:00
|
|
|
if (! mail) {
|
2013-12-08 03:56:03 +04:00
|
|
|
return setTimeout(loop.bind(null, email, cb), 50)
|
|
|
|
}
|
2013-12-17 09:36:42 +04:00
|
|
|
cb(mail)
|
2013-12-08 03:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
api.route(
|
2013-12-17 09:36:42 +04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
path: '/mail/{email}',
|
2014-01-31 03:01:31 +04:00
|
|
|
handler: function (request, reply) {
|
2013-12-17 09:36:42 +04:00
|
|
|
loop(
|
2017-02-04 00:29:46 +03:00
|
|
|
decodeURIComponent(request.params.email),
|
2013-12-17 09:36:42 +04:00
|
|
|
function (emailData) {
|
2014-01-31 03:01:31 +04:00
|
|
|
reply(emailData)
|
2013-12-17 09:36:42 +04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'DELETE',
|
|
|
|
path: '/mail/{email}',
|
2014-01-31 03:01:31 +04:00
|
|
|
handler: function (request, reply) {
|
2017-02-04 00:29:46 +03:00
|
|
|
delete users[decodeURIComponent(request.params.email)]
|
2014-01-31 03:01:31 +04:00
|
|
|
reply()
|
2013-12-17 09:36:42 +04:00
|
|
|
}
|
2013-12-08 03:56:03 +04:00
|
|
|
}
|
2013-12-17 09:36:42 +04:00
|
|
|
]
|
2013-12-08 03:56:03 +04:00
|
|
|
)
|
|
|
|
|
2016-09-14 03:52:06 +03:00
|
|
|
api.start(function () {
|
|
|
|
console.log('mail_helper started...')
|
|
|
|
})
|