You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

muc.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. presMap: {},
  11. preziMap: {},
  12. joined: false,
  13. isOwner: false,
  14. init: function (conn) {
  15. this.connection = conn;
  16. },
  17. initPresenceMap: function (myroomjid) {
  18. this.presMap['to'] = myroomjid;
  19. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  20. },
  21. doJoin: function (jid, password) {
  22. this.myroomjid = jid;
  23. this.initPresenceMap(this.myroomjid);
  24. if (!this.roomjid) {
  25. this.roomjid = Strophe.getBareJidFromJid(jid);
  26. // add handlers (just once)
  27. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  28. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  29. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  30. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  31. }
  32. var join = $pres({to: this.myroomjid }).c('x', {xmlns: 'http://jabber.org/protocol/muc'});
  33. if (password !== undefined) {
  34. join.c('password').t(password);
  35. }
  36. this.connection.send(join);
  37. },
  38. onPresence: function (pres) {
  39. console.log("PRESENCE", pres);
  40. var from = pres.getAttribute('from');
  41. var type = pres.getAttribute('type');
  42. if (type != null) {
  43. return true;
  44. }
  45. var presentation = $(pres).find('>prezi');
  46. if (presentation.length)
  47. {
  48. var url = presentation.attr('url');
  49. var current = presentation.find('>current').text();
  50. console.log('presentation info received from', from, url);
  51. if (this.preziMap[from] == null) {
  52. this.preziMap[from] = url;
  53. $(document).trigger('presentationadded.muc', [from, url, current]);
  54. }
  55. else {
  56. $(document).trigger('gotoslide.muc', [from, url, current]);
  57. }
  58. }
  59. else if (this.preziMap[from] != null) {
  60. var url = this.preziMap[from];
  61. delete this.preziMap[from];
  62. $(document).trigger('presentationremoved.muc', [from, url]);
  63. }
  64. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  65. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  66. this.isOwner = true;
  67. var create = $iq({type: 'set', to: this.roomjid})
  68. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  69. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  70. this.connection.send(create); // fire away
  71. }
  72. var member = {};
  73. member.show = $(pres).find('>show').text();
  74. member.status = $(pres).find('>status').text();
  75. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  76. member.affiliation = tmp.attr('affiliation');
  77. member.role = tmp.attr('role');
  78. if (from == this.myroomjid) {
  79. if (member.affiliation == 'owner') this.isOwner = true;
  80. if (!this.joined) {
  81. this.joined = true;
  82. $(document).trigger('joined.muc', [from, member]);
  83. }
  84. } else if (this.members[from] === undefined) {
  85. // new participant
  86. this.members[from] = member;
  87. $(document).trigger('entered.muc', [from, member, pres]);
  88. } else {
  89. console.log('presence change from', from);
  90. $(document).trigger('presence.muc', [from, member, pres]);
  91. }
  92. return true;
  93. },
  94. onPresenceUnavailable: function (pres) {
  95. var from = pres.getAttribute('from');
  96. delete this.members[from];
  97. $(document).trigger('left.muc', [from]);
  98. return true;
  99. },
  100. onPresenceError: function (pres) {
  101. var from = pres.getAttribute('from');
  102. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  103. $(document).trigger('passwordrequired.muc', [from]);
  104. } else {
  105. console.warn('onPresError ', pres);
  106. }
  107. return true;
  108. },
  109. sendMessage: function (body, nickname) {
  110. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  111. msg.c('body', body).up();
  112. if (nickname) {
  113. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  114. }
  115. this.connection.send(msg);
  116. },
  117. onMessage: function (msg) {
  118. var txt = $(msg).find('>body').text();
  119. // TODO: <subject/>
  120. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  121. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(msg.getAttribute('from'));
  122. if (txt) {
  123. console.log('chat', nick, txt);
  124. updateChatConversation(nick, txt);
  125. }
  126. return true;
  127. },
  128. lockRoom: function (key) {
  129. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  130. var ob = this;
  131. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  132. function (res) {
  133. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  134. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  135. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  136. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  137. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  138. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  139. this.connection.sendIQ(formsubmit,
  140. function (res) {
  141. console.log('set room password');
  142. },
  143. function (err) {
  144. console.warn('setting password failed', err);
  145. }
  146. );
  147. } else {
  148. console.warn('room passwords not supported');
  149. }
  150. },
  151. function (err) {
  152. console.warn('setting password failed', err);
  153. }
  154. );
  155. },
  156. sendPresence: function () {
  157. var pres = $pres({to: this.presMap['to'] });
  158. pres.c('x', {xmlns: this.presMap['xns']}).up();
  159. if (this.presMap['prezins']) {
  160. pres.c('prezi', {xmlns: this.presMap['prezins'], 'url': this.presMap['preziurl']}).
  161. c('current').t(this.presMap['prezicurrent']).up().up();
  162. }
  163. if (this.presMap['medians'])
  164. {
  165. pres.c('media', {xmlns: this.presMap['medians']});
  166. var sourceNumber = 0;
  167. Object.keys(this.presMap).forEach(function (key) {
  168. if (key.indexOf('source') >= 0) {
  169. sourceNumber++;
  170. }
  171. });
  172. if (sourceNumber > 0)
  173. for (var i = 1; i <= sourceNumber/2; i ++) {
  174. pres.c('source',
  175. {type: this.presMap['source' + i + '_type'],
  176. ssrc: this.presMap['source' + i + '_ssrc']}).up();
  177. }
  178. }
  179. pres.up();
  180. connection.send(pres);
  181. },
  182. addMediaToPresence: function (sourceNumber, mtype, ssrcs) {
  183. if (!this.presMap['medians'])
  184. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  185. this.presMap['source' + sourceNumber + '_type'] = mtype;
  186. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  187. },
  188. addPreziToPresence: function (url, currentSlide) {
  189. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  190. this.presMap['preziurl'] = url;
  191. this.presMap['prezicurrent'] = currentSlide;
  192. },
  193. removePreziFromPresence: function () {
  194. delete this.presMap['prezins'];
  195. delete this.presMap['preziurl'];
  196. delete this.presMap['prezicurrent'];
  197. },
  198. addCurrentSlideToPresence: function (currentSlide) {
  199. this.presMap['prezicurrent'] = currentSlide;
  200. },
  201. getPrezi: function (roomjid) {
  202. return this.preziMap[roomjid];
  203. }
  204. });