UNPKG

grabbed

Version:

grabs emails and pulls attachments and dumps into a folder.

96 lines (78 loc) 2.26 kB
// Generated by CoffeeScript 1.6.3 var Imap, child_process, fetchMessages, fs, imap, inspect, messages, messagesPath, mkdirp, openInbox, parsedPath, path; Imap = require("imap"); path = require("path"); fs = require("fs"); mkdirp = require("mkdirp").sync; child_process = require("child_process"); inspect = require("util").inspect; imap = new Imap({ user: process.env.GRABBED_USER, password: process.env.GRABBED_PW, host: "imap.gmail.com", port: 993, tls: true, tlsOptions: { rejectUnauthorized: false } }); process.on("uncaughtException", function(err) { console.log(err.message); return imap.end(); }); messagesPath = path.join(process.cwd(), "/messages/"); parsedPath = path.join(process.cwd(), 'done'); messages = []; openInbox = function(cb) { return imap.openBox("[Gmail]/cctv_shots", false, cb); }; fetchMessages = function(callback) { var cb; cb = callback; return imap.search(["UNSEEN", ["SINCE", "May 20, 2010"]], function(err, results) { var f; f = imap.fetch(results, { markSeen: true, bodies: '' }); f.on("message", function(msg, seqno) { var prefix; console.log("Message #%d", seqno); prefix = "(#" + seqno + ") "; msg.on("body", function(stream, info) { stream.pipe(fs.createWriteStream(path.join(messagesPath, "msg-" + seqno + "-body.txt"))); }); return msg.once("end", function() {}); }); f.once("error", function(err) {}); return f.once("end", function() { console.log("Done fetching all messages!"); console.log("Starting parser!"); console.log("cwd: " + (process.cwd())); return cb(null); }); }); }; imap.once("ready", function() { mkdirp(messagesPath); return openInbox(function(err, box) { return fetchMessages(function(err) { if (err != null) { console.log(err.message); } else { console.log("Fetched messages"); } return imap.end(); }); }); }); imap.once("error", function(err) {}); imap.once("end", function() { var cp; console.log("Connection ended"); cp = child_process.fork(path.join(process.cwd(), 'lib/parser.js')); return cp.on("exit", function() { return process.exit(); }); }); imap.connect();