Browse Source

Exec command plugin

Fixes issue CLIENT-223

Change-Id: I0a25c94b05e364b483227a11311e26238b5a0d78
Reviewed-on: http://gerrit.dmdirc.com/1891
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.7rc1
Simon Mott 13 years ago
parent
commit
cfe085d856

+ 88
- 0
src/com/dmdirc/addons/exec/ExecCommand.java View File

@@ -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
+}

+ 58
- 0
src/com/dmdirc/addons/exec/ExecCommandInfo.java View File

@@ -0,0 +1,58 @@
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.commandparser.CommandInfo;
27
+import com.dmdirc.commandparser.CommandType;
28
+
29
+/**
30
+ * Exec command info.
31
+ */
32
+public class ExecCommandInfo implements CommandInfo {
33
+
34
+    /** {@inheritDoc} */
35
+    @Override
36
+    public String getName() {
37
+        return "exec";
38
+    }
39
+
40
+    /** {@inheritDoc} */
41
+    @Override
42
+    public boolean showInHelp() {
43
+        return true;
44
+    }
45
+
46
+    /** {@inheritDoc} */
47
+    @Override
48
+    public String getHelp() {
49
+        return "exec <command> [<parameters>] - Executes an external program "
50
+                + "and displays the output";
51
+    }
52
+
53
+    /** {@inheritDoc} */
54
+    @Override
55
+    public CommandType getType() {
56
+        return CommandType.TYPE_GLOBAL;
57
+    }
58
+}

+ 51
- 0
src/com/dmdirc/addons/exec/ExecPlugin.java View File

@@ -0,0 +1,51 @@
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.commandparser.CommandManager;
27
+import com.dmdirc.plugins.Plugin;
28
+
29
+/**
30
+ * A plugin which provides an execute command.
31
+ */
32
+public class ExecPlugin extends Plugin {
33
+
34
+    /** The command we register when loaded. */
35
+    private final ExecCommand command = new ExecCommand();
36
+    /** The CommandInfo object describing the exec command. */
37
+    private final ExecCommandInfo commandInfo = new ExecCommandInfo();
38
+
39
+    /** {@inheritDoc} */
40
+    @Override
41
+    public void onLoad() {
42
+        CommandManager.registerCommand(command, commandInfo);
43
+    }
44
+
45
+    /** {@inheritDoc} */
46
+    @Override
47
+    public void onUnload() {
48
+        CommandManager.unregisterCommand(commandInfo);
49
+    }
50
+
51
+}

+ 25
- 0
src/com/dmdirc/addons/exec/plugin.config View File

@@ -0,0 +1,25 @@
1
+# This is a DMDirc configuration file.
2
+
3
+# This section indicates which sections below take key/value
4
+# pairs, rather than a simple list. It should be placed above
5
+# any sections that take key/values.
6
+keysections:
7
+  metadata
8
+  updates
9
+  version
10
+
11
+metadata:
12
+  author=Simon <simon@dmdirc.com>
13
+  mainclass=com.dmdirc.addons.exec.ExecPlugin
14
+  description=Executes commands
15
+  name=exec
16
+  nicename=Exec Plugin
17
+
18
+updates:
19
+  id=65
20
+
21
+version:
22
+  friendly=0.1
23
+
24
+provides:
25
+  exec command

Loading…
Cancel
Save