1 17 18 package org.apache.james.imapserver.commands; 19 20 import org.apache.james.imapserver.ImapRequestLineReader; 21 import org.apache.james.imapserver.ImapResponse; 22 import org.apache.james.imapserver.ImapSession; 23 import org.apache.james.imapserver.ProtocolException; 24 import org.apache.james.imapserver.store.MessageFlags; 25 import org.apache.james.imapserver.store.ImapMailbox; 26 import org.apache.james.imapserver.store.SimpleImapMessage; 27 import org.apache.james.imapserver.store.MailboxException; 28 29 35 class StoreCommand extends SelectedStateCommand implements UidEnabledCommand 36 { 37 public static final String NAME = "STORE"; 38 public static final String ARGS = "<Message-set> ['+'|'-']FLAG[.SILENT] <flag-list>"; 39 40 private final StoreCommandParser parser = new StoreCommandParser(); 41 42 43 protected void doProcess( ImapRequestLineReader request, 44 ImapResponse response, 45 ImapSession session ) 46 throws ProtocolException, MailboxException 47 { 48 doProcess( request, response, session , false ); 49 } 50 51 public void doProcess( ImapRequestLineReader request, 52 ImapResponse response, 53 ImapSession session, 54 boolean useUids ) 55 throws ProtocolException, MailboxException 56 { 57 IdSet idSet = parser.set( request ); 58 StoreDirective directive = parser.storeDirective( request ); 59 MessageFlags flags = parser.flagList( request ); 60 parser.endLine( request ); 61 62 ImapMailbox mailbox = session.getSelected(); 63 long[] uids = mailbox.getMessageUids(); 64 for ( int i = 0; i < uids.length; i++ ) { 65 long uid = uids[i]; 66 int msn = mailbox.getMsn( uid ); 67 68 if ( ( useUids && idSet.includes( uid ) ) || 69 ( !useUids && idSet.includes( msn ) ) ) 70 { 71 SimpleImapMessage imapMessage = mailbox.getMessage( uid ); 72 storeFlags( imapMessage, directive, flags ); 73 mailbox.updateMessage( imapMessage ); 74 75 if ( ! directive.isSilent() ) { 76 StringBuffer out = new StringBuffer ( "FLAGS " ); 77 out.append( imapMessage.getFlags().format() ); 78 response.fetchResponse( msn, out.toString() ); 79 } 80 } 81 } 82 83 session.unsolicitedResponses( response ); 84 response.commandComplete( this ); 85 } 86 87 private void storeFlags( SimpleImapMessage imapMessage, StoreDirective directive, MessageFlags newFlags ) 88 { 89 MessageFlags messageFlags = imapMessage.getFlags(); 90 if ( directive.getSign() == 0 ) { 91 messageFlags.setAll( newFlags ); 92 } 93 else if ( directive.getSign() < 0 ) { 94 messageFlags.removeAll( newFlags ); 95 } 96 else if ( directive.getSign() > 0 ) { 97 messageFlags.addAll( newFlags ); 98 } 99 } 100 101 102 public String getName() 103 { 104 return NAME; 105 } 106 107 108 public String getArgSyntax() 109 { 110 return ARGS; 111 } 112 113 private class StoreCommandParser extends CommandParser 114 { 115 StoreDirective storeDirective( ImapRequestLineReader request ) throws ProtocolException 116 { 117 int sign = 0; 118 boolean silent = false; 119 120 char next = request.nextWordChar(); 121 if ( next == '+' ) { 122 sign = 1; 123 request.consume(); 124 } 125 else if ( next == '-' ) { 126 sign = -1; 127 request.consume(); 128 } 129 else { 130 sign = 0; 131 } 132 133 String directive = consumeWord( request, new NoopCharValidator() ); 134 if ( "FLAGS".equalsIgnoreCase( directive ) ) { 135 silent = false; 136 } 137 else if ( "FLAGS.SILENT".equalsIgnoreCase( directive ) ) { 138 silent = true; 139 } 140 else { 141 throw new ProtocolException( "Invalid Store Directive: '" + directive + "'" ); 142 } 143 return new StoreDirective( sign, silent ); 144 } 145 } 146 147 private class StoreDirective 148 { 149 private int sign; 150 private boolean silent; 151 152 public StoreDirective( int sign, boolean silent ) 153 { 154 this.sign = sign; 155 this.silent = silent; 156 } 157 158 public int getSign() 159 { 160 return sign; 161 } 162 163 public boolean isSilent() 164 { 165 return silent; 166 } 167 } 168 } 169 231 | Popular Tags |