2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var RED = require(process.env.NODE_RED_HOME+"/red/red");
18 var Imap = require('imap');
19 var util = require('util');
22 var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
24 //util.log("[61-imap.js] Info : No Email credentials found.");
30 password: emailkey.pass,
31 host: emailkey.server||"imap.gmail.com",
32 port: emailkey.port||"993",
34 tlsOptions: { rejectUnauthorized: false }
37 function openInbox(cb) {
38 imap.openBox('INBOX', true, cb);
42 function ImapNode(n) {
43 RED.nodes.createNode(this,n);
44 this.warn("This node has been deprecated and will be deleted in a future release. Please update your flow to use the 'e-mail in' node.");
46 this.repeat = n.repeat * 1000 || 300000;
48 this.interval_id = null;
51 if (!isNaN(this.repeat) && this.repeat > 0) {
52 node.log("repeat = "+this.repeat);
53 this.interval_id = setInterval( function() {
54 node.emit("input",{});
58 this.on("input", function(msg) {
60 imap.once('ready', function() {
62 openInbox(function(err, box) {
63 if (box.messages.total > 0) {
64 var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'] });
65 f.on('message', function(msg, seqno) {
66 node.log('message: #'+ seqno);
67 var prefix = '(#' + seqno + ') ';
68 msg.on('body', function(stream, info) {
70 stream.on('data', function(chunk) {
71 buffer += chunk.toString('utf8');
73 stream.on('end', function() {
74 if (info.which !== 'TEXT') {
75 pay.from = Imap.parseHeader(buffer).from[0];
76 pay.topic = Imap.parseHeader(buffer).subject[0];
77 pay.date = Imap.parseHeader(buffer).date[0];
79 var parts = buffer.split("Content-Type");
80 for (var p in parts) {
81 if (parts[p].indexOf("text/plain") >= 0) {
82 pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
84 if (parts[p].indexOf("text/html") >= 0) {
85 pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
92 msg.on('end', function() {
93 //node.log('Finished: '+prefix);
96 f.on('error', function(err) {
97 node.warn('fetch error: ' + err);
99 f.on('end', function() {
100 if (JSON.stringify(pay) !== oldmail) {
102 oldmail = JSON.stringify(pay);
103 node.log('sent new message: '+pay.topic);
105 else { node.log('duplicate not sent: '+pay.topic); }
110 // node.log("you have achieved inbox zero");
117 else { node.warn("No Email credentials found. See info panel."); }
121 imap.on('error', function(err) {
126 this.on("error", function(err) {
127 node.log("error: ",err);
130 this.on("close", function() {
131 if (this.interval_id != null) {
132 clearInterval(this.interval_id);
134 if (imap) { imap.destroy(); }
137 node.emit("input",{});
139 RED.nodes.registerType("imap",ImapNode);