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 5.3KB

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