]> iEval git - fonbot.git/commitdiff
Add partially-functioning getfile command
authorMarius Gavrilescu <marius@ieval.ro>
Sun, 28 Jul 2013 10:52:17 +0000 (13:52 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Sun, 28 Jul 2013 10:52:17 +0000 (13:52 +0300)
AndroidManifest.xml
res/values/strings.xml
src/ro/ieval/fonbot/Heavy.java
src/ro/ieval/fonbot/Utils.java

index cc92df34ed1f6cfa7da475552eedf6cf4f787438..4ae622da68e3f330ecfc2a0baab493bdc502ea78 100644 (file)
@@ -34,6 +34,7 @@
     <uses-permission android:name="android.permission.RECEIVE_SMS" />
     <uses-permission android:name="android.permission.VIBRATE" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 
index 2924533d084a51fcd7b6895f25ebffabe3020c3a..8b4dd013bf6721e201915c78780816e7fb168822 100644 (file)
@@ -466,4 +466,11 @@ The help command can be used to get a list of commands and help for them. Exampl
        <string name="toggling_torch_state">Toggling torch stateā€¦</string>
        <string name="device_booted">Your device has been turned on</string>
        <string name="cannot_parse_camera_number">Cannot parse camera number</string>
+       <string name="getfile_help">
+         Usage: getfile filename url\n
+         Downloads the given url to the file described by the given filename\n
+         Example: getfile /storage/sdcard1 http://ieval.ro/
+       </string>
+       <string name="error_opening_file">Error opening file %1$s (%2$s)</string>
+       <string name="file_received">File received</string>
 </resources>
index 85aff17016325459fb3f1d748f1b169e76ded185..cac04f7a14f3130d300f274bf6f4bff862fdedd8 100644 (file)
@@ -5,13 +5,18 @@ import static ro.ieval.fonbot.Utils.toNonNull;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.UnknownHostException;
+import java.net.URL;
+import java.net.URLConnection;
 import java.nio.channels.FileChannel;
 import java.nio.channels.SocketChannel;
 import java.util.ArrayList;
@@ -287,6 +292,153 @@ final class Heavy {
                }
        }
 
+       /**
+        * ExecutableRunnable that uploads a file to a given host and port, netcat-style
+        *
+        * @author Marius Gavrilescu <marius@ieval.ro>
+        */
+       private static final class NcfileExecutableRunnable extends ExecutableRunnable {
+               private final Context context;
+               private final Address replyTo;
+               private final String filename;
+               private final String hostname;
+               private final int port;
+
+               /**
+                * Construct a NcfileExecutableRunnable
+                *
+                * @param context Context instance
+                * @param replyTo reply Address
+                * @param filename filename to upload
+                * @param hostname hostname to upload to
+                * @param port port to upload to
+                */
+               NcfileExecutableRunnable(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
+                       this.context=context;
+                       this.replyTo=replyTo;
+                       this.filename=filename;
+                       this.hostname=hostname;
+                       this.port=port;
+               }
+
+               @Override public void run(){
+                       final FileChannel in;
+                       final SocketChannel sock;
+                       try{
+                               in=new FileInputStream(filename).getChannel();
+                       } catch (final FileNotFoundException e){
+                               Utils.sendMessage(context, replyTo, file_not_found, filename);
+                               return;
+                       }
+
+                       try{
+                               sock = SocketChannel.open(new InetSocketAddress(hostname, port));
+                       } catch (final IOException e){
+                               Utils.sendMessage(context, replyTo, toNonNull(context.getString(cannot_connect_to_host_on_port, hostname, Integer.valueOf(port))));
+                               try {
+                                       in.close();
+                               } catch (IOException ex) {
+                                       //ignored
+                               }
+                               return;
+                       }
+
+                       try{
+                               in.transferTo(0, in.size(), sock);
+                       } catch (final IOException e){
+                               Utils.sendMessage(context, replyTo, toNonNull(context.getString(io_error, e.getMessage())));
+                       } finally {
+                               try{
+                                       in.close();
+                               } catch (IOException e){
+                                       //ignored
+                               }
+                               try{
+                                       sock.close();
+                               } catch(IOException e){
+                                       //ignored
+                               }
+                       }
+                       Utils.sendMessage(context, replyTo, file_sent);
+               }
+       }
+
+       /**
+        * ExecutableRunnable that downloads a file from a given host and port, netcat-style
+        *
+        * @author Marius Gavrilescu <marius@ieval.ro>
+        */
+       private static final class GetfileExecutableRunnable extends ExecutableRunnable {
+               private final Context context;
+               private final Address replyTo;
+               private final String filename;
+               private final String hostname;
+               private final int port;
+
+               /**
+                * Construct a GetfileExecutableRunnable
+                *
+                * @param context Context instance
+                * @param replyTo reply Address
+                * @param filename filename to save to
+                * @param hostname hostname to download from
+                * @param port port to download from
+                */
+               GetfileExecutableRunnable(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
+                       this.context=context;
+                       this.replyTo=replyTo;
+                       this.filename=filename;
+                       this.hostname=hostname;
+                       this.port=port;
+               }
+
+               @Override public void run(){
+                       final InputStream in;
+                       final FileOutputStream out;
+                       final Socket sock;
+                       try{
+                               out=new FileOutputStream(filename);
+                       } catch (final IOException e){
+                               Utils.sendMessage(context, replyTo, error_opening_file, filename, e.getMessage());
+                               return;
+                       }
+
+                       try{
+                               sock = new Socket(hostname, port);
+                               in = sock.getInputStream();
+                       } catch (final IOException e){
+                               Utils.sendMessage(context, replyTo, toNonNull(context.getString(cannot_connect_to_host_on_port, hostname, Integer.valueOf(port))));
+                               try {
+                                       out.close();
+                               } catch (IOException ex) {
+                                       //ignored
+                               }
+                               return;
+                       }
+
+                       try{
+                               byte[] buffer=new byte[1024*1024*2];
+                               int nread;
+                               while((nread = in.read(buffer)) > 0)
+                                       out.write(buffer, 0, nread);
+                       } catch (final IOException e){
+                               Utils.sendMessage(context, replyTo, toNonNull(context.getString(io_error, e.getMessage())));
+                       } finally {
+                               try{
+                                       out.close();
+                               } catch (IOException e){
+                                       //ignored
+                               }
+                               try{
+                                       sock.close();
+                               } catch(IOException e){
+                                       //ignored
+                               }
+                       }
+                       Utils.sendMessage(context, replyTo, file_received);
+               }
+       }
+
        /**
         * PictureCallback that sends the picture to a server.
         *
@@ -1037,55 +1189,7 @@ final class Heavy {
         * @param port server port
         */
        public static void ncfile(final Context context, final Address replyTo, final String filename,final String hostname,final int port){
-               new AsyncTask<Void, Void, Void>() {
-                       @Override
-                       protected @Nullable Void doInBackground(@Nullable final Void... params) {
-                               final FileChannel in;
-                               try{
-                                       in=new FileInputStream(filename).getChannel();
-                               } catch (final FileNotFoundException e){
-                                       Utils.sendMessage(context, replyTo, file_not_found, filename);
-                                       return null;
-                               }
-                               final SocketChannel sock;
-                               try{
-                                       sock = SocketChannel.open(new InetSocketAddress(hostname, port));
-                               } catch (final IOException e){
-                                       Utils.sendMessage(context, replyTo, toNonNull(context.getString(
-                                                       cannot_connect_to_host_on_port, hostname, Integer.valueOf(port))));
-                                       try {
-                                               in.close();
-                                       } catch (IOException ex) {
-                                               //ignored
-                                       }
-                                       return null;
-                               }
-
-                               try{
-                                       in.transferTo(0, in.size(), sock);
-                               } catch (final IOException e){
-                                       Utils.sendMessage(context, replyTo, toNonNull(context.getString(
-                                                       io_error, e.getMessage())));
-                               } finally {
-                                       try{
-                                               in.close();
-                                       } catch (IOException e){
-                                               //ignored
-                                       }
-                                       try{
-                                               sock.close();
-                                       } catch(IOException e){
-                                               //ignored
-                                       }
-                               }
-                               return null;
-                       }
-
-                       @Override
-                       protected void onPostExecute(@Nullable final Void result) {
-                               Utils.sendMessage(context, replyTo, file_sent);
-                       }
-               }.execute();
+               new NcfileExecutableRunnable(context, replyTo, filename, hostname, port).execute();
        }
 
        /**
@@ -1790,4 +1894,17 @@ final class Heavy {
                context.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
                Utils.sendMessage(context, replyTo, toggling_torch_state);
        }
+
+       /**
+        * Download a file from a given URL to a given filename
+        *
+        * @param context Context instance
+        * @param replyTo reply Address
+        * @param filename filename to save to
+        * @param hostname hostname to download from
+        * @param port port to download from
+        */
+       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();
+       }
 }
index c425420e84013dfa545d55ff2e6633853304d044..f37773a2a98b8de22711d1568e157755be611d7c 100644 (file)
@@ -65,7 +65,7 @@ public final class Utils {
                LS, RM, CONTACTS, DISABLE, ENABLE,
                POLL, HANGUP, ANSWER, LAUNCH, DATA,
                GPS, GLOCATION, REBOOT, NOTIFY, SCREENCAP,
-               TORCH
+               TORCH, GETFILE
        }
 
        /**
@@ -986,6 +986,22 @@ public final class Utils {
                case TORCH:
                        Heavy.torch(context, replyTo);
                        break;
+
+               case GETFILE:
+                       if(args.length != 3){
+                               Heavy.help(context, replyTo, toNonNull(Command.GETFILE));
+                               return;
+                       }
+
+                       final int getfilePort;
+                       try{
+                               getfilePort=Integer.parseInt(args[2]);
+                       } catch (NumberFormatException e){
+                               sendMessage(context, replyTo, cannot_parse_port);
+                               break;
+                       }
+                       Heavy.getfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), getfilePort);
+                       break;
                }
 
        }
This page took 0.043971 seconds and 4 git commands to generate.