2021-04-29 06:07:41 -04:00
|
|
|
const express = require('express')
|
2021-06-04 13:44:23 -04:00
|
|
|
const nodemailer = require('nodemailer')
|
2021-04-29 06:07:41 -04:00
|
|
|
|
|
|
|
require('dotenv').config()
|
|
|
|
|
2021-06-04 14:16:11 -04:00
|
|
|
// handle debug mode
|
2021-06-04 13:44:23 -04:00
|
|
|
let DEBUG
|
|
|
|
|
|
|
|
if (process.env.DEBUG || process.env.CHIPCHOC_DEBUG) {
|
|
|
|
DEBUG = true
|
|
|
|
} else {
|
|
|
|
DEBUG = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG) console.log('Starting ChipChoc in debug mode')
|
|
|
|
|
|
|
|
// set up nodemailer
|
|
|
|
const perumal = nodemailer.createTransport({
|
|
|
|
// (that's the name of badri's postmaster, in case you were wondering ;)
|
|
|
|
host: process.env.CHIPCHOC_EMAIL_HOST,
|
|
|
|
port: process.env.CHIPCHOC_EMAIL_PORT || 587,
|
|
|
|
secure: process.env.CHIPCHOC_EMAIL_SECURE == 'false' ? false : true,
|
|
|
|
auth: {
|
|
|
|
user: process.env.CHIPCHOC_EMAIL_USER,
|
|
|
|
pass: process.env.CHIPCHOC_EMAIL_PASSWORD,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-06-04 14:16:11 -04:00
|
|
|
// decide base url for app (can be configured)
|
|
|
|
let baseUrl = process.env.CHIPCHOC_BASE_URL || '/'
|
|
|
|
|
2021-04-29 06:07:41 -04:00
|
|
|
const app = express()
|
2021-05-28 14:40:16 -04:00
|
|
|
var expressWs = require('express-ws')(app)
|
|
|
|
|
2021-06-04 14:16:11 -04:00
|
|
|
const router = express.Router()
|
|
|
|
app.use(baseUrl, router)
|
|
|
|
|
2021-06-04 13:44:23 -04:00
|
|
|
const port = process.env.CHIPCHOC_PORT || 5000
|
2021-04-29 06:07:41 -04:00
|
|
|
|
|
|
|
const cors = require('cors')
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
app.use(cors())
|
|
|
|
|
2021-06-04 14:16:11 -04:00
|
|
|
router.get('/air', (req, res) => {
|
2021-04-29 06:07:41 -04:00
|
|
|
console.log(`Searching for contacts with the term: ${req.query.name}`)
|
|
|
|
res.send({
|
|
|
|
success: true,
|
|
|
|
data: [],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-04 13:44:23 -04:00
|
|
|
|
2021-06-04 14:16:11 -04:00
|
|
|
router.ws('/hit-send', (ws, request) => {
|
2021-05-28 14:40:16 -04:00
|
|
|
console.log('Hitting send on some emails!')
|
|
|
|
|
2021-06-04 13:44:23 -04:00
|
|
|
ws.on('message', async (message) => {
|
2021-05-28 14:40:16 -04:00
|
|
|
try {
|
|
|
|
message = JSON.parse(message)
|
|
|
|
} catch(err) {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
success: false,
|
|
|
|
error: "We can't understand what you're saying! Me speak JSON only.",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-04 13:44:23 -04:00
|
|
|
if (DEBUG) {
|
|
|
|
console.log(`\n\n-------------------------- BEGIN EMAIL --------------------------\n`)
|
2021-06-04 13:45:52 -04:00
|
|
|
console.log(`From: ${message.from}\nTo:${message.to}\nSubject:${message.subject||'(no subject)'}\n\n${message.text}`)
|
2021-06-04 13:44:23 -04:00
|
|
|
console.log(`\n--------------------------- END EMAIL ---------------------------`)
|
|
|
|
} else {
|
|
|
|
console.log(`Sending email: ${message.from} -> ${message.to}`)
|
|
|
|
|
|
|
|
// actually send the email
|
|
|
|
let receipt = await perumal.sendMail({
|
|
|
|
from: message.from || process.env.CHIPCHOC_DEFAULT_SENDER,
|
|
|
|
to: message.to,
|
|
|
|
bcc: message.from || process.env.CHIPCHOC_DEFAULT_SENDER,
|
2021-06-04 13:45:52 -04:00
|
|
|
subject: message.subject,
|
2021-06-04 13:44:23 -04:00
|
|
|
text: message.text,
|
|
|
|
html: message.html,
|
|
|
|
})
|
2021-05-28 14:40:16 -04:00
|
|
|
|
2021-06-04 13:44:23 -04:00
|
|
|
}
|
2021-05-28 14:41:07 -04:00
|
|
|
|
2021-05-28 14:40:16 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
success: true,
|
|
|
|
from: message.from,
|
|
|
|
to: message.to,
|
|
|
|
}))
|
|
|
|
}, 2000)
|
|
|
|
})
|
|
|
|
|
|
|
|
ws.on('close', () => {
|
|
|
|
console.log('socket closed')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-29 06:07:41 -04:00
|
|
|
app.use(express.static('public'))
|
2021-06-04 14:16:11 -04:00
|
|
|
router.get('/', (req, res) => {
|
2021-04-29 06:07:41 -04:00
|
|
|
res.sendFile(path.resolve(__dirname, 'public', 'index.html'))
|
|
|
|
})
|
2021-05-28 14:40:16 -04:00
|
|
|
|
2021-04-29 06:07:41 -04:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Server is up at port ${port}`)
|
|
|
|
})
|