File Source: NewServer.java

         /* 
    P/P   *  Method: com.dmdirc.commandparser.commands.global.NewServer__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.commandparser.commands.global;
    24  
    25  import com.dmdirc.Server;
    26  import com.dmdirc.commandparser.CommandArguments;
    27  import com.dmdirc.commandparser.CommandManager;
    28  import com.dmdirc.commandparser.commands.GlobalCommand;
    29  import com.dmdirc.config.IdentityManager;
    30  import com.dmdirc.logger.ErrorLevel;
    31  import com.dmdirc.logger.Logger;
    32  import com.dmdirc.ui.interfaces.InputWindow;
    33  
    34  /**
    35   * The new server command allows users to open a new server window.
    36   * 
    37   * @author chris
    38   */
    39  public final class NewServer extends GlobalCommand {
    40      
    41      /**
    42       * Creates a new instance of NewServer.
    43       */
    44      public NewServer() {
                 /* 
    P/P           *  Method: void com.dmdirc.commandparser.commands.global.NewServer()
                  * 
                  *  Preconditions:
                  *    init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  */
    45          super();
    46          
    47          CommandManager.registerCommand(this);
    48      }
    49      
    50      /** {@inheritDoc} */
    51      @Override
    52      public void execute(final InputWindow origin, final boolean isSilent,
    53              final CommandArguments args) {
                 /* 
    P/P           *  Method: void execute(InputWindow, bool, CommandArguments)
                  * 
                  *  Preconditions:
                  *    args != null
                  *    init'ed(args.words)
                  *    (soft) args.line != null
                  *    (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  *    (soft) origin != null
                  * 
                  *  Presumptions:
                  *    offset < getArguments(...).length@76
                  *    offset < getArguments(...).length@77
                  *    offset < getArguments(...).length@97
                  *    init'ed(com.dmdirc.logger.ErrorLevel.LOW)
                  *    getArguments(...).length@66 >= 1
                  *    ...
                  * 
                  *  Postconditions:
                  *    args.words != null
                  *    init'ed(java.lang.String:split(...)._tainted)
                  *    java.lang.String:split(...)._tainted == 0
                  *    init'ed(java.lang.String:split(...).length)
                  * 
                  *  Test Vectors:
                  *    getArguments(...).length@54: {1..+Inf}, {0}
                  *    java.lang.String:charAt(...)@80: {0..42, 44..216-1}, {43}
                  *    java.lang.String:equalsIgnoreCase(...)@66: {0}, {1}
                  *    java.lang.String:indexOf(...)@76: {-231..-1}, {0..232-1}
                  *    java.lang.String:length(...)@80: {0}, {1..232-1}
                  */
    54          if (args.getArguments().length == 0) {
    55              showUsage(origin, isSilent, "newserver", "<host[:[+]port]> [password]");
    56              return;
    57          }
    58          
    59          boolean ssl = false;
    60          String host = "";
    61          String pass = "";
    62          int port = 6667;
    63          int offset = 0;
    64          
    65          // Check for SSL
    66          if (args.getArguments()[offset].equalsIgnoreCase("--ssl")) {
    67              Logger.userError(ErrorLevel.LOW, 
    68                      "Using /newserver --ssl is deprecated, and may be removed in the future."
    69                      + " Use /newserver <host>:+<port> instead.");
    70              
    71              ssl = true;
    72              offset++;
    73          }
    74          
    75          // Check for port
    76          if (args.getArguments()[offset].indexOf(':') > -1) {
    77              final String[] parts = args.getArguments()[offset].split(":");
    78              host = parts[0];
    79              
    80              if (parts[1].length() > 0 && parts[1].charAt(0) == '+') {
    81                  ssl = true;
    82                  parts[1] = parts[1].substring(1);
    83              }
    84              
    85              try {
    86                  port = Integer.parseInt(parts[1]);
    87              } catch (NumberFormatException ex) {
    88                  origin.addLine(FORMAT_ERROR, "Invalid port specified");
    89                  return;
    90              }
    91              
    92              if (port <= 0 || port > 65535) {
    93                  sendLine(origin, isSilent, FORMAT_ERROR, "Port must be between 1 and 65535");
    94                  return;
    95              }            
    96          } else {
    97              host = args.getArguments()[offset];
    98          }
    99          
   100          // Check for password
   101          if (args.getArguments().length > ++offset) {
   102              pass = args.getArgumentsAsString(offset);
   103          }
   104          
   105          new Server(host, port, pass, ssl, IdentityManager.getProfiles().get(0));
   106      }
   107      
   108      
   109      
   110      /** {@inheritDoc}. */
   111      @Override
   112      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"newserver"
                  */
   113          return "newserver";
   114      }
   115      
   116      /** {@inheritDoc}. */
   117      @Override
   118      public boolean showInHelp() {
                 /* 
    P/P           *  Method: bool showInHelp()
                  * 
                  *  Postconditions:
                  *    return_value == 1
                  */
   119          return true;
   120      }
   121      
   122      /** {@inheritDoc}. */
   123      @Override
   124      public String getHelp() {
                 /* 
    P/P           *  Method: String getHelp()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"newserver <host[:[+]port]> [password] - connect to a new server"
                  */
   125          return "newserver <host[:[+]port]> [password] - connect to a new server";
   126      }
   127      
   128  }








SofCheck Inspector Build Version : 2.17854
NewServer.java 2009-Jun-25 01:54:24
NewServer.class 2009-Sep-02 17:04:16