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

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