View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.imap.cmd;
14  
15  import java.io.IOException;
16  import javax.mail.Folder;
17  import javax.mail.MessagingException;
18  import javax.mail.UIDFolder;
19  import javax.mail.internet.MimeMessage;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import org.abstracthorizon.mercury.common.command.CommandException;
25  import org.abstracthorizon.mercury.imap.IMAPSession;
26  import org.abstracthorizon.mercury.imap.response.FetchResponse;
27  import org.abstracthorizon.mercury.imap.util.ComposedSequence;
28  import org.abstracthorizon.mercury.imap.util.FlagUtilities;
29  import org.abstracthorizon.mercury.imap.util.MessageUtilities;
30  import org.abstracthorizon.mercury.imap.util.ParserException;
31  import org.abstracthorizon.mercury.imap.util.IMAPScanner;
32  import org.abstracthorizon.mercury.imap.util.section.Flags;
33  
34  /**
35   * Store IMAP command
36   *
37   * @author Daniel Sendula
38   */
39  public class Store extends UIDCommand {
40  
41      /** Logger */
42      protected static final Logger logger = LoggerFactory.getLogger(Store.class);
43  
44      /** Flags */
45      protected Flags flags = new Flags();
46  
47      /**
48       * Constructor
49       * @param mnemonic mnemonic
50       */
51      public Store(String mnemonic) {
52          super(mnemonic);
53      }
54  
55      /**
56       * Executes the command
57       * @param session
58       * @throws ParserException
59       * @throws MessagingException
60       * @throws CommandException
61       * @throws IOException
62       */
63      protected void execute(IMAPSession session) throws ParserException, MessagingException, CommandException, IOException {
64          IMAPScanner scanner = session.getScanner();
65          ComposedSequence sequenceSet = new ComposedSequence();
66          if (!scanner.sequence_set(sequenceSet)) {
67              throw new ParserException("<sequence-set>");
68          }
69          if (!scanner.is_char(' ')) {
70              throw new ParserException("<SP>");
71          }
72  
73          flags = new Flags();
74          if (!store_atts(scanner, flags)) {
75              throw new ParserException("<store-atts>");
76          }
77          checkEOL(session);
78  
79          Folder f = session.getSelectedFolder();
80  
81          logger.debug("Prepare to process "+sequenceSet);
82          MessageUtilities.sequenceIterator(session, this, f, sequenceSet, asuid);
83          sendOK(session);
84      }
85  
86      /**
87       * Processes the message
88       * @param session session
89       * @param m message
90       * @throws MessagingException
91       * @throws IOException
92       */
93      public void process(IMAPSession session, MimeMessage m) throws MessagingException, IOException {
94          logger.debug("Processing "+((UIDFolder)m.getFolder()).getUID(m));
95          int msgNo = m.getMessageNumber();
96          FetchResponse response = null;
97          if (!flags.silent) {
98              response = new FetchResponse(session, msgNo);
99              response.append('(');
100         }
101         if (!flags.plus && !flags.minus) {
102             javax.mail.Flags flgs = m.getFlags();
103             m.setFlags(flgs, false);
104             m.setFlags(flags.flags, true);
105             logger.debug("Removed "+flgs+" from message UID "+((UIDFolder)m.getFolder()).getUID(m));
106             logger.debug("Set "+flags.flags+" to same message UID "+((UIDFolder)m.getFolder()).getUID(m));
107         } else {
108             m.setFlags(flags.flags, flags.plus);
109             if (flags.plus) {
110                 logger.debug("Set "+flags.flags+" from message UID "+((UIDFolder)m.getFolder()).getUID(m));
111             } else {
112                 logger.debug("Removed "+flags.flags+" from message UID "+((UIDFolder)m.getFolder()).getUID(m));
113             }
114         }
115         if (!flags.silent) {
116             response.append("FLAGS (").append(FlagUtilities.toString(m.getFlags())).append(')');
117             response.append(')');
118             response.submit();
119         }
120     }
121 
122     /**
123      * Scans for store attributes
124      * @param scanner scanner
125      * @param flags flags
126      * @return <code>true</code> if processing is successful
127      * @throws ParserException
128      * @throws IOException
129      */
130     public static boolean store_atts(IMAPScanner scanner, Flags flags) throws ParserException, IOException {
131 
132         if (scanner.is_char('+')) {
133             flags.plus = true;
134         } else if (scanner.is_char('-')) {
135             flags.minus = true;
136         }
137         if (!scanner.keyword("FLAGS")) {
138             if (flags.plus || flags.minus) {
139                 throw new ParserException("'FLAGS'");
140             } else {
141                 return false;
142             }
143         }
144         if (scanner.keyword(".SILENT")) {
145             flags.silent = true;
146         }
147         if (!scanner.is_char(' ')) {
148             throw new ParserException("<SP>");
149         }
150         if (scanner.flag_list(flags.flags)) {
151             return true;
152         }
153         if (scanner.flag(flags.flags)) {
154             while (scanner.is_char(' ')) {
155                 if (!scanner.flag(flags.flags)) {
156                     throw new ParserException("<flags>");
157                 }
158             }
159             return true;
160         }
161         return false;
162     }
163 }