1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02: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:
Dave C-J 2013-11-26 19:55:40 +00:00
parent 7040aaa179
commit ab04fcf7c0
2 changed files with 78 additions and 53 deletions

View File

@ -15,23 +15,24 @@
--> -->
<script type="text/x-red" data-template-name="imap"> <script type="text/x-red" data-template-name="imap">
<div class="form-row node-input-repeat"> <div class="form-row node-input-repeat">
<label for="node-input-repeat"><i class="icon-repeat"></i>Repeat (S)</label> <label for="node-input-repeat"><i class="icon-repeat"></i>Repeat (S)</label>
<input type="text" id="node-input-repeat" placeholder="300"> <input type="text" id="node-input-repeat" placeholder="300">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
</script> </script>
<script type="text/x-red" data-help-name="imap"> <script type="text/x-red" data-help-name="imap">
<p>Repeatedly gets a <b>single email</b> from an IMAP server and forwards on as a msg if not already seen.</p> <p>Repeatedly gets a <b>single email</b> from an IMAP server and forwards on as a msg if not already seen.</p>
<p>The subject is loaded into <b>msg.topic</b> and <b>msg.payload</b> is the body text. <b>msg.from</b> is also set if you need it.</p> <p>The subject is loaded into <b>msg.topic</b> and <b>msg.payload</b> is the plain text body.
<p>Uses the imap module - you also need to pre-configure your email settings in a file emailkeys.js as per below.</p> If there is text/html then that is returned in <b>msg.html</b>. <b>msg.from</b> and <b>msg.date</b> are also set if you need them.</p>
<p><pre>module.exports = { service: "Gmail", user: "blahblah@gmail.com", pass: "password", server: "imap.gmail.com", port: "993" }</pre></p> <p>Uses the imap module - you also need to pre-configure your email settings in a file emailkeys.js as per below.</p>
<p>This <b>must</b> be located in the diectory above node-red.</p> <p><pre>module.exports = { service: "Gmail", user: "blahblah@gmail.com", pass: "password", server: "imap.gmail.com", port: "993" }</pre></p>
<p><b>Note:</b> this node <i>only</i> gets the most recent single email from the inbox, so set the repeat (polling) time appropriately.</p> <p>This <b>must</b> be located in the directory above node-red.</p>
<p><b>Note:</b> this node <i>only</i> gets the most recent single email from the inbox, so set the repeat (polling) time appropriately.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -31,14 +31,12 @@ var imap = new Imap({
password: emailkey.pass, password: emailkey.pass,
host: emailkey.server||"imap.gmail.com", host: emailkey.server||"imap.gmail.com",
port: emailkey.port||"993", port: emailkey.port||"993",
secure: true tls: true,
tlsOptions: { rejectUnauthorized: false }
}); });
function openInbox(cb) { function openInbox(cb) {
imap.connect(function(err) { imap.openBox('INBOX', true, cb);
if (err) util.log("[imap] : error : "+err);
imap.openBox('INBOX', true, cb);
});
} }
function ImapNode(n) { function ImapNode(n) {
@ -56,42 +54,64 @@ function ImapNode(n) {
} }
this.on("input", function(msg) { this.on("input", function(msg) {
openInbox(function(err, mailbox) { imap.once('ready', function() {
if (err) node.log("error : "+err); var pay = {};
imap.seq.fetch(mailbox.messages.total + ':*', { struct: false }, openInbox(function(err, box) {
{ headers: ['from', 'subject'], //if (err) throw err;
body: true, var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'] });
cb: function(fetch) { f.on('message', function(msg, seqno) {
fetch.on('message', function(msg) { node.log('message: #'+ seqno);
node.log('Read message no. ' + msg.seqno); var prefix = '(#' + seqno + ') ';
var pay = {}; msg.on('body', function(stream, info) {
var body = ''; var buffer = '';
msg.on('headers', function(hdrs) { stream.on('data', function(chunk) {
pay.from = hdrs.from[0]; buffer += chunk.toString('utf8');
pay.topic = hdrs.subject[0]; });
}); stream.on('end', function() {
msg.on('data', function(chunk) { if (info.which !== 'TEXT') {
body += chunk.toString('utf8'); pay.from = Imap.parseHeader(buffer).from[0];
}); pay.topic = Imap.parseHeader(buffer).subject[0];
msg.on('end', function() { pay.date = Imap.parseHeader(buffer).date[0];
pay.payload = body; } else {
if ((pay.topic !== oldmail.topic)|(pay.payload !== oldmail.payload)) { var parts = buffer.split("Content-Type");
oldmail = pay; for (var p in parts) {
//node.log("From: "+pay.from); if (parts[p].indexOf("text/plain") >= 0) {
node.log("Subj: "+pay.topic); pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
//node.log("Body: "+pay.payload); }
node.send(pay); 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) {
}, function(err) { node.warn('fetch error: ' + err);
if (err) node.log("error : "+err); });
node.log("Done fetching messages."); f.on('end', function() {
imap.logout(); 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() { this.on("close", function() {
@ -100,6 +120,10 @@ function ImapNode(n) {
} }
}); });
this.on("error", function(err) {
node.log("error: ",err);
});
node.emit("input",{}); node.emit("input",{});
} }
RED.nodes.registerType("imap",ImapNode); RED.nodes.registerType("imap",ImapNode);