From eadfb04ee2f442febb3819ed5c85a274128aa902 Mon Sep 17 00:00:00 2001 From: Andrew Van Tassel Date: Sat, 10 Dec 2016 11:18:03 -0700 Subject: [PATCH] Added notification --- index.js | 15 +++++++++++++++ package.json | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fe5f3c1..533be0e 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var MailListener = require("mail-listener2"); var MailHops = require("mailhops"); var chalk = require('chalk'); +var notifier = require('node-notifier'); var logUpdate = require('log-update'); var _ = require('lodash'); var config = require('./config.json'); @@ -11,6 +12,7 @@ var configuration = { host: "", // imap host port: 993, // imap port tls: true, + notify: true, connTimeout: 10000, // Default by node-imap authTimeout: 5000, // Default by node-imap, debug: console.log, // Or your custom function with only one incoming argument. Default: null @@ -33,6 +35,7 @@ var mhconfiguration = { if(config){ configuration = _.merge(configuration,config); } + if(config && config.mailhops){ mhconfiguration = _.merge(mhconfiguration,config.mailhops); } @@ -66,6 +69,7 @@ mailListener.on("mail", function(mail, seqno, attributes){ mailhops.lookup(ips,function(err, res, body){ if(err) return logUpdate(`${chalk.red('MailHops Error: '+err)}`); if(body.error && body.error.message) return logUpdate(`${chalk.red('MailHops Error: '+body.error.message)}`); + mail.mailHops = body.response; if(typeof mail.mailHops != 'undefined'){ let start = mailhops.getStartHop(mail.mailHops.route); @@ -75,6 +79,17 @@ mailListener.on("mail", function(mail, seqno, attributes){ logUpdate(`${chalk.green( start.city+', '+start.state+' ('+start.countryCode+')' )} -> ${chalk.red( end.city+', '+end.state+' ('+end.countryCode+')')} ${chalk.yellow(Math.round(mail.mailHops.distance.miles)+' mi.')} `); logUpdate.done() + // notify + if(configuration.notify){ + notifier.notify({ + 'title': 'New mail from '+mail.from[0].name, + 'subtitle': start.city+', '+start.state+' ('+start.countryCode+') '+Math.round(mail.mailHops.distance.miles)+' mi.', + 'icon': start.flag, + 'message': mail.subject, + 'sound': true, + 'time': 5000 + }); + } } }); } diff --git a/package.json b/package.json index 646d1de..6a784a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mailhops-imap-listener", - "version": "1.0.0", + "version": "1.0.1", "description": "A nodejs app for using MailHops API to test and monitor your IMAP account.", "main": "./index.js", "scripts": { @@ -20,6 +20,7 @@ "chalk": "^1.1.3", "log-update": "^1.0.2", "mail-listener2": "^0.2.0", - "mailhops": "^2.0.3" + "mailhops": "^2.0.3", + "node-notifier": "^4.6.1" } }