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.

AliasCommand.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2014 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.commands.global;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.actions.Action;
  25. import com.dmdirc.actions.ActionFactory;
  26. import com.dmdirc.actions.ActionManager;
  27. import com.dmdirc.actions.wrappers.Alias;
  28. import com.dmdirc.actions.wrappers.AliasWrapper;
  29. import com.dmdirc.commandparser.BaseCommandInfo;
  30. import com.dmdirc.commandparser.CommandArguments;
  31. import com.dmdirc.commandparser.CommandInfo;
  32. import com.dmdirc.commandparser.CommandType;
  33. import com.dmdirc.commandparser.commands.Command;
  34. import com.dmdirc.commandparser.commands.IntelligentCommand;
  35. import com.dmdirc.commandparser.commands.context.CommandContext;
  36. import com.dmdirc.interfaces.CommandController;
  37. import com.dmdirc.ui.input.AdditionalTabTargets;
  38. import com.dmdirc.ui.input.TabCompleter;
  39. import javax.inject.Inject;
  40. /**
  41. * The alias command allows users to create aliases on-the-fly.
  42. */
  43. public class AliasCommand extends Command implements IntelligentCommand {
  44. /** A command info object for this command. */
  45. public static final CommandInfo INFO = new BaseCommandInfo("alias",
  46. "alias [--remove] <name> [command] - creates or removes the specified alias",
  47. CommandType.TYPE_GLOBAL);
  48. /** Factory to use when creating aliases. */
  49. private final ActionFactory actionFactory;
  50. /** Wrapper to use to modify aliases. */
  51. private final AliasWrapper aliasWrapper;
  52. /**
  53. * Creates a new instance of {@link AliasCommand}.
  54. *
  55. * @param controller The controller that owns this command.
  56. * @param actionFactory The factory to use when creating new aliases.
  57. * @param aliasWrapper The wrapper to use to modify aliases.
  58. */
  59. @Inject
  60. public AliasCommand(
  61. final CommandController controller,
  62. final ActionFactory actionFactory,
  63. final AliasWrapper aliasWrapper) {
  64. super(controller);
  65. this.actionFactory = actionFactory;
  66. this.aliasWrapper = aliasWrapper;
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public void execute(final FrameContainer origin,
  71. final CommandArguments args, final CommandContext context) {
  72. if (args.getArguments().length < 2) {
  73. showUsage(origin, args.isSilent(), "alias", "[--remove] <name> [command]");
  74. return;
  75. }
  76. if (args.getArguments()[0].equalsIgnoreCase("--remove")) {
  77. final String name = args.getArguments()[1].charAt(0)
  78. == getController().getCommandChar()
  79. ? args.getArguments()[1].substring(1) : args.getArguments()[1];
  80. if (doRemove(name)) {
  81. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Alias '"
  82. + name + "' removed.");
  83. } else {
  84. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Alias '"
  85. + name + "' not found.");
  86. }
  87. return;
  88. }
  89. final String name = args.getArguments()[0].charAt(0)
  90. == getController().getCommandChar()
  91. ? args.getArguments()[0].substring(1) : args.getArguments()[0];
  92. for (Action alias : aliasWrapper) {
  93. if (aliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(name)) {
  94. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Alias '" + name
  95. + "' already exists.");
  96. return;
  97. }
  98. }
  99. final Alias myAlias = new Alias(actionFactory, name);
  100. myAlias.setResponse(new String[]{args.getArgumentsAsString(1)});
  101. myAlias.createAction().save();
  102. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Alias '" + name
  103. + "' created.");
  104. }
  105. /**
  106. * Removes the alias with the specified name.
  107. *
  108. * @param name The name of the alias to remove
  109. *
  110. * @return True if the alias was deleted, false otherwise
  111. */
  112. private boolean doRemove(final String name) {
  113. for (Action alias : aliasWrapper) {
  114. if (aliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(name)) {
  115. alias.delete();
  116. ActionManager.getActionManager().removeAction(alias);
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public AdditionalTabTargets getSuggestions(final int arg,
  125. final IntelligentCommandContext context) {
  126. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  127. if (arg == 0) {
  128. res.add("--remove");
  129. } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--remove")) {
  130. for (Action alias : aliasWrapper) {
  131. res.add(aliasWrapper.getCommandName(alias));
  132. }
  133. } else if (arg >= 1 && !context.getPreviousArgs().get(0).equals("--remove")) {
  134. return TabCompleter.getIntelligentResults(arg, context, 1);
  135. }
  136. return res;
  137. }
  138. }