Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GlobalCommandParser.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.parsers;
  18. import com.dmdirc.commandparser.CommandArguments;
  19. import com.dmdirc.commandparser.CommandInfo;
  20. import com.dmdirc.commandparser.CommandType;
  21. import com.dmdirc.commandparser.commands.Command;
  22. import com.dmdirc.commandparser.commands.context.CommandContext;
  23. import com.dmdirc.config.GlobalConfig;
  24. import com.dmdirc.events.CommandErrorEvent;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import com.dmdirc.config.provider.AggregateConfigProvider;
  29. import javax.annotation.Nonnull;
  30. import javax.inject.Inject;
  31. import javax.inject.Singleton;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import static com.dmdirc.util.LogUtils.USER_ERROR;
  35. /**
  36. * The command parser used for global commands.
  37. */
  38. @Singleton
  39. public class GlobalCommandParser extends BaseCommandParser {
  40. private static final Logger LOG = LoggerFactory.getLogger(GlobalCommandParser.class);
  41. /** A version number for this class. */
  42. private static final long serialVersionUID = 1;
  43. /**
  44. * Creates a new command parser for global commands.
  45. *
  46. * @param configManager Config manager to read settings from
  47. * @param commandManager Command manager to load commands from
  48. * @param eventBus eventBus
  49. */
  50. @Inject
  51. public GlobalCommandParser(
  52. @GlobalConfig final AggregateConfigProvider configManager,
  53. final CommandController commandManager,
  54. final EventBus eventBus) {
  55. super(configManager, commandManager, eventBus);
  56. }
  57. /** Loads the relevant commands into the parser. */
  58. @Override
  59. protected void loadCommands() {
  60. commandManager.loadCommands(this, CommandType.TYPE_GLOBAL);
  61. }
  62. @Override
  63. protected CommandContext getCommandContext(
  64. final WindowModel origin,
  65. final CommandInfo commandInfo,
  66. final Command command,
  67. final CommandArguments args) {
  68. return new CommandContext(origin, commandInfo);
  69. }
  70. @Override
  71. protected void executeCommand(
  72. @Nonnull final WindowModel origin,
  73. final CommandInfo commandInfo,
  74. final Command command,
  75. final CommandArguments args,
  76. final CommandContext context) {
  77. command.execute(origin, args, context);
  78. }
  79. /**
  80. * Called when the input was a line of text that was not a command. This normally means it is
  81. * sent to the server/channel/user as-is, with no further processing.
  82. *
  83. * @param origin The window in which the command was typed
  84. * @param line The line input by the user
  85. */
  86. @Override
  87. protected void handleNonCommand(final WindowModel origin, final String line) {
  88. if (origin == null) {
  89. LOG.warn(USER_ERROR, "Invalid Global Command: {}", line,
  90. new IllegalArgumentException("Invalid Global Command: " + line));
  91. } else {
  92. origin.getEventBus().publishAsync(
  93. new CommandErrorEvent(origin, "Invalid global command: " + line));
  94. }
  95. }
  96. }