commit 82ab065636339a0a62ad65f1d2ca6d76d422fbe4
Author: William Casarin <jb55@jb55.com>
Date: Mon, 17 Jul 2017 14:46:01 -0700
initial commit
Diffstat:
3 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -0,0 +1,11 @@
+# imap-notify
+
+ Simplest possible imap notifier
+
+ Connects to an IMAP server and runs a command when new mail is received
+
+## Installation
+
+ Install with npm
+
+ $ npm install -g jb55/imap-notify
diff --git a/index.js b/index.js
@@ -0,0 +1,45 @@
+#!/usr/bin/env node
+
+const tls = require('tls')
+const execFile = require('child_process').execFile
+
+const args = process.argv;
+const user = args[2] || process.env.IMAP_NOTIFY_USER
+const pass = args[3] || process.env.IMAP_NOTIFY_PASS
+const cmd = args[4] || process.env.IMAP_NOTIFY_CMD
+const host = args[5] || process.env.IMAP_NOTIFY_HOST || "imap.gmail.com"
+const port = args[6] || process.env.IMAP_NOTIFY_PORT || 993
+const allow = process.env.IMAP_ALLOW_UNAUTHORIZED == null? false : !!process.env.IMAP_ALLOW_UNAUTHORIZED
+
+function usage() {
+ console.error("usage: imap-notify USER PASS CMD [HOST] [PORT]")
+ process.exit(1)
+}
+
+if (!user || !pass || !cmd) {
+ usage();
+}
+
+const socket = tls.connect({host: host, port: port, rejectUnauthorized: !allow}, () => {
+ function handleNotifications() {
+ socket.on("data", (data) => {
+ var str = data.toString();
+ console.error(str)
+
+ const res = /\* (\d+) EXISTS/.exec(str);
+ if (res && res[1]) {
+ execFile(cmd, [res[1]])
+ }
+ })
+ }
+
+ socket.write(`tag login ${user} ${pass}\r\n`)
+ socket.write("A001 SELECT INBOX\r\n")
+ socket.write("A002 IDLE\r\n")
+
+ handleNotifications()
+
+ socket.on("close", () => process.exit(2))
+
+ setInterval(socket.write.bind(null, "A002 IDLE\r\n"), 300000)
+})
diff --git a/package.json b/package.json
@@ -0,0 +1,12 @@
+{
+ "name": "imap-notify",
+ "description": "minimal imap notifier, run a command when mail is received",
+ "version": "0.1.0",
+ "repository": {
+ "url": "https://github.com/jb55/imap-notify"
+ },
+ "main": "index.js",
+ "bin": {
+ "imap-notify": "./index.js"
+ }
+}