選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CommandManager.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (c) 2006-2007 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 uk.org.ownage.dmdirc.commandparser;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import uk.org.ownage.dmdirc.Config;
  26. import uk.org.ownage.dmdirc.Server;
  27. import uk.org.ownage.dmdirc.ServerManager;
  28. import uk.org.ownage.dmdirc.commandparser.commands.channel.*;
  29. import uk.org.ownage.dmdirc.commandparser.commands.global.*;
  30. import uk.org.ownage.dmdirc.commandparser.commands.query.*;
  31. import uk.org.ownage.dmdirc.commandparser.commands.server.*;
  32. import uk.org.ownage.dmdirc.logger.ErrorLevel;
  33. import uk.org.ownage.dmdirc.logger.Logger;
  34. /**
  35. * The command manager creates and manages a single instance of all commands,
  36. * and provides methods to load each group of commands into a parser instance.
  37. * @author chris
  38. */
  39. public final class CommandManager {
  40. /**
  41. * The global commands that have been instansiated.
  42. */
  43. private static List<Command> globalCommands;
  44. /**
  45. * The server commands that have been instansiated.
  46. */
  47. private static List<Command> serverCommands;
  48. /**
  49. * The channel commands that have been instansiated.
  50. */
  51. private static List<Command> channelCommands;
  52. /**
  53. * The query commands that have been instansiated.
  54. */
  55. private static List<Command> queryCommands;
  56. /**
  57. * The parsers that have requested global commands.
  58. */
  59. private static List<CommandParser> globalParsers;
  60. /**
  61. * The parsers that have requested server commands.
  62. */
  63. private static List<CommandParser> serverParsers;
  64. /**
  65. * The parsers that have requested channel commands.
  66. */
  67. private static List<CommandParser> channelParsers;
  68. /**
  69. * The parsers that have requested query commands.
  70. */
  71. private static List<CommandParser> queryParsers;
  72. /**
  73. * Channel commands that have been registered to appear in the nicklist
  74. * popup.
  75. */
  76. private static List<Command> channelPopupCommands;
  77. /**
  78. * Prevents creation of a new command manager.
  79. */
  80. private CommandManager() {
  81. //do nothing
  82. }
  83. /**
  84. * Registers a command with the command manager.
  85. * @param command The command to be registered
  86. */
  87. public static void registerCommand(final Command command) {
  88. if (channelCommands == null) {
  89. initLists();
  90. }
  91. List<CommandParser> target = null;
  92. if (command instanceof ChannelCommand) {
  93. target = channelParsers;
  94. channelCommands.add(command);
  95. } else if (command instanceof ServerCommand) {
  96. target = serverParsers;
  97. serverCommands.add(command);
  98. } else if (command instanceof QueryCommand) {
  99. target = queryParsers;
  100. queryCommands.add(command);
  101. } else if (command instanceof GlobalCommand) {
  102. target = globalParsers;
  103. globalCommands.add(command);
  104. } else {
  105. Logger.error(ErrorLevel.ERROR, "Attempted to register an invalid command: "
  106. + command.getClass().getName());
  107. }
  108. // FIXME: There's no way to kill old/dead entries in the *Parsers lists.
  109. // Ideally, they'd unregister themselves (or so) when unloaded.
  110. if (target != null) {
  111. for (CommandParser parser : target) {
  112. if (parser != null) {
  113. parser.registerCommand(command);
  114. }
  115. }
  116. final String commandName = Config.getCommandChar() + command.getName();
  117. for (Server server : ServerManager.getServerManager().getServers()) {
  118. if (command instanceof ServerCommand || command instanceof GlobalCommand) {
  119. server.getTabCompleter().addEntry(commandName);
  120. } else if (command instanceof ChannelCommand) {
  121. for (String channelName : server.getChannels()) {
  122. server.getChannel(channelName).getTabCompleter().addEntry(commandName);
  123. }
  124. } else if (command instanceof QueryCommand) {
  125. for (String queryName : server.getQueries()) {
  126. server.getQuery(queryName).getTabCompleter().addEntry(commandName);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Unregisters a command with the command manager.
  134. * @param command The command to be unregistered
  135. */
  136. public static void unregisterCommand(final Command command) {
  137. if (channelCommands == null) {
  138. return;
  139. }
  140. List<CommandParser> target = null;
  141. if (command instanceof ChannelCommand) {
  142. target = channelParsers;
  143. channelCommands.remove(command);
  144. } else if (command instanceof ServerCommand) {
  145. target = serverParsers;
  146. serverCommands.remove(command);
  147. } else if (command instanceof QueryCommand) {
  148. target = queryParsers;
  149. queryCommands.remove(command);
  150. } else if (command instanceof GlobalCommand) {
  151. target = globalParsers;
  152. globalCommands.remove(command);
  153. } else {
  154. Logger.error(ErrorLevel.ERROR, "Attempted to unregister an invalid command: "
  155. + command.getClass().getName());
  156. }
  157. // FIXME: There's no way to kill old/dead entries in the *Parsers lists.
  158. // Ideally, they'd unregister themselves (or so) when unloaded.
  159. if (target != null) {
  160. for (CommandParser parser : target) {
  161. if (parser != null) {
  162. parser.unregisterCommand(command);
  163. }
  164. }
  165. final String commandName = Config.getCommandChar() + command.getName();
  166. for (Server server : ServerManager.getServerManager().getServers()) {
  167. if (command instanceof ServerCommand || command instanceof GlobalCommand) {
  168. server.getTabCompleter().removeEntry(commandName);
  169. } else if (command instanceof ChannelCommand) {
  170. for (String channelName : server.getChannels()) {
  171. server.getChannel(channelName).getTabCompleter().removeEntry(commandName);
  172. }
  173. } else if (command instanceof QueryCommand) {
  174. for (String queryName : server.getQueries()) {
  175. server.getQuery(queryName).getTabCompleter().removeEntry(commandName);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Registers a command for use in the nicklist popup.
  183. * @param command The command to be registered
  184. */
  185. public static void registerPopupCommand(final Command command) {
  186. if (channelPopupCommands == null) {
  187. initLists();
  188. }
  189. channelPopupCommands.add(command);
  190. }
  191. /**
  192. * Retrieves the commands for use in the nicklist popup.
  193. * @return A list of commands suitable for use in the nicklist popup
  194. */
  195. public static List<Command> getNicklistCommands() {
  196. if (channelPopupCommands == null) {
  197. initLists();
  198. }
  199. return channelPopupCommands;
  200. }
  201. /**
  202. * Initialises the command manager's various command lists.
  203. */
  204. private static void initLists() {
  205. channelCommands = new ArrayList<Command>();
  206. serverCommands = new ArrayList<Command>();
  207. queryCommands = new ArrayList<Command>();
  208. globalCommands = new ArrayList<Command>();
  209. channelParsers = new ArrayList<CommandParser>();
  210. serverParsers = new ArrayList<CommandParser>();
  211. queryParsers = new ArrayList<CommandParser>();
  212. globalParsers = new ArrayList<CommandParser>();
  213. channelPopupCommands = new ArrayList<Command>();
  214. initCommands();
  215. }
  216. /**
  217. * Instansiates the default commands.
  218. */
  219. private static void initCommands() {
  220. // Channel commands
  221. new Ban();
  222. new Benchmark();
  223. new ChannelSettings();
  224. new Cycle();
  225. new Kick();
  226. new KickEmpty();
  227. new KickReason();
  228. new Me();
  229. new MeEmpty();
  230. new Mode();
  231. new Notify();
  232. new Part();
  233. new PartDefault();
  234. new SetNickColour();
  235. new SetTopic();
  236. new ShowTopic();
  237. // Server commands
  238. new AllChannels();
  239. new Away();
  240. new Back();
  241. new ChangeServer();
  242. new Clear();
  243. new ConfigInfo();
  244. new Ctcp();
  245. new Disconnect();
  246. new Echo();
  247. new Help();
  248. new Ignore();
  249. new Join();
  250. new Message();
  251. new Motd();
  252. new Nick();
  253. new Notice();
  254. new Query();
  255. new Raw();
  256. new Reconnect();
  257. new Whois();
  258. // Query commands
  259. new QueryMe();
  260. new QueryMeEmpty();
  261. // Global commands
  262. new Exit();
  263. new ExitDefault();
  264. new NewServer();
  265. new AllServers();
  266. new LoadFormatter();
  267. new LoadPlugin();
  268. new ReloadActions();
  269. new ReloadFormatter();
  270. new ReloadPlugin();
  271. new SaveFormatter();
  272. new Set();
  273. }
  274. /**
  275. * Loads all channel commands into the specified parser.
  276. * @param parser The parser to load commands into
  277. */
  278. public static void loadChannelCommands(final CommandParser parser) {
  279. if (channelCommands == null) {
  280. initLists();
  281. }
  282. for (Command com : channelCommands) {
  283. parser.registerCommand(com);
  284. }
  285. channelParsers.add(parser);
  286. }
  287. /**
  288. * Loads all server commands into the specified parser.
  289. * @param parser The parser to load commands into
  290. */
  291. public static void loadServerCommands(final CommandParser parser) {
  292. if (serverCommands == null) {
  293. initLists();
  294. }
  295. for (Command com : serverCommands) {
  296. parser.registerCommand(com);
  297. }
  298. serverParsers.add(parser);
  299. }
  300. /**
  301. * Loads all global commands into the specified parser.
  302. * @param parser The parser to load commands into
  303. */
  304. public static void loadGlobalCommands(final CommandParser parser) {
  305. if (globalCommands == null) {
  306. initLists();
  307. }
  308. for (Command com : globalCommands) {
  309. parser.registerCommand(com);
  310. }
  311. globalParsers.add(parser);
  312. }
  313. /**
  314. * Loads all query commands into the specified parser.
  315. * @param parser The parser to load commands into
  316. */
  317. protected static void loadQueryCommands(final QueryCommandParser parser) {
  318. if (queryCommands == null) {
  319. initLists();
  320. }
  321. for (Command com : queryCommands) {
  322. parser.registerCommand(com);
  323. }
  324. queryParsers.add(parser);
  325. }
  326. /**
  327. * Retrieves the server command identified by the specified signature.
  328. * @param signature The signature to look for
  329. * @return A server command with a matching signature, or null if none
  330. * were found.
  331. */
  332. public static ServerCommand getServerCommand(final String signature) {
  333. if (serverCommands == null) {
  334. initLists();
  335. }
  336. for (Command com : serverCommands) {
  337. if (com.getSignature().equalsIgnoreCase(signature)) {
  338. return (ServerCommand) com;
  339. }
  340. }
  341. return null;
  342. }
  343. /**
  344. * Retrieves the global command identified by the specified signature.
  345. * @param signature The signature to look for
  346. * @return A global command with a matching signature, or null if none
  347. * were found.
  348. */
  349. public static GlobalCommand getGlobalCommand(final String signature) {
  350. if (globalCommands == null) {
  351. initLists();
  352. }
  353. for (Command com : globalCommands) {
  354. if (com.getSignature().equalsIgnoreCase(signature)) {
  355. return (GlobalCommand) com;
  356. }
  357. }
  358. return null;
  359. }
  360. /**
  361. * Retrieves the channel command identified by the specified signature.
  362. * @param signature The signature to look for
  363. * @return A channel command with a matching signature, or null if none
  364. * were found.
  365. */
  366. public static ChannelCommand getChannelCommand(final String signature) {
  367. if (channelCommands == null) {
  368. initLists();
  369. }
  370. for (Command com : channelCommands) {
  371. if (com.getSignature().equalsIgnoreCase(signature)) {
  372. return (ChannelCommand) com;
  373. }
  374. }
  375. return null;
  376. }
  377. /**
  378. * Returns a list containing the server commands that have been initialised
  379. * by this command manager.
  380. * @return An ArrayList of server commands, or null if none have been loaded
  381. */
  382. public static List<Command> getServerCommands() {
  383. return serverCommands;
  384. }
  385. /**
  386. * Returns a list containing the channel commands that have been initialised
  387. * by this command manager.
  388. * @return An ArrayList of channel commands, or null if none have been loaded
  389. */
  390. public static List<Command> getChannelCommands() {
  391. return channelCommands;
  392. }
  393. /**
  394. * Returns a list containing the query commands that have been initialised
  395. * by this command manager.
  396. * @return An ArrayList of query commands, or null if none have been loaded
  397. */
  398. public static List<Command> getQueryCommands() {
  399. return queryCommands;
  400. }
  401. /**
  402. * Determines if the specified command is a valid channel command.
  403. * @param command The name of the command to test
  404. * @return True iff the command is a channel command, false otherwise
  405. */
  406. public static boolean isChannelCommand(final String command) {
  407. if (channelCommands == null) {
  408. CommandManager.initLists();
  409. }
  410. for (Command chanCommand : channelCommands) {
  411. if (chanCommand.getName().equalsIgnoreCase(command)) {
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417. /**
  418. * Returns the names (including command char) of all registered server
  419. * commands.
  420. * @return An ArrayList&lt;String&gt; containing all registered server command
  421. * names
  422. */
  423. public static List<String> getServerCommandNames() {
  424. if (serverCommands == null) {
  425. CommandManager.initLists();
  426. }
  427. return getCommandNames(serverCommands);
  428. }
  429. /**
  430. * Returns the names (including command char) of all registered global
  431. * commands.
  432. * @return An ArrayList&lt;String&gt; containing all registered global command
  433. * names
  434. */
  435. public static List<String> getGlobalCommandNames() {
  436. if (globalCommands == null) {
  437. CommandManager.initLists();
  438. }
  439. return getCommandNames(globalCommands);
  440. }
  441. /**
  442. * Returns the names (including command char) of all registered channel
  443. * commands.
  444. * @return An ArrayList&lt;String&gt; containing all registered server command
  445. * names
  446. */
  447. public static List<String> getChannelCommandNames() {
  448. if (channelCommands == null) {
  449. CommandManager.initLists();
  450. }
  451. return getCommandNames(channelCommands);
  452. }
  453. /**
  454. * Returns the names (including command char) of all registered channel
  455. * commands.
  456. * @return An ArrayList&lt;String&gt; containing all registered server command
  457. * names
  458. */
  459. public static List<String> getQueryCommandNames() {
  460. if (queryCommands == null) {
  461. CommandManager.initLists();
  462. }
  463. return getCommandNames(queryCommands);
  464. }
  465. /**
  466. * Iterates through the specified source and returns a list of the names
  467. * of all commands found in it.
  468. * @param source The source vector to iterate over
  469. * @return A list of all commands in the source
  470. */
  471. private static List<String> getCommandNames(final List<Command> source) {
  472. if (source == null) {
  473. return null;
  474. }
  475. final List<String> res = new ArrayList<String>();
  476. for (Command command : source) {
  477. res.add(Config.getCommandChar() + command.getName());
  478. }
  479. return res;
  480. }
  481. }