From 49692b32555cdc4e954f59126e9e96eafd9145ba Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 12 Jun 2016 17:04:07 +0100 Subject: [PATCH] Better email node parsing (again) --- .gitignore | 3 -- Gruntfile.js | 2 +- coverall | 3 ++ social/email/61-email.js | 101 ++++++++++---------------------------- social/email/package.json | 2 +- 5 files changed, 32 insertions(+), 79 deletions(-) create mode 100755 coverall diff --git a/.gitignore b/.gitignore index b9c6d903..09ee0ffd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,6 @@ npm-debug.log node_modules .npm coverage -coverall - puball.sh - setenv.sh /.project diff --git a/Gruntfile.js b/Gruntfile.js index 6d936e19..f64a8ff8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,5 +1,5 @@ /** - * Copyright 2015 IBM Corp. + * Copyright 2014, 2016 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/coverall b/coverall new file mode 100755 index 00000000..c4a004f8 --- /dev/null +++ b/coverall @@ -0,0 +1,3 @@ +# check coverage of tests... and browse report +istanbul cover ./node_modules/.bin/grunt --report lcovonly && istanbul report html +/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome coverage/index.html diff --git a/social/email/61-email.js b/social/email/61-email.js index afeaf0bb..81818356 100644 --- a/social/email/61-email.js +++ b/social/email/61-email.js @@ -140,7 +140,6 @@ module.exports = function(RED) { }); - // // EmailInNode // @@ -196,64 +195,21 @@ module.exports = function(RED) { msg = JSON.parse(JSON.stringify(msg)); // Clone the message // Populate the msg fields from the content of the email message // that we have just parsed. - if ((mailMessage.text) && (mailMessage.text.indexOf("--=_") !== -1)) { - //processNewMessage(msg,mailMessage.text); - var parts = mailMessage.text.split("--=_"); - msg.payload = parts[0].trim(); - for (var p = 0; p < parts.length; p++) { - //console.log("\n\nP***",p+"\n"+JSON.stringify(parts[p]),"\n***P\n\n"); - if (parts[p].indexOf("text/plain") >= 0) { - msg.payload = parts[p].split("\n\n",2)[1].trim(); - } - if (parts[p].indexOf("text/html") >= 0) { - msg.html = parts[p].split("\n\n",2)[1].trim(); - } - if (parts[p].indexOf("=--\n\n") >= 0) { - //msg.header = "text: "+msg.payload+"\n\n"+parts[p].split("\n\n",2)[1].trim(); - var mailparser = new MailParser(); - mailparser.on("end", function(mailMessage) { - //console.log("DONG",mailMessage); - msg.header = mailMessage.headers; - msg.topic = mailMessage.subject; - msg.date = mailMessage.date; - if (mailMessage.from && mailMessage.from.length > 0) { - msg.from = mailMessage.from[0].address; - } - if (mailMessage.attachments) { - msg.attachments = mailMessage.attachments; - } else { - msg.attachments = []; - } - node.send(msg); // Propagate the message down the flow - }); - mailparser.write(parts[p].split("\n\n",2)[1].trim()); - mailparser.end(); - } - } + msg.payload = mailMessage.text; + msg.topic = mailMessage.subject; + msg.date = mailMessage.date; + if (mailMessage.html) { + msg.html = mailMessage.html; } - else { - if (!mailMessage.text) { - msg.payload = mailMessage.headers.text; - msg.topic = mailMessage.subject; - msg.header = mailMessage.headers; - msg.date = mailMessage.date; - } - else { - msg.payload = mailMessage.text; - } - if (mailMessage.html) { - msg.html = mailMessage.html; - } - if (mailMessage.from && mailMessage.from.length > 0) { - msg.from = mailMessage.from[0].address; - } - if (mailMessage.attachments) { - msg.attachments = mailMessage.attachments; - } else { - msg.attachments = []; - } - node.send(msg); // Propagate the message down the flow + if (mailMessage.from && mailMessage.from.length > 0) { + msg.from = mailMessage.from[0].address; } + if (mailMessage.attachments) { + msg.attachments = mailMessage.attachments; + } else { + msg.attachments = []; + } + node.send(msg); // Propagate the message down the flow } // End of processNewMessage // Check the POP3 email mailbox for any new messages. For any that are found, @@ -383,34 +339,29 @@ module.exports = function(RED) { // We have the search results that contain the list of unseen messages and can now fetch those messages. var fetch = imap.fetch(results, { - //bodies : ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'], - bodies : ['HEADER','TEXT'], + bodies: '', + struct: true, markSeen : true }); // For each fetched message returned ... fetch.on('message', function(imapMessage, seqno) { //node.log(RED._("email.status.message",{number:seqno})); - var messageText = "text: "; + var messageText = ""; //console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno); imapMessage.on('body', function(stream, info) { //console.log("> message - body - stream=?, info=%j", info); - // Info defined which part of the message this is ... for example - // 'TEXT' or 'HEADER' stream.on('data', function(chunk) { //console.log("> stream - data - chunk=??"); messageText += chunk.toString('utf8'); }); stream.once('end', function() { - if (info.which !== 'TEXT') { - var mailparser = new MailParser(); - mailparser.on("end", function(mailMessage) { - //console.log("mailparser: on(end): %j", mailMessage); - processNewMessage(msg, mailMessage); - }); - mailparser.write(messageText); - mailparser.end(); - } + var mailParser = new MailParser(); + mailParser.on('end', function(mailMessage) { + processNewMessage(msg, mailMessage); + }); + mailParser.write(messageText); + mailParser.end(); }); // End of msg->end }); // End of msg->body }); // End of fetch->message @@ -461,9 +412,11 @@ module.exports = function(RED) { connTimeout: node.repeat, authTimeout: node.repeat }); - imap.on('error', function(err) { - node.log(err); - //node.status({fill:"red",shape:"ring",text:"email.status.connecterror"}); + imap.on('error', function(err) { + if (err.errno !== "ECONNRESET") { + node.log(err); + node.status({fill:"red",shape:"ring",text:"email.status.connecterror"}); + } }); } diff --git a/social/email/package.json b/social/email/package.json index 06082c2a..2fed930e 100644 --- a/social/email/package.json +++ b/social/email/package.json @@ -1,6 +1,6 @@ { "name": "node-red-node-email", - "version": "0.1.8", + "version": "0.1.9", "description": "Node-RED nodes to send and receive simple emails", "dependencies": { "nodemailer": "^1.11.0",