From b84a04adb00bcf8205cf750cda5900b908c72d1c Mon Sep 17 00:00:00 2001 From: alseambusher Date: Tue, 13 Dec 2016 14:06:13 +0530 Subject: [PATCH] adding mail config ui options --- app.js | 1 + bin/crontab-ui-mailer.js | 36 +++++++++++++++++++++ config/mailconfig.js | 22 +++++++++++++ crontab.js | 21 ++++++++++-- package.json | 5 +-- public/js/script.js | 57 +++++++++++++++++++++++++++++++++ views/index.ejs | 1 + views/popup.ejs | 69 ++++++++++++++++++++-------------------- 8 files changed, 174 insertions(+), 38 deletions(-) create mode 100644 bin/crontab-ui-mailer.js create mode 100644 config/mailconfig.js diff --git a/app.js b/app.js index 18d904a..25cc60c 100755 --- a/app.js +++ b/app.js @@ -27,6 +27,7 @@ app.use(busboy()); // to support file uploads app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public/css')); app.use(express.static(__dirname + '/public/js')); +app.use(express.static(__dirname + '/config')); app.set('views', __dirname + '/views'); // set port to 8000 or the value set by environment var PORT diff --git a/bin/crontab-ui-mailer.js b/bin/crontab-ui-mailer.js new file mode 100644 index 0000000..7025224 --- /dev/null +++ b/bin/crontab-ui-mailer.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node + +var defaults = require("../config/mailconfig.js"); + +var nodemailer = require('nodemailer'); + +// create reusable transporter object using the default SMTP transport +var transporter = nodemailer.createTransport(defaults.transporterStr); +var mailOptions = defaults.mailOptions; + +var stdin = process.stdin, + stdout = process.stdout, + inputChunks = []; + +stdin.resume(); +stdin.setEncoding('utf8'); + +stdin.on('data', function (chunk) { + inputChunks.push(chunk); +}); + +stdin.on('end', function () { + var inputJSON = inputChunks.join(), + mailOptions = JSON.parse(inputJSON); + + // outputJSON = JSON.stringify(parsedData, null, ' '); + // stdout.write(outputJSON); + // stdout.write('\n'); +}); + +transporter.sendMail(mailOptions, function(error, info){ + if(error){ + return console.log(error); + } + console.log('Message sent: ' + info.response); +}); diff --git a/config/mailconfig.js b/config/mailconfig.js new file mode 100644 index 0000000..2adffb4 --- /dev/null +++ b/config/mailconfig.js @@ -0,0 +1,22 @@ +/*jshint esversion: 6*/ +// refer nodemailer for more info + +var transporterStr = 'smtps://user%40gmail.com:pass@smtp.gmail.com'; + +var mailOptions = { + from: '"Fred Foo 👥" ', // sender address + to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers + subject: 'Hello ✔', // Subject line + text: 'Hello world 🐴', // plaintext body + html: 'Hello world 🐴' // html body +}; + +if (typeof window === 'undefined') { + exports.transporterStr = transporterStr; + exports.mailOptions = mailOptions; +} else { + if (!window.config) + window.config = {}; + window.config.transporterStr = transporterStr; + window.config.mailOptions = mailOptions; +} diff --git a/crontab.js b/crontab.js index 1b39b94..84065e7 100644 --- a/crontab.js +++ b/crontab.js @@ -45,6 +45,8 @@ exports.status = function(_id, stopped){ exports.remove = function(_id){ db.remove({_id: _id}, {}); }; + +// Iterates through all the crontab entries in the db and calls the callback with the entries exports.crontabs = function(callback){ db.find({}).sort({ created: -1 }).exec(function(err, docs){ for(var i=0; i&1 1>&2 2>&3 | tee " + tmp_log +"; if test -f " + tmp_log +"; then date >> " + log_file + "; cat " + tmp_log + " >> " + log_file + "; rm " + tmp_log + "; fi; if test -f " + tmp_hook + "; then " + tab.hook + " < " + tmp_hook + "; rm " + tmp_hook + "; fi \n"; + crontab_string += tab.schedule + " ({ " + tab.command + " } | tee " + tmp_hook + ") 3>&1 1>&2 2>&3 | tee " + tmp_log + + "; if test -f " + tmp_log + + "; then date >> " + log_file + + "; cat " + tmp_log + " >> " + log_file + + "; rm " + tmp_log + + "; fi; if test -f " + tmp_hook + + "; then " + tab.hook + " < " + tmp_hook + + "; rm " + tmp_hook + + "; fi \n"; } else { - crontab_string += tab.schedule + " { " + tab.command + " } 2> " + tmp_log +"; if test -f " + tmp_log +"; then date >> " + log_file + "; cat " + tmp_log + " >> " + log_file + "; rm " + tmp_log + "; fi \n"; + crontab_string += tab.schedule + " { " + tab.command + " } 2> " + tmp_log + + "; if test -f " + tmp_log + + "; then date >> " + log_file + + "; cat " + tmp_log + " >> " + log_file + + "; rm " + tmp_log + + "; fi \n"; } } else { diff --git a/package.json b/package.json index ebaad85..85a359d 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crontab-ui", - "version": "0.2.2", + "version": "0.2.3", "description": "Easy and safe way to manage your crontab file", "main": "index.js", "scripts": { @@ -20,7 +20,8 @@ "node": "latest" }, "bin": { - "crontab-ui": "bin/crontab-ui.js" + "crontab-ui": "bin/crontab-ui.js", + "crontab-ui-mailer": "bin/crontab-ui-mailer.js" }, "repository": { "type": "git", diff --git a/public/js/script.js b/public/js/script.js index 66b1f7a..f7ba184 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -169,6 +169,63 @@ function import_db(){ }); } +function setMailConfig(a){ + let data = JSON.parse(a.getAttribute("data-json")); + let container = document.createElement("div"); + + let message = "

This is based on nodemailer. Refer this for more details.

"; + container.innerHTML += message; + + let transporterLabel = document.createElement("label"); + transporterLabel.innerHTML = "Transporter"; + let transporterInput = document.createElement("input"); + transporterInput.type = "text"; + transporterInput.id = "transporterInput"; + transporterInput.setAttribute("placeholder", config.transporterStr); + transporterInput.className = "form-control"; + if (data.transporterStr){ + transporterInput.setAttribute("value", data.transporterStr); + } + container.appendChild(transporterLabel); + container.appendChild(transporterInput); + + container.innerHTML += "
"; + + let mailOptionsLabel = document.createElement("label"); + mailOptionsLabel.innerHTML = "Mail Config"; + let mailOptionsInput = document.createElement("textarea"); + mailOptionsInput.setAttribute("placeholder", JSON.stringify(config.mailOptions, null, 2)); + mailOptionsInput.className = "form-control"; + mailOptionsInput.id = "mailOptionsInput"; + mailOptionsInput.setAttribute("rows", "10"); + if (data.mailOptions) + mailOptionsInput.innerHTML = JSON.stringify(data.mailOptions, null, 2); + container.appendChild(mailOptionsLabel); + container.appendChild(mailOptionsInput); + + container.innerHTML += "
"; + + let button = document.createElement("a"); + button.className = "btn btn-primary btn-small"; + button.innerHTML = "Use Defaults"; + button.onclick = function(){ + document.getElementById("transporterInput").value = config.transporterStr; + document.getElementById("mailOptionsInput").innerHTML = JSON.stringify(config.mailOptions, null, 2); + }; + container.appendChild(button); + + messageBox(container, "Mailing", null, null, function(){ + let transporterStr = document.getElementById("transporterInput").value; + let mailOptions = JSON.parse(document.getElementById("mailOptionsInput").innerHTML); + if (transporterStr && mailOptions){ + a.setAttribute("data-json", JSON.stringify({transporterStr: transporterStr, mailOptions: mailOptions})); + } + }); +} + +function setHookConfig(a){ + messageBox("

Coming Soon

", "Hooks", null, null, null); +} // script corresponding to job popup management function job_string(){ diff --git a/views/index.ejs b/views/index.ejs index 47e081c..ee9be6d 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -4,6 +4,7 @@ + diff --git a/views/popup.ejs b/views/popup.ejs index f9bb15e..ea64f10 100644 --- a/views/popup.ejs +++ b/views/popup.ejs @@ -1,36 +1,3 @@ - - - - + + + +