Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

muc.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* jshint -W117 */
  2. /* a simple MUC connection plugin
  3. * can only handle a single MUC room
  4. */
  5. Strophe.addConnectionPlugin('emuc', {
  6. connection: null,
  7. roomjid: null,
  8. myroomjid: null,
  9. members: {},
  10. list_members: [], // so we can elect a new focus
  11. presMap: {},
  12. preziMap: {},
  13. joined: false,
  14. isOwner: false,
  15. init: function (conn) {
  16. this.connection = conn;
  17. },
  18. initPresenceMap: function (myroomjid) {
  19. this.presMap['to'] = myroomjid;
  20. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  21. },
  22. doJoin: function (jid, password) {
  23. this.myroomjid = jid;
  24. this.initPresenceMap(this.myroomjid);
  25. if (!this.roomjid) {
  26. this.roomjid = Strophe.getBareJidFromJid(jid);
  27. // add handlers (just once)
  28. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  29. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  30. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  31. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  32. }
  33. if (password !== undefined) {
  34. this.presMap['password'] = password;
  35. }
  36. this.sendPresence();
  37. },
  38. doLeave: function() {
  39. console.log("do leave", this.myroomjid);
  40. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  41. this.presMap.length = 0;
  42. this.connection.send(pres);
  43. },
  44. onPresence: function (pres) {
  45. var from = pres.getAttribute('from');
  46. var type = pres.getAttribute('type');
  47. if (type != null) {
  48. return true;
  49. }
  50. // Parse etherpad tag.
  51. var etherpad = $(pres).find('>etherpad');
  52. if (etherpad.length) {
  53. $(document).trigger('etherpadadded.muc', [from, etherpad.text()]);
  54. }
  55. // Parse prezi tag.
  56. var presentation = $(pres).find('>prezi');
  57. if (presentation.length)
  58. {
  59. var url = presentation.attr('url');
  60. var current = presentation.find('>current').text();
  61. console.log('presentation info received from', from, url);
  62. if (this.preziMap[from] == null) {
  63. this.preziMap[from] = url;
  64. $(document).trigger('presentationadded.muc', [from, url, current]);
  65. }
  66. else {
  67. $(document).trigger('gotoslide.muc', [from, url, current]);
  68. }
  69. }
  70. else if (this.preziMap[from] != null) {
  71. var url = this.preziMap[from];
  72. delete this.preziMap[from];
  73. $(document).trigger('presentationremoved.muc', [from, url]);
  74. }
  75. // Parse audio info tag.
  76. var audioMuted = $(pres).find('>audiomuted');
  77. if (audioMuted.length) {
  78. $(document).trigger('audiomuted.muc', [from, audioMuted.text()]);
  79. }
  80. // Parse video info tag.
  81. var videoMuted = $(pres).find('>videomuted');
  82. if (videoMuted.length) {
  83. $(document).trigger('videomuted.muc', [from, videoMuted.text()]);
  84. }
  85. // Parse status.
  86. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  87. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  88. this.isOwner = true;
  89. var create = $iq({type: 'set', to: this.roomjid})
  90. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  91. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  92. this.connection.send(create); // fire away
  93. }
  94. // Parse roles.
  95. var member = {};
  96. member.show = $(pres).find('>show').text();
  97. member.status = $(pres).find('>status').text();
  98. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  99. member.affiliation = tmp.attr('affiliation');
  100. member.role = tmp.attr('role');
  101. var nicktag = $(pres).find('>nick[xmlns="http://jabber.org/protocol/nick"]');
  102. member.displayName = (nicktag.length > 0 ? nicktag.text() : null);
  103. if (from == this.myroomjid) {
  104. if (member.affiliation == 'owner') this.isOwner = true;
  105. if (!this.joined) {
  106. this.joined = true;
  107. $(document).trigger('joined.muc', [from, member]);
  108. this.list_members.push(from);
  109. }
  110. } else if (this.members[from] === undefined) {
  111. // new participant
  112. this.members[from] = member;
  113. this.list_members.push(from);
  114. $(document).trigger('entered.muc', [from, member, pres]);
  115. }
  116. // Always trigger presence to update bindings
  117. console.log('presence change from', from);
  118. $(document).trigger('presence.muc', [from, member, pres]);
  119. return true;
  120. },
  121. onPresenceUnavailable: function (pres) {
  122. var from = pres.getAttribute('from');
  123. // Status code 110 indicates that this notification is "self-presence".
  124. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  125. delete this.members[from];
  126. this.list_members.splice(this.list_members.indexOf(from), 1);
  127. $(document).trigger('left.muc', [from]);
  128. }
  129. // If the status code is 110 this means we're leaving and we would like
  130. // to remove everyone else from our view, so we trigger the event.
  131. else if (this.list_members.length > 1) {
  132. for (var i = 0; i < this.list_members.length; i++) {
  133. var member = this.list_members[i];
  134. delete this.members[i];
  135. this.list_members.splice(i, 1);
  136. $(document).trigger('left.muc', member);
  137. }
  138. }
  139. return true;
  140. },
  141. onPresenceError: function (pres) {
  142. var from = pres.getAttribute('from');
  143. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  144. $(document).trigger('passwordrequired.muc', [from]);
  145. } else {
  146. console.warn('onPresError ', pres);
  147. }
  148. return true;
  149. },
  150. sendMessage: function (body, nickname) {
  151. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  152. msg.c('body', body).up();
  153. if (nickname) {
  154. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  155. }
  156. this.connection.send(msg);
  157. },
  158. onMessage: function (msg) {
  159. var txt = $(msg).find('>body').text();
  160. // TODO: <subject/>
  161. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  162. var from = msg.getAttribute('from');
  163. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  164. if (txt) {
  165. console.log('chat', nick, txt);
  166. Chat.updateChatConversation(from, nick, txt);
  167. }
  168. return true;
  169. },
  170. lockRoom: function (key) {
  171. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  172. var ob = this;
  173. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  174. function (res) {
  175. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  176. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  177. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  178. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  179. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  180. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  181. this.connection.sendIQ(formsubmit,
  182. function (res) {
  183. console.log('set room password');
  184. },
  185. function (err) {
  186. console.warn('setting password failed', err);
  187. }
  188. );
  189. } else {
  190. console.warn('room passwords not supported');
  191. }
  192. },
  193. function (err) {
  194. console.warn('setting password failed', err);
  195. }
  196. );
  197. },
  198. kick: function (jid) {
  199. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  200. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  201. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  202. .c('reason').t('You have been kicked.').up().up().up();
  203. this.connection.sendIQ(
  204. kickIQ,
  205. function (result) {
  206. console.log('Kick participant with jid: ', jid, result);
  207. },
  208. function (error) {
  209. console.log('Kick participant error: ', error);
  210. });
  211. },
  212. sendPresence: function () {
  213. var pres = $pres({to: this.presMap['to'] });
  214. pres.c('x', {xmlns: this.presMap['xns']});
  215. if (this.presMap['password']) {
  216. pres.c('password').t(this.presMap['password']).up();
  217. }
  218. pres.up();
  219. if (this.presMap['displayName']) {
  220. // XEP-0172
  221. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
  222. .t(this.presMap['displayName']).up();
  223. }
  224. if (this.presMap['audions']) {
  225. pres.c('audiomuted', {xmlns: this.presMap['audions']})
  226. .t(this.presMap['audiomuted']).up();
  227. }
  228. if (this.presMap['videons']) {
  229. pres.c('videomuted', {xmlns: this.presMap['videons']})
  230. .t(this.presMap['videomuted']).up();
  231. }
  232. if (this.presMap['prezins']) {
  233. pres.c('prezi',
  234. {xmlns: this.presMap['prezins'],
  235. 'url': this.presMap['preziurl']})
  236. .c('current').t(this.presMap['prezicurrent']).up().up();
  237. }
  238. if (this.presMap['etherpadns']) {
  239. pres.c('etherpad', {xmlns: this.presMap['etherpadns']})
  240. .t(this.presMap['etherpadname']).up();
  241. }
  242. if (this.presMap['medians'])
  243. {
  244. pres.c('media', {xmlns: this.presMap['medians']});
  245. var sourceNumber = 0;
  246. Object.keys(this.presMap).forEach(function (key) {
  247. if (key.indexOf('source') >= 0) {
  248. sourceNumber++;
  249. }
  250. });
  251. if (sourceNumber > 0)
  252. for (var i = 1; i <= sourceNumber/3; i ++) {
  253. pres.c('source',
  254. {type: this.presMap['source' + i + '_type'],
  255. ssrc: this.presMap['source' + i + '_ssrc'],
  256. direction: this.presMap['source'+ i + '_direction']
  257. || 'sendrecv' }
  258. ).up();
  259. }
  260. }
  261. pres.up();
  262. connection.send(pres);
  263. },
  264. addDisplayNameToPresence: function (displayName) {
  265. this.presMap['displayName'] = displayName;
  266. },
  267. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  268. if (!this.presMap['medians'])
  269. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  270. this.presMap['source' + sourceNumber + '_type'] = mtype;
  271. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  272. this.presMap['source' + sourceNumber + '_direction'] = direction;
  273. },
  274. clearPresenceMedia: function () {
  275. var self = this;
  276. Object.keys(this.presMap).forEach( function(key) {
  277. if(key.indexOf('source') != -1) {
  278. delete self.presMap[key];
  279. }
  280. });
  281. },
  282. addPreziToPresence: function (url, currentSlide) {
  283. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  284. this.presMap['preziurl'] = url;
  285. this.presMap['prezicurrent'] = currentSlide;
  286. },
  287. removePreziFromPresence: function () {
  288. delete this.presMap['prezins'];
  289. delete this.presMap['preziurl'];
  290. delete this.presMap['prezicurrent'];
  291. },
  292. addCurrentSlideToPresence: function (currentSlide) {
  293. this.presMap['prezicurrent'] = currentSlide;
  294. },
  295. getPrezi: function (roomjid) {
  296. return this.preziMap[roomjid];
  297. },
  298. addEtherpadToPresence: function(etherpadName) {
  299. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  300. this.presMap['etherpadname'] = etherpadName;
  301. },
  302. addAudioInfoToPresence: function(isMuted) {
  303. this.presMap['audions'] = 'http://jitsi.org/jitmeet/audio';
  304. this.presMap['audiomuted'] = isMuted.toString();
  305. },
  306. addVideoInfoToPresence: function(isMuted) {
  307. this.presMap['videons'] = 'http://jitsi.org/jitmeet/video';
  308. this.presMap['videomuted'] = isMuted.toString();
  309. }
  310. });