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

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