<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>

<dialog id="jssh_configure" title="Configure JSSh"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        buttons="accept,cancel"
        onload="init();"
        ondialogaccept="return doOK();"
        ondialogcancel="return doCancel();">
  <script>
    var enablectrl;
    var portctrl;
    var scriptctrl;

    function init() {
      enablectrl = document.getElementById("enable");      
      portctrl = document.getElementById("port");
      loopbackctrl = document.getElementById("loopback");
      scriptctrl = document.getElementById("script");      

      if (!opener.jssh_server)
        opener.jssh_server = { server: Components.classes["@mozilla.org/jssh-server;1"]
                                                 .getService(Components.interfaces.nsIJSShServer),
                               port: 9997, 
                               useLoopbackOnly: true, 
                               script: "chrome://jssh/content/jssh-debug.js" };

      // the server might have been started from elsewhere, in which
      // case we need to get the port, etc. parameters directly from the server:
      if (opener.jssh_server.server.serverListening) {
        opener.jssh_server.port   = opener.jssh_server.server.serverPort;
        opener.jssh_server.script = opener.jssh_server.server.serverStartupURI;
        opener.jssh_server.useLoopbackOnly = opener.jssh_server.server.serverLoopbackOnly;
      }

      enablectrl.checked = (opener.jssh_server.server.serverListening);
      portctrl.value = opener.jssh_server.port;
      loopbackctrl.checked = opener.jssh_server.useLoopbackOnly;
      scriptctrl.value = opener.jssh_server.script;
    }

    function doOK() {
      // stop current server instance:
      if (opener.jssh_server.server.serverListening) {
        opener.jssh_server.server.stopServerSocket();
      }
      
      // take new values from ui:
      opener.jssh_server.port = portctrl.value;
      opener.jssh_server.useLoopbackOnly = loopbackctrl.checked;
      opener.jssh_server.script = scriptctrl.value;

      // start new server instance if requested:
      if (enablectrl.checked) {
        try {
          opener.jssh_server.server.startServerSocket(opener.jssh_server.port, opener.jssh_server.script, 
                                                      opener.jssh_server.useLoopbackOnly);
          alert("The JSSh server is now running on port "+opener.jssh_server.port+".");
        }catch(e){
          alert("The JSSh server could not be started at the given port");
          return false;
        }
      }
      return true;
    }

    function doCancel() {
      return true;
    }
  </script>

  <description value="JSSh - JavaScript TCP/IP shell server"/>
  <description maxwidth="300px">
Warning: If your computer is connected to the internet and you haven't
got a properly configured firewall, enabling JSSh on other interfaces
besides the loopback interface means that *ANYONE* can connect to your
machine and execute potentially harmful code!
  </description>
  <checkbox id="enable" label="Listen for connections"/>
  <grid>
    <columns>
      <column/>
      <column flex="1"/>
    </columns>
    <rows>
      <row>  
        <label control="port" value="Port to listen on:"/>
        <textbox id="port" size="5"/>
      </row>
      <row>
        <label control="script" value="Startup script:"/>
        <textbox id="script" />
      </row>
    </rows>
  </grid>
  <checkbox id="loopback" label="Restrict to local loopback interface (127.0.0.1/localhost)"/>
</dialog>