123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- /*
- * Copyright (c) 2006-2012 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.commandparser;
-
- import com.dmdirc.BasicServerFactory;
- import com.dmdirc.Query;
- import com.dmdirc.Server;
- import com.dmdirc.ServerManager;
- import com.dmdirc.commandparser.commands.Command;
- import com.dmdirc.commandparser.commands.channel.Ban;
- import com.dmdirc.commandparser.commands.channel.Cycle;
- import com.dmdirc.commandparser.commands.channel.Invite;
- import com.dmdirc.commandparser.commands.channel.KickReason;
- import com.dmdirc.commandparser.commands.channel.Mode;
- import com.dmdirc.commandparser.commands.channel.Names;
- import com.dmdirc.commandparser.commands.channel.Part;
- import com.dmdirc.commandparser.commands.channel.SetNickColour;
- import com.dmdirc.commandparser.commands.channel.ShowTopic;
- import com.dmdirc.commandparser.commands.chat.Me;
- import com.dmdirc.commandparser.commands.global.AliasCommand;
- import com.dmdirc.commandparser.commands.global.AllServers;
- import com.dmdirc.commandparser.commands.global.Clear;
- import com.dmdirc.commandparser.commands.global.Echo;
- import com.dmdirc.commandparser.commands.global.Exit;
- import com.dmdirc.commandparser.commands.global.Help;
- import com.dmdirc.commandparser.commands.global.Ifplugin;
- import com.dmdirc.commandparser.commands.global.LoadPlugin;
- import com.dmdirc.commandparser.commands.global.NewServer;
- import com.dmdirc.commandparser.commands.global.Notify;
- import com.dmdirc.commandparser.commands.global.OpenWindow;
- import com.dmdirc.commandparser.commands.global.ReloadActions;
- import com.dmdirc.commandparser.commands.global.ReloadIdentities;
- import com.dmdirc.commandparser.commands.global.ReloadPlugin;
- import com.dmdirc.commandparser.commands.global.SaveConfig;
- import com.dmdirc.commandparser.commands.global.Set;
- import com.dmdirc.commandparser.commands.global.UnloadPlugin;
- import com.dmdirc.commandparser.commands.server.AllChannels;
- import com.dmdirc.commandparser.commands.server.Away;
- import com.dmdirc.commandparser.commands.server.Back;
- import com.dmdirc.commandparser.commands.server.ChangeServer;
- import com.dmdirc.commandparser.commands.server.Ctcp;
- import com.dmdirc.commandparser.commands.server.Disconnect;
- import com.dmdirc.commandparser.commands.server.Ignore;
- import com.dmdirc.commandparser.commands.server.JoinChannelCommand;
- import com.dmdirc.commandparser.commands.server.Message;
- import com.dmdirc.commandparser.commands.server.Nick;
- import com.dmdirc.commandparser.commands.server.Notice;
- import com.dmdirc.commandparser.commands.server.OpenQuery;
- import com.dmdirc.commandparser.commands.server.Raw;
- import com.dmdirc.commandparser.commands.server.RawServerCommand;
- import com.dmdirc.commandparser.commands.server.Reconnect;
- import com.dmdirc.commandparser.commands.server.Umode;
- import com.dmdirc.commandparser.parsers.CommandParser;
- import com.dmdirc.config.ConfigBinding;
- import com.dmdirc.config.IdentityManager;
- import com.dmdirc.interfaces.CommandController;
- import com.dmdirc.ui.input.TabCompleter;
- import com.dmdirc.ui.input.TabCompletionType;
- import com.dmdirc.util.collections.MapList;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import lombok.Getter;
-
- /**
- * The command manager creates and manages a single instance of all commands,
- * and provides methods to load each group of commands into a parser instance.
- */
- @SuppressWarnings("PMD.UnusedPrivateField")
- public class CommandManager implements CommandController {
-
- /** A singleton instance of the command manager. */
- private static final CommandManager INSTANCE = new CommandManager();
-
- /** A list of commands that have been instantiated. */
- private final Map<CommandInfo, Command> commands
- = new HashMap<CommandInfo, Command>();
-
- /** A list of command parsers that have been instantiated. */
- private final MapList<CommandType, CommandParser> parsers
- = new MapList<CommandType, CommandParser>();
-
- /** The command char we're using. */
- @ConfigBinding(domain="general", key="commandchar")
- @Getter
- private char commandChar;
-
- /** The silence char we're using. */
- @ConfigBinding(domain="general", key="silencechar")
- @Getter
- private char silenceChar;
-
- /**
- * Creates a new instance of the Command Manager.
- */
- public CommandManager() {
- IdentityManager.getIdentityManager().getGlobalConfiguration()
- .getBinder().bind(this, CommandManager.class);
- }
-
- /** {@inheritDoc} */
- @Override
- public void registerCommand(final Command command, final CommandInfo info) {
- registerCommand(info, command, true);
- }
-
- /** {@inheritDoc} */
- @Override
- public <T extends Command & CommandInfo> void registerCommand(final T command) {
- registerCommand(command, command);
- }
-
- /** {@inheritDoc} */
- @Override
- public void unregisterCommand(final CommandInfo info) {
- registerCommand(info, commands.get(info), false);
- }
-
- /**
- * Registers or unregisters a command.
- *
- * @param info The information about the command
- * @param command The command to be (un)registered
- * @param register True if the command should be registered, false if it
- * should be unregistered.
- * @since 0.6.3m1
- */
- private void registerCommand(final CommandInfo info, final Command command,
- final boolean register) {
- if (parsers.containsKey(info.getType())) {
- registerCommand(info, command, parsers.get(info.getType()), register);
- }
-
- if (register) {
- commands.put(info, command);
- } else {
- commands.remove(info);
- }
-
- registerCommandName(info, register);
- }
-
- /**
- * Registers or unregisters the specified command with all of the specified parsers.
- *
- * @param info The command information object
- * @param command The command to be registered
- * @param myParsers The parsers to register the command with
- * @param register Whether to register or unregister the commands
- * @since 0.6.3m1
- */
- private void registerCommand(final CommandInfo info, final Command command,
- final List<? extends CommandParser> myParsers, final boolean register) {
- for (CommandParser parser : myParsers) {
- if (register) {
- parser.registerCommand(command, info);
- } else {
- parser.unregisterCommand(info);
- }
- }
- }
-
- /**
- * Registers or unregisters the specified command's name with the relevant
- * tab completers.
- *
- * @param command The command to be registered
- * @param register True if the command should be registered, false if it
- * should be unregistered.
- * @since 0.6.3m1
- */
- private void registerCommandName(final CommandInfo command,
- final boolean register) {
- // Do tab completion
- final String commandName = getCommandChar() + command.getName();
-
- // TODO: This logic is probably in two places. Abstract it.
- for (Server server : ServerManager.getServerManager().getServers()) {
- if (command.getType() == CommandType.TYPE_SERVER
- || command.getType() == CommandType.TYPE_GLOBAL) {
- registerCommandName(server.getTabCompleter(), commandName, register);
- }
-
- if (command.getType() == CommandType.TYPE_CHANNEL
- || command.getType() == CommandType.TYPE_CHAT) {
- for (String channelName : server.getChannels()) {
- registerCommandName(server.getChannel(channelName).getTabCompleter(),
- commandName, register);
- }
- }
-
- if (command.getType() == CommandType.TYPE_QUERY
- || command.getType() == CommandType.TYPE_CHAT) {
- for (Query query : server.getQueries()) {
- registerCommandName(query.getTabCompleter(),
- commandName, register);
- }
- }
- }
- }
-
- /**
- * Registers or unregisters the specified command with the specified tab-
- * completer.
- *
- * @param completer The tab completer to be used
- * @param name The command name to be registered
- * @param register True if the command should be registered, false if it
- * should be unregistered.
- */
- private void registerCommandName(final TabCompleter completer,
- final String name, final boolean register) {
- if (register) {
- completer.addEntry(TabCompletionType.COMMAND, name);
- } else {
- completer.removeEntry(TabCompletionType.COMMAND, name);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void initCommands() {
- // Chat commands
- registerCommand(new Me(), Me.INFO);
-
- // Channel commands
- registerCommand(new Ban(), Ban.INFO);
- registerCommand(new Cycle(), Cycle.INFO);
- registerCommand(new Invite(), Invite.INFO);
- registerCommand(new KickReason(), KickReason.INFO);
- registerCommand(new Mode(), Mode.INFO);
- registerCommand(new Names(), Names.INFO);
- registerCommand(new Part(), Part.INFO);
- registerCommand(new SetNickColour(), SetNickColour.INFO);
- registerCommand(new ShowTopic(), ShowTopic.INFO);
-
- // Server commands
- registerCommand(new AllChannels(), AllChannels.INFO);
- registerCommand(new Away(), Away.INFO);
- registerCommand(new Back(), Back.INFO);
- registerCommand(new ChangeServer(), ChangeServer.INFO);
- registerCommand(new Ctcp(), Ctcp.INFO);
- registerCommand(new Disconnect(), Disconnect.INFO);
- registerCommand(new Ignore(), Ignore.INFO);
- registerCommand(new JoinChannelCommand(), JoinChannelCommand.INFO);
- registerCommand(new Message(), Message.INFO);
- registerCommand(new Nick(), Nick.INFO);
- registerCommand(new Notice(), Notice.INFO);
- registerCommand(new OpenQuery(), OpenQuery.INFO);
- registerCommand(new Raw(), Raw.INFO);
- registerCommand(new Reconnect(), Reconnect.INFO);
- registerCommand(new Umode(), Umode.INFO);
-
- registerCommand(new RawServerCommand("lusers"));
- registerCommand(new RawServerCommand("map"));
- registerCommand(new RawServerCommand("motd"));
- registerCommand(new RawServerCommand("oper"));
- registerCommand(new RawServerCommand("whois"));
- registerCommand(new RawServerCommand("who"));
-
- // Query commands
-
- // Global commands
- registerCommand(new AliasCommand(), AliasCommand.INFO);
- registerCommand(new AllServers(), AllServers.INFO);
- registerCommand(new Clear(), Clear.INFO);
- registerCommand(new Echo(), Echo.INFO);
- registerCommand(new Exit(), Exit.INFO);
- registerCommand(new Help(), Help.INFO);
- registerCommand(new Ifplugin(), Ifplugin.INFO);
- registerCommand(new NewServer(new BasicServerFactory()), NewServer.INFO);
- registerCommand(new Notify(), Notify.INFO);
- registerCommand(new LoadPlugin(), LoadPlugin.INFO);
- registerCommand(new UnloadPlugin(), UnloadPlugin.INFO);
- registerCommand(new OpenWindow(), OpenWindow.INFO);
- registerCommand(new ReloadActions(), ReloadActions.INFO);
- registerCommand(new ReloadIdentities(), ReloadIdentities.INFO);
- registerCommand(new ReloadPlugin(), ReloadPlugin.INFO);
- registerCommand(new SaveConfig(), SaveConfig.INFO);
- registerCommand(new Set(), Set.INFO);
- }
-
- /** {@inheritDoc} */
- @Override
- public void loadCommands(final CommandParser parser,
- final CommandType ... supertypes) {
- for (CommandType supertype : supertypes) {
- for (CommandType type : supertype.getComponentTypes()) {
- for (Map.Entry<CommandInfo, Command> pair : getCommands(type, null).entrySet()) {
- parser.registerCommand(pair.getValue(), pair.getKey());
- }
-
- parsers.add(type, parser);
- }
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public Map.Entry<CommandInfo, Command> getCommand(final String name) {
- return getCommand(null, name);
- }
-
- /** {@inheritDoc} */
- @Override
- public Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
- final String name) {
- final Map<CommandInfo, Command> res = getCommands(type, name);
-
- return res.isEmpty() ? null : res.entrySet().iterator().next();
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean isChannelCommand(final String command) {
- return getCommand(CommandType.TYPE_CHANNEL, command) != null
- || getCommand(CommandType.TYPE_CHAT, command) != null;
- }
-
- /** {@inheritDoc} */
- @Override
- public List<String> getCommandNames(final CommandType type) {
- final List<String> res = new ArrayList<String>();
-
- for (CommandInfo command : getCommands(type).keySet()) {
- res.add(getCommandChar() + command.getName());
- }
-
- return res;
- }
-
- /** {@inheritDoc} */
- @Override
- public Map<CommandInfo, Command> getCommands(final CommandType type) {
- return getCommands(type, null);
- }
-
- /**
- * Retrieves a map of all commands of the specified type, with the
- * specified name.
- *
- * @param type The type of command to list, or null for all types
- * @param name The name of the command to look for, or null for any name
- * @return A map of {@link CommandInfo}s and their associated {@link Command}.
- * @since 0.6.3m1
- */
- private Map<CommandInfo, Command> getCommands(final CommandType type,
- final String name) {
- final Map<CommandInfo, Command> res = new HashMap<CommandInfo, Command>();
-
- for (Map.Entry<CommandInfo, Command> entry : commands.entrySet()) {
- if ((type == null || type.equals(entry.getKey().getType()))
- && (name == null || name.equals(entry.getKey().getName()))) {
- res.put(entry.getKey(), entry.getValue());
- }
- }
-
- return res;
- }
-
- /**
- * Retrieves a singleton instance of the CommandManager.
- *
- * @return A singleton instance of the CommandManager.
- */
- public static CommandManager getCommandManager() {
- return INSTANCE;
- }
-
- }
|