PresenceService.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // wrapped by build app
  2. define("dojox/xmpp/PresenceService", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.xmpp.PresenceService");
  4. dojox.xmpp.presence = {
  5. UPDATE: 201,
  6. SUBSCRIPTION_REQUEST: 202,
  7. // SUBSCRIPTION_REQUEST_PENDING: 203,
  8. /* used when 'ask' attribute is absent on a roster item */
  9. SUBSCRIPTION_SUBSTATUS_NONE: 204,
  10. SUBSCRIPTION_NONE: 'none',
  11. SUBSCRIPTION_FROM: 'from',
  12. SUBSCRIPTION_TO: 'to',
  13. SUBSCRIPTION_BOTH: 'both',
  14. SUBSCRIPTION_REQUEST_PENDING: 'pending',
  15. STATUS_ONLINE: 'online',
  16. STATUS_AWAY: 'away',
  17. STATUS_CHAT: 'chat',
  18. STATUS_DND: 'dnd',
  19. STATUS_EXTENDED_AWAY: 'xa',
  20. STATUS_OFFLINE: 'offline',
  21. STATUS_INVISIBLE: 'invisible'
  22. }
  23. dojo.declare("dojox.xmpp.PresenceService", null, {
  24. constructor: function(xmppService){
  25. this.session= xmppService;
  26. this.isInvisible = false;
  27. this.avatarHash = null;
  28. this.presence = null;
  29. this.restrictedContactjids = {};
  30. },
  31. publish: function(presence){
  32. ////console.log("Presence::publish() ", presence);
  33. this.presence = presence;
  34. this._setPresence();
  35. },
  36. /**
  37. <presence from='juliet@capulet.com/balcony'>
  38. <x xmlns='vcard-temp:x:update'>
  39. <photo>sha1-hash-of-image</photo>
  40. </x>
  41. </presence>
  42. <presence>
  43. <x xmlns='vcard-temp:x:update'>
  44. <photo/>
  45. </x>
  46. </presence>
  47. */
  48. sendAvatarHash: function(avatarHash) {
  49. this.avatarHash = avatarHash;
  50. this._setPresence();
  51. },
  52. _setPresence: function() {
  53. var presence = this.presence;
  54. var p = {xmlns: 'jabber:client'};
  55. if (presence && presence.to){
  56. p.to = presence.to;
  57. }
  58. if (presence.show && presence.show==dojox.xmpp.presence.STATUS_OFFLINE){
  59. p.type = 'unavailable';
  60. }
  61. if (presence.show && presence.show==dojox.xmpp.presence.STATUS_INVISIBLE) {
  62. this._setInvisible();
  63. this.isInvisible = true;
  64. return;
  65. };
  66. if(this.isInvisible) {
  67. //console.log("was invisible, making visible");
  68. this._setVisible();
  69. }
  70. var req = new dojox.string.Builder(dojox.xmpp.util.createElement("presence",p, false));
  71. if (presence.show && presence.show!=dojox.xmpp.presence.STATUS_OFFLINE ) {
  72. req.append(dojox.xmpp.util.createElement("show",{},false));
  73. req.append(presence.show);
  74. req.append("</show>");
  75. }
  76. if(presence.status) {
  77. req.append(dojox.xmpp.util.createElement("status",{},false));
  78. req.append(presence.status);
  79. req.append("</status>");
  80. }
  81. if(this.avatarHash) {
  82. req.append(dojox.xmpp.util.createElement("x",{xmlns: 'vcard-temp:x:update'},false));
  83. req.append(dojox.xmpp.util.createElement("photo",{},false));
  84. req.append(this.avatarHash);
  85. req.append("</photo>");
  86. req.append("</x>");
  87. }
  88. if (presence.priority && presence.show!=dojox.xmpp.presence.STATUS_OFFLINE){
  89. if(presence.priority > 127 || presence.priority < -128){
  90. presence.priority = 5;
  91. }
  92. req.append(dojox.xmpp.util.createElement("priority",{},false));
  93. req.append(presence.priority);
  94. req.append("</priority>");
  95. }
  96. req.append("</presence>");
  97. this.session.dispatchPacket(req.toString());
  98. },
  99. /*
  100. <iq from='bilbo@tolkien.lit/shire' type='set' id='inv1'>
  101. <query xmlns='jabber:iq:privacy'>
  102. <list name='invisible'>
  103. <item action='deny' order='1'>
  104. <presence-out/>
  105. </item>
  106. </list>
  107. </query>
  108. </iq>
  109. <iq from='bilbo@tolkien.lit/shire' type='set' id='active1'>
  110. <query xmlns='jabber:iq:privacy'>
  111. <active name='invisible'/>
  112. </query>
  113. </iq>
  114. Make visible:
  115. <iq from='bilbo@tolkien.lit/shire' type='set' id='active6'>
  116. <query xmlns='jabber:iq:privacy'>
  117. <active/>
  118. </query>
  119. </iq>
  120. */
  121. toggleBlockContact: function(jid) {
  122. if(!this.restrictedContactjids[jid]) {
  123. this.restrictedContactjids[jid] = this._createRestrictedJid();
  124. }
  125. this.restrictedContactjids[jid].blocked = !this.restrictedContactjids[jid].blocked;
  126. //console.log("setting outbound block for ", jid, this.restrictedContactjids[jid]);
  127. this._updateRestricted();
  128. return this.restrictedContactjids;
  129. },
  130. toggleContactInvisiblity: function(jid) {
  131. if(!this.restrictedContactjids[jid]) {
  132. this.restrictedContactjids[jid] = this._createRestrictedJid();
  133. }
  134. this.restrictedContactjids[jid].invisible = !this.restrictedContactjids[jid].invisible;
  135. //console.log("setting outbound presence for ", jid, this.restrictedContactjids[jid]);
  136. this._updateRestricted();
  137. return this.restrictedContactjids;
  138. },
  139. _createRestrictedJid: function() {
  140. return {invisible: false, blocked:false};
  141. },
  142. _updateRestricted: function() {
  143. var props={
  144. id: this.session.getNextIqId(),
  145. from: this.session.jid + "/" + this.session.resource,
  146. type: "set"
  147. };
  148. var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
  149. req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
  150. req.append(dojox.xmpp.util.createElement("list",{name: "iwcRestrictedContacts"},false))
  151. var count = 1;
  152. for(var jid in this.restrictedContactjids) {
  153. var item = this.restrictedContactjids[jid];
  154. //console.log("restricted ", jid, item);
  155. if(item.blocked || item.invisible) {
  156. req.append(dojox.xmpp.util.createElement("item",{value: dojox.xmpp.util.encodeJid(jid), action: "deny", order: count++},false));
  157. if(item.blocked) {
  158. req.append(dojox.xmpp.util.createElement("message",{},true));
  159. }
  160. if(item.invisible) {
  161. req.append(dojox.xmpp.util.createElement("presence-out",{},true));
  162. }
  163. req.append("</item>");
  164. } else {
  165. delete this.restrictedContactjids[jid];
  166. }
  167. }
  168. req.append("</list>");
  169. req.append("</query>");
  170. req.append("</iq>");
  171. //console.log("Restricted list: ", req.toString());
  172. var req2 = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
  173. req2.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
  174. req2.append(dojox.xmpp.util.createElement("active",{name:"iwcRestrictedContacts"},true));
  175. req2.append("</query>");
  176. req2.append("</iq>");
  177. //console.log("Activate list: ", req2.toString());
  178. this.session.dispatchPacket(req.toString());
  179. this.session.dispatchPacket(req2.toString());
  180. },
  181. _setVisible: function() {
  182. var props={
  183. id: this.session.getNextIqId(),
  184. from: this.session.jid + "/" + this.session.resource,
  185. type: "set"
  186. };
  187. var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
  188. req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
  189. req.append(dojox.xmpp.util.createElement("active",{},true));
  190. req.append("</query>");
  191. req.append("</iq>");
  192. //console.log(req.toString());
  193. this.session.dispatchPacket(req.toString());
  194. },
  195. _setInvisible: function() {
  196. //console.log("Setting user as invisible");
  197. var props={
  198. id: this.session.getNextIqId(),
  199. from: this.session.jid + "/" + this.session.resource,
  200. type: "set"
  201. };
  202. var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
  203. req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
  204. req.append(dojox.xmpp.util.createElement("list",{name: "invisible"},false))
  205. req.append(dojox.xmpp.util.createElement("item",{action: "deny", order: "1"},false))
  206. req.append(dojox.xmpp.util.createElement("presence-out",{},true));
  207. req.append("</item>");
  208. req.append("</list>");
  209. req.append("</query>");
  210. req.append("</iq>");
  211. props={
  212. id: this.session.getNextIqId(),
  213. from: this.session.jid + "/" + this.session.resource,
  214. type: "set"
  215. };
  216. var req2 = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
  217. req2.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
  218. req2.append(dojox.xmpp.util.createElement("active",{name:"invisible"},true));
  219. req2.append("</query>");
  220. req2.append("</iq>");
  221. //console.log(req.toString());
  222. //console.log(req2.toString());
  223. this.session.dispatchPacket(req.toString());
  224. this.session.dispatchPacket(req2.toString());
  225. },
  226. _manageSubscriptions: function(contact, type){
  227. if (!contact){return;}
  228. if (contact.indexOf('@')==-1){
  229. contact += '@' + this.session.domain;
  230. }
  231. var req = dojox.xmpp.util.createElement("presence",{to:contact,type:type},true);
  232. this.session.dispatchPacket(req);
  233. },
  234. subscribe: function(contact){
  235. this._manageSubscriptions(contact, "subscribe");
  236. },
  237. approveSubscription: function(contact){
  238. this._manageSubscriptions(contact, "subscribed");
  239. },
  240. unsubscribe: function(contact){
  241. this._manageSubscriptions(contact, "unsubscribe");
  242. },
  243. declineSubscription: function(contact){
  244. this._manageSubscriptions(contact, "unsubscribed");
  245. },
  246. cancelSubscription: function(contact){
  247. this._manageSubscriptions(contact, "unsubscribed");
  248. }
  249. });
  250. });