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

CommandManager.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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,
  40. * and provides methods 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. /** {@inheritDoc} */
  80. @Override
  81. public void registerCommand(final Command command, final CommandInfo info) {
  82. registerCommand(info, command, true);
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public void unregisterCommand(final CommandInfo info) {
  87. registerCommand(info, commands.get(info), false);
  88. }
  89. /**
  90. * Registers or unregisters a command.
  91. *
  92. * @param info The information about the command
  93. * @param command The command to be (un)registered
  94. * @param register True if the command should be registered, false if it
  95. * should be unregistered.
  96. * @since 0.6.3m1
  97. */
  98. private void registerCommand(final CommandInfo info, final Command command,
  99. final boolean register) {
  100. if (parsers.containsKey(info.getType())) {
  101. registerCommand(info, command, parsers.get(info.getType()), register);
  102. }
  103. if (register) {
  104. commands.put(info, command);
  105. } else {
  106. commands.remove(info);
  107. }
  108. registerCommandName(info, register);
  109. }
  110. /**
  111. * Registers or unregisters the specified command with all of the specified parsers.
  112. *
  113. * @param info The command information object
  114. * @param command The command to be registered
  115. * @param myParsers The parsers to register the command with
  116. * @param register Whether to register or unregister the commands
  117. * @since 0.6.3m1
  118. */
  119. private void registerCommand(final CommandInfo info, final Command command,
  120. final List<? extends CommandParser> myParsers, final boolean register) {
  121. for (CommandParser parser : myParsers) {
  122. if (register) {
  123. parser.registerCommand(command, info);
  124. } else {
  125. parser.unregisterCommand(info);
  126. }
  127. }
  128. }
  129. /**
  130. * Registers or unregisters the specified command's name with the relevant
  131. * tab completers.
  132. *
  133. * @param command The command to be registered
  134. * @param register True if the command should be registered, false if it
  135. * should be unregistered.
  136. * @since 0.6.3m1
  137. */
  138. private void registerCommandName(final CommandInfo command,
  139. final boolean register) {
  140. // Do tab completion
  141. final String commandName = getCommandChar() + command.getName();
  142. // TODO: This logic is probably in two places. Abstract it.
  143. for (Server server : serverManager.getServers()) {
  144. if (command.getType() == CommandType.TYPE_SERVER
  145. || command.getType() == CommandType.TYPE_GLOBAL) {
  146. registerCommandName(server.getTabCompleter(), commandName, register);
  147. }
  148. if (command.getType() == CommandType.TYPE_CHANNEL
  149. || command.getType() == CommandType.TYPE_CHAT) {
  150. for (String channelName : server.getChannels()) {
  151. registerCommandName(server.getChannel(channelName).getTabCompleter(),
  152. commandName, register);
  153. }
  154. }
  155. if (command.getType() == CommandType.TYPE_QUERY
  156. || command.getType() == CommandType.TYPE_CHAT) {
  157. for (Query query : server.getQueries()) {
  158. registerCommandName(query.getTabCompleter(),
  159. commandName, register);
  160. }
  161. }
  162. }
  163. }
  164. /**
  165. * Registers or unregisters the specified command with the specified tab-
  166. * completer.
  167. *
  168. * @param completer The tab completer to be used
  169. * @param name The command name to be registered
  170. * @param register True if the command should be registered, false if it
  171. * should be unregistered.
  172. */
  173. private void registerCommandName(final TabCompleter completer,
  174. final String name, final boolean register) {
  175. if (register) {
  176. completer.addEntry(TabCompletionType.COMMAND, name);
  177. } else {
  178. completer.removeEntry(TabCompletionType.COMMAND, name);
  179. }
  180. }
  181. /** {@inheritDoc} */
  182. @Override
  183. public void loadCommands(final CommandParser parser,
  184. final CommandType ... supertypes) {
  185. for (CommandType supertype : supertypes) {
  186. for (CommandType type : supertype.getComponentTypes()) {
  187. for (Map.Entry<CommandInfo, Command> pair : getCommands(type, null).entrySet()) {
  188. parser.registerCommand(pair.getValue(), pair.getKey());
  189. }
  190. parsers.add(type, parser);
  191. }
  192. }
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public Map.Entry<CommandInfo, Command> getCommand(final String name) {
  197. return getCommand(null, name);
  198. }
  199. /** {@inheritDoc} */
  200. @Override
  201. public Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
  202. final String name) {
  203. final Map<CommandInfo, Command> res = getCommands(type, name);
  204. return res.isEmpty() ? null : res.entrySet().iterator().next();
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public boolean isChannelCommand(final String command) {
  209. return getCommand(CommandType.TYPE_CHANNEL, command) != null
  210. || getCommand(CommandType.TYPE_CHAT, command) != null;
  211. }
  212. /** {@inheritDoc} */
  213. @Override
  214. public List<String> getCommandNames(final CommandType type) {
  215. final List<String> res = new ArrayList<>();
  216. for (CommandInfo command : getCommands(type).keySet()) {
  217. res.add(getCommandChar() + command.getName());
  218. }
  219. return res;
  220. }
  221. /** {@inheritDoc} */
  222. @Override
  223. public Map<CommandInfo, Command> getCommands(final CommandType type) {
  224. return getCommands(type, null);
  225. }
  226. /**
  227. * Retrieves a map of all commands of the specified type, with the
  228. * specified name.
  229. *
  230. * @param type The type of command to list, or null for all types
  231. * @param name The name of the command to look for, or null for any name
  232. * @return A map of {@link CommandInfo}s and their associated {@link Command}.
  233. * @since 0.6.3m1
  234. */
  235. private Map<CommandInfo, Command> getCommands(final CommandType type,
  236. final String name) {
  237. final Map<CommandInfo, Command> res = new HashMap<>();
  238. for (Map.Entry<CommandInfo, Command> entry : commands.entrySet()) {
  239. if ((type == null || type.equals(entry.getKey().getType()))
  240. && (name == null || name.equals(entry.getKey().getName()))) {
  241. res.put(entry.getKey(), entry.getValue());
  242. }
  243. }
  244. return res;
  245. }
  246. }