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.

moderator.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* global $, $iq, APP, config, messageHandler,
  2. roomName, sessionTerminated, Strophe, Util */
  3. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  4. var Settings = require("../settings/Settings");
  5. var AuthenticationEvents
  6. = require("../../service/authentication/AuthenticationEvents");
  7. /**
  8. * Contains logic responsible for enabling/disabling functionality available
  9. * only to moderator users.
  10. */
  11. var connection = null;
  12. var focusUserJid;
  13. function createExpBackoffTimer(step) {
  14. var count = 1;
  15. return function (reset) {
  16. // Reset call
  17. if (reset) {
  18. count = 1;
  19. return;
  20. }
  21. // Calculate next timeout
  22. var timeout = Math.pow(2, count - 1);
  23. count += 1;
  24. return timeout * step;
  25. };
  26. }
  27. var getNextTimeout = createExpBackoffTimer(1000);
  28. var getNextErrorTimeout = createExpBackoffTimer(1000);
  29. // External authentication stuff
  30. var externalAuthEnabled = false;
  31. // Sip gateway can be enabled by configuring Jigasi host in config.js or
  32. // it will be enabled automatically if focus detects the component through
  33. // service discovery.
  34. var sipGatewayEnabled;
  35. var eventEmitter = null;
  36. var Moderator = {
  37. isModerator: function () {
  38. return connection && connection.emuc.isModerator();
  39. },
  40. isPeerModerator: function (peerJid) {
  41. return connection &&
  42. connection.emuc.getMemberRole(peerJid) === 'moderator';
  43. },
  44. isExternalAuthEnabled: function () {
  45. return externalAuthEnabled;
  46. },
  47. isSipGatewayEnabled: function () {
  48. return sipGatewayEnabled;
  49. },
  50. setConnection: function (con) {
  51. connection = con;
  52. },
  53. init: function (xmpp, emitter) {
  54. this.xmppService = xmpp;
  55. eventEmitter = emitter;
  56. sipGatewayEnabled =
  57. config.hosts && config.hosts.call_control !== undefined;
  58. // Message listener that talks to POPUP window
  59. function listener(event) {
  60. if (event.data && event.data.sessionId) {
  61. if (event.origin !== window.location.origin) {
  62. console.warn("Ignoring sessionId from different origin: " +
  63. event.origin);
  64. return;
  65. }
  66. localStorage.setItem('sessionId', event.data.sessionId);
  67. // After popup is closed we will authenticate
  68. }
  69. }
  70. // Register
  71. if (window.addEventListener) {
  72. window.addEventListener("message", listener, false);
  73. } else {
  74. window.attachEvent("onmessage", listener);
  75. }
  76. },
  77. onMucMemberLeft: function (jid) {
  78. console.info("Someone left is it focus ? " + jid);
  79. var resource = Strophe.getResourceFromJid(jid);
  80. if (resource === 'focus' && !this.xmppService.sessionTerminated) {
  81. console.info(
  82. "Focus has left the room - leaving conference");
  83. //hangUp();
  84. // We'd rather reload to have everything re-initialized
  85. // FIXME: show some message before reload
  86. location.reload();
  87. }
  88. },
  89. setFocusUserJid: function (focusJid) {
  90. if (!focusUserJid) {
  91. focusUserJid = focusJid;
  92. console.info("Focus jid set to: " + focusUserJid);
  93. }
  94. },
  95. getFocusUserJid: function () {
  96. return focusUserJid;
  97. },
  98. getFocusComponent: function () {
  99. // Get focus component address
  100. var focusComponent = config.hosts.focus;
  101. // If not specified use default: 'focus.domain'
  102. if (!focusComponent) {
  103. focusComponent = 'focus.' + config.hosts.domain;
  104. }
  105. return focusComponent;
  106. },
  107. createConferenceIq: function (roomName) {
  108. // Generate create conference IQ
  109. var elem = $iq({to: Moderator.getFocusComponent(), type: 'set'});
  110. // Session Id used for authentication
  111. var sessionId = localStorage.getItem('sessionId');
  112. var machineUID = Settings.getSettings().uid;
  113. console.info(
  114. "Session ID: " + sessionId + " machine UID: " + machineUID);
  115. elem.c('conference', {
  116. xmlns: 'http://jitsi.org/protocol/focus',
  117. room: roomName,
  118. 'machine-uid': machineUID
  119. });
  120. if (sessionId) {
  121. elem.attrs({ 'session-id': sessionId});
  122. }
  123. if (config.hosts.bridge !== undefined) {
  124. elem.c(
  125. 'property',
  126. { name: 'bridge', value: config.hosts.bridge})
  127. .up();
  128. }
  129. // Tell the focus we have Jigasi configured
  130. if (config.hosts.call_control !== undefined) {
  131. elem.c(
  132. 'property',
  133. { name: 'call_control', value: config.hosts.call_control})
  134. .up();
  135. }
  136. if (config.channelLastN !== undefined) {
  137. elem.c(
  138. 'property',
  139. { name: 'channelLastN', value: config.channelLastN})
  140. .up();
  141. }
  142. if (config.adaptiveLastN !== undefined) {
  143. elem.c(
  144. 'property',
  145. { name: 'adaptiveLastN', value: config.adaptiveLastN})
  146. .up();
  147. }
  148. if (config.adaptiveSimulcast !== undefined) {
  149. elem.c(
  150. 'property',
  151. { name: 'adaptiveSimulcast', value: config.adaptiveSimulcast})
  152. .up();
  153. }
  154. if (config.openSctp !== undefined) {
  155. elem.c(
  156. 'property',
  157. { name: 'openSctp', value: config.openSctp})
  158. .up();
  159. }
  160. if(config.startAudioMuted !== undefined)
  161. {
  162. elem.c(
  163. 'property',
  164. { name: 'startAudioMuted', value: config.startAudioMuted})
  165. .up();
  166. }
  167. if(config.startVideoMuted !== undefined)
  168. {
  169. elem.c(
  170. 'property',
  171. { name: 'startVideoMuted', value: config.startVideoMuted})
  172. .up();
  173. }
  174. elem.c(
  175. 'property',
  176. { name: 'simulcastMode', value: 'rewriting'})
  177. .up();
  178. elem.up();
  179. return elem;
  180. },
  181. parseSessionId: function (resultIq) {
  182. var sessionId = $(resultIq).find('conference').attr('session-id');
  183. if (sessionId) {
  184. console.info('Received sessionId: ' + sessionId);
  185. localStorage.setItem('sessionId', sessionId);
  186. }
  187. },
  188. parseConfigOptions: function (resultIq) {
  189. Moderator.setFocusUserJid(
  190. $(resultIq).find('conference').attr('focusjid'));
  191. var authenticationEnabled
  192. = $(resultIq).find(
  193. '>conference>property' +
  194. '[name=\'authentication\'][value=\'true\']').length > 0;
  195. console.info("Authentication enabled: " + authenticationEnabled);
  196. externalAuthEnabled = $(resultIq).find(
  197. '>conference>property' +
  198. '[name=\'externalAuth\'][value=\'true\']').length > 0;
  199. console.info('External authentication enabled: ' + externalAuthEnabled);
  200. if (!externalAuthEnabled) {
  201. // We expect to receive sessionId in 'internal' authentication mode
  202. Moderator.parseSessionId(resultIq);
  203. }
  204. var authIdentity = $(resultIq).find('>conference').attr('identity');
  205. eventEmitter.emit(AuthenticationEvents.IDENTITY_UPDATED,
  206. authenticationEnabled, authIdentity);
  207. // Check if focus has auto-detected Jigasi component(this will be also
  208. // included if we have passed our host from the config)
  209. if ($(resultIq).find(
  210. '>conference>property' +
  211. '[name=\'sipGatewayEnabled\'][value=\'true\']').length) {
  212. sipGatewayEnabled = true;
  213. }
  214. console.info("Sip gateway enabled: " + sipGatewayEnabled);
  215. },
  216. // FIXME: we need to show the fact that we're waiting for the focus
  217. // to the user(or that focus is not available)
  218. allocateConferenceFocus: function (roomName, callback) {
  219. // Try to use focus user JID from the config
  220. Moderator.setFocusUserJid(config.focusUserJid);
  221. // Send create conference IQ
  222. var iq = Moderator.createConferenceIq(roomName);
  223. var self = this;
  224. connection.sendIQ(
  225. iq,
  226. function (result) {
  227. // Setup config options
  228. Moderator.parseConfigOptions(result);
  229. if ('true' === $(result).find('conference').attr('ready')) {
  230. // Reset both timers
  231. getNextTimeout(true);
  232. getNextErrorTimeout(true);
  233. // Exec callback
  234. callback();
  235. } else {
  236. var waitMs = getNextTimeout();
  237. console.info("Waiting for the focus... " + waitMs);
  238. // Reset error timeout
  239. getNextErrorTimeout(true);
  240. window.setTimeout(
  241. function () {
  242. Moderator.allocateConferenceFocus(
  243. roomName, callback);
  244. }, waitMs);
  245. }
  246. },
  247. function (error) {
  248. // Invalid session ? remove and try again
  249. // without session ID to get a new one
  250. var invalidSession
  251. = $(error).find('>error>session-invalid').length;
  252. if (invalidSession) {
  253. console.info("Session expired! - removing");
  254. localStorage.removeItem("sessionId");
  255. }
  256. if ($(error).find('>error>graceful-shutdown').length) {
  257. eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
  258. return;
  259. }
  260. // Check for error returned by the reservation system
  261. var reservationErr = $(error).find('>error>reservation-error');
  262. if (reservationErr.length) {
  263. // Trigger error event
  264. var errorCode = reservationErr.attr('error-code');
  265. var errorMsg;
  266. if ($(error).find('>error>text')) {
  267. errorMsg = $(error).find('>error>text').text();
  268. }
  269. eventEmitter.emit(
  270. XMPPEvents.RESERVATION_ERROR, errorCode, errorMsg);
  271. return;
  272. }
  273. // Not authorized to create new room
  274. if ($(error).find('>error>not-authorized').length) {
  275. console.warn("Unauthorized to start the conference", error);
  276. var toDomain
  277. = Strophe.getDomainFromJid(error.getAttribute('to'));
  278. if (toDomain !== config.hosts.anonymousdomain) {
  279. // FIXME: "is external" should come either from
  280. // the focus or config.js
  281. externalAuthEnabled = true;
  282. }
  283. eventEmitter.emit(
  284. XMPPEvents.AUTHENTICATION_REQUIRED,
  285. function () {
  286. Moderator.allocateConferenceFocus(
  287. roomName, callback);
  288. });
  289. return;
  290. }
  291. var waitMs = getNextErrorTimeout();
  292. console.error("Focus error, retry after " + waitMs, error);
  293. // Show message
  294. var focusComponent = Moderator.getFocusComponent();
  295. var retrySec = waitMs / 1000;
  296. // FIXME: message is duplicated ?
  297. // Do not show in case of session invalid
  298. // which means just a retry
  299. if (!invalidSession) {
  300. eventEmitter.emit(XMPPEvents.FOCUS_DISCONNECTED,
  301. focusComponent, retrySec);
  302. }
  303. // Reset response timeout
  304. getNextTimeout(true);
  305. window.setTimeout(
  306. function () {
  307. Moderator.allocateConferenceFocus(roomName, callback);
  308. }, waitMs);
  309. }
  310. );
  311. },
  312. getLoginUrl: function (roomName, urlCallback) {
  313. var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
  314. iq.c('login-url', {
  315. xmlns: 'http://jitsi.org/protocol/focus',
  316. room: roomName,
  317. 'machine-uid': Settings.getSettings().uid
  318. });
  319. connection.sendIQ(
  320. iq,
  321. function (result) {
  322. var url = $(result).find('login-url').attr('url');
  323. url = url = decodeURIComponent(url);
  324. if (url) {
  325. console.info("Got auth url: " + url);
  326. urlCallback(url);
  327. } else {
  328. console.error(
  329. "Failed to get auth url from the focus", result);
  330. }
  331. },
  332. function (error) {
  333. console.error("Get auth url error", error);
  334. }
  335. );
  336. },
  337. getPopupLoginUrl: function (roomName, urlCallback) {
  338. var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
  339. iq.c('login-url', {
  340. xmlns: 'http://jitsi.org/protocol/focus',
  341. room: roomName,
  342. 'machine-uid': Settings.getSettings().uid,
  343. popup: true
  344. });
  345. connection.sendIQ(
  346. iq,
  347. function (result) {
  348. var url = $(result).find('login-url').attr('url');
  349. url = url = decodeURIComponent(url);
  350. if (url) {
  351. console.info("Got POPUP auth url: " + url);
  352. urlCallback(url);
  353. } else {
  354. console.error(
  355. "Failed to get POPUP auth url from the focus", result);
  356. }
  357. },
  358. function (error) {
  359. console.error('Get POPUP auth url error', error);
  360. }
  361. );
  362. },
  363. logout: function (callback) {
  364. var iq = $iq({to: Moderator.getFocusComponent(), type: 'set'});
  365. var sessionId = localStorage.getItem('sessionId');
  366. if (!sessionId) {
  367. callback();
  368. return;
  369. }
  370. iq.c('logout', {
  371. xmlns: 'http://jitsi.org/protocol/focus',
  372. 'session-id': sessionId
  373. });
  374. connection.sendIQ(
  375. iq,
  376. function (result) {
  377. var logoutUrl = $(result).find('logout').attr('logout-url');
  378. if (logoutUrl) {
  379. logoutUrl = decodeURIComponent(logoutUrl);
  380. }
  381. console.info("Log out OK, url: " + logoutUrl, result);
  382. localStorage.removeItem('sessionId');
  383. callback(logoutUrl);
  384. },
  385. function (error) {
  386. console.error("Logout error", error);
  387. }
  388. );
  389. }
  390. };
  391. module.exports = Moderator;