Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LoginDialog.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* global $, APP, config*/
  2. var XMPP = require('../../xmpp/xmpp');
  3. var Moderator = require('../../xmpp/moderator');
  4. //FIXME: use LoginDialog to add retries to XMPP.connect method used when
  5. // anonymous domain is not enabled
  6. /**
  7. * Creates new <tt>Dialog</tt> instance.
  8. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt> called
  9. * when we either fail to connect or succeed(check Strophe.Status).
  10. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to Jicofo
  11. * in order to create session-id after the connection is established.
  12. * @constructor
  13. */
  14. function Dialog(callback, obtainSession) {
  15. var self = this;
  16. var stop = false;
  17. var connection = APP.xmpp.createConnection();
  18. var message = '<h2 data-i18n="dialog.passwordRequired">';
  19. message += APP.translation.translateString("dialog.passwordRequired");
  20. // ++__authdomain__
  21. message += '</h2>' +
  22. '<input name="username" type="text" ';
  23. if (config.hosts.authdomain) {
  24. message += 'placeholder="user identity" autofocus>';
  25. } else {
  26. message += 'placeholder="user@domain.net" autofocus>';
  27. }
  28. message += '<input name="password" ' +
  29. 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
  30. ' placeholder="user password">';
  31. // --__authdomain__
  32. var okButton = APP.translation.generateTranslationHTML("dialog.Ok");
  33. var cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel");
  34. var states = {
  35. login: {
  36. html: message,
  37. buttons: [
  38. { title: okButton, value: true},
  39. { title: cancelButton, value: false}
  40. ],
  41. focus: ':input:first',
  42. submit: function (e, v, m, f) {
  43. e.preventDefault();
  44. if (v) {
  45. var jid = f.username;
  46. var password = f.password;
  47. if (jid && password) {
  48. stop = false;
  49. //++__authdomain__ complete jid by appending @<domain> if no @ was found in jid
  50. if (jid.indexOf("@") < 0) {
  51. jid = jid.concat('@');
  52. if (config.hosts.authdomain) {
  53. jid += config.hosts.authdomain;
  54. } else {
  55. jid += config.hosts.domain;
  56. }
  57. }
  58. // --__authdomain__
  59. connection.reset();
  60. connDialog.goToState('connecting');
  61. connection.connect(jid, password, stateHandler);
  62. }
  63. } else {
  64. // User cancelled
  65. stop = true;
  66. callback();
  67. }
  68. }
  69. },
  70. connecting: {
  71. title: APP.translation.translateString('dialog.connecting'),
  72. html: '<div id="connectionStatus"></div>',
  73. buttons: [],
  74. defaultButton: 0
  75. },
  76. finished: {
  77. title: APP.translation.translateString('dialog.error'),
  78. html: '<div id="errorMessage"></div>',
  79. buttons: [
  80. {
  81. title: APP.translation.translateString('dialog.retry'),
  82. value: 'retry'
  83. },
  84. {
  85. title: APP.translation.translateString('dialog.Cancel'),
  86. value: 'cancel'
  87. },
  88. ],
  89. defaultButton: 0,
  90. submit: function (e, v, m, f) {
  91. e.preventDefault();
  92. if (v === 'retry')
  93. connDialog.goToState('login');
  94. else
  95. callback();
  96. }
  97. }
  98. };
  99. var connDialog
  100. = APP.UI.messageHandler.openDialogWithStates(states,
  101. { persistent: true, closeText: '' }, null);
  102. var stateHandler = function (status, message) {
  103. if (stop) {
  104. return;
  105. }
  106. var translateKey = "connection." + XMPP.getStatusString(status);
  107. var statusStr = APP.translation.translateString(translateKey);
  108. // Display current state
  109. var connectionStatus =
  110. connDialog.getState('connecting').find('#connectionStatus');
  111. connectionStatus.text(statusStr);
  112. switch (status) {
  113. case XMPP.Status.CONNECTED:
  114. stop = true;
  115. if (!obtainSession) {
  116. callback(connection, status);
  117. return;
  118. }
  119. // Obtaining session-id status
  120. connectionStatus.text(
  121. APP.translation.translateString(
  122. 'connection.FETCH_SESSION_ID'));
  123. // Authenticate with Jicofo and obtain session-id
  124. var roomName = APP.UI.generateRoomName();
  125. // Jicofo will return new session-id when connected
  126. // from authenticated domain
  127. connection.sendIQ(
  128. Moderator.createConferenceIq(roomName),
  129. function (result) {
  130. connectionStatus.text(
  131. APP.translation.translateString(
  132. 'connection.GOT_SESSION_ID'));
  133. stop = true;
  134. // Parse session-id
  135. Moderator.parseSessionId(result);
  136. callback(connection, status);
  137. },
  138. function (error) {
  139. console.error("Auth on the fly failed", error);
  140. stop = true;
  141. var errorMsg =
  142. APP.translation.translateString(
  143. 'connection.GET_SESSION_ID_ERROR') +
  144. $(error).find('>error').attr('code');
  145. self.displayError(errorMsg);
  146. connection.disconnect();
  147. });
  148. break;
  149. case XMPP.Status.AUTHFAIL:
  150. case XMPP.Status.CONNFAIL:
  151. case XMPP.Status.DISCONNECTED:
  152. stop = true;
  153. callback(connection, status);
  154. var errorMessage = statusStr;
  155. if (message)
  156. {
  157. errorMessage += ': ' + message;
  158. }
  159. self.displayError(errorMessage);
  160. break;
  161. default:
  162. break;
  163. }
  164. };
  165. /**
  166. * Displays error message in 'finished' state which allows either to cancel
  167. * or retry.
  168. * @param message the final message to be displayed.
  169. */
  170. this.displayError = function (message) {
  171. var finishedState = connDialog.getState('finished');
  172. var errorMessageElem = finishedState.find('#errorMessage');
  173. errorMessageElem.text(message);
  174. connDialog.goToState('finished');
  175. };
  176. /**
  177. * Closes LoginDialog.
  178. */
  179. this.close = function () {
  180. stop = true;
  181. connDialog.close();
  182. };
  183. }
  184. var LoginDialog = {
  185. /**
  186. * Displays login prompt used to establish new XMPP connection. Given
  187. * <tt>callback(Strophe.Connection, Strophe.Status)</tt> function will be
  188. * called when we connect successfully(status === CONNECTED) or when we fail
  189. * to do so. On connection failure program can call Dialog.close() method in
  190. * order to cancel or do nothing to let the user retry.
  191. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt>
  192. * called when we either fail to connect or succeed(check
  193. * Strophe.Status).
  194. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to
  195. * Jicofo in order to create session-id after the connection is
  196. * established.
  197. * @returns {Dialog}
  198. */
  199. show: function (callback, obtainSession) {
  200. return new Dialog(callback, obtainSession);
  201. }
  202. };
  203. module.exports = LoginDialog;