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