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 6.8KB

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