Update of /cvsroot/mrpostman/mrpostman/src/org/mrbook/mrpostman/hotmail
In directory sc8-pr-cvs1:/tmp/cvs-serv22547
Modified Files:
HotmailMailSession.java
Log Message:
Added old-style Nav support. Fixed circular bug
Index: HotmailMailSession.java
===================================================================
RCS file: /cvsroot/mrpostman/mrpostman/src/org/mrbook/mrpostman/hotmail/HotmailMailSession.java,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -d -r1.32 -r1.33
*** HotmailMailSession.java 4 Dec 2003 00:43:26 -0000 1.32
--- HotmailMailSession.java 6 Dec 2003 11:50:37 -0000 1.33
***************
*** 42,46 ****
/*
! Hotmail module (v0.9) for MyPostman.
Supports retrieval of unread Inbox messages complete with attachements.
--- 42,46 ----
/*
! Hotmail module (v0.10) for MyPostman.
Supports retrieval of unread Inbox messages complete with attachements.
***************
*** 91,95 ****
new ModuleOption("hotmail.onlyunread", "true"), new ModuleOption("hotmail.markasread", "true")
};
! private static final ModuleInfo MODULE_INFO = new ModuleInfo("hotmail", AUTHORS, "0.9",
"http://mrpostman.sourceforge.net/updates/hotmail", "/en/hotmail/index.html", OPTIONS);
private static Logger logger = Logger.getLogger("org.mrbook.mrpostman.hotmail.HotmailMailSession");
--- 91,95 ----
new ModuleOption("hotmail.onlyunread", "true"), new ModuleOption("hotmail.markasread", "true")
};
! private static final ModuleInfo MODULE_INFO = new ModuleInfo("hotmail", AUTHORS, "0.10",
"http://mrpostman.sourceforge.net/updates/hotmail", "/en/hotmail/index.html", OPTIONS);
private static Logger logger = Logger.getLogger("org.mrbook.mrpostman.hotmail.HotmailMailSession");
***************
*** 99,108 ****
/**
! * Locales to work with account in other languages but English.
*/
! public static final java.util.HashMap local = new java.util.HashMap();
!
static {
! /* Old language specific new message count regex...
local.put("EN", new String[] {"Inbox", "(\\d+)\\s\\((\\d+).*new\\)"});
local.put("FR", new String[] {"Boîte de réception", "(\\d+)\\s\\((\\d+).*nouveaux\\)"});
--- 99,109 ----
/**
! * Hashmap to store locale dependant details. Used to support the various languages that Hotmail may
! * use for its UI.
*/
! public static final java.util.HashMap locale = new java.util.HashMap();
!
static {
! /* Old language specific new message count regex. Not needed anymore - CH
local.put("EN", new String[] {"Inbox", "(\\d+)\\s\\((\\d+).*new\\)"});
local.put("FR", new String[] {"Boîte de réception", "(\\d+)\\s\\((\\d+).*nouveaux\\)"});
***************
*** 112,134 ****
local.put("JA",
new String[] {"\u53d7\u4fe1\u30c8\u30ec\u30a4", "(\\d+)\\s+\\((\\d+).*\u901a\u304c\u65b0\u7740\\)"});
!
*/
- // New as of 7 May 03
- local.put("EN", new String[] {"Inbox", "(\\d+)\\s<b>\\((\\d+)\\)"});
- local.put("FR", new String[] {"Boîte de réception", "(\\d+)\\s<b>\\((\\d+)\\)"});
- local.put("ES", new String[] {"Bandeja de entrada", "(\\d+)\\s<b>\\((\\d+)\\)"});
- local.put("IT", new String[] {"Posta in arrivo", "(\\d+).*<b>\\((\\d+)\\)"});
- local.put("DE", new String[] {"Posteingang", "(\\d+)\\s<b>\\((\\d+)\\)"});
- local.put("JA", new String[] {"\u53d7\u4fe1\u30c8\u30ec\u30a4", "(\\d+)\\s<b>\\((\\d+)\\)"});
- local.put("NL", new String[] {"Postvak IN", "(\\d+)\\s<b>\\((\\d+)\\)"});
! // New as of 2 Dec 03
! local.put("EN", new String[] {"Mail", "New mail from my contacts:"});
! local.put("FR", new String[] {"Courrier", "Nouveaux messages de mes contacts :"});
! local.put("ES", new String[] {"Correo", "Correo nuevo de mis contactos:"});
! local.put("IT", new String[] {"Posta", "Nuovi messaggi provenienti dai tuoi contatti:"});
! local.put("DE", new String[] {"Posteingang", "Neue E-Mails von meinen Kontakten:"});
! local.put("JA", new String[] {"\u30e1\u30fc\u30eb", "\u77e5\u4eba\u304b\u3089\u306e\u65b0\u7740\u30e1\u30fc\u30eb :"});
! local.put("NL", new String[] {"Post", "Nieuwe e-mail van mijn contactpersonen:"});
// **TODO** add other languages here!
}
--- 113,139 ----
local.put("JA",
new String[] {"\u53d7\u4fe1\u30c8\u30ec\u30a4", "(\\d+)\\s+\\((\\d+).*\u901a\u304c\u65b0\u7740\\)"});
!
! // Original Hotmail Navigation (as of 7 May 03) - CH
! //originalLocale.put("EN", new String[] {"Inbox", "(\\d+)\\s<b>\\((\\d+)\\)"});
! //originalLocale.put("FR", new String[] {"Boîte de réception", "(\\d+)\\s<b>\\((\\d+)\\)"});
! //originalLocale.put("ES", new String[] {"Bandeja de entrada", "(\\d+)\\s<b>\\((\\d+)\\)"});
! //originalLocale.put("IT", new String[] {"Posta in arrivo", "(\\d+).*<b>\\((\\d+)\\)"});
! //originalLocale.put("DE", new String[] {"Posteingang", "(\\d+)\\s<b>\\((\\d+)\\)"});
! //originalLocale.put("JA", new String[] {"\u53d7\u4fe1\u30c8\u30ec\u30a4", "(\\d+)\\s<b>\\((\\d+)\\)"});
! //originalLocale.put("NL", new String[] {"Postvak IN", "(\\d+)\\s<b>\\((\\d+)\\)"});
*/
! // New as of 2 Dec 03
! // Each entry represents:
! // The new style tab string for the 'Mail' (inbox) link
! // The new style string indicating how many new messages are in the inbox.
! // The old style tab string for the 'Inbox' link. This is still accessible if the user chooses the 'Hotmail Navigation' option in the options screen.
! locale.put("EN", new String[] {"Mail", "New mail from my contacts:", "Inbox"});
! locale.put("FR", new String[] {"Courrier", "Nouveaux messages de mes contacts :", "Boîte de réception"});
! locale.put("ES", new String[] {"Correo", "Correo nuevo de mis contactos:", "Bandeja de entrada"});
! locale.put("IT", new String[] {"Posta", "Nuovi messaggi provenienti dai tuoi contatti:", "Posta in arrivo"});
! locale.put("DE", new String[] {"Posteingang", "Neue E-Mails von meinen Kontakten:", "Posteingang"});
! locale.put("JA", new String[] {"\u30e1\u30fc\u30eb", "\u77e5\u4eba\u304b\u3089\u306e\u65b0\u7740\u30e1\u30fc\u30eb :", "\u53d7\u4fe1\u30c8\u30ec\u30a4"});
! locale.put("NL", new String[] {"Post", "Nieuwe e-mail van mijn contactpersonen:", "Postvak IN"});
// **TODO** add other languages here!
}
***************
*** 140,145 ****
private String msgDeleteBaseUrl = null;
private CookieJar cj = null;
- private int numMessages = -1;
- private int numNewMessages = -1;
private boolean authenticated = false;
--- 145,148 ----
***************
*** 340,345 ****
hrh.setCookieJar(cj);
conn.setRequestProperty("User-Agent", UserAgent);
//conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
-
//FRIG - The cookie handling code doesn't handle this one well at all!
//conn.setRequestProperty("Cookie", "BrowserTest=Success?");
--- 343,349 ----
hrh.setCookieJar(cj);
conn.setRequestProperty("User-Agent", UserAgent);
+ // The following 'hacks' aren't needed, but are useful when debugging Hotmail's login process
+ //should they change it again. So I've left it in. - CH
//conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
//FRIG - The cookie handling code doesn't handle this one well at all!
//conn.setRequestProperty("Cookie", "BrowserTest=Success?");
***************
*** 355,359 ****
/*
! // Do manually...
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
--- 359,365 ----
/*
! // The following 'hacks' aren't needed, but are useful when debugging Hotmail's login process
! //should they change it again. So I've left it in. - CH
! // Manually read page (in case there is a problem with our cookie code)...
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
***************
*** 396,399 ****
--- 402,407 ----
}
+ // The following 'hacks' aren't needed, but are useful when debugging Hotmail's login process
+ //should they change it again. So I've left it in. - CH
//frig - clear the cookie jar here!
//cj.clear();
***************
*** 433,445 ****
// user/pass problem instead of actually doing the post.
Pattern signInFailure = Pattern.compile("uilogin.srf");
! Matcher inboxMatcher = null;
! Matcher langMatcher = null;
! Pattern langPattern = Pattern.compile("<link rel=\"stylesheet\" href=\"/cgi-bin/dasp/([A-Z][A-Z])");
! Pattern inboxUrlPattern = null;
! Pattern messageCountPattern = null;
! Matcher msgCountMatcher = null;
! String lang = null;
! numMessages = -1;
! numNewMessages = -1;
//verify page does not contain password error text. For debugging we'll retrieve the msg summary info too...
--- 441,448 ----
// user/pass problem instead of actually doing the post.
Pattern signInFailure = Pattern.compile("uilogin.srf");
! LanguageSettings languageSettings = new LanguageSettings();
!
! // Set the pattern used to locate the language setting for this page...
! languageSettings.langPattern = Pattern.compile("<link rel=\"stylesheet\" href=\"/cgi-bin/dasp/([A-Z][A-Z])");
//verify page does not contain password error text. For debugging we'll retrieve the msg summary info too...
***************
*** 452,532 ****
}
! if (lang == null) {
! langMatcher = langPattern.matcher(line);
!
! if (langMatcher.find()) {
! lang = langMatcher.group(1);
! logger.info(" Language found for this page: " + lang);
!
! try {
! String[] arr = (String[]) local.get(lang);
! // CH - New login structure 2/12/03
! //inboxUrlPattern = Pattern.compile("<a href=\"(\\S+)\"><font class=\".*\">" + arr[0]
! // + "\\s*:\\s" + arr[1] + "</b>");
! inboxUrlPattern = Pattern.compile("<a href=\"(\\S+)\" tabindex=121 class=\"E\">" + arr[0] + "</a>");
! logger.fine(" Using inboxUrlPattern :" + inboxUrlPattern.pattern());
! //messageCountPattern = Pattern.compile("<td.*><font.*>" + arr[1] + "</font></td>");
! messageCountPattern = Pattern.compile("<td.*><font.*>" + arr[1] + "</font><td.*><font.*> (\\d+)</font></td>");
! logger.fine("Using message count pattern: " + messageCountPattern);
! //logger.fine(" Using inboxUrlPattern :" + messageCountPattern.pattern());
! } catch (Exception e) {
! logger.log(Level.SEVERE, "Language determination error ", e);
! }
! }
! continue;
! }
! inboxMatcher = inboxUrlPattern.matcher(line);
!
! if (inboxMatcher.find()) {
! inboxUrlStr = inboxMatcher.group(1);
! logger.info("Found Inbox url: " + inboxUrlStr);
! //changed again - 2/12/03 - CH
! // new structure has this on the same line:
! //numMessages = Integer.parseInt(inboxMatcher.group(2));
! //numNewMessages = Integer.parseInt(inboxMatcher.group(3));
! //logger.info("Number of messages in inbox: " + numMessages);
! //logger.info("Number new: " + numNewMessages);
! }
! //changed again - 2/12/03 - CH
! msgCountMatcher = messageCountPattern.matcher(line);
! /* //changed as of 7 May 03
! //The msg count should be on the next line...
! if ((line = rd.readLine()) != null) {
! logger.finest("html: " + line);
! msgCountMatcher = messageCountPattern.matcher(line);
! */
! if (msgCountMatcher.find()) {
! logger.info("Line containing the number of new messages found!");
! //numMessages = Integer.parseInt(msgCountMatcher.group(1));
! numNewMessages = Integer.parseInt(msgCountMatcher.group(1));
! //logger.info("Number of messages in inbox: " + numMessages);
! logger.info("Number new: " + numNewMessages);
!
! }
!
! }
! if (lang == null) {
! logger.severe("The language hasn't been found!!!");
}
if (inboxUrlStr == null) {
! logger.severe("The inbox url hasn't been found!!!");
}
rd.close();
baseUrl = getBaseURL(conn.getURL());
logger.fine("url: " + baseUrl.toString());
} catch (Exception e) {
! logger.log(Level.SEVERE, "should not happen", e);
}
if (inboxUrlStr != null) {
! logger.info("Inbox URL: " + inboxUrlStr);
!
! //verify we were able to determine the correct number of messages...
! //if (numMessages > -1) {
! authenticated = true;
! return WebMailSession.WMS_LOGIN_OK;
! //}
}
--- 455,487 ----
}
! //If we haven't found the language for this page yet, check for it now...
! if (!languageSettings.isLanguageSet()) {
! determineLanguageSettings(languageSettings, line);
! } else {
! findInboxURL(languageSettings, line);
! }
! } // end while
! if (!languageSettings.isLanguageSet()) {
! logger.severe("The language hasn't been found - Hotmail changed WebSite structure or using an unsupported language setting");
}
if (inboxUrlStr == null) {
! logger.severe("The inbox url hasn't been found - Hotmail changed WebSite structure?");
}
rd.close();
baseUrl = getBaseURL(conn.getURL());
logger.fine("url: " + baseUrl.toString());
+
+ } catch (LanguageNotSupportedException le) {
+ logger.log(Level.SEVERE, le.getMessage());
} catch (Exception e) {
! logger.log(Level.SEVERE, "Unexpected Exception thrown whilst processing language and inbox page.", e);
}
if (inboxUrlStr != null) {
! logger.info("Inbox URL: " + inboxUrlStr);
! authenticated = true;
! return WebMailSession.WMS_LOGIN_OK;
}
***************
*** 536,539 ****
--- 491,566 ----
/**
+ * Determine the language used and calculate language dependant variables.
+ * Broke this out from login() to improve clarity
+ */
+ private void determineLanguageSettings(LanguageSettings langSettings, String line) throws LanguageNotSupportedException {
+ String[] langStrs = null;
+ //Check if language is set on this line...
+ Matcher langMatcher = langSettings.langPattern.matcher(line);
+
+ if (langMatcher.find()) {
+ langSettings.lang = langMatcher.group(1);
+ logger.info("Language found for this page: " + langSettings.lang);
+
+ //We have found the language, use this to construct the language dependant values...
+ try {
+ //Construct the regex's for the old and new style inbox links...
+ langStrs = (String[]) locale.get(langSettings.lang);
+ langSettings.newInboxUrlPatternStr = "<a href=\"(\\S+)\" tabindex=121 class=\"E\">" + langStrs[0] + "</a>";
+ logger.fine("Using 'new' inboxUrlPattern :" + langSettings.newInboxUrlPatternStr);
+ langSettings.originalInboxUrlPatternStr = "<a href=\"(\\S+)\" tabindex=121 class=\"E\">" + langStrs[2] + "</a>";
+ logger.fine("Using 'Original Hotmail Navigation' inboxUrlPattern :" + langSettings.originalInboxUrlPatternStr);
+ //The message count link will always be new style..
+ langSettings.newMessageCountPatternStr = "<td.*><font.*>" + langStrs[1] + "</font><td.*><font.*> (\\d+)</font></td>";
+ logger.fine("Using 'new' message count pattern: " + langSettings.newMessageCountPatternStr);
+ } catch (Exception e) {
+ String msg = "Language error, language '" + langSettings.lang + "' not supported";
+ throw new LanguageNotSupportedException(msg);
+ }
+ }
+ }
+
+ /**
+ * Helper function to test for the Inbox URL based on language settings.
+ * Supports new and original navigation styles
+ * Updates class attribute inboxUrlStr
+ */
+ private void findInboxURL(LanguageSettings langSettings, String line) {
+ Matcher inboxMatcher, msgCountMatcher = null;
+
+ //Attempt to find 'original' inbox URL...
+ if (langSettings.originalInboxUrlPattern == null) {
+ langSettings.originalInboxUrlPattern = Pattern.compile(langSettings.originalInboxUrlPatternStr);
+ }
+ inboxMatcher = langSettings.originalInboxUrlPattern.matcher(line);
+
+ if (inboxMatcher.find()) {
+ inboxUrlStr = inboxMatcher.group(1);
+ logger.info("Found 'Original' Inbox url: " + inboxUrlStr);
+ }
+
+ //Attempt to find 'new' inbox URL...
+ if (langSettings.newInboxUrlPattern == null) {
+ langSettings.newInboxUrlPattern = Pattern.compile(langSettings.newInboxUrlPatternStr);
+ }
+ inboxMatcher = langSettings.newInboxUrlPattern.matcher(line);
+ if (inboxMatcher.find()) {
+ inboxUrlStr = inboxMatcher.group(1);
+ logger.info("Found 'New' Inbox url: " + inboxUrlStr);
+ }
+
+ if(langSettings.newMessageCountPattern == null) {
+ langSettings.newMessageCountPattern = Pattern.compile(langSettings.newMessageCountPatternStr);
+ }
+
+ msgCountMatcher = langSettings.newMessageCountPattern.matcher(line);
+ if (msgCountMatcher.find()) {
+ logger.info("Line containing the number of new messages found!");
+ int numNewMessages = Integer.parseInt(msgCountMatcher.group(1));
+ logger.info("Number new: " + numNewMessages);
+ }
+ }
+
+ /**
* Strip the base context url from the request url for use in later gets
*/
***************
*** 602,607 ****
}
! // Fixed bug: the .\d* part is optional for MSN system messages
! Pattern messagePattern = Pattern.compile("<a href=\"javascript:G\\('(\\S+)msg=MSG(\\d+(\\.\\d*)?)[\\&|\"]");
Pattern sizePattern = Pattern.compile("\\&start=\\d+\\&len=(\\d+)");
--- 629,634 ----
}
! // Fixed bug: the .\d* part is optional for MSN system messages
! Pattern messagePattern = Pattern.compile("<a href=\"javascript:G\\('(\\S+)msg=MSG(\\d+(\\.\\d*)?)[\\&|']");
Pattern sizePattern = Pattern.compile("\\&start=\\d+\\&len=(\\d+)");
***************
*** 856,902 ****
}
! /**
! * Return hotmail's UIDL for this message. We can get this by parsing the msg header
! * returned from TOP.
! * This is only valid for 'real' emails i.e. not Hotmail circulars
! * We'll have to calculate our our UIDL for the circular messages :(
! */
! /*
! // Removed this pending further investigations, Message-IDs do not always
! //appear and are not always unique. Replaced with the MD5 function below. - CH 28/7/03
! public String getUniqueMessageId(int num) throws MailSessionException {
! try {
! ByteArrayOutputStream bos = new ByteArrayOutputStream();
! PrintWriter pw = new PrintWriter(bos, true);
! retrieveMessage(num, pw, 1);
! bos.flush();
! String header = bos.toString();
! Pattern uidlPattern = Pattern.compile("Message-ID:\\s+<(\\S+)>");
! Matcher matcher = uidlPattern.matcher(header);
!
! if (matcher.find()) {
! return matcher.group(1);
! } else {
! //The message did not have an UIDL header field - must be a hotmail circular
! //we'll create our own hash from the header contents!
! logger.finest("Message with on UIDL, is it a hotmail circular? - returning a hashcode of header: "
! + header);
! String encStr = org.mrbook.mrpostman.another.Base64.encodeString(header);
! logger.finest("Encoded header: " + encStr);
!
! if (encStr.length() > 70) {
! //we'll have to truncate it - lets hope this is really good enough!
! logger.fine("Truncating generated UIDL hashcode (this could cause it to become non-unique)");
! return encStr.substring(0, 69);
! }
! return Integer.toString(header.hashCode());
! }
! } catch (IOException i) {
! logger.log(Level.SEVERE, "should not happen", i);
! throw new MailSessionException("IOException whilst attempting to read UIDL for message" + num);
! }
! }
! */
!
/**
* Return hotmail's UIDL for this message. We can get this by generating the MD5 hash of the message
--- 883,887 ----
}
!
/**
* Return hotmail's UIDL for this message. We can get this by generating the MD5 hash of the message
***************
*** 986,989 ****
--- 971,999 ----
public String toString() {
return new String("MailID=" + MailID + ", unread=" + unread + " flagged=" + flagged + " size=" + size);
+ }
+ }
+
+ /**
+ * Helper class to store dependant variable values
+ */
+ class LanguageSettings {
+ //These Pattern objects exist for efficiency so we don't have to re-calculate every input line...
+ public Pattern langPattern = null;
+ public Pattern originalInboxUrlPattern = null;
+ public Pattern newInboxUrlPattern = null;
+ public Pattern newMessageCountPattern = null;
+
+ //Language dependant attributes...
+ public String lang = null;
+ public String originalInboxUrlPatternStr = null;
+ public String newInboxUrlPatternStr = null;
+ public String newMessageCountPatternStr = null;
+
+ public boolean isLanguageSet() { return lang != null; }
+ }
+
+ class LanguageNotSupportedException extends Exception {
+ public LanguageNotSupportedException(String msg) {
+ super(msg);
}
}
|