CIRCNetwork.prototype.onDisconnect = function()

in suite/chatzilla/content/handlers.js [2328:2553]


CIRCNetwork.prototype.onDisconnect = function (e) {
  var msg, msgNetwork;
  var msgType = MT_ERROR;
  var retrying = true;

  if (typeof e.disconnectStatus != "undefined") {
    switch (e.disconnectStatus) {
      case 0:
        msg = getMsg(MSG_CONNECTION_CLOSED, [this.getURL(), e.server.getURL()]);
        break;

      case NS_ERROR_CONNECTION_REFUSED:
        msg = getMsg(MSG_CONNECTION_REFUSED, [
          this.getURL(),
          e.server.getURL(),
        ]);
        break;

      case NS_ERROR_NET_TIMEOUT:
        msg = getMsg(MSG_CONNECTION_TIMEOUT, [
          this.getURL(),
          e.server.getURL(),
        ]);
        break;

      case NS_ERROR_NET_RESET:
        msg = getMsg(MSG_CONNECTION_RESET, [this.getURL(), e.server.getURL()]);
        break;

      case NS_ERROR_NET_INTERRUPT:
        msg = getMsg(MSG_CONNECTION_INTERRUPT, [
          this.getURL(),
          e.server.getURL(),
        ]);
        break;

      case NS_ERROR_UNKNOWN_HOST:
        msg = getMsg(MSG_UNKNOWN_HOST, [
          e.server.hostname,
          this.getURL(),
          e.server.getURL(),
        ]);
        break;

      case NS_ERROR_UNKNOWN_PROXY_HOST:
        msg = getMsg(MSG_UNKNOWN_PROXY_HOST, [
          this.getURL(),
          e.server.getURL(),
        ]);
        break;

      case NS_ERROR_PROXY_CONNECTION_REFUSED:
        msg = MSG_PROXY_CONNECTION_REFUSED;
        break;

      case NS_ERROR_OFFLINE:
        msg = MSG_ERR_OFFLINE;
        retrying = false;
        break;

      case NS_ERROR_ABORT:
        if (Services.io.offline) {
          msg = getMsg(MSG_CONNECTION_ABORT_OFFLINE, [
            this.getURL(),
            e.server.getURL(),
          ]);
        } else {
          msg = getMsg(MSG_CONNECTION_ABORT_UNKNOWN, [
            this.getURL(),
            e.server.getURL(),
            formatException(e.exception),
          ]);
        }
        retrying = false;
        break;

      default:
        let nssErrSvc = Cc["@mozilla.org/nss_errors_service;1"].getService(
          Ci.nsINSSErrorsService
        );
        let errClass = 0;
        // Check if e.disconnectStatus is within the valid range for
        // NSS Errors.
        if (e.disconnectStatus >= 8192 && e.disconnectStatus < 20480) {
          errClass = nssErrSvc.getErrorClass(e.disconnectStatus);
        }
        // Check here if it's a cert error.
        // The exception adding dialog will explain the reasons.
        if (errClass == Ci.nsINSSErrorsService.ERROR_CLASS_BAD_CERT) {
          var cmd = "ssl-exception";
          cmd += " " + e.server.hostname + " " + e.server.port;
          cmd += " true";
          msg = getMsg(MSG_INVALID_CERT, [this.getURL(), cmd]);
          retrying = false;
          break;
        }

        // If it's a protocol error, we can still display a useful message.
        var statusMsg = e.disconnectStatus;
        if (errClass == Ci.nsINSSErrorsService.ERROR_CLASS_SSL_PROTOCOL) {
          var errMsg = nssErrSvc.getErrorMessage(e.disconnectStatus);
          errMsg = errMsg.replace(/\.$/, "");
          statusMsg += " (" + errMsg + ")";
        }

        msg = getMsg(MSG_CLOSE_STATUS, [
          this.getURL(),
          e.server.getURL(),
          statusMsg,
        ]);
        break;
    }
  } else {
    msg = getMsg(MSG_CONNECTION_CLOSED, [this.getURL(), e.server.getURL()]);
  }

  // e.quitting signals the disconnect was intended: don't use "ERROR".
  if (e.quitting) {
    msgType = "DISCONNECT";
    msg = getMsg(MSG_CONNECTION_QUIT, [
      this.getURL(),
      e.server.getURL(),
      this.unicodeName,
      "reconnect",
    ]);
    msgNetwork = msg;
  }
  // We won't reconnect if the error was really bad, or if the user doesn't
  // want us to do so.
  else if (!retrying || !this.stayingPower) {
    msgNetwork = msg;
  } else {
    var delayStr = formatDateOffset(this.getReconnectDelayMs() / 1000);
    if (this.MAX_CONNECT_ATTEMPTS == -1) {
      msgNetwork = getMsg(MSG_RECONNECTING_IN, [
        msg,
        delayStr,
        this.unicodeName,
        "cancel",
      ]);
    } else if (this.connectAttempt < this.MAX_CONNECT_ATTEMPTS) {
      var left = this.MAX_CONNECT_ATTEMPTS - this.connectAttempt;
      if (left == 1) {
        msgNetwork = getMsg(MSG_RECONNECTING_IN_LEFT1, [
          msg,
          delayStr,
          this.unicodeName,
          "cancel",
        ]);
      } else {
        msgNetwork = getMsg(MSG_RECONNECTING_IN_LEFT, [
          msg,
          left,
          delayStr,
          this.unicodeName,
          "cancel",
        ]);
      }
    } else {
      msgNetwork = getMsg(MSG_CONNECTION_EXHAUSTED, msg);
    }
  }

  /* If we were connected ok, put an error on all tabs. If we were only
   * /trying/ to connect, and failed, just put it on the network tab.
   */
  client.munger.getRule(".inline-buttons").enabled = true;
  if (this.state == NET_ONLINE) {
    for (var v in client.viewsArray) {
      var obj = client.viewsArray[v].source;
      if (obj == this) {
        obj.displayHere(msgNetwork, msgType);
      } else if (obj != client) {
        var details = getObjectDetails(obj);
        if ("server" in details && details.server == e.server) {
          obj.displayHere(msg, msgType);
        }
      }
    }
  } else {
    this.busy = false;
    updateProgress();

    // Don't do anything if we're cancelling.
    if (this.state != NET_CANCELLING) {
      this.displayHere(msgNetwork, msgType);
    }
  }
  client.munger.getRule(".inline-buttons").enabled = false;

  for (var c in this.primServ.channels) {
    var channel = this.primServ.channels[c];
    channel._clearUserList();
  }

  dispatch("sync-header");
  updateTitle();
  updateProgress();
  updateSecurityIcon();

  client.ident.removeNetwork(this);

  if (
    "userClose" in client &&
    client.userClose &&
    client.getConnectionCount() == 0
  ) {
    window.close();
  }

  // Renew the STS policy.
  if (e.server.isSecure && "sts" in e.server.caps && client.sts.ENABLED) {
    var policy = client.sts.parseParameters(e.server.capvals.sts);
    client.sts.setPolicy(e.server.hostname, e.server.port, policy.duration);
  }

  if ("reconnect" in this && this.reconnect) {
    if ("stsUpgradePort" in this) {
      e.server.port = this.stsUpgradePort;
      e.server.isSecure = true;
      delete this.stsUpgradePort;
    }
    this.connect(this.requireSecurity);
    delete this.reconnect;
  }
};