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.

ExecCommand.java 4.2KB

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