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 8.1KB

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