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 *      http://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    XOAUTH(1),
060
061    // commands valid in authenticated state
062    /**
063     * Valid in authenticated state.
064     */
065    SELECT(1),
066
067    /**
068     * Valid in authenticated state.
069     */
070    EXAMINE(1),
071
072    /**
073     * Valid in authenticated state.
074     */
075    CREATE(1),
076
077    /**
078     * Valid in authenticated state.
079     */
080    DELETE(1),
081
082    /**
083     * Valid in authenticated state.
084     */
085    RENAME(2),
086    /**
087     * Valid in authenticated state.
088     */
089    SUBSCRIBE(1),
090    /**
091     * Valid in authenticated state.
092     */
093    UNSUBSCRIBE(1),
094    /**
095     * Valid in authenticated state.
096     */
097    LIST(2),
098    /**
099     * Valid in authenticated state.
100     */
101    LSUB(2),
102    /**
103     * Valid in authenticated state.
104     */
105    STATUS(2), // P2 = list in ()
106
107    /**
108     * Valid in authenticated state.
109     */
110    APPEND(2, 4), // mbox [(flags)] [date-time] literal
111
112    // commands valid in selected state (substate of authenticated)
113    /**
114     * Valid in selected state (substate of authenticated).
115     */
116    CHECK(0),
117
118    /**
119     * Valid in selected state (substate of authenticated).
120     */
121    CLOSE(0),
122
123    /**
124     * Valid in selected state (substate of authenticated).
125     */
126    EXPUNGE(0),
127
128    /**
129     * Valid in selected state (substate of authenticated).
130     */
131    SEARCH(1, Integer.MAX_VALUE),
132
133    /**
134     * Valid in selected state (substate of authenticated).
135     */
136    FETCH(2),
137
138    /**
139     * Valid in selected state (substate of authenticated).
140     */
141    STORE(3),
142
143    /**
144     * Valid in selected state (substate of authenticated).
145     */
146    COPY(2),
147
148    /**
149     * Valid in selected state (substate of authenticated).
150     */
151    UID(2, Integer.MAX_VALUE);
152
153    /**
154     * Gets the IMAP protocol string command corresponding to a command code.
155     *
156     * @param command the {@link IMAPCommand} whose command string is required. Must not be null.
157     * @return The IMAP protocol string command corresponding to a command code.
158     */
159    public static final String getCommand(final IMAPCommand command) {
160        return command.getIMAPCommand();
161    }
162
163    private final String imapCommand;
164
165    @SuppressWarnings("unused") // not yet used
166    private final int minParamCount;
167
168    @SuppressWarnings("unused") // not yet used
169    private final int maxParamCount;
170
171    IMAPCommand() {
172        this(null);
173    }
174
175    IMAPCommand(final int paramCount) {
176        this(null, paramCount, paramCount);
177    }
178
179    IMAPCommand(final int minCount, final int maxCount) {
180        this(null, minCount, maxCount);
181    }
182
183    IMAPCommand(final String name) {
184        this(name, 0);
185    }
186
187    IMAPCommand(final String name, final int paramCount) {
188        this(name, paramCount, paramCount);
189    }
190
191    IMAPCommand(final String name, final int minCount, final int maxCount) {
192        this.imapCommand = name;
193        this.minParamCount = minCount;
194        this.maxParamCount = maxCount;
195    }
196
197    /**
198     * Gets the IMAP protocol string command for this command
199     *
200     * @return The IMAP protocol string command corresponding to this command
201     */
202    public String getIMAPCommand() {
203        return imapCommand != null ? imapCommand : name();
204    }
205
206}
207