File Source: CommandManager.java

         /* 
    P/P   *  Method: com.dmdirc.commandparser.CommandManager$1__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;
    24  
    25  import com.dmdirc.Query;
    26  import com.dmdirc.Server;
    27  import com.dmdirc.ServerManager;
    28  import com.dmdirc.commandparser.commands.Command;
    29  import com.dmdirc.commandparser.commands.channel.*;
    30  import com.dmdirc.commandparser.commands.chat.*;
    31  import com.dmdirc.commandparser.commands.global.*;
    32  //import com.dmdirc.commandparser.commands.query.*;
    33  import com.dmdirc.commandparser.commands.server.*;
    34  import com.dmdirc.commandparser.parsers.CommandParser;
    35  import com.dmdirc.interfaces.ConfigChangeListener;
    36  import com.dmdirc.config.IdentityManager;
    37  import com.dmdirc.ui.input.TabCompleter;
    38  import com.dmdirc.ui.input.TabCompletionType;
    39  import com.dmdirc.util.MapList;
    40  
    41  import java.util.ArrayList;
    42  import java.util.HashMap;
    43  import java.util.List;
    44  import java.util.Map;
    45  
    46  /**
    47   * The command manager creates and manages a single instance of all commands,
    48   * and provides methods to load each group of commands into a parser instance.
    49   * 
    50   * @author chris
    51   */
         /* 
    P/P   *  Method: char access$102(char)
          * 
          *  Postconditions:
          *    return_value == x0
          *    init'ed(return_value)
          *    silenceChar == return_value
          */
    52  public final class CommandManager {
    53      
    54      /** A list of commands that have been instansiated. */
             /* 
    P/P       *  Method: com.dmdirc.commandparser.CommandManager__static_init
              * 
              *  Preconditions:
              *    init'ed(com/dmdirc/config/IdentityManager.globalconfig)
              *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
              * 
              *  Postconditions:
              *    "commandchar"._tainted == 0
              *    "general"._tainted == 0
              *    "silencechar"._tainted == 0
              *    java.lang.StringBuilder:toString(...)._tainted == 0
              *    new ArrayList(getSources#1) num objects == 0
              *    new ConfigManager(getGlobalConfig#1) num objects == 0
              *    new MapList(ConfigManager#1) num objects == 0
              *    com/dmdirc/config/IdentityManager.globalconfig != null
              *    init'ed(commandChar)
              *    commands == &new HashMap(CommandManager__static_init#1)
              *    ...
              */
    55      private static final Map<CommandInfo, Command> commands
    56              = new HashMap<CommandInfo, Command>();
    57      
    58      /** A list of command parsers that have been instansiated. */
    59      private static final MapList<CommandType, CommandParser> parsers
    60              = new MapList<CommandType, CommandParser>();
    61      
    62      /** The command char we're using. */
    63      private static char commandChar = IdentityManager.getGlobalConfig()
    64              .getOptionChar("general", "commandchar");
    65      
    66      /** The silence char we're using. */
    67      private static char silenceChar = IdentityManager.getGlobalConfig()
    68              .getOptionChar("general", "silencechar");
    69      
    70      /**
    71       * Prevents creation of a new command manager.
    72       */
             /* 
    P/P       *  Method: void com.dmdirc.commandparser.CommandManager()
              */
    73      private CommandManager() {
    74          // Do nothing
    75      }
    76      
    77      /**
    78       * Returns the current command character.
    79       *
    80       * @return the current command char
    81       */
    82      public static char getCommandChar() {
                 /* 
    P/P           *  Method: char getCommandChar()
                  * 
                  *  Preconditions:
                  *    init'ed(commandChar)
                  * 
                  *  Postconditions:
                  *    return_value == commandChar
                  *    init'ed(return_value)
                  */
    83          return commandChar;
    84      }
    85      
    86      /**
    87       * Returns the current silence character.
    88       *
    89       * @return the current silence char
    90       */
    91      public static char getSilenceChar() {
                 /* 
    P/P           *  Method: char getSilenceChar()
                  * 
                  *  Preconditions:
                  *    init'ed(silenceChar)
                  * 
                  *  Postconditions:
                  *    return_value == silenceChar
                  *    init'ed(return_value)
                  */
    92          return silenceChar;
    93      }
    94      
    95      /**
    96       * Registers a command with the command manager.
    97       * 
    98       * @param command The command to be registered
    99       * @param info The information about the command
   100       * @since 0.6.3m1
   101       */
   102      public static void registerCommand(final Command command, final CommandInfo info) {
                 /* 
    P/P           *  Method: void registerCommand(Command, CommandInfo)
                  * 
                  *  Preconditions:
                  *    init'ed(commandChar)
                  *    info != null
                  */
   103          registerCommand(info, command, true);
   104      }
   105  
   106      /**
   107       * Registers a {@link Command} which also implements the {@link CommandInfo}
   108       * interface with the command manager.
   109       *
   110       * @param <T> The type of object that's being registered
   111       * @param command An object that extends {@link Command} and implements
   112       * {@link CommandInfo} to be registered.
   113       * @since 0.6.3m1
   114       */
   115      public static <T extends Command & CommandInfo> void registerCommand(final T command) {
                 /* 
    P/P           *  Method: void registerCommand(Command)
                  * 
                  *  Preconditions:
                  *    command != null
                  *    init'ed(commandChar)
                  */
   116          registerCommand(command, command);
   117      }
   118      
   119      /**
   120       * Unregisters a command with the command manager.
   121       * 
   122       * @param info The information object for the command that should be unregistered
   123       * @since 0.6.3m1
   124       */
   125      public static void unregisterCommand(final CommandInfo info) {
                 /* 
    P/P           *  Method: void unregisterCommand(CommandInfo)
                  * 
                  *  Preconditions:
                  *    init'ed(commandChar)
                  *    info != null
                  */
   126          registerCommand(info, commands.get(info), false);
   127      }
   128      
   129      /**
   130       * Registers or unregisters a command.
   131       *
   132       * @param info The information about the command
   133       * @param command The command to be (un)registered
   134       * @param register True if the command should be registered, false if it
   135       * should be unregistered.
   136       * @since 0.6.3m1
   137       */
   138      private static void registerCommand(final CommandInfo info, final Command command,
   139              final boolean register) {
                 /* 
    P/P           *  Method: void registerCommand(CommandInfo, Command, bool)
                  * 
                  *  Preconditions:
                  *    init'ed(commandChar)
                  *    info != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.util.MapList:get(...)@141 != null
                  * 
                  *  Test Vectors:
                  *    register: {0}, {1}
                  *    com.dmdirc.util.MapList:containsKey(...)@140: {0}, {1}
                  */
   140          if (parsers.containsKey(info.getType())) {
   141              registerCommand(info, command, parsers.get(info.getType()), register);
   142          }
   143          
   144          if (register) {
   145              commands.put(info, command);
   146          } else {
   147              commands.remove(info);
   148          }
   149  
   150          registerCommandName(info, register);
   151      }
   152      
   153      /**
   154       * Registers the specified command with all of the specified parsers.
   155       *
   156       * @param info The command information object
   157       * @param command The command to be reigstered
   158       * @param parsers The parsers to register the command with
   159       * @since 0.6.3m1
   160       */
   161      private static void registerCommand(final CommandInfo info, final Command command,
   162              final List<? extends CommandParser> myParsers, final boolean register) {
                 /* 
    P/P           *  Method: void registerCommand(CommandInfo, Command, List, bool)
                  * 
                  *  Preconditions:
                  *    myParsers != null
                  *    (soft) info != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@163 != null
                  *    parser.commands@163 != null
                  * 
                  *  Test Vectors:
                  *    register: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@163: {0}, {1}
                  */
   163          for (CommandParser parser : myParsers) {
   164              if (register) {
   165                  parser.registerCommand(command, info);
   166              } else {
   167                  parser.unregisterCommand(info);
   168              }
   169          }
   170      }
   171      
   172      /**
   173       * Registers or unregisters the specified command's name with the relevant
   174       * tab completers.
   175       *
   176       * @param command The command to be registered
   177       * @param register True if the command should be registered, false if it
   178       * should be unregistered.
   179       * @since 0.6.3m1
   180       */
   181      private static void registerCommandName(final CommandInfo command,
   182              final boolean register) {
   183          // Do tab completion
                 /* 
    P/P           *  Method: void registerCommandName(CommandInfo, bool)
                  * 
                  *  Preconditions:
                  *    command != null
                  *    init'ed(commandChar)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Channel:getTabCompleter(...)@196 != null
                  *    com.dmdirc.Query:getTabCompleter(...)@204 != null
                  *    com.dmdirc.Server:getChannel(...)@196 != null
                  *    com.dmdirc.Server:getChannels(...)@195 != null
                  *    com.dmdirc.Server:getQueries(...)@203 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@187: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@195: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@203: {0}, {1}
                  */
   184          final String commandName = getCommandChar() + command.getName();
   185  
   186          // TODO: This logic is probably in two places. Abstract it.
   187          for (Server server : ServerManager.getServerManager().getServers()) {
   188              if (command.getType() == CommandType.TYPE_SERVER ||
   189                      command.getType() == CommandType.TYPE_GLOBAL) {
   190                  registerCommandName(server.getTabCompleter(), commandName, register);
   191              }
   192              
   193              if (command.getType() == CommandType.TYPE_CHANNEL
   194                      || command.getType() == CommandType.TYPE_CHAT) {
   195                  for (String channelName : server.getChannels()) {
   196                      registerCommandName(server.getChannel(channelName).getTabCompleter(),
   197                              commandName, register);
   198                  }
   199              }
   200              
   201              if (command.getType() == CommandType.TYPE_QUERY
   202                      || command.getType() == CommandType.TYPE_CHAT) {
   203                  for (Query query : server.getQueries()) {
   204                      registerCommandName(query.getTabCompleter(),
   205                              commandName, register);
   206                  }
   207              }
   208          }
   209      }
   210      
   211      /**
   212       * Registers or unregisters the specified command with the specified tab-
   213       * completer.
   214       *
   215       * @param completer The tab completer to be used
   216       * @param name The command name to be registered
   217       * @param register True if the command should be registered, false if it
   218       * should be unregistered.
   219       */
   220      private static void registerCommandName(final TabCompleter completer,
   221              final String name, final boolean register) {
                 /* 
    P/P           *  Method: void registerCommandName(TabCompleter, String, bool)
                  * 
                  *  Preconditions:
                  *    completer != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.ui.input.TabCompletionType.COMMAND)
                  * 
                  *  Test Vectors:
                  *    register: {0}, {1}
                  */
   222          if (register) {
   223              completer.addEntry(TabCompletionType.COMMAND, name);
   224          }  else {
   225              completer.removeEntry(TabCompletionType.COMMAND, name);
   226          }
   227      }
   228          
   229      /**
   230       * Instansiates the default commands.
   231       */
   232      public static void initCommands() {
   233          // Chat commands
                 /* 
    P/P           *  Method: void initCommands()
                  * 
                  *  Preconditions:
                  *    init'ed(commandChar)
                  *    init'ed(com/dmdirc/config/IdentityManager.globalconfig)
                  *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
                  * 
                  *  Presumptions:
                  *    getGlobalConfig(...).listeners != null
                  * 
                  *  Postconditions:
                  *    com/dmdirc/config/IdentityManager.globalconfig != null
                  *    java.lang.StringBuilder:toString(...)._tainted == 0
                  *    new ArrayList(getSources#1) num objects == 0
                  *    new ConfigManager(getGlobalConfig#1) num objects == 0
                  *    new MapList(ConfigManager#1) num objects == 0
                  *    new ArrayList(getSources#1) num objects <= 1
                  *    new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
                  *    new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
                  *    new ConfigManager(getGlobalConfig#1).channel == &amp;java.lang.StringBuilder:toString(...)
                  *    init'ed(new ConfigManager(getGlobalConfig#1).channel)
                  *    ...
                  */
   234          new Me();
   235          
   236          // Channel commands
   237          new Ban();
   238          new ChannelSettings();
   239          new Cycle();
   240          new KickReason();
   241          new Mode();
   242          new Names();
   243          new Part();
   244          new SetNickColour();
   245          new ShowTopic();
   246          
   247          // Server commands
   248          new AllChannels();
   249          new Away();
   250          new Back();
   251          new ChangeServer();
   252          new Ctcp();
   253          new Disconnect();
   254          new Ignore();
   255          new Message();
   256          new Nick();
   257          new Notice();
   258          new OpenQuery();
   259          new Raw();
   260          new Reconnect();
   261          new Umode();
   262          
   263          new RawServerCommand("invite");
   264          new RawServerCommand("join");
   265          new RawServerCommand("lusers");
   266          new RawServerCommand("map");
   267          new RawServerCommand("motd");
   268          new RawServerCommand("oper");
   269          new RawServerCommand("whois");
   270          new RawServerCommand("who");
   271          
   272          // Query commands
   273          
   274          // Global commands
   275          new Active();
   276          new AliasCommand();
   277          new AllServers();
   278          new Clear();
   279          new Debug();
   280          new Echo();
   281          new Exit();
   282          new Help();
   283          new Ifplugin();
   284          new NewServer();
   285          new Notify();
   286          new LoadPlugin();
   287          new UnloadPlugin();
   288          new OpenWindow();
   289          new ReloadActions();
   290          new ReloadIdentities();
   291          new ReloadPlugin();
   292          new SaveConfig();
   293          new Set();
   294          
   295          // Set up a listener for config changes
                 /* 
    P/P           *  Method: void com.dmdirc.commandparser.CommandManager$1()
                  */
   296          final ConfigChangeListener listener = new ConfigChangeListener() {
   297              @Override
   298              public void configChanged(String domain, String key) {
                         /* 
    P/P                   *  Method: void configChanged(String, String)
                          * 
                          *  Preconditions:
                          *    init'ed(com/dmdirc/config/IdentityManager.globalconfig)
                          *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
                          * 
                          *  Postconditions:
                          *    init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                          *    init'ed(com/dmdirc/commandparser/CommandManager.silenceChar)
                          *    com/dmdirc/config/IdentityManager.globalconfig != null
                          *    java.lang.StringBuilder:toString(...)._tainted == 0
                          *    new ArrayList(getSources#1) num objects == 0
                          *    new ConfigManager(getGlobalConfig#1) num objects == 0
                          *    new MapList(ConfigManager#1) num objects == 0
                          *    new ArrayList(getSources#1) num objects <= 1
                          *    new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
                          *    new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
                          *    ...
                          */
   299                  commandChar = IdentityManager.getGlobalConfig()
   300                          .getOptionChar("general", "commandchar");
   301                  silenceChar = IdentityManager.getGlobalConfig()
   302                          .getOptionChar("general", "silencechar");
   303              }
   304          };
   305          
   306          IdentityManager.getGlobalConfig().addChangeListener("general", "commandchar", listener);
   307          IdentityManager.getGlobalConfig().addChangeListener("general", "silencechar", listener);
   308      }
   309  
   310      /**
   311       * Loads all commands of the specified types into the specified parser.
   312       *
   313       * @see CommandType#getComponentTypes()
   314       * @since 0.6.3m1
   315       * @param parser The {@link CommandParser} to load commands in to
   316       * @param supertypes The types of commands that should be loaded
   317       */
   318      public static void loadCommands(final CommandParser parser,
   319              final CommandType ... supertypes) {
                 /* 
    P/P           *  Method: void loadCommands(CommandParser, CommandType[])
                  * 
                  *  Preconditions:
                  *    supertypes != null
                  *    supertypes.length <= 232-1
                  *    (soft) parser != null
                  *    (soft) parser.commands != null
                  *    (soft) supertypes[...] != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@322 != null
                  *    java.util.Map:entrySet(...)@322 != null
                  *    java.util.Map_Entry:getKey(...)@323 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@322: {0}, {1}
                  */
   320          for (CommandType supertype : supertypes) {
   321              for (CommandType type : supertype.getComponentTypes()) {
   322                  for (Map.Entry<CommandInfo, Command> pair : getCommands(type, null).entrySet()) {
   323                      parser.registerCommand(pair.getValue(), pair.getKey());
   324                  }
   325  
   326                  parsers.add(type, parser);
   327              }
   328          }
   329      }
   330      
   331      /**
   332       * Retrieves the command identified by the specified name, regardless of
   333       * type.
   334       *
   335       * @param name The name to look for
   336       * @return A command with a matching signature, or null if none were found
   337       */
   338      public static Map.Entry<CommandInfo, Command> getCommand(final String name) {
                 /* 
    P/P           *  Method: Map$Entry getCommand(String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   339          return getCommand(null, name);
   340      }
   341      
   342      /**
   343       * Retrieves a command of the specified type with the specified name.
   344       * 
   345       * @param type The type of the command to look for
   346       * @param name The name to look for
   347       * @return A command with a matching signature, or null if none were found
   348       */
   349      public static Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
   350              final String name) {
                 /* 
    P/P           *  Method: Map$Entry getCommand(CommandType, String)
                  * 
                  *  Presumptions:
                  *    java.util.Map:entrySet(...)@353 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   351          final Map<CommandInfo, Command> res = getCommands(type, name);
   352          
   353          return res.isEmpty() ? null : res.entrySet().iterator().next();
   354      }    
   355       
   356      /**
   357       * Determines if the specified command is a valid channel command.
   358       * 
   359       * @param command The name of the command to test
   360       * @return True iff the command is a channel command, false otherwise
   361       */
   362      public static boolean isChannelCommand(final String command) {
                 /* 
    P/P           *  Method: bool isChannelCommand(String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   363          return getCommand(CommandType.TYPE_CHANNEL, command) != null
   364                  || getCommand(CommandType.TYPE_CHAT, command) != null;
   365      }
   366         
   367      /**
   368       * Retrieves a list of the names of all commands of the specified type.
   369       * 
   370       * @param type The type of command to list
   371       * @return A list of command names
   372       */
   373      public static List<String> getCommandNames(final CommandType type) {
                 /* 
    P/P           *  Method: List getCommandNames(CommandType)
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(commandChar)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@376 != null
                  *    java.util.Map:keySet(...)@376 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getCommandNames#1)
                  *    new ArrayList(getCommandNames#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@376: {0}, {1}
                  */
   374          final List<String> res = new ArrayList<String>();
   375          
   376          for (CommandInfo command : getCommands(type).keySet()) {
   377              res.add(getCommandChar() + command.getName());
   378          }
   379          
   380          return res;
   381      }
   382      
   383      /**
   384       * Retrieves a map of all {@link CommandInfo}s and their associated
   385       * {@link Command}s of the specified type.
   386       * 
   387       * @param type The type of command to list
   388       * @return A map of commands
   389       * @since 0.6.3m1
   390       */    
   391      public static Map<CommandInfo, Command> getCommands(final CommandType type) {
                 /* 
    P/P           *  Method: Map getCommands(CommandType)
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getCommands#1*)
                  *    new HashMap(getCommands#1*) num objects == 1
                  */
   392          return getCommands(type, null);
   393      }
   394      
   395      /**
   396       * Retrieves a map of all commands of the specified type, with the
   397       * specified name.
   398       * 
   399       * @param type The type of command to list, or null for all types
   400       * @param name The name of the command to look for, or null for any name
   401       * @return A map of {@link CommandInfo}s and their associated {@link Command}.
   402       * @since 0.6.3m1
   403       */    
   404      private static Map<CommandInfo, Command> getCommands(final CommandType type,
   405              final String name) {
                 /* 
    P/P           *  Method: Map getCommands(CommandType, String)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@408 != null
                  *    java.util.Map:entrySet(...)@408 != null
                  *    java.util.Map_Entry:getKey(...)@409 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getCommands#1)
                  *    new HashMap(getCommands#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    name: Addr_Set{null}, Inverse{null}
                  *    type: Addr_Set{null}, Inverse{null}
                  *    com.dmdirc.commandparser.CommandType:equals(...)@409: {0}, {1}
                  *    java.lang.String:equals(...)@409: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@408: {0}, {1}
                  */
   406          final Map<CommandInfo, Command> res = new HashMap<CommandInfo, Command>();
   407          
   408          for (Map.Entry<CommandInfo, Command> entry : commands.entrySet()) {
   409              if ((type == null || type.equals(entry.getKey().getType()))
   410                      && (name == null || name.equals(entry.getKey().getName()))) {
   411                  res.put(entry.getKey(), entry.getValue());
   412              }
   413          }
   414          
   415          return res;
   416      }
   417      
   418  }








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