📄 jsjac.js
字号:
function JSJaCHBCGetRequestString(raw){raw=raw||'';var reqstr='';if(this._rid<=this._last_rid&&typeof(this._last_requests[this._rid])!='undefined')reqstr=this._last_requests[this._rid].xml;else{var xml='';while(this._pQueue.length){var curNode=this._pQueue[0];xml+=curNode;this._pQueue=this._pQueue.slice(1,this._pQueue.length);}reqstr="<body rid='"+this._rid+"' sid='"+this._sid+"' xmlns='http://jabber.org/protocol/httpbind' ";if(JSJAC_HAVEKEYS){reqstr+="key='"+this._keys.getKey()+"' ";if(this._keys.lastKey()){this._keys=new JSJaCKeys(hex_sha1,this.oDbg);reqstr+="newkey='"+this._keys.getKey()+"' ";}}if(this._reinit){reqstr+="xmpp:restart='true' ";this._reinit=false;}if(xml!=''||raw!=''){reqstr+=">"+raw+xml+"</body>";}else{reqstr+="/>";}this._last_requests[this._rid]=new Object();this._last_requests[this._rid].xml=reqstr;this._last_rid=this._rid;for(var i in this._last_requests)if(this._last_requests.hasOwnProperty(i)&&i<this._rid-this._hold)delete(this._last_requests[i]);}return reqstr;}function JSJaCHBCPrepareResponse(req){if(!this.connected())return null;if(!req)return null;var r=req.r;try{if(r.status==404||r.status==403){oCon._abort();return null;}if(r.status!=200||!r.responseXML){this._errcnt++;var errmsg="invalid response ("+r.status+"):\n"+r.getAllResponseHeaders()+"\n"+r.responseText;if(!r.responseXML)errmsg+="\nResponse failed to parse!";this.oDbg.log(errmsg,1);if(this._errcnt>JSJAC_ERR_COUNT){oCon._abort();return null;}this.oDbg.log("repeating ("+this._errcnt+")",1);this._setStatus('proto_error_fallback');setTimeout("oCon._resume()",oCon.getPollInterval());return null;}}catch(e){this.oDbg.log("XMLHttpRequest error: status not available",1);return null;}var body=r.responseXML.documentElement;if(!body||body.tagName!='body'||body.namespaceURI!='http://jabber.org/protocol/httpbind'){this.oDbg.log("invalid response:\n"+r.responseText,1);this._setStatus('internal_server_error');clearTimeout(this._timeout);clearInterval(this._interval);clearInterval(this._inQto);this._handleEvent('onerror',JSJaCError('500','wait','internal-server-error'));this._connected=false;this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');return null;}if(typeof(req.rid)!='undefined'&&this._last_requests[req.rid]){if(this._last_requests[req.rid].handled){this.oDbg.log("already handled "+req.rid,2);return null;}elsethis._last_requests[req.rid].handled=true;}if(body.getAttribute("type")=="terminate"){this.oDbg.log("session terminated:\n"+r.responseText,1);clearTimeout(this._timeout);clearInterval(this._interval);clearInterval(this._inQto);if(body.getAttribute("condition")=="remote-stream-error")if(body.getElementsByTagName("conflict").length>0)this._setStatus("session-terminate-conflict");this._handleEvent('onerror',JSJaCError('503','cancel',body.getAttribute('condition')));this._connected=false;this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');return null;}this._errcnt=0;return r.responseXML.documentElement;}function JSJaCHttpPollingConnection(oArg){this.base=JSJaCConnection;this.base(oArg);JSJACPACKET_USE_XMLNS=false;this.connect=JSJaCHPCConnect;this.disconnect=JSJaCHPCDisconnect;this.isPolling=function(){return true;};this._getFreeSlot=function(){if(typeof(this._req[0])=='undefined'||typeof(this._req[0].r)=='undefined'||this._req[0].r.readyState==4)return 0;elsereturn-1;}this._getRequestString=JSJaCHPCGetRequestString;this._getStreamID=JSJaCHPCGetStream;this._getSuspendVars=function(){return new Array();}this._prepareResponse=JSJaCHPCPrepareResponse;this._reInitStream=JSJaCHPCReInitStream;this._resume=function(){this._process(this._timerval);this._interval=setInterval("oCon._checkQueue()",JSJAC_CHECKQUEUEINTERVAL);this._inQto=setInterval("oCon._checkInQ();",JSJAC_CHECKINQUEUEINTERVAL);}this._setupRequest=JSJaCHPCSetupRequest;this._suspend=function(){};}JSJaCHttpPollingConnection.prototype=new JSJaCConnection();function JSJaCHPCSetupRequest(async){var r=XmlHttp.create();try{r.open("POST",this._httpbase,async);r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');}catch(e){this.oDbg.log(e,1);}var req=new Object();req.r=r;return req;}function JSJaCHPCGetRequestString(raw){var reqstr=this._sid;if(JSJAC_HAVEKEYS){reqstr+=";"+this._keys.getKey();if(this._keys.lastKey()){this._keys=new JSJaCKeys(b64_sha1,this.oDbg);reqstr+=';'+this._keys.getKey();}}reqstr+=',';if(raw)reqstr+=raw;while(this._pQueue.length){reqstr+=this._pQueue[0];this._pQueue=this._pQueue.slice(1,this._pQueue.length);}return reqstr;}function JSJaCHPCPrepareResponse(r){var req=r.r;if(!this.connected())return null;if(req.status!=200){this.oDbg.log("invalid response ("+req.status+"):"+req.responseText+"\n"+req.getAllResponseHeaders(),1);this._setStatus('internal_server_error');clearTimeout(this._timeout);clearInterval(this._interval);clearInterval(this._inQto);this._connected=false;this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');this._handleEvent('onerror',JSJaCError('503','cancel','service-unavailable'));return null;}this.oDbg.log(req.getAllResponseHeaders(),4);var aPList=req.getResponseHeader('Set-Cookie');aPList=aPList.split(";");var sid;for(var i=0;i<aPList.length;i++){var aArg=aPList[i].split("=");if(aArg[0]=='ID')sid=aArg[1];}if(typeof(sid)!='undefined'&&sid.indexOf(':0')!=-1){switch(sid.substring(0,sid.indexOf(':0'))){case'0':this.oDbg.log("invalid response:"+req.responseText,1);break;case'-1':this.oDbg.log("Internal Server Error",1);break;case'-2':this.oDbg.log("Bad Request",1);break;case'-3':this.oDbg.log("Key Sequence Error",1);break;}this._setStatus('internal_server_error');clearTimeout(this._timeout);clearInterval(this._interval);clearInterval(this._inQto);this._handleEvent('onerror',JSJaCError('500','wait','internal-server-error'));this._connected=false;this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');return null;}if(!req.responseText||req.responseText=='')return null;try{var doc=JSJaCHttpPollingConnection.parseTree("<body>"+req.responseText+"</body>");if(!doc||doc.tagName=='parsererror'){this.oDbg.log("parsererror",1);doc=JSJaCHttpPollingConnection.parseTree("<stream:stream xmlns:stream='http://etherx.jabber.org/streams'>"+req.responseText);if(doc&&doc.tagName!='parsererror'){this.oDbg.log("stream closed",1);if(doc.getElementsByTagName('conflict').length>0)this._setStatus("session-terminate-conflict");clearTimeout(this._timeout);clearInterval(this._interval);clearInterval(this._inQto);this._handleEvent('onerror',JSJaCError('503','cancel','session-terminate'));this._connected=false;this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');}elsethis.oDbg.log("parsererror:"+doc,1);return doc;}return doc;}catch(e){this.oDbg.log("parse error:"+e.message,1);}return null;;}function JSJaCHPCConnect(oArg){this.domain=oArg.domain||'localhost';this.username=oArg.username;this.resource=oArg.resource||'jsjac';this.pass=oArg.pass;this.register=oArg.register;this.authtype=oArg.authtype||'sasl';this.jid=this.username+'@'+this.domain;this.fulljid=this.jid+this.resource;this.authhost=oArg.authhost||this.domain;var reqstr="0";if(JSJAC_HAVEKEYS){this._keys=new JSJaCKeys(b64_sha1,this.oDbg);key=this._keys.getKey();reqstr+=";"+key;}var streamto=this.domain;if(this.authhost)streamto=this.authhost;reqstr+=",<stream:stream to='"+streamto+"' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";this.oDbg.log(reqstr,4);this._req[0]=this._setupRequest(false);this._req[0].r.send(reqstr);this.oDbg.log(this._req[0].r.getAllResponseHeaders(),4);var aPList=this._req[0].r.getResponseHeader('Set-Cookie');aPList=aPList.split(";");for(var i=0;i<aPList.length;i++){aArg=aPList[i].split("=");if(aArg[0]=='ID')this._sid=aArg[1];}this.oDbg.log("got sid: "+this._sid,2);oCon=this;this._interval=setInterval("oCon._checkQueue()",JSJAC_CHECKQUEUEINTERVAL);this._inQto=setInterval("oCon._checkInQ();",JSJAC_CHECKINQUEUEINTERVAL);this._getStreamID();}function JSJaCHPCGetStream(){if(!this._req[0].r.responseXML||this._req[0].r.responseText==''){oCon=this;this._timeout=setTimeout("oCon._sendEmpty()",1000);return;}this.oDbg.log(this._req[0].r.responseText,4);if(this._req[0].r.responseText.match(/id=[\'\"]([^\'\"]+)[\'\"]/))this.streamid=RegExp.$1;this.oDbg.log("got streamid: "+this.streamid,2);var doc;try{doc=XmlDocument.create("doc");doc.loadXML(this._req[0].r.responseText+'</stream:stream>');this._parseStreamFeatures(doc);}catch(e){this.oDbg.log("loadXML: "+e.toString(),1);}if(this.register)this._doInBandReg();elsethis._doAuth();this._connected=true;this._process(this._timerval);}function JSJaCHPCReInitStream(to,cb,arg){oCon._sendRaw("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='"+to+"' version='1.0'>",cb,arg);}function JSJaCHPCDisconnect(){if(!this.connected())return;this._checkQueue();clearInterval(this._interval);clearInterval(this._inQto);if(this._timeout)clearTimeout(this._timeout);this._req[0]=this._setupRequest(false);if(JSJAC_HAVEKEYS)this._req[0].r.send(this._sid+";"+this._keys.getKey()+",</stream:stream>");elsethis._req[0].r.send(this._sid+",</stream:stream>");try{JSJaCCookie.read('JSJaC_State').erase();}catch(e){}this.oDbg.log("Disconnected: "+this._req[0].r.responseText,2);this._connected=false;this._handleEvent('ondisconnect');}JSJaCHttpPollingConnection.parseTree=function(s){try{var r=XmlDocument.create("body","foo");if(typeof(r.loadXML)!='undefined'){r.loadXML(s);return r.documentElement;}else if(window.DOMParser)return(new DOMParser()).parseFromString(s,"text/xml").documentElement;}catch(e){}return null;}var JSJACJID_FORBIDDEN=['"',' ','&','\'','/',':','<','>','@'];function JSJaCJID(jid){this._node='';this._domain='';this._resource='';if(typeof(jid)=='string'){if(jid.indexOf('@')!=-1){this.setNode(jid.substring(0,jid.indexOf('@')));jid=jid.substring(jid.indexOf('@')+1);}if(jid.indexOf('/')!=-1){this.setResource(jid.substring(jid.indexOf('/')+1));jid=jid.substring(0,jid.indexOf('/'));}this.setDomain(jid);}else{this.setNode(jid.node);this.setDomain(jid.domain);this.setResource(jid.resource);}}JSJaCJID.prototype.getNode=function(){return this._node;};JSJaCJID.prototype.getDomain=function(){return this._domain;};JSJaCJID.prototype.getResource=function(){return this._resource;};JSJaCJID.prototype.setNode=function(node){JSJaCJID._checkNodeName(node);this._node=node||'';return this;};JSJaCJID.prototype.setDomain=function(domain){if(!domain||domain=='')throw new JSJaCJIDInvalidException("domain name missing");JSJaCJID._checkNodeName(domain);this._domain=domain;return this;};JSJaCJID.prototype.setResource=function(resource){this._resource=resource||'';return this;};JSJaCJID.prototype.toString=function(){var jid='';if(this.getNode()&&this.getNode()!='')jid=this.getNode()+'@';jid+=this.getDomain();if(this.getResource()&&this.getResource()!="")jid+='/'+this.getResource();return jid;};JSJaCJID.prototype.removeResource=function(){return this.setResource();};JSJaCJID._checkNodeName=function(nodeprep){if(!nodeprep||nodeprep=='')return;for(var i=0;i<JSJACJID_FORBIDDEN.length;i++){if(nodeprep.indexOf(JSJACJID_FORBIDDEN[i])!=-1){throw new JSJaCJIDInvalidException("forbidden char in nodename: "+JSJACJID_FORBIDDEN[i]);}}};function JSJaCJIDInvalidException(message){this.message=message;this.name="JSJaCJIDInvalidException";}function JSJaCKeys(func,oDbg){var seed=Math.random();this._k=new Array();this._k[0]=seed.toString();if(oDbg)this.oDbg=oDbg;else{this.oDbg={};this.oDbg.log=function(){};}if(func){for(var i=1;i<JSJAC_NKEYS;i++){this._k[i]=func(this._k[i-1]);oDbg.log(i+": "+this._k[i],4);}}this._indexAt=JSJAC_NKEYS-1;this.getKey=function(){return this._k[this._indexAt--];};this.lastKey=function(){return(this._indexAt==0);};this.size=function(){return this._k.length;};this._getSuspendVars=function(){return('_k,_indexAt').split(',');}}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');elsethis.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;elsereturn null;};JSJaCPacket.prototype.setTo=function(to){if(!to||to=='')this.getNode().removeAttribute('to');else if(typeof(to)=='string')this.getNode().setAttribute('to',to);elsethis.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);elsethis.getNode().setAttribute('from',from.toString());return this;};JSJaCPacket.prototype.setID=function(id){if(!id||id=='')this.getNode().removeAttribute('id');elsethis.getNode().setAttribute('id',id);return this;};JSJaCPacket.prototype.setType=function(type){if(!type||type=='')this.getNode().removeAttribute('type');elsethis.getNode().setAttribute('type',type);ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -