Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CommandManager.java 15KB

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