1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

add Status to email, http nodes (update copyright on tcp)

This commit is contained in:
Dave C-J 2014-05-13 11:39:59 +01:00
parent 0e8f0735cc
commit 6f0e619611
4 changed files with 115 additions and 102 deletions

View File

@ -1,5 +1,5 @@
/**
* Copyright 2013 IBM Corp.
* Copyright 2013,2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -140,70 +140,73 @@ module.exports = function(RED) {
var node = this;
var credentials = RED.nodes.getCredentials(n.id);
this.on("input",function(msg) {
var url;
if (msg.url) {
url = msg.url;
} else if (isTemplatedUrl) {
url = mustache.render(nodeUrl,msg);
node.status({fill:"blue",shape:"dot",text:"requesting"});
var url;
if (msg.url) {
url = msg.url;
} else if (isTemplatedUrl) {
url = mustache.render(nodeUrl,msg);
} else {
url = nodeUrl;
}
var method = (msg.method||nodeMethod).toUpperCase();
var opts = urllib.parse(url);
opts.method = method;
opts.headers = {};
if (msg.headers) {
for (var v in msg.headers) {
opts.headers[v.toLowerCase()] = msg.headers[v];
}
}
if (credentials) {
opts.auth = credentials.user+":"+(credentials.password||"");
}
var payload = null;
if (msg.payload && (method == "POST" || method == "PUT") ) {
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
payload = msg.payload;
} else if (typeof msg.payload == "number") {
payload = msg.payload+"";
} else {
url = nodeUrl;
}
var method = (msg.method||nodeMethod).toUpperCase();
var opts = urllib.parse(url);
opts.method = method;
opts.headers = {};
if (msg.headers) {
for (var v in msg.headers) {
opts.headers[v.toLowerCase()] = msg.headers[v];
}
}
if (credentials) {
opts.auth = credentials.user+":"+(credentials.password||"");
}
var payload = null;
if (msg.payload && (method == "POST" || method == "PUT") ) {
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
payload = msg.payload;
} else if (typeof msg.payload == "number") {
payload = msg.payload+"";
if (opts.headers['content-type'] == 'application/x-www-form-urlencoded') {
payload = querystring.stringify(msg.payload);
} else {
if (opts.headers['content-type'] == 'application/x-www-form-urlencoded') {
payload = querystring.stringify(msg.payload);
} else {
payload = JSON.stringify(msg.payload);
if (opts.headers['content-type'] == null) {
opts.headers['content-type'] = "application/json";
}
payload = JSON.stringify(msg.payload);
if (opts.headers['content-type'] == null) {
opts.headers['content-type'] = "application/json";
}
}
if (opts.headers['content-length'] == null) {
opts.headers['content-length'] = Buffer.byteLength(payload);
}
}
if (opts.headers['content-length'] == null) {
opts.headers['content-length'] = Buffer.byteLength(payload);
}
}
var req = ((/^https/.test(url))?https:http).request(opts,function(res) {
res.setEncoding('utf8');
msg.statusCode = res.statusCode;
msg.headers = res.headers;
msg.payload = "";
res.on('data',function(chunk) {
msg.payload += chunk;
});
res.on('end',function() {
node.send(msg);
});
var req = ((/^https/.test(url))?https:http).request(opts,function(res) {
res.setEncoding('utf8');
msg.statusCode = res.statusCode;
msg.headers = res.headers;
msg.payload = "";
res.on('data',function(chunk) {
msg.payload += chunk;
});
req.on('error',function(err) {
msg.payload = err.toString();
msg.statusCode = err.code;
res.on('end',function() {
node.send(msg);
node.status({});
});
if (payload) {
req.write(payload);
}
req.end();
});
req.on('error',function(err) {
msg.payload = err.toString();
msg.statusCode = err.code;
node.send(msg);
node.status({fill:"red",shape:"ring",text:err.code});
});
if (payload) {
req.write(payload);
}
req.end();
});
}
RED.nodes.registerType("http request",HTTPRequest);

View File

@ -1,5 +1,5 @@
/**
* Copyright 2013 IBM Corp.
* Copyright 2013,2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
<!--
Copyright 2013 IBM Corp.
Copyright 2013,2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/**
* Copyright 2013 IBM Corp.
* Copyright 2013,2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,6 @@ module.exports = function(RED) {
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
// module.exports = { service: "Gmail", user: "blahblah@gmail.com", pass: "password", server: "imap.gmail.com", port: "993" }
try { var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js"); }
catch(err) { }
@ -68,8 +67,8 @@ module.exports = function(RED) {
});
this.on("input", function(msg) {
//node.log("email :",this.id,this.topic," received",msg.payload);
if (msg != null) {
node.status({fill:"blue",shape:"dot",text:"sending"},true);
if (smtpTransport) {
smtpTransport.sendMail({
from: node.userid, // sender address
@ -79,8 +78,10 @@ module.exports = function(RED) {
}, function(error, response) {
if (error) {
node.error(error);
node.status({fill:"red",shape:"ring",text:"post error"},true);
} else {
node.log("Message sent: " + response.message);
node.status({},true);
}
});
}
@ -133,6 +134,7 @@ module.exports = function(RED) {
this.on("input", function(msg) {
imap.once('ready', function() {
node.status({fill:"blue",shape:"dot",text:"fetching"},true);
var pay = {};
imap.openBox('INBOX', true, function(err, box) {
if (box.messages.total > 0) {
@ -170,6 +172,7 @@ module.exports = function(RED) {
});
f.on('error', function(err) {
node.warn('fetch error: ' + err);
node.status({fill:"red",shape:"ring",text:"fetch error"},true);
});
f.on('end', function() {
if (JSON.stringify(pay) !== oldmail) {
@ -178,21 +181,28 @@ module.exports = function(RED) {
node.log('received new email: '+pay.topic);
}
else { node.log('duplicate not sent: '+pay.topic); }
//node.status({fill:"green",shape:"dot",text:"ok"},true);
node.status({},true);
imap.end();
});
}
else {
node.log("you have achieved inbox zero");
//node.status({fill:"green",shape:"dot",text:"ok"},true);
node.status({},true);
imap.end();
}
});
});
imap.on('error', function(err) {
node.log(err);
});
node.status({fill:"grey",shape:"dot",text:"connecting"},true);
imap.connect();
});
imap.on('error', function(err) {
node.log(err);
node.status({fill:"red",shape:"ring",text:"connect error"},true);
});
this.on("error", function(err) {
node.log("error: ",err);
});
@ -207,7 +217,7 @@ module.exports = function(RED) {
node.emit("input",{});
}
if (Imap != null) {
RED.nodes.registerType("e-mail in",EmailInNode);
RED.nodes.registerType("e-mail in",EmailInNode);
}
var querystring = require('querystring');