[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / deprecated / 61-imap.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
17 var RED = require(process.env.NODE_RED_HOME+"/red/red");
18 var Imap = require('imap');
19 var util = require('util');
20
21 try {
22     var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
23 } catch (err) {
24     //util.log("[61-imap.js] Info : No Email credentials found.");
25 }
26
27 if (emailkey) {
28     var imap = new Imap({
29         user: emailkey.user,
30         password: emailkey.pass,
31         host: emailkey.server||"imap.gmail.com",
32         port: emailkey.port||"993",
33         tls: true,
34         tlsOptions: { rejectUnauthorized: false }
35     });
36
37     function openInbox(cb) {
38         imap.openBox('INBOX', true, cb);
39     }
40 }
41
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.");
45     this.name = n.name;
46     this.repeat = n.repeat * 1000 || 300000;
47     var node = this;
48     this.interval_id = null;
49     var oldmail = {};
50
51     if (!isNaN(this.repeat) && this.repeat > 0) {
52         node.log("repeat = "+this.repeat);
53         this.interval_id = setInterval( function() {
54             node.emit("input",{});
55         }, this.repeat );
56     }
57
58     this.on("input", function(msg) {
59         if (imap) {
60             imap.once('ready', function() {
61                 var pay = {};
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) {
69                                 var buffer = '';
70                                 stream.on('data', function(chunk) {
71                                     buffer += chunk.toString('utf8');
72                                 });
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];
78                                     } else {
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();
83                                             }
84                                             if (parts[p].indexOf("text/html") >= 0) {
85                                                 pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
86                                             }
87                                         }
88                                         //pay.body = buffer;
89                                     }
90                                 });
91                             });
92                             msg.on('end', function() {
93                                 //node.log('Finished: '+prefix);
94                             });
95                         });
96                         f.on('error', function(err) {
97                             node.warn('fetch error: ' + err);
98                         });
99                         f.on('end', function() {
100                             if (JSON.stringify(pay) !== oldmail) {
101                                 node.send(pay);
102                                 oldmail = JSON.stringify(pay);
103                                 node.log('sent new message: '+pay.topic);
104                             }
105                             else { node.log('duplicate not sent: '+pay.topic); }
106                             imap.end();
107                         });
108                     }
109                     else {
110                         // node.log("you have achieved inbox zero");
111                         imap.end();
112                     }
113                 });
114             });
115             imap.connect();
116         }
117         else { node.warn("No Email credentials found. See info panel."); }
118     });
119
120     if (imap) {
121         imap.on('error', function(err) {
122             util.log(err);
123         });
124     }
125
126     this.on("error", function(err) {
127         node.log("error: ",err);
128     });
129
130     this.on("close", function() {
131         if (this.interval_id != null) {
132             clearInterval(this.interval_id);
133         }
134         if (imap) { imap.destroy(); }
135     });
136
137     node.emit("input",{});
138 }
139 RED.nodes.registerType("imap",ImapNode);