Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CommandParser.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2006-2007 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. package com.dmdirc.commandparser;
  23. import java.util.Hashtable;
  24. import java.util.Map;
  25. import com.dmdirc.Config;
  26. import com.dmdirc.actions.ActionManager;
  27. import com.dmdirc.actions.CoreActionType;
  28. /**
  29. * Represents a generic command parser. A command parser takes a line of input
  30. * from the user, determines if it is an attempt at executing a command (based
  31. * on the character at the start of the string), and handles it appropriately.
  32. * @author chris
  33. */
  34. public abstract class CommandParser {
  35. /**
  36. * Commands that are associated with this parser.
  37. */
  38. private final Map<String, Command> commands;
  39. /** Creates a new instance of CommandParser. */
  40. public CommandParser() {
  41. commands = new Hashtable<String, Command>();
  42. loadCommands();
  43. }
  44. /** Loads the relevant commands into the parser. */
  45. protected abstract void loadCommands();
  46. /**
  47. * Registers the specified command with this parser.
  48. * @param command Command to be registered
  49. */
  50. public final void registerCommand(final Command command) {
  51. commands.put(command.getSignature().toLowerCase(), command);
  52. }
  53. /**
  54. * Unregisters the specified command with this parser.
  55. * @param command Command to be unregistered
  56. */
  57. public final void unregisterCommand(final Command command) {
  58. commands.remove(command.getSignature().toLowerCase());
  59. }
  60. /**
  61. * Parses the specified string as a command.
  62. * @param origin The window in which the command was typed
  63. * @param line The line to be parsed
  64. * @param parseChannel Whether or not to try and parse the first argument
  65. * as a channel name
  66. */
  67. public final void parseCommand(final CommandWindow origin,
  68. final String line, final boolean parseChannel) {
  69. if (line.length() == 0) {
  70. return;
  71. }
  72. if (line.charAt(0) == Config.getCommandChar().charAt(0)) {
  73. final String[] args = line.split(" ");
  74. final String command = args[0].substring(1);
  75. String[] comargs;
  76. assert args.length > 0;
  77. if (args.length >= 2 && parseChannel
  78. && (origin == null || origin.getServer().getParser().isValidChannelName(args[1]))
  79. && CommandManager.isChannelCommand(command)) {
  80. if (origin.getServer().hasChannel(args[1])) {
  81. final StringBuilder newLine = new StringBuilder();
  82. for (int i = 0; i < args.length; i++) {
  83. if (i == 1) { continue; }
  84. newLine.append(" ").append(args[i]);
  85. }
  86. origin.getServer().getChannel(args[1]).getFrame().getCommandParser().parseCommand(origin, newLine.substring(1), false);
  87. return;
  88. } else {
  89. // Do something haxy involving external commands here...
  90. }
  91. }
  92. comargs = new String[args.length - 1];
  93. System.arraycopy(args, 1, comargs, 0, args.length - 1);
  94. final String signature = command + "/" + (comargs.length);
  95. // Check the specific signature first, so that polyadic commands can
  96. // have error handlers if there are too few arguments (e.g., msg/0 and
  97. // msg/1 would return errors, so msg only gets called with 2+ args).
  98. if (commands.containsKey(signature.toLowerCase())) {
  99. executeCommand(origin, false, commands.get(signature.toLowerCase()), comargs);
  100. } else if (commands.containsKey(command.toLowerCase())) {
  101. executeCommand(origin, false, commands.get(command.toLowerCase()), comargs);
  102. } else {
  103. handleInvalidCommand(origin, command, comargs);
  104. }
  105. } else {
  106. handleNonCommand(origin, line);
  107. }
  108. }
  109. /**
  110. * Parses the specified string as a command.
  111. * @param origin The window in which the command was typed
  112. * @param line The line to be parsed
  113. */
  114. public final void parseCommand(final CommandWindow origin,
  115. final String line) {
  116. parseCommand(origin, line, true);
  117. }
  118. /**
  119. * Handles the specified string as a non-command.
  120. * @param origin The window in which the command was typed
  121. * @param line The line to be parsed
  122. */
  123. public final void parseCommandCtrl(final CommandWindow origin, final String line) {
  124. handleNonCommand(origin, line);
  125. }
  126. /**
  127. * Executes the specified command with the given arguments.
  128. * @param origin The window in which the command was typed
  129. * @param isSilent Whether the command is being silenced or not
  130. * @param command The command to be executed
  131. * @param args The arguments to the command
  132. */
  133. protected abstract void executeCommand(final CommandWindow origin,
  134. final boolean isSilent, final Command command, final String... args);
  135. /**
  136. * Called when the user attempted to issue a command (i.e., used the command
  137. * character) that wasn't found. It could be that the command has a different
  138. * arity, or that it plain doesn't exist.
  139. * @param origin The window in which the command was typed
  140. * @param command The command the user tried to execute
  141. * @param args The arguments passed to the command
  142. */
  143. protected void handleInvalidCommand(final CommandWindow origin,
  144. final String command, final String... args) {
  145. if (origin == null) {
  146. ActionManager.processEvent(CoreActionType.UNKNOWN_COMMAND, null,
  147. null, command, args);
  148. } else {
  149. final StringBuffer buff = new StringBuffer("unknownCommand");
  150. ActionManager.processEvent(CoreActionType.UNKNOWN_COMMAND, buff,
  151. origin.getContainer(), command, args);
  152. origin.addLine(buff, command + "/" + args.length);
  153. }
  154. }
  155. /**
  156. * Called when the input was a line of text that was not a command. This normally
  157. * means it is sent to the server/channel/user as-is, with no further processing.
  158. * @param origin The window in which the command was typed
  159. * @param line The line input by the user
  160. */
  161. protected abstract void handleNonCommand(final CommandWindow origin,
  162. final String line);
  163. }