diff jid.js @ 0:156c2fd7c626

First commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 22 Feb 2012 19:57:56 +0100
parents
children
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/jid.js
@@ -0,0 +1,115 @@
+'use strict';
+
+/**
+  Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+
+  Permission to use, copy, modify, and/or distribute this software for any
+  purpose with or without fee is hereby granted, provided that the above
+  copyright notice and this permission notice appear in all copies.
+
+  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+
+/**
+ * @constructor Creates a new JID object.
+ * @param {String} [aJID] The host, bare or full JID.
+ */
+var JID = function(aJID) {
+  this.node = null;
+  this.domain = null;
+  this.resource = null;
+
+  if (aJID)
+    this.full = aJID;
+
+  //TODO: use a stringprep library to validate the input.
+};
+
+JID.prototype = {
+  toString: function() {
+    return this.full;
+  },
+
+  equals: function(aJID) {
+    if (!(aJID instanceof JID))
+      aJID = new JID(aJID);
+
+    return (this.node === aJID.node &&
+            this.domain === aJID.domain &&
+            this.resource === aJID.resource)
+  },
+
+  get bare() {
+    if (!this.domain)
+      return null;
+
+    if (this.node)
+      return this.node + '@' + this.domain;
+
+    return this.domain;
+  },
+
+  set bare(aJID) {
+    if (!aJID)
+      return;
+
+    var s = aJID.indexOf('/');
+    if (s != -1)
+      aJID = aJID.substring(0, s);
+
+    s = aJID.indexOf('@');
+    if (s == -1) {
+      this.node = null;
+      this.domain = aJID;
+    } else {
+      this.node = aJID.substring(0, s);
+      this.domain = aJID.substring(s+1);
+    }
+  },
+
+  get full() {
+    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;
+  },
+
+  set full(aJID) {
+    if (!aJID)
+      return;
+
+    var s = aJID.indexOf('/');
+    if (s == -1)
+      this.resource = null;
+    else {
+      this.resource = aJID.substring(s+1);
+      aJID = aJID.substring(0, s);
+    }
+
+    s = aJID.indexOf('@');
+    if (s == -1) {
+      this.node = null;
+      this.domain = aJID;
+    } else {
+      this.node = aJID.substring(0, s);
+      this.domain = aJID.substring(s+1);
+    }
+  }
+};
+
+module.exports = JID;