summaryrefslogtreecommitdiffstats
path: root/comm/suite/chatzilla/xul/content/networks-server.js
blob: fde299bf891ffcc317cea2275791a2474e83405d (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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 { Services } =
  ChromeUtils.import("resource://gre/modules/Services.jsm");
var { isLegalHostNameOrIP = 
  ChromeUtils.import("resource:///modules/hostnameUtils.jsm");

var gNetworkServer;
var gNetworksBundle;
var gNameValue;
var gPortValue;
var gDefaultPort;
var gSocketType;

function onLoad(aEvent) {
  gNetworkServer = window.arguments[0].server;

  gNetworksBundle = document.getElementById("bundle_networks");
  gNameValue = document.getElementById("nameValue");
  gPortValue = document.getElementById("portValue");
  gDefaultPort = document.getElementById("defaultPort");
  gSocketType = document.getElementById("socketType");

  // Set labels on socketType menuitems.
  document.getElementById("socketSecurityType-0").label =
    gNetworksBundle.getString("server-ConnectionSecurityType-0");
  document.getElementById("socketSecurityType-3").label =
    gNetworksBundle.getString("server-ConnectionSecurityType-3");

  if (gNetworkServer) {
    gNameValue.value = gNetworkServer.hostname;
    gPortValue.value = gNetworkServer.port;
    gSocketType.value = gNetworkServer.isSecure ? 3 : 0;
  }
  sslChanged(false);
}

function onAccept() {
  let hostname = cleanUpHostName(gNameValue.value.toLowerCase());
  if (!isLegalHostNameOrIP(hostname)) {
    let alertTitle = gNetworksBundle.getString("invalidServerName");
    let alertMsg = gNetworksBundle.getString("enterValidServerName");
    Services.prompt.alert(window, alertTitle, alertMsg);

    window.arguments[0].result = false;
    return false;
  }

  // If we didn't have a server to initialize with, we must create one.
  if (!gNetworkServer) {
    gNetworkServer = {};
  }

  gNetworkServer.hostname = hostname;
  gNetworkServer.port = gPortValue.value;
  gNetworkServer.isSecure = gSocketType.value == 3;

  window.arguments[0].server = gNetworkServer;
  window.arguments[0].result = true;
  return true;
}

/**
 * Resets the default port to IRC or IRCS, dependending on the |gSocketType|
 * value, and sets the port to use to this default, if that's appropriate.
 *
 * @param aUserAction  false for dialog initialization,
 *                     true for user action.
 */
function sslChanged(aUserAction) {
  const DEFAULT_IRC_PORT = "6667";
  const DEFAULT_IRCS_PORT = "6697";
  let otherDefaultPort;
  let prevDefaultPort = gDefaultPort.value;

  if (gSocketType.value == 3) {
    gDefaultPort.value = DEFAULT_IRCS_PORT;
    otherDefaultPort = DEFAULT_IRC_PORT;
  } else {
    gDefaultPort.value = DEFAULT_IRC_PORT;
    otherDefaultPort = DEFAULT_IRCS_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 ((gPortValue.value == 0) ||
      (aUserAction && (gDefaultPort.value != prevDefaultPort) &&
       (gPortValue.value == otherDefaultPort)))
    gPortValue.value = gDefaultPort.value;
}