//>>built
define("dojox/xmpp/xmppSession",["dijit","dojo","dojox","dojo/require!dojox/xmpp/TransportSession,dojox/xmpp/RosterService,dojox/xmpp/PresenceService,dojox/xmpp/UserService,dojox/xmpp/ChatService,dojox/xmpp/sasl"],function(n,h,e){h.provide("dojox.xmpp.xmppSession");h.require("dojox.xmpp.TransportSession");h.require("dojox.xmpp.RosterService");h.require("dojox.xmpp.PresenceService");h.require("dojox.xmpp.UserService");h.require("dojox.xmpp.ChatService");h.require("dojox.xmpp.sasl");e.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"}};e.xmpp.xmppSession=function(a){this.roster=[];this.chatRegister=[];this._iqId=
Math.round(1E9*Math.random());a&&h.isObject(a)&&h.mixin(this,a);this.session=new e.xmpp.TransportSession(a);h.connect(this.session,"onReady",this,"onTransportReady");h.connect(this.session,"onTerminate",this,"onTransportTerminate");h.connect(this.session,"onProcessProtocolResponse",this,"processProtocolResponse")};h.extend(e.xmpp.xmppSession,{roster:[],chatRegister:[],_iqId:0,open:function(a,b,c){if(a)this.jid=a,-1==a.indexOf("@")&&(this.jid=this.jid+"@"+this.domain);else throw Error("User id cannot be null");
b&&(this.password=b);c&&(this.resource=c);this.session.open()},close:function(){this.state=e.xmpp.xmpp.TERMINATE;this.session.close(e.xmpp.util.createElement("presence",{type:"unavailable",xmlns:e.xmpp.xmpp.CLIENT_NS},!0))},processProtocolResponse:function(a){var b=a.nodeName,c=b.indexOf(":");0<c&&(b=b.substring(c+1));switch(b){case "iq":case "presence":case "message":case "features":this[b+"Handler"](a);break;default:a.getAttribute("xmlns")==e.xmpp.xmpp.SASL_NS&&this.saslHandler(a)}},messageHandler:function(a){switch(a.getAttribute("type")){case "chat":this.chatHandler(a);
break;case "normal":break;default:this.simpleMessageHandler(a)}},iqHandler:function(a){"set"==a.getAttribute("type")?this.iqSetHandler(a):a.getAttribute("type")},presenceHandler:function(a){switch(a.getAttribute("type")){case "subscribe":this.presenceSubscriptionRequest(a.getAttribute("from"));break;case "subscribed":case "unsubscribed":break;case "error":this.processXmppError(a);break;default:this.presenceUpdate(a)}},featuresHandler:function(a){var b=[],c=!1,d=!1;if(a.hasChildNodes())for(var g=0;g<
a.childNodes.length;g++){var f=a.childNodes[g];switch(f.nodeName){case "mechanisms":for(var k=0;k<f.childNodes.length;k++)b.push(f.childNodes[k].firstChild.nodeValue);break;case "bind":c=!0;break;case "session":d=!0}}if(this.state==e.xmpp.xmpp.CONNECTED)if(this.auth)c&&this.bindResource(d);else for(g=0;g<b.length;g++)try{this.auth=e.xmpp.sasl.registry.match(b[g],this);break}catch(m){}},saslHandler:function(a){if("success"==a.nodeName)this.auth.onSuccess();else if("challenge"==a.nodeName)this.auth.onChallenge(a);
else a.hasChildNodes()&&(this.onLoginFailure(a.firstChild.nodeName),this.session.setState("Terminate",a.firstChild.nodeName))},sendRestart:function(){this.session._sendRestart()},chatHandler:function(a){var b,c={from:a.getAttribute("from"),to:a.getAttribute("to")};for(b=0;b<a.childNodes.length;b++){var d=a.childNodes[b];if(d.hasChildNodes())switch(d.nodeName){case "thread":c.chatid=d.firstChild.nodeValue;break;case "body":d.getAttribute("xmlns")&&""!==d.getAttribute("xmlns")||(c.body=d.firstChild.nodeValue);
break;case "subject":c.subject=d.firstChild.nodeValue;break;case "html":d.getAttribute("xmlns")==e.xmpp.xmpp.XHTML_IM_NS&&(c.xhtml=d.getElementsByTagName("body")[0])}}a=-1;if(c.chatid)for(b=0;b<this.chatRegister.length;b++){if((d=this.chatRegister[b])&&d.chatid==c.chatid){a=b;break}}else for(b=0;b<this.chatRegister.length;b++)(d=this.chatRegister[b])&&d.uid==this.getBareJid(c.from)&&(a=b);if(c.body&&""!==c.body||c.xhtml)-1<a?(b=this.chatRegister[a],b.receiveMessage(c)):(b=new e.xmpp.ChatService,b.uid=
this.getBareJid(c.from),b.chatid=c.chatid,b.firstMessage=!0,this.useChatState=!1,this.registerChatInstance(b,c))},simpleMessageHandler:function(a){},registerChatInstance:function(a,b){a.setSession(this);this.chatRegister.push(a);this.onRegisterChatInstance(a,b);a.receiveMessage(b,!0)},iqSetHandler:function(a){if(a.hasChildNodes()){var b=a.firstChild;switch(b.nodeName){case "query":"jabber:iq:roster"==b.getAttribute("xmlns")&&(this.rosterSetHandler(b),this.sendIqResult(a.getAttribute("id"),a.getAttribute("from")))}}},
sendIqResult:function(a,b){this.dispatchPacket(e.xmpp.util.createElement("iq",{id:a,to:b||this.domain,type:"result",from:this.jid+"/"+this.resource},!0))},rosterSetHandler:function(a){for(var b,c=0;c<a.childNodes.length;c++){var d=a.childNodes[c];if("item"==d.nodeName){for(var g=!1,f=-1,k=null,m=null,l=0;l<this.roster.length;l++)if(b=this.roster[l],d.getAttribute("jid")==b.jid){g=!0;if("remove"==d.getAttribute("subscription")){k={id:b.jid,name:b.name,groups:[]};for(f=0;f<b.groups.length;f++)k.groups.push(b.groups[f]);
this.roster.splice(l,1);f=e.xmpp.roster.REMOVED}else{m=h.clone(b);if(k=d.getAttribute("name"))this.roster[l].name=k;b.groups=[];d.getAttribute("subscription")&&(b.status=d.getAttribute("subscription"));b.substatus=e.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE;"subscribe"==d.getAttribute("ask")&&(b.substatus=e.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING);for(f=0;f<d.childNodes.length;f++)l=d.childNodes[f],"group"==l.nodeName&&l.hasChildNodes()&&b.groups.push(l.firstChild.nodeValue);k=b;f=e.xmpp.roster.CHANGED}break}g||
"remove"==d.getAttribute("subscription")||(k=b=this.createRosterEntry(d),f=e.xmpp.roster.ADDED);switch(f){case e.xmpp.roster.ADDED:this.onRosterAdded(k);break;case e.xmpp.roster.REMOVED:this.onRosterRemoved(k);break;case e.xmpp.roster.CHANGED:this.onRosterChanged(k,m)}}}},presenceUpdate:function(a){if(!a.getAttribute("to")||this.getBareJid(a.getAttribute("to"))==this.jid){var b=this.getResourceFromJid(a.getAttribute("from"));b={from:this.getBareJid(a.getAttribute("from")),resource:b,show:e.xmpp.presence.STATUS_ONLINE,
priority:5,hasAvatar:!1};"unavailable"==a.getAttribute("type")&&(b.show=e.xmpp.presence.STATUS_OFFLINE);for(var c=0;c<a.childNodes.length;c++){var d=a.childNodes[c];if(d.hasChildNodes())switch(d.nodeName){case "status":case "show":b[d.nodeName]=d.firstChild.nodeValue;break;case "priority":b.priority=parseInt(d.firstChild.nodeValue,10);break;case "x":d.firstChild&&d.firstChild.firstChild&&""!==d.firstChild.firstChild.nodeValue&&(b.avatarHash=d.firstChild.firstChild.nodeValue,b.hasAvatar=!0)}}this.onPresenceUpdate(b)}},
retrieveRoster:function(){var a={id:this.getNextIqId(),from:this.jid+"/"+this.resource,type:"get"},b=new e.string.Builder(e.xmpp.util.createElement("iq",a,!1));b.append(e.xmpp.util.createElement("query",{xmlns:"jabber:iq:roster"},!0));b.append("\x3c/iq\x3e");this.dispatchPacket(b,"iq",a.id).addCallback(this,"onRetrieveRoster")},getRosterIndex:function(a){-1==a.indexOf("@")&&(a+="@"+this.domain);for(var b=0;b<this.roster.length;b++)if(a==this.roster[b].jid)return b;return-1},createRosterEntry:function(a){var b=
{name:a.getAttribute("name"),jid:a.getAttribute("jid"),groups:[],status:e.xmpp.presence.SUBSCRIPTION_NONE,substatus:e.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE};b.name||(b.name=b.id);for(var c=0;c<a.childNodes.length;c++){var d=a.childNodes[c];"group"==d.nodeName&&d.hasChildNodes()&&b.groups.push(d.firstChild.nodeValue)}a.getAttribute("subscription")&&(b.status=a.getAttribute("subscription"));"subscribe"==a.getAttribute("ask")&&(b.substatus=e.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING);return b},
bindResource:function(a){var b={id:this.getNextIqId(),type:"set"},c=new e.string.Builder(e.xmpp.util.createElement("iq",b,!1));c.append(e.xmpp.util.createElement("bind",{xmlns:e.xmpp.xmpp.BIND_NS},!1));this.resource&&(c.append(e.xmpp.util.createElement("resource")),c.append(this.resource),c.append("\x3c/resource\x3e"));c.append("\x3c/bind\x3e\x3c/iq\x3e");this.dispatchPacket(c,"iq",b.id).addCallback(this,function(d){this.onBindResource(d,a);return d})},getNextIqId:function(){return"im_"+this._iqId++},
presenceSubscriptionRequest:function(a){this.onSubscriptionRequest(a)},dispatchPacket:function(a,b,c){if("Terminate"!=this.state)return this.session.dispatchPacket(a,b,c)},setState:function(a,b){if(this.state!=a){if(this["on"+a])this["on"+a](a,this.state,b);this.state=a}},search:function(a,b,c){b={id:this.getNextIqId(),"xml:lang":this.lang,type:"set",from:this.jid+"/"+this.resource,to:b};var d=new e.string.Builder(e.xmpp.util.createElement("iq",b,!1));d.append(e.xmpp.util.createElement("query",{xmlns:"jabber:iq:search"},
!1));d.append(e.xmpp.util.createElement(c,{},!1));d.append(a);d.append("\x3c/").append(c).append("\x3e");d.append("\x3c/query\x3e\x3c/iq\x3e");this.dispatchPacket(d.toString,"iq",b.id).addCallback(this,"_onSearchResults")},_onSearchResults:function(a){if("result"==a.getAttribute("type")&&a.hasChildNodes())this.onSearchResults([])},onLogin:function(){this.retrieveRoster()},onLoginFailure:function(a){},onBindResource:function(a,b){if("result"==a.getAttribute("type")){if(a.hasChildNodes()&&"bind"==a.firstChild.nodeName){var c=
a.firstChild;c.hasChildNodes()&&"jid"==c.firstChild.nodeName&&c.firstChild.hasChildNodes()&&(c=c.firstChild.firstChild.nodeValue,this.jid=this.getBareJid(c),this.resource=this.getResourceFromJid(c));if(b){c={id:this.getNextIqId(),type:"set"};var d=new e.string.Builder(e.xmpp.util.createElement("iq",c,!1));d.append(e.xmpp.util.createElement("session",{xmlns:e.xmpp.xmpp.SESSION_NS},!0));d.append("\x3c/iq\x3e");this.dispatchPacket(d,"iq",c.id).addCallback(this,"onBindSession");return}}this.onLogin()}else"error"==
a.getAttribute("type")&&(c=this.processXmppError(a),this.onLoginFailure(c))},onBindSession:function(a){if("error"==a.getAttribute("type"))a=this.processXmppError(a),this.onLoginFailure(a);else this.onLogin()},onSearchResults:function(a){},onRetrieveRoster:function(a){if("result"==a.getAttribute("type")&&a.hasChildNodes()){var b=a.getElementsByTagName("query")[0];if("jabber:iq:roster"==b.getAttribute("xmlns"))for(var c=0;c<b.childNodes.length;c++)"item"==b.childNodes[c].nodeName&&(this.roster[c]=this.createRosterEntry(b.childNodes[c]))}else a.getAttribute("type");
this.setState(e.xmpp.xmpp.ACTIVE);this.onRosterUpdated();return a},onRosterUpdated:function(){},onSubscriptionRequest:function(a){},onPresenceUpdate:function(a){},onTransportReady:function(){this.setState(e.xmpp.xmpp.CONNECTED);this.rosterService=new e.xmpp.RosterService(this);this.presenceService=new e.xmpp.PresenceService(this);this.userService=new e.xmpp.UserService(this)},onTransportTerminate:function(a,b,c){this.setState(e.xmpp.xmpp.TERMINATE,c)},onConnected:function(){},onTerminate:function(a,
b,c){},onActive:function(){},onRegisterChatInstance:function(a,b){},onRosterAdded:function(a){},onRosterRemoved:function(a){},onRosterChanged:function(a,b){},processXmppError:function(a){for(var b={stanzaType:a.nodeName,id:a.getAttribute("id")},c=0;c<a.childNodes.length;c++){var d=a.childNodes[c];switch(d.nodeName){case "error":b.errorType=d.getAttribute("type");for(var g=0;g<d.childNodes.length;g++){var f=d.childNodes[g];"text"==f.nodeName&&f.getAttribute("xmlns")==e.xmpp.xmpp.STANZA_NS&&f.hasChildNodes()?
b.message=f.firstChild.nodeValue:f.getAttribute("xmlns")!=e.xmpp.xmpp.STANZA_NS||f.hasChildNodes()||(b.condition=f.nodeName)}}}return b},sendStanzaError:function(a,b,c,d,g,f){g={type:"error"};b&&(g.to=b);c&&(g.id=c);b=new e.string.Builder(e.xmpp.util.createElement(a,g,!1));b.append(e.xmpp.util.createElement("error",{type:d},!1));b.append(e.xmpp.util.createElement("condition",{xmlns:e.xmpp.xmpp.STANZA_NS},!0));f&&(b.append(e.xmpp.util.createElement("text",{xmlns:e.xmpp.xmpp.STANZA_NS,"xml:lang":this.lang},
!1)),b.append(f).append("\x3c/text\x3e"));b.append("\x3c/error\x3e\x3c/").append(a).append("\x3e");this.dispatchPacket(b.toString())},getBareJid:function(a){var b=a.indexOf("/");return-1!=b?a.substring(0,b):a},getResourceFromJid:function(a){var b=a.indexOf("/");return-1!=b?a.substring(b+1,a.length):""}})});