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.

ConditionalExecuteCommand.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.conditional_execute;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandType;
  21. import com.dmdirc.commandparser.commands.BaseCommand;
  22. import com.dmdirc.commandparser.commands.context.CommandContext;
  23. import com.dmdirc.interfaces.CommandController;
  24. import com.dmdirc.interfaces.InputModel;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import javax.annotation.Nonnull;
  27. import javax.inject.Inject;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. /**
  31. * The ConditionalExecute command allows the user to conditionally execute a command based on
  32. * external and pre-determined conditions.
  33. */
  34. public class ConditionalExecuteCommand extends BaseCommand {
  35. /** A command info object for this command. */
  36. public static final BaseCommandInfo INFO = new BaseCommandInfo("conditionalexecute",
  37. "conditionalexecute <args> - Conditionally execute a command", CommandType.TYPE_GLOBAL);
  38. /** Store details about current namespaces. */
  39. private final Map<String, ConditionalExecuteNamespace> namespaces = new HashMap<>();
  40. /**
  41. * Creates a new instance of this command.
  42. *
  43. * @param controller The controller to use for command information.
  44. */
  45. @Inject
  46. public ConditionalExecuteCommand(final CommandController controller) {
  47. super(controller);
  48. }
  49. @Override
  50. public void execute(@Nonnull final WindowModel origin, final CommandArguments args,
  51. final CommandContext context) {
  52. final String cmdname = args.getWordsAsString(0, 0);
  53. ConditionalExecuteNamespace namespace = null;
  54. final String[] arguments = args.getArguments();
  55. boolean manipulated = false;
  56. boolean inverse = false;
  57. for (int i = 0; i < arguments.length; i++) {
  58. final String arg = arguments[i].toLowerCase();
  59. final String nextArg = i + 1 < arguments.length ? arguments[i + 1] : "";
  60. if ("--help".equalsIgnoreCase(arg)) {
  61. showOutput(origin, args.isSilent(), "Usage:");
  62. showOutput(origin, args.isSilent(), "");
  63. showOutput(origin, args.isSilent(), cmdname + " <args>");
  64. showOutput(origin, args.isSilent(), cmdname
  65. + " --namespace <name> <namespace commands>");
  66. showOutput(origin, args.isSilent(), cmdname
  67. + " --namespace <name> [--inverse] </commandToRun <command args>>");
  68. showOutput(origin, args.isSilent(), "");
  69. showOutput(origin, args.isSilent(),
  70. "Commands can only be specified if no other non-namespace args are given.");
  71. showOutput(origin, args.isSilent(), "");
  72. showOutput(origin, args.isSilent(),
  73. "When trying to run a command, the namespace will be checked to see if the command can be run.");
  74. showOutput(origin, args.isSilent(),
  75. "The checks performed are as follows:");
  76. showOutput(origin, args.isSilent(),
  77. " 1) Does the namespace exist? if not, run the command and create the namespace.");
  78. showOutput(origin, args.isSilent(),
  79. " 2) Is the namespace inhibited? - Do not run the command.");
  80. showOutput(origin, args.isSilent(),
  81. " 3) Is the namespace in forced mode? - Run the command.");
  82. showOutput(origin, args.isSilent(),
  83. " 4) If --inverse is specified, are we under the limit time? Run the command");
  84. showOutput(origin, args.isSilent(),
  85. " 5) If --inverse is not specified, are we over the limit time? Run the command");
  86. showOutput(origin, args.isSilent(), " 6) Do not run the command.");
  87. showOutput(origin, args.isSilent(), "");
  88. showOutput(origin, args.isSilent(), "General Arguments.");
  89. showOutput(origin, args.isSilent(),
  90. " --list - List all current namespaces and their status");
  91. showOutput(origin, args.isSilent(),
  92. " --help - Print this help.");
  93. showOutput(origin, args.isSilent(),
  94. " --reset - Remove all namespaces.");
  95. showOutput(origin, args.isSilent(), "");
  96. showOutput(origin, args.isSilent(), "Useful things:");
  97. showOutput(origin, args.isSilent(),
  98. " --namespace <name> - Namespace to modify. If the namespace does not exist, it will be created. Namespaces are not remembered across sessions.");
  99. showOutput(origin, args.isSilent(), "");
  100. showOutput(origin, args.isSilent(), "Arguments related to a namespace:");
  101. showOutput(origin, args.isSilent(),
  102. " --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.");
  103. showOutput(origin, args.isSilent(),
  104. " --delay <seconds> - Increase the 'limit' time on this namespace by <seconds> seconds");
  105. showOutput(origin, args.isSilent(),
  106. " --inhibit - Prevent any attempts at running commands in this namespace from executing");
  107. showOutput(origin, args.isSilent(),
  108. " --force - Any future attempts at running commands in this namespace will always execute");
  109. showOutput(origin, args.isSilent(),
  110. " --allow - Disable '--force' or '--inhibit' and resume normal operation.");
  111. showOutput(origin, args.isSilent(),
  112. " --remove - Remove this namespace.");
  113. showOutput(origin, args.isSilent(),
  114. " --status - Show the status of this namespace.");
  115. showOutput(origin, args.isSilent(), "");
  116. showOutput(origin, args.isSilent(), "Arguments when running a command:");
  117. showOutput(origin, args.isSilent(),
  118. " --inverse - Inverse the match against the 'limit' time.");
  119. return;
  120. } else if ("--list".equalsIgnoreCase(arg)) {
  121. if (namespaces.isEmpty()) {
  122. showOutput(origin, args.isSilent(),
  123. "There are currently no known namespaces.");
  124. } else {
  125. showOutput(origin, args.isSilent(), "Current namespaces: ");
  126. for (final Map.Entry<String, ConditionalExecuteNamespace> e : namespaces.
  127. entrySet()) {
  128. showOutput(origin, args.isSilent()," " + e.getValue());
  129. }
  130. }
  131. return;
  132. } else if ("--reset".equalsIgnoreCase(arg)) {
  133. namespaces.clear();
  134. showOutput(origin, args.isSilent(), "All namespaces removed.");
  135. return;
  136. } else if (namespace == null) {
  137. if ("--namespace".equalsIgnoreCase(arg)) {
  138. if (nextArg.isEmpty()) {
  139. showError(origin, args.isSilent(), "Error: You must specify a namespace.");
  140. return;
  141. } else {
  142. if (!namespaces.containsKey(nextArg.toLowerCase())) {
  143. namespaces.put(nextArg.toLowerCase(), new ConditionalExecuteNamespace(
  144. nextArg.toLowerCase()));
  145. }
  146. namespace = namespaces.get(nextArg.toLowerCase());
  147. // Skip the next argument.
  148. i++;
  149. }
  150. } else {
  151. showError(origin, args.isSilent(),
  152. "Error: You must specify a namespace first.");
  153. return;
  154. }
  155. } else if ("--inhibit".equalsIgnoreCase(arg)) {
  156. namespace.inhibit();
  157. manipulated = true;
  158. } else if ("--force".equalsIgnoreCase(arg)) {
  159. namespace.force();
  160. manipulated = true;
  161. } else if ("--allow".equalsIgnoreCase(arg)) {
  162. namespace.reset();
  163. manipulated = true;
  164. } else if ("--settime".equalsIgnoreCase(arg)) {
  165. if (nextArg.isEmpty()) {
  166. showError(origin, args.isSilent(), "Error: You must provide a time to use.");
  167. return;
  168. } else if ("now".equalsIgnoreCase(nextArg)) {
  169. namespace.setLimit(System.currentTimeMillis());
  170. i++;
  171. manipulated = true;
  172. } else if ("nowifless".equalsIgnoreCase(nextArg)) {
  173. if (namespace.getLimitTime() < System.currentTimeMillis()) {
  174. namespace.setLimit(System.currentTimeMillis());
  175. }
  176. i++;
  177. manipulated = true;
  178. } else {
  179. try {
  180. namespace.setLimit(Long.parseLong(nextArg) * 1000);
  181. i++;
  182. manipulated = true;
  183. } catch (final NumberFormatException nfe) {
  184. showError(origin, args.isSilent(), "Error: Invalid time: " + nextArg);
  185. return;
  186. }
  187. }
  188. } else if ("--delay".equalsIgnoreCase(arg)) {
  189. if (nextArg.isEmpty()) {
  190. showError(origin, args.isSilent(), "Error: You must provide a delay to use.");
  191. return;
  192. } else {
  193. try {
  194. namespace.changeLimit(Long.parseLong(nextArg) * 1000);
  195. i++;
  196. manipulated = true;
  197. } catch (final NumberFormatException nfe) {
  198. showError(origin, args.isSilent(), "Error: Invalid delay: " + nextArg);
  199. return;
  200. }
  201. }
  202. } else if ("--remove".equalsIgnoreCase(arg)) {
  203. namespaces.remove(namespace.getName());
  204. showOutput(origin, args.isSilent(),
  205. "Removed namespace '" + namespace.getName() + '\'');
  206. return;
  207. } else if ("--status".equalsIgnoreCase(arg)) {
  208. // Show the current status, in case some manipulations occurred prior to this.
  209. showOutput(origin, args.isSilent(), namespaces.get(namespace.getName()).toString());
  210. return;
  211. } else if ("--inverse".equalsIgnoreCase(arg)) {
  212. inverse = true;
  213. } else if (manipulated) {
  214. showError(origin, args.isSilent(),
  215. "You can't run commands and manipulate the namespace at the same time, ignored.");
  216. } else {
  217. // Command to run!
  218. if (namespace.canRun(inverse)) {
  219. final String command = args.getArgumentsAsString(i);
  220. origin.getInputModel().map(InputModel::getCommandParser)
  221. .ifPresent(cp -> cp.parseCommand(origin, command));
  222. }
  223. return;
  224. }
  225. }
  226. // If we get here, we either manipulated something, or should show the usage text.
  227. if (manipulated) {
  228. showOutput(origin, args.isSilent(), "Namespace updated.");
  229. showOutput(origin, args.isSilent(), namespace.toString());
  230. namespaces.put(namespace.getName(), namespace);
  231. } else {
  232. showError(origin, args.isSilent(), "Usage:");
  233. showError(origin, args.isSilent(), "");
  234. showError(origin, args.isSilent(), cmdname + " <args>");
  235. showError(origin, args.isSilent(), cmdname
  236. + " --namespace <name> <namespace commands>");
  237. showError(origin, args.isSilent(), cmdname
  238. + " --namespace <name> [--inverse] </commandToRun <command args>>");
  239. showError(origin, args.isSilent(), "");
  240. showError(origin, args.isSilent(), "For more information, see " + cmdname
  241. + " --help");
  242. }
  243. }
  244. }