change fetch() to fetchResponse() in GMail.java
[org.ibex.mail.git] / src / org / ibex / mail / GMail.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import org.ibex.crypto.*;
7 import org.ibex.mail.protocol.*;
8 import org.ibex.util.*;
9 import org.ibex.net.*;
10 import org.ibex.js.*;
11 import org.ibex.mail.target.*;
12 import java.util.*;
13 import java.net.*;
14 import java.text.*;
15 import java.io.*;
16 import org.ibex.io.*;
17 import org.ibex.io.Fountain;
18
19 public class GMail extends Account {
20
21     private      GMailIMAP imap = new GMailIMAP();
22     private HTTP.Cookie.Jar jar = new HTTP.Cookie.Jar();
23     private      String captcha = null;
24     private       String ctoken = null;
25     private        String email = null;
26     private     String password = null;
27     private     boolean invalid = false;
28     private     String[] labels = new String[0];
29     private    String[] queries = new String[0];
30     private Summary[] summaries = new Summary[0];
31
32     // Constructor, Pooling  ///////////////////////////////////////////////////////////////////////////
33
34     public GMail(String email, String pass) throws IOException {
35         super(email.substring(0, email.indexOf('@')), Address.parse(email));
36         this.email = email; this.password = pass;
37         Log.warn(GMail.class, "logging in " + email);
38         getCookies();
39     }
40
41     public void close() { cache.remove(email, password); invalid = true; }
42
43     private static Hash cache = new Hash();
44     static { HTTP.userAgent = "Mozilla/5.0 (compatible;)"; }
45     public static GMail getGMail(String email, String pass) {
46         try {
47             GMail g = (GMail)cache.get(email, pass);
48             if (g == null) cache.put(email, pass, g = new GMail(email, pass));
49             return g;
50         } catch (Exception e) {
51             Log.error(GMail.class, e);
52             return null;
53         }
54     }
55
56     // IMAP Interface //////////////////////////////////////////////////////////////////////////////
57
58     public IMAP.Server getIMAP() { return imap; }
59     private class GMailIMAP implements IMAP.Server {
60
61         private String query = "?search=inbox&start=0&view=tl";
62         private int validity = new Random().nextInt();
63
64         private IMAP.Client client;
65         public void setClient(IMAP.Client client) { this.client = client; }
66
67         public String[]  capability() { return new String[] { }; }
68         public Hashtable id(Hashtable clientId) { return null; }
69         public void      logout() { GMail.this.close(); }
70         public void      subscribe(String mailbox) { }
71         public void      unsubscribe(String mailbox) { }
72
73         public void      unselect() { summaries = new Summary[0]; }
74         public void      close() { summaries = new Summary[0]; }
75
76         public void      noop() { check(); }
77         public void      expunge() { }
78
79         public void      setFlags(Query q, int flags, boolean uid, boolean silent) { }
80         public void      removeFlags(Query q, int flags, boolean uid, boolean silent) { }
81         public void      addFlags(Query q, int flags, boolean uid, boolean silent) { }
82
83         public void      rename(String from, String to) { }
84         public void      delete(String m) { }
85         public void      create(String m) {
86             String[] newqueries = new String[queries.length+1];
87             System.arraycopy(queries, 0, newqueries, 0, queries.length);
88             newqueries[queries.length] = m;
89             queries = newqueries;
90         }
91
92         public void      append(String m, int flags, Date arrival, String body) { }
93         public void      copy(Query q, String to) { }
94
95         public void      check() { query(query); }
96         public void      select(String mailbox, boolean examineOnly) {
97             String oldquery = query;
98             if (mailbox.equalsIgnoreCase("inbox")) {
99                 query = "?search=inbox&start=0&view=tl";
100                 if (!query.equals(oldquery)) check();
101                 return;
102             }
103             for(int i=0; i<labels.length; i++) {
104                 if (labels[i].equals(mailbox)) {
105                     query = "?search=cat&cat="+mailbox+"&inbox&start=0&view=tl";
106                     if (!query.equals(oldquery)) check();
107                     return;
108                 }
109             }
110             query = "?search=query&q="+URLEncoder.encode(mailbox)+"&start=0&view=tl";
111             if (!query.equals(oldquery)) check();
112         }
113
114         public void      lsub(String start, String ref) { list(true); }
115         public void      list(String start, String ref) { list(false); }
116         private void list(boolean lsub) {
117             client.list('/', "inbox", lsub, false);
118             for(int i=0; i<labels.length; i++) client.list('/', labels[i], lsub, false);
119             for(int i=0; i<queries.length; i++) client.list('/', queries[i], lsub, false);
120         }
121
122         public int[]     search(Query q, boolean uid) { return null; }
123         public void      fetch(Query q, int spec, String[] headers, int start, int end, boolean uid) {
124             if (captchaMessage != null) { client.fetchResponse(1, 0, 100, captchaMessage, 100); return; }
125             for(int i=0; i<summaries.length; i++) try {
126                 final Message m = summaries[i].getMessage();
127                 final int num = i;
128                 if (q.match(new Mailbox.Default.Iterator() {
129                         public Message cur() { return m; }
130                         public Headers head() { return m.headers; }
131                         public boolean next() { return false; }
132                         public int     uid() { return num; }
133                         public int     imapNumber() { return num; }
134                         public int     nntpNumber() { throw new RuntimeException("not supported"); }
135                         public void    delete() { }
136                         public void    set(String key, String val) { }
137                         public String  get(String key) { return null; }
138                         public boolean seen() { return false; }
139                         public boolean deleted() { return false; }
140                         public boolean flagged() { return false; }
141                         public boolean draft() { return false;}
142                         public boolean answered() { return false; }
143                         public boolean recent() { return true; }
144                         public void    seen(boolean on) { }
145                         public void    deleted(boolean on) { }
146                         public void    flagged(boolean on) { }
147                         public void    draft(boolean on) { }
148                         public void    answered(boolean on) { }
149                         public void    recent(boolean on) { }
150                         public int     flags() { return 0; }
151                         public void    addFlags(int flags) { }
152                         public void    removeFlags(int flags) { }
153                         public void    setFlags(int flags) { }
154                     })) {
155                     Log.info(GMail.class, "fetch " + summaries[i].subject);
156                     throw new Error("broken");
157                     //client.fetch(i+1, 0, m.size(), m,/* summaries[i].getIntId()*/ i);
158                 }
159             } catch (Exception e) { Log.warn(this, e); }
160         }
161
162         public int       unseen(String mailbox)      { return summaries.length; }
163         public int       recent(String mailbox)      { return summaries.length; }
164         public int       count(String mailbox)       { return summaries.length; }
165         public int       count()       { return summaries.length; }
166         public int       maxuid()      { return summaries.length; }
167         public int       uidNext(String mailbox)     { return summaries.length+1; }
168         public int       uidValidity(String mailbox) { return validity; }
169     }
170
171     public void getCookies() throws IOException {
172         jar = new HTTP.Cookie.Jar();
173         String params =
174             "continue="     + URLEncoder.encode(gmail) +
175             "&service=mail" +
176             "&Email="       + URLEncoder.encode(user) +
177             "&Passwd="      + password + 
178             "&null=Sign+in" +
179             (captcha != null ? "&captcha="+URLEncoder.encode(captcha) : "") +
180             (captcha != null ? "&ctoken="+URLEncoder.encode(ctoken) : "");
181         Log.info("[request]", params);
182         HTTP http = captcha==null ? new HTTP(login+"?"+params) : new HTTP(login);
183         InputStream reply = captcha==null ?
184             http.GET(null, jar) :
185             http.POST("application/x-www-form-urlencoded", params, null, jar);
186         String result = new String(InputStreamToByteArray.convert(reply));
187         System.err.println(result);
188         captcha = null;
189         
190         if (result.indexOf("top.location") == -1) { doCaptcha(result); return; }
191         result = result.substring(result.indexOf("top.location"));
192         result = result.substring(result.indexOf("CheckCookie?continue=") + "CheckCookie?continue=".length());
193         result = result.substring(0, result.indexOf('\"'));
194         result = URLDecoder.decode(result);
195         Log.warn("[]", "getting " + result);
196
197         // just need the cookie off of this page
198         InputStreamToByteArray.convert(new HTTP(result).GET(null, jar));
199         imap.check();
200         Log.warn("[]", "done");
201     }
202
203     Message captchaMessage = null;
204     public void doCaptcha(String result) throws IOException {
205         /*
206         Log.warn(GMail.class,"no relocator found; checking for captcha");
207         ctoken = result.substring(result.indexOf("id=\"ctoken\" value=\"") + "id=\"ctoken\" value=\"".length());
208         ctoken = ctoken.substring(0, ctoken.indexOf("\""));
209         String image = result.substring(result.indexOf("Captcha?"));
210         image = image.substring(0, image.indexOf("\""));
211         String str =                                            
212             "From: google@google.com\r\n" +
213             "To: you@yourself.com\r\n" +
214             "Subject: Captcha\r\n" +
215             "Date: Mon Aug 30 19:05:40 PDT 2004\r\n" +
216             "Content-Type: text/html\r\n" +
217             "\r\n" +
218             "<html><body>\r\n" +
219             "Hi there.  Google is lame; please type in the word you see below and " +
220             "click submit.  You might have to click 'get mail' again after that.<br>  " +
221             "<img src=\"https://www.google.com/accounts/"+image+"\">\r\n" +
222             "<form method=get action=http://gmail.megacz.com:8099/Captcha>\r\n"+
223             "  <input type=text name=captcha>\r\n"+
224             "  <input type=hidden name=email value=\""+email+"\">\r\n"+
225             "  <input type=hidden name=pass value="+password+">\r\n"+
226             "  <input type=hidden name=ctoken value=\""+ctoken+"\">\r\n"+
227             "  <input type=submit>\r\n"+
228             "</form>\r\n"+
229             "</body></html>\r\n";
230         try {
231             captchaMessage = Message.newMessage(new Fountain.StringFountain(str));
232         } catch (Message.Malformed e) {
233             Log.warn(this, e);
234             throw new IOException(e.toString());
235         }
236         */
237     }
238
239     public synchronized Summary[] query(String query) { 
240         if (captcha != null) return new Summary[0];
241         try {
242             Log.info(GMail.class, "query: " + query);
243             JSArray ret = http(gmail + query, jar);
244             Hashtable h = new Hashtable();
245             for(int i=0; i<ret.size(); i++) {
246                 JSArray j = (JSArray)ret.get(i);
247                 if (j.get(0).equals("t")) {
248                     for(int k=1; k<j.size(); k++) getSummary((String)((JSArray)j.get(k)).get(0), h);
249                 } else if (j.get(0).equals("ct") && labels.length == 0) {
250                     Vec v = new Vec();
251                     j = (JSArray)j.get(1);
252                     for(int k=0; k<j.size(); k++) v.addElement(((JSArray)j.get(k)).get(0));
253                     v.copyInto(labels = new String[v.size()]);
254                 }
255             }
256             Enumeration e = h.keys();
257             Vec v = new Vec();
258             while(e.hasMoreElements()) v.addElement(h.get(e.nextElement()));
259             return summaries = (Summary[])v.copyInto(new Summary[v.size()]);
260         } catch (Exception e) {
261             Log.warn(this, e);
262             return new Summary[0];
263         }
264     }
265
266     public void getSummary(String id, Hashtable ret) {
267         try {
268             JSArray js2 = http(gmail + "?search=query&start=0&view=cv&q=in:anywhere&th=" + URLEncoder.encode(id), jar);
269             for(int i2=0; i2<js2.size(); i2++) {
270                 JSArray args = (JSArray)js2.get(i2);
271                 if (!args.get(0).equals("mi")) continue;
272                 Summary sum = new Summary(args);
273                 Log.info(GMail.class, "summary: " + sum.subject);
274                 ret.put(sum.id, sum);
275             }
276         } catch (Exception e) {
277             e.printStackTrace();
278         }
279     }
280
281     private class Summary {
282         public Address from;
283         public Address to;
284         public String  subject;
285         public Date    date;
286         public String  id;
287         public Message message = null;
288         
289         public int getIntId() { return Math.abs(Integer.parseInt(id.toLowerCase().substring(id.length()-7), 16)); }
290
291         public Message getMessage() throws Message.Malformed, IOException {
292             throw new Error("broken right now");
293             /*
294             if (message != null) return message;
295             Stream thestream =
296                 new Stream(new HTTP(gmail+"?search=query&start=0&view=om&th=" + URLEncoder.encode(id)).GET(null, jar));
297             thestream.readln();
298             return message = Message.newMessage(new Fountain.StringFountainthestream);
299             */
300         }
301         
302         public Summary(JSArray m) {
303             try { this.date = new Date(m.get(9).toString()); } catch (Exception e) { this.date = null; }
304             this.id = m.get(3).toString();
305             this.to = Address.parse(m.get(8).toString() + " <" + m.get(10).toString() + ">");
306             this.from = Address.parse(m.get(6).toString()+"<"+m.get(7).toString()+">");
307             this.subject = m.get(15).toString();
308         }
309     }
310
311     public JSArray http(String url, HTTP.Cookie.Jar jar) throws JSExn, IOException  {
312         Stream stream = new Stream(new HTTP(url).GET(null, jar));                    
313         boolean inscript = false;
314         StringBuffer buf = new StringBuffer("var ret = []; var D = function(x){ret.push(x);};");
315         String s = null;
316         while((s = stream.readln()) != null) {
317             if (s.indexOf("<script>") != -1)  { inscript = true; continue; }
318             if (s.indexOf("</script>") != -1) { inscript = false; continue; }
319             if (inscript) buf.append(s);
320         }
321         buf.append("return ret;");
322         synchronized(GMail.class) {
323             JS js = JSU.fromReader("google", 0, new StringReader(buf.toString()));
324             return (JSArray)js.call(null, JSU.emptyArgs);
325         }
326     }
327
328
329     // HTTP Listener for Captcha requests //////////////////////////////////////////////////////////////////////////////
330     
331     public static void handleRequest(Connection conn) {
332         String top = null;
333         for(String s = conn.readln(); s != null && s.length() > 0; s = conn.readln()) {
334             if (top == null) top = s;
335             Log.warn(GMail.class, s);
336         }
337         if (top.startsWith("GET /Captcha")) {
338             top = top.substring(top.indexOf('?')+1);
339             top = top.substring(0, top.indexOf(' '));
340             StringTokenizer st = new StringTokenizer(top, "&");
341             Hash h = new Hash();
342             while(st.hasMoreTokens()) {
343                 String tok = st.nextToken();
344                 h.put(URLDecoder.decode(tok.substring(0, tok.indexOf('='))),
345                       URLDecoder.decode(tok.substring(tok.indexOf('=')+1)));
346             }
347             ((GMail)cache.get((String)h.get("email"), (String)h.get("pass"))).setCaptcha(h);
348             conn.println("HTTP/1.0 200 OK\r\n");
349             conn.println("Content-Type: text/plain\r\n");
350             conn.println("\r\n");
351             conn.println("<html><body><script>window.close()</script></body></html>\r\n");
352         } else {
353             conn.println("HTTP/1.0 500 Error\r\n\r\n");
354         }
355         conn.flush();
356         conn.close();
357     }
358
359     public void setCaptcha(Hash h) {
360         captcha = (String)h.get("captcha");
361         ctoken = (String)h.get("ctoken");
362         Log.warn(GMail.class, "captcha = " + captcha);
363         Log.warn(GMail.class, "ctoken = " + ctoken);
364         Log.warn(GMail.class, "initting..." + ctoken);
365         try {
366             getCookies();
367             Log.warn(GMail.class, "  done..." + ctoken);
368         } catch (Exception e) {
369             Log.error(this, e);
370         }
371     }
372
373     // Constants //////////////////////////////////////////////////////////////////////////////
374
375     public static final String login = "https://www.google.com/accounts/ServiceLoginBoxAuth";
376     public static final String gmail = "https://gmail.google.com/gmail";
377 }