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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.exec;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandType;
  21. import com.dmdirc.commandparser.commands.BaseCommand;
  22. import com.dmdirc.commandparser.commands.context.CommandContext;
  23. import com.dmdirc.interfaces.CommandController;
  24. import com.dmdirc.interfaces.WindowModel;
  25. import com.dmdirc.util.CommandUtils;
  26. import com.dmdirc.util.LogUtils;
  27. import com.dmdirc.util.io.StreamUtils;
  28. import com.google.common.io.CharStreams;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import javax.annotation.Nonnull;
  32. import javax.inject.Inject;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.io.InputStreamReader;
  36. import java.util.List;
  37. /**
  38. * A command which allows users execute scripts.
  39. */
  40. public class ExecCommand extends BaseCommand {
  41. private static final Logger LOG = LoggerFactory.getLogger(ExecCommand.class);
  42. /** A command info object for this command. */
  43. public static final BaseCommandInfo INFO = new BaseCommandInfo("exec",
  44. "exec <command> [<parameters>] - executes an external program "
  45. + "and displays the output", CommandType.TYPE_GLOBAL);
  46. @Inject
  47. public ExecCommand(final CommandController controller) {
  48. super(controller);
  49. }
  50. @Override
  51. public void execute(@Nonnull final WindowModel origin,
  52. final CommandArguments args, final CommandContext context) {
  53. final String[] commandArray = CommandUtils.parseArguments(
  54. args.getArgumentsAsString());
  55. try {
  56. // This checks the command to execute has correct quotes
  57. // (if necessary). Without this /exec "command arg1 arg2 would error.
  58. if (commandArray.length == 0) {
  59. showError(origin, args.isSilent(),
  60. "Could not execute: Invalid file name provided");
  61. } else if (!new File(commandArray[0]).exists()) {
  62. showError(origin, args.isSilent(),
  63. "Could not execute: " + commandArray[0] + " does not exist.");
  64. } else {
  65. final Process p = Runtime.getRuntime().exec(commandArray);
  66. if (args.isSilent()) {
  67. StreamUtils.readStream(p.getInputStream());
  68. StreamUtils.readStream(p.getErrorStream());
  69. } else {
  70. final List<String> execOutput = CharStreams.readLines(
  71. new InputStreamReader(p.getInputStream()));
  72. final List<String> errorOutput = CharStreams.readLines(
  73. new InputStreamReader(p.getErrorStream()));
  74. for (String line : execOutput) {
  75. showOutput(origin, args.isSilent(), line);
  76. }
  77. for (String line : errorOutput) {
  78. showError(origin, args.isSilent(), line);
  79. }
  80. }
  81. }
  82. } catch (IOException ex) {
  83. LOG.info(LogUtils.USER_ERROR, "Unable to run application: {}", ex.getMessage(), ex);
  84. }
  85. }
  86. }