You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.addons.debug;
  23. import com.dmdirc.commandparser.BaseCommandInfo;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.Command;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.commandparser.commands.context.CommandContext;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.WindowModel;
  32. import com.dmdirc.ui.input.AdditionalTabTargets;
  33. import java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.Collection;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import java.util.stream.Collectors;
  39. import javax.annotation.Nonnull;
  40. import javax.inject.Inject;
  41. import javax.inject.Singleton;
  42. /**
  43. * Provides various handy ways to test or debug the client.
  44. */
  45. @Singleton
  46. public class Debug extends Command implements IntelligentCommand {
  47. /** A command info object for this command. */
  48. public static final CommandInfo INFO = new BaseCommandInfo("debug",
  49. "debug <command> [args] - facilitates debugging of DMDirc",
  50. CommandType.TYPE_GLOBAL);
  51. /** The command controller to use to lookup command information. */
  52. private final CommandController controller;
  53. /** List of registered debug commands. */
  54. private final Map<String, DebugCommand> commands;
  55. /**
  56. * Creates a new debug command.
  57. *
  58. * @param controller The command controller to use to lookup command information.
  59. * @param subcommands The subcommands to be loaded.
  60. */
  61. @Inject
  62. public Debug(
  63. final CommandController controller,
  64. final Collection<DebugCommand> subcommands) {
  65. super(controller);
  66. this.controller = controller;
  67. this.commands = new HashMap<>(subcommands.size());
  68. for (DebugCommand command : subcommands) {
  69. commands.put(command.getName(), command);
  70. }
  71. }
  72. @Override
  73. public void execute(@Nonnull final WindowModel origin,
  74. final CommandArguments args, final CommandContext context) {
  75. if (args.getArguments().length == 0) {
  76. showUsage(origin, args.isSilent(), "debug",
  77. "<debug command> [options]");
  78. } else {
  79. final DebugCommand command = commands.get(args.getArguments()[0]);
  80. if (command == null) {
  81. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  82. "Unknown debug action.");
  83. } else {
  84. final CommandArguments newArgs = new CommandArguments(
  85. controller,
  86. Arrays.asList((controller.getCommandChar()
  87. + command.getName() + ' '
  88. + args.getArgumentsAsString(1)).split(" ")));
  89. command.execute(origin, newArgs, context);
  90. }
  91. }
  92. }
  93. /**
  94. * Sends a line, if appropriate, to the specified target.
  95. *
  96. * @param target The command window to send the line to
  97. * @param isSilent Whether this command is being silenced or not
  98. * @param type The type of message to send
  99. * @param args The arguments of the message
  100. */
  101. public void proxySendLine(final WindowModel target,
  102. final boolean isSilent, final String type, final Object... args) {
  103. sendLine(target, isSilent, type, args);
  104. }
  105. /**
  106. * Sends a usage line, if appropriate, to the specified target.
  107. *
  108. * @param target The command window to send the line to
  109. * @param isSilent Whether this command is being silenced or not
  110. * @param name The name of the command that's raising the error
  111. * @param args The arguments that the command accepts or expects
  112. */
  113. public void proxyShowUsage(final WindowModel target,
  114. final boolean isSilent, final String name, final String args) {
  115. showUsage(target, isSilent, INFO.getName(), name + ' ' + args);
  116. }
  117. /**
  118. * Formats the specified data into a table suitable for output in the textpane. It is expected
  119. * that each String[] in data has the same number of elements as the headers array.
  120. *
  121. * @param headers The headers of the table.
  122. * @param data The contents of the table.
  123. *
  124. * @return A string containing an ASCII table
  125. */
  126. public String proxyDoTable(final String[] headers, final String[][] data) {
  127. return doTable(headers, data);
  128. }
  129. /**
  130. * Returns a list of command names.
  131. *
  132. * @return List of command names
  133. */
  134. public Collection<String> getCommandNames() {
  135. final Collection<String> names = new ArrayList<>(commands.size());
  136. names.addAll(commands.values().stream()
  137. .map(DebugCommand::getName)
  138. .collect(Collectors.toList()));
  139. return names;
  140. }
  141. @Override
  142. public AdditionalTabTargets getSuggestions(final int arg,
  143. final IntelligentCommandContext context) {
  144. AdditionalTabTargets res = new AdditionalTabTargets();
  145. res.excludeAll();
  146. if (arg == 0) {
  147. res.addAll(getCommandNames());
  148. } else {
  149. final DebugCommand command = commands.get(context.getPreviousArgs().get(0));
  150. if (command instanceof IntelligentCommand) {
  151. final IntelligentCommandContext newContext = new IntelligentCommandContext(context.
  152. getWindow(),
  153. context.getPreviousArgs().subList(1,
  154. context.getPreviousArgs().size()),
  155. context.getPartial());
  156. res = ((IntelligentCommand) command).getSuggestions(
  157. arg, newContext);
  158. }
  159. }
  160. return res;
  161. }
  162. }