diff jid.js @ 0:f62b5c395a48

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 04 Jun 2011 05:02:47 +0200
parents
children eb5b6d3ab09b
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/jid.js
@@ -0,0 +1,105 @@
+var JID = function(jid) {
+	this.node = null;
+	this.domain = null;
+	this.resource = null;
+	this.action = null;
+	this.query = {};
+
+	this.toString = function() {
+		return this.full;
+	}
+
+	this.__defineGetter__('bare', function() {
+		if (!this.domain)
+			return null;
+
+		if (this.node)
+			return this.node + '@' + this.domain;
+
+		return this.domain;
+	});
+
+	this.__defineSetter__('bare', function(jid) {
+		var s = jid.indexOf('/');
+		if (s != -1)
+			jid = jid.substring(0, s);
+
+		s = jid.indexOf('@');
+		if (s == -1) {
+			this.node = null;
+			this.domain = jid;
+		} else {
+			this.node = jid.substring(0, s);
+			this.domain = jid.substring(s+1);
+		}
+	});
+
+	this.__defineGetter__('full', function() {
+		if (!this.domain)
+			return null;
+
+		var full = this.domain;
+
+		if (this.node)
+			full = this.node + '@' + full;
+
+		if (this.resource)
+			full = full + '/' + this.resource;
+
+		return full;
+	});
+
+	this.__defineSetter__('full', function(jid) {
+		var s = jid.indexOf('/');
+		if (s == -1)
+			this.resource = null;
+		else {
+			this.resource = jid.substring(s+1);
+			jid = jid.substring(0, s);
+		}
+
+		s = jid.indexOf('@');
+		if (s == -1) {
+			this.node = null;
+			this.domain = jid;
+		} else {
+			this.node = jid.substring(0, s);
+			this.domain = jid.substring(s+1);
+		}
+	});
+
+	this.__defineSetter__('uri', function(uri) {
+		if (uri.indexOf('xmpp:') != 0)
+			return;
+		uri = uri.substring(5);
+
+		var s = uri.indexOf('?');
+		if (s == -1) {
+			this.full = uri;
+			return;
+		}
+
+		this.full = uri.substring(0, s);
+
+		uri = uri.substring(s+1).split(';');
+		this.action = null;
+		this.query = {};
+
+		for (var i in uri) {
+			if (i == 0) {
+				this.action = uri[i];
+				continue;
+			}
+			s = uri[i].indexOf('=');
+			var key = uri[i].substring(0, s);
+			var value = uri[i].substring(s+1);
+			this.query[key] = value;
+		}
+	});
+
+	if (jid)
+		this.full = jid;
+};
+
+if (typeof exports === 'object')
+	exports.JID = JID;