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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(msg.getAttribute('from'));
  134. if (txt) {
  135. console.log('chat', nick, txt);
  136. Chat.updateChatConversation(nick, txt);
  137. }
  138. return true;
  139. },
  140. lockRoom: function (key) {
  141. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  142. var ob = this;
  143. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  144. function (res) {
  145. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  146. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  147. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  148. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  149. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  150. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  151. this.connection.sendIQ(formsubmit,
  152. function (res) {
  153. console.log('set room password');
  154. },
  155. function (err) {
  156. console.warn('setting password failed', err);
  157. }
  158. );
  159. } else {
  160. console.warn('room passwords not supported');
  161. }
  162. },
  163. function (err) {
  164. console.warn('setting password failed', err);
  165. }
  166. );
  167. },
  168. sendPresence: function () {
  169. var pres = $pres({to: this.presMap['to'] });
  170. pres.c('x', {xmlns: this.presMap['xns']});
  171. if (this.presMap['password']) {
  172. pres.c('password').t(this.presMap['password']).up();
  173. }
  174. pres.up();
  175. if (this.presMap['displayName']) {
  176. // XEP-0172
  177. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(this.presMap['displayName']).up();
  178. }
  179. if (this.presMap['prezins']) {
  180. pres.c('prezi', {xmlns: this.presMap['prezins'], 'url': this.presMap['preziurl']}).
  181. c('current').t(this.presMap['prezicurrent']).up().up();
  182. }
  183. if (this.presMap['etherpadns']) {
  184. pres.c('etherpad', {xmlns: this.presMap['etherpadns']}).t(this.presMap['etherpadname']).up();
  185. }
  186. if (this.presMap['medians'])
  187. {
  188. pres.c('media', {xmlns: this.presMap['medians']});
  189. var sourceNumber = 0;
  190. Object.keys(this.presMap).forEach(function (key) {
  191. if (key.indexOf('source') >= 0) {
  192. sourceNumber++;
  193. }
  194. });
  195. if (sourceNumber > 0)
  196. for (var i = 1; i <= sourceNumber/2; i ++) {
  197. pres.c('source',
  198. {type: this.presMap['source' + i + '_type'],
  199. ssrc: this.presMap['source' + i + '_ssrc']}).up();
  200. }
  201. }
  202. pres.up();
  203. connection.send(pres);
  204. },
  205. addDisplayNameToPresence: function (displayName) {
  206. this.presMap['displayName'] = displayName;
  207. },
  208. addMediaToPresence: function (sourceNumber, mtype, ssrcs) {
  209. if (!this.presMap['medians'])
  210. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  211. this.presMap['source' + sourceNumber + '_type'] = mtype;
  212. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  213. },
  214. addPreziToPresence: function (url, currentSlide) {
  215. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  216. this.presMap['preziurl'] = url;
  217. this.presMap['prezicurrent'] = currentSlide;
  218. },
  219. removePreziFromPresence: function () {
  220. delete this.presMap['prezins'];
  221. delete this.presMap['preziurl'];
  222. delete this.presMap['prezicurrent'];
  223. },
  224. addCurrentSlideToPresence: function (currentSlide) {
  225. this.presMap['prezicurrent'] = currentSlide;
  226. },
  227. getPrezi: function (roomjid) {
  228. return this.preziMap[roomjid];
  229. },
  230. addEtherpadToPresence: function(etherpadName) {
  231. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  232. this.presMap['etherpadname'] = etherpadName;
  233. }
  234. });