| 1 |
dojo.provide("dojox.xmpp.PresenceService"); |
| 2 |
|
| 3 |
dojox.xmpp.presence = { |
| 4 |
UPDATE: 201, |
| 5 |
SUBSCRIPTION_REQUEST: 202, |
| 6 |
|
| 7 |
|
| 8 |
SUBSCRIPTION_SUBSTATUS_NONE: 204, |
| 9 |
|
| 10 |
SUBSCRIPTION_NONE: 'none', |
| 11 |
SUBSCRIPTION_FROM: 'from', |
| 12 |
SUBSCRIPTION_TO: 'to', |
| 13 |
SUBSCRIPTION_BOTH: 'both', |
| 14 |
SUBSCRIPTION_REQUEST_PENDING: 'pending', |
| 15 |
|
| 16 |
STATUS_ONLINE: 'online', |
| 17 |
STATUS_AWAY: 'away', |
| 18 |
STATUS_CHAT: 'chat', |
| 19 |
STATUS_DND: 'dnd', |
| 20 |
STATUS_EXTENDED_AWAY: 'xa', |
| 21 |
STATUS_OFFLINE: 'offline', |
| 22 |
|
| 23 |
STATUS_INVISIBLE: 'invisible' |
| 24 |
}; |
| 25 |
|
| 26 |
dojo.declare("dojox.xmpp.PresenceService", null, { |
| 27 |
constructor: function(xmppService){ |
| 28 |
this.session= xmppService; |
| 29 |
this.isInvisible = false; |
| 30 |
this.avatarHash = null; |
| 31 |
this.presence = null; |
| 32 |
this.restrictedContactjids = {}; |
| 33 |
}, |
| 34 |
|
| 35 |
publish: function(presence){ |
| 36 |
|
| 37 |
this.presence = presence; |
| 38 |
this._setPresence(); |
| 39 |
}, |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
sendAvatarHash: function(avatarHash) { |
| 58 |
this.avatarHash = avatarHash; |
| 59 |
this._setPresence(); |
| 60 |
}, |
| 61 |
|
| 62 |
|
| 63 |
_setPresence: function() { |
| 64 |
var presence = this.presence; |
| 65 |
var p = {xmlns: 'jabber:client'}; |
| 66 |
|
| 67 |
if (presence && presence.to){ |
| 68 |
p.to = presence.to; |
| 69 |
} |
| 70 |
|
| 71 |
if (presence.show && presence.show==dojox.xmpp.presence.STATUS_OFFLINE){ |
| 72 |
p.type = 'unavailable'; |
| 73 |
} |
| 74 |
|
| 75 |
if (presence.show && presence.show==dojox.xmpp.presence.STATUS_INVISIBLE) { |
| 76 |
this._setInvisible(); |
| 77 |
this.isInvisible = true; |
| 78 |
return; |
| 79 |
}; |
| 80 |
|
| 81 |
if(this.isInvisible) { |
| 82 |
|
| 83 |
this._setVisible(); |
| 84 |
} |
| 85 |
|
| 86 |
var req = new dojox.string.Builder(dojox.xmpp.util.createElement("presence",p, false)); |
| 87 |
|
| 88 |
if (presence.show && presence.show!=dojox.xmpp.presence.STATUS_OFFLINE ) { |
| 89 |
req.append(dojox.xmpp.util.createElement("show",{},false)); |
| 90 |
req.append(presence.show); |
| 91 |
req.append("</show>"); |
| 92 |
} |
| 93 |
|
| 94 |
if(presence.status) { |
| 95 |
req.append(dojox.xmpp.util.createElement("status",{},false)); |
| 96 |
req.append(presence.status); |
| 97 |
req.append("</status>"); |
| 98 |
} |
| 99 |
|
| 100 |
if(this.avatarHash) { |
| 101 |
req.append(dojox.xmpp.util.createElement("x",{xmlns: 'vcard-temp:x:update'},false)); |
| 102 |
req.append(dojox.xmpp.util.createElement("photo",{},false)); |
| 103 |
req.append(this.avatarHash); |
| 104 |
req.append("</photo>"); |
| 105 |
req.append("</x>"); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
if (presence.priority && presence.show!=dojox.xmpp.presence.STATUS_OFFLINE){ |
| 110 |
if(presence.priority > 127 || presence.priority < -128){ |
| 111 |
presence.priority = 5; |
| 112 |
} |
| 113 |
req.append(dojox.xmpp.util.createElement("priority",{},false)); |
| 114 |
req.append(presence.priority); |
| 115 |
req.append("</priority>"); |
| 116 |
} |
| 117 |
|
| 118 |
req.append("</presence>"); |
| 119 |
this.session.dispatchPacket(req.toString()); |
| 120 |
}, |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
toggleBlockContact: function(jid) { |
| 150 |
if(!this.restrictedContactjids[jid]) { |
| 151 |
this.restrictedContactjids[jid] = this._createRestrictedJid(); |
| 152 |
} |
| 153 |
|
| 154 |
this.restrictedContactjids[jid].blocked = !this.restrictedContactjids[jid].blocked; |
| 155 |
|
| 156 |
this._updateRestricted(); |
| 157 |
return this.restrictedContactjids; |
| 158 |
}, |
| 159 |
|
| 160 |
|
| 161 |
toggleContactInvisiblity: function(jid) { |
| 162 |
if(!this.restrictedContactjids[jid]) { |
| 163 |
this.restrictedContactjids[jid] = this._createRestrictedJid(); |
| 164 |
} |
| 165 |
|
| 166 |
this.restrictedContactjids[jid].invisible = !this.restrictedContactjids[jid].invisible; |
| 167 |
|
| 168 |
this._updateRestricted(); |
| 169 |
return this.restrictedContactjids; |
| 170 |
}, |
| 171 |
|
| 172 |
_createRestrictedJid: function() { |
| 173 |
return {invisible: false, blocked:false}; |
| 174 |
}, |
| 175 |
|
| 176 |
_updateRestricted: function() { |
| 177 |
|
| 178 |
var props={ |
| 179 |
id: this.session.getNextIqId(), |
| 180 |
from: this.session.jid + "/" + this.session.resource, |
| 181 |
type: "set" |
| 182 |
}; |
| 183 |
|
| 184 |
var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
| 185 |
req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false)); |
| 186 |
req.append(dojox.xmpp.util.createElement("list",{name: "iwcRestrictedContacts"},false)) |
| 187 |
var count = 1; |
| 188 |
for(var jid in this.restrictedContactjids) { |
| 189 |
var item = this.restrictedContactjids[jid]; |
| 190 |
|
| 191 |
if(item.blocked || item.invisible) { |
| 192 |
req.append(dojox.xmpp.util.createElement("item",{value: dojox.xmpp.util.encodeJid(jid), action: "deny", order: count++},false)); |
| 193 |
if(item.blocked) { |
| 194 |
req.append(dojox.xmpp.util.createElement("message",{},true)); |
| 195 |
} |
| 196 |
if(item.invisible) { |
| 197 |
req.append(dojox.xmpp.util.createElement("presence-out",{},true)); |
| 198 |
} |
| 199 |
req.append("</item>"); |
| 200 |
} else { |
| 201 |
delete this.restrictedContactjids[jid]; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
} |
| 207 |
req.append("</list>"); |
| 208 |
req.append("</query>"); |
| 209 |
req.append("</iq>"); |
| 210 |
|
| 211 |
|
| 212 |
var req2 = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
| 213 |
req2.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false)); |
| 214 |
req2.append(dojox.xmpp.util.createElement("active",{name:"iwcRestrictedContacts"},true)); |
| 215 |
req2.append("</query>"); |
| 216 |
req2.append("</iq>"); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
this.session.dispatchPacket(req.toString()); |
| 222 |
this.session.dispatchPacket(req2.toString()); |
| 223 |
}, |
| 224 |
|
| 225 |
_setVisible: function() { |
| 226 |
var props={ |
| 227 |
id: this.session.getNextIqId(), |
| 228 |
from: this.session.jid + "/" + this.session.resource, |
| 229 |
type: "set" |
| 230 |
}; |
| 231 |
var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
| 232 |
req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false)); |
| 233 |
req.append(dojox.xmpp.util.createElement("active",{},true)); |
| 234 |
req.append("</query>"); |
| 235 |
req.append("</iq>"); |
| 236 |
|
| 237 |
this.session.dispatchPacket(req.toString()); |
| 238 |
}, |
| 239 |
|
| 240 |
_setInvisible: function() { |
| 241 |
|
| 242 |
var props={ |
| 243 |
id: this.session.getNextIqId(), |
| 244 |
from: this.session.jid + "/" + this.session.resource, |
| 245 |
type: "set" |
| 246 |
}; |
| 247 |
var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
| 248 |
req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false)); |
| 249 |
req.append(dojox.xmpp.util.createElement("list",{name: "invisible"},false)) |
| 250 |
req.append(dojox.xmpp.util.createElement("item",{action: "deny", order: "1"},false)) |
| 251 |
req.append(dojox.xmpp.util.createElement("presence-out",{},true)); |
| 252 |
req.append("</item>"); |
| 253 |
req.append("</list>"); |
| 254 |
req.append("</query>"); |
| 255 |
req.append("</iq>"); |
| 256 |
|
| 257 |
|
| 258 |
props={ |
| 259 |
id: this.session.getNextIqId(), |
| 260 |
from: this.session.jid + "/" + this.session.resource, |
| 261 |
type: "set" |
| 262 |
}; |
| 263 |
|
| 264 |
var req2 = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
| 265 |
req2.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false)); |
| 266 |
req2.append(dojox.xmpp.util.createElement("active",{name:"invisible"},true)); |
| 267 |
req2.append("</query>"); |
| 268 |
req2.append("</iq>"); |
| 269 |
|
| 270 |
|
| 271 |
this.session.dispatchPacket(req.toString()); |
| 272 |
this.session.dispatchPacket(req2.toString()); |
| 273 |
}, |
| 274 |
|
| 275 |
_manageSubscriptions: function(contact, type){ |
| 276 |
if (!contact){return;} |
| 277 |
|
| 278 |
if (contact.indexOf('@')==-1){ |
| 279 |
contact += '@' + this.session.domain; |
| 280 |
} |
| 281 |
|
| 282 |
var req = dojox.xmpp.util.createElement("presence",{to:contact,type:type},true); |
| 283 |
this.session.dispatchPacket(req); |
| 284 |
|
| 285 |
}, |
| 286 |
|
| 287 |
subscribe: function(contact){ |
| 288 |
this._manageSubscriptions(contact, "subscribe"); |
| 289 |
}, |
| 290 |
|
| 291 |
approveSubscription: function(contact){ |
| 292 |
this._manageSubscriptions(contact, "subscribed"); |
| 293 |
}, |
| 294 |
|
| 295 |
unsubscribe: function(contact){ |
| 296 |
this._manageSubscriptions(contact, "unsubscribe"); |
| 297 |
}, |
| 298 |
|
| 299 |
declineSubscription: function(contact){ |
| 300 |
this._manageSubscriptions(contact, "unsubscribed"); |
| 301 |
}, |
| 302 |
|
| 303 |
cancelSubscription: function(contact){ |
| 304 |
this._manageSubscriptions(contact, "unsubscribed"); |
| 305 |
} |
| 306 |
|
| 307 |
}); |