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.

GlobalCommandParser.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.parsers;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.CommandInfo;
  25. import com.dmdirc.commandparser.CommandType;
  26. import com.dmdirc.commandparser.commands.Command;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.config.GlobalConfig;
  29. import com.dmdirc.events.CommandErrorEvent;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.EventBus;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  34. import javax.annotation.Nonnull;
  35. import javax.inject.Inject;
  36. import javax.inject.Singleton;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import static com.dmdirc.util.LogUtils.USER_ERROR;
  40. /**
  41. * The command parser used for global commands.
  42. */
  43. @Singleton
  44. public class GlobalCommandParser extends CommandParser {
  45. private static final Logger LOG = LoggerFactory.getLogger(GlobalCommandParser.class);
  46. /** A version number for this class. */
  47. private static final long serialVersionUID = 1;
  48. /**
  49. * Creates a new command parser for global commands.
  50. *
  51. * @param configManager Config manager to read settings from
  52. * @param commandManager Command manager to load commands from
  53. * @param eventBus eventBus
  54. */
  55. @Inject
  56. public GlobalCommandParser(
  57. @GlobalConfig final AggregateConfigProvider configManager,
  58. final CommandController commandManager,
  59. final EventBus eventBus) {
  60. super(configManager, commandManager, eventBus);
  61. }
  62. /** Loads the relevant commands into the parser. */
  63. @Override
  64. protected void loadCommands() {
  65. commandManager.loadCommands(this, CommandType.TYPE_GLOBAL);
  66. }
  67. @Override
  68. protected CommandContext getCommandContext(
  69. final WindowModel origin,
  70. final CommandInfo commandInfo,
  71. final Command command,
  72. final CommandArguments args) {
  73. return new CommandContext(origin, commandInfo);
  74. }
  75. @Override
  76. protected void executeCommand(
  77. @Nonnull final WindowModel origin,
  78. final CommandInfo commandInfo,
  79. final Command command,
  80. final CommandArguments args,
  81. final CommandContext context) {
  82. command.execute(origin, args, context);
  83. }
  84. /**
  85. * Called when the input was a line of text that was not a command. This normally means it is
  86. * sent to the server/channel/user as-is, with no further processing.
  87. *
  88. * @param origin The window in which the command was typed
  89. * @param line The line input by the user
  90. */
  91. @Override
  92. protected void handleNonCommand(final WindowModel origin, final String line) {
  93. if (origin == null) {
  94. LOG.warn(USER_ERROR, "Invalid Global Command: {}", line,
  95. new IllegalArgumentException("Invalid Global Command: " + line));
  96. } else {
  97. origin.getEventBus().publishAsync(
  98. new CommandErrorEvent(origin, "Invalid global command: " + line));
  99. }
  100. }
  101. }