2014-07-01 13:49:19 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
2014-09-05 13:02:02 -04:00
|
|
|
var ENCRYPTION_METHOD_NONE = 0;
|
|
|
|
var ENCRYPTION_METHOD_STARTTLS = 1;
|
|
|
|
var ENCRYPTION_METHOD_TLS = 2;
|
|
|
|
|
2014-07-01 13:49:19 -04:00
|
|
|
var appCtrl = require('js/app-controller'),
|
2014-09-20 14:47:32 -04:00
|
|
|
config = require('js/app-config').config;
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
var SetCredentialsCtrl = function($scope, $location, $routeParams) {
|
2014-09-20 14:47:32 -04:00
|
|
|
if (!appCtrl._auth && !$routeParams.dev) {
|
2014-08-13 10:16:28 -04:00
|
|
|
$location.path('/'); // init app
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:49:19 -04:00
|
|
|
var auth = appCtrl._auth;
|
2014-09-20 14:47:32 -04:00
|
|
|
var doctor = appCtrl._doctor;
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
//
|
|
|
|
// Presets and Settings
|
|
|
|
//
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
var provider = $location.search().provider;
|
2014-07-01 13:49:19 -04:00
|
|
|
$scope.hasProviderPreset = !!config[provider];
|
|
|
|
$scope.useOAuth = !!auth.oauthToken;
|
|
|
|
$scope.showDetails = (provider === 'custom');
|
|
|
|
|
|
|
|
if ($scope.useOAuth) {
|
|
|
|
$scope.emailAddress = auth.emailAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.hasProviderPreset) {
|
2014-09-20 14:47:32 -04:00
|
|
|
// use non-editable presets
|
|
|
|
|
|
|
|
// SMTP config
|
2014-07-01 13:49:19 -04:00
|
|
|
$scope.smtpHost = config[provider].smtp.host;
|
|
|
|
$scope.smtpPort = config[provider].smtp.port;
|
|
|
|
$scope.smtpCert = config[provider].smtp.ca;
|
|
|
|
$scope.smtpPinned = config[provider].smtp.pinned;
|
2014-09-05 13:02:02 -04:00
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
// transport encryption method
|
2014-09-05 13:02:02 -04:00
|
|
|
if (config[provider].smtp.secure && !config[provider].smtp.ignoreTLS) {
|
|
|
|
$scope.smtpEncryption = ENCRYPTION_METHOD_TLS;
|
|
|
|
} else if (!config[provider].smtp.secure && !config[provider].smtp.ignoreTLS) {
|
|
|
|
$scope.smtpEncryption = ENCRYPTION_METHOD_STARTTLS;
|
|
|
|
} else {
|
|
|
|
$scope.smtpEncryption = ENCRYPTION_METHOD_NONE;
|
|
|
|
}
|
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
// IMAP config
|
2014-07-01 13:49:19 -04:00
|
|
|
$scope.imapHost = config[provider].imap.host;
|
|
|
|
$scope.imapPort = config[provider].imap.port;
|
|
|
|
$scope.imapCert = config[provider].imap.ca;
|
|
|
|
$scope.imapPinned = config[provider].imap.pinned;
|
2014-09-05 13:02:02 -04:00
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
// transport encryption method
|
2014-09-05 13:02:02 -04:00
|
|
|
if (config[provider].imap.secure && !config[provider].imap.ignoreTLS) {
|
|
|
|
$scope.imapEncryption = ENCRYPTION_METHOD_TLS;
|
|
|
|
} else if (!config[provider].imap.secure && !config[provider].imap.ignoreTLS) {
|
|
|
|
$scope.imapEncryption = ENCRYPTION_METHOD_STARTTLS;
|
|
|
|
} else {
|
|
|
|
$scope.imapEncryption = ENCRYPTION_METHOD_NONE;
|
|
|
|
}
|
2014-07-01 13:49:19 -04:00
|
|
|
}
|
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
$scope.test = function() {
|
|
|
|
// parse the <select> dropdown lists
|
2014-09-05 13:02:02 -04:00
|
|
|
var imapEncryption = parseInt($scope.imapEncryption, 10);
|
|
|
|
var smtpEncryption = parseInt($scope.smtpEncryption, 10);
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
// build credentials object
|
|
|
|
var credentials = {
|
2014-07-01 13:49:19 -04:00
|
|
|
provider: provider,
|
|
|
|
emailAddress: $scope.emailAddress,
|
|
|
|
username: $scope.username || $scope.emailAddress,
|
|
|
|
realname: $scope.realname,
|
|
|
|
password: $scope.password,
|
2014-09-20 14:47:32 -04:00
|
|
|
xoauth2: auth.oauthToken,
|
2014-07-01 13:49:19 -04:00
|
|
|
imap: {
|
|
|
|
host: $scope.imapHost.toLowerCase(),
|
|
|
|
port: $scope.imapPort,
|
2014-09-05 13:02:02 -04:00
|
|
|
secure: imapEncryption === ENCRYPTION_METHOD_TLS,
|
|
|
|
ignoreTLS: imapEncryption === ENCRYPTION_METHOD_NONE,
|
2014-07-01 13:49:19 -04:00
|
|
|
ca: $scope.imapCert,
|
|
|
|
pinned: !!$scope.imapPinned
|
|
|
|
},
|
|
|
|
smtp: {
|
|
|
|
host: $scope.smtpHost.toLowerCase(),
|
|
|
|
port: $scope.smtpPort,
|
2014-09-05 13:02:02 -04:00
|
|
|
secure: smtpEncryption === ENCRYPTION_METHOD_TLS,
|
|
|
|
ignoreTLS: smtpEncryption === ENCRYPTION_METHOD_NONE,
|
2014-07-01 13:49:19 -04:00
|
|
|
ca: $scope.smtpCert,
|
|
|
|
pinned: !!$scope.smtpPinned
|
|
|
|
}
|
2014-09-20 14:47:32 -04:00
|
|
|
};
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-09-20 14:47:32 -04:00
|
|
|
// use the credentials in the connection doctor
|
|
|
|
doctor.configure(credentials);
|
|
|
|
|
|
|
|
// run connection doctor test suite
|
|
|
|
$scope.busy = true;
|
|
|
|
doctor.check(function(err) {
|
|
|
|
if (err) {
|
|
|
|
// display the error in the settings UI
|
|
|
|
$scope.connectionError = err;
|
|
|
|
} else {
|
|
|
|
// persists the credentials and forwards to /login
|
|
|
|
auth.setCredentials(credentials);
|
|
|
|
$location.path('/login');
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.busy = false;
|
|
|
|
$scope.$apply();
|
|
|
|
});
|
|
|
|
};
|
2014-07-01 13:49:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return SetCredentialsCtrl;
|
|
|
|
});
|