Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ExecCommand.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.addons.exec;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.commandparser.BaseCommandInfo;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.Command;
  28. import com.dmdirc.commandparser.commands.context.CommandContext;
  29. import com.dmdirc.events.UserErrorEvent;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.WindowModel;
  32. import com.dmdirc.logger.ErrorLevel;
  33. import com.dmdirc.util.CommandUtils;
  34. import com.dmdirc.util.io.StreamUtils;
  35. import com.google.common.io.CharStreams;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import java.io.InputStreamReader;
  39. import java.util.List;
  40. import javax.annotation.Nonnull;
  41. import javax.inject.Inject;
  42. /**
  43. * A command which allows users execute scripts.
  44. */
  45. public class ExecCommand extends Command {
  46. /** A command info object for this command. */
  47. public static final BaseCommandInfo INFO = new BaseCommandInfo("exec",
  48. "exec <command> [<parameters>] - executes an external program "
  49. + "and displays the output", CommandType.TYPE_GLOBAL);
  50. /** Event bus to post errors to. */
  51. private final DMDircMBassador eventBus;
  52. /**
  53. * Creates a new instance of this command.
  54. *
  55. * @param controller The controller to use for command information.
  56. * @param eventBus The event bus to post errors to
  57. */
  58. @Inject
  59. public ExecCommand(final CommandController controller, final DMDircMBassador eventBus) {
  60. super(controller);
  61. this.eventBus = eventBus;
  62. }
  63. @Override
  64. public void execute(@Nonnull final WindowModel origin,
  65. final CommandArguments args, final CommandContext context) {
  66. final String[] commandArray = CommandUtils.parseArguments(
  67. args.getArgumentsAsString());
  68. try {
  69. // This checks the command to execute has correct quotes
  70. // (if necessary). Without this /exec "command arg1 arg2 would error.
  71. if (commandArray.length == 0) {
  72. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  73. "Could not execute: Invalid file name provided");
  74. } else if (!new File(commandArray[0]).exists()) {
  75. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  76. "Could not execute: " + commandArray[0] + " does not exist.");
  77. } else {
  78. final Process p = Runtime.getRuntime().exec(commandArray);
  79. if (args.isSilent()) {
  80. StreamUtils.readStream(p.getInputStream());
  81. StreamUtils.readStream(p.getErrorStream());
  82. } else {
  83. final List<String> execOutput = CharStreams.readLines(
  84. new InputStreamReader(p.getInputStream()));
  85. final List<String> errorOutput = CharStreams.readLines(
  86. new InputStreamReader(p.getErrorStream()));
  87. for (String line : execOutput) {
  88. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, line);
  89. }
  90. for (String line : errorOutput) {
  91. sendLine(origin, args.isSilent(), FORMAT_ERROR, line);
  92. }
  93. }
  94. }
  95. } catch (IOException ex) {
  96. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
  97. "Unable to run application: " + ex.getMessage(), ""));
  98. }
  99. }
  100. }