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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.commandparser.aliases;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.Command;
  26. import com.dmdirc.commandparser.commands.context.CommandContext;
  27. import com.dmdirc.interfaces.CommandController;
  28. import javax.annotation.Nonnull;
  29. /**
  30. * {@link Command} implementation that handles an alias.
  31. */
  32. public class AliasCommandHandler extends Command {
  33. private final Alias alias;
  34. public AliasCommandHandler(final CommandController controller, final Alias alias) {
  35. super(controller);
  36. this.alias = alias;
  37. }
  38. @Override
  39. public void execute(@Nonnull final FrameContainer origin, final CommandArguments args,
  40. final CommandContext context) {
  41. if (args.getArguments().length >= alias.getMinArguments()) {
  42. for (String line : alias.getSubstitution().split("\n")) {
  43. origin.getCommandParser().parseCommand(origin, getSubstituteCommand(line, args));
  44. }
  45. } else {
  46. sendLine(origin, args.isSilent(), FORMAT_ERROR, alias.getName() + " requires at least "
  47. + alias.getMinArguments() + " argument"
  48. + (alias.getMinArguments() == 1 ? "" : "s") + ".");
  49. }
  50. }
  51. /**
  52. * Gets the command that should be executed, with the appropriate substitutions made.
  53. * <p>
  54. * The returned command will have arguments substituted (replacing "$1-", "$2", etc), and will
  55. * be silenced if the given args are silent.
  56. *
  57. * @param line The line to substitute.
  58. * @param args The arguments entered by the user.
  59. *
  60. * @return The substituted command to execute.
  61. */
  62. private String getSubstituteCommand(final String line, final CommandArguments args) {
  63. final StringBuilder builder = new StringBuilder(line.trim());
  64. final String[] arguments = args.getArguments();
  65. for (int i = 0; i < arguments.length; i++) {
  66. replaceAll(builder, "$" + (i + 1) + "-", args.getArgumentsAsString(i));
  67. replaceAll(builder, "$" + (i + 1), arguments[i]);
  68. }
  69. if (args.isSilent()) {
  70. builder.insert(0, getController().getSilenceChar());
  71. }
  72. builder.insert(0, getController().getCommandChar());
  73. return builder.toString();
  74. }
  75. /**
  76. * Replaces all instances of the specified substring in the builder.
  77. *
  78. * @param builder The builder to modify.
  79. * @param substr The substring to replace.
  80. * @param replacement The string to use as a replacement.
  81. */
  82. private static void replaceAll(final StringBuilder builder,
  83. final String substr, final String replacement) {
  84. int index = builder.indexOf(substr);
  85. while (index > -1) {
  86. builder.replace(index, index + substr.length(), replacement);
  87. index = builder.indexOf(substr);
  88. }
  89. }
  90. }