選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CommandManager.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2006-2014 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.commandparser;
  23. import com.dmdirc.Query;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.commandparser.commands.Command;
  27. import com.dmdirc.commandparser.parsers.CommandParser;
  28. import com.dmdirc.config.ConfigBinding;
  29. import com.dmdirc.interfaces.CommandController;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.ui.input.TabCompleter;
  32. import com.dmdirc.ui.input.TabCompletionType;
  33. import com.dmdirc.util.collections.MapList;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * The command manager creates and manages a single instance of all commands, and provides methods
  40. * to load each group of commands into a parser instance.
  41. */
  42. public class CommandManager implements CommandController {
  43. /** A list of commands that have been instantiated. */
  44. private final Map<CommandInfo, Command> commands = new HashMap<>();
  45. /** A list of command parsers that have been instantiated. */
  46. private final MapList<CommandType, CommandParser> parsers = new MapList<>();
  47. /** The manager to use to iterate servers. */
  48. private final ServerManager serverManager;
  49. /** The command char we're using. */
  50. @ConfigBinding(domain = "general", key = "commandchar")
  51. private char commandChar;
  52. /** The silence char we're using. */
  53. @ConfigBinding(domain = "general", key = "silencechar")
  54. private char silenceChar;
  55. /**
  56. * Creates a new instance of the Command Manager.
  57. *
  58. * @param serverManager the manager to use to iterate servers.
  59. */
  60. public CommandManager(final ServerManager serverManager) {
  61. this.serverManager = serverManager;
  62. }
  63. @Override
  64. public char getCommandChar() {
  65. return commandChar;
  66. }
  67. @Override
  68. public char getSilenceChar() {
  69. return silenceChar;
  70. }
  71. /**
  72. * Initialises the command manager.
  73. *
  74. * @param configManager The configuration manager to read settings from.
  75. */
  76. public void initialise(final AggregateConfigProvider configManager) {
  77. configManager.getBinder().bind(this, CommandManager.class);
  78. }
  79. @Override
  80. public void registerCommand(final Command command, final CommandInfo info) {
  81. registerCommand(info, command, true);
  82. }
  83. @Override
  84. public void unregisterCommand(final CommandInfo info) {
  85. registerCommand(info, commands.get(info), false);
  86. }
  87. /**
  88. * Registers or unregisters a command.
  89. *
  90. * @param info The information about the command
  91. * @param command The command to be (un)registered
  92. * @param register True if the command should be registered, false if it should be unregistered.
  93. *
  94. * @since 0.6.3m1
  95. */
  96. private void registerCommand(final CommandInfo info, final Command command,
  97. final boolean register) {
  98. if (parsers.containsKey(info.getType())) {
  99. registerCommand(info, command, parsers.get(info.getType()), register);
  100. }
  101. if (register) {
  102. commands.put(info, command);
  103. } else {
  104. commands.remove(info);
  105. }
  106. registerCommandName(info, register);
  107. }
  108. /**
  109. * Registers or unregisters the specified command with all of the specified parsers.
  110. *
  111. * @param info The command information object
  112. * @param command The command to be registered
  113. * @param myParsers The parsers to register the command with
  114. * @param register Whether to register or unregister the commands
  115. *
  116. * @since 0.6.3m1
  117. */
  118. private void registerCommand(final CommandInfo info, final Command command,
  119. final List<? extends CommandParser> myParsers, final boolean register) {
  120. for (CommandParser parser : myParsers) {
  121. if (register) {
  122. parser.registerCommand(command, info);
  123. } else {
  124. parser.unregisterCommand(info);
  125. }
  126. }
  127. }
  128. /**
  129. * Registers or unregisters the specified command's name with the relevant tab completers.
  130. *
  131. * @param command The command to be registered
  132. * @param register True if the command should be registered, false if it should be unregistered.
  133. *
  134. * @since 0.6.3m1
  135. */
  136. private void registerCommandName(final CommandInfo command,
  137. final boolean register) {
  138. // Do tab completion
  139. final String commandName = getCommandChar() + command.getName();
  140. // TODO: This logic is probably in two places. Abstract it.
  141. for (Server server : serverManager.getServers()) {
  142. if (command.getType() == CommandType.TYPE_SERVER
  143. || command.getType() == CommandType.TYPE_GLOBAL) {
  144. registerCommandName(server.getTabCompleter(), commandName, register);
  145. }
  146. if (command.getType() == CommandType.TYPE_CHANNEL
  147. || command.getType() == CommandType.TYPE_CHAT) {
  148. for (String channelName : server.getChannels()) {
  149. registerCommandName(server.getChannel(channelName).getTabCompleter(),
  150. commandName, register);
  151. }
  152. }
  153. if (command.getType() == CommandType.TYPE_QUERY
  154. || command.getType() == CommandType.TYPE_CHAT) {
  155. for (Query query : server.getQueries()) {
  156. registerCommandName(query.getTabCompleter(),
  157. commandName, register);
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * Registers or unregisters the specified command with the specified tab- completer.
  164. *
  165. * @param completer The tab completer to be used
  166. * @param name The command name to be registered
  167. * @param register True if the command should be registered, false if it should be
  168. * unregistered.
  169. */
  170. private void registerCommandName(final TabCompleter completer,
  171. final String name, final boolean register) {
  172. if (register) {
  173. completer.addEntry(TabCompletionType.COMMAND, name);
  174. } else {
  175. completer.removeEntry(TabCompletionType.COMMAND, name);
  176. }
  177. }
  178. @Override
  179. public void loadCommands(final CommandParser parser,
  180. final CommandType... supertypes) {
  181. for (CommandType supertype : supertypes) {
  182. for (CommandType type : supertype.getComponentTypes()) {
  183. for (Map.Entry<CommandInfo, Command> pair : getCommands(type, null).entrySet()) {
  184. parser.registerCommand(pair.getValue(), pair.getKey());
  185. }
  186. parsers.add(type, parser);
  187. }
  188. }
  189. }
  190. @Override
  191. public Map.Entry<CommandInfo, Command> getCommand(final String name) {
  192. return getCommand(null, name);
  193. }
  194. @Override
  195. public Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
  196. final String name) {
  197. final Map<CommandInfo, Command> res = getCommands(type, name);
  198. return res.isEmpty() ? null : res.entrySet().iterator().next();
  199. }
  200. @Override
  201. public boolean isChannelCommand(final String command) {
  202. return getCommand(CommandType.TYPE_CHANNEL, command) != null
  203. || getCommand(CommandType.TYPE_CHAT, command) != null;
  204. }
  205. @Override
  206. public List<String> getCommandNames(final CommandType type) {
  207. final List<String> res = new ArrayList<>();
  208. for (CommandInfo command : getCommands(type).keySet()) {
  209. res.add(getCommandChar() + command.getName());
  210. }
  211. return res;
  212. }
  213. @Override
  214. public Map<CommandInfo, Command> getCommands(final CommandType type) {
  215. return getCommands(type, null);
  216. }
  217. /**
  218. * Retrieves a map of all commands of the specified type, with the specified name.
  219. *
  220. * @param type The type of command to list, or null for all types
  221. * @param name The name of the command to look for, or null for any name
  222. *
  223. * @return A map of {@link CommandInfo}s and their associated {@link Command}.
  224. *
  225. * @since 0.6.3m1
  226. */
  227. private Map<CommandInfo, Command> getCommands(final CommandType type,
  228. final String name) {
  229. final Map<CommandInfo, Command> res = new HashMap<>();
  230. for (Map.Entry<CommandInfo, Command> entry : commands.entrySet()) {
  231. if ((type == null || type.equals(entry.getKey().getType()))
  232. && (name == null || name.equals(entry.getKey().getName()))) {
  233. res.put(entry.getKey(), entry.getValue());
  234. }
  235. }
  236. return res;
  237. }
  238. }