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.

Ignore.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2006-2015 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.server;
  23. import com.dmdirc.commandparser.BaseCommandInfo;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.BaseCommand;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.commandparser.commands.context.CommandContext;
  30. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  31. import com.dmdirc.interfaces.CommandController;
  32. import com.dmdirc.interfaces.Connection;
  33. import com.dmdirc.interfaces.WindowModel;
  34. import com.dmdirc.parser.common.IgnoreList;
  35. import com.dmdirc.ui.input.AdditionalTabTargets;
  36. import com.dmdirc.ui.input.TabCompletionType;
  37. import javax.annotation.Nonnull;
  38. import javax.inject.Inject;
  39. import java.util.List;
  40. import java.util.regex.Pattern;
  41. import java.util.regex.PatternSyntaxException;
  42. import java.util.stream.Collectors;
  43. /**
  44. * Allows the user to add/view/delete ignores.
  45. */
  46. public class Ignore extends BaseCommand implements IntelligentCommand {
  47. /** A command info object for this command. */
  48. public static final CommandInfo INFO = new BaseCommandInfo("ignore",
  49. "ignore [--remove|--regex] [host] - manages the network's ignore list",
  50. CommandType.TYPE_SERVER);
  51. /**
  52. * Creates a new instance of this command.
  53. *
  54. * @param controller The controller to use for command information.
  55. */
  56. @Inject
  57. public Ignore(final CommandController controller) {
  58. super(controller);
  59. }
  60. @Override
  61. public void execute(@Nonnull final WindowModel origin,
  62. final CommandArguments args, final CommandContext context) {
  63. final Connection connection = ((ServerCommandContext) context).getConnection();
  64. if (args.getArguments().length == 0) {
  65. executeView(origin, connection, args, false);
  66. } else if ("--remove".equalsIgnoreCase(args.getArguments()[0])) {
  67. executeRemove(origin, connection, args);
  68. } else if ("--regex".equalsIgnoreCase(args.getArguments()[0])) {
  69. executeRegex(origin, connection, args);
  70. } else {
  71. executeAdd(origin, connection, args);
  72. }
  73. }
  74. protected void executeView(final WindowModel origin, final Connection connection,
  75. final CommandArguments args, final boolean forceRegex) {
  76. final IgnoreList ignoreList = connection.getIgnoreList();
  77. if (ignoreList.count() == 0) {
  78. showError(origin, args.isSilent(), "No ignore list entries for this network.");
  79. return;
  80. }
  81. final List<String> entries;
  82. if (ignoreList.canConvert() && !forceRegex) {
  83. entries = ignoreList.getSimpleList();
  84. } else {
  85. if (!forceRegex) {
  86. showError(origin, args.isSilent(),
  87. "Unable to convert ignore list to simple format");
  88. }
  89. entries = ignoreList.getRegexList();
  90. }
  91. int i = 0;
  92. for (String line : entries) {
  93. i++;
  94. showOutput(origin, args.isSilent(), i + ". " + line);
  95. }
  96. }
  97. protected void executeAdd(final WindowModel origin, final Connection connection,
  98. final CommandArguments args) {
  99. final IgnoreList ignoreList = connection.getIgnoreList();
  100. final String target = args.getArgumentsAsString();
  101. ignoreList.addSimple(target);
  102. connection.saveIgnoreList();
  103. showOutput(origin, args.isSilent(), "Added " + target + " to the ignore list.");
  104. }
  105. protected void executeRegex(final WindowModel origin, final Connection connection,
  106. final CommandArguments args) {
  107. if (args.getArguments().length == 1) {
  108. executeView(origin, connection, args, true);
  109. return;
  110. }
  111. final IgnoreList ignoreList = connection.getIgnoreList();
  112. final String target = args.getArgumentsAsString(1);
  113. try {
  114. //noinspection ResultOfMethodCallIgnored
  115. Pattern.compile(target);
  116. } catch (PatternSyntaxException ex) {
  117. showError(origin, args.isSilent(), "Unable to compile regex: " + ex.getDescription());
  118. return;
  119. }
  120. ignoreList.add(target);
  121. connection.saveIgnoreList();
  122. showOutput(origin, args.isSilent(), "Added " + target + " to the ignore list.");
  123. }
  124. protected void executeRemove(final WindowModel origin, final Connection connection,
  125. final CommandArguments args) {
  126. if (args.getArguments().length == 1) {
  127. showUsage(origin, args.isSilent(), "ignore", "--remove <host>");
  128. return;
  129. }
  130. final IgnoreList ignoreList = connection.getIgnoreList();
  131. final String host = args.getArgumentsAsString(1);
  132. if (ignoreList.canConvert() && ignoreList.getSimpleList().contains(host)) {
  133. ignoreList.remove(ignoreList.getSimpleList().indexOf(host));
  134. connection.saveIgnoreList();
  135. showOutput(origin, args.isSilent(), "Removed " + host
  136. + " from the ignore list.");
  137. return;
  138. }
  139. if (ignoreList.getRegexList().contains(host)) {
  140. ignoreList.remove(ignoreList.getRegexList().indexOf(host));
  141. connection.saveIgnoreList();
  142. showOutput(origin, args.isSilent(), "Removed " + host
  143. + " from the ignore list.");
  144. return;
  145. }
  146. showError(origin, args.isSilent(), "Ignore list doesn't contain '" + host + "'.");
  147. }
  148. @Override
  149. public AdditionalTabTargets getSuggestions(final int arg,
  150. final IntelligentCommandContext context) {
  151. final AdditionalTabTargets targets = new AdditionalTabTargets();
  152. targets.excludeAll();
  153. if (arg == 0) {
  154. targets.add("--regex");
  155. targets.add("--remove");
  156. targets.include(TabCompletionType.CHANNEL_NICK);
  157. targets.include(TabCompletionType.QUERY_NICK);
  158. } else if (arg == 1 && "--remove".equals(context.getPreviousArgs().get(0))) {
  159. final IgnoreList ignoreList = context.getWindow().getConnection()
  160. .get().getIgnoreList();
  161. if (ignoreList.canConvert()) {
  162. targets.addAll(ignoreList.getSimpleList().stream().collect(Collectors.toList()));
  163. }
  164. targets.addAll(ignoreList.getRegexList().stream().collect(Collectors.toList()));
  165. } else if (arg == 1 && "--regex".equals(context.getPreviousArgs().get(0))) {
  166. targets.include(TabCompletionType.CHANNEL_NICK);
  167. targets.include(TabCompletionType.QUERY_NICK);
  168. }
  169. return targets;
  170. }
  171. }