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/. */
|
|
|
|
|
|
|
|
var config = require('../config').root()
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return emailAddress.split('@')[0]
|
|
|
|
}
|
|
|
|
|
2014-07-20 23:18:44 +04:00
|
|
|
require('simplesmtp').createSimpleServer(
|
|
|
|
{
|
|
|
|
SMTPBanner: "FXATEST"
|
|
|
|
},
|
|
|
|
function (req) {
|
2013-12-17 09:36:42 +04:00
|
|
|
var mp = new MailParser({ defaultCharset: 'utf8' })
|
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']
|
|
|
|
var vc = mail.headers['x-verify-code']
|
2015-01-23 17:50:55 +03:00
|
|
|
var uc = mail.headers['x-unlock-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
|
|
|
}
|
2015-01-23 17:50:55 +03:00
|
|
|
else if (uc) {
|
|
|
|
console.log('\x1B[36m %s', link, '\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')
|
2013-12-08 03:56:03 +04:00
|
|
|
var api = hapi.createServer(config.smtp.api.host, config.smtp.api.port)
|
|
|
|
|
|
|
|
function loop(email, cb) {
|
2013-12-17 09:36:42 +04:00
|
|
|
var mail = users[email]
|
|
|
|
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(
|
|
|
|
request.params.email,
|
|
|
|
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) {
|
2013-12-17 09:36:42 +04:00
|
|
|
delete users[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
|
|
|
)
|
|
|
|
|
|
|
|
api.start()
|