JSJaCPacket.js
Summary
Contains all Jabber/XMPP packet related classes.
Version: $Revision$
Author: Stefan Strigler steve@zeank.in-berlin.de
Class Summary
|
JSJaCIQ |
Models the XMPP notion of an 'iq' packet
|
JSJaCMessage |
Models the XMPP notion of an 'message' packet
|
JSJaCPacket |
Somewhat abstract base class for all kinds of specialised packets
|
JSJaCPresence |
Models the XMPP notion of a 'presence' packet
|
var JSJACPACKET_USE_XMLNS = true;
function JSJaCPacket(name) {
this.name = name;
if (typeof(JSJACPACKET_USE_XMLNS) != 'undefined' && JSJACPACKET_USE_XMLNS)
this.doc = XmlDocument.create(name,'jabber:client');
else
this.doc = XmlDocument.create(name,'');
}
JSJaCPacket.prototype.pType = function() { return this.name; };
JSJaCPacket.prototype.getDoc = function() {
return this.doc;
};
JSJaCPacket.prototype.getNode = function() {
if (this.getDoc() && this.getDoc().documentElement)
return this.getDoc().documentElement;
else
return null;
};
JSJaCPacket.prototype.setTo = function(to) {
if (!to || to == '')
this.getNode().removeAttribute('to');
else if (typeof(to) == 'string')
this.getNode().setAttribute('to',to);
else
this.getNode().setAttribute('to',to.toString());
return this;
};
JSJaCPacket.prototype.setFrom = function(from) {
if (!from || from == '')
this.getNode().removeAttribute('from');
else if (typeof(from) == 'string')
this.getNode().setAttribute('from',from);
else
this.getNode().setAttribute('from',from.toString());
return this;
};
JSJaCPacket.prototype.setID = function(id) {
if (!id || id == '')
this.getNode().removeAttribute('id');
else
this.getNode().setAttribute('id',id);
return this;
};
JSJaCPacket.prototype.setType = function(type) {
if (!type || type == '')
this.getNode().removeAttribute('type');
else
this.getNode().setAttribute('type',type);
return this;
};
JSJaCPacket.prototype.setXMLLang = function(xmllang) {
if (!xmllang || xmllang == '')
this.getNode().removeAttribute('xml:lang');
else
this.getNode().setAttribute('xml:lang',xmllang);
return this;
};
JSJaCPacket.prototype.getTo = function() {
return this.getNode().getAttribute('to');
};
JSJaCPacket.prototype.getFrom = function() {
return this.getNode().getAttribute('from');
};
JSJaCPacket.prototype.getToJID = function() {
return new JSJaCJID(this.getTo());
};
JSJaCPacket.prototype.getFromJID = function() {
return new JSJaCJID(this.getFrom());
};
JSJaCPacket.prototype.getID = function() {
return this.getNode().getAttribute('id');
};
JSJaCPacket.prototype.getType = function() {
return this.getNode().getAttribute('type');
};
JSJaCPacket.prototype.getXMLLang = function() {
return this.getNode().getAttribute('xml:lang');
};
JSJaCPacket.prototype.getXMLNS = function() {
return this.getNode().namespaceURI || this.getNode().getAttribute('xmlns');
};
JSJaCPacket.prototype.getChild = function(name, ns) {
if (!this.getNode()) {
return null;
}
name = name || '*';
ns = ns || '*';
if (this.getNode().getElementsByTagNameNS) {
return this.getNode().getElementsByTagNameNS(ns, name).item(0);
}
var nodes = this.getNode().getElementsByTagName(name);
if (ns != '*') {
for (var i=0; i<nodes.length; i++) {
if (nodes.item(i).namespaceURI == ns || nodes.item(i).getAttribute('xmlns') == ns) {
return nodes.item(i);
}
}
} else {
return nodes.item(0);
}
return null;
};
JSJaCPacket.prototype.getChildVal = function(name, ns) {
var node = this.getChild(name, ns);
var ret = '';
if (node && node.hasChildNodes()) {
for (var i=0; i<node.childNodes.length; i++)
if (node.childNodes.item(i).nodeValue)
ret += node.childNodes.item(i).nodeValue;
}
return ret;
};
JSJaCPacket.prototype.clone = function() {
return JSJaCPacket.wrapNode(this.getNode());
};
JSJaCPacket.prototype.isError = function() {
return (this.getType() == 'error');
};
JSJaCPacket.prototype.errorReply = function(stanza_error) {
var rPacket = this.clone();
rPacket.setTo(this.getFrom());
rPacket.setFrom();
rPacket.setType('error');
rPacket.appendNode('error',
{code: stanza_error.code, type: stanza_error.type},
[[stanza_error.cond]]);
return rPacket;
};
JSJaCPacket.prototype.xml = typeof XMLSerializer != 'undefined' ?
function() {
var r = (new XMLSerializer()).serializeToString(this.getNode());
if (typeof(r) == 'undefined')
r = (new XMLSerializer()).serializeToString(this.doc);
return r
} :
function() {
return this.getDoc().xml
};
JSJaCPacket.prototype._getAttribute = function(attr) {
return this.getNode().getAttribute(attr);
};
if (document.ELEMENT_NODE == null) {
document.ELEMENT_NODE = 1;
document.ATTRIBUTE_NODE = 2;
document.TEXT_NODE = 3;
document.CDATA_SECTION_NODE = 4;
document.ENTITY_REFERENCE_NODE = 5;
document.ENTITY_NODE = 6;
document.PROCESSING_INSTRUCTION_NODE = 7;
document.COMMENT_NODE = 8;
document.DOCUMENT_NODE = 9;
document.DOCUMENT_TYPE_NODE = 10;
document.DOCUMENT_FRAGMENT_NODE = 11;
document.NOTATION_NODE = 12;
}
JSJaCPacket.prototype._importNode = function(node, allChildren) {
switch (node.nodeType) {
case document.ELEMENT_NODE:
if (this.getDoc().createElementNS) {
var newNode = this.getDoc().createElementNS(node.namespaceURI, node.nodeName);
} else {
var newNode = this.getDoc().createElement(node.nodeName);
}
if (node.attributes && node.attributes.length > 0)
for (var i = 0, il = node.attributes.length;i < il; i++) {
var attr = node.attributes.item(i);
if (attr.nodeName == 'xmlns' && newNode.getAttribute('xmlns') != null ) continue;
if (newNode.setAttributeNS && attr.namespaceURI) {
newNode.setAttributeNS(attr.namespaceURI,
attr.nodeName,
attr.nodeValue);
} else {
newNode.setAttribute(attr.nodeName,
attr.nodeValue);
}
}
if (allChildren && node.childNodes && node.childNodes.length > 0) {
for (var i = 0, il = node.childNodes.length; i < il; i++) {
newNode.appendChild(this._importNode(node.childNodes.item(i), allChildren));
}
}
return newNode;
break;
case document.TEXT_NODE:
case document.CDATA_SECTION_NODE:
case document.COMMENT_NODE:
return this.getDoc().createTextNode(node.nodeValue);
break;
}
};
JSJaCPacket.prototype._setChildNode = function(nodeName, nodeValue) {
var aNode = this.getChild(nodeName);
var tNode = this.getDoc().createTextNode(nodeValue);
if (aNode)
try {
aNode.replaceChild(tNode,aNode.firstChild);
} catch (e) { }
else {
try {
aNode = this.getDoc().createElementNS(this.getNode().namespaceURI,
nodeName);
} catch (ex) {
aNode = this.getDoc().createElement(nodeName)
}
this.getNode().appendChild(aNode);
aNode.appendChild(tNode);
}
return aNode;
};
JSJaCPacket.prototype.buildNode = function(elementName) {
return JSJaCBuilder.buildNode(this.getDoc(),
elementName,
arguments[1],
arguments[2]);
};
JSJaCPacket.prototype.appendNode = function(element) {
if (typeof element=='object') {
return this.getNode().appendChild(element)
} else {
return this.getNode().appendChild(this.buildNode(element,
arguments[1],
arguments[2],
null,
this.getNode().namespaceURI));
}
};
function JSJaCPresence() {
this.base = JSJaCPacket;
this.base('presence');
}
JSJaCPresence.prototype = new JSJaCPacket;
JSJaCPresence.prototype.setStatus = function(status) {
this._setChildNode("status", status);
return this;
};
JSJaCPresence.prototype.setShow = function(show) {
if (show == 'chat' || show == 'away' || show == 'xa' || show == 'dnd')
this._setChildNode("show",show);
return this;
};
JSJaCPresence.prototype.setPriority = function(prio) {
this._setChildNode("priority", prio);
return this;
};
JSJaCPresence.prototype.setPresence = function(show,status,prio) {
if (show)
this.setShow(show);
if (status)
this.setStatus(status);
if (prio)
this.setPriority(prio);
return this;
};
JSJaCPresence.prototype.getStatus = function() {
return this.getChildVal('status');
};
JSJaCPresence.prototype.getShow = function() {
return this.getChildVal('show');
};
JSJaCPresence.prototype.getPriority = function() {
return this.getChildVal('priority');
};
function JSJaCIQ() {
this.base = JSJaCPacket;
this.base('iq');
}
JSJaCIQ.prototype = new JSJaCPacket;
JSJaCIQ.prototype.setIQ = function(to,type,id) {
if (to)
this.setTo(to);
if (type)
this.setType(type);
if (id)
this.setID(id);
return this;
};
JSJaCIQ.prototype.setQuery = function(xmlns) {
var query;
try {
query = this.getDoc().createElementNS(xmlns,'query');
} catch (e) {
query = this.getDoc().createElement('query');
query.setAttribute('xmlns',xmlns);
}
this.getNode().appendChild(query);
return query;
};
JSJaCIQ.prototype.getQuery = function() {
return this.getNode().getElementsByTagName('query').item(0);
};
JSJaCIQ.prototype.getQueryXMLNS = function() {
if (this.getQuery()) {
return this.getQuery().namespaceURI || this.getQuery().getAttribute('xmlns');
} else {
return null;
}
};
JSJaCIQ.prototype.reply = function(payload) {
var rIQ = this.clone();
rIQ.setTo(this.getFrom());
rIQ.setFrom();
rIQ.setType('result');
if (payload) {
if (typeof payload == 'string')
rIQ.getChild().appendChild(rIQ.getDoc().loadXML(payload));
else if (payload.constructor == Array) {
var node = rIQ.getChild();
for (var i=0; i<payload.length; i++)
if(typeof payload[i] == 'string')
node.appendChild(rIQ.getDoc().loadXML(payload[i]));
else if (typeof payload[i] == 'object')
node.appendChild(payload[i]);
}
else if (typeof payload == 'object')
rIQ.getChild().appendChild(payload);
}
return rIQ;
};
function JSJaCMessage() {
this.base = JSJaCPacket;
this.base('message');
}
JSJaCMessage.prototype = new JSJaCPacket;
JSJaCMessage.prototype.setBody = function(body) {
this._setChildNode("body",body);
return this;
};
JSJaCMessage.prototype.setSubject = function(subject) {
this._setChildNode("subject",subject);
return this;
};
JSJaCMessage.prototype.setThread = function(thread) {
this._setChildNode("thread", thread);
return this;
};
JSJaCMessage.prototype.getThread = function() {
return this.getChildVal('thread');
};
JSJaCMessage.prototype.getBody = function() {
return this.getChildVal('body');
};
JSJaCMessage.prototype.getSubject = function() {
return this.getChildVal('subject')
};
JSJaCPacket.wrapNode = function(node) {
var oPacket = null;
switch (node.nodeName.toLowerCase()) {
case 'presence':
oPacket = new JSJaCPresence();
break;
case 'message':
oPacket = new JSJaCMessage();
break;
case 'iq':
oPacket = new JSJaCIQ();
break;
}
if (oPacket) {
oPacket.getDoc().replaceChild(oPacket._importNode(node, true),
oPacket.getNode());
}
return oPacket;
};
Documentation generated by
JSDoc on Fri Jun 28 11:55:35 2024