int colonIndex = argument.indexOf(":");
recipient = argument.substring(colonIndex + 1);
argument = argument.substring(0, colonIndex);
}
if (!session.getState().containsKey(SMTPSession.SENDER)) {
return new SMTPResponse(SMTPRetCode.BAD_SEQUENCE, DSNStatus
.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_OTHER)
+ " Need MAIL before RCPT");
} else if (argument == null
|| !argument.toUpperCase(Locale.US).equals("TO")
|| recipient == null) {
return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,
DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.DELIVERY_SYNTAX)
+ " Usage: RCPT TO:<recipient>");
}
Collection rcptColl = (Collection) session.getState().get(
SMTPSession.RCPT_LIST);
if (rcptColl == null) {
rcptColl = new ArrayList();
}
recipient = recipient.trim();
int lastChar = recipient.lastIndexOf('>');
// Check to see if any options are present and, if so, whether they
// are correctly formatted
// (separated from the closing angle bracket by a ' ').
String rcptOptionString = null;
if ((lastChar > 0) && (recipient.length() > lastChar + 2)
&& (recipient.charAt(lastChar + 1) == ' ')) {
rcptOptionString = recipient.substring(lastChar + 2);
// Remove the options from the recipient
recipient = recipient.substring(0, lastChar + 1);
}
if (session.useAddressBracketsEnforcement()
&& (!recipient.startsWith("<") || !recipient.endsWith(">"))) {
if (session.getLogger().isInfoEnabled()) {
StringBuilder errorBuffer = new StringBuilder(192).append(
"Error parsing recipient address: ").append(
"Address did not start and end with < >").append(
getContext(session, null, recipient));
session.getLogger().info(errorBuffer.toString());
}
return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,
DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.DELIVERY_SYNTAX)
+ " Syntax error in parameters or arguments");
}
MailAddress recipientAddress = null;
// Remove < and >
if (session.useAddressBracketsEnforcement()
|| (recipient.startsWith("<") && recipient.endsWith(">"))) {
recipient = recipient.substring(1, recipient.length() - 1);
}
if (recipient.indexOf("@") < 0) {
// set the default domain
recipient = recipient
+ "@"
+ getDefaultDomain();
}
try {
recipientAddress = new MailAddress(recipient);
} catch (Exception pe) {
if (session.getLogger().isInfoEnabled()) {
StringBuilder errorBuffer = new StringBuilder(192).append(
"Error parsing recipient address: ").append(
getContext(session, recipientAddress, recipient))
.append(pe.getMessage());
session.getLogger().info(errorBuffer.toString());
}
/*
* from RFC2822; 553 Requested action not taken: mailbox name
* not allowed (e.g., mailbox syntax incorrect)
*/
return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_MAILBOX,
DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.ADDRESS_SYNTAX)
+ " Syntax error in recipient address");
}
if (rcptOptionString != null) {
StringTokenizer optionTokenizer = new StringTokenizer(
rcptOptionString, " ");
while (optionTokenizer.hasMoreElements()) {
String rcptOption = optionTokenizer.nextToken();
int equalIndex = rcptOption.indexOf('=');
String rcptOptionName = rcptOption;
String rcptOptionValue = "";
if (equalIndex > 0) {
rcptOptionName = rcptOption.substring(0, equalIndex)
.toUpperCase(Locale.US);
rcptOptionValue = rcptOption.substring(equalIndex + 1);
}
// Unexpected option attached to the RCPT command
if (session.getLogger().isDebugEnabled()) {
StringBuilder debugBuffer = new StringBuilder(128)
.append(
"RCPT command had unrecognized/unexpected option ")
.append(rcptOptionName).append(" with value ")
.append(rcptOptionValue).append(
getContext(session, recipientAddress,
recipient));
session.getLogger().debug(debugBuffer.toString());
}
return new SMTPResponse(
SMTPRetCode.PARAMETER_NOT_IMPLEMENTED,
"Unrecognized or unsupported option: "
+ rcptOptionName);
}
optionTokenizer = null;