root / trunk / web / dojo / dojox / xmpp / PresenceService.js @ 12
History | View | Annotate | Download (6.26 KB)
| 1 | 9 | andrej.cim | /*
 | 
|---|---|---|---|
| 2 |         Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
 | ||
| 3 |         Available via Academic Free License >= 2.1 OR the modified BSD license.
 | ||
| 4 |         see: http://dojotoolkit.org/license for details
 | ||
| 5 | */
 | ||
| 6 | |||
| 7 | |||
| 8 | if(!dojo._hasResource["dojox.xmpp.PresenceService"]){ | ||
| 9 | dojo._hasResource["dojox.xmpp.PresenceService"]=true; | ||
| 10 | dojo.provide("dojox.xmpp.PresenceService");
 | ||
| 11 | dojox.xmpp.presence={UPDATE:201,SUBSCRIPTION_REQUEST:202,SUBSCRIPTION_SUBSTATUS_NONE:204,SUBSCRIPTION_NONE:"none",SUBSCRIPTION_FROM:"from",SUBSCRIPTION_TO:"to",SUBSCRIPTION_BOTH:"both",SUBSCRIPTION_REQUEST_PENDING:"pending",STATUS_ONLINE:"online",STATUS_AWAY:"away",STATUS_CHAT:"chat",STATUS_DND:"dnd",STATUS_EXTENDED_AWAY:"xa",STATUS_OFFLINE:"offline",STATUS_INVISIBLE:"invisible"};
 | ||
| 12 | dojo.declare("dojox.xmpp.PresenceService",null,{constructor:function(_1){ | ||
| 13 | this.session=_1;
 | ||
| 14 | this.isInvisible=false; | ||
| 15 | this.avatarHash=null; | ||
| 16 | this.presence=null; | ||
| 17 | this.restrictedContactjids={};
 | ||
| 18 | },publish:function(_2){ | ||
| 19 | this.presence=_2;
 | ||
| 20 | this._setPresence();
 | ||
| 21 | },sendAvatarHash:function(_3){ | ||
| 22 | this.avatarHash=_3;
 | ||
| 23 | this._setPresence();
 | ||
| 24 | },_setPresence:function(){ | ||
| 25 | var _4=this.presence; | ||
| 26 | var p={xmlns:"jabber:client"}; | ||
| 27 | if(_4&&_4.to){
 | ||
| 28 | p.to=_4.to; | ||
| 29 | } | ||
| 30 | if(_4.show&&_4.show==dojox.xmpp.presence.STATUS_OFFLINE){
 | ||
| 31 | p.type="unavailable";
 | ||
| 32 | } | ||
| 33 | if(_4.show&&_4.show==dojox.xmpp.presence.STATUS_INVISIBLE){
 | ||
| 34 | this._setInvisible();
 | ||
| 35 | this.isInvisible=true; | ||
| 36 | return;
 | ||
| 37 | } | ||
| 38 | if(this.isInvisible){ | ||
| 39 | this._setVisible();
 | ||
| 40 | } | ||
| 41 | var _5=new dojox.string.Builder(dojox.xmpp.util.createElement("presence",p,false)); | ||
| 42 | if(_4.show&&_4.show!=dojox.xmpp.presence.STATUS_OFFLINE){
 | ||
| 43 | _5.append(dojox.xmpp.util.createElement("show",{},false)); | ||
| 44 | _5.append(_4.show); | ||
| 45 | _5.append("</show>");
 | ||
| 46 | } | ||
| 47 | if(_4.status){
 | ||
| 48 | _5.append(dojox.xmpp.util.createElement("status",{},false)); | ||
| 49 | _5.append(_4.status); | ||
| 50 | _5.append("</status>");
 | ||
| 51 | } | ||
| 52 | if(this.avatarHash){ | ||
| 53 | _5.append(dojox.xmpp.util.createElement("x",{xmlns:"vcard-temp:x:update"},false)); | ||
| 54 | _5.append(dojox.xmpp.util.createElement("photo",{},false)); | ||
| 55 | _5.append(this.avatarHash);
 | ||
| 56 | _5.append("</photo>");
 | ||
| 57 | _5.append("</x>");
 | ||
| 58 | } | ||
| 59 | if(_4.priority&&_4.show!=dojox.xmpp.presence.STATUS_OFFLINE){
 | ||
| 60 | if(_4.priority>127||_4.priority<-128){ | ||
| 61 | _4.priority=5;
 | ||
| 62 | } | ||
| 63 | _5.append(dojox.xmpp.util.createElement("priority",{},false)); | ||
| 64 | _5.append(_4.priority); | ||
| 65 | _5.append("</priority>");
 | ||
| 66 | } | ||
| 67 | _5.append("</presence>");
 | ||
| 68 | this.session.dispatchPacket(_5.toString());
 | ||
| 69 | },toggleBlockContact:function(_6){ | ||
| 70 | if(!this.restrictedContactjids[_6]){ | ||
| 71 | this.restrictedContactjids[_6]=this._createRestrictedJid(); | ||
| 72 | } | ||
| 73 | this.restrictedContactjids[_6].blocked=!this.restrictedContactjids[_6].blocked; | ||
| 74 | this._updateRestricted();
 | ||
| 75 | return this.restrictedContactjids; | ||
| 76 | },toggleContactInvisiblity:function(_7){ | ||
| 77 | if(!this.restrictedContactjids[_7]){ | ||
| 78 | this.restrictedContactjids[_7]=this._createRestrictedJid(); | ||
| 79 | } | ||
| 80 | this.restrictedContactjids[_7].invisible=!this.restrictedContactjids[_7].invisible; | ||
| 81 | this._updateRestricted();
 | ||
| 82 | return this.restrictedContactjids; | ||
| 83 | },_createRestrictedJid:function(){ | ||
| 84 | return {invisible:false,blocked:false}; | ||
| 85 | },_updateRestricted:function(){ | ||
| 86 | var _8={id:this.session.getNextIqId(),from:this.session.jid+"/"+this.session.resource,type:"set"}; | ||
| 87 | var _9=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_8,false)); | ||
| 88 | _9.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:privacy"},false)); | ||
| 89 | _9.append(dojox.xmpp.util.createElement("list",{name:"iwcRestrictedContacts"},false)); | ||
| 90 | var _a=1; | ||
| 91 | for(var _b in this.restrictedContactjids){ | ||
| 92 | var _c=this.restrictedContactjids[_b]; | ||
| 93 | if(_c.blocked||_c.invisible){
 | ||
| 94 | _9.append(dojox.xmpp.util.createElement("item",{value:dojox.xmpp.util.encodeJid(_b),action:"deny",order:_a++},false)); | ||
| 95 | if(_c.blocked){
 | ||
| 96 | _9.append(dojox.xmpp.util.createElement("message",{},true)); | ||
| 97 | } | ||
| 98 | if(_c.invisible){
 | ||
| 99 | _9.append(dojox.xmpp.util.createElement("presence-out",{},true)); | ||
| 100 | } | ||
| 101 | _9.append("</item>");
 | ||
| 102 | }else{
 | ||
| 103 | delete this.restrictedContactjids[_b]; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | _9.append("</list>");
 | ||
| 107 | _9.append("</query>");
 | ||
| 108 | _9.append("</iq>");
 | ||
| 109 | var _d=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_8,false)); | ||
| 110 | _d.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:privacy"},false)); | ||
| 111 | _d.append(dojox.xmpp.util.createElement("active",{name:"iwcRestrictedContacts"},true)); | ||
| 112 | _d.append("</query>");
 | ||
| 113 | _d.append("</iq>");
 | ||
| 114 | this.session.dispatchPacket(_9.toString());
 | ||
| 115 | this.session.dispatchPacket(_d.toString());
 | ||
| 116 | },_setVisible:function(){ | ||
| 117 | var _e={id:this.session.getNextIqId(),from:this.session.jid+"/"+this.session.resource,type:"set"}; | ||
| 118 | var _f=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_e,false)); | ||
| 119 | _f.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:privacy"},false)); | ||
| 120 | _f.append(dojox.xmpp.util.createElement("active",{},true)); | ||
| 121 | _f.append("</query>");
 | ||
| 122 | _f.append("</iq>");
 | ||
| 123 | this.session.dispatchPacket(_f.toString());
 | ||
| 124 | },_setInvisible:function(){ | ||
| 125 | var _10={id:this.session.getNextIqId(),from:this.session.jid+"/"+this.session.resource,type:"set"}; | ||
| 126 | var req=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_10,false)); | ||
| 127 | req.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:privacy"},false)); | ||
| 128 | req.append(dojox.xmpp.util.createElement("list",{name:"invisible"},false)); | ||
| 129 | req.append(dojox.xmpp.util.createElement("item",{action:"deny",order:"1"},false)); | ||
| 130 | req.append(dojox.xmpp.util.createElement("presence-out",{},true)); | ||
| 131 | req.append("</item>");
 | ||
| 132 | req.append("</list>");
 | ||
| 133 | req.append("</query>");
 | ||
| 134 | req.append("</iq>");
 | ||
| 135 | _10={id:this.session.getNextIqId(),from:this.session.jid+"/"+this.session.resource,type:"set"};
 | ||
| 136 | var _11=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_10,false)); | ||
| 137 | _11.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:privacy"},false)); | ||
| 138 | _11.append(dojox.xmpp.util.createElement("active",{name:"invisible"},true)); | ||
| 139 | _11.append("</query>");
 | ||
| 140 | _11.append("</iq>");
 | ||
| 141 | this.session.dispatchPacket(req.toString());
 | ||
| 142 | this.session.dispatchPacket(_11.toString());
 | ||
| 143 | },_manageSubscriptions:function(_12,_13){ | ||
| 144 | if(!_12){
 | ||
| 145 | return;
 | ||
| 146 | } | ||
| 147 | if(_12.indexOf("@")==-1){ | ||
| 148 | _12+="@"+this.session.domain; | ||
| 149 | } | ||
| 150 | var req=dojox.xmpp.util.createElement("presence",{to:_12,type:_13},true); | ||
| 151 | this.session.dispatchPacket(req);
 | ||
| 152 | },subscribe:function(_14){ | ||
| 153 | this._manageSubscriptions(_14,"subscribe"); | ||
| 154 | },approveSubscription:function(_15){ | ||
| 155 | this._manageSubscriptions(_15,"subscribed"); | ||
| 156 | },unsubscribe:function(_16){ | ||
| 157 | this._manageSubscriptions(_16,"unsubscribe"); | ||
| 158 | },declineSubscription:function(_17){ | ||
| 159 | this._manageSubscriptions(_17,"unsubscribed"); | ||
| 160 | },cancelSubscription:function(_18){ | ||
| 161 | this._manageSubscriptions(_18,"unsubscribed"); | ||
| 162 | }}); | ||
| 163 | } |