2013-09-05 16:02:48 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2013-11-14 16:44:54 +01:00
|
|
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
2013-09-05 16:02:48 +02:00
|
|
|
var Imap = require('imap');
|
2013-10-15 22:12:30 +02:00
|
|
|
var util = require('util');
|
2013-09-05 16:02:48 +02:00
|
|
|
|
|
|
|
try {
|
2013-11-24 14:10:48 +01:00
|
|
|
var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
|
|
|
|
} catch (err) {
|
|
|
|
util.log("[imap] : Failed to load Email credentials");
|
|
|
|
return;
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var imap = new Imap({
|
2013-11-24 14:10:48 +01:00
|
|
|
user: emailkey.user,
|
|
|
|
password: emailkey.pass,
|
|
|
|
host: emailkey.server||"imap.gmail.com",
|
|
|
|
port: emailkey.port||"993",
|
2013-11-26 20:55:40 +01:00
|
|
|
tls: true,
|
|
|
|
tlsOptions: { rejectUnauthorized: false }
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function openInbox(cb) {
|
2013-11-26 20:55:40 +01:00
|
|
|
imap.openBox('INBOX', true, cb);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ImapNode(n) {
|
2013-11-24 14:10:48 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.name = n.name;
|
|
|
|
this.repeat = n.repeat * 1000 || 300000;
|
|
|
|
var node = this;
|
|
|
|
this.interval_id = null;
|
2013-11-29 20:16:11 +01:00
|
|
|
var oldmail = {};
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-24 14:10:48 +01:00
|
|
|
if (!isNaN(this.repeat) && this.repeat > 0) {
|
|
|
|
node.log("repeat = "+this.repeat);
|
|
|
|
this.interval_id = setInterval( function() {
|
|
|
|
node.emit("input",{});
|
|
|
|
}, this.repeat );
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-24 14:10:48 +01:00
|
|
|
this.on("input", function(msg) {
|
2013-11-26 20:55:40 +01:00
|
|
|
imap.once('ready', function() {
|
|
|
|
var pay = {};
|
|
|
|
openInbox(function(err, box) {
|
|
|
|
//if (err) throw err;
|
|
|
|
var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'] });
|
|
|
|
f.on('message', function(msg, seqno) {
|
|
|
|
node.log('message: #'+ seqno);
|
|
|
|
var prefix = '(#' + seqno + ') ';
|
|
|
|
msg.on('body', function(stream, info) {
|
|
|
|
var buffer = '';
|
|
|
|
stream.on('data', function(chunk) {
|
|
|
|
buffer += chunk.toString('utf8');
|
|
|
|
});
|
|
|
|
stream.on('end', function() {
|
|
|
|
if (info.which !== 'TEXT') {
|
|
|
|
pay.from = Imap.parseHeader(buffer).from[0];
|
|
|
|
pay.topic = Imap.parseHeader(buffer).subject[0];
|
|
|
|
pay.date = Imap.parseHeader(buffer).date[0];
|
|
|
|
} else {
|
|
|
|
var parts = buffer.split("Content-Type");
|
|
|
|
for (var p in parts) {
|
|
|
|
if (parts[p].indexOf("text/plain") >= 0) {
|
|
|
|
pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
|
|
|
|
}
|
|
|
|
if (parts[p].indexOf("text/html") >= 0) {
|
|
|
|
pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//pay.body = buffer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
msg.on('end', function() {
|
|
|
|
//node.log('Finished: '+prefix);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
f.on('error', function(err) {
|
|
|
|
node.warn('fetch error: ' + err);
|
|
|
|
});
|
|
|
|
f.on('end', function() {
|
|
|
|
if (JSON.stringify(pay) !== oldmail) {
|
2013-11-24 14:10:48 +01:00
|
|
|
node.send(pay);
|
2013-11-26 20:55:40 +01:00
|
|
|
oldmail = JSON.stringify(pay);
|
2013-11-29 20:16:11 +01:00
|
|
|
node.log('sent new message: '+pay.topic);
|
2013-11-24 14:10:48 +01:00
|
|
|
}
|
2013-11-29 20:16:11 +01:00
|
|
|
else { node.log('duplicate not sent: '+pay.topic); }
|
2013-11-26 20:55:40 +01:00
|
|
|
imap.end();
|
2013-11-24 14:10:48 +01:00
|
|
|
});
|
2013-11-26 20:55:40 +01:00
|
|
|
});
|
2013-11-24 14:10:48 +01:00
|
|
|
});
|
2013-11-26 20:55:40 +01:00
|
|
|
imap.connect();
|
|
|
|
});
|
|
|
|
|
|
|
|
imap.on('error', function(err) {
|
|
|
|
util.log(err);
|
|
|
|
});
|
|
|
|
|
2013-11-29 20:16:11 +01:00
|
|
|
this.on("error", function(err) {
|
|
|
|
node.log("error: ",err);
|
2013-11-24 14:10:48 +01:00
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-24 14:10:48 +01:00
|
|
|
this.on("close", function() {
|
|
|
|
if (this.interval_id != null) {
|
|
|
|
clearInterval(this.interval_id);
|
|
|
|
}
|
2013-11-29 20:16:11 +01:00
|
|
|
imap.destroy();
|
2013-11-26 20:55:40 +01:00
|
|
|
});
|
|
|
|
|
2013-11-24 14:10:48 +01:00
|
|
|
node.emit("input",{});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-10-15 22:12:30 +02:00
|
|
|
RED.nodes.registerType("imap",ImapNode);
|