2017-06-02 10:24:46 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-06-20 12:58:14 +02:00
|
|
|
angular.module('Application').controller('EmailController', ['$scope', '$location', '$rootScope', 'Client', 'AppStore', function ($scope, $location, $rootScope, Client, AppStore) {
|
2017-06-02 10:24:46 +02:00
|
|
|
Client.onReady(function () { if (!Client.getUserInfo().admin) $location.path('/'); });
|
|
|
|
|
|
|
|
|
|
$scope.client = Client;
|
|
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
$scope.config = Client.getConfig();
|
|
|
|
|
$scope.dnsConfig = {};
|
|
|
|
|
$scope.outboundPort25 = {};
|
|
|
|
|
$scope.expectedDnsRecords = {};
|
|
|
|
|
$scope.expectedDnsRecordsTypes = [
|
|
|
|
|
{ name: 'MX', value: 'mx' },
|
|
|
|
|
{ name: 'DKIM', value: 'dkim' },
|
|
|
|
|
{ name: 'SPF', value: 'spf' },
|
|
|
|
|
{ name: 'DMARC', value: 'dmarc' },
|
|
|
|
|
{ name: 'PTR', value: 'ptr' }
|
|
|
|
|
];
|
|
|
|
|
$scope.mailConfig = null;
|
2017-06-12 13:18:47 +02:00
|
|
|
$scope.users = [];
|
2017-06-20 12:58:14 +02:00
|
|
|
$scope.isPaying = false;
|
2017-06-02 10:24:46 +02:00
|
|
|
|
|
|
|
|
$scope.showView = function (view) {
|
|
|
|
|
// wait for dialog to be fully closed to avoid modal behavior breakage when moving to a different view already
|
|
|
|
|
$('.modal').on('hidden.bs.modal', function () {
|
|
|
|
|
$('.modal').off('hidden.bs.modal');
|
|
|
|
|
$location.path(view);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('.modal').modal('hide');
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-12 13:18:47 +02:00
|
|
|
$scope.catchall = {
|
|
|
|
|
addresses: [],
|
|
|
|
|
busy: false,
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.catchall.busy = true;
|
|
|
|
|
|
|
|
|
|
Client.setCatchallAddresses($scope.catchall.addresses, function (error) {
|
|
|
|
|
if (error) console.error('Unable to add catchall address.', error);
|
|
|
|
|
|
|
|
|
|
$scope.catchall.busy = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-02 10:24:46 +02:00
|
|
|
$scope.email = {
|
|
|
|
|
refreshBusy: false,
|
|
|
|
|
|
|
|
|
|
toggle: function () {
|
|
|
|
|
if ($scope.mailConfig.enabled) return $scope.email.disable();
|
|
|
|
|
|
|
|
|
|
// show warning first
|
|
|
|
|
$('#enableEmailModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
enable: function () {
|
|
|
|
|
$('#enableEmailModal').modal('hide');
|
|
|
|
|
|
|
|
|
|
Client.setMailConfig({ enabled: true }, function (error) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.mailConfig.enabled = true;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
disable: function () {
|
|
|
|
|
Client.setMailConfig({ enabled: false }, function (error) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.mailConfig.enabled = false;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
refresh: function () {
|
|
|
|
|
$scope.email.refreshBusy = true;
|
|
|
|
|
|
|
|
|
|
showExpectedDnsRecords(function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.email.refreshBusy = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-27 13:39:08 +02:00
|
|
|
$scope.mailRelayPresets = [
|
2017-06-28 09:51:01 -05:00
|
|
|
{ provider: 'cloudron-smtp', name: 'Built-in SMTP server' },
|
2017-06-28 16:56:11 -05:00
|
|
|
{ provider: 'external-smtp', name: 'External SMTP server', host: '', port: 587, tls: false },
|
|
|
|
|
{ provider: 'ses-smtp', name: 'Amazon SES', host: 'email-smtp.us-east-1.amazonaws.com', port: 25, tls: false },
|
|
|
|
|
{ provider: 'google-smtp', name: 'Google', host: 'smtp.gmail.com', port: 587, tls: false },
|
|
|
|
|
{ provider: 'mailgun-smtp', name: 'Mailgun', host: 'smtp.mailgun.org', port: 587, tls: false },
|
|
|
|
|
{ provider: 'postmark-smtp', name: 'Postmark', host: 'smtp.postmarkapp.com', port: 587, tls: false },
|
|
|
|
|
{ provider: 'sendgrid-smtp', name: 'SendGrid', host: 'smtp.sendgrid.net', port: 587, tls: false },
|
2017-06-27 13:39:08 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$scope.mailRelay = {
|
2017-06-27 15:44:36 -05:00
|
|
|
error: null,
|
2017-06-27 13:39:08 +02:00
|
|
|
busy: false,
|
|
|
|
|
preset: $scope.mailRelayPresets[0],
|
|
|
|
|
|
|
|
|
|
presetChanged: function () {
|
2017-06-27 15:44:36 -05:00
|
|
|
$scope.mailRelay.error = null;
|
|
|
|
|
|
2017-06-28 09:51:01 -05:00
|
|
|
$scope.mailRelay.relay.provider = $scope.mailRelay.preset.provider;
|
2017-06-27 13:45:49 -05:00
|
|
|
$scope.mailRelay.relay.host = $scope.mailRelay.preset.host;
|
|
|
|
|
$scope.mailRelay.relay.port = $scope.mailRelay.preset.port;
|
|
|
|
|
$scope.mailRelay.relay.tls = $scope.mailRelay.preset.tls;
|
2017-06-27 13:39:08 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// form data to be set on load
|
2017-06-27 13:45:49 -05:00
|
|
|
relay: {
|
2017-06-28 09:51:01 -05:00
|
|
|
provider: 'cloudron-smtp',
|
2017-06-27 13:45:49 -05:00
|
|
|
host: '',
|
|
|
|
|
port: 25,
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
2017-06-28 16:56:11 -05:00
|
|
|
tls: true // whether we should connect with TLS. STARTTLS is always supported
|
2017-06-27 13:45:49 -05:00
|
|
|
},
|
2017-06-27 13:39:08 +02:00
|
|
|
|
|
|
|
|
submit: function () {
|
2017-06-27 15:44:36 -05:00
|
|
|
$scope.mailRelay.error = null;
|
2017-06-27 13:45:49 -05:00
|
|
|
$scope.mailRelay.busy = true;
|
|
|
|
|
|
2017-06-28 16:56:11 -05:00
|
|
|
if ($scope.mailRelay.relay.provider === 'external-smtp') {
|
|
|
|
|
// this is generally the case
|
|
|
|
|
$scope.mailRelay.relay.tls = $scope.mailRelay.relay.port === 465;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 13:45:49 -05:00
|
|
|
Client.setMailRelay($scope.mailRelay.relay, function (error) {
|
2017-06-27 15:44:36 -05:00
|
|
|
if (error) {
|
|
|
|
|
$scope.mailRelay.error = error.message;
|
|
|
|
|
}
|
2017-06-27 13:45:49 -05:00
|
|
|
|
|
|
|
|
$scope.mailRelay.busy = false;
|
|
|
|
|
});
|
2017-06-27 13:39:08 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-02 10:24:46 +02:00
|
|
|
function getMailConfig() {
|
|
|
|
|
Client.getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.mailConfig = mailConfig;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 13:45:49 -05:00
|
|
|
function getMailRelay() {
|
|
|
|
|
Client.getMailRelay(function (error, relay) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.mailRelay.relay = relay;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < $scope.mailRelayPresets.length; i++) {
|
2017-06-28 09:51:01 -05:00
|
|
|
if ($scope.mailRelayPresets[i].provider === relay.provider) {
|
2017-06-27 13:45:49 -05:00
|
|
|
$scope.mailRelay.preset = $scope.mailRelayPresets[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 10:24:46 +02:00
|
|
|
function getDnsConfig() {
|
|
|
|
|
Client.getDnsConfig(function (error, dnsConfig) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.dnsConfig = dnsConfig;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showExpectedDnsRecords(callback) {
|
|
|
|
|
callback = callback || function (error) { if (error) console.error(error); };
|
|
|
|
|
|
|
|
|
|
Client.getEmailStatus(function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
$scope.expectedDnsRecords = result.dns;
|
|
|
|
|
$scope.outboundPort25 = result.outboundPort25;
|
|
|
|
|
|
|
|
|
|
// open the record details if they are not correct
|
|
|
|
|
for (var type in $scope.expectedDnsRecords) {
|
|
|
|
|
if (!$scope.expectedDnsRecords[type].status) {
|
|
|
|
|
$('#collapse_dns_' + type).collapse('show');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$scope.outboundPort25.status) {
|
|
|
|
|
$('#collapse_dns_port').collapse('show');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 13:18:47 +02:00
|
|
|
function getUsers() {
|
|
|
|
|
Client.getUsers(function (error, result) {
|
|
|
|
|
if (error) return console.error('Unable to get user listing.', error);
|
|
|
|
|
|
|
|
|
|
// only allow users with a Cloudron email address
|
2017-06-21 19:34:55 -07:00
|
|
|
$scope.catchall.availableAddresses = result.filter(function (u) { return !!u.email; }).map(function (u) { return u.username; });
|
2017-06-12 13:18:47 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCatchallAddresses() {
|
|
|
|
|
Client.getCatchallAddresses(function (error, result) {
|
|
|
|
|
if (error) return console.error('Unable to get catchall address listing.', error);
|
|
|
|
|
|
|
|
|
|
// dedupe in case to avoid angular breakage
|
|
|
|
|
$scope.catchall.addresses = result.filter(function(item, pos, self) {
|
|
|
|
|
return self.indexOf(item) == pos;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 12:58:14 +02:00
|
|
|
function getSubscription() {
|
2017-06-21 20:37:24 -07:00
|
|
|
if ($scope.config.provider === 'caas') {
|
|
|
|
|
$scope.isPaying = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 12:58:14 +02:00
|
|
|
Client.getAppstoreConfig(function (error, result) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
if (!result.token) return;
|
|
|
|
|
|
2017-06-20 13:10:07 +02:00
|
|
|
AppStore.getSubscription(result, function (error, result) {
|
2017-06-20 12:58:14 +02:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.isPaying = result.plan.id !== 'free' && result.plan.id !== 'undecided';
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 10:24:46 +02:00
|
|
|
Client.onReady(function () {
|
|
|
|
|
getMailConfig();
|
2017-06-27 13:45:49 -05:00
|
|
|
getMailRelay();
|
2017-06-02 10:24:46 +02:00
|
|
|
getDnsConfig();
|
2017-06-20 12:58:14 +02:00
|
|
|
getSubscription();
|
2017-06-12 13:18:47 +02:00
|
|
|
getUsers();
|
|
|
|
|
getCatchallAddresses();
|
2017-06-02 10:41:35 +02:00
|
|
|
$scope.email.refresh();
|
2017-06-02 10:24:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('.modal-backdrop').remove();
|
|
|
|
|
}]);
|