commit b5da37b4993d80198cf70ec06238637b2791ede0 Author: Andrew Van Tassel Date: Fri Dec 9 19:34:12 2016 -0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2432776 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.json +*.log +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..f87c97e --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# MailHops IMAP listener +[www.MailHops.com](https://www.mailhops.com) + +MailHops logo + +This app will loop through all the mail in an IMAP account and geo route all messages. You can use the live feed on your mailhops.com account to monitor your incoming messages. Leave it running and it will route incoming messages. + +```sh +# install npm modules +npm install + +# Add you email connection info in config.json +cp config.sample config.json + +# set searchFilter to UNSEEN to only monitor new incoming messages +vim config.json + +# start the app +npm start +``` + + diff --git a/config.sample b/config.sample new file mode 100644 index 0000000..add936e --- /dev/null +++ b/config.sample @@ -0,0 +1,10 @@ +{ + "username": "", + "password": "", + "host": "", + "port": 993, + "searchFilter": ["SEEN"] + "mailhops": { + "api_key": "" + } +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..fe5f3c1 --- /dev/null +++ b/index.js @@ -0,0 +1,85 @@ +var MailListener = require("mail-listener2"); +var MailHops = require("mailhops"); +var chalk = require('chalk'); +var logUpdate = require('log-update'); +var _ = require('lodash'); +var config = require('./config.json'); + +var configuration = { + username: "", // imap username + password: "", // imap password + host: "", // imap host + port: 993, // imap port + tls: 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 + tlsOptions: { rejectUnauthorized: false }, + mailbox: "INBOX", // mailbox to monitor + searchFilter: ["UNSEEN"], // the search filter being used after an IDLE notification has been retrieved + markSeen: false, // all fetched email willbe marked as seen and not fetched next time + fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`, + mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib. + attachments: false, // download attachments as they are encountered to the project directory + attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments +}; + +var mhconfiguration = { + api_key: "", + api_version: 2, + app_name: "Node Inbox Report" +}; + +if(config){ + configuration = _.merge(configuration,config); +} +if(config && config.mailhops){ + mhconfiguration = _.merge(mhconfiguration,config.mailhops); +} + +var mailListener = new MailListener(configuration); + +var mailhops = new MailHops(mhconfiguration); + +mailListener.start(); // start listening + +// stop listening +//mailListener.stop(); + +mailListener.on("server:connected", function(){ + console.log("imapConnected"); +}); + +mailListener.on("server:disconnected", function(){ + console.log("imapDisconnected"); +}); + +mailListener.on("error", function(err){ + console.log(err); +}); + +mailListener.on("mail", function(mail, seqno, attributes){ + // do something with mail object including attachments + // mail processing code goes here + var ips = mailhops.getIPsFromMailParser(mail); + if(ips){ + 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); + let end = mailhops.getEndHop(mail.mailHops.route); + logUpdate(`${chalk.bold(mail.from[0].name+' '+mail.from[0].address)}`); + logUpdate.done() + 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() + } + }); + } +}); + +mailListener.on("attachment", function(attachment){ + console.log(attachment.path); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..646d1de --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "mailhops-imap-listener", + "version": "1.0.0", + "description": "A nodejs app for using MailHops API to test and monitor your IMAP account.", + "main": "./index.js", + "scripts": { + "start": "node index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "http://github.com/mailhops/mailhops-imap-listener" + }, + "keywords": [ + "MailHops" + ], + "author": "Andrew Van Tassel", + "license": "MIT", + "dependencies": { + "chalk": "^1.1.3", + "log-update": "^1.0.2", + "mail-listener2": "^0.2.0", + "mailhops": "^2.0.3" + } +} diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..73cc2dd Binary files /dev/null and b/screenshot.png differ