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

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