Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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