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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.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.parser.common.IgnoreList;
  33. import com.dmdirc.ui.input.AdditionalTabTargets;
  34. import com.dmdirc.ui.input.TabCompletionType;
  35. import java.util.List;
  36. import java.util.regex.Pattern;
  37. import java.util.regex.PatternSyntaxException;
  38. /**
  39. * Allows the user to add/view/delete ignores.
  40. */
  41. public class Ignore extends Command implements IntelligentCommand,
  42. CommandInfo {
  43. /** {@inheritDoc} */
  44. @Override
  45. public void execute(final FrameContainer origin,
  46. final CommandArguments args, final CommandContext context) {
  47. final Server server = ((ServerCommandContext) context).getServer();
  48. if (args.getArguments().length == 0) {
  49. executeView(origin, server, args.isSilent(), args, false);
  50. } else if ("--remove".equalsIgnoreCase(args.getArguments()[0])) {
  51. executeRemove(origin, server, args.isSilent(), args);
  52. } else if ("--regex".equalsIgnoreCase(args.getArguments()[0])) {
  53. executeRegex(origin, server, args.isSilent(), args);
  54. } else {
  55. executeAdd(origin, server, args.isSilent(), args);
  56. }
  57. }
  58. protected void executeView(final FrameContainer origin, final Server server,
  59. final boolean isSilent, final CommandArguments args, final boolean forceRegex) {
  60. final IgnoreList ignoreList = server.getIgnoreList();
  61. if (ignoreList.count() == 0) {
  62. sendLine(origin, args.isSilent(), FORMAT_ERROR, "No ignore list entries for this network.");
  63. return;
  64. }
  65. final List<String> entries;
  66. if (ignoreList.canConvert() && !forceRegex) {
  67. entries = ignoreList.getSimpleList();
  68. } else {
  69. if (!forceRegex) {
  70. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  71. "Unable to convert ignore list to simple format");
  72. }
  73. entries = ignoreList.getRegexList();
  74. }
  75. int i = 0;
  76. for (String line : entries) {
  77. i++;
  78. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, i + ". " + line);
  79. }
  80. }
  81. protected void executeAdd(final FrameContainer origin, final Server server,
  82. final boolean isSilent, final CommandArguments args) {
  83. final IgnoreList ignoreList = server.getIgnoreList();
  84. final String target = args.getArgumentsAsString();
  85. ignoreList.addSimple(target);
  86. server.saveIgnoreList();
  87. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Added " + target + " to the ignore list.");
  88. }
  89. protected void executeRegex(final FrameContainer origin, final Server server,
  90. final boolean isSilent, final CommandArguments args) {
  91. if (args.getArguments().length == 1) {
  92. executeView(origin, server, args.isSilent(), args, true);
  93. return;
  94. }
  95. final IgnoreList ignoreList = server.getIgnoreList();
  96. final String target = args.getArgumentsAsString(1);
  97. try {
  98. Pattern.compile(target);
  99. } catch (PatternSyntaxException ex) {
  100. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Unable to compile regex: "
  101. + ex.getDescription());
  102. return;
  103. }
  104. ignoreList.add(target);
  105. server.saveIgnoreList();
  106. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Added " + target + " to the ignore list.");
  107. }
  108. protected void executeRemove(final FrameContainer origin, final Server server,
  109. final boolean isSilent, final CommandArguments args) {
  110. if (args.getArguments().length == 1) {
  111. showUsage(origin, args.isSilent(), "ignore", "--remove <host>");
  112. return;
  113. }
  114. final IgnoreList ignoreList = server.getIgnoreList();
  115. final String host = args.getArgumentsAsString(1);
  116. if (ignoreList.canConvert() && ignoreList.getSimpleList().contains(host)) {
  117. ignoreList.remove(ignoreList.getSimpleList().indexOf(host));
  118. server.saveIgnoreList();
  119. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Removed " + host + " from the ignore list.");
  120. return;
  121. }
  122. if (ignoreList.getRegexList().contains(host)) {
  123. ignoreList.remove(ignoreList.getRegexList().indexOf(host));
  124. server.saveIgnoreList();
  125. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Removed " + host + " from the ignore list.");
  126. return;
  127. }
  128. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Ignore list doesn't contain '" + host + "'.");
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public String getName() {
  133. return "ignore";
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public boolean showInHelp() {
  138. return true;
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public CommandType getType() {
  143. return CommandType.TYPE_SERVER;
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public String getHelp() {
  148. return "ignore [--remove|--regex] [host] - manages the network's ignore list";
  149. }
  150. /** {@inheritDoc} */
  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 && context.getPreviousArgs().get(0).equals("--regex")) {
  162. targets.include(TabCompletionType.CHANNEL_NICK);
  163. targets.include(TabCompletionType.QUERY_NICK);
  164. }
  165. return targets;
  166. }
  167. }