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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. console.info("Joined MUC as " + this.myroomjid);
  25. this.initPresenceMap(this.myroomjid);
  26. if (!this.roomjid) {
  27. this.roomjid = Strophe.getBareJidFromJid(jid);
  28. // add handlers (just once)
  29. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  30. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  31. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  32. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  33. }
  34. if (password !== undefined) {
  35. this.presMap['password'] = password;
  36. }
  37. this.sendPresence();
  38. },
  39. doLeave: function() {
  40. console.log("do leave", this.myroomjid);
  41. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  42. this.presMap.length = 0;
  43. this.connection.send(pres);
  44. },
  45. onPresence: function (pres) {
  46. var from = pres.getAttribute('from');
  47. var type = pres.getAttribute('type');
  48. if (type != null) {
  49. return true;
  50. }
  51. // Parse etherpad tag.
  52. var etherpad = $(pres).find('>etherpad');
  53. if (etherpad.length) {
  54. $(document).trigger('etherpadadded.muc', [from, etherpad.text()]);
  55. }
  56. // Parse prezi tag.
  57. var presentation = $(pres).find('>prezi');
  58. if (presentation.length)
  59. {
  60. var url = presentation.attr('url');
  61. var current = presentation.find('>current').text();
  62. console.log('presentation info received from', from, url);
  63. if (this.preziMap[from] == null) {
  64. this.preziMap[from] = url;
  65. $(document).trigger('presentationadded.muc', [from, url, current]);
  66. }
  67. else {
  68. $(document).trigger('gotoslide.muc', [from, url, current]);
  69. }
  70. }
  71. else if (this.preziMap[from] != null) {
  72. var url = this.preziMap[from];
  73. delete this.preziMap[from];
  74. $(document).trigger('presentationremoved.muc', [from, url]);
  75. }
  76. // Parse audio info tag.
  77. var audioMuted = $(pres).find('>audiomuted');
  78. if (audioMuted.length) {
  79. $(document).trigger('audiomuted.muc', [from, audioMuted.text()]);
  80. }
  81. // Parse video info tag.
  82. var videoMuted = $(pres).find('>videomuted');
  83. if (videoMuted.length) {
  84. $(document).trigger('videomuted.muc', [from, videoMuted.text()]);
  85. }
  86. var stats = $(pres).find('>stats');
  87. if(stats.length)
  88. {
  89. var statsObj = {};
  90. Strophe.forEachChild(stats[0], "stat", function (el) {
  91. statsObj[el.getAttribute("name")] = el.getAttribute("value");
  92. });
  93. ConnectionQuality.updateRemoteStats(from, statsObj);
  94. }
  95. // Parse status.
  96. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  97. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  98. this.isOwner = true;
  99. var create = $iq({type: 'set', to: this.roomjid})
  100. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  101. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  102. this.connection.send(create); // fire away
  103. }
  104. // Parse roles.
  105. var member = {};
  106. member.show = $(pres).find('>show').text();
  107. member.status = $(pres).find('>status').text();
  108. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  109. member.affiliation = tmp.attr('affiliation');
  110. member.role = tmp.attr('role');
  111. var nicktag = $(pres).find('>nick[xmlns="http://jabber.org/protocol/nick"]');
  112. member.displayName = (nicktag.length > 0 ? nicktag.text() : null);
  113. if (from == this.myroomjid) {
  114. if (member.affiliation == 'owner') this.isOwner = true;
  115. if (!this.joined) {
  116. this.joined = true;
  117. $(document).trigger('joined.muc', [from, member]);
  118. this.list_members.push(from);
  119. }
  120. } else if (this.members[from] === undefined) {
  121. // new participant
  122. this.members[from] = member;
  123. this.list_members.push(from);
  124. $(document).trigger('entered.muc', [from, member, pres]);
  125. }
  126. // Always trigger presence to update bindings
  127. console.log('presence change from', from);
  128. $(document).trigger('presence.muc', [from, member, pres]);
  129. // Trigger status message update
  130. if (member.status) {
  131. $(document).trigger('presence.status.muc', [from, member, pres]);
  132. }
  133. return true;
  134. },
  135. onPresenceUnavailable: function (pres) {
  136. var from = pres.getAttribute('from');
  137. // Status code 110 indicates that this notification is "self-presence".
  138. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  139. delete this.members[from];
  140. this.list_members.splice(this.list_members.indexOf(from), 1);
  141. $(document).trigger('left.muc', [from]);
  142. }
  143. // If the status code is 110 this means we're leaving and we would like
  144. // to remove everyone else from our view, so we trigger the event.
  145. else if (this.list_members.length > 1) {
  146. for (var i = 0; i < this.list_members.length; i++) {
  147. var member = this.list_members[i];
  148. delete this.members[i];
  149. this.list_members.splice(i, 1);
  150. $(document).trigger('left.muc', member);
  151. }
  152. }
  153. return true;
  154. },
  155. onPresenceError: function (pres) {
  156. var from = pres.getAttribute('from');
  157. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  158. $(document).trigger('passwordrequired.muc', [from]);
  159. } else if ($(pres).find(
  160. '>error[type="cancel"]>not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  161. var toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  162. if(toDomain === config.hosts.anonymousdomain) {
  163. // we are connected with anonymous domain and only non anonymous users can create rooms
  164. // we must authorize the user
  165. $(document).trigger('passwordrequired.main');
  166. } else {
  167. console.warn('onPresError ', pres);
  168. messageHandler.openReportDialog(null,
  169. 'Oops! Something went wrong and we couldn`t connect to the conference.',
  170. pres);
  171. }
  172. } else {
  173. console.warn('onPresError ', pres);
  174. messageHandler.openReportDialog(null,
  175. 'Oops! Something went wrong and we couldn`t connect to the conference.',
  176. pres);
  177. }
  178. return true;
  179. },
  180. sendMessage: function (body, nickname) {
  181. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  182. msg.c('body', body).up();
  183. if (nickname) {
  184. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  185. }
  186. this.connection.send(msg);
  187. },
  188. setSubject: function (subject){
  189. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  190. msg.c('subject', subject);
  191. this.connection.send(msg);
  192. console.log("topic changed to " + subject);
  193. },
  194. onMessage: function (msg) {
  195. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  196. var from = msg.getAttribute('from');
  197. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  198. var txt = $(msg).find('>body').text();
  199. var type = msg.getAttribute("type");
  200. if(type == "error")
  201. {
  202. Chat.chatAddError($(msg).find('>text').text(), txt);
  203. return true;
  204. }
  205. var subject = $(msg).find('>subject');
  206. if(subject.length)
  207. {
  208. var subjectText = subject.text();
  209. if(subjectText || subjectText == "") {
  210. Chat.chatSetSubject(subjectText);
  211. console.log("Subject is changed to " + subjectText);
  212. }
  213. }
  214. if (txt) {
  215. console.log('chat', nick, txt);
  216. Chat.updateChatConversation(from, nick, txt);
  217. }
  218. return true;
  219. },
  220. lockRoom: function (key) {
  221. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  222. var ob = this;
  223. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  224. function (res) {
  225. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  226. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  227. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  228. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  229. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  230. // Fixes a bug in prosody 0.9.+ https://code.google.com/p/lxmppd/issues/detail?id=373
  231. formsubmit.c('field', {'var': 'muc#roomconfig_whois'}).c('value').t('anyone').up().up();
  232. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  233. this.connection.sendIQ(formsubmit,
  234. function (res) {
  235. console.log('set room password');
  236. },
  237. function (err) {
  238. console.warn('setting password failed', err);
  239. messageHandler.showError('Lock failed',
  240. 'Failed to lock conference.',
  241. err);
  242. }
  243. );
  244. } else {
  245. console.warn('room passwords not supported');
  246. messageHandler.showError('Warning',
  247. 'Room passwords are currently not supported.');
  248. }
  249. },
  250. function (err) {
  251. console.warn('setting password failed', err);
  252. messageHandler.showError('Lock failed',
  253. 'Failed to lock conference.',
  254. err);
  255. }
  256. );
  257. },
  258. kick: function (jid) {
  259. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  260. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  261. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  262. .c('reason').t('You have been kicked.').up().up().up();
  263. this.connection.sendIQ(
  264. kickIQ,
  265. function (result) {
  266. console.log('Kick participant with jid: ', jid, result);
  267. },
  268. function (error) {
  269. console.log('Kick participant error: ', error);
  270. });
  271. },
  272. sendPresence: function () {
  273. var pres = $pres({to: this.presMap['to'] });
  274. pres.c('x', {xmlns: this.presMap['xns']});
  275. if (this.presMap['password']) {
  276. pres.c('password').t(this.presMap['password']).up();
  277. }
  278. pres.up();
  279. // Send XEP-0115 'c' stanza that contains our capabilities info
  280. if (connection.caps) {
  281. connection.caps.node = config.clientNode;
  282. pres.c('c', connection.caps.generateCapsAttrs()).up();
  283. }
  284. if(this.presMap['bridgeIsDown']) {
  285. pres.c('bridgeIsDown').up();
  286. }
  287. if (this.presMap['displayName']) {
  288. // XEP-0172
  289. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
  290. .t(this.presMap['displayName']).up();
  291. }
  292. if (this.presMap['audions']) {
  293. pres.c('audiomuted', {xmlns: this.presMap['audions']})
  294. .t(this.presMap['audiomuted']).up();
  295. }
  296. if (this.presMap['videons']) {
  297. pres.c('videomuted', {xmlns: this.presMap['videons']})
  298. .t(this.presMap['videomuted']).up();
  299. }
  300. if(this.presMap['statsns'])
  301. {
  302. var stats = pres.c('stats', {xmlns: this.presMap['statsns']});
  303. for(var stat in this.presMap["stats"])
  304. if(this.presMap["stats"][stat] != null)
  305. stats.c("stat",{name: stat, value: this.presMap["stats"][stat]}).up();
  306. pres.up();
  307. }
  308. if (this.presMap['prezins']) {
  309. pres.c('prezi',
  310. {xmlns: this.presMap['prezins'],
  311. 'url': this.presMap['preziurl']})
  312. .c('current').t(this.presMap['prezicurrent']).up().up();
  313. }
  314. if (this.presMap['etherpadns']) {
  315. pres.c('etherpad', {xmlns: this.presMap['etherpadns']})
  316. .t(this.presMap['etherpadname']).up();
  317. }
  318. if (this.presMap['medians'])
  319. {
  320. pres.c('media', {xmlns: this.presMap['medians']});
  321. var sourceNumber = 0;
  322. Object.keys(this.presMap).forEach(function (key) {
  323. if (key.indexOf('source') >= 0) {
  324. sourceNumber++;
  325. }
  326. });
  327. if (sourceNumber > 0)
  328. for (var i = 1; i <= sourceNumber/3; i ++) {
  329. pres.c('source',
  330. {type: this.presMap['source' + i + '_type'],
  331. ssrc: this.presMap['source' + i + '_ssrc'],
  332. direction: this.presMap['source'+ i + '_direction']
  333. || 'sendrecv' }
  334. ).up();
  335. }
  336. }
  337. pres.up();
  338. connection.send(pres);
  339. },
  340. addDisplayNameToPresence: function (displayName) {
  341. this.presMap['displayName'] = displayName;
  342. },
  343. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  344. if (!this.presMap['medians'])
  345. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  346. this.presMap['source' + sourceNumber + '_type'] = mtype;
  347. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  348. this.presMap['source' + sourceNumber + '_direction'] = direction;
  349. },
  350. clearPresenceMedia: function () {
  351. var self = this;
  352. Object.keys(this.presMap).forEach( function(key) {
  353. if(key.indexOf('source') != -1) {
  354. delete self.presMap[key];
  355. }
  356. });
  357. },
  358. addPreziToPresence: function (url, currentSlide) {
  359. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  360. this.presMap['preziurl'] = url;
  361. this.presMap['prezicurrent'] = currentSlide;
  362. },
  363. removePreziFromPresence: function () {
  364. delete this.presMap['prezins'];
  365. delete this.presMap['preziurl'];
  366. delete this.presMap['prezicurrent'];
  367. },
  368. addCurrentSlideToPresence: function (currentSlide) {
  369. this.presMap['prezicurrent'] = currentSlide;
  370. },
  371. getPrezi: function (roomjid) {
  372. return this.preziMap[roomjid];
  373. },
  374. addEtherpadToPresence: function(etherpadName) {
  375. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  376. this.presMap['etherpadname'] = etherpadName;
  377. },
  378. addAudioInfoToPresence: function(isMuted) {
  379. this.presMap['audions'] = 'http://jitsi.org/jitmeet/audio';
  380. this.presMap['audiomuted'] = isMuted.toString();
  381. },
  382. addVideoInfoToPresence: function(isMuted) {
  383. this.presMap['videons'] = 'http://jitsi.org/jitmeet/video';
  384. this.presMap['videomuted'] = isMuted.toString();
  385. },
  386. addConnectionInfoToPresence: function(stats) {
  387. this.presMap['statsns'] = 'http://jitsi.org/jitmeet/stats';
  388. this.presMap['stats'] = stats;
  389. },
  390. findJidFromResource: function(resourceJid) {
  391. var peerJid = null;
  392. Object.keys(this.members).some(function (jid) {
  393. peerJid = jid;
  394. return Strophe.getResourceFromJid(jid) === resourceJid;
  395. });
  396. return peerJid;
  397. },
  398. addBridgeIsDownToPresence: function() {
  399. this.presMap['bridgeIsDown'] = true;
  400. }
  401. });