File: LocalFolder.m

package info (click to toggle)
pantomime 1.1.2-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,224 kB
  • ctags: 389
  • sloc: objc: 15,433; ansic: 2,843; makefile: 75; sh: 4
file content (1629 lines) | stat: -rw-r--r-- 41,290 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
/*
**  LocalFolder.m
**
**  Copyright (c) 2001, 2002, 2003
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Ujwal S. Sathyam <ujwal@setlurgroup.com>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Pantomime/LocalFolder.h>

#include <Pantomime/Constants.h>
#include <Pantomime/Flags.h>
#include <Pantomime/InternetAddress.h>
#include <Pantomime/LocalFolderCacheManager.h>
#include <Pantomime/LocalMessage.h>
#include <Pantomime/LocalStore.h>
#include <Pantomime/MimeMultipart.h>
#include <Pantomime/MimeUtility.h>
#include <Pantomime/NSData+Extensions.h>
#include <Pantomime/NSRegEx.h>
#include <Pantomime/Parser.h>
#include <Pantomime/NSString+Extensions.h>

#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSDebug.h>
#include <Foundation/NSException.h>
#include <Foundation/NSHost.h>
#include <Foundation/NSPathUtilities.h>

#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <sys/file.h> 
#include <time.h>
#include <unistd.h>

#ifndef LOCK_SH
#define LOCK_SH 1
#endif
#ifndef LOCK_EX
#define LOCK_EX 2
#endif 
#ifndef LOCK_NB
#define LOCK_NB 4
#endif
#ifndef LOCK_UN
#define LOCK_UN 8
#endif

//
//
//
@implementation LocalFolder

- (id) initWithPathToFile: (NSString *) thePath
{
  LocalFolderCacheManager *aLocalFolderCacheManager;
  NSDictionary *attributes;
  NSString *pathToCache, *localPath;
  BOOL bIsLocal;
  NSFileManager *aFileManager;
  
  self = [super initWithName: [thePath lastPathComponent]];

  // We verify if a <name>.tmp was present. If yes, we simply remove it.
  if ( [[NSFileManager defaultManager] fileExistsAtPath: [thePath stringByAppendingString: @".tmp"]] )
    {
      // NSDebugLog(@"Removed %@", [thePath stringByAppendingString: @".tmp"]);
      [[NSFileManager defaultManager] removeFileAtPath: [thePath stringByAppendingString: @".tmp"]
				      handler: nil];
    }

  [self setPath: thePath];
  
  NSDebugLog(@"Opening %@...", [self path]);
  
  // We set our initial file attributes for the mail store
  aFileManager = [NSFileManager defaultManager];
  localPath = [NSString stringWithFormat: @"%@/new", [self path]];
  
  if ([aFileManager fileExistsAtPath: localPath  isDirectory: &bIsLocal] && bIsLocal)
    {
      attributes = [aFileManager fileAttributesAtPath: [self path] traverseLink: NO];
      [self setFolderType: MAILBOX_FORMAT_MAILDIR];
    }
  else
    {
      attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [self path]
						   traverseLink: NO];
      [self setFolderType: MAILBOX_FORMAT_MBOX];
    }

  [self setFileAttributes: attributes];
  

  if ( ([self folderType] == MAILBOX_FORMAT_MBOX) && ! [self _openAndLockFolder: [self path]] )
    {
      AUTORELEASE(self);
      return nil;
    }
  
  // We load our cache
  pathToCache = [NSString stringWithFormat: @"%@/.%@.cache",
			  [[self path] substringToIndex: 
					 ([[self path] length] - [[[self path] lastPathComponent] length])],
			  [[self path] lastPathComponent] ];
    
  // We load our cache from the file, creating it if it doesn't exist
  aLocalFolderCacheManager = [LocalFolderCacheManager localFolderCacheFromDiskWithPath: pathToCache];
  [self setCacheManager: aLocalFolderCacheManager];
  
  // We update the path to this folder for the cache manager
  [[self cacheManager] setPathToFolder: [self path]];
  
  NSDebugLog(@"Folder (%@) opened...", [self path]);

  return self;
}


//
//
//
- (void) dealloc
{
  RELEASE(fileAttributes);
  RELEASE(path);
  RELEASE(mailFilename);
  
  [super dealloc];
}


//
// This method is used to parse the message headers (and only that)
// from the current folder.
//
- (void) parse
{
  NSAutoreleasePool *pool;
  NSString *aPath;
  
  int numCurMessages, numNewMessages, numTmpMessages;
  BOOL bUseCache;

  numCurMessages = numNewMessages = numTmpMessages = 0;
  bUseCache = YES;
  
  //
  // We first verify if we need to parse the folder.
  // We invalidate our cache if our size OR or modification date have changed
  // For local, we check the number of messages in the "new" and cur sub-directories.
  bUseCache = [[[self fileAttributes] objectForKey: NSFileModificationDate] isEqualToDate: [[self cacheManager] modificationDate]] || [[[self fileAttributes] objectForKey: NSFileSize] intValue] == [(LocalFolderCacheManager *)[self cacheManager] fileSize];
  
  if ([self folderType] == MAILBOX_FORMAT_MAILDIR)
    {
      // Count the messages in the "cur" sub-directory
      aPath = [NSString stringWithFormat: @"%@/cur", [self path]];
      numCurMessages = [[[NSFileManager defaultManager] directoryContentsAtPath: aPath] count];
      
      // Count the messages in the "new" sub-directory
      aPath = [NSString stringWithFormat: @"%@/new", [self path]];
      numNewMessages = [[[NSFileManager defaultManager] directoryContentsAtPath: aPath] count];
      
      // Count the messages in the "tmp" sub-directory
      aPath = [NSString stringWithFormat: @"%@/tmp", [self path]];
      numTmpMessages = [[[NSFileManager defaultManager] directoryContentsAtPath: aPath] count];	
      
      // Compare with cache
      if (numCurMessages != [[[self cacheManager] messages] count])
	{
	  bUseCache = NO;		
	}
    }
  
  if ( bUseCache )
    {
      NSArray *array;
      int i;
          
      // In a local, if there are new messages, add them to the cache
      if (numNewMessages > 0 || numTmpMessages > 0)
	{
	  // We create a temporary autorelease pool since parse can be
	  // memory consuming on our default autorelease pool.
	  pool = [[NSAutoreleasePool alloc] init];
	  [self _parseMaildir: @"new"];
	  [self _parseMaildir: @"tmp"];
	  RELEASE(pool);
	}
      
      array = [[self cacheManager] messages];
      
      for (i = 0; i < [array count]; i++)
	{
	  [[array objectAtIndex: i] setFolder: self];
	}
      
      [self setMessages: array];
      
      return;
    }
  else
    {
      // NSDebugLog(@"Invalidating cache.");
      [[self cacheManager] invalidate];
    }
  
  NSDebugLog(@"Rebuilding cache for folder %@", [self name]);
  NSDebugLog(@"PLEASE, BE PATIENT!");
  
  // We create a temporary autorelease pool since parse can be
  // memory consuming on our default autorelease pool.
  pool = [[NSAutoreleasePool alloc] init];
  
  // Parse the mail store. For mbox, it will be one file.
  // For local, there will be a file for each message in the "cur" and "new"
  // sub-directories.
  switch ([self folderType])
    {
    case MAILBOX_FORMAT_MAILDIR:
      [self _parseMaildir: @"cur"];
      [self _parseMaildir: @"new"];
      break;
    case MAILBOX_FORMAT_MBOX:
    default:
      [self _parseMailFile: [self path] fileStream: [self stream] index: 0];
      break;
    }
  
  RELEASE(pool);
}


//
// This method is used to unfold the lines that have been folded
// by starting with the first line.
//
- (NSData *) unfoldLinesStartingWith: (char *) firstLine 
			  fileStream: (FILE*) theStream
{
  NSMutableData *aMutableData;
  NSData *aData;
  char aLine[1024], buf[1024];
  long mark;
  
  // We initialize our buffers
  memset(aLine, 0, 1024);
  memset(buf, 0, 1024);
    
  mark = ftell(theStream);
  fgets(aLine, 1024, theStream);

  if (aLine == NULL)
    {
      return [NSData dataWithBytes: firstLine  length: strlen(firstLine)];
    }

  // We create our mutable data
  aMutableData = [[NSMutableData alloc] initWithCapacity: strlen(firstLine)];

  // We remove the trailing \n and we append our first line to our mutable data
  strncpy(buf, firstLine, strlen(firstLine) - 1);
  [aMutableData appendCFormat: @"%s ", buf];
  
  // We loop as long as we have a space or tab character as the first character 
  // of the line that we just read
  while ( aLine[0] == 9 || aLine[0] == 32 )
    {
      char *ptr;

      // We skip the first char
      ptr = aLine;
      ptr++;
      
      // We init our buffer and we copy the data into it by trimming the trailing \n
      memset(buf, 0, 1024);
      strncpy(buf, ptr, strlen(ptr) - 1);
      [aMutableData appendCFormat: @"%s ", buf];

      // We set our mark and get the next folded line (if there's one)
      mark = ftell(theStream);
      memset(aLine, 0, 1024);
      fgets(aLine, 1024, theStream);
      
      if (aLine == NULL)
        {
	  RELEASE(aMutableData);
          return nil;
        }
    }

  // We reset our file pointer position and we free our C buffers.
  fseek(theStream, mark, SEEK_SET);
  
  // We trim our last " " that we added to our data
  aData = [aMutableData subdataToIndex: [aMutableData length] - 1];
  
  RELEASE(aMutableData);

  return aData;
}


//
// This method is used to close the current folder.
// It creates a temporary file where the folder is written to and
// it replaces the current folder file by this one once everything is
// alright.
//
- (void) close
{
  LocalStore *aLocalStore = nil;

  // We first obtain a reference to our local store
  aLocalStore = (LocalStore *)[self store];
  
  // We close the current folder
  // NSDebugLog(@"Closing %@...", [self name]);
  if ([self folderType] == MAILBOX_FORMAT_MBOX)
    {
      fclose([self stream]);
      flock([self fd], LOCK_UN);
      close([self fd]);	  
    }
  
  // We synchorize our cache one last time
  [[self cacheManager] synchronize];

  // We remove our current folder from the list of opened folders in the store
  [aLocalStore removeFolderFromOpenedFolders: self];
}


//
// This method permanently removes messages that have the flag DELETED.
//
// This method returns all messages that have the flag DELETED.
// All the returned message ARE IN RAW SOURCE.
//
- (NSArray *) expunge: (BOOL) returnDeletedMessages
{
  switch ([self folderType])
    {
    case MAILBOX_FORMAT_MBOX:
      return ([self _expungeMBOX: returnDeletedMessages]);
      break;
    case MAILBOX_FORMAT_MAILDIR:
      return ([self _expungeMAILDIR: returnDeletedMessages]);
      break;
    }
  return (nil);
}


//
// access / mutation methods
//

//
// This method returns the file descriptor used by this local folder.
//
- (int) fd
{
  return fd;
}


//
// This method sets the file descriptor to be used by this local folder.
//
- (void) setFD: (int) theFD
{
  fd = theFD;
}


//
//
//
- (NSString *) path
{
  return path;
}


- (void) setPath: (NSString *) thePath
{
  RETAIN(thePath);
  RELEASE(path);
  path = thePath;
}


//
// This method returns the file stream used by this local folder.
//
- (FILE *) stream
{
  return stream;
}


//
// This method sets the file stream to be used by this local folder.
//
- (void) setStream: (FILE *) theStream
{
  stream = theStream;
}


//
// This method returns the name of the mail file currently being processed.
//
- (NSString *) mailFilename
{
  return mailFilename;
}


//
// This method sets the mail file name to be processed.
//
- (void) setMailFilename: (NSString *) theFilename
{

  if ( theFilename )
    {
      RETAIN(theFilename);
      RELEASE(mailFilename);
      mailFilename = theFilename;
    }
  else
    {
      DESTROY(mailFilename);
    }
}


//
//
//
- (NSDictionary *) fileAttributes
{
  return fileAttributes;
}


- (void) setFileAttributes: (NSDictionary *) theAttributes
{
  RETAIN(theAttributes);
  RELEASE(fileAttributes);
  fileAttributes = theAttributes;
}


//
//
//
- (int) folderType
{
  return folderType;
}

- (void) setFolderType: (int) thisType
{
  folderType = thisType;
}


//
//
//
- (int) mode
{
  return PantomimeReadWriteMode;
}


//
// This method is used to append a message to this folder. The message
// must be specified in raw source. The message is appended to the 
// local file and is initialized after.
//
- (void) appendMessageFromRawSource: (NSData *) theData
                              flags: (Flags *) theFlags
{
  NSString *aMailFile, *aMailFilePath;
  NSMutableData *aMutableData;
  NSAutoreleasePool *pool;
  LocalMessage *aMessage;
  NSRange aRange;
  FILE *aStream;
  
  long mark, filePosition, bodyFilePosition;

  pool = [[NSAutoreleasePool alloc] init];

  aMutableData = [[NSMutableData alloc] initWithData: theData];
  aMailFile = nil;
  aStream = NULL;
  
  NSDebugLog(@"Appending message to %@", [self path]);


  // Set the appropriate stream
  if ( [self folderType] == MAILBOX_FORMAT_MAILDIR )
    {
      NSString *uniquePattern;
      NSMutableString *info;

      // Generate a unique file name
      uniquePattern = [NSString stringWithFormat: @"%d.%d_%d.%@",
				time(NULL), 
				getpid(),
				[[[self cacheManager] messages] count],
				[[NSHost currentHost] name]];
      
      // Generate the info field representing the status
      info = [[NSMutableString alloc] initWithString: @"2,"];
      
      if ([theFlags contain: DRAFT])
	{
	  [info appendString: @"D"];
	}
      
      if ([theFlags contain: FLAGGED])
	{
	  [info appendString: @"F"];
	}
      
      if ([theFlags contain: ANSWERED])
	{
	  [info appendString: @"R"];
	}

      if ([theFlags contain: SEEN])
	{
	  [info appendString: @"S"];
	}
      
      if ([theFlags contain: DELETED])
	{
	  [info appendString: @"T"];
	}
      
      // build the new file name
      aMailFile = [NSString stringWithFormat: @"%@:%@", uniquePattern, info];
      RELEASE(info);      
      
      // We need to put it in the tmp directory first.
      aMailFilePath = [NSString stringWithFormat: @"%@/tmp/%@", [self path], aMailFile];
      
      NSDebugLog(@"Unique file name = %@", aMailFile);
      
      aStream = fopen([aMailFilePath cString], "w+");
      
      if ( !aStream )
	{
	  RELEASE(pool);
	  return;
	}
    }
  else
    {
      aStream = [self stream];
      aMailFilePath = [self path];
    }

  // We now create the message from the raw source by using only the headers.
  aRange = [aMutableData rangeOfCString: "\n\n"];
  aMessage = [[LocalMessage alloc] initWithHeadersFromData: [MimeUtility unfoldLinesFromData: 
									   [aMutableData subdataToIndex: 
											   aRange.location + 1]]]; 
  
  // We keep the position where we were in the file
  mark = ftell( aStream );
  
  // If the message doesn't contain the "From ", we add it
  if ( ![aMutableData hasCPrefix: "From "] && 
       [self folderType] == MAILBOX_FORMAT_MBOX )
    {
      NSString *aSender, *aString;
      NSCalendarDate *aDate;

      // We get a valid sender
      if ( [aMessage from] && [[aMessage from] address] )
	{
	  aSender = [[aMessage from] address];
	}
      else
	{
	  aSender = @"unknown";
	}

      // We get a valid delivery date
      aDate = [aMessage receivedDate];

      if ( !aDate )
	{
	  aDate = [NSCalendarDate calendarDate];
	}
      

      // We must add our mbox delimiter
      aString = [NSString stringWithFormat: @"From %@ %@\n", aSender, 
			  [aDate descriptionWithCalendarFormat: @"%a %b %d %H:%M:%S %Y"]];
      [aMutableData insertCString: [aString cString]
		    atIndex: 0];
    }
  
  // We MUST replace every "\nFrom " in the message by "\n>From "
  aRange = [aMutableData rangeOfCString: "\nFrom "];
  
  while (aRange.location != NSNotFound)
    {
      [aMutableData replaceBytesInRange: aRange
		    withBytes: "\n>From "];
      
      aRange = [aMutableData rangeOfCString: "\nFrom "
			     options: 0
			     range: NSMakeRange(aRange.location + aRange.length,
						[aMutableData length] - aRange.location - aRange.length) ];
    }
  
  // We add our message separator to the end of the raw source of this message
  // It's a simple \n in the mbox format.
  [aMutableData appendCString: "\n"];
  
  // We go at the end of the file...
  if ( fseek(aStream, 0L, SEEK_END) < 0 )
    {
      NSException *anException;
     
      RELEASE(aMutableData);
      RELEASE(pool);
      anException = [NSException exceptionWithName: @"PantomimeFolderAppendMessageException"
				 reason: @"Error in seeking to the end of the local folder."
				 userInfo: nil];
      [anException raise];
      return;
    }
  
  // We get the position of our message in the file
  filePosition = ftell( aStream );

  // We get the body position of the body
  aRange = [aMutableData rangeOfCString: "\n\n"];
  bodyFilePosition = filePosition + aRange.location + 2;
  
  // We write the string to our local folder
  if ( fwrite([aMutableData bytes], 1, [aMutableData length], aStream) <= 0 )
    {
      NSException *anException;
      
      RELEASE(aMutableData);
      RELEASE(aMessage);
      RELEASE(pool);
      anException = [NSException exceptionWithName: @"PantomimeFolderAppendMessageException"
				 reason: @"Error in appending the raw source of a message to the local folder."
				 userInfo: nil];
      [anException raise];
      return;
    }

  [aMessage setFilePosition: filePosition];
  [aMessage setBodyFilePosition: bodyFilePosition];
  [aMessage setSize: (ftell(aStream) - filePosition) ];
  [aMessage setMessageNumber: ([self count] + 1)];
  [aMessage setFolder: self];
  [aMessage setMessageType: [self folderType]];
  
  // We set our flags
  if ( theFlags )
    {
      [aMessage setFlags: theFlags];
    }

  // If we are processing a maildir, close the stream and move the message into the "cur" directory.
  if ( [self folderType] == MAILBOX_FORMAT_MAILDIR )
    {
      NSString *curFilePath;
      
      fclose(aStream);
      curFilePath = [NSString stringWithFormat: @"%@/cur/%@", [self path], aMailFile];
      
      if ( [[NSFileManager defaultManager] movePath: aMailFilePath toPath: curFilePath handler: nil] == YES )
	{
	  aMailFilePath = curFilePath;
	  
	  // We enforce the file attribute (0600)
	  [(LocalStore *)[self store] enforceMode: 0600  atPath: aMailFilePath];
	}
      else
	{
	  NSDebugLog(@"Could not move %@ to %@", aMailFilePath, curFilePath);
	}  
    }
  
  [aMessage setMailFilename: aMailFilePath];
  
  
  // We append it to our folder
  [self appendMessage: aMessage];
  
  // We also append it to our cache
  if ( cacheManager )
    {
      [cacheManager addMessage: aMessage];
    }
  
  RELEASE(aMessage);
  
  // We finally reset our fp where the mark was set
  if  ([self folderType] != MAILBOX_FORMAT_MAILDIR )
    {
      fseek(aStream, mark, SEEK_SET);
    }
  
  RELEASE(aMutableData);
  RELEASE(pool);
}


//
//
//
- (NSArray *) search: (NSString *) theString
                mask: (int) theMask
             options: (int) theOptions
{
  NSMutableArray *aMutableArray;
  NSAutoreleasePool *pool;
  LocalMessage *aMessage;

  int i;

  aMutableArray = [[NSMutableArray alloc] init];
  pool = [[NSAutoreleasePool alloc] init];
  
  for (i = 0; i < [allMessages count]; i++)
    {
      aMessage = [allMessages objectAtIndex: i];
          
      //
      // We search inside the Message's content.
      //
      if ( theMask == PantomimeContent )
	{
	  BOOL messageWasInitialized, messageWasMatched;
	  
	  messageWasInitialized = [aMessage isInitialized];
	  messageWasMatched = NO;
	  
	  if ( !messageWasInitialized )
	    {
	      [aMessage setInitialized: YES];
	    }
	  
	  // We search recursively in all Message's parts
	  if ( [self _findInPart: (Part *)aMessage
		     string: theString
		     mask: theMask
		     options: theOptions] )
	    {
	      [aMutableArray addObject: aMessage];
	      messageWasMatched = YES;
	    }
	  
	  // We restore the message initialization status if the message doesn't match
	  if ( !messageWasInitialized && !messageWasMatched )
	    {
	      [aMessage setInitialized: NO];
	    }
	}
      //
      // We aren't searching in the content. For now, we search only in the Subject header value.
      //
      else
	{
	  NSString *aString;

	  aString = nil;

	  switch ( theMask )
	    {
	    case PantomimeFrom:
	      if ( [aMessage from] )
		{
		  aString = [[aMessage from] unicodeStringValue];
		}
	      break;
	      
	    case PantomimeTo:
	      aString = [MimeUtility stringFromRecipients: [aMessage recipients]
				     type: TO];
	      break;

	    case PantomimeSubject:
	    default:
	      aString = [aMessage subject];
	    }
	 
	  
	  if ( aString )
	    {
	      if ( (theOptions&PantomimeRegularExpression) )
		{
		  NSArray *anArray;
		  
		  anArray = [NSRegEx matchString: aString
				     withPattern : theString
				     isCaseSensitive: (theOptions&PantomimeCaseInsensitiveSearch)];
		  
		  if ( [anArray count] > 0 )
		    {
		      [aMutableArray addObject: aMessage];
		    }
		}
	      else
		{
		  NSRange aRange;
		  
		  if ( (theOptions&PantomimeCaseInsensitiveSearch) )
		    {
		      aRange = [aString rangeOfString: theString
					options: NSCaseInsensitiveSearch]; 
		    }
		  else
		    {
		      aRange = [aString rangeOfString: theString]; 
		    }
		  
		  if ( aRange.length > 0 )
		    {
		      [aMutableArray addObject: aMessage];
		    }
		}
	    }
	}
    } // for (i = 0; ...
	  
  RELEASE(pool);

  return AUTORELEASE(aMutableArray);
}

@end


//
// Private methods
//
@implementation LocalFolder (Private)

- (FILE *) _openAndLockFolder: (NSString *) thePath
{
  FILE *aStream;
  
  if ( !thePath )
    {
      return NULL;
    }

  fd = open([thePath cString], O_RDWR);
  
  if (fd < 0)
    {
      NSDebugLog(@"LocalFolder: Unable to get folder descriptor...");
      return NULL;
    }
  
  [self setMailFilename: thePath];
  
  if (flock(fd, LOCK_EX|LOCK_NB) < 0) 
    {
      NSDebugLog(@"LocalFolder: Unable to obtain the lock on the folder descriptor...");
      return NULL;
    }
  else 
    {
      flock(fd, LOCK_UN);
    }
  
  aStream = fdopen(fd, "r+");
  stream = aStream;
  
  if (aStream == NULL)
    {
      NSDebugLog(@"LocalFolder: Unable to open the specified mailbox...");
      return NULL;
    }
  
  flock(fd, LOCK_EX|LOCK_NB);
  
  return aStream;
}


//
//
//
- (int) _parseMailFile: (NSString *) theFile fileStream: (FILE*) aStream index: (int) theIndex
{
  LocalMessage *aLocalMessage;
  long begin, end, size;
  char aLine[1024];
  int index;
  BOOL success = NO;
  
  // We initialize our variables
  aLocalMessage = [[LocalMessage alloc] init];
  begin = ftell(aStream);
  end = 0L;
  index = theIndex;
  
  while (fgets(aLine, 1024, aStream) != NULL)
    {
      switch ( tolower(aLine[0]) )
	{	  
	case 'c':
	  if (strncasecmp(aLine, "Cc", 2) == 0)
	    {
	      [Parser parseDestination: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      forType: CC
		      inMessage: aLocalMessage];
	    }

	  break;
	  
	case 'd':
	  if (strncasecmp(aLine, "Date", 4) == 0)
	    {
	      [Parser parseDate: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case 'f':
	  if (strncasecmp(aLine, "From ", 5) == 0)
	    {
	      // do nothing, it's our message separator
	    }
	  else if (strncasecmp(aLine, "From", 4) == 0)
	    {
	      [Parser parseFrom: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case 'i':
	  if (strncasecmp(aLine, "In-Reply-To", 11) == 0)
	    {
	      [Parser parseInReplyTo: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;

	case 'm':
	  if (strncasecmp(aLine, "Message-ID", 10) == 0)
	    {
	      [Parser parseMessageID: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  else if (strncasecmp(aLine, "MIME-Version", 12) == 0)
	    {
	      [Parser parseMimeVersion: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case 'r':
	  if (strncasecmp(aLine, "References", 10) == 0)
	    {
	      [Parser parseReferences: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case 's':
	  if (strncasecmp(aLine, "Status", 6) == 0)
	    {
	      [Parser parseStatus: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  else if (strncasecmp(aLine, "Subject", 7) == 0)
	    {
	      [Parser parseSubject: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case 't':
	  if (strncasecmp(aLine, "To", 2) == 0)
	    {
	      [Parser parseDestination: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      forType: TO
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case 'x':
	  if (strncasecmp(aLine, "X-Status", 8) == 0)
	    {
	      [Parser parseXStatus: [self unfoldLinesStartingWith: aLine  fileStream: aStream]
		      inMessage: aLocalMessage];
	    }
	  break;
	  
	case '\n':
	  
	  [aLocalMessage setFilePosition: begin];
	  [aLocalMessage setBodyFilePosition: ftell(aStream)];
	  
	  // We must set this in case the last message of our mbox is
	  // an "empty message", i.e, a message with all the headers but
	  // with an empty content.
	  end = ftell(aStream);
	  
	  while (fgets(aLine, 1024, aStream) != NULL)
	    {
	      if (strncmp(aLine, "From ", 5) == 0) break;
	      else end = ftell(aStream);
	    }
	  
	  fseek(aStream, end, SEEK_SET);
	  size = end - begin;
	  
	  
	  // We increment our index
	  index = index + 1;
	  
	  // We set the properties of our message object and we add it to our folder.
	  [aLocalMessage setSize: size];
	  [aLocalMessage setMessageNumber: index];
	  [aLocalMessage setFolder: self];
	  [aLocalMessage setMessageType: [self folderType]];
	  [aLocalMessage setMailFilename: theFile];
	  [self appendMessage: aLocalMessage];

	  // if we are reading a maildir message, check for flag information in the file name
	  if ([self folderType] == MAILBOX_FORMAT_MAILDIR)
	    {
	      NSString *uniquePattern, *info;
	      int indexOfPatternSeparator;
	      
	      // name of file will be unique_pattern:info with the status flags in the info field
	      indexOfPatternSeparator = [theFile indexOfCharacter: ':'];
	      
	      if (indexOfPatternSeparator > 1)
		{
		  uniquePattern = [theFile substringToIndex: indexOfPatternSeparator];
		  info = [theFile substringFromIndex: indexOfPatternSeparator];
		}
	      else
		{
		  uniquePattern = theFile;
		  info = @"";
		}
	      
	      // remove all the flags and rebuild
	      [[aLocalMessage flags] removeAll];
	      
	      if ([info indexOfCharacter: 'S'] >= 0)
		{
		  [[aLocalMessage flags] add: SEEN];
		}
	      
	      if ([info indexOfCharacter: 'R'] >= 0)
		{
		  [[aLocalMessage flags] add: ANSWERED];
		}
	      
	      if ([info indexOfCharacter: 'F'] >= 0)
		{
		  [[aLocalMessage flags] add: FLAGGED];
		}
	      
	      if ([info indexOfCharacter: 'D'] >= 0)
		{
		  [[aLocalMessage flags] add: DRAFT];
		}
	      
	      if ([info indexOfCharacter: 'T'] >= 0)
		{
		  [[aLocalMessage flags] add: DELETED];
		}
	    }		
	  
	  // We add to our cache
	  [[self cacheManager] addMessage: aLocalMessage];
	  success = YES;		
	  
	  RELEASE(aLocalMessage);
	  
	  begin = ftell(aStream);
	  
	  // We re-init our message and our mutable string for the next message we're gonna read
	  aLocalMessage = [[LocalMessage alloc] init];
	  break;
	  
	default:
	  break;
	}
    }

  
  // We sync our cache
  [[self cacheManager] synchronize];
  
  RELEASE(aLocalMessage);

  if(success == NO)
      NSLog(@"Failed to parse mail file %@!", theFile);
  
  // Return the position of the last message read in the cache list.
  return (index-1);
}


//
// This parses a local structure for messages by looking in the "cur" and "new" sub-directories.
//
- (BOOL) _parseMaildir: (NSString *) theDir
{
  NSMutableArray *mailFiles;
  NSFileManager *aFileManager;
  NSString *aPath, *thisMailFile;
  int i, fileCount;
  int numMessages, messageIndex;
  FILE *aStream;
  
  if (theDir == nil)
    {
      return NO;
    }
  
  // Get our current count of messages
  numMessages = [[[self cacheManager] messages] count];
  
  aFileManager = [NSFileManager defaultManager];

  // Read the directory
  aPath = [NSString stringWithFormat: @"%@/%@", [self path], theDir];
  mailFiles = [[NSMutableArray alloc] initWithArray: [aFileManager directoryContentsAtPath: aPath]];
  AUTORELEASE(mailFiles);

  // We remove Apple Mac OS X .DS_Store file
  [mailFiles removeObject: @".DS_Store"];
  fileCount = [mailFiles count];
  
  NSDebugLog(@"Found %d messages in %@", fileCount, theDir);
  
  if ( mailFiles != nil &&
       fileCount > 0)
    {
      for (i = 0; i < fileCount; i++)
	{
	  // NSDebugLog(@"Checking mail file %@", [mailFiles objectAtIndex: i]);
	  thisMailFile = [NSString stringWithFormat: @"%@/%@", aPath, [mailFiles objectAtIndex: i]];
	  aStream = fopen([thisMailFile cString], "r");
	  if ( !aStream )
	    {
	      continue;
	    }
	  
	  [self setMailFilename: thisMailFile];
	  messageIndex = [self _parseMailFile: thisMailFile fileStream: aStream index: numMessages];
	  
	  if (messageIndex >= 0)
	    {
	      numMessages++;
	    }
	  fclose(aStream);
	  
	  if (messageIndex < 0)
	    {
	      continue;
	    }
	  
	  // If we read this from the "new" or "tmp" sub-directories, move it to the "cur" directory
	  if ([theDir isEqualToString: @"new"] || [theDir isEqualToString: @"tmp"])
	    {
	      NSString *newPath;
	      LocalMessage *aLocalMessage;
	      
	      newPath = [NSString stringWithFormat: @"%@/cur/%@", [self path], [mailFiles objectAtIndex: i]];
	      
	      if ([aFileManager movePath: thisMailFile toPath: newPath handler: nil] == YES)
		{
		  aLocalMessage = [[[self cacheManager] messages] objectAtIndex: messageIndex];
		 
		  if (aLocalMessage)
		    {
		      [aLocalMessage setMailFilename: newPath];
		    }
		}
	      else
		{
		  NSDebugLog(@"Could not move %@ to %@", thisMailFile, newPath);
		}
	    }
	  
	}
    }
  
  return YES;
}


//
// Expunges a mbox file
//
- (NSArray *) _expungeMBOX: (BOOL) returnDeletedMessages
{
  NSMutableArray *aMutableArray;
  
  FILE *theInputStream, *theOutputStream;
  LocalStore *aLocalStore;
  
  LocalMessage *aMessage;
  Flags *theFlags;
  
  BOOL writeWasSuccessful, seenStatus, seenXStatus, doneWritingHeaders;
  NSString *pathToMailbox;
  
  int i, messageNumber;
  char aLine[1024];
  
  // We first obtain a reference to our local store
  aLocalStore = (LocalStore *)[self store];
  
  pathToMailbox = [NSString stringWithFormat: @"%@/%@", [aLocalStore path], [self name]];
  
  // The stream is used to store (temporarily) the new local folder
  theOutputStream = fopen([[NSString stringWithFormat: @"%@.tmp", pathToMailbox] cString], "a");
  theInputStream = [self stream];

  // We assume that our write operation was successful and we initialize our messageNumber to 1
  writeWasSuccessful = YES;
  messageNumber = 1;
  
  // We verify it the creation failed
  if ( !theOutputStream )
    {
      return [NSArray array];
    }
  
  aMutableArray = [[NSMutableArray alloc] init];
  
  for (i = 0; i < [allMessages count]; i++)
    {
      aMessage = [allMessages objectAtIndex: i];
      theFlags = [aMessage flags];

      doneWritingHeaders = seenStatus = seenXStatus = NO;    
      
      if ( [theFlags contain: DELETED] )
	{
	  // We add our message to our array of deleted messages, if we need to.
	  if ( returnDeletedMessages )
	    {
	      [aMutableArray addObject: [aMessage rawSource]];
	    }
	  
	  [(LocalFolderCacheManager *)[self cacheManager] removeMessage: aMessage];
	}
      else
	{
	  long delta, position, size;
	  int headers_length;
	  
	  // We get our position and headers_length
	  position = ftell(theOutputStream);
	  headers_length = 0;
	  
	  // We seek to the beginning of the message
	  fseek(theInputStream, [aMessage filePosition], SEEK_SET);
	 
	  size = [aMessage size];
	  memset(aLine, 0, 1024);
	  
	  while ( fgets(aLine, 1024, theInputStream) != NULL &&
		  (ftell(theInputStream) < ([aMessage filePosition] + size)) )
	    {
	      // We verify if we aren't finished reading our headers
	      if ( !doneWritingHeaders )
		{
		  // We check for the "null line" (ie., end of headers)
		  if ( strlen(aLine) == 1 && strcmp("\n", aLine) == 0 )
		    {
		      doneWritingHeaders = YES;
		      
		      if ( !seenStatus ) 
			{
			  fputs( [[NSString stringWithFormat: @"Status: %s\n",
					    [theFlags statusString]] cString], theOutputStream );
			}
		      
		      if ( !seenXStatus ) 
			{
			  fputs( [[NSString stringWithFormat: @"X-Status: %s\n",
						[theFlags xstatusString]] cString], theOutputStream );
			}

		      // Since we are done writing our headers, we update the headers_length variable
		      headers_length = ftell(theOutputStream) - position;

		      // We adjust the size of the message since headers might have been rewritten (Status/X-Status).
		      // We need the trailing -1 to actually remove the header/content separator (a single \n).
		      delta = headers_length - ([aMessage bodyFilePosition] - [aMessage filePosition] - 1);
		      
		      if ( delta > 0 )
			{
			  [aMessage setSize: (size+delta)];
			}
		    }
		  
		  // If we read the Status header, we replace it with the current Status header
		  if (strncasecmp(aLine,"Status:", 7) == 0) 
		    {
		      seenStatus = YES;
		      memset(aLine, 0, 1024);
		      sprintf(aLine, "Status: %s\n", [theFlags statusString]); 
		    }
		  else if (strncasecmp(aLine,"X-Status:", 9) == 0)
		    {
		      seenXStatus = YES;
		      memset(aLine, 0, 1024);
		      sprintf(aLine, "X-Status: %s\n", [theFlags xstatusString]); 
		    }
		}
	      
	      // We write our line to our new stream
	      if ( fputs(aLine, theOutputStream) < 0 )
		{
		  writeWasSuccessful = NO;
		  break;
		}
	      
	      memset(aLine, 0, 1024);
	    } // while (...)
	  
	  // We add our message separator
	  if ( fputs("\n", theOutputStream) < 0 )
	    {
	      writeWasSuccessful = NO;
	      break;
	    }
	    
	  // We update our message's ivars
	  [aMessage setFilePosition: position];
	  [aMessage setBodyFilePosition: (position+headers_length+1)];
	  [aMessage setMessageNumber: messageNumber];
	  
	  // We increment our messageNumber local variable
	  messageNumber++;
	}

    } // for (i = 0; i < count; i++)
  
  // We close our output stream
  if ( fclose(theOutputStream) != 0 )
    {
      writeWasSuccessful = NO;
    }
  
  //
  // We verify if the last write was successful, if yes, we remove our original mailbox
  // and we replace it by our temporary mailbox.
  //
  if ( writeWasSuccessful )
    {
      // We close the current folder
      fclose(theInputStream);
      flock([self fd], LOCK_UN);
      close([self fd]);
      
      // Now that Everything is alright, replace <folder name> by <folder name>.tmp
      [[NSFileManager defaultManager] removeFileAtPath: pathToMailbox
				      handler: nil];
      [[NSFileManager defaultManager] movePath: [NSString stringWithFormat: @"%@.tmp", pathToMailbox]
				      toPath: pathToMailbox
				      handler: nil];
      
      // We sync our cache
      [[self cacheManager] synchronize];
      
      // Now we re-open our folder and update the 'allMessages' ivar in the Folder superclass
      if ( ![self _openAndLockFolder: [self path]] )
	{
	  NSDebugLog(@"A fatal error occured in LocalFolder: -expunge.");
	}
      
      [self setMessages: [[self cacheManager] messages]];
    }
  
  //
  // The last write failed, let's remove our temporary file and keep the original mbox which, might
  // contains non-updated status flags or messages that have been transferred/deleted.
  //
  else
    {
      NSDebugLog(@"Writing to %@ failed. We keep the original mailbox.", pathToMailbox);
      NSDebugLog(@"This can be due to the fact that your partition containing this mailbox is full or that you don't have write permission in the directory where this mailbox is.");
      [[NSFileManager defaultManager] removeFileAtPath: [NSString stringWithFormat: @"%@.tmp", pathToMailbox]
				      handler: nil];
    }
  
  return AUTORELEASE(aMutableArray);	
}


//
// Expunges a maildir folder
//
- (NSArray *) _expungeMAILDIR: (BOOL) returnDeletedMessages
{
  NSMutableArray *aMutableArray;
  LocalMessage *aMessage;
  Flags *theFlags;
  int i, messageNumber;
  
  aMutableArray = [[NSMutableArray alloc] init];
  
  // We assume that our write operation was successful and we initialize our messageNumber to 1
  messageNumber = 1;
  
  for (i = 0; i < [allMessages count]; i++)
    {
      aMessage = [allMessages objectAtIndex: i];
      
      theFlags = [aMessage flags];
      
      if ( [theFlags contain: DELETED] )
	{
	  // We add our message to our array of deleted messages, if we need to.
	  if ( returnDeletedMessages )
	    {
	      [aMutableArray addObject: [aMessage rawSource]];
	    }
	  
	  // Delete the message file
	  [[NSFileManager defaultManager] removeFileAtPath: [aMessage mailFilename]  handler: nil];
	  
	  // Remove the message from the cache
	  [(LocalFolderCacheManager *)[self cacheManager] removeMessage: aMessage];			
	}
      else
	{
	  // rewrite the message to account for changes in the flags
	  BOOL moveWasSuccessful;
	  NSString *uniquePattern, *newFileName;
	  NSMutableString *info;
	  int indexOfPatternSeparator;
	  Flags *theFlags;
	  

	  // We update our message's ivars (folder and size don't change)
	  [aMessage setMessageNumber: messageNumber];

	  // We increment our messageNumber local variable
	  messageNumber++;

	  // we rename the message according to the maildir spec by appending the status information the name
	  // name of file will be unique_pattern:info with the status flags in the info field
	  indexOfPatternSeparator = [[aMessage mailFilename] indexOfCharacter: ':'];
	  
	  if (indexOfPatternSeparator > 1)
	    {
	      uniquePattern = [[aMessage mailFilename] substringToIndex: indexOfPatternSeparator];
	    }
	  else
	    {
	      uniquePattern = [aMessage mailFilename];
	    }

	  // build the info field
	  info = [[NSMutableString alloc] initWithString: @"2,"];
	  
	  theFlags = [aMessage flags];
	  
	  if ([theFlags contain: DRAFT])
	    {
	      [info appendString: @"D"];
	    }
	  
	  if ([theFlags contain: FLAGGED])
	    {
	      [info appendString: @"F"];
	    }
	  
	  if ([theFlags contain: ANSWERED])
	    {
	      [info appendString: @"R"];
	    }
	  
	  if ([theFlags contain: SEEN])
	    {
	      [info appendString: @"S"];
	    }
	  
	  if ([theFlags contain: DELETED])
	    {
	      [info appendString: @"T"];
	    }
	  
	  // build the new file name
	  newFileName = [NSString stringWithFormat: @"%@:%@", uniquePattern, info];
	  RELEASE(info);

	  // rename the message file
	  moveWasSuccessful = [[NSFileManager defaultManager] movePath: [aMessage mailFilename] toPath: newFileName handler: nil];
	  
	  if (moveWasSuccessful)
	    {
	      [aMessage setMailFilename: newFileName];
	    }
	}
    }
    
    // We sync our cache
    [[self cacheManager] synchronize];
        
    [self setMessages: [[self cacheManager] messages]];
    
  return AUTORELEASE(aMutableArray);	
}


//
//
//
- (BOOL) _findInPart: (Part *) thePart
	      string: (NSString *) theString
		mask: (int) theMask
             options: (int) theOptions
  
{  
  if ( [[thePart content] isKindOfClass:[NSString class]] )
    {
      // The part content is text; we perform the search      
      if ( (theOptions&PantomimeRegularExpression) )
	{
	  // The search pattern is a regexp

	  NSArray *anArray;
	  
	  anArray = [NSRegEx matchString: (NSString *)[thePart content]
			     withPattern : theString
			     isCaseSensitive: (theOptions&PantomimeCaseInsensitiveSearch)];
		  
	  if ( [anArray count] > 0 )
	    {
	      return YES;
	    }
	}
      else
	{
	  NSRange range;

	  if ( !(theOptions&PantomimeCaseInsensitiveSearch) )
	    {
	      range = [(NSString *)[thePart content] rangeOfString: theString
				   options: NSCaseInsensitiveSearch];
	    }
	  else
	    {
	      range = [(NSString *)[thePart content] rangeOfString: theString]; 
	    }
		  
	  if ( range.length > 0 )
	    {
	      return YES;
	    }
	}
    }
  
  else if ( [[thePart content] isKindOfClass: [Message class]] )
    {
      // The part content is a message; we parse it recursively
      return [self _findInPart: (Part *)[thePart content]
		   string: theString
		   mask: theMask
		   options: theOptions];
    }
  else if ( [[thePart content] isKindOfClass: [MimeMultipart class]] )
    {
      // The part content contains many part; we parse each part
      MimeMultipart *aMimeMultipart;
      Part *aPart;
      int i;
      
      aMimeMultipart = (MimeMultipart*)[thePart content];
      
      for (i = 0; i < [aMimeMultipart count]; i++)
	{
	  // We get our part
	  aPart = [aMimeMultipart bodyPartAtIndex: i];
	  
	  if ( [self _findInPart: (Part *)aPart
		     string: theString 
		     mask: theMask
		     options: theOptions] )
	    {
	      return YES;
	    }
	}
    }
  
  return NO;
}

@end