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

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