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

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