snappymail/dev/View/Popup/Domain.js

284 lines
6.7 KiB
JavaScript
Raw Normal View History

import { pInt, pString } from 'Common/Utils';
import { i18n, getNotification } from 'Common/Translator';
2016-06-30 08:02:45 +08:00
import Remote from 'Remote/Admin/Fetch';
2016-06-30 08:02:45 +08:00
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { DomainAdminStore } from 'Stores/Admin/Domain';
class DomainPopupView extends AbstractViewPopup {
constructor() {
super('Domain');
this.viewNoUserSelect = true;
2021-08-20 21:40:07 +08:00
this.addObservables(this.getDefaults());
this.addObservables({
edit: false,
2021-08-20 21:40:07 +08:00
saving: false,
testing: false,
testingDone: false,
testingImapError: false,
testingSieveError: false,
testingSmtpError: false,
testingImapErrorDesc: '',
testingSieveErrorDesc: '',
testingSmtpErrorDesc: '',
imapServerFocus: false,
sieveServerFocus: false,
smtpServerFocus: false,
});
this.addComputables({
headerText: () => {
const name = this.name(),
aliasName = this.aliasName();
let result = '';
if (this.edit()) {
result = i18n('POPUPS_DOMAIN/TITLE_EDIT_DOMAIN', { NAME: name });
if (aliasName) {
result += ' ← ' + aliasName;
}
} else {
result = name
? i18n('POPUPS_DOMAIN/TITLE_ADD_DOMAIN_WITH_NAME', { NAME: name })
: i18n('POPUPS_DOMAIN/TITLE_ADD_DOMAIN');
}
return result;
},
domainDesc: () => {
const name = this.name();
return !this.edit() && name ? i18n('POPUPS_DOMAIN/NEW_DOMAIN_DESC', { NAME: '*@' + name }) : '';
},
domainIsComputed: () => {
const usePhpMail = this.smtpPhpMail(),
useSieve = this.useSieve();
return (
this.name() &&
this.imapServer() &&
this.imapPort() &&
2021-03-14 19:15:48 +08:00
(useSieve ? this.sieveServer() && this.sievePort() : true) &&
((this.smtpServer() && this.smtpPort()) || usePhpMail)
);
},
canBeTested: () => !this.testing() && this.domainIsComputed(),
canBeSaved: () => !this.saving() && this.domainIsComputed()
});
this.addSubscribables({
testingImapError: value => value || this.testingImapErrorDesc(''),
testingSieveError: value => value || this.testingSieveErrorDesc(''),
testingSmtpError: value => value || this.testingSmtpErrorDesc(''),
// smart form improvements
imapServerFocus: value =>
value && this.name() && !this.imapServer() && this.imapServer(this.name().replace(/[.]?[*][.]?/g, '')),
sieveServerFocus: value =>
value && this.imapServer() && !this.sieveServer() && this.sieveServer(this.imapServer()),
smtpServerFocus: value => value && this.imapServer() && !this.smtpServer()
&& this.smtpServer(this.imapServer().replace(/imap/gi, 'smtp')),
imapSecure: value => {
if (this.enableSmartPorts()) {
const port = pInt(this.imapPort());
switch (pString(value)) {
case '0':
case '2':
if (993 === port) {
this.imapPort('143');
}
break;
case '1':
if (143 === port) {
this.imapPort('993');
}
break;
// no default
}
}
},
smtpSecure: value => {
if (this.enableSmartPorts()) {
const port = pInt(this.smtpPort());
switch (pString(value)) {
case '0':
if (465 === port || 587 === port) {
this.smtpPort('25');
}
break;
case '1':
if (25 === port || 587 === port) {
this.smtpPort('465');
}
break;
case '2':
if (25 === port || 465 === port) {
this.smtpPort('587');
}
break;
// no default
}
}
}
});
decorateKoCommands(this, {
createOrAddCommand: self => self.canBeSaved(),
2021-08-27 02:31:38 +08:00
testConnectionCommand: self => self.canBeTested()
});
}
2015-01-29 00:27:23 +08:00
2016-09-10 06:38:16 +08:00
createOrAddCommand() {
this.saving(true);
Remote.createOrUpdateDomain(
this.onDomainCreateOrSaveResponse.bind(this),
this
2016-09-10 06:38:16 +08:00
);
}
testConnectionCommand() {
this.testingDone(false);
this.testingImapError(false);
this.testingSieveError(false);
this.testingSmtpError(false);
this.testing(true);
Remote.testConnectionForDomain(
2021-04-23 16:47:24 +08:00
(iError, oData) => {
this.testing(false);
if (iError) {
this.testingImapError(true);
this.testingSieveError(true);
this.testingSmtpError(true);
} else {
this.testingDone(true);
this.testingImapError(true !== oData.Result.Imap);
this.testingSieveError(true !== oData.Result.Sieve);
this.testingSmtpError(true !== oData.Result.Smtp);
if (this.testingImapError() && oData.Result.Imap) {
this.testingImapErrorDesc('');
this.testingImapErrorDesc(oData.Result.Imap);
}
if (this.testingSieveError() && oData.Result.Sieve) {
this.testingSieveErrorDesc('');
this.testingSieveErrorDesc(oData.Result.Sieve);
}
if (this.testingSmtpError() && oData.Result.Smtp) {
this.testingSmtpErrorDesc('');
this.testingSmtpErrorDesc(oData.Result.Smtp);
}
}
},
this
2016-09-10 06:38:16 +08:00
);
}
onDomainCreateOrSaveResponse(iError) {
this.saving(false);
if (iError) {
this.savingError(getNotification(iError));
2019-07-05 03:19:24 +08:00
} else {
DomainAdminStore.fetch();
this.closeCommand();
2016-06-30 08:02:45 +08:00
}
}
clearTesting() {
this.testing(false);
this.testingDone(false);
this.testingImapError(false);
this.testingSieveError(false);
this.testingSmtpError(false);
}
onShow(oDomain) {
this.saving(false);
this.clearTesting();
2016-06-30 08:02:45 +08:00
this.clearForm();
2019-07-05 03:19:24 +08:00
if (oDomain) {
this.enableSmartPorts(false);
this.edit(true);
this.name(oDomain.Name);
this.imapServer(oDomain.IncHost);
this.imapPort('' + pInt(oDomain.IncPort));
this.imapSecure(oDomain.IncSecure);
this.imapShortLogin(!!oDomain.IncShortLogin);
this.useSieve(!!oDomain.UseSieve);
this.sieveServer(oDomain.SieveHost);
this.sievePort('' + pInt(oDomain.SievePort));
this.sieveSecure(oDomain.SieveSecure);
this.smtpServer(oDomain.OutHost);
this.smtpPort('' + pInt(oDomain.OutPort));
this.smtpSecure(oDomain.OutSecure);
this.smtpShortLogin(!!oDomain.OutShortLogin);
this.smtpAuth(!!oDomain.OutAuth);
this.smtpSetSender(!!oDomain.OutSetSender);
this.smtpPhpMail(!!oDomain.OutUsePhpMail);
this.whiteList(oDomain.WhiteList);
this.aliasName(oDomain.AliasName);
this.enableSmartPorts(true);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
2021-08-20 21:40:07 +08:00
getDefaults() {
return {
savingError: '',
2021-08-20 21:40:07 +08:00
name: '',
2021-08-20 21:40:07 +08:00
imapServer: '',
imapPort: '143',
imapSecure: 0,
imapShortLogin: false,
2021-08-20 21:40:07 +08:00
useSieve: false,
sieveServer: '',
sievePort: '4190',
sieveSecure: 0,
2014-11-20 05:32:20 +08:00
2021-08-20 21:40:07 +08:00
smtpServer: '',
smtpPort: '25',
smtpSecure: 0,
smtpShortLogin: false,
smtpAuth: true,
smtpSetSender: false,
smtpPhpMail: false,
2014-11-20 05:32:20 +08:00
2021-08-20 21:40:07 +08:00
whiteList: '',
aliasName: '',
2014-10-14 06:39:23 +08:00
2021-08-20 21:40:07 +08:00
enableSmartPorts: false
};
}
2021-08-20 21:40:07 +08:00
clearForm() {
this.edit(false);
Object.entries(this.getDefaults()).forEach(([key, value]) => this[key](value));
this.enableSmartPorts(true);
}
}
2019-07-05 03:19:24 +08:00
export { DomainPopupView, DomainPopupView as default };