Skip to contentPackage: MailboxInfo
MailboxInfo
Coverage
1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.imap.protocol;
18:
19: import java.util.List;
20: import java.util.ArrayList;
21:
22: import jakarta.mail.Flags;
23:
24: import org.eclipse.angus.mail.iap.*;
25: import org.eclipse.angus.mail.iap.ParsingException;
26: import org.eclipse.angus.mail.iap.Response;
27:
28: /**
29: * Information collected when opening a mailbox.
30: *
31: * @author John Mani
32: * @author Bill Shannon
33: */
34:
35: public class MailboxInfo {
36: /** The available flags. */
37: public Flags availableFlags = null;
38: /** The permanent flags. */
39: public Flags permanentFlags = null;
40: /** The total number of messages. */
41: public int total = -1;
42: /** The number of recent messages. */
43: public int recent = -1;
44: /** The first unseen message. */
45: public int first = -1;
46: /** The UIDVALIDITY. */
47: public long uidvalidity = -1;
48: /** The next UID value to be assigned. */
49: public long uidnext = -1;
50: /** UIDs are not sticky. */
51: public boolean uidNotSticky = false;        // RFC 4315
52: /** The highest MODSEQ value. */
53: public long highestmodseq = -1;        // RFC 4551 - CONDSTORE
54: /** Folder.READ_WRITE or Folder.READ_ONLY, set by IMAPProtocol. */
55: public int mode;
56: /** VANISHED or FETCH responses received while opening the mailbox. */
57: public List<IMAPResponse> responses;
58:
59: /**
60: * Collect the information about this mailbox from the
61: * responses to a SELECT or EXAMINE.
62: *
63: * @param        r        the responses
64: * @exception ParsingException for errors parsing the responses
65: */
66: public MailboxInfo(Response[] r) throws ParsingException {
67:•        for (int i = 0; i < r.length; i++) {
68:•         if (r[i] == null || !(r[i] instanceof IMAPResponse))
69:                 continue;
70:
71:          IMAPResponse ir = (IMAPResponse)r[i];
72:
73:•         if (ir.keyEquals("EXISTS")) {
74:                 total = ir.getNumber();
75:                 r[i] = null; // remove this response
76:•         } else if (ir.keyEquals("RECENT")) {
77:                 recent = ir.getNumber();
78:                 r[i] = null; // remove this response
79:•         } else if (ir.keyEquals("FLAGS")) {
80:                 availableFlags = new FLAGS(ir);
81:                 r[i] = null; // remove this response
82:•         } else if (ir.keyEquals("VANISHED")) {
83:•                if (responses == null)
84:                  responses = new ArrayList<>();
85:                 responses.add(ir);
86:                 r[i] = null; // remove this response
87:•         } else if (ir.keyEquals("FETCH")) {
88:•                if (responses == null)
89:                  responses = new ArrayList<>();
90:                 responses.add(ir);
91:                 r[i] = null; // remove this response
92:•         } else if (ir.isUnTagged() && ir.isOK()) {
93:                 /*
94:                  * should be one of:
95:                  *         * OK [UNSEEN 12]
96:                  *         * OK [UIDVALIDITY 3857529045]
97:                  *         * OK [PERMANENTFLAGS (\Deleted)]
98:                  *         * OK [UIDNEXT 44]
99:                  *         * OK [HIGHESTMODSEQ 103]
100:                  */
101:                 ir.skipSpaces();
102:
103:•                if (ir.readByte() != '[') {        // huh ???
104:                  ir.reset();
105:                  continue;
106:                 }
107:
108:                 boolean handled = true;
109:                 String s = ir.readAtom();
110:•                if (s.equalsIgnoreCase("UNSEEN"))
111:                  first = ir.readNumber();
112:•                else if (s.equalsIgnoreCase("UIDVALIDITY"))
113:                  uidvalidity = ir.readLong();
114:•                else if (s.equalsIgnoreCase("PERMANENTFLAGS"))
115:                  permanentFlags = new FLAGS(ir);
116:•                else if (s.equalsIgnoreCase("UIDNEXT"))
117:                  uidnext = ir.readLong();
118:•                else if (s.equalsIgnoreCase("HIGHESTMODSEQ"))
119:                  highestmodseq = ir.readLong();
120:                 else
121:                  handled = false;        // possibly an ALERT
122:
123:•                if (handled)
124:                  r[i] = null; // remove this response
125:                 else
126:                  ir.reset();        // so ALERT can be read
127:•         } else if (ir.isUnTagged() && ir.isNO()) {
128:                 /*
129:                  * should be one of:
130:                  *         * NO [UIDNOTSTICKY]
131:                  */
132:                 ir.skipSpaces();
133:
134:•                if (ir.readByte() != '[') {        // huh ???
135:                  ir.reset();
136:                  continue;
137:                 }
138:
139:                 boolean handled = true;
140:                 String s = ir.readAtom();
141:•                if (s.equalsIgnoreCase("UIDNOTSTICKY"))
142:                  uidNotSticky = true;
143:                 else
144:                  handled = false;        // possibly an ALERT
145:
146:•                if (handled)
147:                  r[i] = null; // remove this response
148:                 else
149:                  ir.reset();        // so ALERT can be read
150:          }
151:         }
152:
153:         /*
154:          * The PERMANENTFLAGS response code is optional, and if
155:          * not present implies that all flags in the required FLAGS
156:          * response can be changed permanently.
157:          */
158:•        if (permanentFlags == null) {
159:•         if (availableFlags != null)
160:                 permanentFlags = new Flags(availableFlags);
161:          else
162:                 permanentFlags = new Flags();
163:         }
164: }
165: }