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

Further XMPP tidy - more accurate from field - more docs.

This commit is contained in:
Dave Conway-Jones 2018-09-29 15:17:18 +01:00
parent 7bd255a341
commit 6540439e5f
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
3 changed files with 70 additions and 49 deletions

View File

@ -6,7 +6,7 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-to"><i class="fa fa-envelope"></i> Buddy</label> <label for="node-input-to"><i class="fa fa-envelope"></i> Buddy</label>
<input type="text" id="node-input-to" placeholder="joe@gmail.com"> <input type="text" id="node-input-to" placeholder="joe@blah.im">
</div> </div>
<div class="form-row"> <div class="form-row">
<label>&nbsp;</label> <label>&nbsp;</label>
@ -21,9 +21,10 @@
<script type="text/x-red" data-help-name="xmpp in"> <script type="text/x-red" data-help-name="xmpp in">
<p>Connects to an XMPP server to receive messages.</p> <p>Connects to an XMPP server to receive messages.</p>
<p>The <b>Buddy</b> field is the id of the buddy or room you want to receive messages from. Leave blank to receive from anyone.</p> <p>The <b>Buddy</b> field is the jid of the buddy or room you want to receive messages from. Leave blank to receive from anyone.</p>
<p>Incoming messages will appear as <code>msg.payload</code> on the first output, while <code>msg.topic</code> will contain who it is from.</p> <p>Incoming messages will appear as <code>msg.payload</code> on the first output, while <code>msg.topic</code> will contain who it is from.
<p>The second output will show the presence and status of a user in <code>msg.payload</code>. Again <code>msg.topic</code> will hold the user.</p> If part of a chat room then <code>msg.room</code> may also be set.</p>
<p>The second output will show the presence and status of a user in <code>msg.payload</code>. Again <code>msg.topic</code> will hold the users jid.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -48,6 +49,7 @@
}); });
</script> </script>
<script type="text/x-red" data-template-name="xmpp out"> <script type="text/x-red" data-template-name="xmpp out">
<div class="form-row"> <div class="form-row">
<label for="node-input-server"><i class="fa fa-bookmark"></i> Connect as</label> <label for="node-input-server"><i class="fa fa-bookmark"></i> Connect as</label>
@ -60,7 +62,7 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-to"><i class="fa fa-envelope"></i> To</label> <label for="node-input-to"><i class="fa fa-envelope"></i> To</label>
<input type="text" id="node-input-to" placeholder="joe@gmail.com"> <input type="text" id="node-input-to" placeholder="joe@blah.im">
</div> </div>
<div class="form-row"> <div class="form-row">
<label>&nbsp;</label> <label>&nbsp;</label>
@ -115,11 +117,11 @@
</div> --> </div> -->
<div class="form-row"> <div class="form-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> JID</label> <label for="node-config-input-user"><i class="fa fa-user"></i> JID</label>
<input type="text" id="node-config-input-user"> <input type="text" id="node-config-input-user" placeholder="joe@blah.im">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-nickname"><i class="fa fa-user"></i> Nickname</label> <label for="node-config-input-nickname"><i class="fa fa-user"></i> Nickname</label>
<input type="text" id="node-config-input-nickname" placeholder="Joe"> <input type="text" id="node-config-input-nickname" placeholder="Joe (optional)">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label> <label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label>
@ -133,8 +135,8 @@
defaults: { defaults: {
// server: {required:true}, // server: {required:true},
// port: {value:5222,required:true,validate:RED.validators.number()}, // port: {value:5222,required:true,validate:RED.validators.number()},
nickname: {value:""},
user: {type:"text"}, user: {type:"text"},
nickname: {value:""}
}, },
credentials: { credentials: {
password: {type:"password"} password: {type:"password"}
@ -144,3 +146,11 @@
} }
}); });
</script> </script>
<script type="text/x-red" data-help-name="xmpp-server">
<p>The connection to an XMPP server to send and receive messages.</p>
<p>Connects to the standard C2S port 5222 on the server.</p>
<p>The JID is the full username of the client used to connect to the server
and must contain the resolvable fdqn of the server.</p>
<p>The nickname is optional.</p>
</script>

View File

@ -53,7 +53,6 @@ module.exports = function(RED) {
RED.nodes.registerType("xmpp-server",XMPPServerNode,{ RED.nodes.registerType("xmpp-server",XMPPServerNode,{
credentials: { credentials: {
user: {type:"text"},
password: {type: "password"} password: {type: "password"}
} }
}); });
@ -61,13 +60,8 @@ module.exports = function(RED) {
function XmppInNode(n) { function XmppInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.server = n.server; this.server = n.server;
this.serverConfig = RED.nodes.getNode(this.server); this.serverConfig = RED.nodes.getNode(this.server);
// this.host = this.serverConfig.server; this.nick = this.serverConfig.nickname || this.serverConfig.username.split("@")[0];
// this.port = this.serverConfig.port;
var pa = this.serverConfig.username.split("@");
this.nick = this.serverConfig.nickname || pa[0];
this.join = n.join || false; this.join = n.join || false;
this.sendAll = n.sendObject; this.sendAll = n.sendObject;
this.from = n.to || ""; this.from = n.to || "";
@ -88,17 +82,35 @@ module.exports = function(RED) {
} }
}); });
xmpp.on('chat', function(from, message) { // xmpp.on('chat', function(from, message) {
var msg = { topic:from, payload:message }; // var msg = { topic:from, payload:message };
// if (!node.join && ((node.from === "") || (node.from === from))) {
// node.send([msg,null]);
// }
// });
xmpp.on('stanza', function(stanza) {
if (stanza.is('message')) {
if (stanza.attrs.type == 'chat') {
//console.log(stanza);
var body = stanza.getChild('body');
if (body) {
var msg = { payload:body.getText() };
var ids = stanza.attrs.from.split('/');
if (ids[1].length !== 36) {
msg.topic = stanza.attrs.from
}
else { msg.topic = ids[0]; }
if (!node.join && ((node.from === "") || (node.from === from))) { if (!node.join && ((node.from === "") || (node.from === from))) {
node.send([msg,null]); node.send([msg,null]);
} }
}
}
}
}); });
xmpp.on('groupchat', function(conference, from, message, stamp) { xmpp.on('groupchat', function(conference, from, message, stamp) {
if (!stamp) {stamp = Date.now(); } var msg = { topic:from, payload:message, room:conference };
//else { console.log("STAMP",stamp) }
var msg = { topic:from, payload:message, room:conference, ts:stamp };
if (from != node.nick) { if (from != node.nick) {
if ((node.join) && (node.from === conference)) { if ((node.join) && (node.from === conference)) {
node.send([msg,null]); node.send([msg,null]);
@ -108,7 +120,7 @@ module.exports = function(RED) {
//xmpp.on('chatstate', function(from, state) { //xmpp.on('chatstate', function(from, state) {
//console.log('%s is currently %s', from, state); //console.log('%s is currently %s', from, state);
//var msg = { topic:from, payload:state }; //var msg = { topic:from, payload: {presence:state} };
//node.send([null,msg]); //node.send([null,msg]);
//}); //});
@ -118,6 +130,11 @@ module.exports = function(RED) {
node.send([null,msg]); node.send([null,msg]);
}); });
// xmpp.on('groupbuddy', function(conference, from, state, statusText) {
// //console.log('%s: %s is in %s state - %s',conference, from, state, statusText);
// var msg = { topic:from, payload: { presence:state, status:statusText}, room:conference };
// });
xmpp.on('error', function(err) { xmpp.on('error', function(err) {
if (RED.settings.verbose) { node.log(err); } if (RED.settings.verbose) { node.log(err); }
if (err.hasOwnProperty("stanza")) { if (err.hasOwnProperty("stanza")) {
@ -165,15 +182,8 @@ module.exports = function(RED) {
function XmppOutNode(n) { function XmppOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.server = n.server; this.server = n.server;
this.serverConfig = RED.nodes.getNode(this.server); this.serverConfig = RED.nodes.getNode(this.server);
// this.host = this.serverConfig.server; this.nick = this.serverConfig.nickname || this.serverConfig.username.split("@")[0];
// this.port = this.serverConfig.port;
//this.nick = this.serverConfig.nickname || "Node-RED";
this.userid = this.serverConfig.username;
var pa = this.userid.split("@");
this.nick = this.serverConfig.nickname || pa[0];
this.join = n.join || false; this.join = n.join || false;
this.sendAll = n.sendObject; this.sendAll = n.sendObject;
this.to = n.to || ""; this.to = n.to || "";
@ -232,11 +242,11 @@ module.exports = function(RED) {
if (['away', 'dnd', 'xa', 'chat'].indexOf(msg.presence) > -1 ) { if (['away', 'dnd', 'xa', 'chat'].indexOf(msg.presence) > -1 ) {
xmpp.setPresence(msg.presence, msg.payload); xmpp.setPresence(msg.presence, msg.payload);
} }
else { node.warn("Can't set presence - invalid value"); } else { node.warn("Can't set presence - invalid value: "+msg.presence); }
} }
else { else {
var to = msg.topic; var to = node.to || msg.topic || "";
if (node.to !== "") { to = node.to; } if (to !== "") {
if (node.sendAll) { if (node.sendAll) {
xmpp.send(to, JSON.stringify(msg), node.join); xmpp.send(to, JSON.stringify(msg), node.join);
} }
@ -249,6 +259,7 @@ module.exports = function(RED) {
} }
} }
} }
}
}); });
node.on("close", function() { node.on("close", function() {

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-xmpp", "name" : "node-red-node-xmpp",
"version" : "0.2.1", "version" : "0.2.2",
"description" : "A Node-RED node to talk to an XMPP server", "description" : "A Node-RED node to talk to an XMPP server",
"dependencies" : { "dependencies" : {
"simple-xmpp" : "^1.3.0" "simple-xmpp" : "^1.3.0"