2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ENCRYPTION_METHOD_NONE = 0;
|
|
|
|
var ENCRYPTION_METHOD_STARTTLS = 1;
|
|
|
|
var ENCRYPTION_METHOD_TLS = 2;
|
|
|
|
|
2014-11-10 12:36:26 -05:00
|
|
|
var appCtrl = require('../app-controller');
|
2014-10-02 16:05:44 -04:00
|
|
|
|
|
|
|
var SetCredentialsCtrl = function($scope, $location, $routeParams) {
|
|
|
|
if (!appCtrl._auth && !$routeParams.dev) {
|
|
|
|
$location.path('/'); // init app
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var auth = appCtrl._auth;
|
|
|
|
var doctor = appCtrl._doctor;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Presets and Settings
|
|
|
|
//
|
|
|
|
|
2014-11-11 13:30:58 -05:00
|
|
|
var mailConfig = $scope.state.login.mailConfig;
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.useOAuth = !!auth.oauthToken;
|
2014-11-11 13:30:58 -05:00
|
|
|
$scope.showDetails = (mailConfig.imap.source === 'guess');
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-08 07:02:24 -05:00
|
|
|
// set email address
|
2014-10-02 16:05:44 -04:00
|
|
|
if ($scope.useOAuth) {
|
|
|
|
$scope.emailAddress = auth.emailAddress;
|
2014-11-11 13:30:58 -05:00
|
|
|
} else {
|
2014-11-08 07:02:24 -05:00
|
|
|
$scope.emailAddress = $scope.state.login.emailAddress;
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
2014-11-11 13:30:58 -05:00
|
|
|
// SMTP config
|
|
|
|
$scope.smtpHost = mailConfig.smtp.hostname;
|
|
|
|
$scope.smtpPort = parseInt(mailConfig.smtp.port, 10);
|
|
|
|
|
|
|
|
// transport encryption method
|
|
|
|
if (mailConfig.smtp.secure && !mailConfig.smtp.ignoreTLS) {
|
|
|
|
$scope.smtpEncryption = ENCRYPTION_METHOD_TLS;
|
|
|
|
} else if (!mailConfig.smtp.secure && !mailConfig.smtp.ignoreTLS) {
|
|
|
|
$scope.smtpEncryption = ENCRYPTION_METHOD_STARTTLS;
|
|
|
|
} else {
|
|
|
|
$scope.smtpEncryption = ENCRYPTION_METHOD_NONE;
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
2014-11-11 13:30:58 -05:00
|
|
|
// IMAP config
|
|
|
|
$scope.imapHost = mailConfig.imap.hostname;
|
|
|
|
$scope.imapPort = parseInt(mailConfig.imap.port, 10);
|
|
|
|
|
|
|
|
// transport encryption method
|
|
|
|
if (mailConfig.imap.secure && !mailConfig.imap.ignoreTLS) {
|
|
|
|
$scope.imapEncryption = ENCRYPTION_METHOD_TLS;
|
|
|
|
} else if (!mailConfig.imap.secure && !mailConfig.imap.ignoreTLS) {
|
|
|
|
$scope.imapEncryption = ENCRYPTION_METHOD_STARTTLS;
|
|
|
|
} else {
|
|
|
|
$scope.imapEncryption = ENCRYPTION_METHOD_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Scope functions
|
|
|
|
//
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.test = function() {
|
|
|
|
// parse the <select> dropdown lists
|
|
|
|
var imapEncryption = parseInt($scope.imapEncryption, 10);
|
|
|
|
var smtpEncryption = parseInt($scope.smtpEncryption, 10);
|
|
|
|
|
|
|
|
// build credentials object
|
|
|
|
var credentials = {
|
|
|
|
emailAddress: $scope.emailAddress,
|
|
|
|
username: $scope.username || $scope.emailAddress,
|
|
|
|
realname: $scope.realname,
|
|
|
|
password: $scope.password,
|
|
|
|
xoauth2: auth.oauthToken,
|
|
|
|
imap: {
|
|
|
|
host: $scope.imapHost.toLowerCase(),
|
|
|
|
port: $scope.imapPort,
|
|
|
|
secure: imapEncryption === ENCRYPTION_METHOD_TLS,
|
2014-11-11 13:30:58 -05:00
|
|
|
ignoreTLS: imapEncryption === ENCRYPTION_METHOD_NONE
|
2014-10-02 16:05:44 -04:00
|
|
|
},
|
|
|
|
smtp: {
|
|
|
|
host: $scope.smtpHost.toLowerCase(),
|
|
|
|
port: $scope.smtpPort,
|
|
|
|
secure: smtpEncryption === ENCRYPTION_METHOD_TLS,
|
2014-11-11 13:30:58 -05:00
|
|
|
ignoreTLS: smtpEncryption === ENCRYPTION_METHOD_NONE
|
2014-09-05 13:02:02 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-09-05 13:02:02 -04:00
|
|
|
|
2014-10-02 16:05:44 -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;
|
2014-09-05 13:02:02 -04:00
|
|
|
} else {
|
2014-10-02 16:05:44 -04:00
|
|
|
// persists the credentials and forwards to /login
|
|
|
|
auth.setCredentials(credentials);
|
|
|
|
$location.path('/login');
|
2014-09-05 13:02:02 -04:00
|
|
|
}
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.busy = false;
|
|
|
|
$scope.$apply();
|
|
|
|
});
|
2014-07-01 13:49:19 -04:00
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = SetCredentialsCtrl;
|