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.

AutoCommandHandler.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.auto;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.GlobalWindow;
  25. import com.dmdirc.commandparser.parsers.CommandParser;
  26. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  27. import com.dmdirc.events.ClientOpenedEvent;
  28. import com.dmdirc.events.ServerConnectedEvent;
  29. import com.dmdirc.interfaces.CommandController;
  30. import java.util.Optional;
  31. import net.engio.mbassy.listener.Handler;
  32. /**
  33. * Handles execution of {@link AutoCommand}s.
  34. */
  35. public class AutoCommandHandler {
  36. private final CommandController commandController;
  37. private final GlobalCommandParser globalCommandParser;
  38. private final GlobalWindow globalWindow;
  39. private final AutoCommand autoCommand;
  40. public AutoCommandHandler(
  41. final CommandController commandController,
  42. final GlobalCommandParser globalCommandParser,
  43. final GlobalWindow globalWindow,
  44. final AutoCommand autoCommand) {
  45. this.commandController = commandController;
  46. this.globalCommandParser = globalCommandParser;
  47. this.globalWindow = globalWindow;
  48. this.autoCommand = autoCommand;
  49. }
  50. /**
  51. * Handles auto-commands that respond to the client opening.
  52. *
  53. * @param event The event triggering the command.
  54. */
  55. @Handler
  56. public void checkAutoCommand(final ClientOpenedEvent event) {
  57. if (isGlobalCommand()) {
  58. execute(globalWindow, globalCommandParser);
  59. }
  60. }
  61. /**
  62. * Handles auto-commands that respond to a server connecting.
  63. *
  64. * @param event The event triggering the command.
  65. */
  66. @Handler
  67. public void checkAutoCommand(final ServerConnectedEvent event) {
  68. if (appliesToServer(event.getConnection().getNetwork(),
  69. event.getConnection().getAddress(), event.getConnection().getProfile().getName())) {
  70. final FrameContainer container = event.getConnection().getWindowModel();
  71. final CommandParser parser = container.getCommandParser();
  72. execute(container, parser);
  73. }
  74. }
  75. private boolean appliesToServer(final String network, final String server,
  76. final String profile) {
  77. return !isGlobalCommand()
  78. && matchesIfPresent(autoCommand.getNetwork(), network)
  79. && matchesIfPresent(autoCommand.getServer(), server)
  80. && matchesIfPresent(autoCommand.getProfile(), profile);
  81. }
  82. private boolean isGlobalCommand() {
  83. return !autoCommand.getServer().isPresent() && !autoCommand.getNetwork().isPresent();
  84. }
  85. private boolean matchesIfPresent(final Optional<String> target, final String value) {
  86. return target.map(value::equalsIgnoreCase).orElse(true);
  87. }
  88. private void execute(final FrameContainer origin, final CommandParser parser) {
  89. for (String line : autoCommand.getResponse().split("\n")) {
  90. parser.parseCommand(origin, commandController.getCommandChar() + line);
  91. }
  92. }
  93. }