You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandManager.java 11KB

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