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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2014 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.FrameContainer;
  25. import com.dmdirc.Server;
  26. import com.dmdirc.commandparser.BaseCommandInfo;
  27. import com.dmdirc.commandparser.CommandArguments;
  28. import com.dmdirc.commandparser.CommandInfo;
  29. import com.dmdirc.commandparser.CommandType;
  30. import com.dmdirc.commandparser.commands.Command;
  31. import com.dmdirc.commandparser.commands.IntelligentCommand;
  32. import com.dmdirc.commandparser.commands.context.CommandContext;
  33. import com.dmdirc.commandparser.commands.flags.CommandFlag;
  34. import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
  35. import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
  36. import com.dmdirc.interfaces.CommandController;
  37. import com.dmdirc.ui.WindowManager;
  38. import com.dmdirc.ui.input.AdditionalTabTargets;
  39. import com.google.common.base.Optional;
  40. import java.util.ArrayList;
  41. import java.util.Date;
  42. import java.util.List;
  43. import javax.annotation.Nonnull;
  44. import javax.inject.Inject;
  45. /**
  46. * The echo commands simply echos text to the current window.
  47. */
  48. public class Echo extends Command 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 FrameContainer origin,
  76. final CommandArguments args, final CommandContext context) {
  77. 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. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Unable to process timestamp");
  87. return;
  88. }
  89. }
  90. if (results.hasFlag(targetFlag)) {
  91. FrameContainer frame = null;
  92. Optional<FrameContainer> target = Optional.of(origin);
  93. while (frame == null && target.isPresent()) {
  94. frame = windowManager.findCustomWindow(target.get(),
  95. results.getArgumentsAsString(targetFlag));
  96. target = target.get().getParent();
  97. }
  98. if (frame == null) {
  99. frame = windowManager.findCustomWindow(results.getArgumentsAsString(targetFlag));
  100. }
  101. if (frame == null) {
  102. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  103. "Unable to find target window");
  104. } else if (!args.isSilent()) {
  105. frame.addLine(FORMAT_OUTPUT, time, results.getArgumentsAsString());
  106. }
  107. } else if (origin != null && !args.isSilent()) {
  108. origin.addLine(FORMAT_OUTPUT, time, results.getArgumentsAsString());
  109. }
  110. }
  111. @Override
  112. public AdditionalTabTargets getSuggestions(final int arg,
  113. final IntelligentCommandContext context) {
  114. final AdditionalTabTargets targets = new AdditionalTabTargets();
  115. if (arg == 0) {
  116. targets.add("--target");
  117. targets.add("--ts");
  118. } else if ((arg == 1 && context.getPreviousArgs().get(0).equals("--target"))
  119. || (arg == 3 && context.getPreviousArgs().get(2).equals("--target")
  120. && context.getPreviousArgs().get(0).equals("--ts"))) {
  121. final List<FrameContainer> windowList = new ArrayList<>();
  122. final Server currentServer = (Server) context.getWindow().getConnection();
  123. //Active window's Children
  124. windowList.addAll(context.getWindow().getChildren());
  125. //Children of Current Window's server
  126. if (currentServer != null) {
  127. windowList.addAll(currentServer.getChildren());
  128. }
  129. //Global Windows
  130. windowList.addAll(windowManager.getRootWindows());
  131. for (FrameContainer customWindow : windowList) {
  132. if (customWindow instanceof CustomWindow) {
  133. targets.add(customWindow.getName());
  134. }
  135. }
  136. targets.excludeAll();
  137. } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--ts")) {
  138. targets.add(String.valueOf(new Date().getTime()));
  139. targets.excludeAll();
  140. }
  141. return targets;
  142. }
  143. }