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

Make imap node check for email right away on start/restart. Add some more console logging for re-assurance of things happening - or not.

This commit is contained in:
Dave C-J 2013-11-24 13:10:48 +00:00
parent f2ed2365cd
commit f051fbd1e1

View File

@ -20,89 +20,86 @@ var util = require('util');
var oldmail = {}; var oldmail = {};
try { try {
var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js"); var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
} catch(err) { } catch (err) {
throw new Error("Failed to load Email credentials"); util.log("[imap] : Failed to load Email credentials");
return;
} }
var imap = new Imap({ var imap = new Imap({
user: emailkey.user, user: emailkey.user,
password: emailkey.pass, password: emailkey.pass,
host: emailkey.server||"imap.gmail.com", host: emailkey.server||"imap.gmail.com",
port: emailkey.port||"993", port: emailkey.port||"993",
secure: true secure: true
}); });
function fail(err) {
util.log('[imap] : ' + err);
}
function openInbox(cb) { function openInbox(cb) {
imap.connect(function(err) { imap.connect(function(err) {
if (err) fail(err); if (err) util.log("[imap] : error : "+err);
imap.openBox('INBOX', true, cb); imap.openBox('INBOX', true, cb);
}); });
} }
function ImapNode(n) { function ImapNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.name = n.name; this.name = n.name;
this.repeat = n.repeat * 1000; this.repeat = n.repeat * 1000 || 300000;
var node = this; var node = this;
this.interval_id = null; this.interval_id = null;
if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) { if (!isNaN(this.repeat) && this.repeat > 0) {
this.log("repeat = "+this.repeat); node.log("repeat = "+this.repeat);
this.interval_id = setInterval( function() { this.interval_id = setInterval( function() {
node.emit("input",{}); node.emit("input",{});
}, this.repeat ); }, this.repeat );
} }
this.on("input", function(msg) { this.on("input", function(msg) {
openInbox(function(err, mailbox) { openInbox(function(err, mailbox) {
if (err) fail(err); if (err) node.log("error : "+err);
imap.seq.fetch(mailbox.messages.total + ':*', { struct: false }, imap.seq.fetch(mailbox.messages.total + ':*', { struct: false },
{ headers: ['from', 'subject'], { headers: ['from', 'subject'],
body: true, body: true,
cb: function(fetch) { cb: function(fetch) {
fetch.on('message', function(msg) { fetch.on('message', function(msg) {
//node.log('Saw message no. ' + msg.seqno); node.log('Read message no. ' + msg.seqno);
var pay = {}; var pay = {};
var body = ''; var body = '';
msg.on('headers', function(hdrs) { msg.on('headers', function(hdrs) {
pay.from = hdrs.from[0]; pay.from = hdrs.from[0];
pay.topic = hdrs.subject[0]; pay.topic = hdrs.subject[0];
}); });
msg.on('data', function(chunk) { msg.on('data', function(chunk) {
body += chunk.toString('utf8'); body += chunk.toString('utf8');
}); });
msg.on('end', function() { msg.on('end', function() {
pay.payload = body; pay.payload = body;
if ((pay.topic !== oldmail.topic)|(pay.payload !== oldmail.payload)) { if ((pay.topic !== oldmail.topic)|(pay.payload !== oldmail.payload)) {
oldmail = pay; oldmail = pay;
//node.log("From: "+pay.from); //node.log("From: "+pay.from);
node.log("Subj: "+pay.topic); node.log("Subj: "+pay.topic);
//node.log("Body: "+pay.payload); //node.log("Body: "+pay.payload);
node.send(pay); node.send(pay);
} }
}); });
}); });
} }
}, function(err) { }, function(err) {
if (err) node.log("Err : "+err); if (err) node.log("error : "+err);
//node.log("Done fetching messages."); node.log("Done fetching messages.");
imap.logout(); imap.logout();
} }
); );
}); });
});
}); this.on("close", function() {
if (this.interval_id != null) {
clearInterval(this.interval_id);
}
});
this.on("close", function() { node.emit("input",{});
if (this.interval_id != null) {
clearInterval(this.interval_id);
}
});
} }
RED.nodes.registerType("imap",ImapNode); RED.nodes.registerType("imap",ImapNode);