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

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