1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Extracted POP3 commands and capabilities to constants

This commit is contained in:
cketti 2011-07-17 14:13:04 +02:00
parent 45e29ccba4
commit 9f581cb6f3

View File

@ -32,6 +32,24 @@ public class Pop3Store extends Store {
public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3; public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4; public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;
private static final String STLS_COMMAND = "STLS";
private static final String USER_COMMAND = "USER";
private static final String PASS_COMMAND = "PASS";
private static final String CAPA_COMMAND = "PASS";
private static final String STAT_COMMAND = "STAT";
private static final String LIST_COMMAND = "LIST";
private static final String UIDL_COMMAND = "UIDL";
private static final String TOP_COMMAND = "TOP";
private static final String RETR_COMMAND = "RETR";
private static final String DELE_COMMAND = "DELE";
private static final String QUIT_COMMAND = "QUIT";
private static final String STLS_CAPABILITY = "STLS";
private static final String UIDL_CAPABILITY = "UIDL";
private static final String PIPELINING_CAPABILITY = "PIPELINING";
private static final String USER_CAPABILITY = "USER";
private static final String TOP_CAPABILITY = "TOP";
private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED }; private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED };
private String mHost; private String mHost;
@ -145,7 +163,7 @@ public class Pop3Store extends Store {
* If the server doesn't support UIDL it will return a - response, which causes * If the server doesn't support UIDL it will return a - response, which causes
* executeSimpleCommand to throw a MessagingException, exiting this method. * executeSimpleCommand to throw a MessagingException, exiting this method.
*/ */
folder.executeSimpleCommand("UIDL"); folder.executeSimpleCommand(UIDL_COMMAND);
} }
folder.close(); folder.close();
@ -211,7 +229,7 @@ public class Pop3Store extends Store {
mCapabilities = getCapabilities(); mCapabilities = getCapabilities();
if (mCapabilities.stls) { if (mCapabilities.stls) {
writeLine("STLS"); writeLine(STLS_COMMAND);
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED; boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED;
@ -243,8 +261,8 @@ public class Pop3Store extends Store {
} }
} else { } else {
try { try {
executeSimpleCommand("USER " + mUsername); executeSimpleCommand(USER_COMMAND + " " + mUsername);
executeSimpleCommand("PASS " + mPassword, true); executeSimpleCommand(PASS_COMMAND + " " + mPassword, true);
} catch (MessagingException me) { } catch (MessagingException me) {
throw new AuthenticationFailedException(null, me); throw new AuthenticationFailedException(null, me);
} }
@ -260,7 +278,7 @@ public class Pop3Store extends Store {
throw new MessagingException("Unable to open connection to POP server.", ioe); throw new MessagingException("Unable to open connection to POP server.", ioe);
} }
String response = executeSimpleCommand("STAT"); String response = executeSimpleCommand(STAT_COMMAND);
String[] parts = response.split(" "); String[] parts = response.split(" ");
mMessageCount = Integer.parseInt(parts[1]); mMessageCount = Integer.parseInt(parts[1]);
@ -284,7 +302,7 @@ public class Pop3Store extends Store {
public void close() { public void close() {
try { try {
if (isOpen()) { if (isOpen()) {
executeSimpleCommand("QUIT"); executeSimpleCommand(QUIT_COMMAND);
} }
} catch (Exception e) { } catch (Exception e) {
/* /*
@ -425,7 +443,7 @@ public class Pop3Store extends Store {
for (int msgNum = start; msgNum <= end; msgNum++) { for (int msgNum = start; msgNum <= end; msgNum++) {
Pop3Message message = mMsgNumToMsgMap.get(msgNum); Pop3Message message = mMsgNumToMsgMap.get(msgNum);
if (message == null) { if (message == null) {
String response = executeSimpleCommand("UIDL " + msgNum); String response = executeSimpleCommand(UIDL_COMMAND + " " + msgNum);
int uidIndex = response.lastIndexOf(' '); int uidIndex = response.lastIndexOf(' ');
String msgUid = response.substring(uidIndex + 1); String msgUid = response.substring(uidIndex + 1);
message = new Pop3Message(msgUid, this); message = new Pop3Message(msgUid, this);
@ -433,7 +451,7 @@ public class Pop3Store extends Store {
} }
} }
} else { } else {
String response = executeSimpleCommand("UIDL"); String response = executeSimpleCommand(UIDL_COMMAND);
while ((response = readLine()) != null) { while ((response = readLine()) != null) {
if (response.equals(".")) { if (response.equals(".")) {
break; break;
@ -484,7 +502,7 @@ public class Pop3Store extends Store {
* get them is to do a full UIDL list. A possible optimization * get them is to do a full UIDL list. A possible optimization
* would be trying UIDL for the latest X messages and praying. * would be trying UIDL for the latest X messages and praying.
*/ */
String response = executeSimpleCommand("UIDL"); String response = executeSimpleCommand(UIDL_COMMAND);
while ((response = readLine()) != null) { while ((response = readLine()) != null) {
if (response.equals(".")) { if (response.equals(".")) {
break; break;
@ -629,7 +647,7 @@ public class Pop3Store extends Store {
if (listener != null) { if (listener != null) {
listener.messageStarted(pop3Message.getUid(), i, count); listener.messageStarted(pop3Message.getUid(), i, count);
} }
String response = executeSimpleCommand(String.format("LIST %d", String response = executeSimpleCommand(String.format(LIST_COMMAND + " %d",
mUidToMsgNumMap.get(pop3Message.getUid()))); mUidToMsgNumMap.get(pop3Message.getUid())));
String[] listParts = response.split(" "); String[] listParts = response.split(" ");
//int msgNum = Integer.parseInt(listParts[1]); //int msgNum = Integer.parseInt(listParts[1]);
@ -645,7 +663,7 @@ public class Pop3Store extends Store {
msgUidIndex.add(message.getUid()); msgUidIndex.add(message.getUid());
} }
int i = 0, count = messages.length; int i = 0, count = messages.length;
String response = executeSimpleCommand("LIST"); String response = executeSimpleCommand(LIST_COMMAND);
while ((response = readLine()) != null) { while ((response = readLine()) != null) {
if (response.equals(".")) { if (response.equals(".")) {
break; break;
@ -688,7 +706,7 @@ public class Pop3Store extends Store {
"Checking to see if the TOP command is supported nevertheless."); "Checking to see if the TOP command is supported nevertheless.");
} }
response = executeSimpleCommand(String.format("TOP %d %d", response = executeSimpleCommand(String.format(TOP_COMMAND + " %d %d",
mUidToMsgNumMap.get(message.getUid()), lines)); mUidToMsgNumMap.get(message.getUid()), lines));
// TOP command is supported. Remember this for the next time. // TOP command is supported. Remember this for the next time.
@ -710,7 +728,7 @@ public class Pop3Store extends Store {
} }
if (response == null) { if (response == null) {
response = executeSimpleCommand(String.format("RETR %d", response = executeSimpleCommand(String.format(RETR_COMMAND + " %d",
mUidToMsgNumMap.get(message.getUid()))); mUidToMsgNumMap.get(message.getUid())));
} }
@ -790,7 +808,7 @@ public class Pop3Store extends Store {
me.setPermanentFailure(true); me.setPermanentFailure(true);
throw me; throw me;
} }
executeSimpleCommand(String.format("DELE %s", msgNum)); executeSimpleCommand(String.format(DELE_COMMAND + " %s", msgNum));
} }
} }
@ -826,20 +844,20 @@ public class Pop3Store extends Store {
private Pop3Capabilities getCapabilities() throws IOException { private Pop3Capabilities getCapabilities() throws IOException {
Pop3Capabilities capabilities = new Pop3Capabilities(); Pop3Capabilities capabilities = new Pop3Capabilities();
try { try {
String response = executeSimpleCommand("CAPA"); String response = executeSimpleCommand(CAPA_COMMAND);
while ((response = readLine()) != null) { while ((response = readLine()) != null) {
if (response.equals(".")) { if (response.equals(".")) {
break; break;
} }
if (response.equalsIgnoreCase("STLS")) { if (response.equalsIgnoreCase(STLS_CAPABILITY)) {
capabilities.stls = true; capabilities.stls = true;
} else if (response.equalsIgnoreCase("UIDL")) { } else if (response.equalsIgnoreCase(UIDL_CAPABILITY)) {
capabilities.uidl = true; capabilities.uidl = true;
} else if (response.equalsIgnoreCase("PIPELINING")) { } else if (response.equalsIgnoreCase(PIPELINING_CAPABILITY)) {
capabilities.pipelining = true; capabilities.pipelining = true;
} else if (response.equalsIgnoreCase("USER")) { } else if (response.equalsIgnoreCase(USER_CAPABILITY)) {
capabilities.user = true; capabilities.user = true;
} else if (response.equalsIgnoreCase("TOP")) { } else if (response.equalsIgnoreCase(TOP_CAPABILITY)) {
capabilities.top = true; capabilities.top = true;
} }
} }