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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 if (this.members[from] === undefined) {
  93. // new participant
  94. this.members[from] = member;
  95. this.list_members.push(from);
  96. $(document).trigger('entered.muc', [from, member, pres]);
  97. }
  98. // Always trigger presence to update bindings
  99. console.log('presence change from', from);
  100. $(document).trigger('presence.muc', [from, member, pres]);
  101. return true;
  102. },
  103. onPresenceUnavailable: function (pres) {
  104. var from = pres.getAttribute('from');
  105. delete this.members[from];
  106. this.list_members.splice(this.list_members.indexOf(from), 1);
  107. $(document).trigger('left.muc', [from]);
  108. return true;
  109. },
  110. onPresenceError: function (pres) {
  111. var from = pres.getAttribute('from');
  112. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  113. $(document).trigger('passwordrequired.muc', [from]);
  114. } else {
  115. console.warn('onPresError ', pres);
  116. }
  117. return true;
  118. },
  119. sendMessage: function (body, nickname) {
  120. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  121. msg.c('body', body).up();
  122. if (nickname) {
  123. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  124. }
  125. this.connection.send(msg);
  126. },
  127. onMessage: function (msg) {
  128. var txt = $(msg).find('>body').text();
  129. // TODO: <subject/>
  130. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  131. var from = msg.getAttribute('from');
  132. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  133. if (txt) {
  134. console.log('chat', nick, txt);
  135. Chat.updateChatConversation(from, nick, txt);
  136. }
  137. return true;
  138. },
  139. lockRoom: function (key) {
  140. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  141. var ob = this;
  142. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  143. function (res) {
  144. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  145. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  146. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  147. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  148. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  149. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  150. this.connection.sendIQ(formsubmit,
  151. function (res) {
  152. console.log('set room password');
  153. },
  154. function (err) {
  155. console.warn('setting password failed', err);
  156. }
  157. );
  158. } else {
  159. console.warn('room passwords not supported');
  160. }
  161. },
  162. function (err) {
  163. console.warn('setting password failed', err);
  164. }
  165. );
  166. },
  167. sendPresence: function () {
  168. var pres = $pres({to: this.presMap['to'] });
  169. pres.c('x', {xmlns: this.presMap['xns']});
  170. if (this.presMap['password']) {
  171. pres.c('password').t(this.presMap['password']).up();
  172. }
  173. pres.up();
  174. if (this.presMap['displayName']) {
  175. // XEP-0172
  176. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(this.presMap['displayName']).up();
  177. }
  178. if (this.presMap['prezins']) {
  179. pres.c('prezi', {xmlns: this.presMap['prezins'], 'url': this.presMap['preziurl']}).
  180. c('current').t(this.presMap['prezicurrent']).up().up();
  181. }
  182. if (this.presMap['etherpadns']) {
  183. pres.c('etherpad', {xmlns: this.presMap['etherpadns']}).t(this.presMap['etherpadname']).up();
  184. }
  185. if (this.presMap['medians'])
  186. {
  187. pres.c('media', {xmlns: this.presMap['medians']});
  188. var sourceNumber = 0;
  189. Object.keys(this.presMap).forEach(function (key) {
  190. if (key.indexOf('source') >= 0) {
  191. sourceNumber++;
  192. }
  193. });
  194. if (sourceNumber > 0)
  195. for (var i = 1; i <= sourceNumber/3; i ++) {
  196. pres.c('source',
  197. {type: this.presMap['source' + i + '_type'],
  198. ssrc: this.presMap['source' + i + '_ssrc'],
  199. direction: this.presMap['source'+ i + '_direction'] || 'sendrecv' }
  200. ).up();
  201. }
  202. }
  203. pres.up();
  204. connection.send(pres);
  205. },
  206. addDisplayNameToPresence: function (displayName) {
  207. this.presMap['displayName'] = displayName;
  208. },
  209. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  210. if (!this.presMap['medians'])
  211. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  212. this.presMap['source' + sourceNumber + '_type'] = mtype;
  213. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  214. this.presMap['source' + sourceNumber + '_direction'] = direction;
  215. },
  216. clearPresenceMedia: function () {
  217. var self = this;
  218. Object.keys(this.presMap).forEach( function(key) {
  219. if(key.indexOf('source') != -1) {
  220. delete self.presMap[key];
  221. }
  222. });
  223. },
  224. addPreziToPresence: function (url, currentSlide) {
  225. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  226. this.presMap['preziurl'] = url;
  227. this.presMap['prezicurrent'] = currentSlide;
  228. },
  229. removePreziFromPresence: function () {
  230. delete this.presMap['prezins'];
  231. delete this.presMap['preziurl'];
  232. delete this.presMap['prezicurrent'];
  233. },
  234. addCurrentSlideToPresence: function (currentSlide) {
  235. this.presMap['prezicurrent'] = currentSlide;
  236. },
  237. getPrezi: function (roomjid) {
  238. return this.preziMap[roomjid];
  239. },
  240. addEtherpadToPresence: function(etherpadName) {
  241. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  242. this.presMap['etherpadname'] = etherpadName;
  243. }
  244. });