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.

ServerCommandParser.java 4.7KB

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