    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.imap;
    019
    020/**
    021 * IMAPCommand stores IMAP command codes.
    022 */
    023public enum IMAPCommand {
    024    // These enums must either use the same name as the IMAP command
    025    // or must provide the correct string as the parameter.
    026
    027    // Commands valid in any state:
    028    /**
    029     * Valid in any state.
    030     */
    031    CAPABILITY(0),
    032
    033    /**
    034     * Valid in any state.
    035     */
    036    NOOP(0),
    037
    038    /**
    039     * Valid in any state.
    040     */
    041    LOGOUT(0),
    042
    043    // Commands valid in Not Authenticated state
    044    /**
    045     * Valid in Not Authenticated state
    046     */
    047    STARTTLS(0),
    048
    049    /**
    050     * Valid in Not Authenticated state
    051     */
    052    AUTHENTICATE(1),
    053
    054    /**
    055     * Valid in Not Authenticated state
    056     */
    057    LOGIN(2),
    058
    059    /**
    060     * Authenticate an IMAP connection using OAuth.
    061     */
    062    XOAUTH(1),
    063
    064    // commands valid in authenticated state
    065    /**
    066     * Valid in authenticated state.
    067     */
    068    SELECT(1),
    069
    070    /**
    071     * Valid in authenticated state.
    072     */
    073    EXAMINE(1),
    074
    075    /**
    076     * Valid in authenticated state.
    077     */
    078    CREATE(1),
    079
    080    /**
    081     * Valid in authenticated state.
    082     */
    083    DELETE(1),
    084
    085    /**
    086     * Valid in authenticated state.
    087     */
    088    RENAME(2),
    089    /**
    090     * Valid in authenticated state.
    091     */
    092    SUBSCRIBE(1),
    093    /**
    094     * Valid in authenticated state.
    095     */
    096    UNSUBSCRIBE(1),
    097    /**
    098     * Valid in authenticated state.
    099     */
    100    LIST(2),
    101    /**
    102     * Valid in authenticated state.
    103     */
    104    LSUB(2),
    105    /**
    106     * Valid in authenticated state.
    107     */
    108    STATUS(2), // P2 = list in ()
    109
    110    /**
    111     * Valid in authenticated state.
    112     */
    113    APPEND(2, 4), // mbox [(flags)] [date-time] literal
    114
    115    // commands valid in selected state (substate of authenticated)
    116    /**
    117     * Valid in selected state (substate of authenticated).
    118     */
    119    CHECK(0),
    120
    121    /**
    122     * Valid in selected state (substate of authenticated).
    123     */
    124    CLOSE(0),
    125
    126    /**
    127     * Valid in selected state (substate of authenticated).
    128     */
    129    EXPUNGE(0),
    130
    131    /**
    132     * Valid in selected state (substate of authenticated).
    133     */
    134    SEARCH(1, Integer.MAX_VALUE),
    135
    136    /**
    137     * Valid in selected state (substate of authenticated).
    138     */
    139    FETCH(2),
    140
    141    /**
    142     * Valid in selected state (substate of authenticated).
    143     */
    144    STORE(3),
    145
    146    /**
    147     * Valid in selected state (substate of authenticated).
    148     */
    149    COPY(2),
    150
    151    /**
    152     * Valid in selected state (substate of authenticated).
    153     */
    154    UID(2, Integer.MAX_VALUE);
    155
    156    /**
    157     * Gets the IMAP protocol string command corresponding to a command code.
    158     *
    159     * @param command the {@link IMAPCommand} whose command string is required. Must not be null.
    160     * @return The IMAP protocol string command corresponding to a command code.
    161     */
    162    public static String getCommand(final IMAPCommand command) {
    163        return command.getIMAPCommand();
    164    }
    165
    166    private final String imapCommand;
    167
    168    @SuppressWarnings("unused") // not yet used
    169    private final int minParamCount;
    170
    171    @SuppressWarnings("unused") // not yet used
    172    private final int maxParamCount;
    173
    174    IMAPCommand() {
    175        this(null);
    176    }
    177
    178    IMAPCommand(final int paramCount) {
    179        this(null, paramCount, paramCount);
    180    }
    181
    182    IMAPCommand(final int minCount, final int maxCount) {
    183        this(null, minCount, maxCount);
    184    }
    185
    186    IMAPCommand(final String name) {
    187        this(name, 0);
    188    }
    189
    190    IMAPCommand(final String name, final int paramCount) {
    191        this(name, paramCount, paramCount);
    192    }
    193
    194    IMAPCommand(final String name, final int minCount, final int maxCount) {
    195        this.imapCommand = name;
    196        this.minParamCount = minCount;
    197        this.maxParamCount = maxCount;
    198    }
    199
    200    /**
    201     * Gets the IMAP protocol string command for this command
    202     *
    203     * @return The IMAP protocol string command corresponding to this command
    204     */
    205    public String getIMAPCommand() {
    206        return imapCommand != null ? imapCommand : name();
    207    }
    208
    209}
    210