dojo.provide("dojox.xmpp.xmppSession");
dojo.require("dojox.xmpp.TransportSession");
dojo.require("dojox.xmpp.RosterService");
dojo.require("dojox.xmpp.PresenceService");
dojo.require("dojox.xmpp.UserService");
dojo.require("dojox.xmpp.ChatService");
dojo.require("dojox.xmpp.sasl");
dojox.xmpp.xmpp = {
STREAM_NS: 'http://etherx.jabber.org/streams',
CLIENT_NS: 'jabber:client',
STANZA_NS: 'urn:ietf:params:xml:ns:xmpp-stanzas',
SASL_NS: 'urn:ietf:params:xml:ns:xmpp-sasl',
BIND_NS: 'urn:ietf:params:xml:ns:xmpp-bind',
SESSION_NS: 'urn:ietf:params:xml:ns:xmpp-session',
BODY_NS: "http://jabber.org/protocol/httpbind",
XHTML_BODY_NS: "http://www.w3.org/1999/xhtml",
XHTML_IM_NS: "http://jabber.org/protocol/xhtml-im",
INACTIVE: "Inactive",
CONNECTED: "Connected",
ACTIVE: "Active",
TERMINATE: "Terminate",
LOGIN_FAILURE: "LoginFailure",
INVALID_ID: -1,
NO_ID: 0,
error:{
BAD_REQUEST: 'bad-request',
CONFLICT: 'conflict',
FEATURE_NOT_IMPLEMENTED: 'feature-not-implemented',
FORBIDDEN: 'forbidden',
GONE: 'gone',
INTERNAL_SERVER_ERROR: 'internal-server-error',
ITEM_NOT_FOUND: 'item-not-found',
ID_MALFORMED: 'jid-malformed',
NOT_ACCEPTABLE: 'not-acceptable',
NOT_ALLOWED: 'not-allowed',
NOT_AUTHORIZED: 'not-authorized',
SERVICE_UNAVAILABLE: 'service-unavailable',
SUBSCRIPTION_REQUIRED: 'subscription-required',
UNEXPECTED_REQUEST: 'unexpected-request'
}
};
dojox.xmpp.xmppSession = function(props){
this.roster = [];
this.chatRegister = [];
this._iqId = Math.round(Math.random() * 1000000000);
//mixin any options that we want to provide to this service
if (props && dojo.isObject(props)) {
dojo.mixin(this, props);
}
this.session = new dojox.xmpp.TransportSession(props);
dojo.connect(this.session, "onReady", this, "onTransportReady");
dojo.connect(this.session, "onTerminate", this, "onTransportTerminate");
dojo.connect(this.session, "onProcessProtocolResponse", this, "processProtocolResponse");
};
dojo.extend(dojox.xmpp.xmppSession, {
roster: [],
chatRegister: [],
_iqId: 0,
open: function(user, password, resource){
if (!user) {
throw new Error("User id cannot be null");
} else {
this.jid = user;
if(user.indexOf('@') == -1) {
this.jid = this.jid + '@' + this.domain;
}
}
//allow null password here as its not needed in the SSO case
if (password) {
this.password = password;
}
//normally you should NOT supply a resource and let the server send you one
//as part of your jid...see onBindResource()
if (resource) {
this.resource = resource;
}
this.session.open();
},
close: function(){
this.state = dojox.xmpp.xmpp.TERMINATE;
this.session.close(dojox.xmpp.util.createElement("presence",{type:"unavailable",xmlns:dojox.xmpp.xmpp.CLIENT_NS},true));
},
processProtocolResponse: function(msg){
//console.log("xmppSession::processProtocolResponse() ", msg, msg.nodeName);
var type = msg.nodeName;
var nsIndex =type.indexOf(":");
if(nsIndex > 0) {
type = type.substring(nsIndex+1);
}
switch(type){
case "iq":
case "presence":
case "message":
case "features":
this[type + "Handler"](msg);
break;
default:
//console.log("default action?", msg.getAttribute('xmlns'));
if(msg.getAttribute('xmlns')==dojox.xmpp.xmpp.SASL_NS){
this.saslHandler(msg);
}
}
},
//HANDLERS
messageHandler: function(msg){
//console.log("xmppSession::messageHandler() ",msg);
switch(msg.getAttribute('type')){
case "chat":
this.chatHandler(msg);
break;
case "normal":
default:
this.simpleMessageHandler(msg);
}
},
iqHandler: function(msg){
//console.log("xmppSession::iqHandler()", msg);
if (msg.getAttribute('type')=="set"){
this.iqSetHandler(msg);
return;
} else if (msg.getAttribute('type')=='get'){
// this.sendStanzaError('iq', this.domain, msg.getAttribute('from'), 'cancel', 'service-unavailable', 'service not implemented');
return;
}
},
presenceHandler: function(msg){
//console.log("xmppSession::presenceHandler()");
switch(msg.getAttribute('type')){
case 'subscribe':
//console.log("PresenceHandler: ", msg.getAttribute('from'));
this.presenceSubscriptionRequest(msg.getAttribute('from'));
break;
case 'subscribed':
case 'unsubscribed':
break;
case 'error':
this.processXmppError(msg);
//console.log("xmppService::presenceHandler() Error");
break;
default:
this.presenceUpdate(msg);
break;
}
},
featuresHandler: function(msg){
//console.log("xmppSession::featuresHandler() ",msg);
var authMechanisms = [];
var hasBindFeature = false;
var hasSessionFeature = false;
if(msg.hasChildNodes()){
for(var i=0; i
var n = msg.childNodes[i];
//console.log("featuresHandler::node", n);
switch(n.nodeName){
case 'mechanisms':
for (var x=0; x //console.log("featuresHandler::node::mechanisms", n.childNodes[x].firstChild.nodeValue);
authMechanisms.push(n.childNodes[x].firstChild.nodeValue);
}
break;
case 'bind':
//if (n.getAttribute('xmlns')==dojox.xmpp.xmpp.BIND_NS) {
hasBindFeature = true;
// }
break;
case 'session':
hasSessionFeature = true;
}
}
}
//console.log("Has connected/bind?", this.state, hasBindFeature, authMechanisms);
if(this.state == dojox.xmpp.xmpp.CONNECTED){
if(!this.auth){
// start the login
for(var i=0; i try{
this.auth = dojox.xmpp.sasl.registry.match(authMechanisms[i], this);
break;
}catch(e){
console.warn("No suitable auth mechanism found for: ", authMechanisms[i]);
}
}
}else if(hasBindFeature){
this.bindResource(hasSessionFeature);
}
}