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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2012 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.ActionManager;
  26. import com.dmdirc.actions.wrappers.Alias;
  27. import com.dmdirc.actions.wrappers.AliasWrapper;
  28. import com.dmdirc.commandparser.BaseCommandInfo;
  29. import com.dmdirc.commandparser.CommandArguments;
  30. import com.dmdirc.commandparser.CommandInfo;
  31. import com.dmdirc.commandparser.CommandManager;
  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.ui.input.AdditionalTabTargets;
  37. import com.dmdirc.ui.input.TabCompleter;
  38. /**
  39. * The alias command allows users to create aliases on-the-fly.
  40. */
  41. public class AliasCommand extends Command implements IntelligentCommand {
  42. /** A command info object for this command. */
  43. public static final CommandInfo INFO = new BaseCommandInfo("alias",
  44. "alias [--remove] <name> [command] - creates or removes the specified alias",
  45. CommandType.TYPE_GLOBAL);
  46. /** {@inheritDoc} */
  47. @Override
  48. public void execute(final FrameContainer origin,
  49. final CommandArguments args, final CommandContext context) {
  50. if (args.getArguments().length < 2) {
  51. showUsage(origin, args.isSilent(), "alias", "[--remove] <name> [command]");
  52. return;
  53. }
  54. if (args.getArguments()[0].equalsIgnoreCase("--remove")) {
  55. final String name
  56. = args.getArguments()[1].charAt(0)
  57. == CommandManager.getCommandManager().getCommandChar()
  58. ? args.getArguments()[1].substring(1) : args.getArguments()[1];
  59. if (doRemove(name)) {
  60. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Alias '"
  61. + name + "' removed.");
  62. } else {
  63. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Alias '"
  64. + name + "' not found.");
  65. }
  66. return;
  67. }
  68. final String name = args.getArguments()[0].charAt(0)
  69. == CommandManager.getCommandManager().getCommandChar()
  70. ? args.getArguments()[0].substring(1) : args.getArguments()[0];
  71. for (Action alias : AliasWrapper.getAliasWrapper()) {
  72. if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
  73. name)) {
  74. sendLine(origin, args.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, args.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 static 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.getActionManager().removeAction(alias);
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public AdditionalTabTargets getSuggestions(final int arg,
  105. final IntelligentCommandContext context) {
  106. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  107. if (arg == 0) {
  108. res.add("--remove");
  109. } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--remove")) {
  110. for (Action alias : AliasWrapper.getAliasWrapper()) {
  111. res.add(AliasWrapper.getCommandName(alias));
  112. }
  113. } else if (arg >= 1 && !context.getPreviousArgs().get(0).equals("--remove")) {
  114. return TabCompleter.getIntelligentResults(arg, context, 1);
  115. }
  116. return res;
  117. }
  118. }