選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ConditionalExecuteCommand.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.addons.conditional_execute;
  23. import com.dmdirc.commandparser.BaseCommandInfo;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandType;
  26. import com.dmdirc.commandparser.commands.Command;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import javax.annotation.Nonnull;
  33. import javax.inject.Inject;
  34. /**
  35. * The ConditionalExecute command allows the user to conditionally execute a command based on
  36. * external and pre-determined conditions.
  37. */
  38. public class ConditionalExecuteCommand extends Command {
  39. /** A command info object for this command. */
  40. public static final BaseCommandInfo INFO = new BaseCommandInfo("conditionalexecute",
  41. "conditionalexecute <args> - Conditionally execute a command", CommandType.TYPE_GLOBAL);
  42. /** Store details about current namespaces. */
  43. private final Map<String, ConditionalExecuteNamespace> namespaces = new HashMap<>();
  44. /**
  45. * Creates a new instance of this command.
  46. *
  47. * @param controller The controller to use for command information.
  48. */
  49. @Inject
  50. public ConditionalExecuteCommand(final CommandController controller) {
  51. super(controller);
  52. }
  53. @Override
  54. public void execute(@Nonnull final WindowModel origin, final CommandArguments args,
  55. final CommandContext context) {
  56. final String cmdname = args.getWordsAsString(0, 0);
  57. ConditionalExecuteNamespace namespace = null;
  58. final String[] arguments = args.getArguments();
  59. boolean manipulated = false;
  60. boolean inverse = false;
  61. for (int i = 0; i < arguments.length; i++) {
  62. final String arg = arguments[i].toLowerCase();
  63. final String nextArg = i + 1 < arguments.length ? arguments[i + 1] : "";
  64. if ("--help".equalsIgnoreCase(arg)) {
  65. showOutput(origin, args.isSilent(), "Usage:");
  66. showOutput(origin, args.isSilent(), "");
  67. showOutput(origin, args.isSilent(), cmdname + " <args>");
  68. showOutput(origin, args.isSilent(), cmdname
  69. + " --namespace <name> <namespace commands>");
  70. showOutput(origin, args.isSilent(), cmdname
  71. + " --namespace <name> [--inverse] </commandToRun <command args>>");
  72. showOutput(origin, args.isSilent(), "");
  73. showOutput(origin, args.isSilent(),
  74. "Commands can only be specified if no other non-namespace args are given.");
  75. showOutput(origin, args.isSilent(), "");
  76. showOutput(origin, args.isSilent(),
  77. "When trying to run a command, the namespace will be checked to see if the command can be run.");
  78. showOutput(origin, args.isSilent(),
  79. "The checks performed are as follows:");
  80. showOutput(origin, args.isSilent(),
  81. " 1) Does the namespace exist? if not, run the command and create the namespace.");
  82. showOutput(origin, args.isSilent(),
  83. " 2) Is the namespace inhibited? - Do not run the command.");
  84. showOutput(origin, args.isSilent(),
  85. " 3) Is the namespace in forced mode? - Run the command.");
  86. showOutput(origin, args.isSilent(),
  87. " 4) If --inverse is specified, are we under the limit time? Run the command");
  88. showOutput(origin, args.isSilent(),
  89. " 5) If --inverse is not specified, are we over the limit time? Run the command");
  90. showOutput(origin, args.isSilent(), " 6) Do not run the command.");
  91. showOutput(origin, args.isSilent(), "");
  92. showOutput(origin, args.isSilent(), "General Arguments.");
  93. showOutput(origin, args.isSilent(),
  94. " --list - List all current namespaces and their status");
  95. showOutput(origin, args.isSilent(),
  96. " --help - Print this help.");
  97. showOutput(origin, args.isSilent(),
  98. " --reset - Remove all namespaces.");
  99. showOutput(origin, args.isSilent(), "");
  100. showOutput(origin, args.isSilent(), "Useful things:");
  101. showOutput(origin, args.isSilent(),
  102. " --namespace <name> - Namespace to modify. If the namespace does not exist, it will be created. Namespaces are not remembered across sessions.");
  103. showOutput(origin, args.isSilent(), "");
  104. showOutput(origin, args.isSilent(), "Arguments related to a namespace:");
  105. showOutput(origin, args.isSilent(),
  106. " --settime <time> - Set the limit time on this namespace. Time can be either a time in seconds, 'now' for now, or 'nowifless' to set to now only if it is currently less.");
  107. showOutput(origin, args.isSilent(),
  108. " --delay <seconds> - Increase the 'limit' time on this namespace by <seconds> seconds");
  109. showOutput(origin, args.isSilent(),
  110. " --inhibit - Prevent any attempts at running commands in this namespace from executing");
  111. showOutput(origin, args.isSilent(),
  112. " --force - Any future attempts at running commands in this namespace will always execute");
  113. showOutput(origin, args.isSilent(),
  114. " --allow - Disable '--force' or '--inhibit' and resume normal operation.");
  115. showOutput(origin, args.isSilent(),
  116. " --remove - Remove this namespace.");
  117. showOutput(origin, args.isSilent(),
  118. " --status - Show the status of this namespace.");
  119. showOutput(origin, args.isSilent(), "");
  120. showOutput(origin, args.isSilent(), "Arguments when running a command:");
  121. showOutput(origin, args.isSilent(),
  122. " --inverse - Inverse the match against the 'limit' time.");
  123. return;
  124. } else if ("--list".equalsIgnoreCase(arg)) {
  125. if (namespaces.isEmpty()) {
  126. showOutput(origin, args.isSilent(),
  127. "There are currently no known namespaces.");
  128. } else {
  129. showOutput(origin, args.isSilent(), "Current namespaces: ");
  130. for (final Map.Entry<String, ConditionalExecuteNamespace> e : namespaces.
  131. entrySet()) {
  132. showOutput(origin, args.isSilent()," " + e.getValue());
  133. }
  134. }
  135. return;
  136. } else if ("--reset".equalsIgnoreCase(arg)) {
  137. namespaces.clear();
  138. showOutput(origin, args.isSilent(), "All namespaces removed.");
  139. return;
  140. } else if (namespace == null) {
  141. if ("--namespace".equalsIgnoreCase(arg)) {
  142. if (nextArg.isEmpty()) {
  143. showError(origin, args.isSilent(), "Error: You must specify a namespace.");
  144. return;
  145. } else {
  146. if (!namespaces.containsKey(nextArg.toLowerCase())) {
  147. namespaces.put(nextArg.toLowerCase(), new ConditionalExecuteNamespace(
  148. nextArg.toLowerCase()));
  149. }
  150. namespace = namespaces.get(nextArg.toLowerCase());
  151. // Skip the next argument.
  152. i++;
  153. }
  154. } else {
  155. showError(origin, args.isSilent(),
  156. "Error: You must specify a namespace first.");
  157. return;
  158. }
  159. } else if ("--inhibit".equalsIgnoreCase(arg)) {
  160. namespace.inhibit();
  161. manipulated = true;
  162. } else if ("--force".equalsIgnoreCase(arg)) {
  163. namespace.force();
  164. manipulated = true;
  165. } else if ("--allow".equalsIgnoreCase(arg)) {
  166. namespace.reset();
  167. manipulated = true;
  168. } else if ("--settime".equalsIgnoreCase(arg)) {
  169. if (nextArg.isEmpty()) {
  170. showError(origin, args.isSilent(), "Error: You must provide a time to use.");
  171. return;
  172. } else if ("now".equalsIgnoreCase(nextArg)) {
  173. namespace.setLimit(System.currentTimeMillis());
  174. i++;
  175. manipulated = true;
  176. } else if ("nowifless".equalsIgnoreCase(nextArg)) {
  177. if (namespace.getLimitTime() < System.currentTimeMillis()) {
  178. namespace.setLimit(System.currentTimeMillis());
  179. }
  180. i++;
  181. manipulated = true;
  182. } else {
  183. try {
  184. namespace.setLimit(Long.parseLong(nextArg) * 1000);
  185. i++;
  186. manipulated = true;
  187. } catch (final NumberFormatException nfe) {
  188. showError(origin, args.isSilent(), "Error: Invalid time: " + nextArg);
  189. return;
  190. }
  191. }
  192. } else if ("--delay".equalsIgnoreCase(arg)) {
  193. if (nextArg.isEmpty()) {
  194. showError(origin, args.isSilent(), "Error: You must provide a delay to use.");
  195. return;
  196. } else {
  197. try {
  198. namespace.changeLimit(Long.parseLong(nextArg) * 1000);
  199. i++;
  200. manipulated = true;
  201. } catch (final NumberFormatException nfe) {
  202. showError(origin, args.isSilent(), "Error: Invalid delay: " + nextArg);
  203. return;
  204. }
  205. }
  206. } else if ("--remove".equalsIgnoreCase(arg)) {
  207. namespaces.remove(namespace.getName());
  208. showOutput(origin, args.isSilent(),
  209. "Removed namespace '" + namespace.getName() + '\'');
  210. return;
  211. } else if ("--status".equalsIgnoreCase(arg)) {
  212. // Show the current status, in case some manipulations occurred prior to this.
  213. showOutput(origin, args.isSilent(), namespaces.get(namespace.getName()).toString());
  214. return;
  215. } else if ("--inverse".equalsIgnoreCase(arg)) {
  216. inverse = true;
  217. } else if (manipulated) {
  218. showError(origin, args.isSilent(),
  219. "You can't run commands and manipulate the namespace at the same time, ignored.");
  220. } else {
  221. // Command to run!
  222. if (namespace.canRun(inverse) && origin.isWritable()) {
  223. origin.getCommandParser().parseCommand(origin, args.getArgumentsAsString(i));
  224. }
  225. return;
  226. }
  227. }
  228. // If we get here, we either manipulated something, or should show the usage text.
  229. if (manipulated) {
  230. showOutput(origin, args.isSilent(), "Namespace updated.");
  231. showOutput(origin, args.isSilent(), namespace.toString());
  232. namespaces.put(namespace.getName(), namespace);
  233. } else {
  234. showError(origin, args.isSilent(), "Usage:");
  235. showError(origin, args.isSilent(), "");
  236. showError(origin, args.isSilent(), cmdname + " <args>");
  237. showError(origin, args.isSilent(), cmdname
  238. + " --namespace <name> <namespace commands>");
  239. showError(origin, args.isSilent(), cmdname
  240. + " --namespace <name> [--inverse] </commandToRun <command args>>");
  241. showError(origin, args.isSilent(), "");
  242. showError(origin, args.isSilent(), "For more information, see " + cmdname
  243. + " --help");
  244. }
  245. }
  246. }