mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Update IMAP node to use new 0.8.x API
Fixes Issue #96 this necessitates an update to the underlying npm npm install --force imap
This commit is contained in:
		| @@ -31,14 +31,12 @@ var imap = new Imap({ | ||||
|     password: emailkey.pass, | ||||
|     host: emailkey.server||"imap.gmail.com", | ||||
|     port: emailkey.port||"993", | ||||
|     secure: true | ||||
|     tls: true, | ||||
|     tlsOptions: { rejectUnauthorized: false } | ||||
| }); | ||||
|  | ||||
| function openInbox(cb) { | ||||
|     imap.connect(function(err) { | ||||
|         if (err) util.log("[imap] : error : "+err); | ||||
|         imap.openBox('INBOX', true, cb); | ||||
|     }); | ||||
|     imap.openBox('INBOX', true, cb); | ||||
| } | ||||
|  | ||||
| function ImapNode(n) { | ||||
| @@ -56,42 +54,64 @@ function ImapNode(n) { | ||||
|     } | ||||
|  | ||||
|     this.on("input", function(msg) { | ||||
|         openInbox(function(err, mailbox) { | ||||
|           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('Read message no. ' + msg.seqno); | ||||
|                   var pay = {}; | ||||
|                   var body = ''; | ||||
|                   msg.on('headers', function(hdrs) { | ||||
|                     pay.from = hdrs.from[0]; | ||||
|                     pay.topic = hdrs.subject[0]; | ||||
|                   }); | ||||
|                   msg.on('data', function(chunk) { | ||||
|                     body += chunk.toString('utf8'); | ||||
|                   }); | ||||
|                   msg.on('end', function() { | ||||
|                     pay.payload = body; | ||||
|                     if ((pay.topic !== oldmail.topic)|(pay.payload !== oldmail.payload)) { | ||||
|                         oldmail = pay; | ||||
|                         //node.log("From: "+pay.from); | ||||
|                         node.log("Subj: "+pay.topic); | ||||
|                         //node.log("Body: "+pay.payload); | ||||
|                         node.send(pay); | ||||
|                     } | ||||
|                   }); | ||||
|         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); | ||||
|                     }); | ||||
|                 }); | ||||
|               } | ||||
|             }, function(err) { | ||||
|                 if (err) node.log("error : "+err); | ||||
|                 node.log("Done fetching messages."); | ||||
|                 imap.logout(); | ||||
|             } | ||||
|           ); | ||||
|                 f.on('error', function(err) { | ||||
|                     node.warn('fetch error: ' + err); | ||||
|                 }); | ||||
|                 f.on('end', function() { | ||||
|                     if (JSON.stringify(pay) !== oldmail) { | ||||
|                         node.send(pay); | ||||
|                         oldmail = JSON.stringify(pay); | ||||
|                     } | ||||
|                     imap.end(); | ||||
|                     node.log('done fetching message'); | ||||
|                 }); | ||||
|             }); | ||||
|         }); | ||||
|         imap.connect(); | ||||
|     }); | ||||
|  | ||||
|     imap.on('error', function(err) { | ||||
|         util.log(err); | ||||
|     }); | ||||
|  | ||||
|     imap.on('end', function() { | ||||
|         //util.log('Connection ended'); | ||||
|     }); | ||||
|  | ||||
|     this.on("close", function() { | ||||
| @@ -100,6 +120,10 @@ function ImapNode(n) { | ||||
|         } | ||||
|     }); | ||||
|  | ||||
|     this.on("error", function(err) { | ||||
|         node.log("error: ",err); | ||||
|     }); | ||||
|  | ||||
|     node.emit("input",{}); | ||||
| } | ||||
| RED.nodes.registerType("imap",ImapNode); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user