From c8f6c4d62578973ca6bc2360e333a83aae9e5edf Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Thu, 7 Aug 2014 17:00:54 -0400 Subject: [PATCH] Eliminate searching for '3' in exception message This was dead code. The exception message will always start with either "SMTP response is 0 length" from checkLine() or else "Negative SMTP reply" from NegativeSmtpReplyException(). The problem originated from way back before 4.904. --- .../fsck/k9/mail/transport/SmtpTransport.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/com/fsck/k9/mail/transport/SmtpTransport.java b/src/com/fsck/k9/mail/transport/SmtpTransport.java index 66b0a6b19..76ac71cf3 100644 --- a/src/com/fsck/k9/mail/transport/SmtpTransport.java +++ b/src/com/fsck/k9/mail/transport/SmtpTransport.java @@ -666,6 +666,12 @@ public class SmtpTransport extends Transport { * Read lines as long as the length is 4 or larger, e.g. "220-banner text here". * Shorter lines are either errors of contain only a reply code. Those cases will * be handled by checkLine() below. + * + * TODO: All responses should be checked to confirm that they start with a valid + * reply code, and that the reply code is appropriate for the command being executed. + * That means it should either be a 2xx code (generally) or a 3xx code in special cases + * (e.g., DATA & AUTH LOGIN commands). Reply codes should be made available as part of + * the returned object. */ String line = readLine(); while (line.length() >= 4) { @@ -711,12 +717,14 @@ public class SmtpTransport extends Transport { executeSimpleCommand("AUTH LOGIN"); executeSimpleCommand(Utility.base64Encode(username), true); executeSimpleCommand(Utility.base64Encode(password), true); - } catch (MessagingException me) { - if (me.getMessage().length() > 1 && me.getMessage().charAt(1) == '3') { - throw new AuthenticationFailedException("AUTH LOGIN failed (" + me.getMessage() - + ")"); + } catch (NegativeSmtpReplyException exception) { + if (exception.getReplyCode() == 535) { + // Authentication credentials invalid + throw new AuthenticationFailedException("AUTH LOGIN failed (" + + exception.getMessage() + ")"); + } else { + throw exception; } - throw me; } } @@ -725,12 +733,14 @@ public class SmtpTransport extends Transport { String data = Utility.base64Encode("\000" + username + "\000" + password); try { executeSimpleCommand("AUTH PLAIN " + data, true); - } catch (MessagingException me) { - if (me.getMessage().length() > 1 && me.getMessage().charAt(1) == '3') { - throw new AuthenticationFailedException("AUTH PLAIN failed (" + me.getMessage() - + ")"); + } catch (NegativeSmtpReplyException exception) { + if (exception.getReplyCode() == 535) { + // Authentication credentials invalid + throw new AuthenticationFailedException("AUTH PLAIN failed (" + + exception.getMessage() + ")"); + } else { + throw exception; } - throw me; } } @@ -748,7 +758,12 @@ public class SmtpTransport extends Transport { try { executeSimpleCommand(b64CRAMString, true); } catch (NegativeSmtpReplyException exception) { - throw new AuthenticationFailedException(exception.getMessage(), exception); + if (exception.getReplyCode() == 535) { + // Authentication credentials invalid + throw new AuthenticationFailedException(exception.getMessage(), exception); + } else { + throw exception; + } } }