Skip to content

Instantly share code, notes, and snippets.

@shamdasani
Created May 13, 2017 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamdasani/2f727ebbff20b4f033797eb2a9daeb59 to your computer and use it in GitHub Desktop.
Save shamdasani/2f727ebbff20b4f033797eb2a9daeb59 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var http = require('http');
var inbox = require("inbox");
const MailParser = require('mailparser').MailParser;
let parser = new MailParser()
var journal = {
entries: []
};
var JournalEntry = {};
var client = inbox.createConnection(false, "mail.pawnmail.com", {
secureConnection: true,
auth: {
user: "",
pass: ""
}
});
client.connect();
client.on("connect", function() {
client.openMailbox("INBOX", {unseen: true}, function(error, info) {
if (error) throw error;
client.listMessages(-1, function(err, messages) {
messages.forEach(function(message) {
var messageStream = client.createMessageStream(message.UID);
messageStream.pipe(parser);
parser.on('headers', headers => {
JournalEntry.date = headers.get('date')
JournalEntry.title = headers.get('subject')
});
parser.on('data', data => {
if (data.type === 'text') {
JournalEntry.entry = data.text;
};
fs.readFile('entries.json', 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
journal = JSON.parse(data);
journal.entries.push({
JournalEntry
});
var json = JSON.stringify(journal);
fs.writeFile('entries.json', json, 'utf8');
}
});
var database = fs.readFileSync('entries.json');
http.createServer(function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
res.writeHead(200, {
'Content-Type': 'jsonp'
});
res.end(database);
}).listen(9615);
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment