Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CommandManager.java 10KB

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