123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * 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 uk.org.ownage.dmdirc.commandparser;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import uk.org.ownage.dmdirc.Config;
- import uk.org.ownage.dmdirc.Server;
- import uk.org.ownage.dmdirc.ServerManager;
- import uk.org.ownage.dmdirc.commandparser.commands.channel.*;
- import uk.org.ownage.dmdirc.commandparser.commands.global.*;
- import uk.org.ownage.dmdirc.commandparser.commands.query.*;
- import uk.org.ownage.dmdirc.commandparser.commands.server.*;
- import uk.org.ownage.dmdirc.logger.ErrorLevel;
- import uk.org.ownage.dmdirc.logger.Logger;
-
- /**
- * 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.
- * @author chris
- */
- public final class CommandManager {
-
- /**
- * The global commands that have been instansiated.
- */
- private static List<Command> globalCommands;
- /**
- * The server commands that have been instansiated.
- */
- private static List<Command> serverCommands;
- /**
- * The channel commands that have been instansiated.
- */
- private static List<Command> channelCommands;
- /**
- * The query commands that have been instansiated.
- */
- private static List<Command> queryCommands;
- /**
- * The parsers that have requested global commands.
- */
- private static List<CommandParser> globalParsers;
- /**
- * The parsers that have requested server commands.
- */
- private static List<CommandParser> serverParsers;
- /**
- * The parsers that have requested channel commands.
- */
- private static List<CommandParser> channelParsers;
- /**
- * The parsers that have requested query commands.
- */
- private static List<CommandParser> queryParsers;
-
- /**
- * Channel commands that have been registered to appear in the nicklist
- * popup.
- */
- private static List<Command> channelPopupCommands;
-
- /**
- * Prevents creation of a new command manager.
- */
- private CommandManager() {
- //do nothing
- }
-
- /**
- * Registers a command with the command manager.
- * @param command The command to be registered
- */
- public static void registerCommand(final Command command) {
- if (channelCommands == null) {
- initLists();
- }
-
- List<CommandParser> target = null;
-
- if (command instanceof ChannelCommand) {
- target = channelParsers;
- channelCommands.add(command);
- } else if (command instanceof ServerCommand) {
- target = serverParsers;
- serverCommands.add(command);
- } else if (command instanceof QueryCommand) {
- target = queryParsers;
- queryCommands.add(command);
- } else if (command instanceof GlobalCommand) {
- target = globalParsers;
- globalCommands.add(command);
- } else {
- Logger.error(ErrorLevel.ERROR, "Attempted to register an invalid command: "
- + command.getClass().getName());
- }
-
- // FIXME: There's no way to kill old/dead entries in the *Parsers lists.
- // Ideally, they'd unregister themselves (or so) when unloaded.
- if (target != null) {
- for (CommandParser parser : target) {
- if (parser != null) {
- parser.registerCommand(command);
- }
- }
-
- final String commandName = Config.getCommandChar() + command.getName();
-
- for (Server server : ServerManager.getServerManager().getServers()) {
- if (command instanceof ServerCommand || command instanceof GlobalCommand) {
- server.getTabCompleter().addEntry(commandName);
- } else if (command instanceof ChannelCommand) {
- for (String channelName : server.getChannels()) {
- server.getChannel(channelName).getTabCompleter().addEntry(commandName);
- }
- } else if (command instanceof QueryCommand) {
- for (String queryName : server.getQueries()) {
- server.getQuery(queryName).getTabCompleter().addEntry(commandName);
- }
- }
- }
- }
- }
-
- /**
- * Unregisters a command with the command manager.
- * @param command The command to be unregistered
- */
- public static void unregisterCommand(final Command command) {
- if (channelCommands == null) {
- return;
- }
-
- List<CommandParser> target = null;
-
- if (command instanceof ChannelCommand) {
- target = channelParsers;
- channelCommands.remove(command);
- } else if (command instanceof ServerCommand) {
- target = serverParsers;
- serverCommands.remove(command);
- } else if (command instanceof QueryCommand) {
- target = queryParsers;
- queryCommands.remove(command);
- } else if (command instanceof GlobalCommand) {
- target = globalParsers;
- globalCommands.remove(command);
- } else {
- Logger.error(ErrorLevel.ERROR, "Attempted to unregister an invalid command: "
- + command.getClass().getName());
- }
-
- // FIXME: There's no way to kill old/dead entries in the *Parsers lists.
- // Ideally, they'd unregister themselves (or so) when unloaded.
- if (target != null) {
- for (CommandParser parser : target) {
- if (parser != null) {
- parser.unregisterCommand(command);
- }
- }
-
- final String commandName = Config.getCommandChar() + command.getName();
-
- for (Server server : ServerManager.getServerManager().getServers()) {
- if (command instanceof ServerCommand || command instanceof GlobalCommand) {
- server.getTabCompleter().removeEntry(commandName);
- } else if (command instanceof ChannelCommand) {
- for (String channelName : server.getChannels()) {
- server.getChannel(channelName).getTabCompleter().removeEntry(commandName);
- }
- } else if (command instanceof QueryCommand) {
- for (String queryName : server.getQueries()) {
- server.getQuery(queryName).getTabCompleter().removeEntry(commandName);
- }
- }
- }
- }
- }
-
- /**
- * Registers a command for use in the nicklist popup.
- * @param command The command to be registered
- */
- public static void registerPopupCommand(final Command command) {
- if (channelPopupCommands == null) {
- initLists();
- }
-
- channelPopupCommands.add(command);
- }
-
- /**
- * Retrieves the commands for use in the nicklist popup.
- * @return A list of commands suitable for use in the nicklist popup
- */
- public static List<Command> getNicklistCommands() {
- if (channelPopupCommands == null) {
- initLists();
- }
-
- return channelPopupCommands;
- }
-
- /**
- * Initialises the command manager's various command lists.
- */
- private static void initLists() {
- channelCommands = new ArrayList<Command>();
- serverCommands = new ArrayList<Command>();
- queryCommands = new ArrayList<Command>();
- globalCommands = new ArrayList<Command>();
-
- channelParsers = new ArrayList<CommandParser>();
- serverParsers = new ArrayList<CommandParser>();
- queryParsers = new ArrayList<CommandParser>();
- globalParsers = new ArrayList<CommandParser>();
-
- channelPopupCommands = new ArrayList<Command>();
-
- initCommands();
- }
-
- /**
- * Instansiates the default commands.
- */
- private static void initCommands() {
- // Channel commands
- new Ban();
- new Benchmark();
- new ChannelSettings();
- new Cycle();
- new Kick();
- new KickEmpty();
- new KickReason();
- new Me();
- new MeEmpty();
- new Mode();
- new Notify();
- new Part();
- new PartDefault();
- new SetNickColour();
- new SetTopic();
- new ShowTopic();
-
- // Server commands
- new AllChannels();
- new Away();
- new Back();
- new ChangeServer();
- new Clear();
- new ConfigInfo();
- new Ctcp();
- new Disconnect();
- new Echo();
- new Help();
- new Ignore();
- new Join();
- new Message();
- new Motd();
- new Nick();
- new Notice();
- new Query();
- new Raw();
- new Reconnect();
- new Whois();
-
- // Query commands
- new QueryMe();
- new QueryMeEmpty();
-
- // Global commands
- new Exit();
- new ExitDefault();
- new NewServer();
- new AllServers();
- new LoadFormatter();
- new LoadPlugin();
- new ReloadActions();
- new ReloadFormatter();
- new ReloadPlugin();
- new SaveFormatter();
- new Set();
- }
-
- /**
- * Loads all channel commands into the specified parser.
- * @param parser The parser to load commands into
- */
- public static void loadChannelCommands(final CommandParser parser) {
- if (channelCommands == null) {
- initLists();
- }
-
- for (Command com : channelCommands) {
- parser.registerCommand(com);
- }
-
- channelParsers.add(parser);
- }
-
- /**
- * Loads all server commands into the specified parser.
- * @param parser The parser to load commands into
- */
- public static void loadServerCommands(final CommandParser parser) {
- if (serverCommands == null) {
- initLists();
- }
-
- for (Command com : serverCommands) {
- parser.registerCommand(com);
- }
-
- serverParsers.add(parser);
- }
-
- /**
- * Loads all global commands into the specified parser.
- * @param parser The parser to load commands into
- */
- public static void loadGlobalCommands(final CommandParser parser) {
- if (globalCommands == null) {
- initLists();
- }
-
- for (Command com : globalCommands) {
- parser.registerCommand(com);
- }
-
- globalParsers.add(parser);
- }
-
- /**
- * Loads all query commands into the specified parser.
- * @param parser The parser to load commands into
- */
- protected static void loadQueryCommands(final QueryCommandParser parser) {
- if (queryCommands == null) {
- initLists();
- }
-
- for (Command com : queryCommands) {
- parser.registerCommand(com);
- }
-
- queryParsers.add(parser);
- }
-
- /**
- * Retrieves the server command identified by the specified signature.
- * @param signature The signature to look for
- * @return A server command with a matching signature, or null if none
- * were found.
- */
- public static ServerCommand getServerCommand(final String signature) {
- if (serverCommands == null) {
- initLists();
- }
-
- for (Command com : serverCommands) {
- if (com.getSignature().equalsIgnoreCase(signature)) {
- return (ServerCommand) com;
- }
- }
-
- return null;
- }
-
- /**
- * Retrieves the global command identified by the specified signature.
- * @param signature The signature to look for
- * @return A global command with a matching signature, or null if none
- * were found.
- */
- public static GlobalCommand getGlobalCommand(final String signature) {
- if (globalCommands == null) {
- initLists();
- }
-
- for (Command com : globalCommands) {
- if (com.getSignature().equalsIgnoreCase(signature)) {
- return (GlobalCommand) com;
- }
- }
-
- return null;
- }
-
- /**
- * Retrieves the channel command identified by the specified signature.
- * @param signature The signature to look for
- * @return A channel command with a matching signature, or null if none
- * were found.
- */
- public static ChannelCommand getChannelCommand(final String signature) {
- if (channelCommands == null) {
- initLists();
- }
-
- for (Command com : channelCommands) {
- if (com.getSignature().equalsIgnoreCase(signature)) {
- return (ChannelCommand) com;
- }
- }
-
- return null;
- }
-
- /**
- * Returns a list containing the server commands that have been initialised
- * by this command manager.
- * @return An ArrayList of server commands, or null if none have been loaded
- */
- public static List<Command> getServerCommands() {
- return serverCommands;
- }
-
- /**
- * Returns a list containing the channel commands that have been initialised
- * by this command manager.
- * @return An ArrayList of channel commands, or null if none have been loaded
- */
- public static List<Command> getChannelCommands() {
- return channelCommands;
- }
-
- /**
- * Returns a list containing the query commands that have been initialised
- * by this command manager.
- * @return An ArrayList of query commands, or null if none have been loaded
- */
- public static List<Command> getQueryCommands() {
- return queryCommands;
- }
-
- /**
- * Determines if the specified command is a valid channel command.
- * @param command The name of the command to test
- * @return True iff the command is a channel command, false otherwise
- */
- public static boolean isChannelCommand(final String command) {
- if (channelCommands == null) {
- CommandManager.initLists();
- }
-
- for (Command chanCommand : channelCommands) {
- if (chanCommand.getName().equalsIgnoreCase(command)) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Returns the names (including command char) of all registered server
- * commands.
- * @return An ArrayList<String> containing all registered server command
- * names
- */
- public static List<String> getServerCommandNames() {
- if (serverCommands == null) {
- CommandManager.initLists();
- }
-
- return getCommandNames(serverCommands);
- }
-
- /**
- * Returns the names (including command char) of all registered global
- * commands.
- * @return An ArrayList<String> containing all registered global command
- * names
- */
- public static List<String> getGlobalCommandNames() {
- if (globalCommands == null) {
- CommandManager.initLists();
- }
-
- return getCommandNames(globalCommands);
- }
-
- /**
- * Returns the names (including command char) of all registered channel
- * commands.
- * @return An ArrayList<String> containing all registered server command
- * names
- */
- public static List<String> getChannelCommandNames() {
- if (channelCommands == null) {
- CommandManager.initLists();
- }
-
- return getCommandNames(channelCommands);
- }
-
- /**
- * Returns the names (including command char) of all registered channel
- * commands.
- * @return An ArrayList<String> containing all registered server command
- * names
- */
- public static List<String> getQueryCommandNames() {
- if (queryCommands == null) {
- CommandManager.initLists();
- }
-
- return getCommandNames(queryCommands);
- }
-
- /**
- * Iterates through the specified source and returns a list of the names
- * of all commands found in it.
- * @param source The source vector to iterate over
- * @return A list of all commands in the source
- */
- private static List<String> getCommandNames(final List<Command> source) {
- if (source == null) {
- return null;
- }
-
- final List<String> res = new ArrayList<String>();
-
- for (Command command : source) {
- res.add(Config.getCommandChar() + command.getName());
- }
-
- return res;
- }
-
- }
|