/* * Copyright (c) 2006-2015 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.commandparser.BaseCommandInfo; import com.dmdirc.commandparser.CommandArguments; import com.dmdirc.commandparser.CommandType; import com.dmdirc.commandparser.commands.BaseCommand; import com.dmdirc.commandparser.commands.context.CommandContext; import com.dmdirc.interfaces.CommandController; import com.dmdirc.interfaces.InputModel; import com.dmdirc.interfaces.WindowModel; import javax.annotation.Nonnull; import javax.inject.Inject; import java.util.HashMap; import java.util.Map; /** * The ConditionalExecute command allows the user to conditionally execute a command based on * external and pre-determined conditions. */ public class ConditionalExecuteCommand extends BaseCommand { /** 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 WindowModel 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 ("--help".equalsIgnoreCase(arg)) { showOutput(origin, args.isSilent(), "Usage:"); showOutput(origin, args.isSilent(), ""); showOutput(origin, args.isSilent(), cmdname + " "); showOutput(origin, args.isSilent(), cmdname + " --namespace "); showOutput(origin, args.isSilent(), cmdname + " --namespace [--inverse] >"); showOutput(origin, args.isSilent(), ""); showOutput(origin, args.isSilent(), "Commands can only be specified if no other non-namespace args are given."); showOutput(origin, args.isSilent(), ""); showOutput(origin, args.isSilent(), "When trying to run a command, the namespace will be checked to see if the command can be run."); showOutput(origin, args.isSilent(), "The checks performed are as follows:"); showOutput(origin, args.isSilent(), " 1) Does the namespace exist? if not, run the command and create the namespace."); showOutput(origin, args.isSilent(), " 2) Is the namespace inhibited? - Do not run the command."); showOutput(origin, args.isSilent(), " 3) Is the namespace in forced mode? - Run the command."); showOutput(origin, args.isSilent(), " 4) If --inverse is specified, are we under the limit time? Run the command"); showOutput(origin, args.isSilent(), " 5) If --inverse is not specified, are we over the limit time? Run the command"); showOutput(origin, args.isSilent(), " 6) Do not run the command."); showOutput(origin, args.isSilent(), ""); showOutput(origin, args.isSilent(), "General Arguments."); showOutput(origin, args.isSilent(), " --list - List all current namespaces and their status"); showOutput(origin, args.isSilent(), " --help - Print this help."); showOutput(origin, args.isSilent(), " --reset - Remove all namespaces."); showOutput(origin, args.isSilent(), ""); showOutput(origin, args.isSilent(), "Useful things:"); showOutput(origin, args.isSilent(), " --namespace - Namespace to modify. If the namespace does not exist, it will be created. Namespaces are not remembered across sessions."); showOutput(origin, args.isSilent(), ""); showOutput(origin, args.isSilent(), "Arguments related to a namespace:"); showOutput(origin, args.isSilent(), " --settime