String username = "";
String domain = "";
String password = "";
EmailAddress address = null;
DeliveryService deliveryService = DeliveryService.getDeliveryService();
//Get the username from the client.
boolean userAccepted = false;
while( !userAccepted ) {
inputString = read();
command = parseCommand( inputString );
argument = parseArgument( inputString );
//Check to see if they sent the user command.
if( command.equals( COMMAND_USER ) ) {
//Make sure they sent a username
if( argument.equals( "" ) ) {
write( MESSAGE_TOO_FEW_ARGUMENTS );
}
else {
int atIndex = argument.indexOf( "@" );
//Verify that the username contains the domain.
if( atIndex == -1 ) {
write( MESSAGE_NEED_USER_DOMAIN );
}
else {
//Accept the user, and proceed to get the password.
username = argument.substring( 0, atIndex );
domain = argument.substring( atIndex + 1 );
address = new EmailAddress( username, domain );
//Check to see if the user's mailbox is locked
if( deliveryService.isMailboxLocked( address ) ) {
write( MESSAGE_USER_MAILBOX_LOCKED );
}
else {
write( MESSAGE_USER_ACCEPTED + argument );
userAccepted = true;
}
}
}
}
else {
write( MESSAGE_INVALID_COMMAND + command );
}
}
//The user has been accepted, now get the password.
boolean passwordAccepted = false;
while( !passwordAccepted ) {
inputString = read();
command = parseCommand( inputString );
argument = parseArgument( inputString );
//Check to see if they sent the user command.
if( command.equals( COMMAND_PASS ) ) {
//Make sure they sent a password
if( argument.equals( "" ) ) {
write( MESSAGE_TOO_FEW_ARGUMENTS );
}
else {
password = argument;
passwordAccepted = true;
}
}
else {
write( MESSAGE_INVALID_COMMAND + command );
}
}
User user = configurationManager.getUser( address );
if( user != null && user.isPasswordValid( password ) )
{
deliveryService.ipAuthenticated( clientIp );
deliveryService.lockMailbox( address );
write( MESSAGE_LOGIN_SUCCESSFUL );
if( log.isInfoEnabled() ) log.info( "User: " + address.getAddress() + " logged in successfully.");
return user;
}
else