1
0
mirror of https://github.com/moparisthebest/davmail synced 2025-01-07 11:48:02 -05:00

Various message updates and some code cleanup

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@101 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2007-11-08 17:12:36 +00:00
parent 3344d90b97
commit 15bde1c236
2 changed files with 48 additions and 73 deletions

View File

@ -19,7 +19,7 @@ public class Settings {
public static synchronized void setConfigFilePath(String value) { public static synchronized void setConfigFilePath(String value) {
configFilePath = value; configFilePath = value;
} }
public static boolean isFirstStart() { public static boolean isFirstStart() {
return isFirstStart; return isFirstStart;
} }
@ -105,13 +105,7 @@ public class Settings {
} }
public static synchronized boolean getBooleanProperty(String property) { public static synchronized boolean getBooleanProperty(String property) {
boolean value = false; String propertyValue = SETTINGS.getProperty(property);
try { return "true".equals(propertyValue);
String propertyValue = SETTINGS.getProperty(property);
value = "true".equals(propertyValue);
} catch (NumberFormatException e) {
DavGatewayTray.error("Invalid setting value in " + property, e);
}
return value;
} }
} }

View File

@ -141,24 +141,30 @@ public class ExchangeSession {
// do not send basic auth automatically // do not send basic auth automatically
httpClient.getState().setAuthenticationPreemptive(false); httpClient.getState().setAuthenticationPreemptive(false);
String enableProxy = Settings.getProperty("davmail.enableProxy"); boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
String proxyHost = null; String proxyHost = null;
String proxyPort = null; int proxyPort = 0;
String proxyUser = null; String proxyUser = null;
String proxyPassword = null; String proxyPassword = null;
if ("true".equals(enableProxy)) { if (enableProxy) {
proxyHost = Settings.getProperty("davmail.proxyHost"); proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getProperty("davmail.proxyPort"); proxyPort = Settings.getIntProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser"); proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPassword = Settings.getProperty("davmail.proxyPassword"); proxyPassword = Settings.getProperty("davmail.proxyPassword");
} }
// configure proxy // configure proxy
if (proxyHost != null && proxyHost.length() > 0) { if (proxyHost != null && proxyHost.length() > 0) {
httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort)); httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
if (proxyUser != null && proxyUser.length() > 0) { if (proxyUser != null && proxyUser.length() > 0) {
// detect ntlm authentication (windows domain name in user name)
/* // Only available in newer HttpClient releases, not compatible with slide library
List authPrefs = new ArrayList();
authPrefs.add(AuthPolicy.BASIC);
httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,authPrefs);
*/
// instead detect ntlm authentication (windows domain name in user name)
int backslashindex = proxyUser.indexOf("\\"); int backslashindex = proxyUser.indexOf("\\");
if (backslashindex > 0) { if (backslashindex > 0) {
httpClient.getState().setProxyCredentials(null, proxyHost, httpClient.getState().setProxyCredentials(null, proxyHost,
@ -229,12 +235,6 @@ public class ExchangeSession {
// get the internal HttpClient instance // get the internal HttpClient instance
HttpClient httpClient = wdr.retrieveSessionInstance(); HttpClient httpClient = wdr.retrieveSessionInstance();
/* // Only available in newer HttpClient releases, not compatible with slide library
List authPrefs = new ArrayList();
authPrefs.add(AuthPolicy.BASIC);
httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,authPrefs);
*/
configureClient(httpClient); configureClient(httpClient);
// get webmail root url (will follow redirects) // get webmail root url (will follow redirects)
@ -243,7 +243,7 @@ public class ExchangeSession {
wdr.executeHttpRequestMethod(httpClient, wdr.executeHttpRequestMethod(httpClient,
initmethod); initmethod);
if (initmethod.getPath().indexOf("exchweb/bin") > 0) { if (initmethod.getPath().indexOf("exchweb/bin") > 0) {
LOGGER.debug("** Form based authentication detected"); LOGGER.debug("Form based authentication detected");
PostMethod logonMethod = new PostMethod( PostMethod logonMethod = new PostMethod(
"/exchweb/bin/auth/owaauth.dll?" + "/exchweb/bin/auth/owaauth.dll?" +
@ -288,18 +288,17 @@ public class ExchangeSession {
String body = method.getResponseBodyAsString(); String body = method.getResponseBodyAsString();
int beginIndex = body.indexOf(url); int beginIndex = body.indexOf(url);
if (beginIndex < 0) { if (beginIndex < 0) {
throw new HttpException(url + "not found in body"); throw new HttpException(url + " not found in body");
} }
body = body.substring(beginIndex); body = body.substring(beginIndex);
int endIndex = body.indexOf('"'); int endIndex = body.indexOf('"');
if (endIndex < 0) { if (endIndex < 0) {
throw new HttpException(url + "not found in body"); throw new HttpException(url + " not found in body");
} }
body = body.substring(url.length(), endIndex); body = body.substring(url.length(), endIndex);
// got base http mailbox http url // got base http mailbox http url
mailPath = "/exchange/" + body; mailPath = "/exchange/" + body;
wdr.setPath(mailPath); wdr.setPath(mailPath);
// wdr.propfindMethod(0);
// Retrieve inbox and trash URLs // Retrieve inbox and trash URLs
Vector<String> reqProps = new Vector<String>(); Vector<String> reqProps = new Vector<String>();
@ -308,15 +307,15 @@ public class ExchangeSession {
reqProps.add("urn:schemas:httpmail:sendmsg"); reqProps.add("urn:schemas:httpmail:sendmsg");
reqProps.add("urn:schemas:httpmail:drafts"); reqProps.add("urn:schemas:httpmail:drafts");
Enumeration inboxEnum = wdr.propfindMethod(0, reqProps); Enumeration foldersEnum = wdr.propfindMethod(0, reqProps);
if (!inboxEnum.hasMoreElements()) { if (!foldersEnum.hasMoreElements()) {
throw new IOException("Unable to get inbox"); throw new IOException("Unable to get mail folders");
} }
ResponseEntity inboxResponse = (ResponseEntity) inboxEnum. ResponseEntity inboxResponse = (ResponseEntity) foldersEnum.
nextElement(); nextElement();
Enumeration inboxPropsEnum = inboxResponse.getProperties(); Enumeration inboxPropsEnum = inboxResponse.getProperties();
if (!inboxPropsEnum.hasMoreElements()) { if (!inboxPropsEnum.hasMoreElements()) {
throw new IOException("Unable to get inbox"); throw new IOException("Unable to get mail folders");
} }
while (inboxPropsEnum.hasMoreElements()) { while (inboxPropsEnum.hasMoreElements()) {
Property inboxProp = (Property) inboxPropsEnum.nextElement(); Property inboxProp = (Property) inboxPropsEnum.nextElement();
@ -343,6 +342,8 @@ public class ExchangeSession {
LOGGER.debug("Inbox URL : " + inboxUrl); LOGGER.debug("Inbox URL : " + inboxUrl);
LOGGER.debug("Trash URL : " + deleteditemsUrl); LOGGER.debug("Trash URL : " + deleteditemsUrl);
LOGGER.debug("Send URL : " + sendmsgUrl); LOGGER.debug("Send URL : " + sendmsgUrl);
LOGGER.debug("Drafts URL : " + draftsUrl);
// TODO : sometimes path, sometimes Url ?
deleteditemsUrl = URIUtil.getPath(deleteditemsUrl); deleteditemsUrl = URIUtil.getPath(deleteditemsUrl);
wdr.setPath(URIUtil.getPath(inboxUrl)); wdr.setPath(URIUtil.getPath(inboxUrl));
@ -376,7 +377,6 @@ public class ExchangeSession {
LOGGER.error(message.toString()); LOGGER.error(message.toString());
throw new IOException(message.toString()); throw new IOException(message.toString());
} }
} }
/** /**
@ -479,12 +479,10 @@ public class ExchangeSession {
public Message getMessage(String messageUrl) throws IOException { public Message getMessage(String messageUrl) throws IOException {
// TODO switch according to Log4J log level // TODO switch according to Log4J log level
//wdr.setDebug(4);
wdr.setDebug(4); //wdr.propfindMethod(messageUrl, 0);
wdr.propfindMethod(messageUrl, 0);
Enumeration messageEnum = wdr.propfindMethod(messageUrl, 0, MESSAGE_REQUEST_PROPERTIES); Enumeration messageEnum = wdr.propfindMethod(messageUrl, 0, MESSAGE_REQUEST_PROPERTIES);
wdr.setDebug(0); //wdr.setDebug(0);
// 201 created in some cases ?!? // 201 created in some cases ?!?
if ((wdr.getStatusCode() != HttpURLConnection.HTTP_OK && wdr.getStatusCode() != HttpURLConnection.HTTP_CREATED) if ((wdr.getStatusCode() != HttpURLConnection.HTTP_OK && wdr.getStatusCode() != HttpURLConnection.HTTP_CREATED)
@ -500,11 +498,11 @@ public class ExchangeSession {
public List<Message> getAllMessages() throws IOException { public List<Message> getAllMessages() throws IOException {
List<Message> messages = new ArrayList<Message>(); List<Message> messages = new ArrayList<Message>();
wdr.setDebug(4); //wdr.setDebug(4);
wdr.propfindMethod(currentFolderUrl, 1); //wdr.propfindMethod(currentFolderUrl, 1);
// one level search
Enumeration folderEnum = wdr.propfindMethod(currentFolderUrl, 1, MESSAGE_REQUEST_PROPERTIES); Enumeration folderEnum = wdr.propfindMethod(currentFolderUrl, 1, MESSAGE_REQUEST_PROPERTIES);
wdr.setDebug(0); //wdr.setDebug(0);
while (folderEnum.hasMoreElements()) { while (folderEnum.hasMoreElements()) {
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement(); ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
@ -533,7 +531,7 @@ public class ExchangeSession {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -keepDelay); cal.add(Calendar.DAY_OF_MONTH, -keepDelay);
LOGGER.debug("Keep message not before " + cal.getTime()); LOGGER.debug("Delete messages in trash since " + cal.getTime());
long keepTimestamp = cal.getTimeInMillis(); long keepTimestamp = cal.getTimeInMillis();
Vector<String> deleteRequestProperties = new Vector<String>(); Vector<String> deleteRequestProperties = new Vector<String>();
@ -592,6 +590,7 @@ public class ExchangeSession {
subject = subject.replaceAll("/", "_xF8FF_"); subject = subject.replaceAll("/", "_xF8FF_");
// '?' is also invalid // '?' is also invalid
subject = subject.replaceAll("\\?", ""); subject = subject.replaceAll("\\?", "");
// TODO : test & in subject
} }
} }
@ -607,6 +606,14 @@ public class ExchangeSession {
} }
/**
* Select current folder.
* Folder name can be logical names INBOX or TRASH (translated to local names),
* relative path to user base folder or absolute path.
* @param folderName folder name
* @return Folder object
* @throws IOException when unable to change folder
*/
public Folder selectFolder(String folderName) throws IOException { public Folder selectFolder(String folderName) throws IOException {
Folder folder = new Folder(); Folder folder = new Folder();
folder.folderUrl = null; folder.folderUrl = null;
@ -625,6 +632,7 @@ public class ExchangeSession {
reqProps.add("urn:schemas:httpmail:unreadcount"); reqProps.add("urn:schemas:httpmail:unreadcount");
reqProps.add("DAV:childcount"); reqProps.add("DAV:childcount");
Enumeration folderEnum = wdr.propfindMethod(folder.folderUrl, 0, reqProps); Enumeration folderEnum = wdr.propfindMethod(folder.folderUrl, 0, reqProps);
if (folderEnum.hasMoreElements()) { if (folderEnum.hasMoreElements()) {
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement(); ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
Enumeration propertiesEnum = entity.getProperties(); Enumeration propertiesEnum = entity.getProperties();
@ -639,7 +647,7 @@ public class ExchangeSession {
} }
} else { } else {
throw new IOException("Folder not found :" + folder.folderUrl); throw new IOException("Folder not found: " + folder.folderUrl);
} }
currentFolderUrl = folder.folderUrl; currentFolderUrl = folder.folderUrl;
return folder; return folder;
@ -704,7 +712,8 @@ public class ExchangeSession {
Date parsedDate = dateParser.parse(date); Date parsedDate = dateParser.parse(date);
date = dateFormatter.format(parsedDate); date = dateFormatter.format(parsedDate);
} }
fullHeaders = "Skipped header\n" + getReceived() + fullHeaders = "Skipped header\n" +
getReceived() +
"MIME-Version: 1.0\n" + "MIME-Version: 1.0\n" +
"Content-Type: application/ms-tnef;\n" + "Content-Type: application/ms-tnef;\n" +
"\tname=\"winmail.dat\"\n" + "\tname=\"winmail.dat\"\n" +
@ -735,8 +744,7 @@ public class ExchangeSession {
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = new BufferedReader(new StringReader(fullHeaders)); reader = new BufferedReader(new StringReader(fullHeaders));
String line; String line = reader.readLine();
line = reader.readLine();
while (line != null && line.length() > 0) { while (line != null && line.length() > 0) {
// patch exchange Content type // patch exchange Content type
if (line.equals(CONTENT_TYPE_HEADER + "application/ms-tnef;")) { if (line.equals(CONTENT_TYPE_HEADER + "application/ms-tnef;")) {
@ -785,9 +793,7 @@ public class ExchangeSession {
try { try {
parsedAttachmentIndex = Integer.parseInt(attachmentName); parsedAttachmentIndex = Integer.parseInt(attachmentName);
} catch (Exception e) { } catch (Exception e) {
if (LOGGER.isDebugEnabled()) { LOGGER.debug("Current attachment name " + attachmentName + " is not an index", e);
LOGGER.debug("Current attachment name " + attachmentName + " is not an index", e);
}
} }
if (parsedAttachmentIndex == 0) { if (parsedAttachmentIndex == 0) {
Attachment attachment = attachmentsMap.get(attachmentName); Attachment attachment = attachmentsMap.get(attachmentName);
@ -1105,31 +1111,7 @@ public class ExchangeSession {
// TODO : refactor // TODO : refactor
String destination = deleteditemsUrl + messageUrl.substring(messageUrl.lastIndexOf("/")); String destination = deleteditemsUrl + messageUrl.substring(messageUrl.lastIndexOf("/"));
LOGGER.debug("Deleting : " + messageUrl + " to " + destination); LOGGER.debug("Deleting : " + messageUrl + " to " + destination);
/*
// first try without webdav library
GetMethod moveMethod = new GetMethod(URIUtil.encodePathQuery(messageUrl)) {
public String getName() {
return "MOVE";
}
};
moveMethod.addRequestHeader("Destination", URIUtil.encodePathQuery(destination));
moveMethod.addRequestHeader("Overwrite", "F");
wdr.retrieveSessionInstance().executeMethod(moveMethod);
if (moveMethod.getStatusCode() == 412) {
int count = 2;
// name conflict, try another name
while (wdr.getStatusCode() == 412) {
moveMethod = new GetMethod(URIUtil.encodePathQuery(messageUrl)) {
public String getName() {
return "MOVE";
}
};
moveMethod.addRequestHeader("Destination", URIUtil.encodePathQuery(destination.substring(0, destination.lastIndexOf('.')) + "-" + count++ + ".eml"));
moveMethod.addRequestHeader("Overwrite", "F");
}
}
*/
wdr.moveMethod(messageUrl, destination); wdr.moveMethod(messageUrl, destination);
if (wdr.getStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED) { if (wdr.getStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED) {
int count = 2; int count = 2;
@ -1140,7 +1122,6 @@ public class ExchangeSession {
} }
LOGGER.debug("Deleted to :" + destination + " " + wdr.getStatusCode() + " " + wdr.getStatusMessage()); LOGGER.debug("Deleted to :" + destination + " " + wdr.getStatusCode() + " " + wdr.getStatusMessage());
} }
public void printHeaders(OutputStream os) throws IOException { public void printHeaders(OutputStream os) throws IOException {