/* * Copyright (c) 2006-2014 DMDirc Developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.dmdirc.addons.conditional_execute; import com.dmdirc.FrameContainer; import com.dmdirc.commandparser.BaseCommandInfo; import com.dmdirc.commandparser.CommandArguments; import com.dmdirc.commandparser.CommandType; import com.dmdirc.commandparser.commands.Command; import com.dmdirc.commandparser.commands.context.CommandContext; import com.dmdirc.interfaces.CommandController; import java.util.HashMap; import java.util.Map; import javax.annotation.Nonnull; import javax.inject.Inject; /** * The ConditionalExecute command allows the user to conditionally execute a command based on * external and pre-determined conditions. */ public class ConditionalExecuteCommand extends Command { /** A command info object for this command. */ public static final BaseCommandInfo INFO = new BaseCommandInfo("conditionalexecute", "conditionalexecute - Conditionally execute a command", CommandType.TYPE_GLOBAL); /** Store details about current namespaces. */ private final Map namespaces = new HashMap<>(); /** * Creates a new instance of this command. * * @param controller The controller to use for command information. */ @Inject public ConditionalExecuteCommand(final CommandController controller) { super(controller); } @Override public void execute(@Nonnull final FrameContainer origin, final CommandArguments args, final CommandContext context) { final String cmdname = args.getWordsAsString(0, 0); ConditionalExecuteNamespace namespace = null; final String[] arguments = args.getArguments(); boolean manipulated = false; boolean inverse = false; for (int i = 0; i < arguments.length; i++) { final String arg = arguments[i].toLowerCase(); final String nextArg = i + 1 < arguments.length ? arguments[i + 1] : ""; if (arg.equalsIgnoreCase("--help")) { sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Usage:"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, ""); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, cmdname + " "); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, cmdname + " --namespace "); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, cmdname + " --namespace [--inverse] >"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, ""); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Commands can only be specified if no other non-namespace args are given."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, ""); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "When trying to run a command, the namespace will be checked to see if the command can be run."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "The checks performed are as follows:"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 1) Does the namespace exist? if not, run the command and create the namespace."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 2) Is the namespace inhibited? - Do not run the command."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 3) Is the namespace in forced mode? - Run the command."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 4) If --inverse is specified, are we under the limit time? Run the command"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 5) If --inverse is not specified, are we over the limit time? Run the command"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 6) Do not run the command."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, ""); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "General Arguments."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --list - List all current namespaces and their status"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --help - Print this help."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --reset - Remove all namespaces."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, ""); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Useful things:"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --namespace - Namespace to modify. If the namespace does not exist, it will be created. Namespaces are not remembered across sessions."); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, ""); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Arguments related to a namespace:"); sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --settime