Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- const tls = require('tls')
- const execFile = require('child_process').execFile
- const args = process.argv;
- const user = args[2] || process.env.GMAIL_NOTIFY_USER
- const pass = args[3] || process.env.GMAIL_NOTIFY_PASS
- const cmd = args[4] || process.env.GMAIL_NOTIFY_CMD
- function usage() {
- console.error("usage: gmail-notify [user] [pass] [cmd]")
- process.exit(1)
- }
- if (!user || !pass || !cmd) {
- usage();
- }
- console.error(`using ${user} ${pass} ${cmd}`)
- const socket = tls.connect({host: "imap.gmail.com", port: 993}, () => {
- 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)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement