Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. const tls = require('tls')
  4. const execFile = require('child_process').execFile
  5.  
  6. const args = process.argv;
  7. const user = args[2] || process.env.GMAIL_NOTIFY_USER
  8. const pass = args[3] || process.env.GMAIL_NOTIFY_PASS
  9. const cmd = args[4] || process.env.GMAIL_NOTIFY_CMD
  10.  
  11. function usage() {
  12. console.error("usage: gmail-notify [user] [pass] [cmd]")
  13. process.exit(1)
  14. }
  15.  
  16. if (!user || !pass || !cmd) {
  17. usage();
  18. }
  19.  
  20. console.error(`using ${user} ${pass} ${cmd}`)
  21.  
  22. const socket = tls.connect({host: "imap.gmail.com", port: 993}, () => {
  23. function handleNotifications() {
  24. socket.on("data", (data) => {
  25. var str = data.toString();
  26. console.error(str)
  27.  
  28. const res = /\* (\d+) EXISTS/.exec(str);
  29. if (res && res[1]) {
  30. execFile(cmd, [res[1]])
  31. }
  32. })
  33. }
  34.  
  35. socket.write(`tag login ${user} ${pass}\r\n`)
  36. socket.write("A001 SELECT INBOX\r\n")
  37. socket.write("A002 IDLE\r\n")
  38.  
  39. handleNotifications()
  40.  
  41. socket.on("close", () => process.exit(2))
  42.  
  43. setInterval(socket.write.bind(null, "A002 IDLE\r\n"), 300000)
  44. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement