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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. onPresence: function (pres) {
  39. var from = pres.getAttribute('from');
  40. var type = pres.getAttribute('type');
  41. if (type != null) {
  42. return true;
  43. }
  44. // Parse etherpad tag.
  45. var etherpad = $(pres).find('>etherpad');
  46. if (etherpad.length) {
  47. $(document).trigger('etherpadadded.muc', [from, etherpad.text()]);
  48. }
  49. // Parse prezi tag.
  50. var presentation = $(pres).find('>prezi');
  51. if (presentation.length)
  52. {
  53. var url = presentation.attr('url');
  54. var current = presentation.find('>current').text();
  55. console.log('presentation info received from', from, url);
  56. if (this.preziMap[from] == null) {
  57. this.preziMap[from] = url;
  58. $(document).trigger('presentationadded.muc', [from, url, current]);
  59. }
  60. else {
  61. $(document).trigger('gotoslide.muc', [from, url, current]);
  62. }
  63. }
  64. else if (this.preziMap[from] != null) {
  65. var url = this.preziMap[from];
  66. delete this.preziMap[from];
  67. $(document).trigger('presentationremoved.muc', [from, url]);
  68. }
  69. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  70. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  71. this.isOwner = true;
  72. var create = $iq({type: 'set', to: this.roomjid})
  73. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  74. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  75. this.connection.send(create); // fire away
  76. }
  77. var member = {};
  78. member.show = $(pres).find('>show').text();
  79. member.status = $(pres).find('>status').text();
  80. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  81. member.affiliation = tmp.attr('affiliation');
  82. member.role = tmp.attr('role');
  83. var nicktag = $(pres).find('>nick[xmlns="http://jabber.org/protocol/nick"]');
  84. member.displayName = (nicktag.length > 0 ? nicktag.text() : null);
  85. if (from == this.myroomjid) {
  86. if (member.affiliation == 'owner') this.isOwner = true;
  87. if (!this.joined) {
  88. this.joined = true;
  89. $(document).trigger('joined.muc', [from, member]);
  90. this.list_members.push(from);
  91. }
  92. else
  93. $(document).trigger('presence.muc', [from, member, pres]);
  94. } else if (this.members[from] === undefined) {
  95. // new participant
  96. this.members[from] = member;
  97. this.list_members.push(from);
  98. $(document).trigger('entered.muc', [from, member, pres]);
  99. } else {
  100. console.log('presence change from', from);
  101. $(document).trigger('presence.muc', [from, member, pres]);
  102. }
  103. return true;
  104. },
  105. onPresenceUnavailable: function (pres) {
  106. var from = pres.getAttribute('from');
  107. delete this.members[from];
  108. this.list_members.splice(this.list_members.indexOf(from), 1);
  109. $(document).trigger('left.muc', [from]);
  110. return true;
  111. },
  112. onPresenceError: function (pres) {
  113. var from = pres.getAttribute('from');
  114. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  115. $(document).trigger('passwordrequired.muc', [from]);
  116. } else {
  117. console.warn('onPresError ', pres);
  118. }
  119. return true;
  120. },
  121. sendMessage: function (body, nickname) {
  122. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  123. msg.c('body', body).up();
  124. if (nickname) {
  125. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  126. }
  127. this.connection.send(msg);
  128. },
  129. onMessage: function (msg) {
  130. var txt = $(msg).find('>body').text();
  131. // TODO: <subject/>
  132. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  133. var from = msg.getAttribute('from');
  134. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  135. if (txt) {
  136. console.log('chat', nick, txt);
  137. Chat.updateChatConversation(from, nick, txt);
  138. }
  139. return true;
  140. },
  141. lockRoom: function (key) {
  142. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  143. var ob = this;
  144. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  145. function (res) {
  146. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  147. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  148. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  149. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  150. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  151. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  152. this.connection.sendIQ(formsubmit,
  153. function (res) {
  154. console.log('set room password');
  155. },
  156. function (err) {
  157. console.warn('setting password failed', err);
  158. }
  159. );
  160. } else {
  161. console.warn('room passwords not supported');
  162. }
  163. },
  164. function (err) {
  165. console.warn('setting password failed', err);
  166. }
  167. );
  168. },
  169. sendPresence: function () {
  170. var pres = $pres({to: this.presMap['to'] });
  171. pres.c('x', {xmlns: this.presMap['xns']});
  172. if (this.presMap['password']) {
  173. pres.c('password').t(this.presMap['password']).up();
  174. }
  175. pres.up();
  176. if (this.presMap['displayName']) {
  177. // XEP-0172
  178. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(this.presMap['displayName']).up();
  179. }
  180. if (this.presMap['prezins']) {
  181. pres.c('prezi', {xmlns: this.presMap['prezins'], 'url': this.presMap['preziurl']}).
  182. c('current').t(this.presMap['prezicurrent']).up().up();
  183. }
  184. if (this.presMap['etherpadns']) {
  185. pres.c('etherpad', {xmlns: this.presMap['etherpadns']}).t(this.presMap['etherpadname']).up();
  186. }
  187. if (this.presMap['medians'])
  188. {
  189. pres.c('media', {xmlns: this.presMap['medians']});
  190. var sourceNumber = 0;
  191. Object.keys(this.presMap).forEach(function (key) {
  192. if (key.indexOf('source') >= 0) {
  193. sourceNumber++;
  194. }
  195. });
  196. if (sourceNumber > 0)
  197. for (var i = 1; i <= sourceNumber/3; i ++) {
  198. pres.c('source',
  199. {type: this.presMap['source' + i + '_type'],
  200. ssrc: this.presMap['source' + i + '_ssrc'],
  201. direction: this.presMap['source'+ i + '_direction'] || 'sendrecv' }
  202. ).up();
  203. }
  204. }
  205. pres.up();
  206. connection.send(pres);
  207. },
  208. addDisplayNameToPresence: function (displayName) {
  209. this.presMap['displayName'] = displayName;
  210. },
  211. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  212. if (!this.presMap['medians'])
  213. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  214. this.presMap['source' + sourceNumber + '_type'] = mtype;
  215. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  216. this.presMap['source' + sourceNumber + '_direction'] = direction;
  217. },
  218. addPreziToPresence: function (url, currentSlide) {
  219. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  220. this.presMap['preziurl'] = url;
  221. this.presMap['prezicurrent'] = currentSlide;
  222. },
  223. removePreziFromPresence: function () {
  224. delete this.presMap['prezins'];
  225. delete this.presMap['preziurl'];
  226. delete this.presMap['prezicurrent'];
  227. },
  228. addCurrentSlideToPresence: function (currentSlide) {
  229. this.presMap['prezicurrent'] = currentSlide;
  230. },
  231. getPrezi: function (roomjid) {
  232. return this.preziMap[roomjid];
  233. },
  234. addEtherpadToPresence: function(etherpadName) {
  235. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  236. this.presMap['etherpadname'] = etherpadName;
  237. }
  238. });