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

@ -22,7 +22,8 @@ var oldmail = {};
try {
var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
} catch (err) {
throw new Error("Failed to load Email credentials");
util.log("[imap] : Failed to load Email credentials");
return;
}
var imap = new Imap({
@ -33,13 +34,9 @@ var imap = new Imap({
secure: true
});
function fail(err) {
util.log('[imap] : ' + err);
}
function openInbox(cb) {
imap.connect(function(err) {
if (err) fail(err);
if (err) util.log("[imap] : error : "+err);
imap.openBox('INBOX', true, cb);
});
}
@ -47,12 +44,12 @@ function openInbox(cb) {
function ImapNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.repeat = n.repeat * 1000;
this.repeat = n.repeat * 1000 || 300000;
var node = this;
this.interval_id = null;
if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) {
this.log("repeat = "+this.repeat);
if (!isNaN(this.repeat) && this.repeat > 0) {
node.log("repeat = "+this.repeat);
this.interval_id = setInterval( function() {
node.emit("input",{});
}, this.repeat );
@ -60,13 +57,13 @@ function ImapNode(n) {
this.on("input", function(msg) {
openInbox(function(err, mailbox) {
if (err) fail(err);
if (err) node.log("error : "+err);
imap.seq.fetch(mailbox.messages.total + ':*', { struct: false },
{ headers: ['from', 'subject'],
body: true,
cb: function(fetch) {
fetch.on('message', function(msg) {
//node.log('Saw message no. ' + msg.seqno);
node.log('Read message no. ' + msg.seqno);
var pay = {};
var body = '';
msg.on('headers', function(hdrs) {
@ -89,13 +86,12 @@ function ImapNode(n) {
});
}
}, function(err) {
if (err) node.log("Err : "+err);
//node.log("Done fetching messages.");
if (err) node.log("error : "+err);
node.log("Done fetching messages.");
imap.logout();
}
);
});
});
this.on("close", function() {
@ -103,6 +99,7 @@ function ImapNode(n) {
clearInterval(this.interval_id);
}
});
}
node.emit("input",{});
}
RED.nodes.registerType("imap",ImapNode);