<commit_before>/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/



package org.apache.james.pop3server;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.james.services.MailRepository;
import org.apache.james.util.POP3BeforeSMTPHelper;

/**
  * Handles PASS command
  */
public class PassCmdHandler extends AbstractLogEnabled implements CommandHandler {

    /**
     * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
     */
    public void onCommand(POP3Session session) {
        doPASS(session,session.getCommandArgument());
    }

    /**
     * Handler method called upon receipt of a PASS command.
     * Reads in and validates the password.
     *
     * @param argument the first argument parsed by the parseCommand method
     */
    private void doPASS(POP3Session session,String argument) {
        String responseString = null;
        if (session.getHandlerState() == POP3Handler.AUTHENTICATION_USERSET && argument != null) {
            String passArg = argument;
            if (session.getConfigurationData().getUsersRepository().test(session.getUser(), passArg)) {
                try {
                    MailRepository inbox = session.getConfigurationData().getMailServer().getUserInbox(session.getUser());
                    if (inbox == null) {
                        throw new IllegalStateException("MailServer returned a null inbox for "+session.getUser());
                    }
                    session.setUserInbox(inbox);
                    RsetCmdHandler.stat(session, getLogger());
                    
                    // Store the ipAddress to use it later for pop before smtp 
                    POP3BeforeSMTPHelper.addIPAddress(session.getRemoteIPAddress());
                    
                    StringBuffer responseBuffer =
                        new StringBuffer(64)
                                .append(POP3Handler.OK_RESPONSE)
                                .append(" Welcome ")
                                .append(session.getUser());
                    responseString = responseBuffer.toString();
                    session.setHandlerState(POP3Handler.TRANSACTION);
                    session.writeResponse(responseString);
                } catch (RuntimeException e) {
                    getLogger().error("Unexpected error accessing mailbox for "+session.getUser(),e);
                    responseString = POP3Handler.ERR_RESPONSE + " Unexpected error accessing mailbox";
                    session.setHandlerState(POP3Handler.AUTHENTICATION_READY);
                    session.writeResponse(responseString);
                }
            } else {
                responseString = POP3Handler.ERR_RESPONSE + " Authentication failed.";
                session.setHandlerState(POP3Handler.AUTHENTICATION_READY);
                session.writeResponse(responseString);
            }
        } else {
            responseString = POP3Handler.ERR_RESPONSE;<commit_msg>JAMES-922 Upgrade to Java 1.5 https://issues.apache.org/jira/browse/JAMES-922<commit_after>/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/



package org.apache.james.pop3server;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.james.services.MailRepository;
import org.apache.james.util.POP3BeforeSMTPHelper;

/**
  * Handles PASS command
  */
public class PassCmdHandler extends AbstractLogEnabled implements CommandHandler {

    /**
     * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
     */
    public void onCommand(POP3Session session) {
        doPASS(session,session.getCommandArgument());
    }

    /**
     * Handler method called upon receipt of a PASS command.
     * Reads in and validates the password.
     *
     * @param argument the first argument parsed by the parseCommand method
     */
    private void doPASS(POP3Session session,String argument) {
        String responseString = null;
        if (session.getHandlerState() == POP3Handler.AUTHENTICATION_USERSET && argument != null) {
            String passArg = argument;
            if (session.getConfigurationData().getUsersRepository().test(session.getUser(), passArg)) {
                try {
                    MailRepository inbox = session.getConfigurationData().getMailServer().getUserInbox(session.getUser());
                    if (inbox == null) {
                        throw new IllegalStateException("MailServer returned a null inbox for "+session.getUser());
                    }
                    session.setUserInbox(inbox);
                    RsetCmdHandler.stat(session, getLogger());
                    
                    // Store the ipAddress to use it later for pop before smtp 
                    POP3BeforeSMTPHelper.addIPAddress(session.getRemoteIPAddress());
                    
                    StringBuilder responseBuffer =
                        new StringBuilder(64)
                                .append(POP3Handler.OK_RESPONSE)
                                .append(" Welcome ")
                                .append(session.getUser());
                    responseString = responseBuffer.toString();
                    session.setHandlerState(POP3Handler.TRANSACTION);
                    session.writeResponse(responseString);
                } catch (RuntimeException e) {
                    getLogger().error("Unexpected error accessing mailbox for "+session.getUser(),e);
                    responseString = POP3Handler.ERR_RESPONSE + " Unexpected error accessing mailbox";
                    session.setHandlerState(POP3Handler.AUTHENTICATION_READY);
                    session.writeResponse(responseString);
                }
            } else {
                responseString = POP3Handler.ERR_RESPONSE + " Authentication failed.";
                session.setHandlerState(POP3Handler.AUTHENTICATION_READY);
                session.writeResponse(responseString);
            }
        } else {
            responseString = POP3Handler.ERR_RESPONSE;
{'fraction_non_alphanumeric': 0.10559827006246997, 'fraction_numerical': 0.006006727534839019, 'mean_word_length': 1.954222853087296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7593369', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1844, 'n_words': 508}