From: adam Date: Sun, 5 Sep 2004 21:34:03 +0000 (+0000) Subject: updates to gmail X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=0ad6417b12ec057688baa5a65094a83ea752385b;p=org.ibex.mail.git updates to gmail darcs-hash:20040905213403-5007d-47554737c1d85913293c110d85f7fddaae382835.gz --- diff --git a/src/org/ibex/mail/protocol/GMail.java b/src/org/ibex/mail/protocol/GMail.java index a476ed6..2753f30 100644 --- a/src/org/ibex/mail/protocol/GMail.java +++ b/src/org/ibex/mail/protocol/GMail.java @@ -1,6 +1,7 @@ package org.ibex.mail.protocol; import org.ibex.io.*; import org.ibex.crypto.*; +import org.ibex.mail.protocol.*; import org.ibex.jinetd.Listener; import org.ibex.jinetd.Worker; import org.ibex.mail.*; @@ -15,17 +16,27 @@ import java.io.*; public class GMail extends Account { - public static final String login = "https://www.google.com/accounts/ServiceLoginBoxAuth"; - public static final String gmail = "https://gmail.google.com/gmail"; - + private GMailIMAP imap = new GMailIMAP(); private HTTP.Cookie.Jar jar = new HTTP.Cookie.Jar(); - private String captchaString = null; - private String ctoken = null; - private String email = null; - private String password = null; + private String captcha = null; + private String ctoken = null; + private String email = null; + private String password = null; + private boolean invalid = false; + private String[] labels = new String[0]; + private String[] queries = new String[0]; + private Summary[] summaries = new Summary[0]; + // Constructor, Pooling /////////////////////////////////////////////////////////////////////////// - // Constructor ////////////////////////////////////////////////////////////////////////////// + public GMail(String email, String pass) throws IOException { + super(email.substring(0, email.indexOf('@')), Address.parse(email)); + this.email = email; this.password = pass; + Log.warn(GMail.class, "logging in " + email); + getCookies(); + } + + public void close() { cache.remove(email, password); invalid = true; } private static Hash cache = new Hash(); static { HTTP.userAgent = "Mozilla/5.0 (compatible;)"; } @@ -33,7 +44,6 @@ public class GMail extends Account { try { GMail g = (GMail)cache.get(email, pass); if (g == null) cache.put(email, pass, g = new GMail(email, pass)); - g.init(); return g; } catch (Exception e) { Log.error(GMail.class, e); @@ -41,96 +51,134 @@ public class GMail extends Account { } } - public GMail(String email, String pass) throws IOException { - super(email.substring(0, email.indexOf('@')), Address.parse(email)); - this.email = email; this.password = pass; - Log.warn(GMail.class, "logging in " + email); - } + // IMAP Interface ////////////////////////////////////////////////////////////////////////////// + + public IMAP.Server getIMAP() { return imap; } + private class GMailIMAP implements IMAP.Server { - void init() throws IOException { - if (success) return; - Vector v = makeRequest(); - Message[] m = new Message[v.size()]; - v.copyInto(m); - final Mailbox inbox = new MessageArrayMailbox(m); - this.root = - new Mailbox.Default() { - public void add(Message m) { throw new RuntimeException("not supported"); } - public void add(Message m, int i) { throw new RuntimeException("not supported"); } - public Mailbox.Iterator iterator() { return new Mailbox.Iterator.NullIterator(); } - public int uidNext() { return 500; } - public String[] children() { - if (success) return new String[] { "INBOX" }; - return new String[] { "Captcha" }; + private String query = "?search=inbox&start=0&view=tl"; + private int validity = new Random().nextInt(); + + private IMAP.Client client; + public void setClient(IMAP.Client client) { this.client = client; } + + public String[] capability() { return new String[] { }; } + public Hashtable id(Hashtable clientId) { return null; } + public void logout() { GMail.this.close(); } + public void subscribe(String mailbox) { } + public void unsubscribe(String mailbox) { } + + public void unselect() { summaries = new Summary[0]; } + public void close() { summaries = new Summary[0]; } + + public void noop() { check(); } + public void expunge() { } + + public void setFlags(Query q, int flags, boolean uid, boolean silent) { } + public void removeFlags(Query q, int flags, boolean uid, boolean silent) { } + public void addFlags(Query q, int flags, boolean uid, boolean silent) { } + + public void rename(String from, String to) { } + public void delete(String m) { } + public void create(String m) { + String[] newqueries = new String[queries.length+1]; + System.arraycopy(queries, 0, newqueries, 0, queries.length); + newqueries[queries.length] = m; + queries = newqueries; + } + + public void append(String m, int flags, Date arrival, String body) { } + public void copy(Query q, String to) { } + + public void check() { query(query); } + public void select(String mailbox, boolean examineOnly) { + for(int i=0; i\r\n" + - "Hi there. Google is lame; please type in the word you see below and " + - "click submit. You might have to click 'get mail' again after that.
" + - "\r\n" + - "
\r\n"+ - " \r\n"+ - " \r\n"+ - " \r\n"+ - " \r\n"+ - " \r\n"+ - "
\r\n"+ - "\r\n"), - null); - messages.addElement(m); - return messages; - } - } catch (Exception e) { - e.printStackTrace(); - return messages; - } + if (result.indexOf("top.location") == -1) { doCaptcha(result); return; } result = result.substring(result.indexOf("top.location")); result = result.substring(result.indexOf("CheckCookie?continue=") + "CheckCookie?continue=".length()); result = result.substring(0, result.indexOf('\"')); @@ -138,75 +186,115 @@ public class GMail extends Account { Log.warn("[]", "getting " + result); // just need the cookie off of this page - String s = new String(InputStreamToByteArray.convert(new HTTP(result).GET(null, jar))); - Log.warn("[]", s); + InputStreamToByteArray.convert(new HTTP(result).GET(null, jar)); + imap.check(); + Log.warn("[]", "done"); + } - final Semaphore sem = new Semaphore(); + Message captchaMessage = null; + public void doCaptcha(String result) throws IOException { + Log.warn(GMail.class,"no relocator found; checking for captcha"); + ctoken = result.substring(result.indexOf("id=\"ctoken\" value=\"") + "id=\"ctoken\" value=\"".length()); + ctoken = ctoken.substring(0, ctoken.indexOf("\"")); + String image = result.substring(result.indexOf("Captcha?")); + image = image.substring(0, image.indexOf("\"")); + String str = + "From: google@google.com\r\n" + + "To: you@yourself.com\r\n" + + "Subject: Captcha\r\n" + + "Date: Mon Aug 30 19:05:40 PDT 2004\r\n" + + "Content-Type: text/html\r\n" + + "\r\n" + + "\r\n" + + "Hi there. Google is lame; please type in the word you see below and " + + "click submit. You might have to click 'get mail' again after that.
" + + "\r\n" + + "
\r\n"+ + " \r\n"+ + " \r\n"+ + " \r\n"+ + " \r\n"+ + " \r\n"+ + "
\r\n"+ + "\r\n"; try { - JSArray ret = grab(gmail + "?search=inbox&start=0&view=tl", jar); + captchaMessage = new Message(new Stream(str), null); + } catch (Message.Malformed e) { + Log.warn(this, e); + throw new IOException(e.toString()); + } + } + + public synchronized Summary[] query(String query) { + if (captcha != null) return new Summary[0]; + try { + Log.info(GMail.class, "query: " + query); + JSArray ret = http(gmail + query, jar); + Hashtable h = new Hashtable(); for(int i=0; i') + 1); - name = name.substring(0, name.indexOf("")); - String subject = (String)m.get(6); - //Log.warn("[]", name + " <"+email+"> " + subject); - //Log.warn("[]", ); - } - } else if (j2.elementAt(0).equals("mi")) { - JSArray m = j2; - String date = m.get(9).toString(); - String to = m.get(8).toString(); - String toemail = m.get(10).toString(); - String from = m.get(4).toString(); - String name = m.get(6).toString(); - String email = m.get(7).toString(); - String subject = m.get(15).toString(); - Log.warn("[]", name + " <"+email+"> " + subject); - Stream thestream = new Stream(new HTTP(gmail+"?search=query&start=0&view=om&th=" + - URLEncoder.encode(threadid)).GET(null, jar)); - thestream.readln(); - messages.addElement(new Message(thestream, null)); - } - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - sem.release(); - } - } }.start(); + if (j.elementAt(0).equals("t")) { + for(int k=1; k"); + this.from = Address.parse(m.elementAt(6).toString()+"<"+m.elementAt(7).toString()+">"); + this.subject = m.elementAt(15).toString(); + } + } - public JSArray grab(String url, HTTP.Cookie.Jar jar) throws JSExn, IOException { + public JSArray http(String url, HTTP.Cookie.Jar jar) throws JSExn, IOException { Stream stream = new Stream(new HTTP(url).GET(null, jar)); boolean inscript = false; StringBuffer buf = new StringBuffer("var ret = []; var D = function(x){ret.push(x);};"); @@ -223,8 +311,6 @@ public class GMail extends Account { } } - public Mailbox getMailbox(Class protocol) { return this.root; } - // HTTP Listener for Captcha requests ////////////////////////////////////////////////////////////////////////////// @@ -245,7 +331,10 @@ public class GMail extends Account { URLDecoder.decode(tok.substring(tok.indexOf('=')+1))); } ((GMail)cache.get((String)h.get("email"), (String)h.get("pass"))).setCaptcha(h); - conn.println("HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n\r\n"); + conn.println("HTTP/1.0 200 OK\r\n"); + conn.println("Content-Type: text/plain\r\n"); + conn.println("\r\n"); + conn.println("\r\n"); } else { conn.println("HTTP/1.0 500 Error\r\n\r\n"); } @@ -254,16 +343,21 @@ public class GMail extends Account { } public void setCaptcha(Hash h) { - captchaString = (String)h.get("captcha"); + captcha = (String)h.get("captcha"); ctoken = (String)h.get("ctoken"); - Log.warn(GMail.class, "captchaString = " + captchaString); + Log.warn(GMail.class, "captcha = " + captcha); Log.warn(GMail.class, "ctoken = " + ctoken); Log.warn(GMail.class, "initting..." + ctoken); try { - init(); + getCookies(); Log.warn(GMail.class, " done..." + ctoken); } catch (Exception e) { Log.error(this, e); } } + + // Constants ////////////////////////////////////////////////////////////////////////////// + + public static final String login = "https://www.google.com/accounts/ServiceLoginBoxAuth"; + public static final String gmail = "https://gmail.google.com/gmail"; }