]> iEval git - fonbot.git/commitdiff
Add SH and ROOTSH commands
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 26 Aug 2014 11:39:22 +0000 (14:39 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 26 Aug 2014 11:39:22 +0000 (14:39 +0300)
res/values/strings.xml
src/ro/ieval/fonbot/Heavy.java
src/ro/ieval/fonbot/Utils.java

index 887f39c9f28b5afb627a393d13ab1a5a4753fa44..f72cfd0b40a9dabd744d6126bccb1e0581c92e7b 100644 (file)
@@ -471,4 +471,14 @@ The help command can be used to get a list of commands and help for them. Exampl
        <string name="error_opening_file">Error opening file %1$s (%2$s)</string>
        <string name="file_received">File received</string>
        <string name="reboot_failed">Reboot failed</string>
        <string name="error_opening_file">Error opening file %1$s (%2$s)</string>
        <string name="file_received">File received</string>
        <string name="reboot_failed">Reboot failed</string>
+       <string name="sh_help">
+         Usage: sh command [arg1 arg2 ...]\n
+         Executes a shell command and returns its output. FonBot waits for the command to finish, therefore this must not be used with long-running commands.\n
+         Example: sh ls /sdcard/
+       </string>
+       <string name="rootsh_help">
+         Usage: rootsh command [arg1 arg2 ...]\n
+         Executes a shell command as root and returns its output. FonBot waits for the command to finish, therefore this must not be used with long-running commands. This requires root access.\n
+         Example: rootsh stop;sleep 3;start
+       </string>
 </resources>
 </resources>
index 4dcd0fc4317723b6cc1740bb05bb1416f677d891..b1f443c5f6067fb8a1366f2e51e71777d3504b32 100644 (file)
@@ -3,11 +3,13 @@ package ro.ieval.fonbot;
 import static ro.ieval.fonbot.R.string.*;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
 import static ro.ieval.fonbot.R.string.*;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.reflect.InvocationTargetException;
@@ -665,6 +667,12 @@ final class Heavy {
                case TORCH:
                        Utils.sendMessage(context, replyTo, torch_help);
                        break;
                case TORCH:
                        Utils.sendMessage(context, replyTo, torch_help);
                        break;
+               case SH:
+                       Utils.sendMessage(context, replyTo, sh_help);
+                       break;
+               case ROOTSH:
+                       Utils.sendMessage(context, replyTo, rootsh_help);
+                       break;
                }
        }
 
                }
        }
 
@@ -1927,4 +1935,30 @@ final class Heavy {
        public static void getfile(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
                new GetfileExecutableRunnable(context, replyTo, filename, hostname, port).execute();
        }
        public static void getfile(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
                new GetfileExecutableRunnable(context, replyTo, filename, hostname, port).execute();
        }
+
+       /**
+        * Execute a command using a given shell and reply with the output.
+        *
+        * @param context Context instance
+        * @param replyTo reply Address
+        * @param shell The shell to execute with. Usually sh or su.
+        * @param command The command to pass to the shell.
+        */
+       public static void execute(final Context context, final Address replyTo, final String shell, final String command) {
+               try {
+                       final Process proc = Runtime.getRuntime().exec(new String[]{
+                                       shell,
+                                       "-c",
+                                       command
+                               });
+                       final BufferedReader br = new BufferedReader (new InputStreamReader(proc.getInputStream()));
+                       String line;
+                       while((line = br.readLine()) != null)
+                               Utils.sendMessage(context, replyTo, line);
+                       proc.waitFor();
+               } catch (final Exception e){
+                       Utils.sendMessage(context, replyTo, error_while_processing_command, e.getClass().getName(), e.getMessage());
+                       Log.w(Heavy.class.getName(), "Error while processing command", e);
+               }
+       }
 }
 }
index be7923ba5694d7c1eb169c0eb649fc9cfad8f4cc..e2e0dab2319ed86e085e98c52c91972ca8a8bcf7 100644 (file)
@@ -68,7 +68,7 @@ public final class Utils {
                LS, RM, CONTACTS, DISABLE, ENABLE,
                POLL, HANGUP, ANSWER, LAUNCH, DATA,
                GPS, GLOCATION, REBOOT, NOTIFY, SCREENCAP,
                LS, RM, CONTACTS, DISABLE, ENABLE,
                POLL, HANGUP, ANSWER, LAUNCH, DATA,
                GPS, GLOCATION, REBOOT, NOTIFY, SCREENCAP,
-               TORCH, GETFILE
+               TORCH, GETFILE, SH, ROOTSH
        }
 
        /**
        }
 
        /**
@@ -1009,6 +1009,24 @@ public final class Utils {
                        }
                        Heavy.getfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), getfilePort);
                        break;
                        }
                        Heavy.getfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), getfilePort);
                        break;
+
+               case SH:
+                       if(args.length == 0){
+                               Heavy.help(context, replyTo, toNonNull(Command.SH));
+                               return;
+                       }
+
+                       Heavy.execute(context, replyTo, "sh", join(" ", args));
+                       break;
+
+               case ROOTSH:
+                       if(args.length == 0){
+                               Heavy.help(context, replyTo, toNonNull(Command.ROOTSH));
+                               return;
+                       }
+
+                       Heavy.execute(context, replyTo, "su", join(" ", args));
+                       break;
                }
 
        }
                }
 
        }
This page took 0.03716 seconds and 4 git commands to generate.