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.

ChatCommandParser.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.ChatCommandContext;
  23. import com.dmdirc.commandparser.commands.context.CommandContext;
  24. import com.dmdirc.interfaces.Chat;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import javax.annotation.Nonnull;
  29. /**
  30. * A command parser which implements common functionality for chat windows (queries and channels).
  31. *
  32. * @since 0.6.4
  33. */
  34. public class ChatCommandParser extends ServerCommandParser {
  35. /** A version number for this class. */
  36. private static final long serialVersionUID = 1;
  37. /** The container that owns this parser. */
  38. private final Chat owner;
  39. /**
  40. * Creates a new chat command parser that belongs to a child of the specified server.
  41. *
  42. * @param owner The container which owns this parser's container
  43. * @param commandController The controller to load commands from.
  44. * @param eventBus Event but to post events on
  45. */
  46. public ChatCommandParser(
  47. final WindowModel owner,
  48. final CommandController commandController,
  49. final EventBus eventBus,
  50. final Chat chat) {
  51. super(owner.getConfigManager(), commandController, eventBus, chat.getConnection().get());
  52. this.owner = chat;
  53. }
  54. @Override
  55. protected CommandContext getCommandContext(
  56. final WindowModel origin,
  57. final CommandInfo commandInfo,
  58. final Command command,
  59. final CommandArguments args) {
  60. return new ChatCommandContext(origin, commandInfo, owner);
  61. }
  62. @Override
  63. protected void executeCommand(
  64. @Nonnull final WindowModel origin,
  65. final CommandInfo commandInfo,
  66. final Command command,
  67. final CommandArguments args,
  68. final CommandContext context) {
  69. if (commandInfo.getType() == CommandType.TYPE_CHAT) {
  70. command.execute(origin, args, context);
  71. } else {
  72. super.executeCommand(origin, commandInfo, command, args, context);
  73. }
  74. }
  75. @Override
  76. protected void handleNonCommand(final WindowModel origin,
  77. final String line) {
  78. owner.sendLine(line);
  79. }
  80. }