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

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