您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CommandParser.java 8.0KB

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