1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Better email node parsing (again)

This commit is contained in:
Dave Conway-Jones 2016-06-12 17:04:07 +01:00
parent f91b7c4630
commit 49692b3255
5 changed files with 32 additions and 79 deletions

3
.gitignore vendored
View File

@ -2,9 +2,6 @@ npm-debug.log
node_modules node_modules
.npm .npm
coverage coverage
coverall
puball.sh puball.sh
setenv.sh setenv.sh
/.project /.project

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2015 IBM Corp. * Copyright 2014, 2016 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

3
coverall Executable file
View File

@ -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

View File

@ -140,7 +140,6 @@ module.exports = function(RED) {
}); });
// //
// EmailInNode // EmailInNode
// //
@ -196,64 +195,21 @@ module.exports = function(RED) {
msg = JSON.parse(JSON.stringify(msg)); // Clone the message msg = JSON.parse(JSON.stringify(msg)); // Clone the message
// Populate the msg fields from the content of the email message // Populate the msg fields from the content of the email message
// that we have just parsed. // that we have just parsed.
if ((mailMessage.text) && (mailMessage.text.indexOf("--=_") !== -1)) { msg.payload = mailMessage.text;
//processNewMessage(msg,mailMessage.text); msg.topic = mailMessage.subject;
var parts = mailMessage.text.split("--=_"); msg.date = mailMessage.date;
msg.payload = parts[0].trim(); if (mailMessage.html) {
for (var p = 0; p < parts.length; p++) { msg.html = mailMessage.html;
//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();
}
}
} }
else { if (mailMessage.from && mailMessage.from.length > 0) {
if (!mailMessage.text) { msg.from = mailMessage.from[0].address;
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.attachments) {
msg.attachments = mailMessage.attachments;
} else {
msg.attachments = [];
}
node.send(msg); // Propagate the message down the flow
} // End of processNewMessage } // End of processNewMessage
// Check the POP3 email mailbox for any new messages. For any that are found, // 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. // We have the search results that contain the list of unseen messages and can now fetch those messages.
var fetch = imap.fetch(results, { var fetch = imap.fetch(results, {
//bodies : ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'], bodies: '',
bodies : ['HEADER','TEXT'], struct: true,
markSeen : true markSeen : true
}); });
// For each fetched message returned ... // For each fetched message returned ...
fetch.on('message', function(imapMessage, seqno) { fetch.on('message', function(imapMessage, seqno) {
//node.log(RED._("email.status.message",{number:seqno})); //node.log(RED._("email.status.message",{number:seqno}));
var messageText = "text: "; var messageText = "";
//console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno); //console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno);
imapMessage.on('body', function(stream, info) { imapMessage.on('body', function(stream, info) {
//console.log("> message - body - stream=?, info=%j", 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) { stream.on('data', function(chunk) {
//console.log("> stream - data - chunk=??"); //console.log("> stream - data - chunk=??");
messageText += chunk.toString('utf8'); messageText += chunk.toString('utf8');
}); });
stream.once('end', function() { stream.once('end', function() {
if (info.which !== 'TEXT') { var mailParser = new MailParser();
var mailparser = new MailParser(); mailParser.on('end', function(mailMessage) {
mailparser.on("end", function(mailMessage) { processNewMessage(msg, mailMessage);
//console.log("mailparser: on(end): %j", mailMessage); });
processNewMessage(msg, mailMessage); mailParser.write(messageText);
}); mailParser.end();
mailparser.write(messageText);
mailparser.end();
}
}); // End of msg->end }); // End of msg->end
}); // End of msg->body }); // End of msg->body
}); // End of fetch->message }); // End of fetch->message
@ -461,9 +412,11 @@ module.exports = function(RED) {
connTimeout: node.repeat, connTimeout: node.repeat,
authTimeout: node.repeat authTimeout: node.repeat
}); });
imap.on('error', function(err) { imap.on('error', function(err) {
node.log(err); if (err.errno !== "ECONNRESET") {
//node.status({fill:"red",shape:"ring",text:"email.status.connecterror"}); node.log(err);
node.status({fill:"red",shape:"ring",text:"email.status.connecterror"});
}
}); });
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-node-email", "name": "node-red-node-email",
"version": "0.1.8", "version": "0.1.9",
"description": "Node-RED nodes to send and receive simple emails", "description": "Node-RED nodes to send and receive simple emails",
"dependencies": { "dependencies": {
"nodemailer": "^1.11.0", "nodemailer": "^1.11.0",