📄 fetchcommand.java
字号:
next = nextNonSpaceChar( request ); while ( next != ')' ) { fetch.addElement( getNextElement( request ) ); next = nextNonSpaceChar( request ); } consumeChar(request, ')'); return fetch; } private FetchElement getNextElement( ImapRequestLineReader request) throws ProtocolException { char next = nextCharInLine( request ); StringBuffer element = new StringBuffer(); while ( next != ' ' && next != '[' && next != ')' ) { element.append(next); request.consume(); next = nextCharInLine( request ); } String name = element.toString(); // Simple elements with no '[]' parameters. if ( next == ' ' || next == ')' ) { if ( "FAST".equalsIgnoreCase( name ) ) { return FetchElement.FAST; } if ( "FULL".equalsIgnoreCase( name ) ) { return FetchElement.FULL; } if ( "ALL".equalsIgnoreCase( name ) ) { return FetchElement.ALL; } if ( "FLAGS".equalsIgnoreCase( name ) ) { return FetchElement.FLAGS; } if ( "RFC822.SIZE".equalsIgnoreCase( name ) ) { return FetchElement.RFC822_SIZE; } if ( "ENVELOPE".equalsIgnoreCase( name ) ) { return FetchElement.ENVELOPE; } if ( "INTERNALDATE".equalsIgnoreCase( name ) ) { return FetchElement.INTERNALDATE; } if ( "BODY".equalsIgnoreCase( name ) ) { return FetchElement.BODY; } if ( "BODYSTRUCTURE".equalsIgnoreCase( name ) ) { return FetchElement.BODYSTRUCTURE; } if ( "UID".equalsIgnoreCase( name ) ) { return FetchElement.UID; } if ( "RFC822".equalsIgnoreCase( name ) ) { return new BodyFetchElement( "RFC822", "", false ); } if ( "RFC822.HEADER".equalsIgnoreCase( name ) ) { return new BodyFetchElement( "RFC822.HEADER", "HEADER", true ); } if ( "RFC822.TEXT".equalsIgnoreCase( name ) ) { return new BodyFetchElement( "RFC822.TEXT", "TEXT", false ); } else { throw new ProtocolException( "Invalid fetch attribute: " + name ); } } else { consumeChar( request, '[' ); StringBuffer sectionIdentifier = new StringBuffer(); next = nextCharInLine( request ); while ( next != ']' ) { sectionIdentifier.append( next ); request.consume(); next = nextCharInLine(request); } consumeChar( request, ']' ); String parameter = sectionIdentifier.toString(); if ( "BODY".equalsIgnoreCase( name ) ) { return new BodyFetchElement( "BODY[" + parameter + "]", parameter, false ); } if ( "BODY.PEEK".equalsIgnoreCase( name ) ) { return new BodyFetchElement( "BODY[" + parameter + "]", parameter, true ); } else { throw new ProtocolException( "Invalid fetch attibute: " + name + "[]" ); } } } private char nextCharInLine( ImapRequestLineReader request ) throws ProtocolException { char next = request.nextChar(); if ( next == '\r' || next == '\n' ) { throw new ProtocolException( "Unexpected end of line." ); } return next; } private char nextNonSpaceChar( ImapRequestLineReader request ) throws ProtocolException { char next = request.nextChar(); while ( next == ' ' ) { request.consume(); next = request.nextChar(); } return next; } } private static class FetchRequest { private ArrayList elements = new ArrayList(); List getElements() { return Collections.unmodifiableList( elements ); } void addElement( FetchElement element ) { if ( element == FetchElement.ALL ) { elements.add( FetchElement.FLAGS ); elements.add( FetchElement.INTERNALDATE ); elements.add( FetchElement.RFC822_SIZE ); elements.add( FetchElement.ENVELOPE ); } else if ( element == FetchElement.FAST ) { elements.add( FetchElement.FLAGS ); elements.add( FetchElement.INTERNALDATE ); elements.add( FetchElement.RFC822_SIZE ); } else if ( element == FetchElement.FULL ) { elements.add( FetchElement.FLAGS ); elements.add( FetchElement.INTERNALDATE ); elements.add( FetchElement.RFC822_SIZE ); elements.add( FetchElement.ENVELOPE ); elements.add( FetchElement.BODY ); } else { elements.add( element ); } } } private static class FetchElement { public static final FetchElement FLAGS = new FetchElement( "FLAGS" ); public static final FetchElement INTERNALDATE = new FetchElement( "INTERNALDATE" ); public static final FetchElement RFC822_SIZE= new FetchElement( "RFC822.SIZE" ); public static final FetchElement ENVELOPE = new FetchElement( "ENVELOPE" ); public static final FetchElement BODY = new FetchElement( "BODY" ); public static final FetchElement BODYSTRUCTURE = new FetchElement( "BODYSTRUCTURE" ); public static final FetchElement UID = new FetchElement( "UID" ); public static final FetchElement FAST = new FetchElement( "FAST" ); public static final FetchElement FULL = new FetchElement( "FULL" ); public static final FetchElement ALL = new FetchElement( "ALL" ); String name; protected FetchElement( String name ) { this.name = name; } public String getResponseName() { return this.name; } } private class BodyFetchElement extends FetchElement { private boolean peek; private String sectionIdentifier; public BodyFetchElement( String name, String sectionIdentifier, boolean peek ) { super( name ); this.sectionIdentifier = sectionIdentifier; this.peek = peek; } public String getParameters() { return this.sectionIdentifier; } public boolean isPeek() { return this.peek; } }}/*6.4.5. FETCH Command Arguments: message set message data item names Responses: untagged responses: FETCH Result: OK - fetch completed NO - fetch error: can't fetch that data BAD - command unknown or arguments invalid The FETCH command retrieves data associated with a message in the mailbox. The data items to be fetched can be either a single atom or a parenthesized list. The currently defined data items that can be fetched are: ALL Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE) BODY Non-extensible form of BODYSTRUCTURE. BODY[<section>]<<partial>> The text of a particular body section. The section specification is a set of zero or more part specifiers delimited by periods. A part specifier is either a part number or one of the following: HEADER, HEADER.FIELDS, HEADER.FIELDS.NOT, MIME, and TEXT. An empty section specification refers to the entire message, including the header. Every message has at least one part number. Non-[MIME-IMB] messages, and non-multipart [MIME-IMB] messages with no encapsulated message, only have a part 1. Multipart messages are assigned consecutive part numbers, as they occur in the message. If a particular part is of type message or multipart, its parts MUST be indicated by a period followed by the part number within that nested multipart part. A part of type MESSAGE/RFC822 also has nested part numbers, referring to parts of the MESSAGE part's body. The HEADER, HEADER.FIELDS, HEADER.FIELDS.NOT, and TEXT part specifiers can be the sole part specifier or can be prefixed by one or more numeric part specifiers, provided that the numeric part specifier refers to a part of type MESSAGE/RFC822. The MIME part specifier MUST be prefixed by one or more numeric part specifiers. The HEADER, HEADER.FIELDS, and HEADER.FIELDS.NOT part specifiers refer to the [RFC-822] header of the message or of an encapsulated [MIME-IMT] MESSAGE/RFC822 message. HEADER.FIELDS and HEADER.FIELDS.NOT are followed by a list of field-name (as defined in [RFC-822]) names, and return a subset of the header. The subset returned by HEADER.FIELDS contains only those header fields with a field-name that matches one of the names in the list; similarly, the subset returned by HEADER.FIELDS.NOT contains only the header fields with a non-matching field-name. The field-matching is case-insensitive but otherwise exact. In all cases, the delimiting blank line between the header and the body is always included. The MIME part specifier refers to the [MIME-IMB] header for this part. The TEXT part specifier refers to the text body of the message, omitting the [RFC-822] header. Here is an example of a complex message with some of its part specifiers: HEADER ([RFC-822] header of the message) TEXT MULTIPART/MIXED 1 TEXT/PLAIN 2 APPLICATION/OCTET-STREAM 3 MESSAGE/RFC822 3.HEADER ([RFC-822] header of the message) 3.TEXT ([RFC-822] text body of the message) 3.1 TEXT/PLAIN 3.2 APPLICATION/OCTET-STREAM 4 MULTIPART/MIXED 4.1 IMAGE/GIF 4.1.MIME ([MIME-IMB] header for the IMAGE/GIF) 4.2 MESSAGE/RFC822 4.2.HEADER ([RFC-822] header of the message) 4.2.TEXT ([RFC-822] text body of the message) 4.2.1 TEXT/PLAIN 4.2.2 MULTIPART/ALTERNATIVE 4.2.2.1 TEXT/PLAIN 4.2.2.2 TEXT/RICHTEXT It is possible to fetch a substring of the designated text. This is done by appending an open angle bracket ("<"), the octet position of the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -