Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CommandManager.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser;
  18. import com.dmdirc.GlobalWindow;
  19. import com.dmdirc.commandparser.commands.Command;
  20. import com.dmdirc.commandparser.parsers.CommandParser;
  21. import com.dmdirc.config.binding.ConfigBinding;
  22. import com.dmdirc.interfaces.CommandController;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.interfaces.ConnectionManager;
  25. import com.dmdirc.interfaces.GroupChat;
  26. import com.dmdirc.interfaces.PrivateChat;
  27. import com.dmdirc.config.provider.AggregateConfigProvider;
  28. import com.dmdirc.ui.input.TabCompleter;
  29. import com.dmdirc.ui.input.TabCompletionType;
  30. import com.google.common.collect.ArrayListMultimap;
  31. import com.google.common.collect.Multimap;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.stream.Collectors;
  36. import javax.inject.Provider;
  37. /**
  38. * The command manager creates and manages a single instance of all commands, and provides methods
  39. * to load each group of commands into a parser instance.
  40. */
  41. public class CommandManager implements CommandController {
  42. /** A list of commands that have been instantiated. */
  43. private final Map<CommandInfo, Command> commands = new HashMap<>();
  44. /** A list of command parsers that have been instantiated. */
  45. private final Multimap<CommandType, CommandParser> parsers = ArrayListMultimap.create();
  46. /** The manager to use to iterate servers. */
  47. private final ConnectionManager connectionManager;
  48. /** Provider to use to retrieve the global window. */
  49. private final Provider<GlobalWindow> globalWindowProvider;
  50. /** The command char we're using. */
  51. @ConfigBinding(domain = "general", key = "commandchar")
  52. private char commandChar;
  53. /** The silence char we're using. */
  54. @ConfigBinding(domain = "general", key = "silencechar")
  55. private char silenceChar;
  56. /**
  57. * Creates a new instance of the Command Manager.
  58. *
  59. * @param connectionManager the manager to use to iterate servers.
  60. * @param globalWindowProvider provider to use to retrieve the global window.
  61. */
  62. public CommandManager(final ConnectionManager connectionManager,
  63. final Provider<GlobalWindow> globalWindowProvider) {
  64. this.connectionManager = connectionManager;
  65. this.globalWindowProvider = globalWindowProvider;
  66. }
  67. @Override
  68. public char getCommandChar() {
  69. return commandChar;
  70. }
  71. @Override
  72. public char getSilenceChar() {
  73. return silenceChar;
  74. }
  75. /**
  76. * Initialises the command manager.
  77. *
  78. * @param configManager The configuration manager to read settings from.
  79. */
  80. public void initialise(final AggregateConfigProvider configManager) {
  81. configManager.getBinder().bind(this, CommandManager.class);
  82. }
  83. @Override
  84. public void registerCommand(final Command command, final CommandInfo info) {
  85. registerCommand(info, command, true);
  86. }
  87. @Override
  88. public void unregisterCommand(final CommandInfo info) {
  89. registerCommand(info, commands.get(info), false);
  90. }
  91. /**
  92. * Registers or unregisters a command.
  93. *
  94. * @param info The information about the command
  95. * @param command The command to be (un)registered
  96. * @param register True if the command should be registered, false if it should be unregistered.
  97. *
  98. * @since 0.6.3m1
  99. */
  100. private void registerCommand(final CommandInfo info, final Command command,
  101. final boolean register) {
  102. if (parsers.containsKey(info.getType())) {
  103. registerCommand(info, command, parsers.get(info.getType()), register);
  104. }
  105. if (register) {
  106. commands.put(info, command);
  107. } else {
  108. commands.remove(info);
  109. }
  110. registerCommandName(info, register);
  111. }
  112. /**
  113. * Registers or unregisters the specified command with all of the specified parsers.
  114. *
  115. * @param info The command information object
  116. * @param command The command to be registered
  117. * @param myParsers The parsers to register the command with
  118. * @param register Whether to register or unregister the commands
  119. *
  120. * @since 0.6.3m1
  121. */
  122. private void registerCommand(final CommandInfo info, final Command command,
  123. final Iterable<? extends CommandParser> myParsers, final boolean register) {
  124. for (CommandParser parser : myParsers) {
  125. if (register) {
  126. parser.registerCommand(command, info);
  127. } else {
  128. parser.unregisterCommand(info);
  129. }
  130. }
  131. }
  132. /**
  133. * Registers or unregisters the specified command's name with the relevant tab completers.
  134. *
  135. * @param command The command to be registered
  136. * @param register True if the command should be registered, false if it should be unregistered.
  137. *
  138. * @since 0.6.3m1
  139. */
  140. private void registerCommandName(final CommandInfo command,
  141. final boolean register) {
  142. // Do tab completion
  143. final String plainCommandName = getCommandChar() + command.getName();
  144. final String silencedCommandName = getCommandChar() + getSilenceChar() + command.getName();
  145. if (command.getType() == CommandType.TYPE_GLOBAL) {
  146. registerCommandName(globalWindowProvider.get().getInputModel().get().getTabCompleter(),
  147. plainCommandName, register);
  148. registerCommandName(globalWindowProvider.get().getInputModel().get().getTabCompleter(),
  149. silencedCommandName, register);
  150. }
  151. // TODO: This logic is probably in two places. Abstract it.
  152. for (Connection server : connectionManager.getConnections()) {
  153. if (command.getType() == CommandType.TYPE_SERVER
  154. || command.getType() == CommandType.TYPE_GLOBAL) {
  155. registerCommandName(server.getWindowModel().getInputModel().get().getTabCompleter(),
  156. plainCommandName, register);
  157. registerCommandName(server.getWindowModel().getInputModel().get().getTabCompleter(),
  158. silencedCommandName, register);
  159. }
  160. if (command.getType() == CommandType.TYPE_CHANNEL
  161. || command.getType() == CommandType.TYPE_CHAT) {
  162. for (GroupChat channel : server.getGroupChatManager().getChannels()) {
  163. registerCommandName(
  164. channel.getWindowModel().getInputModel().get().getTabCompleter(),
  165. plainCommandName, register);
  166. registerCommandName(
  167. channel.getWindowModel().getInputModel().get().getTabCompleter(),
  168. silencedCommandName, register);
  169. }
  170. }
  171. if (command.getType() == CommandType.TYPE_QUERY
  172. || command.getType() == CommandType.TYPE_CHAT) {
  173. for (PrivateChat query : server.getQueries()) {
  174. registerCommandName(
  175. query.getWindowModel().getInputModel().get().getTabCompleter(),
  176. plainCommandName, register);
  177. registerCommandName(
  178. query.getWindowModel().getInputModel().get().getTabCompleter(),
  179. silencedCommandName, register);
  180. }
  181. }
  182. }
  183. }
  184. /**
  185. * Registers or unregisters the specified command with the specified tab- completer.
  186. *
  187. * @param completer The tab completer to be used
  188. * @param name The command name to be registered
  189. * @param register True if the command should be registered, false if it should be
  190. * unregistered.
  191. */
  192. private void registerCommandName(final TabCompleter completer,
  193. final String name, final boolean register) {
  194. if (register) {
  195. completer.addEntry(TabCompletionType.COMMAND, name);
  196. } else {
  197. completer.removeEntry(TabCompletionType.COMMAND, name);
  198. }
  199. }
  200. @Override
  201. public void loadCommands(final CommandParser parser,
  202. final CommandType... supertypes) {
  203. for (CommandType supertype : supertypes) {
  204. for (CommandType type : supertype.getComponentTypes()) {
  205. for (Map.Entry<CommandInfo, Command> pair : getCommands(type, null).entrySet()) {
  206. parser.registerCommand(pair.getValue(), pair.getKey());
  207. }
  208. parsers.put(type, parser);
  209. }
  210. }
  211. }
  212. @Override
  213. public Map.Entry<CommandInfo, Command> getCommand(final String name) {
  214. return getCommand(null, name);
  215. }
  216. @Override
  217. public Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
  218. final String name) {
  219. final Map<CommandInfo, Command> res = getCommands(type, name);
  220. return res.isEmpty() ? null : res.entrySet().iterator().next();
  221. }
  222. @Override
  223. public boolean isChannelCommand(final String command) {
  224. return getCommand(CommandType.TYPE_CHANNEL, command) != null
  225. || getCommand(CommandType.TYPE_CHAT, command) != null;
  226. }
  227. @Override
  228. public List<String> getCommandNames(final CommandType type) {
  229. return getCommands(type).keySet().stream()
  230. .map(command -> getCommandChar() + command.getName())
  231. .collect(Collectors.toList());
  232. }
  233. @Override
  234. public Map<CommandInfo, Command> getCommands(final CommandType type) {
  235. return getCommands(type, null);
  236. }
  237. /**
  238. * Retrieves a map of all commands of the specified type, with the specified name.
  239. *
  240. * @param type The type of command to list, or null for all types
  241. * @param name The name of the command to look for, or null for any name
  242. *
  243. * @return A map of {@link CommandInfo}s and their associated {@link Command}.
  244. *
  245. * @since 0.6.3m1
  246. */
  247. private Map<CommandInfo, Command> getCommands(final CommandType type,
  248. final String name) {
  249. final Map<CommandInfo, Command> res = new HashMap<>();
  250. commands.entrySet().stream()
  251. .filter(entry -> (type == null || type == entry.getKey().getType()) &&
  252. (name == null || name.equals(entry.getKey().getName())))
  253. .forEach(entry -> res.put(entry.getKey(), entry.getValue()));
  254. return res;
  255. }
  256. }