summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/prefs/content/SmtpServerEdit.js
blob: 56d6076c5fb09587f68013c256b14fa3c1ac5697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { cleanUpHostName, isLegalHostNameOrIP } = ChromeUtils.import(
  "resource:///modules/hostnameUtils.jsm"
);
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { OAuth2Providers } = ChromeUtils.import(
  "resource:///modules/OAuth2Providers.jsm"
);

var gSmtpServer;
var gSmtpUsername;
var gSmtpDescription;
var gSmtpUsernameLabel;
var gSmtpHostname;
var gSmtpPort;
var gSmtpAuthMethod;
var gSmtpSocketType;
var gPort;
var gDefaultPort;

window.addEventListener("DOMContentLoaded", onLoad);
document.addEventListener("dialogaccept", onAccept);

function onLoad() {
  gSmtpServer = window.arguments[0].server;
  initSmtpSettings(gSmtpServer);
}

function onAccept(event) {
  if (!isLegalHostNameOrIP(cleanUpHostName(gSmtpHostname.value))) {
    let prefsBundle = document.getElementById("bundle_prefs");
    let brandBundle = document.getElementById("bundle_brand");
    let alertTitle = brandBundle.getString("brandShortName");
    let alertMsg = prefsBundle.getString("enterValidServerName");
    Services.prompt.alert(window, alertTitle, alertMsg);

    window.arguments[0].result = false;
    event.preventDefault();
    return;
  }

  // If we didn't have an SMTP server to initialize with,
  // we must be creating one.
  try {
    if (!gSmtpServer) {
      gSmtpServer = MailServices.smtp.createServer();
      window.arguments[0].addSmtpServer = gSmtpServer.key;
    }

    saveSmtpSettings(gSmtpServer);
  } catch (ex) {
    console.error("Error saving smtp server: " + ex);
  }

  window.arguments[0].result = true;
}

function initSmtpSettings(server) {
  gSmtpUsername = document.getElementById("smtpUsername");
  gSmtpDescription = document.getElementById("smtp.description");
  gSmtpUsernameLabel = document.getElementById("smtpUsernameLabel");
  gSmtpHostname = document.getElementById("smtp.hostname");
  gSmtpPort = document.getElementById("smtp.port");
  gSmtpAuthMethod = document.getElementById("smtp.authMethod");
  gSmtpSocketType = document.getElementById("smtp.socketType");
  gDefaultPort = document.getElementById("smtp.defaultPort");
  gPort = document.getElementById("smtp.port");

  if (server) {
    gSmtpHostname.value = server.hostname;
    gSmtpDescription.value = server.description;
    gSmtpPort.value = server.port;
    gSmtpUsername.value = server.username;
    gSmtpAuthMethod.value = server.authMethod;
    gSmtpSocketType.value = server.socketType < 4 ? server.socketType : 1;
  } else {
    // New server, load default values.
    gSmtpAuthMethod.value = Services.prefs.getIntPref(
      "mail.smtpserver.default.authMethod"
    );
    gSmtpSocketType.value = Services.prefs.getIntPref(
      "mail.smtpserver.default.try_ssl"
    );
  }

  // Although sslChanged will set a label for cleartext password,
  // we need to use the long label so that we can size the dialog.
  setLabelFromStringBundle("authMethod-no", "authNo");
  setLabelFromStringBundle(
    "authMethod-password-encrypted",
    "authPasswordEncrypted"
  );
  setLabelFromStringBundle(
    "authMethod-password-cleartext",
    "authPasswordCleartextInsecurely"
  );
  setLabelFromStringBundle("authMethod-kerberos", "authKerberos");
  setLabelFromStringBundle("authMethod-ntlm", "authNTLM");
  setLabelFromStringBundle("authMethod-oauth2", "authOAuth2");
  setLabelFromStringBundle("authMethod-anysecure", "authAnySecure");
  setLabelFromStringBundle("authMethod-any", "authAny");

  window.sizeToContent();

  sslChanged(false);
  authMethodChanged(false);

  if (MailServices.smtp.defaultServer) {
    onLockPreference();
  }

  // Hide OAuth2 option if we can't use it.
  let details = server
    ? OAuth2Providers.getHostnameDetails(server.hostname)
    : null;
  document.getElementById("authMethod-oauth2").hidden = !details;

  // Hide deprecated/hidden auth options, unless selected
  hideUnlessSelected(document.getElementById("authMethod-anysecure"));
  hideUnlessSelected(document.getElementById("authMethod-any"));

  // "STARTTLS, if available" is vulnerable to MITM attacks so we shouldn't
  // allow users to choose it anymore. Hide the option unless the user already
  // has it set.
  hideUnlessSelected(document.getElementById("connectionSecurityType-1"));
}

function hideUnlessSelected(element) {
  element.hidden = !element.selected;
}

function setLabelFromStringBundle(elementID, stringName) {
  document.getElementById(elementID).label = document
    .getElementById("bundle_messenger")
    .getString(stringName);
}

// Disables xul elements that have associated preferences locked.
function onLockPreference() {
  try {
    let allPrefElements = {
      hostname: gSmtpHostname,
      description: gSmtpDescription,
      port: gSmtpPort,
      authMethod: gSmtpAuthMethod,
      try_ssl: gSmtpSocketType,
    };
    disableIfLocked(allPrefElements);
  } catch (e) {
    // non-fatal
    console.error("Error while getting locked prefs: " + e);
  }
}

/**
 * Does the work of disabling an element given the array which contains
 * id/prefstring pairs.
 *
 * @param {Element[]} prefstrArray - Elements to check.
 *
 * TODO: try to merge this with disableIfLocked function in am-offline.js (bug 755885)
 */
function disableIfLocked(prefstrArray) {
  let smtpPrefBranch = Services.prefs.getBranch(
    "mail.smtpserver." + MailServices.smtp.defaultServer.key + "."
  );

  for (let prefstring in prefstrArray) {
    if (smtpPrefBranch.prefIsLocked(prefstring)) {
      prefstrArray[prefstring].disabled = true;
    }
  }
}

function saveSmtpSettings(server) {
  if (server) {
    server.hostname = cleanUpHostName(gSmtpHostname.value);
    server.description = gSmtpDescription.value;
    server.port = gSmtpPort.value;
    server.authMethod = gSmtpAuthMethod.value;
    server.username = gSmtpUsername.value;
    server.socketType = gSmtpSocketType.value;
  }
}

function authMethodChanged(userAction) {
  var noUsername = gSmtpAuthMethod.value == Ci.nsMsgAuthMethod.none;
  gSmtpUsername.disabled = noUsername;
  gSmtpUsernameLabel.disabled = noUsername;
}

/**
 * Resets the default port to SMTP or SMTPS, dependending on
 * the |gSmtpSocketType| value, and sets the port to use to this default,
 * if that's appropriate.
 *
 * @param {boolean} userAction - false for dialog initialization,
 *   true for user action.
 */
function sslChanged(userAction) {
  const DEFAULT_SMTP_PORT = "587";
  const DEFAULT_SMTPS_PORT = "465";
  var socketType = gSmtpSocketType.value;
  var otherDefaultPort;
  var prevDefaultPort = gDefaultPort.value;

  if (socketType == Ci.nsMsgSocketType.SSL) {
    gDefaultPort.value = DEFAULT_SMTPS_PORT;
    otherDefaultPort = DEFAULT_SMTP_PORT;
  } else {
    gDefaultPort.value = DEFAULT_SMTP_PORT;
    otherDefaultPort = DEFAULT_SMTPS_PORT;
  }

  // If the port is not set,
  // or the user is causing the default port to change,
  //   and the port is set to the default for the other protocol,
  // then set the port to the default for the new protocol.
  if (
    gPort.value == 0 ||
    (userAction &&
      gDefaultPort.value != prevDefaultPort &&
      gPort.value == otherDefaultPort)
  ) {
    gPort.value = gDefaultPort.value;
  }

  // switch "insecure password" label
  setLabelFromStringBundle(
    "authMethod-password-cleartext",
    socketType == Ci.nsMsgSocketType.SSL ||
      socketType == Ci.nsMsgSocketType.alwaysSTARTTLS
      ? "authPasswordCleartextViaSSL"
      : "authPasswordCleartextInsecurely"
  );
}