<commit_before>/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.james.imapserver;

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.mail.internet.*;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
import org.apache.avalon.cornerstone.services.scheduler.Target;
import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
import org.apache.james.AccessControlException;
import org.apache.james.AuthenticationException;
import org.apache.james.AuthorizationException;
import org.apache.james.Constants;
import org.apache.james.services.*;
import org.apache.james.util.InternetPrintWriter;
import org.apache.log.LogKit;
import org.apache.log.Logger;

/**
 * An IMAP Handler handles one IMAP connection. TBC - it may spawn worker
 * threads someday.
 *
 * <p> Based on SMTPHandler and POP3Handler by <NAME> <<EMAIL>>
 *
 * @author  <a href="mailto:<EMAIL>"><NAME></a>
 * @version 0.1 on 14 Dec 2000
 */
public class SingleThreadedConnectionHandler
    extends BaseCommand
    implements ConnectionHandler, Composable, Configurable,
               Initializable, Disposable, Target, MailboxEventListener {

    //mainly to switch on stack traces and catch responses;
    private static final boolean DEEP_DEBUG = true;

    // Connection states
    private static final int NON_AUTHENTICATED = 0;
    private static final int AUTHENTICATED = 1;
    private static final int SELECTED = 2;
    private static final int LOGOUT = 3;

    // Connection termination options
    private static final int NORMAL_CLOSE = 0;
    private static final int OK_BYE = 1;
    private static final int UNTAGGED_BYE = 2;
    private static final int TAGGED_NO = 3;
    private static final int NO_BYE = 4;

    // Basic response types
    private static final String OK = "OK";
    private static final String NO = "NO";
    private static final String BAD = "BAD";
    private static final String UNTAGGED = "*";

    private static final String SP = " ";
    private static final String VERSION = "IMAP4rev1";
    private static final String CAPABILITY_RESPONSE = "CAPABILITY " + VERSION
        + " NAMESPACE" + " ACL"; //add as implemented

    private static final String LIST_WILD = "*";
    private static final String LIST_WILD_FLAT = "%";
    private static final char[] CTL = {};
    private static final String[] ATOM_SPECIALS
        = {"(", ")", "{", " ", LIST_WILD, LIST_WILD_FLAT,};

    private static final String AUTH_FAIL_MSG
        = "NO Command not authorized on this mailbox";
    private static final String BAD_LISTRIGHTS_MSG
        = "BAD Command should be <tag> <LISTRIGHTS> <mailbox> <identifier>";
    private static final String BAD_MYRIGHTS_MSG
        = "BAD Command should be <tag> <MYRIGHTS> <mailbox>";
    private static final String BAD_LIST_MSG
        = "BAD Command should be <tag> <LIST> <reference name> <mailbox>";
    private static final String BAD_LSUB_MSG
        = "BAD Command should be <tag> <LSUB> <reference name> <mailbox>";
    private static final String NO_NOTLOCAL_MSG
        = "NO Mailbox does not exist on this server";

    private Logger securityLogger = LogKit.getLoggerFor("james.Security");
    private MailServer mailServer;
    private UsersRepository users;
    private TimeScheduler scheduler;

    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private OutputStream outs;
    private String remoteHost;
    private String remoteIP;
    private String softwaretype  = "JAMES IMAP4rev1 Server " + Constants.SOFTWARE_VERSION;
    private int state;
    private String user;

    private IMAPSystem imapSystem;
    private Host imapHost;
    private String namespaceToken;
    private String currentNamespace = null;
    private String currentSeperator = null;
    private String commandRaw;

    //currentFolder holds the client-dependent absolute address of the current
    //folder, that is current Namespace and full mailbox hierarchy.
    private String currentFolder = null;
    private ACLMailbox currentMailbox = null;
    private boolean currentIsReadOnly = false;
    private boolean connectionClosed = false;
    private String tag;
    private boolean checkMailboxFlag = false;
    private int exists;
    private int recent;
    private List sequence;

    public void compose( final ComponentManager componentManager )
        throws ComponentException {

        mailServer = (MailServer)componentManager.
            lookup("org.apache.james.services.MailServer");
        users = (UsersRepository)componentManager.
            lookup("org.apache.james.services.UsersRepository");
        scheduler = (TimeScheduler)componentManager.
            lookup("org.apache.avalon.cornerstone.services.scheduler.TimeScheduler");
        imapSystem = (IMAPSystem)componentManager.
            lookup("org.apache.james.imapserver.IMAPSystem");
        imapHost = (Host)componentManager.
            lookup("org.apache.james.imapserver.Host");
    }

    public void initialize() throws Exception {
        getLogger().info("SingleThreadedConnectionHandler starting ...");
        namespaceToken = imapSystem.getNamespaceToken();
        getLogger().info("SingleThreadedConnectionHandler initialized");
    }

    /**
     * Handle a connection.
     * This handler is responsible for processing connections as they occur.
     *
     * @param connection the connection<commit_msg>Adjusted for LogKit deprecation.<commit_after>/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.james.imapserver;

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.mail.internet.*;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
import org.apache.avalon.cornerstone.services.scheduler.Target;
import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
import org.apache.james.AccessControlException;
import org.apache.james.AuthenticationException;
import org.apache.james.AuthorizationException;
import org.apache.james.Constants;
import org.apache.james.services.*;
import org.apache.james.util.InternetPrintWriter;
import org.apache.log.Logger;

/**
 * An IMAP Handler handles one IMAP connection. TBC - it may spawn worker
 * threads someday.
 *
 * <p> Based on SMTPHandler and POP3Handler by <NAME> <<EMAIL>>
 *
 * @author  <a href="mailto:<EMAIL>"><NAME></a>
 * @version 0.1 on 14 Dec 2000
 */
public class SingleThreadedConnectionHandler
    extends BaseCommand
    implements ConnectionHandler, Composable, Configurable,
               Initializable, Disposable, Target, MailboxEventListener {

    //mainly to switch on stack traces and catch responses;
    private static final boolean DEEP_DEBUG = true;

    // Connection states
    private static final int NON_AUTHENTICATED = 0;
    private static final int AUTHENTICATED = 1;
    private static final int SELECTED = 2;
    private static final int LOGOUT = 3;

    // Connection termination options
    private static final int NORMAL_CLOSE = 0;
    private static final int OK_BYE = 1;
    private static final int UNTAGGED_BYE = 2;
    private static final int TAGGED_NO = 3;
    private static final int NO_BYE = 4;

    // Basic response types
    private static final String OK = "OK";
    private static final String NO = "NO";
    private static final String BAD = "BAD";
    private static final String UNTAGGED = "*";

    private static final String SP = " ";
    private static final String VERSION = "IMAP4rev1";
    private static final String CAPABILITY_RESPONSE = "CAPABILITY " + VERSION
        + " NAMESPACE" + " ACL"; //add as implemented

    private static final String LIST_WILD = "*";
    private static final String LIST_WILD_FLAT = "%";
    private static final char[] CTL = {};
    private static final String[] ATOM_SPECIALS
        = {"(", ")", "{", " ", LIST_WILD, LIST_WILD_FLAT,};

    private static final String AUTH_FAIL_MSG
        = "NO Command not authorized on this mailbox";
    private static final String BAD_LISTRIGHTS_MSG
        = "BAD Command should be <tag> <LISTRIGHTS> <mailbox> <identifier>";
    private static final String BAD_MYRIGHTS_MSG
        = "BAD Command should be <tag> <MYRIGHTS> <mailbox>";
    private static final String BAD_LIST_MSG
        = "BAD Command should be <tag> <LIST> <reference name> <mailbox>";
    private static final String BAD_LSUB_MSG
        = "BAD Command should be <tag> <LSUB> <reference name> <mailbox>";
    private static final String NO_NOTLOCAL_MSG
        = "NO Mailbox does not exist on this server";

    private Logger securityLogger;
    private MailServer mailServer;
    private UsersRepository users;
    private TimeScheduler scheduler;

    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private OutputStream outs;
    private String remoteHost;
    private String remoteIP;
    private String softwaretype  = "JAMES IMAP4rev1 Server " + Constants.SOFTWARE_VERSION;
    private int state;
    private String user;

    private IMAPSystem imapSystem;
    private Host imapHost;
    private String namespaceToken;
    private String currentNamespace = null;
    private String currentSeperator = null;
    private String commandRaw;

    //currentFolder holds the client-dependent absolute address of the current
    //folder, that is current Namespace and full mailbox hierarchy.
    private String currentFolder = null;
    private ACLMailbox currentMailbox = null;
    private boolean currentIsReadOnly = false;
    private boolean connectionClosed = false;
    private String tag;
    private boolean checkMailboxFlag = false;
    private int exists;
    private int recent;
    private List sequence;

    public void compose( final ComponentManager componentManager )
        throws ComponentException {

        mailServer = (MailServer)componentManager.
            lookup("org.apache.james.services.MailServer");
        users = (UsersRepository)componentManager.
            lookup("org.apache.james.services.UsersRepository");
        scheduler = (TimeScheduler)componentManager.
            lookup("org.apache.avalon.cornerstone.services.scheduler.TimeScheduler");
        imapSystem = (IMAPSystem)componentManager.
            lookup("org.apache.james.imapserver.IMAPSystem");
        imapHost = (Host)componentManager.
            lookup("org.apache.james.imapserver.Host");
    }

    public void initialize() throws Exception {
        getLogger().info("SingleThreadedConnectionHandler starting ...");
        namespaceToken = imapSystem.getNamespaceToken();
	securityLogger = getLogger().getChildLogger("security");
        getLogger().info("SingleThreadedConnectionHandler initialized");
    }

    /**
     * Handle a connection.
     * This handler is responsible for processing connections as they occur.
     *
     * @param connection the connection
{'fraction_non_alphanumeric': 0.08424737456242708, 'fraction_numerical': 0.0037339556592765463, 'mean_word_length': 3.7845180498697433, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142855', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3383, 'n_words': 1059}