Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Ignore.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.commands.server;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  26. import com.dmdirc.interfaces.CommandController;
  27. import com.dmdirc.interfaces.Connection;
  28. import com.dmdirc.interfaces.WindowModel;
  29. import com.dmdirc.parser.common.IgnoreList;
  30. import com.dmdirc.ui.input.AdditionalTabTargets;
  31. import com.dmdirc.ui.input.TabCompletionType;
  32. import javax.annotation.Nonnull;
  33. import javax.inject.Inject;
  34. import java.util.List;
  35. import java.util.regex.Pattern;
  36. import java.util.regex.PatternSyntaxException;
  37. import java.util.stream.Collectors;
  38. /**
  39. * Allows the user to add/view/delete ignores.
  40. */
  41. public class Ignore extends BaseCommand implements IntelligentCommand {
  42. /** A command info object for this command. */
  43. public static final CommandInfo INFO = new BaseCommandInfo("ignore",
  44. "ignore [--remove|--regex] [host] - manages the network's ignore list",
  45. CommandType.TYPE_SERVER);
  46. /**
  47. * Creates a new instance of this command.
  48. *
  49. * @param controller The controller to use for command information.
  50. */
  51. @Inject
  52. public Ignore(final CommandController controller) {
  53. super(controller);
  54. }
  55. @Override
  56. public void execute(@Nonnull final WindowModel origin,
  57. final CommandArguments args, final CommandContext context) {
  58. final Connection connection = ((ServerCommandContext) context).getConnection();
  59. if (args.getArguments().length == 0) {
  60. executeView(origin, connection, args, false);
  61. } else if ("--remove".equalsIgnoreCase(args.getArguments()[0])) {
  62. executeRemove(origin, connection, args);
  63. } else if ("--regex".equalsIgnoreCase(args.getArguments()[0])) {
  64. executeRegex(origin, connection, args);
  65. } else {
  66. executeAdd(origin, connection, args);
  67. }
  68. }
  69. protected void executeView(final WindowModel origin, final Connection connection,
  70. final CommandArguments args, final boolean forceRegex) {
  71. final IgnoreList ignoreList = connection.getIgnoreList();
  72. if (ignoreList.count() == 0) {
  73. showError(origin, args.isSilent(), "No ignore list entries for this network.");
  74. return;
  75. }
  76. final List<String> entries;
  77. if (ignoreList.canConvert() && !forceRegex) {
  78. entries = ignoreList.getSimpleList();
  79. } else {
  80. if (!forceRegex) {
  81. showError(origin, args.isSilent(),
  82. "Unable to convert ignore list to simple format");
  83. }
  84. entries = ignoreList.getRegexList();
  85. }
  86. int i = 0;
  87. for (String line : entries) {
  88. i++;
  89. showOutput(origin, args.isSilent(), i + ". " + line);
  90. }
  91. }
  92. protected void executeAdd(final WindowModel origin, final Connection connection,
  93. final CommandArguments args) {
  94. final IgnoreList ignoreList = connection.getIgnoreList();
  95. final String target = args.getArgumentsAsString();
  96. ignoreList.addSimple(target);
  97. connection.saveIgnoreList();
  98. showOutput(origin, args.isSilent(), "Added " + target + " to the ignore list.");
  99. }
  100. protected void executeRegex(final WindowModel origin, final Connection connection,
  101. final CommandArguments args) {
  102. if (args.getArguments().length == 1) {
  103. executeView(origin, connection, args, true);
  104. return;
  105. }
  106. final IgnoreList ignoreList = connection.getIgnoreList();
  107. final String target = args.getArgumentsAsString(1);
  108. try {
  109. //noinspection ResultOfMethodCallIgnored
  110. Pattern.compile(target);
  111. } catch (PatternSyntaxException ex) {
  112. showError(origin, args.isSilent(), "Unable to compile regex: " + ex.getDescription());
  113. return;
  114. }
  115. ignoreList.add(target);
  116. connection.saveIgnoreList();
  117. showOutput(origin, args.isSilent(), "Added " + target + " to the ignore list.");
  118. }
  119. protected void executeRemove(final WindowModel origin, final Connection connection,
  120. final CommandArguments args) {
  121. if (args.getArguments().length == 1) {
  122. showUsage(origin, args.isSilent(), "ignore", "--remove <host>");
  123. return;
  124. }
  125. final IgnoreList ignoreList = connection.getIgnoreList();
  126. final String host = args.getArgumentsAsString(1);
  127. if (ignoreList.canConvert() && ignoreList.getSimpleList().contains(host)) {
  128. ignoreList.remove(ignoreList.getSimpleList().indexOf(host));
  129. connection.saveIgnoreList();
  130. showOutput(origin, args.isSilent(), "Removed " + host
  131. + " from the ignore list.");
  132. return;
  133. }
  134. if (ignoreList.getRegexList().contains(host)) {
  135. ignoreList.remove(ignoreList.getRegexList().indexOf(host));
  136. connection.saveIgnoreList();
  137. showOutput(origin, args.isSilent(), "Removed " + host
  138. + " from the ignore list.");
  139. return;
  140. }
  141. showError(origin, args.isSilent(), "Ignore list doesn't contain '" + host + "'.");
  142. }
  143. @Override
  144. public AdditionalTabTargets getSuggestions(final int arg,
  145. final IntelligentCommandContext context) {
  146. final AdditionalTabTargets targets = new AdditionalTabTargets();
  147. targets.excludeAll();
  148. if (arg == 0) {
  149. targets.add("--regex");
  150. targets.add("--remove");
  151. targets.include(TabCompletionType.CHANNEL_NICK);
  152. targets.include(TabCompletionType.QUERY_NICK);
  153. } else if (arg == 1 && "--remove".equals(context.getPreviousArgs().get(0))) {
  154. final IgnoreList ignoreList = context.getWindow().getConnection()
  155. .get().getIgnoreList();
  156. if (ignoreList.canConvert()) {
  157. targets.addAll(ignoreList.getSimpleList().stream().collect(Collectors.toList()));
  158. }
  159. targets.addAll(ignoreList.getRegexList().stream().collect(Collectors.toList()));
  160. } else if (arg == 1 && "--regex".equals(context.getPreviousArgs().get(0))) {
  161. targets.include(TabCompletionType.CHANNEL_NICK);
  162. targets.include(TabCompletionType.QUERY_NICK);
  163. }
  164. return targets;
  165. }
  166. }