489040b8f305578b4238b35a8d76fa551e49d422
[dumload.git] / src / com / joshuawise / dumload / Uploader.java
1 package com.joshuawise.dumload;
2
3 import java.io.InputStream;
4
5 import com.jcraft.jsch.*;
6 import java.lang.Boolean;
7
8 import android.app.Activity;
9 import android.app.Service;
10 import android.content.Intent;
11 import android.app.PendingIntent;
12 import android.content.Context;
13 import android.net.Uri;
14 import android.os.Bundle;
15 import android.os.IBinder;
16 import android.widget.TextView;
17 import android.widget.Toast;
18 import android.util.Log;
19 import android.app.NotificationManager;
20 import android.app.Notification;
21 import android.os.Handler;
22 import android.os.Messenger;
23 import android.os.Looper;
24 import android.os.Message;
25 import android.os.SystemClock;
26
27 public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardInteractive {
28         private Uri uri;
29         private String homedir;
30         private Thread me;
31         private static final int HELPME_ID = 1;
32         
33         public Object _theObject;
34
35         private Object /* pick one type, and fixate on it */ dance(final String type, final String text)        /* for inside the thread */
36         {
37                 final Uploader thisupl = this;
38                 final Message msg = Message.obtain();
39                 
40                 /* t(*A*t) */
41                 Thread t = new Thread() {
42                         public void run() {
43                                 Looper.prepare();
44                                 int bogon = (int)SystemClock.elapsedRealtime();
45                                 
46                                 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
47                                 Notification notification = new Notification(R.drawable.icon, "Dumload prompt", System.currentTimeMillis());
48                                 
49                                 Handler h = new Handler() {
50                                         public void handleMessage(Message M) {
51                                                 msg.copyFrom(M);
52                                                 Looper.myLooper().quit();
53                                         }
54                                 };
55                                 Messenger m = new Messenger(h);
56                                 
57                                 Intent intent = new Intent(thisupl, NotifSlave.class);
58                                         
59                                 intent.setAction("com.joshuawise.dumload.NotifSlave");
60                                 intent.putExtra("com.joshuawise.dumload.returnmessenger", m);
61                                 intent.putExtra("com.joshuawise.dumload.reqtype", type);
62                                 intent.putExtra("com.joshuawise.dumload.prompt", text);
63                                 intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
64                                 
65                                 PendingIntent contentIntent = PendingIntent.getActivity(thisupl, 0, intent, 0);
66                                 notification.defaults |= Notification.DEFAULT_VIBRATE;
67                                 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
68                                 notification.setLatestEventInfo(getApplicationContext(), "I've been had!", "Dumload needs your input.", contentIntent);
69                                 
70                                 Log.e("Dumload.Uploader[thread]", "Notifying...");
71                                 
72                                 mNotificationManager.notify(bogon, notification);
73                                 
74                                 Log.e("Dumload.Uploader[thread]", "About to go to 'sleep'...");
75                                 Looper.loop();
76                                 Log.e("Dumload.Uploader[thread]", "And we're alive!");
77                                         
78                                 Log.e("Dumload.Uploader[thread]", "result was: "+(Integer.toString(msg.arg1)));
79                                 
80                                 mNotificationManager.cancel(bogon);
81                         }
82                 };
83                 
84                 t.start();
85                 try {
86                         t.join();
87                 } catch (Exception e) {
88                         return null;
89                 }
90                 
91                 if (type.equals("yesno"))
92                         return new Boolean(msg.arg1 == 1);
93                 else if (type.equals("message"))
94                         return null;
95                 else if (type.equals("password")) {
96                         if (msg.arg1 == 0)
97                                 return null;
98                         Bundle b = msg.getData();
99                         return b.getString("response");
100                 } else
101                         return null;
102         }
103         
104         /* UserInfo bits */
105         String _password = null;
106         public String getPassword()
107         {
108                 return _password;
109         }
110         public boolean promptPassword(String message)
111         {
112                 _password = (String)dance("password", message); 
113                 return (_password != null);
114         }
115         
116         String _passphrase = null;
117         public String getPassphrase()
118         {
119                 return _passphrase;
120         }
121         public boolean promptPassphrase(String message)
122         {
123                 _passphrase = (String)dance("password", message); 
124                 return (_passphrase != null);
125         }
126         
127         public boolean promptYesNo(String str)
128         {
129                 return ((Boolean)dance("yesno", str)).booleanValue();
130         }
131         
132         public void showMessage(String str)
133         {
134                 dance("message", str);
135         }
136         
137         public String[] promptKeyboardInteractive(String dest, String name, String instr, String[] prompt, boolean[] echo)
138         {
139                 int i;
140                 String [] responses = new String[prompt.length];
141                 
142                 Log.e("Dumload.Uploader", "dest: "+dest);
143                 Log.e("Dumload.Uploader", "name: "+name);
144                 Log.e("Dumload.Uploader", "instr: "+instr);
145                 for (i = 0; i < prompt.length; i++)
146                 {
147                         responses[i] = (String) dance("password", "[" + dest + "]\n" + prompt[i]);
148                 }
149                 return responses;
150         }
151         
152         @Override
153         public void run()
154         {
155                 Looper.prepare();
156                 
157                 Log.e("Dumload.Uploader[thread]", "This brought to you from the new thread.");
158                 
159                 try {
160                         JSch jsch = new JSch();
161                         jsch.setKnownHosts(homedir + "/known_hosts");
162                         Session s = jsch.getSession("joshua", "nyus.joshuawise.com", 22);
163                         s.setUserInfo(this);
164                         s.connect();
165                         
166                         Channel channel = s.openChannel("exec");
167                         ((ChannelExec)channel).setCommand("echo foo > /tmp/lol");
168                         channel.connect();
169                         
170                         dance("message", "done");
171
172                         channel.disconnect();
173                         s.disconnect();
174                 } catch (JSchException e) {
175                         Log.e("Dumload.uploader[thread]", "JSchException: "+(e.toString()));
176                 }
177                 
178                 
179                 Log.e("Dumload.uploader[thread]", "And now I'm back to life!");
180         }
181         
182         private void say(String s) {
183                 Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
184         }
185         
186         @Override
187         public void onStart(Intent i, int startId)
188         {
189                 uri = i.getData();
190                 homedir = getApplicationContext().getFilesDir().getAbsolutePath();
191                 int shits = 0;
192                 
193                 super.onStart(i, startId);
194                 
195                 Log.e("Dumload.Uploader", "Started.");
196                 Log.e("Dumload.Uploader", "My path is "+homedir);
197                 
198                 try {
199                         InputStream is = getContentResolver().openInputStream(uri);
200                         shits = is.available();
201                 } catch (Exception e) {
202                 }
203                 
204                 say("Your shit was "+(Integer.toString(shits))+" bytes long");
205                 
206                 me = new Thread(this, "Uploader thread");
207                 me.start();
208         }
209         
210         @Override
211         public IBinder onBind(Intent i) {
212                 Log.e("Dumload.Uploader", "bound");
213                 
214                 return null;
215         }
216 }
This page took 0.023005 seconds and 2 git commands to generate.