160 lines
5.5 KiB
JavaScript
160 lines
5.5 KiB
JavaScript
'use strict';
|
|
|
|
/* global angular:false */
|
|
/* global $:false */
|
|
|
|
angular.module('Application').controller('EmailsController', ['$scope', '$location', 'Client', function ($scope, $location, Client) {
|
|
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
|
|
|
|
$scope.ready = false;
|
|
$scope.config = Client.getConfig();
|
|
$scope.user = Client.getUserInfo();
|
|
$scope.domains = [];
|
|
|
|
$scope.activity = {
|
|
busy: true,
|
|
eventLogs: [],
|
|
activeEventLog: null,
|
|
currentPage: 1,
|
|
perPage: 20,
|
|
search: '',
|
|
|
|
refresh: function () {
|
|
$scope.activity.busy = true;
|
|
|
|
Client.getMailEventLogs($scope.activity.search, $scope.activity.currentPage, $scope.activity.perPage, function (error, result) {
|
|
if (error) return console.error('Failed to fetch mail eventlogs.', error);
|
|
|
|
$scope.activity.busy = false;
|
|
|
|
$scope.activity.eventLogs = result;
|
|
});
|
|
},
|
|
|
|
showNextPage: function () {
|
|
$scope.activity.currentPage++;
|
|
$scope.activity.refresh();
|
|
},
|
|
|
|
showPrevPage: function () {
|
|
if ($scope.activity.currentPage > 1) $scope.activity.currentPage--;
|
|
else $scope.activity.currentPage = 1;
|
|
$scope.activity.refresh();
|
|
},
|
|
|
|
showEventLogDetails: function (eventLog) {
|
|
if ($scope.activity.activeEventLog === eventLog) $scope.activity.activeEventLog = null;
|
|
else $scope.activity.activeEventLog = eventLog;
|
|
},
|
|
|
|
updateFilter: function () {
|
|
$scope.activity.currentPage = 1;
|
|
$scope.activity.refresh();
|
|
}
|
|
};
|
|
|
|
$scope.testEmail = {
|
|
busy: false,
|
|
error: {},
|
|
|
|
mailTo: '',
|
|
|
|
domain: null,
|
|
|
|
clearForm: function () {
|
|
$scope.testEmail.mailTo = '';
|
|
},
|
|
|
|
show: function (domain) {
|
|
$scope.testEmail.error = {};
|
|
$scope.testEmail.busy = false;
|
|
|
|
$scope.testEmail.domain = domain;
|
|
$scope.testEmail.mailTo = $scope.user.email;
|
|
|
|
$('#testEmailModal').modal('show');
|
|
},
|
|
|
|
submit: function () {
|
|
$scope.testEmail.error = {};
|
|
$scope.testEmail.busy = true;
|
|
|
|
Client.sendTestMail($scope.testEmail.domain.domain, $scope.testEmail.mailTo, function (error) {
|
|
$scope.testEmail.busy = false;
|
|
|
|
if (error) {
|
|
$scope.testEmail.error.generic = error.message;
|
|
console.error(error);
|
|
$('#inputTestMailTo').focus();
|
|
return;
|
|
}
|
|
|
|
$('#testEmailModal').modal('hide');
|
|
});
|
|
}
|
|
};
|
|
|
|
function refreshDomainStatuses() {
|
|
$scope.domains.forEach(function (domain) {
|
|
Client.getMailStatusForDomain(domain.domain, function (error, result) {
|
|
if (error) return console.error('Failed to fetch mail status for domain', domain.domain, error);
|
|
|
|
domain.status = result;
|
|
|
|
// those states always exist
|
|
domain.statusOk = result.relay.status && result.dns.mx.status;
|
|
|
|
// those states only exist if no relay is used
|
|
if (typeof result.dns.spf !== 'undefined') domain.statusOk = domain.statusOk && result.dns.spf.status;
|
|
if (typeof result.dns.dkim !== 'undefined') domain.statusOk = domain.statusOk && result.dns.dkim.status;
|
|
if (typeof result.dns.dmarc !== 'undefined') domain.statusOk = domain.statusOk && result.dns.dmarc.status;
|
|
if (typeof result.dns.ptr !== 'undefined') domain.statusOk = domain.statusOk && result.dns.ptr.status;
|
|
if (typeof result.rbl.status === 'boolean') domain.statusOk = domain.statusOk && result.rbl.status;
|
|
});
|
|
|
|
Client.getMailConfigForDomain(domain.domain, function (error, mailConfig) {
|
|
if (error) return console.error('Failed to fetch mail config for domain', domain.domain, error);
|
|
|
|
domain.inbound = mailConfig.enabled;
|
|
domain.outbound = mailConfig.relay.provider !== 'noop';
|
|
|
|
// do this even if no outbound since people forget to remove mailboxes
|
|
Client.getMailboxes(domain.domain, function (error, mailboxes) {
|
|
if (error) return console.error('Failed to fetch mailboxes for domain', domain.domain, error);
|
|
|
|
domain.mailboxCount = mailboxes.length;
|
|
|
|
Client.getMailUsage(domain.domain, function (error, usage) {
|
|
if (error) return console.error('Failed to fetch usage for domain', domain.domain, error);
|
|
|
|
domain.usage = 0;
|
|
Object.keys(usage).forEach(function (m) { domain.usage += usage[m].size; });
|
|
});
|
|
});
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
Client.onReady(function () {
|
|
Client.getDomains(function (error, domains) {
|
|
if (error) return console.error('Unable to get domain listing.', error);
|
|
|
|
$scope.domains = domains;
|
|
$scope.ready = true;
|
|
|
|
$scope.activity.refresh();
|
|
refreshDomainStatuses();
|
|
});
|
|
});
|
|
|
|
// setup all the dialog focus handling
|
|
['testEmailModal'].forEach(function (id) {
|
|
$('#' + id).on('shown.bs.modal', function () {
|
|
$(this).find('[autofocus]:first').focus();
|
|
});
|
|
});
|
|
|
|
$('.modal-backdrop').remove();
|
|
}]);
|