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.

Echo.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.commands.global;
  18. import com.dmdirc.CustomWindow;
  19. import com.dmdirc.commandparser.BaseCommandInfo;
  20. import com.dmdirc.commandparser.CommandArguments;
  21. import com.dmdirc.commandparser.CommandInfo;
  22. import com.dmdirc.commandparser.CommandType;
  23. import com.dmdirc.commandparser.commands.BaseCommand;
  24. import com.dmdirc.commandparser.commands.IntelligentCommand;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.commandparser.commands.flags.CommandFlag;
  27. import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
  28. import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
  29. import com.dmdirc.events.CommandOutputEvent;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.ui.WindowManager;
  34. import com.dmdirc.ui.input.AdditionalTabTargets;
  35. import javax.annotation.Nonnull;
  36. import javax.annotation.Nullable;
  37. import javax.inject.Inject;
  38. import java.time.LocalDateTime;
  39. import java.time.ZoneId;
  40. import java.util.ArrayList;
  41. import java.util.Collection;
  42. import java.util.Date;
  43. import java.util.Optional;
  44. import java.util.stream.Collectors;
  45. /**
  46. * The echo commands simply echos text to the current window.
  47. */
  48. public class Echo extends BaseCommand implements IntelligentCommand {
  49. /** A command info object for this command. */
  50. public static final CommandInfo INFO = new BaseCommandInfo("echo",
  51. "echo [--ts <timestamp>] [--target <window>] <line> "
  52. + "- echos the specified line to the window",
  53. CommandType.TYPE_GLOBAL);
  54. /** The flag used to specify a timestamp for the echo command. */
  55. private final CommandFlag timeStampFlag = new CommandFlag("ts", true, 1, 0);
  56. /** The flag used to specify a target for the echo command. */
  57. private final CommandFlag targetFlag = new CommandFlag("target", true, 1, 0);
  58. /** The command flag handler for this command. */
  59. private final CommandFlagHandler handler;
  60. /** Window management. */
  61. private final WindowManager windowManager;
  62. /**
  63. * Creates a new instance of Echo.
  64. *
  65. * @param controller Command controller
  66. * @param windowManager Window management
  67. */
  68. @Inject
  69. public Echo(final CommandController controller, final WindowManager windowManager) {
  70. super(controller);
  71. this.windowManager = windowManager;
  72. handler = new CommandFlagHandler(timeStampFlag, targetFlag);
  73. }
  74. @Override
  75. public void execute(@Nonnull final WindowModel origin,
  76. final CommandArguments args, final CommandContext context) {
  77. @Nullable final CommandFlagResult results = handler.process(origin, args);
  78. if (results == null) {
  79. return;
  80. }
  81. Date time = new Date();
  82. if (results.hasFlag(timeStampFlag)) {
  83. try {
  84. time = new Date(Long.parseLong(results.getArgumentsAsString(timeStampFlag)));
  85. } catch (NumberFormatException ex) {
  86. showError(origin, args.isSilent(), "Unable to process timestamp");
  87. return;
  88. }
  89. }
  90. if (results.hasFlag(targetFlag)) {
  91. WindowModel frame = null;
  92. Optional<WindowModel> target = Optional.of(origin);
  93. while (frame == null && target.isPresent()) {
  94. frame = windowManager.findCustomWindow(target.get(),
  95. results.getArgumentsAsString(targetFlag));
  96. target = target.flatMap(windowManager::getParent);
  97. }
  98. if (frame == null) {
  99. frame = windowManager.findCustomWindow(results.getArgumentsAsString(targetFlag));
  100. }
  101. if (frame == null) {
  102. showError(origin, args.isSilent(), "Unable to find target window");
  103. } else if (!args.isSilent()) {
  104. frame.getEventBus().publishAsync(new CommandOutputEvent(
  105. LocalDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault()),
  106. frame, results.getArgumentsAsString()));
  107. }
  108. } else if (!args.isSilent()) {
  109. origin.getEventBus().publishAsync(new CommandOutputEvent(
  110. LocalDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault()), origin,
  111. results.getArgumentsAsString()));
  112. }
  113. }
  114. @Override
  115. public AdditionalTabTargets getSuggestions(final int arg,
  116. final IntelligentCommandContext context) {
  117. final AdditionalTabTargets targets = new AdditionalTabTargets();
  118. if (arg == 0) {
  119. targets.add("--target");
  120. targets.add("--ts");
  121. } else if (arg == 1 && "--target".equals(context.getPreviousArgs().get(0))
  122. || arg == 3 && "--target".equals(context.getPreviousArgs().get(2))
  123. && "--ts".equals(context.getPreviousArgs().get(0))) {
  124. final Collection<WindowModel> windowList = new ArrayList<>();
  125. final Optional<Connection> connection = context.getWindow().getConnection();
  126. //Active window's Children
  127. windowList.addAll(windowManager.getChildren(context.getWindow()));
  128. //Children of Current Window's server
  129. connection
  130. .map(Connection::getWindowModel)
  131. .map(windowManager::getChildren)
  132. .ifPresent(windowList::addAll);
  133. //Global Windows
  134. windowList.addAll(windowManager.getRootWindows());
  135. targets.addAll(
  136. windowList.stream().filter(customWindow -> customWindow instanceof CustomWindow)
  137. .map(WindowModel::getName).collect(Collectors.toList()));
  138. targets.excludeAll();
  139. } else if (arg == 1 && "--ts".equals(context.getPreviousArgs().get(0))) {
  140. targets.add(String.valueOf(new Date().getTime()));
  141. targets.excludeAll();
  142. }
  143. return targets;
  144. }
  145. }