From 1cda61f3760f8c0bc03e01572197a6c0e15289b4 Mon Sep 17 00:00:00 2001 From: John Shaver Date: Wed, 4 Sep 2019 00:06:15 -0700 Subject: [PATCH] initial commit --- errorSender.js | 13 +++++++++++ index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 ++++++++++ templater.js | 11 ++++++++++ 4 files changed, 93 insertions(+) create mode 100644 errorSender.js create mode 100644 index.js create mode 100644 package.json create mode 100644 templater.js diff --git a/errorSender.js b/errorSender.js new file mode 100644 index 0000000..92c6867 --- /dev/null +++ b/errorSender.js @@ -0,0 +1,13 @@ +module.exports = function(req, res, next) { + res.sendError = function(code, text) { + try { + res.status(code).type('json').send(JSON.stringify({ + error: text + })); + } catch(err) { + console.error("Error formatting error response: ", err); + res.sendStatus(500); + } + } + next(); +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..046239c --- /dev/null +++ b/index.js @@ -0,0 +1,58 @@ +let fs = require('fs'); + +let bodyParser = require('body-parser'); +let outbound = require('./outbound'); + +let getTemplates = require('./templates'); +let errorSender = require('./errorSender'); + +exports.register = function() { + this.register_hook('hook_init_http', function(next, server) { + server.http.app.use('/webSend', setupRoutes(server.http.express.Router())); + next(); + }); +} + +function setupRoutes(app) { + app.use(bodyParser.json()); + app.use(errorSender); + + app.post('/template', function(req, res) { + if(!req.body) { + return res.sendError(400, "No body provided."); + } + + let {to, from, template, headers} = req.body; + if(!template) { + return res.sendError(400, "No template name provided."); + } + return getTemplates.then(function(templates) { + let emailText = `From: ${from}\nTo: ${to}`; + if(headers) { + headers.forEach(function(header) { + emailText += `\n${header}`; + }); + } + try { + let [subject, body] = `\n\n${templates[template](req.body)}\n`; + emailText += `\nSubject: ${subject}\n\n${body}`; + } catch (err) { + console.error("Error building email from template: ", err); + res.sendError(err.httpStatus || 400, err.userMessage || "Error parsing template"); + } + + outbound.send_email(from, to, emailText, function(code, message) { + console.log(`Queue mail result code: ${code} msg: ${msg}`); + res.type("json").send(JSON.stringigy({ + code, + message + })); + }); + }).catch(function(err) { + console.error("Error loading templates:", err); + res.sendError(500, "Server Error"); + }); + }); + + return app; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..5c0b9e5 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "haraka-plugin-webmailer", + "version": "1.0.0", + "description": "Plugin to add our webhooks to haraka", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "john@rootprojects.com", + "license": "UNLICENSED" +} diff --git a/templater.js b/templater.js new file mode 100644 index 0000000..84cdc92 --- /dev/null +++ b/templater.js @@ -0,0 +1,11 @@ +let escape = require("lodash/escape"); + +module.exports = function(strings) { + //first element is the strings, don't need it + let args = arguments.slice(1); + + //first element will be the initial value + return strings.slice(1).reduce(function(result, string, i) { + return result + escape(args[i]) + string; + }, strings[0]); +};