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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2006-2011 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.commands.channel.*; //NOPMD
  28. import com.dmdirc.commandparser.commands.chat.*; //NOPMD
  29. import com.dmdirc.commandparser.commands.global.*; //NOPMD
  30. import com.dmdirc.commandparser.commands.server.*; //NOPMD
  31. import com.dmdirc.commandparser.parsers.CommandParser;
  32. import com.dmdirc.config.IdentityManager;
  33. import com.dmdirc.interfaces.ConfigChangeListener;
  34. import com.dmdirc.ui.input.TabCompleter;
  35. import com.dmdirc.ui.input.TabCompletionType;
  36. import com.dmdirc.util.MapList;
  37. import java.util.ArrayList;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. /**
  42. * The command manager creates and manages a single instance of all commands,
  43. * and provides methods to load each group of commands into a parser instance.
  44. */
  45. public class CommandManager {
  46. /** A singleton instance of the command manager. */
  47. private static final CommandManager INSTANCE = new CommandManager();
  48. /** A list of commands that have been instantiated. */
  49. private final Map<CommandInfo, Command> commands
  50. = new HashMap<CommandInfo, Command>();
  51. /** A list of command parsers that have been instantiated. */
  52. private final MapList<CommandType, CommandParser> parsers
  53. = new MapList<CommandType, CommandParser>();
  54. /** The command char we're using. */
  55. private char commandChar = IdentityManager.getGlobalConfig()
  56. .getOptionChar("general", "commandchar");
  57. /** The silence char we're using. */
  58. private char silenceChar = IdentityManager.getGlobalConfig()
  59. .getOptionChar("general", "silencechar");
  60. /**
  61. * Returns the current command character.
  62. *
  63. * @return the current command char
  64. */
  65. public char getCommandChar() {
  66. return commandChar;
  67. }
  68. /**
  69. * Returns the current silence character.
  70. *
  71. * @return the current silence char
  72. */
  73. public char getSilenceChar() {
  74. return silenceChar;
  75. }
  76. /**
  77. * Registers a command with the command manager.
  78. *
  79. * @param command The command to be registered
  80. * @param info The information about the command
  81. * @since 0.6.3m1
  82. */
  83. public void registerCommand(final Command command, final CommandInfo info) {
  84. registerCommand(info, command, true);
  85. }
  86. /**
  87. * Registers a {@link Command} which also implements the {@link CommandInfo}
  88. * interface with the command manager.
  89. *
  90. * @param <T> The type of object that's being registered
  91. * @param command An object that extends {@link Command} and implements
  92. * {@link CommandInfo} to be registered.
  93. * @since 0.6.3m1
  94. */
  95. public <T extends Command & CommandInfo> void registerCommand(final T command) {
  96. registerCommand(command, command);
  97. }
  98. /**
  99. * Unregisters a command with the command manager.
  100. *
  101. * @param info The information object for the command that should be unregistered
  102. * @since 0.6.3m1
  103. */
  104. public void unregisterCommand(final CommandInfo info) {
  105. registerCommand(info, commands.get(info), false);
  106. }
  107. /**
  108. * Registers or unregisters a command.
  109. *
  110. * @param info The information about the command
  111. * @param command The command to be (un)registered
  112. * @param register True if the command should be registered, false if it
  113. * should be unregistered.
  114. * @since 0.6.3m1
  115. */
  116. private void registerCommand(final CommandInfo info, final Command command,
  117. final boolean register) {
  118. if (parsers.containsKey(info.getType())) {
  119. registerCommand(info, command, parsers.get(info.getType()), register);
  120. }
  121. if (register) {
  122. commands.put(info, command);
  123. } else {
  124. commands.remove(info);
  125. }
  126. registerCommandName(info, register);
  127. }
  128. /**
  129. * Registers or unregisters the specified command with all of the specified parsers.
  130. *
  131. * @param info The command information object
  132. * @param command The command to be registered
  133. * @param myParsers The parsers to register the command with
  134. * @param register Whether to register or unregister the commands
  135. * @since 0.6.3m1
  136. */
  137. private void registerCommand(final CommandInfo info, final Command command,
  138. final List<? extends CommandParser> myParsers, final boolean register) {
  139. for (CommandParser parser : myParsers) {
  140. if (register) {
  141. parser.registerCommand(command, info);
  142. } else {
  143. parser.unregisterCommand(info);
  144. }
  145. }
  146. }
  147. /**
  148. * Registers or unregisters the specified command's name with the relevant
  149. * tab completers.
  150. *
  151. * @param command The command to be registered
  152. * @param register True if the command should be registered, false if it
  153. * should be unregistered.
  154. * @since 0.6.3m1
  155. */
  156. private void registerCommandName(final CommandInfo command,
  157. final boolean register) {
  158. // Do tab completion
  159. final String commandName = getCommandChar() + command.getName();
  160. // TODO: This logic is probably in two places. Abstract it.
  161. for (Server server : ServerManager.getServerManager().getServers()) {
  162. if (command.getType() == CommandType.TYPE_SERVER
  163. || command.getType() == CommandType.TYPE_GLOBAL) {
  164. registerCommandName(server.getTabCompleter(), commandName, register);
  165. }
  166. if (command.getType() == CommandType.TYPE_CHANNEL
  167. || command.getType() == CommandType.TYPE_CHAT) {
  168. for (String channelName : server.getChannels()) {
  169. registerCommandName(server.getChannel(channelName).getTabCompleter(),
  170. commandName, register);
  171. }
  172. }
  173. if (command.getType() == CommandType.TYPE_QUERY
  174. || command.getType() == CommandType.TYPE_CHAT) {
  175. for (Query query : server.getQueries()) {
  176. registerCommandName(query.getTabCompleter(),
  177. commandName, register);
  178. }
  179. }
  180. }
  181. }
  182. /**
  183. * Registers or unregisters the specified command with the specified tab-
  184. * completer.
  185. *
  186. * @param completer The tab completer to be used
  187. * @param name The command name to be registered
  188. * @param register True if the command should be registered, false if it
  189. * should be unregistered.
  190. */
  191. private void registerCommandName(final TabCompleter completer,
  192. final String name, final boolean register) {
  193. if (register) {
  194. completer.addEntry(TabCompletionType.COMMAND, name);
  195. } else {
  196. completer.removeEntry(TabCompletionType.COMMAND, name);
  197. }
  198. }
  199. /**
  200. * Instansiates the default commands.
  201. */
  202. public void initCommands() {
  203. // Chat commands
  204. registerCommand(new Me(), Me.INFO);
  205. // Channel commands
  206. registerCommand(new Ban(), Ban.INFO);
  207. registerCommand(new Cycle(), Cycle.INFO);
  208. registerCommand(new Invite(), Invite.INFO);
  209. registerCommand(new KickReason(), KickReason.INFO);
  210. registerCommand(new Mode(), Mode.INFO);
  211. registerCommand(new Names(), Names.INFO);
  212. registerCommand(new Part(), Part.INFO);
  213. registerCommand(new SetNickColour(), SetNickColour.INFO);
  214. registerCommand(new ShowTopic(), ShowTopic.INFO);
  215. // Server commands
  216. registerCommand(new AllChannels(), AllChannels.INFO);
  217. registerCommand(new Away(), Away.INFO);
  218. registerCommand(new Back(), Back.INFO);
  219. registerCommand(new ChangeServer(), ChangeServer.INFO);
  220. registerCommand(new Ctcp(), Ctcp.INFO);
  221. registerCommand(new Disconnect(), Disconnect.INFO);
  222. registerCommand(new Ignore(), Ignore.INFO);
  223. registerCommand(new JoinChannelCommand(), JoinChannelCommand.INFO);
  224. registerCommand(new Message(), Message.INFO);
  225. registerCommand(new Nick(), Nick.INFO);
  226. registerCommand(new Notice(), Notice.INFO);
  227. registerCommand(new OpenQuery(), OpenQuery.INFO);
  228. registerCommand(new Raw(), Raw.INFO);
  229. registerCommand(new Reconnect(), Reconnect.INFO);
  230. registerCommand(new Umode(), Umode.INFO);
  231. registerCommand(new RawServerCommand("lusers"));
  232. registerCommand(new RawServerCommand("map"));
  233. registerCommand(new RawServerCommand("motd"));
  234. registerCommand(new RawServerCommand("oper"));
  235. registerCommand(new RawServerCommand("whois"));
  236. registerCommand(new RawServerCommand("who"));
  237. // Query commands
  238. // Global commands
  239. registerCommand(new AliasCommand(), AliasCommand.INFO);
  240. registerCommand(new AllServers(), AllServers.INFO);
  241. registerCommand(new Clear(), Clear.INFO);
  242. registerCommand(new Echo(), Echo.INFO);
  243. registerCommand(new Exit(), Exit.INFO);
  244. registerCommand(new Help(), Help.INFO);
  245. registerCommand(new Ifplugin(), Ifplugin.INFO);
  246. registerCommand(new NewServer(), NewServer.INFO);
  247. registerCommand(new Notify(), Notify.INFO);
  248. registerCommand(new LoadPlugin(), LoadPlugin.INFO);
  249. registerCommand(new UnloadPlugin(), UnloadPlugin.INFO);
  250. registerCommand(new OpenWindow(), OpenWindow.INFO);
  251. registerCommand(new ReloadActions(), ReloadActions.INFO);
  252. registerCommand(new ReloadIdentities(), ReloadIdentities.INFO);
  253. registerCommand(new ReloadPlugin(), ReloadPlugin.INFO);
  254. registerCommand(new SaveConfig(), SaveConfig.INFO);
  255. registerCommand(new Set(), Set.INFO);
  256. // Set up a listener for config changes
  257. final ConfigChangeListener listener = new ConfigChangeListener() {
  258. @Override
  259. public void configChanged(final String domain, final String key) {
  260. commandChar = IdentityManager.getGlobalConfig()
  261. .getOptionChar("general", "commandchar");
  262. silenceChar = IdentityManager.getGlobalConfig()
  263. .getOptionChar("general", "silencechar");
  264. }
  265. };
  266. IdentityManager.getGlobalConfig().addChangeListener("general", "commandchar", listener);
  267. IdentityManager.getGlobalConfig().addChangeListener("general", "silencechar", listener);
  268. }
  269. /**
  270. * Loads all commands of the specified types into the specified parser.
  271. *
  272. * @see CommandType#getComponentTypes()
  273. * @since 0.6.3m1
  274. * @param parser The {@link CommandParser} to load commands in to
  275. * @param supertypes The types of commands that should be loaded
  276. */
  277. public void loadCommands(final CommandParser parser,
  278. final CommandType ... supertypes) {
  279. for (CommandType supertype : supertypes) {
  280. for (CommandType type : supertype.getComponentTypes()) {
  281. for (Map.Entry<CommandInfo, Command> pair : getCommands(type, null).entrySet()) {
  282. parser.registerCommand(pair.getValue(), pair.getKey());
  283. }
  284. parsers.add(type, parser);
  285. }
  286. }
  287. }
  288. /**
  289. * Retrieves the command identified by the specified name, regardless of
  290. * type.
  291. *
  292. * @param name The name to look for
  293. * @return A command with a matching signature, or null if none were found
  294. */
  295. public Map.Entry<CommandInfo, Command> getCommand(final String name) {
  296. return getCommand(null, name);
  297. }
  298. /**
  299. * Retrieves a command of the specified type with the specified name.
  300. *
  301. * @param type The type of the command to look for
  302. * @param name The name to look for
  303. * @return A command with a matching signature, or null if none were found
  304. */
  305. public Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
  306. final String name) {
  307. final Map<CommandInfo, Command> res = getCommands(type, name);
  308. return res.isEmpty() ? null : res.entrySet().iterator().next();
  309. }
  310. /**
  311. * Determines if the specified command is a valid channel command.
  312. *
  313. * @param command The name of the command to test
  314. * @return True iff the command is a channel command, false otherwise
  315. */
  316. public boolean isChannelCommand(final String command) {
  317. return getCommand(CommandType.TYPE_CHANNEL, command) != null
  318. || getCommand(CommandType.TYPE_CHAT, command) != null;
  319. }
  320. /**
  321. * Retrieves a list of the names of all commands of the specified type.
  322. *
  323. * @param type The type of command to list
  324. * @return A list of command names
  325. */
  326. public List<String> getCommandNames(final CommandType type) {
  327. final List<String> res = new ArrayList<String>();
  328. for (CommandInfo command : getCommands(type).keySet()) {
  329. res.add(getCommandChar() + command.getName());
  330. }
  331. return res;
  332. }
  333. /**
  334. * Retrieves a map of all {@link CommandInfo}s and their associated
  335. * {@link Command}s of the specified type.
  336. *
  337. * @param type The type of command to list
  338. * @return A map of commands
  339. * @since 0.6.3m1
  340. */
  341. public Map<CommandInfo, Command> getCommands(final CommandType type) {
  342. return getCommands(type, null);
  343. }
  344. /**
  345. * Retrieves a map of all commands of the specified type, with the
  346. * specified name.
  347. *
  348. * @param type The type of command to list, or null for all types
  349. * @param name The name of the command to look for, or null for any name
  350. * @return A map of {@link CommandInfo}s and their associated {@link Command}.
  351. * @since 0.6.3m1
  352. */
  353. private Map<CommandInfo, Command> getCommands(final CommandType type,
  354. final String name) {
  355. final Map<CommandInfo, Command> res = new HashMap<CommandInfo, Command>();
  356. for (Map.Entry<CommandInfo, Command> entry : commands.entrySet()) {
  357. if ((type == null || type.equals(entry.getKey().getType()))
  358. && (name == null || name.equals(entry.getKey().getName()))) {
  359. res.put(entry.getKey(), entry.getValue());
  360. }
  361. }
  362. return res;
  363. }
  364. /**
  365. * Retrieves a singleton instance of the CommandManager.
  366. *
  367. * @return A singleton instance of the CommandManager.
  368. */
  369. public static CommandManager getCommandManager() {
  370. return INSTANCE;
  371. }
  372. }