⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsjac.js

📁 WEB IM 实现 j2ee项目的IM功能 可继承到现有的系统当中
💻 JS
📖 第 1 页 / 共 5 页
字号:
elseelement=doc.createElement(elementName);if(arguments[3])JSJaCBuilder._children(doc,element,arguments[3]);return element;},_text:function(doc,text){return doc.createTextNode(text);},_children:function(doc,element,children){if(typeof children=='object'){for(var i in children){if(children.hasOwnProperty(i)){var e=children[i];if(typeof e=='object'){if(e instanceof Array){var node=JSJaCBuilder.buildNode(doc,e[0],e[1],e[2]);element.appendChild(node);}else{element.appendChild(e);}}else{if(JSJaCBuilder._isStringOrNumber(e)){element.appendChild(JSJaCBuilder._text(doc,e));}}}}}else{if(JSJaCBuilder._isStringOrNumber(children)){element.appendChild(JSJaCBuilder._text(doc,children));}}},_attributes:function(attributes){var attrs=[];for(attribute in attributes)if(attributes.hasOwnProperty(attribute))attrs.push(attribute+'="'+attributes[attribute].toString().htmlEnc()+'"');return attrs.join(" ");},_isStringOrNumber:function(param){return(typeof param=='string'||typeof param=='number');}};if(typeof JSJaC=='undefined'){JSJaC={Version:'1.2'};}var NS_DISCO_ITEMS="http://jabber.org/protocol/disco#items";var NS_DISCO_INFO="http://jabber.org/protocol/disco#info";var NS_VCARD="vcard-temp";var NS_AUTH="jabber:iq:auth";var NS_AUTH_ERROR="jabber:iq:auth:error";var NS_REGISTER="jabber:iq:register";var NS_SEARCH="jabber:iq:search";var NS_ROSTER="jabber:iq:roster";var NS_PRIVACY="jabber:iq:privacy";var NS_PRIVATE="jabber:iq:private";var NS_VERSION="jabber:iq:version";var NS_TIME="jabber:iq:time";var NS_LAST="jabber:iq:last";var NS_XDATA="jabber:x:data";var NS_IQDATA="jabber:iq:data";var NS_DELAY="jabber:x:delay";var NS_EXPIRE="jabber:x:expire";var NS_EVENT="jabber:x:event";var NS_XCONFERENCE="jabber:x:conference";var NS_STATS="http://jabber.org/protocol/stats";var NS_MUC="http://jabber.org/protocol/muc";var NS_MUC_USER="http://jabber.org/protocol/muc#user";var NS_MUC_ADMIN="http://jabber.org/protocol/muc#admin";var NS_MUC_OWNER="http://jabber.org/protocol/muc#owner";var NS_PUBSUB="http://jabber.org/protocol/pubsub";var NS_PUBSUB_EVENT="http://jabber.org/protocol/pubsub#event";var NS_PUBSUB_OWNER="http://jabber.org/protocol/pubsub#owner";var NS_PUBSUB_NMI="http://jabber.org/protocol/pubsub#node-meta-info";var NS_COMMANDS="http://jabber.org/protocol/commands";var NS_STREAM="http://etherx.jabber.org/streams";var NS_STANZAS="urn:ietf:params:xml:ns:xmpp-stanzas";var NS_STREAMS="urn:ietf:params:xml:ns:xmpp-streams";var NS_TLS="urn:ietf:params:xml:ns:xmpp-tls";var NS_SASL="urn:ietf:params:xml:ns:xmpp-sasl";var NS_SESSION="urn:ietf:params:xml:ns:xmpp-session";var NS_BIND="urn:ietf:params:xml:ns:xmpp-bind";var NS_FEATURE_IQAUTH="http://jabber.org/features/iq-auth";var NS_FEATURE_IQREGISTER="http://jabber.org/features/iq-register";var NS_FEATURE_COMPRESS="http://jabber.org/features/compress";var NS_COMPRESS="http://jabber.org/protocol/compress";function STANZA_ERROR(code,type,cond){if(window==this)return new STANZA_ERROR(code,type,cond);this.code=code;this.type=type;this.cond=cond;}var ERR_BAD_REQUEST=STANZA_ERROR("400","modify","bad-request");var ERR_CONFLICT=STANZA_ERROR("409","cancel","conflict");var ERR_FEATURE_NOT_IMPLEMENTED=STANZA_ERROR("501","cancel","feature-not-implemented");var ERR_FORBIDDEN=STANZA_ERROR("403","auth","forbidden");var ERR_GONE=STANZA_ERROR("302","modify","gone");var ERR_INTERNAL_SERVER_ERROR=STANZA_ERROR("500","wait","internal-server-error");var ERR_ITEM_NOT_FOUND=STANZA_ERROR("404","cancel","item-not-found");var ERR_JID_MALFORMED=STANZA_ERROR("400","modify","jid-malformed");var ERR_NOT_ACCEPTABLE=STANZA_ERROR("406","modify","not-acceptable");var ERR_NOT_ALLOWED=STANZA_ERROR("405","cancel","not-allowed");var ERR_NOT_AUTHORIZED=STANZA_ERROR("401","auth","not-authorized");var ERR_PAYMENT_REQUIRED=STANZA_ERROR("402","auth","payment-required");var ERR_RECIPIENT_UNAVAILABLE=STANZA_ERROR("404","wait","recipient-unavailable");var ERR_REDIRECT=STANZA_ERROR("302","modify","redirect");var ERR_REGISTRATION_REQUIRED=STANZA_ERROR("407","auth","registration-required");var ERR_REMOTE_SERVER_NOT_FOUND=STANZA_ERROR("404","cancel","remote-server-not-found");var ERR_REMOTE_SERVER_TIMEOUT=STANZA_ERROR("504","wait","remote-server-timeout");var ERR_RESOURCE_CONSTRAINT=STANZA_ERROR("500","wait","resource-constraint");var ERR_SERVICE_UNAVAILABLE=STANZA_ERROR("503","cancel","service-unavailable");var ERR_SUBSCRIPTION_REQUIRED=STANZA_ERROR("407","auth","subscription-required");var ERR_UNEXPECTED_REQUEST=STANZA_ERROR("400","wait","unexpected-request");function JSJaCConnection(oArg){oCon=this;if(oArg&&oArg.oDbg&&oArg.oDbg.log)this.oDbg=oArg.oDbg;else{this.oDbg=new Object();this.oDbg.log=function(){};}if(oArg&&oArg.httpbase)this._httpbase=oArg.httpbase;if(oArg&&oArg.allow_plain)this.allow_plain=oArg.allow_plain;elsethis.allow_plain=JSJAC_ALLOW_PLAIN;this._connected=false;this._events=new Array();this._keys=null;this._ID=0;this._inQ=new Array();this._pQueue=new Array();this._regIDs=new Array();this._req=new Array();this._status='intialized';this._errcnt=0;this._inactivity=JSJAC_INACTIVITY;if(oArg&&oArg.timerval)this.setPollInterval(oArg.timerval);}JSJaCConnection.prototype.connected=function(){return this._connected;};JSJaCConnection.prototype.getPollInterval=function(){return this._timerval;};JSJaCConnection.prototype.registerHandler=function(event){event=event.toLowerCase();var eArg={handler:arguments[arguments.length-1],childName:'*',childNS:'*',type:'*'};if(arguments.length>2)eArg.childName=arguments[1];if(arguments.length>3)eArg.childNS=arguments[2];if(arguments.length>4)eArg.type=arguments[3];if(!this._events[event])this._events[event]=new Array(eArg);elsethis._events[event]=this._events[event].concat(eArg);this._events[event]=this._events[event].sort(function(a,b){with(a){if(type=='*'){if(b.type=='*')return 0;return 1;}if(childNS=='*'){if(b.childNS=='*')return 0;return 1;}if(childName=='*'){if(b.childName=='*')return 0;return 1;}}return-1;});this.oDbg.log("registered handler for event '"+event+"'",2);};JSJaCConnection.prototype.registerIQSet=function(childName,childNS,handler){this.registerHandler('iq',childName,childNS,'set',handler);};JSJaCConnection.prototype.registerIQGet=function(childName,childNS,handler){this.registerHandler('iq',childName,childNS,'get',handler);};JSJaCConnection.prototype.resume=function(){try{this._setStatus('resuming');var s=unescape(JSJaCCookie.read('JSJaC_State').getValue());this.oDbg.log('read cookie: '+s,2);var o=s.parseJSON();for(var i in o)if(o.hasOwnProperty(i))this[i]=o[i];if(this._keys){this._keys2=new JSJaCKeys();var u=this._keys2._getSuspendVars();for(var i=0;i<u.length;i++)this._keys2[u[i]]=this._keys[u[i]];this._keys=this._keys2;}try{JSJaCCookie.read('JSJaC_State').erase();}catch(e){}if(this._connected){this._handleEvent('onresume');setTimeout("oCon._resume()",this.getPollInterval());}return this._connected;}catch(e){if(e.message)this.oDbg.log("Resume failed: "+e.message,1);elsethis.oDbg.log("Resume failed: "+e,1);return false;}};JSJaCConnection.prototype.send=function(packet,cb,arg){if(!packet||!packet.pType){this.oDbg.log("no packet: "+packet,1);return false;}if(cb){if(!packet.getID())packet.setID('JSJaCID_'+this._ID++);this._registerPID(packet.getID(),cb,arg);}try{this._handleEvent(packet.pType()+'_out',packet);this._handleEvent("packet_out",packet);this._pQueue=this._pQueue.concat(packet.xml());}catch(e){this.oDbg.log(e.toString(),1);return false;}return true;};JSJaCConnection.prototype.sendIQ=function(iq,handlers,arg){handlers=handlers||{};var error_handler=handlers.error_handler||function(aIq){oCon.oDbg.log(iq.xml(),1);};var result_handler=handlers.result_handler||function(aIq){oCon.oDbg.log(aIq.xml(),2);};var default_handler=handlers.default_handler||function(aIq){oCon.oDbg.log(aIq.xml(),2);}var iqHandler=function(aIq,arg){switch(aIq.getType()){case'error':error_handler(aIq);break;case'result':result_handler(aIq,arg);break;default:default_handler(aIq,arg);}};this.send(iq,iqHandler,arg);};JSJaCConnection.prototype.setPollInterval=function(timerval){if(!timerval||isNaN(timerval)){this.oDbg.log("Invalid timerval: "+timerval,1);throw"Invalid interval";}this._timerval=timerval;return this._timerval;};JSJaCConnection.prototype.status=function(){return this._status;};JSJaCConnection.prototype.suspend=function(){clearTimeout(this._timeout);clearInterval(this._interval);clearInterval(this._inQto);this._suspend();var u=('_connected,_keys,_ID,_inQ,_pQueue,_regIDs,_errcnt,_inactivity,domain,username,resource,jid,fulljid,_sid,_httpbase,_timerval,_is_polling').split(',');u=u.concat(this._getSuspendVars());var s=new Object();for(var i=0;i<u.length;i++){if(!this[u[i]])continue;if(this[u[i]]._getSuspendVars){var uo=this[u[i]]._getSuspendVars();var o=new Object();for(var j=0;j<uo.length;j++)o[uo[j]]=this[u[i]][uo[j]];}elsevar o=this[u[i]];s[u[i]]=o;}var c=new JSJaCCookie('JSJaC_State',escape(s.toJSONString()),this._inactivity);this.oDbg.log("writing cookie: "+unescape(c.value)+"\n(length:"+unescape(c.value).length+")",2);c.write();try{var c2=JSJaCCookie.read('JSJaC_State');if(c.value!=c2.value){this.oDbg.log("Suspend failed writing cookie.\nRead: "+unescape(JSJaCCookie.read('JSJaC_State')),1);c.erase();}this._connected=false;this._setStatus('suspending');}catch(e){this.oDbg.log("Failed reading cookie 'JSJaC_State': "+e.message);}};JSJaCConnection.prototype._abort=function(){clearTimeout(this._timeout);this._connected=false;this._setStatus('aborted');this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');this._handleEvent('onerror',JSJaCError('500','cancel','service-unavailable'));};JSJaCConnection.prototype._checkInQ=function(){for(var i=0;i<this._inQ.length&&i<10;i++){var item=this._inQ[0];this._inQ=this._inQ.slice(1,this._inQ.length);var packet=JSJaCPacket.wrapNode(item);if(!packet)return;this._handleEvent("packet_in",packet);if(packet.pType&&!this._handlePID(packet)){this._handleEvent(packet.pType()+'_in',packet);this._handleEvent(packet.pType(),packet);}}};JSJaCConnection.prototype._checkQueue=function(){if(this._pQueue.length!=0)this._process();return true;};JSJaCConnection.prototype._doAuth=function(){if(this.has_sasl&&this.authtype=='nonsasl')this.oDbg.log("Warning: SASL present but not used",1);if(!this._doSASLAuth()&&!this._doLegacyAuth()){this.oDbg.log("Auth failed for authtype "+this.authtype,1);this.disconnect();return false;}return true;};JSJaCConnection.prototype._doInBandReg=function(){if(this.authtype=='saslanon'||this.authtype=='anonymous')return;var iq=new JSJaCIQ();iq.setType('set');iq.setID('reg1');iq.appendNode("query",{xmlns:"jabber:iq:register"},[["username",this.username],["password",this.pass]]);this.send(iq,this._doInBandRegDone);};JSJaCConnection.prototype._doInBandRegDone=function(iq){if(iq&&iq.getType()=='error'){oCon.oDbg.log("registration failed for "+oCon.username,0);oCon._handleEvent('onerror',iq.getChild('error'));return;}oCon.oDbg.log(oCon.username+" registered succesfully",0);oCon._doAuth();};JSJaCConnection.prototype._doLegacyAuth=function(){if(this.authtype!='nonsasl'&&this.authtype!='anonymous')return false;var iq=new JSJaCIQ();iq.setIQ(oCon.server,'get','auth1');iq.appendNode('query',{xmlns:'jabber:iq:auth'},[['username',oCon.username]]);this.send(iq,this._doLegacyAuth2);return true;};JSJaCConnection.prototype._doLegacyAuth2=function(iq){if(!iq||iq.getType()!='result'){if(iq&&iq.getType()=='error')oCon._handleEvent('onerror',iq.getChild('error'));oCon.disconnect();return;}var use_digest=(iq.getChild('digest')!=null);var iq=new JSJaCIQ();iq.setIQ(oCon.server,'set','auth2');query=iq.appendNode('query',{xmlns:'jabber:iq:auth'},[['username',oCon.username],['resource',oCon.resource]]);if(use_digest){query.appendChild(iq.buildNode('digest',hex_sha1(oCon.streamid+oCon.pass)));}else if(oCon.allow_plain){query.appendChild(iq.buildNode('password',oCon.pass));}else{oCon.oDbg.log("no valid login mechanism found",1);oCon.disconnect();return false;}oCon.send(iq,oCon._doLegacyAuthDone);};JSJaCConnection.prototype._doLegacyAuthDone=function(iq){if(iq.getType()!='result'){if(iq.getType()=='error')oCon._handleEvent('onerror',iq.getChild('error'));oCon.disconnect();}elseoCon._handleEvent('onconnect');};JSJaCConnection.prototype._sendRaw=function(xml,cb,arg){var slot=this._getFreeSlot();this._req[slot]=this._setupRequest(true);this._req[slot].r.onreadystatechange=function(){if(typeof(oCon)=='undefined'||!oCon||!oCon.connected())return;if(oCon._req[slot].r.readyState==4){oCon.oDbg.log("async recv: "+oCon._req[slot].r.responseText,4);if(typeof(cb)!='undefined')eval("oCon."+cb+"(oCon._req[slot],"+arg+")");}}if(typeof(this._req[slot].r.onerror)!='undefined'){this._req[slot].r.onerror=function(e){if(typeof(oCon)=='undefined'||!oCon||!oCon.connected())return;oCon.oDbg.log('XmlHttpRequest error',1);return false;}}var reqstr=this._getRequestString(xml);this.oDbg.log("sending: "+reqstr,4);this._req[slot].r.send(reqstr);return true;};JSJaCConnection.prototype._doSASLAuth=function(){if(this.authtype=='nonsasl'||this.authtype=='anonymous')return false;if(this.authtype=='saslanon'){if(this.mechs['ANONYMOUS']){this.oDbg.log("SASL using mechanism 'ANONYMOUS'",2);return this._sendRaw("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>",'_doSASLAuthDone');}this.oDbg.log("SASL ANONYMOUS requested but not supported",1);}else{if(this.mechs['DIGEST-MD5']){this.oDbg.log("SASL using mechanism 'DIGEST-MD5'",2);return this._sendRaw("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>",'_doSASLAuthDigestMd5S1');}else if(this.allow_plain&&this.mechs['PLAIN']){this.oDbg.log("SASL using mechanism 'PLAIN'",2);var authStr=this.username+'@'+this.domain+String.fromCharCode(0)+this.username+String.fromCharCode(0)+this.pass;this.oDbg.log("authenticating with '"+authStr+"'",2);authStr=btoa(authStr);return this._sendRaw("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>"+authStr+"</auth>",'_doSASLAuthDone');}this.oDbg.log("No SASL mechanism applied",1);this.authtype='nonsasl';}return false;};JSJaCConnection.prototype._doSASLAuthDigestMd5S1=function(req){this.oDbg.log(req.r.responseText,2);var doc=oCon._prepareResponse(req);if(!doc||doc.getElementsByTagName("challenge").length==0){this.oDbg.log("challenge missing",1);oCon._handleEvent('onerror',JSJaCError('401','auth','not-authorized'));this.disconnect();}else{var challenge=atob(doc.getElementsByTagName("challenge").item(0).firstChild.nodeValue);this.oDbg.log("got challenge: "+challenge,2);this._nonce=challenge.substring(challenge.indexOf("nonce=")+7);this._nonce=this._nonce.substring(0,this._nonce.indexOf("\""));this.oDbg.log("nonce: "+this._nonce,2);if(this._nonce==''||this._nonce.indexOf('\"')!=-1){this.oDbg.log("nonce not valid, aborting",1);this.disconnect();return;}this._digest_uri="xmpp/";this._digest_uri+=this.domain;this._cnonce=cnonce(14);this._nc='00000001';var A1=str_md5(this.username+':'+this.domain+':'+this.pass)+':'+this._nonce+':'+this._cnonce;var A2='AUTHENTICATE:'+this._digest_uri;var response=hex_md5(hex_md5(A1)+':'+this._nonce+':'+this._nc+':'+this._cnonce+':auth:'+hex_md5(A2));var rPlain='username="'+this.username+'",realm="'+this.domain+'",nonce="'+this._nonce+'",cnonce="'+this._cnonce+'",nc="'+this._nc+'",qop=auth,digest-uri="'+this._digest_uri+'",response="'+response+'",charset=utf-8';this.oDbg.log("response: "+rPlain,2);this._sendRaw("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"+binb2b64(str2binb(rPlain))+"</response>",'_doSASLAuthDigestMd5S2');}};JSJaCConnection.prototype._doSASLAuthDigestMd5S2=function(req){this.oDbg.log(req.r.responseText,2);var doc=this._prepareResponse(req);if(doc.firstChild.nodeName=='failure'){if(doc.firstChild.xml)this.oDbg.log("auth error: "+doc.firstChild.xml,1);elsethis.oDbg.log("auth error",1);oCon._handleEvent('onerror',JSJaCError('401','auth','not-authorized'));this.disconnect();return;}var response=atob(doc.firstChild.firstChild.nodeValue)this.oDbg.log("response: "+response,2);var rspauth=response.substring(response.indexOf("rspauth=")+8);this.oDbg.log("rspauth: "+rspauth,2);var A1=str_md5(this.username+':'+this.domain+':'+this.pass)+':'+this._nonce+':'+this._cnonce;var A2=':'+this._digest_uri;var rsptest=hex_md5(hex_md5(A1)+':'+this._nonce+':'+this._nc+':'+this._cnonce+':auth:'+hex_md5(A2));this.oDbg.log("rsptest: "+rsptest,2);if(rsptest!=rspauth){this.oDbg.log("SASL Digest-MD5: server repsonse with wrong rspauth",1);this.disconnect();return;}if(doc.firstChild.nodeName=='success')this._reInitStream(this.domain,'_doStreamBind');elsethis._sendRaw("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>",'_doSASLAuthDone');};JSJaCConnection.prototype._doSASLAuthDone=function(req){var doc=this._prepareResponse(req);if(doc.firstChild.nodeName!='success'){this.oDbg.log("auth failed",1);this.disconnect();}elsethis._reInitStream(this.domain,'_doStreamBind');};JSJaCConnection.prototype._doStreamBind=function(){var iq=new JSJaCIQ();iq.setIQ(this.domain,'set','bind_1');iq.appendNode("bind",{xmlns:"urn:ietf:params:xml:ns:xmpp-bind"},[["resource",this.resource]]);this.oDbg.log(iq.xml());this.send(iq,this._doXMPPSess);};JSJaCConnection.prototype._doXMPPSess=function(iq){if(iq.getType()!='result'||iq.getType()=='error'){oCon.disconnect();if(iq.getType()=='error')oCon._handleEvent('onerror',iq.getChild('error'));return;}oCon.fulljid=iq.getChildVal("jid");oCon.jid=oCon.fulljid.substring(0,oCon.fulljid.lastIndexOf('/'));iq=new JSJaCIQ();iq.setIQ(this.domain,'set','sess_1');iq.appendNode("session",{xmlns:"urn:ietf:params:xml:ns:xmpp-session"},[]);oCon.oDbg.log(iq.xml());oCon.send(iq,oCon._doXMPPSessDone);};JSJaCConnection.prototype._doXMPPSessDone=function(iq){if(iq.getType()!='result'||iq.getType()=='error'){oCon.disconnect();if(iq.getType()=='error')oCon._handleEvent('onerror',iq.getChild('error'));return;}elseoCon._handleEvent('onconnect');};JSJaCConnection.prototype._handleEvent=function(event,arg){event=event.toLowerCase();this.oDbg.log("incoming event '"+event+"'",3);if(!this._events[event])return;this.oDbg.log("handling event '"+event+"'",2);for(var i=0;i<this._events[event].length;i++){var aEvent=this._events[event][i];if(aEvent.handler){try{if(arg){if(arg.pType){if(!arg.getChild(aEvent.childName,aEvent.childNS))continue;if(aEvent.type!='*'&&arg.getType()!=aEvent.type)continue;this.oDbg.log(aEvent.childName+"/"+aEvent.childNS+"/"+aEvent.type+" => match for handler "+aEvent.handler,3);}if(aEvent.handler(arg))break;}elseif(aEvent.handler())break;}catch(e){this.oDbg.log(aEvent.handler+"\n>>>"+e.name+": "+e.message,1);}}}};JSJaCConnection.prototype._handlePID=function(aJSJaCPacket){if(!aJSJaCPacket.getID())return false;for(var i in this._regIDs){if(this._regIDs.hasOwnProperty(i)&&this._regIDs[i]&&i==aJSJaCPacket.getID()){var pID=aJSJaCPacket.getID();this.oDbg.log("handling "+pID,3);try{this._regIDs[i].cb(aJSJaCPacket,this._regIDs[i].arg);}catch(e){this.oDbg.log(e.name+": "+e.message);}this._unregisterPID(pID);return true;}}return false;};JSJaCConnection.prototype._handleResponse=function(req){var rootEl=this._prepareResponse(req);if(!rootEl)return null;this.oDbg.log("childNodes: "+rootEl.childNodes.length,3);for(var i=0;i<rootEl.childNodes.length;i++){this.oDbg.log("rootEl.childNodes.item("+i+").nodeName: "+rootEl.childNodes.item(i).nodeName,3);this._inQ=this._inQ.concat(rootEl.childNodes.item(i));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -