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.1KB

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