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

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