[Pkg-javascript-commits] [ltx] 30/469: component support

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:00:54 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 18268d9300eaecbc1a866e1d7d9c0afd9eacc51b
Author: Astro <astro at spaceboyz.net>
Date:   Mon May 31 01:28:52 2010 +0200

    component support
---
 examples/send_message_component.js | 42 +++++++++++++++++++++++++++++
 lib/xmpp.js                        |  2 ++
 lib/xmpp/component.js              | 55 ++++++++++++++++++++++++++++++++++++++
 lib/xmpp/connection.js             |  6 ++++-
 lib/xmpp/xml.js                    |  5 +++-
 5 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/examples/send_message_component.js b/examples/send_message_component.js
new file mode 100644
index 0000000..b143784
--- /dev/null
+++ b/examples/send_message_component.js
@@ -0,0 +1,42 @@
+require.paths.push('../lib');
+var sys = require('sys');
+var xmpp = require('xmpp');
+var argv = process.argv;
+
+if (argv.length < 6) {
+    sys.puts('Usage: node send_message.js <my-jid> <my-password> <server> <port> <my-text> <jid1> [jid2] ... [jidN]');
+    process.exit(1);
+}
+
+var c = new xmpp.Component({ jid: argv[2],
+			     password: argv[3],
+			     host: argv[4],
+			     port: Number(argv[5])
+			   });
+c.addListener('online',
+	       function() {
+		   argv.slice(7).forEach(
+		       function(to) {
+			   c.send(new xmpp.Element('message',
+						   { to: to,
+						     from: c.jid,
+						     type: 'chat'}).
+				  c('body').
+				  t(argv[4]));
+		       });
+		   c.end();
+	       });
+c.addListener('authFail',
+	       function() {
+		   sys.puts("Authentication failure");
+		   process.exit(1);
+	       });
+c.addListener('error',
+	       function(e) {
+		   sys.puts(e);
+		   process.exit(1);
+	       });
+c.addListener('end',
+	       function() {
+		   /* node.js will exit by itself */
+	       });
diff --git a/lib/xmpp.js b/lib/xmpp.js
index 759d9a3..e74b311 100644
--- a/lib/xmpp.js
+++ b/lib/xmpp.js
@@ -1,8 +1,10 @@
 var Client = require('./xmpp/client').Client;
+var Component = require('./xmpp/component').Component;
 var JID = require('./xmpp/jid').JID;
 var XML = require('./xmpp/xml');
 
 exports.Client = Client;
+exports.Component = Component;
 exports.JID = JID;
 exports.XML = XML;
 exports.Element = XML.Element;
diff --git a/lib/xmpp/component.js b/lib/xmpp/component.js
new file mode 100644
index 0000000..8c16376
--- /dev/null
+++ b/lib/xmpp/component.js
@@ -0,0 +1,55 @@
+var Connection = require('./connection');
+var JID = require('./jid').JID;
+var xml = require('./xml');
+var sys = require('sys');
+var crypto = require('crypto');
+
+var NS_COMPONENT = 'jabber:component:accept';
+
+/**
+ * params:
+ *   jid: String (required)
+ *   password: String (required)
+ *   host: String (required)
+ *   port: Number (required)
+ */
+function Component(params) {
+    Connection.Connection.call(this);
+
+    if (typeof params.jid == 'string')
+	this.jid = new JID(params.jid);
+    else
+	this.jid = params.jid;
+    this.password = params.password;
+    this.xmlns = NS_COMPONENT;
+    this.xmppVersion = "1.0";
+    this.streamTo = this.jid.domain;
+    this.addListener('streamStart', this.onStreamStart);
+    this.addListener('rawStanza', this.onRawStanza);
+
+    this.connect(params.port, params.host);
+}
+
+sys.inherits(Component, Connection.Connection);
+exports.Component = Component;
+
+Component.prototype.onStreamStart = function(streamAttrs) {
+    var digest = sha1_hex(streamAttrs.id + this.password);
+    this.send(new xml.Element('handshake').t(digest));
+};
+
+Component.prototype.onRawStanza = function(stanza) {
+    if (!this.authenticated &&
+	stanza.is('handshake', NS_COMPONENT)) {
+	this.authenticated = true;
+	this.emit('online');
+    } else if (this.authenticated) {
+	this.emit('stanza', stanza);
+    }
+};
+
+function sha1_hex(s) {
+    var hash = crypto.createHash('sha1');
+    hash.update(s);
+    return hash.digest('hex');
+}
diff --git a/lib/xmpp/connection.js b/lib/xmpp/connection.js
index 80f798e..c30fe09 100644
--- a/lib/xmpp/connection.js
+++ b/lib/xmpp/connection.js
@@ -49,7 +49,11 @@ Connection.prototype.startParser = function() {
 		    k.substr(0, 6) == 'xmlns:')
 		    self.streamNsAttrs[k] = attrs[k];
 	    }
-	} else {
+
+	    /* Notify in case we don't wait for <stream:features/> (Component)
+	     */
+	    self.emit('streamStart', attrs);
+	     } else {
 	    var child = new xml.Element(name, attrs);
 	    if (!self.element) {
 		/* A new stanza */
diff --git a/lib/xmpp/xml.js b/lib/xmpp/xml.js
index 3e99423..6a84497 100644
--- a/lib/xmpp/xml.js
+++ b/lib/xmpp/xml.js
@@ -139,7 +139,10 @@ Element.prototype.write = function(writer) {
 	writer(" ");
 	writer(k);
 	writer("=\"");
-	writer(escapeXml(this.attrs[k]));
+	var v = this.attrs[k];
+	if (typeof v != 'string')
+	    v = v.toString();
+	writer(escapeXml(v));
 	writer("\"");
     }
     if (this.children.length == 0) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list