diff --git a/social/xmpp/92-xmpp.js b/social/xmpp/92-xmpp.js index 69a0cd13..c324d8d7 100644 --- a/social/xmpp/92-xmpp.js +++ b/social/xmpp/92-xmpp.js @@ -50,6 +50,8 @@ module.exports = function(RED) { this.connected = false; // store the nodes that have us as config so we know when to tear it all down. this.users = {}; + // Store the chatrooms (MUC) that we've joined (sent "presence" XML to) already + this.MUCs = {}; // helper variable, because "this" changes definition inside a callback var that = this; @@ -216,14 +218,23 @@ module.exports = function(RED) { // if we want to support passwords, we need to add that as a child of the x element // (third argument to the x/muc/children ) // We also turn off chat history (maxstanzas 0) because that's not what this node is about. - var stanza = xml('presence', - {"to": name}, - xml("x",'http://jabber.org/protocol/muc'), - { maxstanzas:0, seconds:1 } - ); - node.serverConfig.used(node); - xmpp.send(stanza); - + // Yes, there's a race condition, but it's not a huge problem to send two messages + // so we don't care. + if (name in node.serverConfig.MUCs) { + if (RED.settings.verbose || LOGITALL) { + node.log("already joined MUC "+name); + } + } + else { + var stanza = xml('presence', + {"to": name}, + xml("x",'http://jabber.org/protocol/muc'), + { maxstanzas:0, seconds:1 } + ); + node.serverConfig.used(node); + node.serverConfig.MUCs[name] = "joined"; + xmpp.send(stanza); + } } // separated out since we want the same functionality from both in and out nodes @@ -579,7 +590,13 @@ module.exports = function(RED) { var to = node.to || msg.topic || ""; if (to !== "") { var message; - var type = node.join? "groupchat":"chat"; + var type = "chat"; + if (node.join) { + // we want to connect to groupchat / chatroom / MUC + type = "groupchat"; + // joinMUC will do nothing if we're already joined + joinMUC(node, xmpp, to+'/'+node.nick); + } if (node.sendAll) { message = xml( "message", diff --git a/social/xmpp/package.json b/social/xmpp/package.json index e9c9149b..4f65efd9 100644 --- a/social/xmpp/package.json +++ b/social/xmpp/package.json @@ -1,6 +1,6 @@ { "name": "node-red-node-xmpp", - "version": "0.3.2", + "version": "0.3.3", "description": "A Node-RED node to talk to an XMPP server", "dependencies": { "@xmpp/client": "^0.12.0"