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.

AliasCommandHandler.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.aliases;
  18. import com.dmdirc.commandparser.CommandArguments;
  19. import com.dmdirc.commandparser.commands.BaseCommand;
  20. import com.dmdirc.commandparser.commands.Command;
  21. import com.dmdirc.commandparser.commands.context.CommandContext;
  22. import com.dmdirc.interfaces.CommandController;
  23. import com.dmdirc.interfaces.InputModel;
  24. import com.dmdirc.interfaces.WindowModel;
  25. import javax.annotation.Nonnull;
  26. /**
  27. * {@link Command} implementation that handles an alias.
  28. */
  29. public class AliasCommandHandler extends BaseCommand {
  30. private final Alias alias;
  31. public AliasCommandHandler(final CommandController controller, final Alias alias) {
  32. super(controller);
  33. this.alias = alias;
  34. }
  35. @Override
  36. public void execute(@Nonnull final WindowModel origin, final CommandArguments args,
  37. final CommandContext context) {
  38. if (args.getArguments().length >= alias.getMinArguments()) {
  39. for (String line : alias.getSubstitution().split("\n")) {
  40. origin.getInputModel().map(InputModel::getCommandParser)
  41. .ifPresent(cp -> cp.parseCommand(origin,
  42. getSubstituteCommand(line, args)));
  43. }
  44. } else {
  45. showError(origin, args.isSilent(), alias.getName() + " requires at least "
  46. + alias.getMinArguments() + " argument"
  47. + (alias.getMinArguments() == 1 ? "" : "s") + '.');
  48. }
  49. }
  50. /**
  51. * Gets the command that should be executed, with the appropriate substitutions made.
  52. * <p>
  53. * The returned command will have arguments substituted (replacing "$1-", "$2", etc), and will
  54. * be silenced if the given args are silent.
  55. *
  56. * @param line The line to substitute.
  57. * @param args The arguments entered by the user.
  58. *
  59. * @return The substituted command to execute.
  60. */
  61. private String getSubstituteCommand(final String line, final CommandArguments args) {
  62. final StringBuilder builder = new StringBuilder(line.trim());
  63. final String[] arguments = args.getArguments();
  64. for (int i = 0; i < arguments.length; i++) {
  65. replaceAll(builder, "$" + (i + 1) + '-', args.getArgumentsAsString(i));
  66. replaceAll(builder, "$" + (i + 1), arguments[i]);
  67. }
  68. if (args.isSilent()) {
  69. builder.insert(0, getController().getSilenceChar());
  70. }
  71. builder.insert(0, getController().getCommandChar());
  72. return builder.toString();
  73. }
  74. /**
  75. * Replaces all instances of the specified substring in the builder.
  76. *
  77. * @param builder The builder to modify.
  78. * @param substr The substring to replace.
  79. * @param replacement The string to use as a replacement.
  80. */
  81. private static void replaceAll(final StringBuilder builder,
  82. final String substr, final String replacement) {
  83. int index = builder.indexOf(substr);
  84. while (index > -1) {
  85. builder.replace(index, index + substr.length(), replacement);
  86. index = builder.indexOf(substr);
  87. }
  88. }
  89. }