Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ServerCommandParser.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.ServerState;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.Command;
  23. import com.dmdirc.commandparser.commands.context.CommandContext;
  24. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  25. import com.dmdirc.events.CommandErrorEvent;
  26. import com.dmdirc.interfaces.CommandController;
  27. import com.dmdirc.interfaces.Connection;
  28. import com.dmdirc.events.eventbus.EventBus;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import com.dmdirc.config.provider.AggregateConfigProvider;
  31. import javax.annotation.Nonnull;
  32. import static com.google.common.base.Preconditions.checkNotNull;
  33. /**
  34. * A command parser used in the context of a server.
  35. */
  36. public class ServerCommandParser extends GlobalCommandParser {
  37. /** A version number for this class. */
  38. private static final long serialVersionUID = 1;
  39. /**
  40. * The server instance that this parser is attached to.
  41. */
  42. private final Connection server;
  43. /**
  44. * Creates a new command parser for server commands.
  45. *
  46. * @param configManager Config manager to read settings from
  47. * @param commandController The controller to load commands from.
  48. * @param eventBus Event bus to post events on
  49. */
  50. public ServerCommandParser(
  51. final AggregateConfigProvider configManager,
  52. final CommandController commandController,
  53. final EventBus eventBus,
  54. final Connection connection) {
  55. super(configManager, commandController, eventBus);
  56. this.server = checkNotNull(connection);
  57. }
  58. /** Loads the relevant commands into the parser. */
  59. @Override
  60. protected void loadCommands() {
  61. commandManager.loadCommands(this, CommandType.TYPE_GLOBAL, CommandType.TYPE_SERVER);
  62. }
  63. @Override
  64. protected CommandContext getCommandContext(
  65. final WindowModel origin,
  66. final CommandInfo commandInfo,
  67. final Command command,
  68. final CommandArguments args) {
  69. return new ServerCommandContext(origin, commandInfo, server);
  70. }
  71. @Override
  72. protected void executeCommand(
  73. @Nonnull final WindowModel origin,
  74. final CommandInfo commandInfo,
  75. final Command command,
  76. final CommandArguments args,
  77. final CommandContext context) {
  78. if (commandInfo.getType() == CommandType.TYPE_SERVER) {
  79. if (hasCommandOptions(command) && !getCommandOptions(command).allowOffline()
  80. && (server == null || (server.getState() != ServerState.CONNECTED
  81. && server.getState() != ServerState.CONNECTING)
  82. || !server.getParser().isPresent())) {
  83. if (!args.isSilent()) {
  84. origin.getEventBus().publishAsync(new CommandErrorEvent(origin,
  85. "You must be connected to use this command"));
  86. }
  87. } else {
  88. command.execute(origin, args, context);
  89. }
  90. } else {
  91. super.executeCommand(origin, commandInfo, command, args, context);
  92. }
  93. }
  94. /**
  95. * Called when the input was a line of text that was not a command. This normally means it is
  96. * sent to the server/channel/user as-is, with no further processing.
  97. *
  98. * @param origin The window in which the command was typed
  99. * @param line The line input by the user
  100. */
  101. @Override
  102. protected void handleNonCommand(final WindowModel origin, final String line) {
  103. server.sendLine(line);
  104. }
  105. }