view jid.js @ 15:5149a856d9dd default tip

Fix hybrid mode.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 03 Nov 2011 17:28:49 -0700
parents 461a24a5a788
children
line wrap: on
line source

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 module !== 'undefined')
	module.exports = JID;