summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-11-20 12:41:41 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-11-20 15:08:34 +0100
commit9ef099c2679047569aa2da0d688a919bf707e6c6 (patch)
treea8e9c4cbfebf723473e9e5f0d9458150e87f81b7
parentc77d725864de5908bc0ad9ce98ec8be29311838a (diff)
Email module
-rw-r--r--modules/email/.gitignore2
-rw-r--r--modules/email/email.js68
2 files changed, 70 insertions, 0 deletions
diff --git a/modules/email/.gitignore b/modules/email/.gitignore
new file mode 100644
index 0000000..df02057
--- /dev/null
+++ b/modules/email/.gitignore
@@ -0,0 +1,2 @@
+allowed_recipients.txt
+password.txt
diff --git a/modules/email/email.js b/modules/email/email.js
new file mode 100644
index 0000000..834a471
--- /dev/null
+++ b/modules/email/email.js
@@ -0,0 +1,68 @@
+var cmn = require("../$common.js"),
+ fs = require("fs"),
+ child_process = require("child_process"),
+ bodyParser = require("body-parser");
+
+var allowedRecipients = [];
+
+var ratelimitAllowed = (function() {
+ var numEvents = 20, period = 24 * 3600 * 1000;
+ var eventList = [];
+
+ return function ratelimitAllowed() {
+ var now = new Date();
+ while (now - eventList[0] > period) eventList.unshift();
+ if (eventList.length >= numEvents) return false;
+ eventList.push(now);
+ return true;
+ };
+})();
+
+function sendEmail(recip, text) {
+ var opts = {
+ stdio: ["pipe", "inherit", "inherit"]
+ };
+ var proc = child_process.spawn("sendmail", [recip], opts);
+
+ // Make sure it doesn't run indefinitely
+ var timeout = setTimeout(() => {proc.kill();}, 5000);
+ proc.on("exit", () => clearTimeout(timeout));
+ proc.on("error", err => {
+ console.log("email: Failed to start sendmail:", err);
+ clearTimeout(timeout);
+ });
+ proc.stdin.on("error", err => {
+ console.log("email: Failed to write to sendmail:", err);
+ clearTimeout(timeout);
+ });
+ proc.stdin.end(
+ "From: email-module@tomsmeding.com\n" +
+ "To: " + recip + "\n" +
+ "Subject: Mail from email module\n\n" +
+ text
+ );
+}
+
+module.exports = function(app, io, moddir) {
+ var allowedRecipients =
+ fs.readFileSync(moddir + "/allowed_recipients.txt").toString().trim().split("\n");
+
+ var password = fs.readFileSync(moddir + "/password.txt").toString().trim();
+
+ app.post("/email", bodyParser.json(), function(req, res) {
+ var body = req.body;
+ console.log(body);
+ if (typeof body != "object" ||
+ typeof body.password != "string" ||
+ typeof body.to != "string" ||
+ typeof body.text != "string" ||
+ body.password != password ||
+ allowedRecipients.indexOf(body.to) == -1) {
+ res.status(400).send("Invalid request");
+ return;
+ }
+ var text = body.text.slice(0, 1000000); // 1 MB is enough
+ sendEmail(body.to, body.text);
+ res.end();
+ });
+};