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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2006-2010 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. CommandManager.registerCommand(this);
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public void execute(final FrameContainer<?> origin,
  54. final CommandArguments args, final CommandContext context) {
  55. if (args.getArguments().length < 2) {
  56. showUsage(origin, args.isSilent(), "alias", "[--remove] <name> [command]");
  57. return;
  58. }
  59. if (args.getArguments()[0].equalsIgnoreCase("--remove")) {
  60. final String name
  61. = args.getArguments()[1].charAt(0) == CommandManager.getCommandChar()
  62. ? args.getArguments()[1].substring(1) : args.getArguments()[1];
  63. if (doRemove(name)) {
  64. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Alias '" + name +
  65. "' removed.");
  66. } else {
  67. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Alias '" + name +
  68. "' not found.");
  69. }
  70. return;
  71. }
  72. final String name = args.getArguments()[0].charAt(0) == CommandManager.getCommandChar()
  73. ? args.getArguments()[0].substring(1) : args.getArguments()[0];
  74. for (Action alias : AliasWrapper.getAliasWrapper()) {
  75. if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
  76. name)) {
  77. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Alias '" + name +
  78. "' already exists.");
  79. return;
  80. }
  81. }
  82. final Alias myAlias = new Alias(name);
  83. myAlias.setResponse(new String[]{args.getArgumentsAsString(1)});
  84. myAlias.createAction().save();
  85. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Alias '" + name +
  86. "' created.");
  87. }
  88. /**
  89. * Removes the alias with the specified name.
  90. *
  91. * @param name The name of the alias to remove
  92. * @return True if the alias was deleted, false otherwise
  93. */
  94. private static boolean doRemove(final String name) {
  95. for (Action alias : AliasWrapper.getAliasWrapper()) {
  96. if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
  97. name)) {
  98. alias.delete();
  99. ActionManager.unregisterAction(alias);
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public String getName() {
  108. return "alias";
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public boolean showInHelp() {
  113. return true;
  114. }
  115. /** {@inheritDoc} */
  116. @Override
  117. public CommandType getType() {
  118. return CommandType.TYPE_GLOBAL;
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. public String getHelp() {
  123. return "alias [--remove] <name> [command] - creates or removes the specified alias";
  124. }
  125. /** {@inheritDoc} */
  126. @Override
  127. public AdditionalTabTargets getSuggestions(final int arg,
  128. final IntelligentCommandContext context) {
  129. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  130. if (arg == 0) {
  131. res.add("--remove");
  132. } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--remove")) {
  133. for (Action alias : AliasWrapper.getAliasWrapper()) {
  134. res.add(AliasWrapper.getCommandName(alias));
  135. }
  136. } else if (arg >= 1 && !context.getPreviousArgs().get(0).equals("--remove")) {
  137. return TabCompleter.getIntelligentResults(arg, context, 1);
  138. }
  139. return res;
  140. }
  141. }