| 1 |
/* |
| 2 |
** CWLocalFolder.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2017-2022 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/CWLocalFolder.h> |
| 25 |
|
| 26 |
#import <Pantomime/CWConstants.h> |
| 27 |
#import <Pantomime/CWRegEx.h> |
| 28 |
#import <Pantomime/CWFlags.h> |
| 29 |
#import <Pantomime/CWInternetAddress.h> |
| 30 |
#import <Pantomime/CWLocalCacheManager.h> |
| 31 |
#import <Pantomime/CWLocalFolder+maildir.h> |
| 32 |
#import <Pantomime/CWLocalFolder+mbox.h> |
| 33 |
#import <Pantomime/CWLocalMessage.h> |
| 34 |
#import <Pantomime/CWLocalStore.h> |
| 35 |
#import <Pantomime/CWMIMEMultipart.h> |
| 36 |
#import <Pantomime/NSData+Extensions.h> |
| 37 |
#import <Pantomime/NSFileManager+Extensions.h> |
| 38 |
#import <Pantomime/NSString+Extensions.h> |
| 39 |
|
| 40 |
#import <Foundation/NSAutoreleasePool.h> |
| 41 |
#import <Foundation/NSException.h> |
| 42 |
#import <Foundation/NSHost.h> |
| 43 |
#import <Foundation/NSNotification.h> |
| 44 |
#import <Foundation/NSPathUtilities.h> |
| 45 |
|
| 46 |
#include <ctype.h> |
| 47 |
#include <fcntl.h> |
| 48 |
#include <string.h> |
| 49 |
#include <sys/file.h> // FIXME remove |
| 50 |
#include <time.h> |
| 51 |
#include <unistd.h> // FIXME remove |
| 52 |
#import <stdlib.h> // for rand() |
| 53 |
|
| 54 |
|
| 55 |
// |
| 56 |
// Private methods |
| 57 |
// |
| 58 |
@interface CWLocalFolder (Private) |
| 59 |
|
| 60 |
- (BOOL) _findInPart: (CWPart *) thePart |
| 61 |
string: (NSString *) theString |
| 62 |
mask: (PantomimeSearchMask) theMask |
| 63 |
options: (PantomimeSearchOption) theOptions; |
| 64 |
@end |
| 65 |
|
| 66 |
|
| 67 |
// |
| 68 |
// |
| 69 |
// |
| 70 |
@implementation CWLocalFolder |
| 71 |
|
| 72 |
- (id) initWithPath: (NSString *) thePath |
| 73 |
{ |
| 74 |
BOOL b; |
| 75 |
|
| 76 |
self = [super initWithName: [thePath lastPathComponent]]; |
| 77 |
|
| 78 |
if (self) |
| 79 |
{ |
| 80 |
// We initialize those ivars in order to make sure we don't call |
| 81 |
// the assertion handler when using a maildir-based mailbox. |
| 82 |
stream = NULL; |
| 83 |
fd = -1; |
| 84 |
|
| 85 |
[self setPath: thePath]; |
| 86 |
|
| 87 |
if ([[NSFileManager defaultManager] fileExistsAtPath: [NSString stringWithFormat: @"%@/new", _path] isDirectory: &b] && b) |
| 88 |
{ |
| 89 |
[self setType: PantomimeFormatMaildir]; |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
[self setType: PantomimeFormatMbox]; |
| 94 |
|
| 95 |
// We verify if a <name>.tmp was present. If yes, we simply remove it. |
| 96 |
if ([[NSFileManager defaultManager] fileExistsAtPath: [thePath stringByAppendingString: @".tmp"]]) |
| 97 |
{ |
| 98 |
[[NSFileManager defaultManager] removeFileAtPath: [thePath stringByAppendingString: @".tmp"] |
| 99 |
handler: nil]; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ((_type == PantomimeFormatMbox) && ![self open_mbox]) |
| 104 |
{ |
| 105 |
AUTORELEASE(self); |
| 106 |
return nil; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
return self; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
// |
| 115 |
// |
| 116 |
// |
| 117 |
- (void) dealloc |
| 118 |
{ |
| 119 |
//NSLog(@"LocalFolder: -dealloc. fd = %d, stream is NULL? %d", fd, (stream == NULL)); |
| 120 |
|
| 121 |
NSAssert3(fd < 0 && !stream, |
| 122 |
@"-[%@ %@, path %@] must invoke -close before - dealloc'ing", |
| 123 |
NSStringFromClass([self class]), |
| 124 |
NSStringFromSelector(_cmd), _path); |
| 125 |
|
| 126 |
RELEASE(_path); |
| 127 |
[super dealloc]; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
// |
| 132 |
// |
| 133 |
// |
| 134 |
- (void) parse: (BOOL) theBOOL |
| 135 |
{ |
| 136 |
NSAutoreleasePool *pool; |
| 137 |
|
| 138 |
// |
| 139 |
// If we already have messages in our folder, that means parse was already invoked. |
| 140 |
// In this particular case, we do nothing. If we got no messages but we already |
| 141 |
// have invoked -parse before, that won't do any harm. |
| 142 |
// |
| 143 |
if ([_allMessages count]) |
| 144 |
{ |
| 145 |
// |
| 146 |
// If we are using a maildir-based mailbox, we scan the /new and /tmp directories |
| 147 |
// in order to move any messages in there to our /cur directory. |
| 148 |
// |
| 149 |
if (_type == PantomimeFormatMaildir) |
| 150 |
{ |
| 151 |
NSFileManager *aFileManager; |
| 152 |
|
| 153 |
aFileManager = [NSFileManager defaultManager]; |
| 154 |
|
| 155 |
if ([[aFileManager directoryContentsAtPath: [NSString stringWithFormat: @"%@/new", _path]] count] > 0 || |
| 156 |
[[aFileManager directoryContentsAtPath: [NSString stringWithFormat: @"%@/tmp", _path]] count] > 0) |
| 157 |
{ |
| 158 |
pool = [[NSAutoreleasePool alloc] init]; |
| 159 |
[self parse_maildir: @"new" all: theBOOL]; |
| 160 |
[self parse_maildir: @"tmp" all: theBOOL]; |
| 161 |
RELEASE(pool); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
PERFORM_SELECTOR_2([[self store] delegate], @selector(folderPrefetchCompleted:), PantomimeFolderPrefetchCompleted, self, @"Folder"); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
// |
| 172 |
// We are NOT using the cache. |
| 173 |
// |
| 174 |
pool = [[NSAutoreleasePool alloc] init]; |
| 175 |
|
| 176 |
// |
| 177 |
// Parse the mail store. For mbox, it will be one file. |
| 178 |
// For maildir, there will be a file for each message |
| 179 |
// in the "cur" and "new" sub-directories. |
| 180 |
// |
| 181 |
switch (_type) |
| 182 |
{ |
| 183 |
case PantomimeFormatMaildir: |
| 184 |
[self parse_maildir: @"cur" all: theBOOL]; |
| 185 |
[self parse_maildir: @"new" all: theBOOL]; |
| 186 |
break; |
| 187 |
case PantomimeFormatMbox: |
| 188 |
default: |
| 189 |
[self parse_mbox: _path stream: [self stream] flags: nil all: theBOOL]; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
PERFORM_SELECTOR_2([[self store] delegate], @selector(folderPrefetchCompleted:), PantomimeFolderPrefetchCompleted, self, @"Folder"); |
| 194 |
RELEASE(pool); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
// |
| 202 |
// This method is used to close the current folder. |
| 203 |
// It creates a temporary file where the folder is written to and |
| 204 |
// it replaces the current folder file by this one once everything is |
| 205 |
// alright. |
| 206 |
// |
| 207 |
- (void) close |
| 208 |
{ |
| 209 |
//NSLog(@"LocalFolder: -close"); |
| 210 |
|
| 211 |
// We close the current folder |
| 212 |
if (_type == PantomimeFormatMbox || _type == PantomimeFormatMailSpoolFile) |
| 213 |
{ |
| 214 |
[self close_mbox]; |
| 215 |
} |
| 216 |
|
| 217 |
// We synchorize our cache one last time |
| 218 |
if (_type == PantomimeFormatMbox || _type == PantomimeFormatMaildir) |
| 219 |
{ |
| 220 |
[_cacheManager synchronize]; |
| 221 |
} |
| 222 |
|
| 223 |
POST_NOTIFICATION(PantomimeFolderCloseCompleted, _store, [NSDictionary dictionaryWithObject: self forKey: @"Folder"]); |
| 224 |
PERFORM_SELECTOR_2([_store delegate], @selector(folderCloseCompleted:), PantomimeFolderCloseCompleted, self, @"Folder"); |
| 225 |
|
| 226 |
// We remove our current folder from the list of open folders in the store |
| 227 |
[_store removeFolderFromOpenFolders: self]; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
// |
| 232 |
// This method permanently removes messages that have the flag PantomimeDeleted. |
| 233 |
// |
| 234 |
- (void) expunge |
| 235 |
{ |
| 236 |
switch (_type) |
| 237 |
{ |
| 238 |
case PantomimeFormatMbox: |
| 239 |
[self expunge_mbox]; |
| 240 |
break; |
| 241 |
case PantomimeFormatMaildir: |
| 242 |
[self expunge_maildir]; |
| 243 |
break; |
| 244 |
default: |
| 245 |
{ |
| 246 |
// Do nothing. |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if (_allContainers) |
| 251 |
{ |
| 252 |
[self thread]; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
// |
| 258 |
// access / mutation methods |
| 259 |
// |
| 260 |
|
| 261 |
// |
| 262 |
// This method returns the file descriptor used by this local folder. |
| 263 |
// |
| 264 |
- (int) fd |
| 265 |
{ |
| 266 |
return fd; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
// |
| 271 |
// This method sets the file descriptor to be used by this local folder. |
| 272 |
// |
| 273 |
- (void) setFD: (int) theFD |
| 274 |
{ |
| 275 |
fd = theFD; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
// |
| 280 |
// |
| 281 |
// |
| 282 |
- (NSString *) path |
| 283 |
{ |
| 284 |
return _path; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
- (void) setPath: (NSString *) thePath |
| 289 |
{ |
| 290 |
ASSIGN(_path, thePath); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
// |
| 295 |
// This method returns the file stream used by this local folder. |
| 296 |
// |
| 297 |
- (FILE *) stream |
| 298 |
{ |
| 299 |
return stream; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
// |
| 304 |
// This method sets the file stream to be used by this local folder. |
| 305 |
// |
| 306 |
- (void) setStream: (FILE *) theStream |
| 307 |
{ |
| 308 |
stream = theStream; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
// |
| 313 |
// |
| 314 |
// |
| 315 |
- (PantomimeFolderFormat) type |
| 316 |
{ |
| 317 |
return _type; |
| 318 |
} |
| 319 |
|
| 320 |
- (void) setType: (PantomimeFolderFormat) theType |
| 321 |
{ |
| 322 |
_type = theType; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
// |
| 327 |
// |
| 328 |
// |
| 329 |
- (PantomimeFolderMode) mode |
| 330 |
{ |
| 331 |
return PantomimeReadWriteMode; |
| 332 |
} |
| 333 |
|
| 334 |
// |
| 335 |
// This method is used to append a message to this folder. The message |
| 336 |
// must be specified in raw source. The message is appended to the |
| 337 |
// stream that must be passed open. The caller has resposibility of |
| 338 |
// eventualy closing it. BOOL returned YES if successful, NO otherwise |
| 339 |
// |
| 340 |
- (BOOL) appendMessageFromRawSource: (NSData *) theData |
| 341 |
toStream: (FILE *) theStream |
| 342 |
withMailFile: (NSString *) mailFile |
| 343 |
flags: (CWFlags *) theFlags |
| 344 |
|
| 345 |
{ |
| 346 |
NSMutableData *aMutableData; |
| 347 |
CWLocalMessage *aMessage; |
| 348 |
long file_position; |
| 349 |
|
| 350 |
aMutableData = [[NSMutableData alloc] initWithData: theData]; |
| 351 |
|
| 352 |
// |
| 353 |
// If the message doesn't contain the "From ", we add it. |
| 354 |
// |
| 355 |
// From qmail's mbox(5) man page: |
| 356 |
// |
| 357 |
// The From_ line always looks like From envsender date |
| 358 |
// moreinfo. envsender is one word, without spaces or tabs; it |
| 359 |
// is usually the envelope sender of the message. date is the |
| 360 |
// delivery date of the message. It always contains exactly 24 |
| 361 |
// characters in asctime format. moreinfo is optional; it may |
| 362 |
// contain arbitrary information. |
| 363 |
// |
| 364 |
if (![aMutableData hasCPrefix: "From "] && _type == PantomimeFormatMbox) |
| 365 |
{ |
| 366 |
NSString *aString; |
| 367 |
|
| 368 |
// If there was no envelope sender, by convention the mailbox name used is MAILER-DAEMON. |
| 369 |
// Whitespace characters in the envelope sender mailbox name are by convention replaced by hyphens. |
| 370 |
aString = [NSString stringWithFormat: @"From MAILER-DAEMON %@\n", [[NSCalendarDate calendarDate] |
| 371 |
descriptionWithCalendarFormat: @"%a %b %d %H:%M:%S %Y"]]; |
| 372 |
[aMutableData insertCString: [aString cString] atIndex: 0]; |
| 373 |
} |
| 374 |
|
| 375 |
// We MUST replace every "\nFrom " in the message by "\n From ", if we have a mbox file. |
| 376 |
if (_type == PantomimeFormatMbox) |
| 377 |
{ |
| 378 |
NSRange aRange; |
| 379 |
|
| 380 |
aRange = [aMutableData rangeOfCString: "\nFrom "]; |
| 381 |
|
| 382 |
while (aRange.location != NSNotFound) |
| 383 |
{ |
| 384 |
[aMutableData replaceBytesInRange: aRange withBytes: "\n From "]; |
| 385 |
|
| 386 |
aRange = [aMutableData rangeOfCString: "\nFrom " |
| 387 |
options: 0 |
| 388 |
range: NSMakeRange(aRange.location + aRange.length, |
| 389 |
[aMutableData length] - aRange.location - aRange.length) ]; |
| 390 |
} |
| 391 |
|
| 392 |
// |
| 393 |
// From qmail's mbox(5) man page: |
| 394 |
// |
| 395 |
// A message encoded in mbox format begins with a From_ line, |
| 396 |
// continues with a series of non-From_ lines, and ends with a |
| 397 |
// blank line. |
| 398 |
// ... |
| 399 |
// The final line is a completely blank line (no spaces or |
| 400 |
// tabs). Notice that blank lines may also appear elsewhere in |
| 401 |
// the message. |
| 402 |
// |
| 403 |
[aMutableData appendCString: "\n\n"]; |
| 404 |
} |
| 405 |
|
| 406 |
// We go at the end of the file... |
| 407 |
if (fseek(theStream, 0L, SEEK_END) < 0) |
| 408 |
{ |
| 409 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: fseek() failed"); |
| 410 |
[aMutableData release]; |
| 411 |
return NO; |
| 412 |
} |
| 413 |
|
| 414 |
// We get the position of our message in the file. We need |
| 415 |
// to keep it in order to correctly seek back at the beginning |
| 416 |
// of the message to parse it. |
| 417 |
if ((file_position = ftell(theStream)) < 0) |
| 418 |
{ |
| 419 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: ftell() failed"); |
| 420 |
[aMutableData release]; |
| 421 |
return NO; |
| 422 |
} |
| 423 |
|
| 424 |
// We write the string to our local folder |
| 425 |
if (fwrite([aMutableData bytes], 1, [aMutableData length], theStream) <= 0) |
| 426 |
{ |
| 427 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: fwrite() failed"); |
| 428 |
[aMutableData release]; |
| 429 |
return NO; |
| 430 |
} |
| 431 |
|
| 432 |
// We parse the message using our code, which will also update |
| 433 |
// our cache if present |
| 434 |
if (fseek(theStream, file_position, SEEK_SET) < 0) |
| 435 |
{ |
| 436 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: fseek() failed"); |
| 437 |
[aMutableData release]; |
| 438 |
return NO; |
| 439 |
} |
| 440 |
|
| 441 |
[self parse_mbox: mailFile stream: theStream flags: theFlags all: NO]; |
| 442 |
|
| 443 |
// We get back our message |
| 444 |
aMessage = [_allMessages objectAtIndex: [_allMessages count]-1]; |
| 445 |
|
| 446 |
// We set our flags |
| 447 |
if (theFlags) |
| 448 |
{ |
| 449 |
[aMessage setFlags: theFlags]; |
| 450 |
} |
| 451 |
|
| 452 |
[aMutableData release]; |
| 453 |
return YES; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
// |
| 458 |
// This method is used to append a message to this folder. The message |
| 459 |
// must be specified in raw source. The message is appended to the |
| 460 |
// local file and is initialized after. |
| 461 |
// |
| 462 |
- (void) appendMessageFromRawSource: (NSData *) theData |
| 463 |
flags: (CWFlags *) theFlags |
| 464 |
{ |
| 465 |
NSString *aMailFile, *aMailFilePath; |
| 466 |
NSDictionary *aDictionary; |
| 467 |
FILE *aStream; |
| 468 |
BOOL appendResult; |
| 469 |
NSAutoreleasePool *arp; |
| 470 |
long mark; |
| 471 |
|
| 472 |
aMailFile = nil; |
| 473 |
aStream = NULL; |
| 474 |
mark = 0L; |
| 475 |
|
| 476 |
aDictionary = (theFlags ? [NSDictionary dictionaryWithObjectsAndKeys: theData, @"NSData", self, @"Folder", theFlags, @"Flags", nil] : |
| 477 |
[NSDictionary dictionaryWithObjectsAndKeys: theData, @"NSData", self, @"Folder", nil]); |
| 478 |
|
| 479 |
if (!theData || [theData length] == 0) |
| 480 |
{ |
| 481 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: no data"); |
| 482 |
PERFORM_SELECTOR_3([[self store] delegate], @selector(folderAppendFailed:), PantomimeFolderAppendFailed, aDictionary); |
| 483 |
return; |
| 484 |
} |
| 485 |
|
| 486 |
// Set the appropriate stream |
| 487 |
if (_type == PantomimeFormatMaildir) |
| 488 |
{ |
| 489 |
aMailFile = [NSString stringWithFormat: @"%@:%@", |
| 490 |
[NSString stringWithFormat: @"%lld.%d%d%lu.%@", |
| 491 |
(long long)time(NULL), |
| 492 |
getpid(), |
| 493 |
rand(), |
| 494 |
(unsigned long)[_cacheManager count], |
| 495 |
[[NSHost currentHost] name]], |
| 496 |
((id)theFlags ? (id)[theFlags maildirString] : (id)@"2,")]; |
| 497 |
|
| 498 |
aMailFilePath = [NSString stringWithFormat: |
| 499 |
@"%@/cur/%@", _path, aMailFile]; |
| 500 |
|
| 501 |
#ifdef __MINGW32__ |
| 502 |
aStream = fopen([aMailFilePath UTF8String], "bw+"); |
| 503 |
#else |
| 504 |
aStream = fopen([aMailFilePath UTF8String], "w+"); |
| 505 |
#endif |
| 506 |
if (!aStream) |
| 507 |
{ |
| 508 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: fopen() failed"); |
| 509 |
PERFORM_SELECTOR_3([[self store] delegate], @selector(folderAppendFailed:), PantomimeFolderAppendFailed, aDictionary); |
| 510 |
return; |
| 511 |
} |
| 512 |
} |
| 513 |
else |
| 514 |
{ |
| 515 |
// since we suppose it is already open, we do not close it afterwards |
| 516 |
aStream = [self stream]; |
| 517 |
|
| 518 |
// We keep the position where we were in the file |
| 519 |
mark = ftell(aStream); |
| 520 |
if (mark < 0) |
| 521 |
{ |
| 522 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: ftell() failed"); |
| 523 |
PERFORM_SELECTOR_3([[self store] delegate], @selector(folderAppendFailed:), PantomimeFolderAppendFailed, aDictionary); |
| 524 |
return; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
arp = [[NSAutoreleasePool alloc] init]; |
| 530 |
appendResult = [self appendMessageFromRawSource:theData toStream:aStream withMailFile: aMailFile flags:theFlags]; |
| 531 |
[arp release]; |
| 532 |
|
| 533 |
if (appendResult) |
| 534 |
{ |
| 535 |
PERFORM_SELECTOR_3([[self store] delegate], @selector(folderAppendCompleted:), PantomimeFolderAppendCompleted, aDictionary); |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
PERFORM_SELECTOR_3([[self store] delegate], @selector(folderAppendFailed:), PantomimeFolderAppendFailed, aDictionary); |
| 540 |
} |
| 541 |
|
| 542 |
// should we close it earlier before performing the selector? |
| 543 |
// If opened the stream, we close it |
| 544 |
if (_type == PantomimeFormatMaildir) |
| 545 |
{ |
| 546 |
if (fclose(aStream) != 0) |
| 547 |
{ |
| 548 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: fclose() failed"); |
| 549 |
} |
| 550 |
} |
| 551 |
// else we seek back |
| 552 |
else |
| 553 |
{ |
| 554 |
if (fseek(aStream, mark, SEEK_SET) < 0) |
| 555 |
{ |
| 556 |
NSLog(@"CWLocalfolder appendMessageFromRawSource: resetting fp, fseek() failed"); |
| 557 |
} |
| 558 |
|
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
// |
| 564 |
// |
| 565 |
// |
| 566 |
- (void) search: (NSString *) theString |
| 567 |
mask: (PantomimeSearchMask) theMask |
| 568 |
options: (PantomimeSearchOption) theOptions |
| 569 |
{ |
| 570 |
NSMutableArray *aMutableArray; |
| 571 |
NSAutoreleasePool *pool; |
| 572 |
NSDictionary *userInfo; |
| 573 |
CWLocalMessage *aMessage; |
| 574 |
|
| 575 |
NSUInteger i, count; |
| 576 |
|
| 577 |
aMutableArray = [NSMutableArray array]; |
| 578 |
|
| 579 |
pool = [[NSAutoreleasePool alloc] init]; |
| 580 |
count = [_allMessages count]; |
| 581 |
|
| 582 |
for (i = 0; i < count; i++) |
| 583 |
{ |
| 584 |
aMessage = [_allMessages objectAtIndex: i]; |
| 585 |
|
| 586 |
// |
| 587 |
// We search inside the Message's content. |
| 588 |
// |
| 589 |
if (theMask == PantomimeContent) |
| 590 |
{ |
| 591 |
BOOL messageWasInitialized, messageWasMatched; |
| 592 |
|
| 593 |
messageWasInitialized = [aMessage isInitialized]; |
| 594 |
messageWasMatched = NO; |
| 595 |
|
| 596 |
if (!messageWasInitialized) |
| 597 |
{ |
| 598 |
[aMessage setInitialized: YES]; |
| 599 |
} |
| 600 |
|
| 601 |
// We search recursively in all Message's parts |
| 602 |
if ([self _findInPart: (CWPart *)aMessage |
| 603 |
string: theString |
| 604 |
mask: theMask |
| 605 |
options: theOptions]) |
| 606 |
{ |
| 607 |
[aMutableArray addObject: aMessage]; |
| 608 |
messageWasMatched = YES; |
| 609 |
} |
| 610 |
|
| 611 |
// We restore the message initialization status if the message doesn't match |
| 612 |
if (!messageWasInitialized && !messageWasMatched) |
| 613 |
{ |
| 614 |
[aMessage setInitialized: NO]; |
| 615 |
} |
| 616 |
} |
| 617 |
// |
| 618 |
// We aren't searching in the content. For now, we search only in the Subject header value. |
| 619 |
// |
| 620 |
else |
| 621 |
{ |
| 622 |
NSString *aString; |
| 623 |
|
| 624 |
aString = nil; |
| 625 |
|
| 626 |
switch (theMask) |
| 627 |
{ |
| 628 |
case PantomimeFrom: |
| 629 |
if ([aMessage from]) |
| 630 |
{ |
| 631 |
aString = [[aMessage from] stringValue]; |
| 632 |
} |
| 633 |
break; |
| 634 |
|
| 635 |
case PantomimeTo: |
| 636 |
aString = [NSString stringFromRecipients: [aMessage recipients] |
| 637 |
type: PantomimeToRecipient]; |
| 638 |
break; |
| 639 |
|
| 640 |
case PantomimeSubject: |
| 641 |
default: |
| 642 |
aString = [aMessage subject]; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
if (aString) |
| 647 |
{ |
| 648 |
if ((theOptions&PantomimeRegularExpression)) |
| 649 |
{ |
| 650 |
NSArray *anArray; |
| 651 |
|
| 652 |
anArray = [CWRegEx matchString: aString |
| 653 |
withPattern : theString |
| 654 |
isCaseSensitive: (theOptions&PantomimeCaseInsensitiveSearch)]; |
| 655 |
|
| 656 |
if ([anArray count] > 0) |
| 657 |
{ |
| 658 |
[aMutableArray addObject: aMessage]; |
| 659 |
} |
| 660 |
} |
| 661 |
else |
| 662 |
{ |
| 663 |
NSRange aRange; |
| 664 |
|
| 665 |
if ((theOptions&PantomimeCaseInsensitiveSearch)) |
| 666 |
{ |
| 667 |
aRange = [aString rangeOfString: theString |
| 668 |
options: NSCaseInsensitiveSearch]; |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
aRange = [aString rangeOfString: theString]; |
| 673 |
} |
| 674 |
|
| 675 |
if (aRange.length > 0) |
| 676 |
{ |
| 677 |
[aMutableArray addObject: aMessage]; |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
} // for (i = 0; ... |
| 683 |
|
| 684 |
RELEASE(pool); |
| 685 |
|
| 686 |
userInfo = [NSDictionary dictionaryWithObjectsAndKeys: self, @"Folder", aMutableArray, @"Results", nil]; |
| 687 |
|
| 688 |
POST_NOTIFICATION(PantomimeFolderSearchCompleted, [self store], userInfo); |
| 689 |
PERFORM_SELECTOR_3([[self store] delegate], @selector(folderSearchCompleted:), PantomimeFolderSearchCompleted, userInfo); |
| 690 |
} |
| 691 |
|
| 692 |
@end |
| 693 |
|
| 694 |
|
| 695 |
// |
| 696 |
// Private methods |
| 697 |
// |
| 698 |
@implementation CWLocalFolder (Private) |
| 699 |
|
| 700 |
- (BOOL) _findInPart: (CWPart *) thePart |
| 701 |
string: (NSString *) theString |
| 702 |
mask: (PantomimeSearchMask) theMask |
| 703 |
options: (PantomimeSearchOption) theOptions |
| 704 |
|
| 705 |
{ |
| 706 |
if ([[thePart content] isKindOfClass:[NSString class]]) |
| 707 |
{ |
| 708 |
// The part content is text; we perform the search |
| 709 |
if ((theOptions&PantomimeRegularExpression)) |
| 710 |
{ |
| 711 |
// The search pattern is a regexp |
| 712 |
|
| 713 |
NSArray *anArray; |
| 714 |
|
| 715 |
anArray = [CWRegEx matchString: (NSString *)[thePart content] |
| 716 |
withPattern : theString |
| 717 |
isCaseSensitive: (theOptions&PantomimeCaseInsensitiveSearch)]; |
| 718 |
|
| 719 |
if ([anArray count] > 0) |
| 720 |
{ |
| 721 |
return YES; |
| 722 |
} |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
NSRange range; |
| 727 |
|
| 728 |
if (theOptions&PantomimeCaseInsensitiveSearch) |
| 729 |
{ |
| 730 |
range = [(NSString *)[thePart content] rangeOfString: theString |
| 731 |
options: NSCaseInsensitiveSearch]; |
| 732 |
} |
| 733 |
else |
| 734 |
{ |
| 735 |
range = [(NSString *)[thePart content] rangeOfString: theString]; |
| 736 |
} |
| 737 |
|
| 738 |
if (range.length > 0) |
| 739 |
{ |
| 740 |
return YES; |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
else if ([[thePart content] isKindOfClass: [CWMessage class]]) |
| 746 |
{ |
| 747 |
// The part content is a message; we parse it recursively |
| 748 |
return [self _findInPart: (CWPart *)[thePart content] |
| 749 |
string: theString |
| 750 |
mask: theMask |
| 751 |
options: theOptions]; |
| 752 |
} |
| 753 |
else if ([[thePart content] isKindOfClass: [CWMIMEMultipart class]]) |
| 754 |
{ |
| 755 |
// The part content contains many part; we parse each part |
| 756 |
CWMIMEMultipart *aMimeMultipart; |
| 757 |
CWPart *aPart; |
| 758 |
NSUInteger i, count; |
| 759 |
|
| 760 |
aMimeMultipart = (CWMIMEMultipart*)[thePart content]; |
| 761 |
count = [aMimeMultipart count]; |
| 762 |
|
| 763 |
for (i = 0; i < count; i++) |
| 764 |
{ |
| 765 |
// We get our part |
| 766 |
aPart = [aMimeMultipart partAtIndex: i]; |
| 767 |
|
| 768 |
if ([self _findInPart: (CWPart *)aPart |
| 769 |
string: theString |
| 770 |
mask: theMask |
| 771 |
options: theOptions]) |
| 772 |
{ |
| 773 |
return YES; |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
return NO; |
| 779 |
} |
| 780 |
|
| 781 |
@end |