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

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