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

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