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

basics of moving XMPP node to using config node rather than creds file

This commit is contained in:
Ben Hardill 2014-04-25 15:55:48 +01:00
parent 2c80c90f84
commit 2f83f71ace
2 changed files with 142 additions and 14 deletions

View File

@ -17,9 +17,7 @@
<script type="text/x-red" data-template-name="xmpp">
<div class="form-row">
<label for="node-input-server"><i class="icon-bookmark"></i> Server</label>
<input type="text" id="node-input-server" placeholder="talk.google.com" style="width: 40%;" >
<label for="node-input-port" style="margin-left: 10px; width: 35px; "> Port</label>
<input type="text" id="node-input-port" placeholder="Port" style="width:45px">
<input type="text" id="node-input-server">
</div>
<div class="form-row">
<label>&nbsp;</label>
@ -66,8 +64,7 @@
color:"Silver",
defaults: {
name: {value:""},
server: { value:"",required:true},
port: {value:5222,required:true},
server: {type:"xmpp-server",required:true},
to: {value:""},
join: {value:false},
sendObject: {value:false},
@ -84,3 +81,73 @@
}
});
</script>
<script type="text/x-red" data-template-name="xmpp-server">
<div class="form-row node-input-server">
<label for="node-config-input-server"><i class="icon-bookmark"></i> Server</label>
<input class="input-append-left" type="text" id="node-config-input-server" placeholder="Server" style="width: 40%;" >
<label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> Port</label>
<input type="text" id="node-config-input-port" placeholder="Port" style="width:45px">
</div>
<div class="form-row">
<label for="node-config-input-user"><i class="icon-user"></i> Username</label>
<input type="text" id="node-config-input-user">
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-config-input-pass">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('xmpp-server',{
category: 'config',
defaults: {
server: {value:"localhost",required:true},
port: {value:5222,required:true,validate:RED.validators.number()},
user: {required:true},
pass: {required:true}
//user -> credentials
//pass -> credentials
},
label: function() {
return (this.user?this.user+"@":"")+this.server+":"+this.port;
},
oneditprepare: function() {
$.getJSON('xmpp-server/'+this.id,function(data) {
if (data.user) {
$('#node-config-input-user').val(data.user);
}
if (data.hasPassword) {
$('#node-config-input-pass').val('__PWRD__');
} else {
$('#node-config-input-pass').val('');
}
});
},
oneditsave: function() {
var newUser = $('#node-config-input-user').val();
var newPass = $('#node-config-input-pass').val();
var credentials = {};
credentials.user = newUser;
if (newPass != '__PWRD__') {
credentials.password = newPass;
}
$.ajax({
url: 'xmpp-server/'+this.id,
type: 'POST',
data: credentials,
success:function(result){}
});
},
ondelete: function() {
$.ajax({
url: 'xmpp-server/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
});
</script>

View File

@ -22,20 +22,81 @@ console.warn=(function() { // suppress warning from stringprep when not needed)
};
})();
try {
var xmppkey = RED.settings.xmpp || require(process.env.NODE_RED_HOME+"/../xmppkeys.js");
} catch(err) {
// throw new Error("Failed to load XMPP credentials");
}
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var xmpp = require('simple-xmpp');
console.warn = orig;
try {
var xmppkey = RED.settings.xmpp || require(process.env.NODE_RED_HOME+"/../xmppkeys.js");
} catch(err) {
throw new Error("Failed to load XMPP credentials");
function XMPPServerNode(n) {
RED.nodes.createNode(this,n);
this.server = n.server;
this.port = n.port;
var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.username = credentials.user;
this.password = credentials.password;
}
}
RED.nodes.registerType("xmpp-server",XMPPServerNode);
var querystring = require('querystring');
RED.httpAdmin.get('/xmpp-server/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
} else {
res.send(JSON.stringify({}));
}
});
RED.httpAdmin.delete('/xmpp-server/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/xmpp-server/:id',function(req,res) {
var body = "";
req.on('data', function(chunk) {
body+=chunk;
});
req.on('end', function(){
var newCreds = querystring.parse(body);
var credentials = RED.nodes.getCredentials(req.params.id)||{};
if (newCreds.user == null || newCreds.user == "") {
delete credentials.user;
} else {
credentials.user = newCreds.user;
}
if (newCreds.password == "") {
delete credentials.password;
} else {
credentials.password = newCreds.password||credentials.password;
}
RED.nodes.addCredentials(req.params.id,credentials);
res.send(200);
});
});
function XmppNode(n) {
RED.nodes.createNode(this,n);
this.server = n.server;
this.port = n.port;
this.serverConfig = RED.nodes.getNode(this.server);
if (this.serverConfig){
this.host = this.serverConfig.server;
this.port = this.serverConfig.port;
this.jid = this.serverConfig.username;
this.password = this.serverConfig.password;
} else {
console.log("no serverConfig found");
}
this.join = n.join || false;
this.nick = n.nick || "Node-RED";
this.sendAll = n.sendObject;
@ -44,10 +105,10 @@ function XmppNode(n) {
setTimeout(function() {
xmpp.connect({
jid : xmppkey.jid,
password : xmppkey.password,
host : this.server,
port : this.port,
jid : node.jid,
password : node.password,
host : node.host,
port : node.port,
skipPresence : true,
reconnect : false
});