KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > CommandFetch


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver;
19
20 import org.apache.james.imapserver.AccessControlException;
21 import org.apache.james.imapserver.AuthorizationException;
22 import org.apache.james.core.MimeMessageWrapper;
23 import org.apache.james.imapserver.commands.ImapCommand;
24
25 import javax.mail.MessagingException;
26 import javax.mail.internet.InternetHeaders;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.PrintWriter;
30 import java.util.*;
31
32 /**
33  * Implements the IMAP FETCH command for a given ImapRequestImpl.
34  *
35  * References: rfc 2060, rfc 2193, rfc 2221
36  * @version 0.2 on 04 Aug 2002
37  */

38 public class CommandFetch
39     extends BaseCommand implements ImapCommand
40 {
41
42     //mainly to switch on stack traces and catch responses;
43
private static final boolean DEEP_DEBUG = true;
44
45     private static final String OK = "OK";
46     private static final String NO = "NO";
47     private static final String BAD = "BAD";
48     private static final String UNTAGGED = "*";
49     private static final String SP = " ";
50
51     private StringTokenizer commandLine;
52     private boolean useUIDs;
53     private ACLMailbox currentMailbox;
54     private String commandRaw;
55     private PrintWriter out;
56     private OutputStream outs;
57     private String tag;
58     private String user;
59     private SingleThreadedConnectionHandler caller;
60     private String currentFolder;
61
62     public boolean validForState( ImapSessionState state )
63     {
64         return ( state == ImapSessionState.SELECTED );
65     }
66
67     public boolean process( ImapRequest request, ImapSession session )
68     {
69         setRequest( request );
70     StringTokenizer txt = request.getCommandLine();
71
72 /* System.out.println("CommandFetch.process: #args="+txt.countTokens());
73         while (txt.hasMoreTokens()) {
74             System.out.println("CommandFetch.process: arg='"+txt.nextToken()+"'");
75         }
76 */

77    // ((ImapRequestImpl)request).setCommandLine(new java.util.StringTokenizer(request.getCommandRaw()));
78
if ( request.arguments() < 2 ) {
79             session.badResponse( "#args="+request.arguments()+" '"+request.getCommandLine().nextToken()+"', '"+request.getCommandLine().nextToken()+"' Command should be <tag> <FETCH> <message set> <message data item names>" );
80             return true;
81         }
82         service();
83         return true;
84     }
85
86     /**
87      * Debugging method - will probably disappear
88      */

89     public void setRequest(ImapRequest request) {
90         commandLine = request.getCommandLine();
91         useUIDs = request.useUIDs();
92         currentMailbox = request.getCurrentMailbox();
93         System.out.println("currentMailbox="+((currentMailbox!=null)?currentMailbox.getClass().getName():"null"));
94         commandRaw = request.getCommandRaw();
95         tag = request.getTag();
96         currentFolder = request.getCurrentFolder();
97
98         caller = request.getCaller();
99         out = caller.getPrintWriter();
100         outs = caller.getOutputStream();
101         user = caller.getUser();
102     }
103
104     /**
105      * Implements IMAP fetch commands given an ImapRequestImpl.
106      * This implementation attempts to satisfy the fetch command with the
107      * smallest objects deserialized from storage.
108      * <p>Warning - maybecome service(ImapRequestImpl request)
109      * <p>Not yet complete - no partial (octet-counted or sub-parts) fetches.
110      */

111     public void service() {
112         // decode the message set
113
List set;
114         List uidsList = null;
115         String setArg = commandLine.nextToken();
116         if (useUIDs) {
117             uidsList = currentMailbox.listUIDs(user);
118             set = decodeUIDSet(setArg, uidsList);
119         } else {
120             set = decodeSet(setArg, currentMailbox.getExists());
121         }
122         if (DEEP_DEBUG) {
123             getLogger().debug("Fetching message set of size: " + set.size());
124         }
125         String firstFetchArg = commandLine.nextToken();
126         int pos = commandRaw.indexOf(firstFetchArg);
127         //int pos = commandRaw.indexOf(setArg) + setArg.length() + 1;
128
String fetchAttrsRaw = null;
129         if (firstFetchArg.startsWith("(")) { //paranthesised fetch attrs
130
fetchAttrsRaw = commandRaw.substring(pos + 1, commandRaw.lastIndexOf(")"));
131         } else {
132             fetchAttrsRaw = commandRaw.substring(pos);
133         }
134
135         if (DEEP_DEBUG) {
136             System.out.println("Found fetchAttrsRaw: " + fetchAttrsRaw);
137             getLogger().debug("Found fetchAttrsRaw: " + fetchAttrsRaw);
138         }
139         // decode the fetch attributes
140
List fetchAttrs = new ArrayList();
141         StringTokenizer fetchTokens = new StringTokenizer(fetchAttrsRaw);
142         while (fetchTokens.hasMoreTokens()) {
143             String attr = fetchTokens.nextToken();
144             if (attr.indexOf("(") == -1 ) { //not the start of a fields list
145
fetchAttrs.add(attr);
146             } else {
147                 StringBuffer attrWithFields = new StringBuffer();
148                 attrWithFields.append(fetchAttrs.remove(fetchAttrs.size() -1));
149                 attrWithFields.append(" " + attr);
150                 boolean endOfFields = false;
151                 while (! endOfFields && fetchTokens.hasMoreTokens()) {
152                     String field = fetchTokens.nextToken();
153                     attrWithFields.append(" " + field);
154                     if (field.indexOf(")") != -1) {
155                         endOfFields = true;
156                     }
157                 }
158                 fetchAttrs.add(attrWithFields.toString());
159             }
160         }
161
162         fetchAttrs = convertMacroCommands( fetchAttrs );
163         try {
164             for (int i = 0; i < set.size(); i++) {
165                 Integer uidObject = null;
166                 int uid = 0;
167                 int msn = 0;
168                 if (useUIDs) {
169                     System.out.println("USE UIDS");
170                     uidObject = (Integer)set.get(i);
171                     uid = uidObject.intValue();
172                     msn = uidsList.indexOf(uidObject) + 1;
173                 } else {
174                     msn = ((Integer)set.get(i)).intValue();
175                 }
176                 MessageAttributes attrs = null;
177                 String flags = null;
178                 //EnhancedMimeMessage msg = null;
179
MimeMessageWrapper msg = null;
180                 String response = UNTAGGED + SP + msn + SP + "FETCH (";
181                 boolean responseAdded = false;
182                 Iterator it = fetchAttrs.iterator();
183                 while(it.hasNext()) {
184                     String arg = (String) it.next();
185                     // commands that only need flags object
186
if (arg.equalsIgnoreCase("FLAGS")) {
187                         if (flags == null) {
188                             if (useUIDs) {
189                                 System.out.println("TRYING UIDFLAGS"+uid);
190                                 flags = currentMailbox.getFlagsUID(uid, user);
191                             } else {
192                                 System.out.println("TRYING MSNFLAGS"+msn);
193                                 flags = currentMailbox.getFlags(msn, user);
194                             }
195                         }
196                         if (flags == null) { // bad
197
//out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message flags.");
198
//System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message flags.");
199
//return;
200
}
201                         if (responseAdded) {
202                             response += SP + "FLAGS " + flags ;
203                         } else {
204                             response += "FLAGS " + flags ;
205                             responseAdded = true;
206                         }
207                     }
208                     // command that only need MessageAttributes object
209
else if (arg.equalsIgnoreCase("INTERNALDATE")) {
210                         System.out.println("Starting INTERNALDATE");
211                         if (attrs == null) {
212                             if (useUIDs) {
213                                 attrs = currentMailbox.getMessageAttributesUID(uid, user);
214                             } else {
215                                 attrs = currentMailbox.getMessageAttributes(msn, user);
216                             }
217                         }
218                         if (attrs == null) { // bad
219
out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes.");
220                             System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes.");
221                             getLogger().error("Retrieved null attributes for msn:" + msn);
222                             return;
223                         }
224                         if (responseAdded) {
225                             response += SP + "INTERNALDATE \""
226                                 + attrs.getInternalDateAsString() + "\"" ;
227                         } else {
228                             response += "INTERNALDATE \""
229                                 + attrs.getInternalDateAsString() + "\"" ;
230                             responseAdded = true;
231                         }
232                     } else if (arg.equalsIgnoreCase("RFC822.SIZE")) {
233                         System.out.println("Starting RFC822.SIZE");
234                         if (attrs == null) {
235                             if (useUIDs) {
236                                 attrs = currentMailbox.getMessageAttributesUID(uid, user);
237                             } else {
238                                 attrs = currentMailbox.getMessageAttributes(msn, user);
239                             }
240                         }
241                         if (attrs == null) { // bad
242
out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes.");
243                             System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes.");
244                             getLogger().error("Retrieved null attributes for msn:" + msn);
245                             return;
246                         }
247                         if (responseAdded) {
248                             response += SP + "RFC822.SIZE " + attrs.getSize();
249                         } else {
250                             response += "RFC822.SIZE " + attrs.getSize();
251                             responseAdded = true;
252                         }
253                     } else if (arg.equalsIgnoreCase("ENVELOPE")) {
254                         System.out.println("Starting ENVELOPE");
255                         if (attrs == null) {
256                             if (useUIDs) {
257                                 attrs = currentMailbox.getMessageAttributesUID(uid, user);
258                             } else {
259                                 attrs = currentMailbox.getMessageAttributes(msn, user);
260                             }
261                         }
262                         if (attrs == null) { // bad
263
out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes.");
264                             System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes.");
265                             getLogger().error("Retrieved null attributes for msn:" + msn);
266                             return;
267                         }
268                         if (responseAdded) {
269                             response += SP + "ENVELOPE " + attrs.getEnvelope();
270                         } else {
271                             response += "ENVELOPE " + attrs.getEnvelope();
272                             responseAdded = true;
273                         }
274                     } else if (arg.equalsIgnoreCase("BODY")) {
275                        System.out.println("CommandFetch BODY start");
276                        if (attrs == null) {
277                         System.out.println("CommandFetch BODY fetching attrs");
278                             if (useUIDs) {
279                                 attrs = currentMailbox.getMessageAttributesUID(uid, user);
280                                 System.out.println("currentMailbox.getMessageAttributesUID("+uid+","+user+")");
281                             } else {
282                                 attrs = currentMailbox.getMessageAttributes(msn, user);
283                                 System.out.println("currentMailbox.getMessageAttributes("+msn+","+user+")");
284                             }
285                             System.out.println("attrs="+((attrs!=null)?attrs.getClass().getName():"null"));
286
287                         }
288                            System.out.println("CommandFetch BODY attrs="+attrs);
289                             System.out.println("CommandFetch BODY attrs.getUID: "+attrs.getUID());
290                         if (attrs == null) { // bad
291
out.println(tag + SP + msn + SP + NO + "Error retrieving message attributes.");
292                             getLogger().error("Retrieved null attributes for msn:" + msn);
293                             return;
294                         }
295                         if (responseAdded) {
296                             response += SP + "BODY " + attrs.getBodyStructure();
297                         } else {
298                             response += "BODY " + attrs.getBodyStructure();
299                             responseAdded = true;
300                         }
301                     } else if (arg.equalsIgnoreCase("BODYSTRUCTURE")) {
302                         System.out.println("Starting BODYSTRUCTURE");
303                         if (attrs == null) {
304                             if (useUIDs) {
305                                 attrs = currentMailbox.getMessageAttributesUID(uid, user);
306                             } else {
307                                 attrs = currentMailbox.getMessageAttributes(msn, user);
308                             }
309                         }
310                         if (attrs == null) { // bad
311
out.println(tag + SP + msn + SP + NO + "Error retrieving message attributes.");
312                             getLogger().error("Retrieved null attributes for msn:" + msn);
313                             return;
314                         }
315                         if (responseAdded) {
316                             response += SP + "BODYSTRUCTURE "+ attrs.getBodyStructure();
317                         } else {
318                             response += "BODYSTRUCTURE "+ attrs.getBodyStructure();
319                             responseAdded = true;
320                         }
321                     } else if (arg.equalsIgnoreCase("UID")) {
322                         System.out.println("CommandFetch UID start");
323                         if (!useUIDs || fetchAttrs.size() == 1){
324                             System.out.println("CommandFetch.!useUIDs");
325                             if (attrs == null) {
326                                 System.out.println("CommandFetch UID fetching attrs: "+attrs);
327                                 attrs = currentMailbox.getMessageAttributes(msn, user);
328                             }
329                             uid = attrs.getUID();
330                             System.out.println("CommandFetch UID attrs: "+attrs);
331                             System.out.println("CommandFetch UID attrs.getUID: "+attrs.getUID());
332                             if (attrs == null) { // bad
333
out.println(tag + SP + msn + SP + NO + "Error retrieving message attributes.");
334                                 getLogger().error("Retrieved null attributes for msn:" + msn);
335                                 return;
336                             }
337                             System.out.println("CommandFetch UID printing UID: "+uid);
338
339                             if (responseAdded) {
340                                 response += SP + "UID "+ uid;
341                             } else {
342                                 response += "UID "+ uid;
343                                 responseAdded = true;
344                             }
345                         } // don't duplicate on UID FETCH requests
346
System.out.println("CommandFetch UID end");
347                     }
348                     // commands that can be satisifed with just top-level headers of message and flags
349
else if (arg.equalsIgnoreCase("BODY[HEADER]")
350                             || arg.equalsIgnoreCase("BODY.PEEK[HEADER]")
351                             || "RFC822.HEADER".equalsIgnoreCase(arg)) {
352                         System.out.println("Starting BODY[HEADER]");
353                         if (responseAdded) { // unlikely
354
if (useUIDs) {
355                                 response += " UID " + uid + ")";
356                             } else {
357                                 response += ")";
358                             }
359                             System.out.println(response);
360                             out.println(response);
361                             getLogger().debug("Sending: " + response);
362                         }
363                         InternetHeaders headers = null;
364                         if (useUIDs) {
365                             headers = currentMailbox.getInternetHeadersUID(uid, user);
366                         } else {
367                             headers = currentMailbox.getInternetHeaders(msn, user);
368                         }
369                         if (headers == null) { // bad
370
System.out.println(tag + SP + msn + SP + NO + "Error retrieving message1.");
371                             out.println(tag + SP + msn + SP + NO + "Error retrieving message1.");
372                             getLogger().error("Retrieved null headers for msn:" + msn);
373                             return;
374                         }
375                         if (flags == null) {
376                             if (useUIDs) {
377                                 flags = currentMailbox.getFlagsUID(uid, user);
378                             } else {
379                                 flags = currentMailbox.getFlags(msn, user);
380                             }
381                         }
382                         response = UNTAGGED + SP + msn + SP + "FETCH (";
383                         response += "BODY[HEADER] ";
384                         
385                         Enumeration enum = headers.getAllHeaderLines();
386                         List lines = new ArrayList();
387                         int count = 0;
388                         while (enum.hasMoreElements()) {
389                             String line = (String)enum.nextElement();
390                             count += line.length() + 2;
391                             lines.add(line);
392                         }
393                         response += "{" + (count + 2) + "}";
394                         out.println(response);
395                         System.out.println(response);
396                         getLogger().debug("Sending: " + response);
397                         Iterator lit = lines.iterator();
398                         while (lit.hasNext()) {
399                             String line = (String)lit.next();
400                             out.println(line);
401                             System.out.println(line);
402                             getLogger().debug("Sending: " + line);
403                         }
404                         out.println();
405                         System.out.println();
406                         getLogger().debug("Sending blank line");
407                         if (useUIDs) {
408                             out.println( " UID " + uid + ")");
409                             System.out.println( " UID " + uid + ")");
410                             getLogger().debug("Sending: UID " + uid + ")");
411                         } else {
412                             out.println( ")" );
413                             System.out.println( ")" );
414                             getLogger().debug("Sending: )");
415                         }
416                         if (! arg.equalsIgnoreCase("BODY.PEEK[HEADER]")) {
417                             try { // around setFlags()
418
if (flags.indexOf("Seen") == -1 ) {
419                                     String newflags;
420                                     if (useUIDs) {
421                                         currentMailbox.setFlagsUID(uid, user, "+flags (\\Seen)");
422                                         newflags = currentMailbox.getFlagsUID(uid, user);
423                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
424                                                     + newflags + " UID " + uid +")");
425                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
426                                                     + newflags + " UID " + uid +")");
427                                     } else {
428                                         currentMailbox.setFlags(msn, user, "+flags (\\Seen)");
429                                         newflags = currentMailbox.getFlags(msn, user);
430                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
431                                                     + newflags + ")");
432                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
433                                                     + newflags + ")");
434                                     }
435                                 }
436                             } catch (AccessControlException ace) {
437                                 getLogger().error("Exception storing flags for message: " + ace);
438                             } catch (AuthorizationException aze) {
439                                 getLogger().error("Exception storing flags for message: " + aze);
440                             } catch (Exception e) {
441                                 getLogger().error("Unanticipated exception storing flags for message: " + e);
442                             }
443                         }
444                         response = UNTAGGED + SP + msn + SP + "FETCH (";
445                         responseAdded = false;
446                     } else if (arg.toUpperCase().startsWith("BODY[HEADER.FIELDS")
447                                || arg.toUpperCase().startsWith("BODY.PEEK[HEADER.FIELDS")
448                                || arg.toUpperCase().startsWith("(BODY.PEEK[HEADER.FIELDS")){
449                         System.out.println("Starting BODY[HEADER.FIELDS]");
450                         if (responseAdded) {
451                             if (useUIDs) {
452                                 response += " UID " + uid + ")";
453                             } else {
454                                 response += ")";
455                             }
456                             out.println(response);
457                             System.out.println(response);
458                             getLogger().debug("Sending: " + response);
459                         }
460                         InternetHeaders headers = null;
461                         if (useUIDs) {
462                             headers = currentMailbox.getInternetHeadersUID(uid, user);
463                         } else {
464                             headers = currentMailbox.getInternetHeaders(msn, user);
465                         }
466                         if (headers == null) { // bad
467
out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message2.");
468                             System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message2.");
469                             getLogger().error("Retrieved null headers for msn:" + msn);
470                             return;
471                         }
472                         if (flags == null) {
473                             if (useUIDs) {
474                                 flags = currentMailbox.getFlagsUID(uid, user);
475                             } else {
476                                 flags = currentMailbox.getFlags(msn, user);
477                             }
478                         }
479                         boolean not = (commandRaw.toUpperCase().indexOf("HEADER.FIELDS.NOT") != -1);
480                         boolean peek = (commandRaw.toUpperCase().indexOf("PEEK") != -1);
481                         response = UNTAGGED + SP + msn + SP + "FETCH (BODY" ;
482                         if (peek) {response += ".PEEK";}
483                         if (not) {
484                             response += "[HEADER.FIELDS.NOT (";
485                         } else {
486                             response += "[HEADER.FIELDS (";
487                         }
488                         responseAdded = false;
489                         //int h = commandRaw.indexOf("[");
490
int left = arg.indexOf("(");
491                         int right = arg.indexOf(")");
492                         String fieldsRequested = arg.substring(left + 1, right);
493                         response += fieldsRequested + ")] ";
494                         ArrayList fields = new ArrayList();
495                         if (fieldsRequested.indexOf(" ") == -1) { //only one field
496
fields.add(fieldsRequested);
497                         } else {
498                             StringTokenizer tok = new StringTokenizer(fieldsRequested);
499                             while (tok.hasMoreTokens()) {
500                                 fields.add((String)tok.nextToken());
501                             }
502                         }
503                         Iterator it2 = fields.iterator();
504                         while (it2.hasNext()) {
505                             getLogger().debug("request for field: " + (String)it2.next());
506                         }
507                         String[] names = (String[])fields.toArray(new String[fields.size()]);
508                         Enumeration enum = null;
509                         if (not) {
510                             enum = headers.getNonMatchingHeaderLines(names);
511                         } else {
512                             enum = headers.getMatchingHeaderLines(names);
513                         }
514                         List lines = new ArrayList();
515                         int count = 0;
516                         while (enum.hasMoreElements()) {
517                             String line = (String)enum.nextElement();
518                             count += line.length() + 2;
519                             lines.add(line);
520                         }
521                         response += "{" + (count + 2) + "}";
522                         out.println(response);
523                         System.out.println(response);
524                         getLogger().debug("Sending: " + response);
525                         Iterator lit = lines.iterator();
526                         while (lit.hasNext()) {
527                             String line = (String)lit.next();
528                             out.println(line);
529                             System.out.println(line);
530                             getLogger().debug("Sending: " + line);
531                         }
532                         out.println();
533                         System.out.println();
534                         if (useUIDs) {
535                             out.println( " UID " + uid + ")");
536                             System.out.println( " UID " + uid + ")");
537                         } else {
538                             out.println(")");
539                             System.out.println(")");
540                         }
541                         if (! peek) {
542                             if (flags.indexOf("Seen") == -1 ) {
543                                 try {
544                                     String newflags;
545                                     if (useUIDs) {
546                                         currentMailbox.setFlagsUID(uid, user, "+flags (\\Seen)");
547                                         newflags = currentMailbox.getFlagsUID(uid, user);
548                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
549                                                     + newflags + " UID " + uid +")");
550                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
551                                                     + newflags + " UID " + uid +")");
552                                     } else {
553                                         currentMailbox.setFlags(msn, user, "+flags (\\Seen)");
554                                         newflags = currentMailbox.getFlags(msn, user);
555                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
556                                                     + newflags + ")");
557                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
558                                                     + newflags + ")");
559                                     }
560                                 } catch (AccessControlException ace) {
561                                     getLogger().error("Exception storing flags for message: " + ace);
562                                 } catch (AuthorizationException aze) {
563                                     getLogger().error("Exception storing flags for message: " + aze);
564                                 } catch (Exception e) {
565                                     getLogger().error("Unanticipated exception storing flags for message: " + e);
566                                 }
567                             }
568                         }
569                         response = UNTAGGED + SP + msn + SP + "FETCH (";
570                         responseAdded = false;
571                     }
572                     // Calls to underlying MimeMessage
573
else if (arg.equalsIgnoreCase("RFC822")
574                              || arg.equalsIgnoreCase("BODY[]")
575                              || arg.equalsIgnoreCase("BODY.PEEK[]")) {
576                         System.out.println("Starting BODY[]");
577                         if (responseAdded) { // unlikely
578
if (useUIDs) {
579                                 response += " UID " + uid + ")";
580                             } else {
581                                 response += ")";
582                             }
583                             out.println(response);
584                             System.out.println(response);
585                         }
586                         if (msg == null) { // probably
587
if (useUIDs) {
588                                 msg = currentMailbox.retrieveUID(uid, user);
589                             } else {
590                                 msg = currentMailbox.retrieve(msn, user);
591                             }
592                         }
593                         if (flags == null) {
594                             if (useUIDs) {
595                                 flags = currentMailbox.getFlagsUID(uid, user);
596                             } else {
597                                 flags = currentMailbox.getFlags(msn, user);
598                             }
599                         }
600                         if (msg == null) { // bad
601
out.println(tag + SP + msn + SP + BAD + "Error retrieving message3.");
602                             System.out.println(tag + SP + msn + SP + BAD + "Error retrieving message3.");
603                             getLogger().error("Retrieved null message");
604                             return;
605                         }
606                         try {
607                             long size = msg.getMessageSize();
608                             if (arg.equalsIgnoreCase("RFC822")) {
609                                 out.println(UNTAGGED + SP + msn + SP + "FETCH ( RFC822 {" + size + "}");
610                                 System.out.println(UNTAGGED + SP + msn + SP + "FETCH ( RFC822 {" + size + "}");
611                             } else {
612                                 out.println(UNTAGGED + SP + msn + SP + "FETCH ( BODY[] {" + size + "}");
613                                 System.out.println(UNTAGGED + SP + msn + SP + "FETCH ( BODY[] {" + size + "}");
614                             }
615                             msg.writeTo(outs);
616                             if (useUIDs) {
617                                 out.println(" UID " + uid + ")");
618                                 System.out.println(" UID " + uid + ")");
619                             } else {
620                                 out.println(")");
621                                 System.out.println(")");
622                             }
623                             if (! arg.equalsIgnoreCase("BODY.PEEK[]")) {
624                                 if (flags.indexOf("Seen") == -1 ) {
625                                     String newflags;
626                                     if (useUIDs) {
627                                         currentMailbox.setFlagsUID(uid, user, "+flags (\\Seen)");
628                                         newflags = currentMailbox.getFlagsUID(uid, user);
629                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
630                                                     + newflags + " UID " + uid +")");
631                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
632                                                     + newflags + " UID " + uid +")");
633                                     } else {
634                                         currentMailbox.setFlags(msn, user, "+flags (\\Seen)");
635                                         newflags = currentMailbox.getFlags(msn, user);
636                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
637                                                     + newflags + ")");
638                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
639                                                     + newflags + ")");
640                                     }
641                                 }
642                             }
643                         } catch (MessagingException me) {
644                             out.println(UNTAGGED + SP + NO + SP + "Error retrieving message4");
645                             System.out.println(UNTAGGED + SP + NO + SP + "Error retrieving message4");
646                             getLogger().error("Exception retrieving message: " + me);
647                         } catch (IOException ioe) {
648                             out.println(UNTAGGED + SP + NO + SP + "Error retrieving message5");
649                             System.out.println(UNTAGGED + SP + NO + SP + "Error retrieving message5");
650                             getLogger().error("Exception sending message: " + ioe);
651                         } catch (Exception e) {
652                             out.println(UNTAGGED + SP + NO + SP + "Error retrieving message6");
653                             System.out.println(UNTAGGED + SP + NO + SP + "Error retrieving message6");
654                             getLogger().error("Unanticipated exception retrieving message: " + e);
655                         }
656                         response = UNTAGGED + SP + msn + SP + "FETCH (";
657                         responseAdded = false;
658                     } else if (arg.equalsIgnoreCase("RFC822.TEXT")
659                                || arg.equalsIgnoreCase("BODY[TEXT]")
660                                || arg.equalsIgnoreCase("BODY.PEEK[TEXT]")) {
661                         System.out.println("Starting BODY[TEXT]");
662                         if (responseAdded) { // unlikely
663
if (useUIDs) {
664                                 response += " UID " + uid + ")";
665                             } else {
666                                 response += ")";
667                             }
668                             out.println(response);
669                             System.out.println(response);
670                         }
671                         if (msg == null) { // probably
672
if (useUIDs) {
673                                 msg = currentMailbox.retrieveUID(uid, user);
674                             } else {
675                                 msg = currentMailbox.retrieve(msn, user);
676                             }
677                         }
678                         if (flags == null) {
679                             if (useUIDs) {
680                                 flags = currentMailbox.getFlagsUID(uid, user);
681                             } else {
682                                 flags = currentMailbox.getFlags(msn, user);
683                             }
684                         }
685                         if (msg == null) { // bad
686
out.println(tag + SP + msn + SP + NO + "Error retrieving message7.");
687                             System.out.println(tag + SP + msn + SP + NO + "Error retrieving message7.");
688                             getLogger().error("Retrieved null message");
689                             return;
690                         }
691                         try {
692                             int size = msg.getSize();
693                             if (arg.equalsIgnoreCase("RFC822.TEXT")) {
694                                 out.println(UNTAGGED + SP + msn + SP + "FETCH ( RFC822.TEXT {" + size + "}");
695                                 System.out.println(UNTAGGED + SP + msn + SP + "FETCH ( RFC822.TEXT {" + size + "}");
696                             } else {
697                                 out.println(UNTAGGED + SP + msn + SP + "FETCH ( BODY[TEXT] {" + size + "}");
698                                 System.out.println(UNTAGGED + SP + msn + SP + "FETCH ( BODY[TEXT] {" + size + "}");
699                             }
700                             msg.writeContentTo(outs);
701                             if (useUIDs) {
702                                 out.println( " UID " + uid + ")");
703                                 System.out.println( " UID " + uid + ")");
704                             } else {
705                                 out.println(")");
706                                 System.out.println(")");
707                             }
708                             if (! arg.equalsIgnoreCase("BODY.PEEK[TEXT]")) {
709                                 if (flags.indexOf("Seen") == -1 ) {
710                                     String newflags;
711                                     if (useUIDs) {
712                                         currentMailbox.setFlagsUID(uid, user, "+flags (\\Seen)");
713                                         newflags = currentMailbox.getFlagsUID(uid, user);
714                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
715                                                     + newflags + " UID " + uid +")");
716                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
717                                                     + newflags + " UID " + uid +")");
718                                     } else {
719                                         currentMailbox.setFlags(msn, user, "+flags (\\Seen)");
720                                         newflags = currentMailbox.getFlags(msn, user);
721                                         out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
722                                                     + newflags + ")");
723                                         System.out.println(UNTAGGED + SP + msn + SP + "FETCH (FLAGS "
724                                                     + newflags + ")");
725                                     }
726                                 }
727                             }
728                         } catch (MessagingException me) {
729                             out.println(UNTAGGED + SP + NO + SP + "Error retrieving message8:"+me);
730                             System.out.println(UNTAGGED + SP + NO + SP + "Error retrieving message8:"+me);
731                             me.printStackTrace();
732                             getLogger().error("Exception retrieving message: " + me);
733                         } catch (IOException ioe) {
734                             out.println(UNTAGGED + SP + NO + SP + "Error retrieving message9");
735                             System.out.println(UNTAGGED + SP + NO + SP + "Error retrieving message9");
736                             getLogger().error("Exception sending message: " + ioe);
737                         } catch (Exception e) {
738                             out.println(UNTAGGED + SP + NO + SP + "Error retrieving message10");
739                             System.out.println(UNTAGGED + SP + NO + SP + "Error retrieving message10");
740                             getLogger().error("Unanticipated exception retrieving message: " + e);
741                         }
742                         response = UNTAGGED + SP + msn + SP + "FETCH (";
743                         responseAdded = false;
744                     } else { //unrecognised or not yet implemented
745
if (responseAdded) {
746                             if (useUIDs) {
747                                 response += " UID " + uid + ")";
748                             } else {
749                                 response += ")";
750                             }
751                             out.println(response);
752                             System.out.println(response);
753                         }
754                         out.println(tag + SP + NO + SP
755                                     + "FETCH attribute not recognized");
756                         System.out.println(tag + SP + NO + SP
757                                     + "FETCH attribute not recognized");
758                         getLogger().error("Received: " + arg + " as argument to fetch");
759                         return;
760                     }
761                 } // end while loop
762
if (responseAdded) {
763                     if (useUIDs) {
764                         response += " UID " + uid + ")";
765                     } else {
766                         response += ")";
767                     }
768                     out.println(response);
769                     System.out.println(response);
770                 }
771             } // end for loop
772

773             out.println(tag + SP + OK + SP + "FETCH completed");
774             System.out.println(tag + SP + OK + SP + "FETCH completed");
775             caller.checkSize();
776             return;
777         } catch (AccessControlException ace) {
778             out.println(tag + SP + NO + SP + "No such mailbox");
779             System.out.println(tag + SP + NO + SP + "No such mailbox");
780             caller.logACE(ace);
781             return;
782         } catch (AuthorizationException aze) {
783             out.println(tag + SP + NO + SP
784                         + "You do not have the rights to read from mailbox: " + currentFolder);
785             System.out.println(tag + SP + NO + SP
786                         + "You do not have the rights to read from mailbox: " + currentFolder);
787             caller.logAZE(aze);
788             return ;
789         } catch (Exception e) {
790             out.println(tag + SP + NO + SP
791                         + "Unknown server error.");
792             System.out.println(tag + SP + NO + SP
793                         + "Unknown server error.");
794             getLogger().error("Exception expunging mailbox " + currentFolder + " by user " + user + " was : " + e);
795             if (DEEP_DEBUG) {e.printStackTrace();}
796             return;
797         }
798     }
799
800     private List convertMacroCommands( List fetchAttributes )
801     {
802         List convertedAttributes = new ArrayList();
803
804         // convert macro fetch commands to basic commands
805
Iterator iter = fetchAttributes.iterator();
806         while ( iter.hasNext() ) {
807             String arg = (String)iter.next();
808             if (arg.equalsIgnoreCase("FAST")) {
809                 convertedAttributes.add("FLAGS");
810                 convertedAttributes.add("INTERNALDATE");
811                 convertedAttributes.add("RFC822.SIZE");
812             } else if (arg.equalsIgnoreCase("ALL")) {
813                 convertedAttributes.add("FLAGS");
814                 convertedAttributes.add("INTERNALDATE");
815                 convertedAttributes.add("RFC822.SIZE");
816                 convertedAttributes.add("ENVELOPE");
817             } else if (arg.equalsIgnoreCase("FULL")) {
818                 convertedAttributes.add("FLAGS");
819                 convertedAttributes.add("INTERNALDATE");
820                 convertedAttributes.add("RFC822.SIZE");
821                 convertedAttributes.add("ENVELOPE");
822                 convertedAttributes.add("BODY");
823             }
824             else {
825                 convertedAttributes.add( arg );
826             }
827             getLogger().debug("Found convertedAttributes: " + arg);
828         }
829
830         return convertedAttributes;
831     }
832 }
833
Popular Tags