| 1 |
/* |
| 2 |
** CWLocalFolder+mbox.m |
| 3 |
** |
| 4 |
** Copyright (c) 2004-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2014-2022 Riccardo Mottola, Sebastian Reitenbach |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola |
| 9 |
** Sebastian Reitenbach |
| 10 |
** |
| 11 |
** This library is free software; you can redistribute it and/or |
| 12 |
** modify it under the terms of the GNU Lesser General Public |
| 13 |
** License as published by the Free Software Foundation; either |
| 14 |
** version 2.1 of the License, or (at your option) any later version. |
| 15 |
** |
| 16 |
** This library is distributed in the hope that it will be useful, |
| 17 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
** Lesser General Public License for more details. |
| 20 |
** |
| 21 |
** You should have received a copy of the GNU General Public License |
| 22 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
*/ |
| 24 |
|
| 25 |
#import <Foundation/Foundation.h> |
| 26 |
|
| 27 |
#import <Pantomime/CWLocalFolder+mbox.h> |
| 28 |
|
| 29 |
#import <Pantomime/CWFlags.h> |
| 30 |
#import <Pantomime/CWCacheManager.h> |
| 31 |
#import <Pantomime/CWLocalCacheManager.h> |
| 32 |
#import <Pantomime/CWLocalMessage.h> |
| 33 |
#import <Pantomime/CWLocalStore.h> |
| 34 |
#import <Pantomime/NSData+Extensions.h> |
| 35 |
#import <Pantomime/NSString+Extensions.h> |
| 36 |
#import <Pantomime/CWParser.h> |
| 37 |
|
| 38 |
|
| 39 |
#ifdef __MINGW32__ |
| 40 |
#include <io.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <ctype.h> |
| 44 |
#include <errno.h> |
| 45 |
#include <fcntl.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include <string.h> |
| 48 |
#include <sys/file.h> |
| 49 |
#include <unistd.h> |
| 50 |
|
| 51 |
// |
| 52 |
// |
| 53 |
// |
| 54 |
@implementation CWLocalFolder (mbox) |
| 55 |
|
| 56 |
- (void) close_mbox |
| 57 |
{ |
| 58 |
#ifndef __MINGW32__ |
| 59 |
struct flock lock; |
| 60 |
|
| 61 |
#ifdef __linux__ |
| 62 |
if (flock(fd, LOCK_UN) == -1) |
| 63 |
{ |
| 64 |
NSLog(@"CWLocalFolder+mbox: Could not remove advisory file lock for: %@. Rationale: %s", _path, strerror(errno)); |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
// We remove the mandatory lock |
| 69 |
lock.l_type = F_UNLCK; |
| 70 |
lock.l_whence = SEEK_SET; |
| 71 |
lock.l_start = lock.l_len = 0; |
| 72 |
lock.l_pid = getpid(); |
| 73 |
|
| 74 |
if (fcntl(fd, F_SETLK, &lock) == -1) |
| 75 |
{ |
| 76 |
NSLog(@"CWLocalFolder+mbox: Could not remove mandatory file lock for: %@. Rationale: %s", _path, strerror(errno)); |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
// |
| 81 |
// We close the stream. This will also close our file descriptor. |
| 82 |
// |
| 83 |
// fopen(3) says: |
| 84 |
// |
| 85 |
// The file descriptor is not dup'ed, and will be closed when the |
| 86 |
// stream created by fdopen is closed. |
| 87 |
// |
| 88 |
fclose(stream); |
| 89 |
|
| 90 |
stream = NULL; |
| 91 |
fd = -1; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
// |
| 96 |
// |
| 97 |
// |
| 98 |
- (void) expunge_mbox |
| 99 |
{ |
| 100 |
FILE *theInputStream, *theOutputStream; |
| 101 |
|
| 102 |
NSMutableArray *aMutableArray; |
| 103 |
CWLocalMessage *aMessage; |
| 104 |
CWFlags *theFlags; |
| 105 |
|
| 106 |
BOOL writeWasSuccessful, seenStatus, seenXStatus, doneWritingHeaders; |
| 107 |
NSString *pathToMailbox; |
| 108 |
|
| 109 |
NSUInteger i, count; |
| 110 |
unsigned int messageNumber; |
| 111 |
char aLine[1024]; |
| 112 |
|
| 113 |
aMutableArray = AUTORELEASE([[NSMutableArray alloc] init]); |
| 114 |
|
| 115 |
pathToMailbox = [[_store path] stringByAppendingPathComponent: _name]; |
| 116 |
|
| 117 |
// The stream is used to store (temporarily) the new local folder |
| 118 |
#ifdef __MINGW32__ |
| 119 |
theOutputStream = fopen([[pathToMailbox stringByAppendingPathExtension:@"tmp"] UTF8String], "ab"); |
| 120 |
#else |
| 121 |
theOutputStream = fopen([[pathToMailbox stringByAppendingPathExtension:@"tmp"] UTF8String], "a"); |
| 122 |
#endif |
| 123 |
theInputStream = [self stream]; |
| 124 |
|
| 125 |
// We assume that our write operation was successful and we initialize our messageNumber to 1 |
| 126 |
writeWasSuccessful = YES; |
| 127 |
messageNumber = 1; |
| 128 |
|
| 129 |
// We verify it the creation failed |
| 130 |
if (!theOutputStream) |
| 131 |
{ |
| 132 |
POST_NOTIFICATION(PantomimeFolderExpungeFailed, self, nil); |
| 133 |
PERFORM_SELECTOR_2([[self store] delegate], @selector(folderExpungeFailed:), PantomimeFolderExpungeFailed, self, @"Folder"); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
count = [_allMessages count]; |
| 138 |
|
| 139 |
for (i = 0; i < count; i++) |
| 140 |
{ |
| 141 |
aMessage = [_allMessages objectAtIndex: i]; |
| 142 |
theFlags = [aMessage flags]; |
| 143 |
|
| 144 |
doneWritingHeaders = seenStatus = seenXStatus = NO; |
| 145 |
|
| 146 |
if ([theFlags contain: PantomimeDeleted]) |
| 147 |
{ |
| 148 |
//[(CWLocalCacheManager *)_cacheManager removeObject: aMessage]; |
| 149 |
[aMutableArray addObject: aMessage]; |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
unsigned long size; |
| 154 |
long delta, position; |
| 155 |
long headers_length; |
| 156 |
|
| 157 |
// We get our position and headers_length |
| 158 |
position = ftell(theOutputStream); |
| 159 |
|
| 160 |
// We seek to the beginning of the message |
| 161 |
fseek(theInputStream, [aMessage filePosition], SEEK_SET); |
| 162 |
|
| 163 |
size = [aMessage size]; |
| 164 |
memset(aLine, 0, 1024); |
| 165 |
|
| 166 |
while (fgets(aLine, 1024, theInputStream) != NULL && |
| 167 |
(ftell(theInputStream) < ([aMessage filePosition] + (long)size))) |
| 168 |
{ |
| 169 |
// We verify if we aren't finished reading our headers |
| 170 |
if (!doneWritingHeaders) |
| 171 |
{ |
| 172 |
// We check for the "null line" (ie., end of headers) |
| 173 |
if (strlen(aLine) == 1 && strcmp("\n", aLine) == 0) |
| 174 |
{ |
| 175 |
doneWritingHeaders = YES; |
| 176 |
headers_length = ftell(theOutputStream); |
| 177 |
|
| 178 |
if (!seenStatus) |
| 179 |
{ |
| 180 |
fputs([[NSString stringWithFormat: @"Status: %@\n", |
| 181 |
[theFlags statusString]] cString], theOutputStream); |
| 182 |
} |
| 183 |
|
| 184 |
if (!seenXStatus) |
| 185 |
{ |
| 186 |
fputs([[NSString stringWithFormat: @"X-Status: %@\n", |
| 187 |
[theFlags xstatusString]] cString], theOutputStream); |
| 188 |
} |
| 189 |
|
| 190 |
delta = ftell(theOutputStream)-headers_length; |
| 191 |
|
| 192 |
// Since we are done writing our headers, we update the headers_length variable |
| 193 |
//headers_length = ftell(theOutputStream) - position; |
| 194 |
|
| 195 |
// We adjust the size of the message since headers might have been rewritten (Status/X-Status). |
| 196 |
// We need the trailing -1 to actually remove the header/content separator (a single \n). |
| 197 |
//delta = headers_length - ([aMessage bodyFilePosition] - [aMessage filePosition] - 1); |
| 198 |
|
| 199 |
if (delta > 0) |
| 200 |
{ |
| 201 |
//NSLog(@"delta = %d", delta); |
| 202 |
[aMessage setSize: (size+(unsigned long)delta)]; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// If we read the Status header, we replace it with the current Status header |
| 207 |
#warning we might need to adjust the size here too! |
| 208 |
if (strncasecmp(aLine,"Status:", 7) == 0) |
| 209 |
{ |
| 210 |
seenStatus = YES; |
| 211 |
memset(aLine, 0, 1024); |
| 212 |
sprintf(aLine, "Status: %s\n", [[theFlags statusString] cString]); |
| 213 |
} |
| 214 |
else if (strncasecmp(aLine,"X-Status:", 9) == 0) |
| 215 |
{ |
| 216 |
seenXStatus = YES; |
| 217 |
memset(aLine, 0, 1024); |
| 218 |
sprintf(aLine, "X-Status: %s\n", [[theFlags xstatusString] cString]); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// We write our line to our new stream |
| 223 |
if (fputs(aLine, theOutputStream) < 0) |
| 224 |
{ |
| 225 |
writeWasSuccessful = NO; |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
memset(aLine, 0, 1024); |
| 230 |
} // while (...) |
| 231 |
|
| 232 |
// We add our message separator |
| 233 |
if (fputs("\n", theOutputStream) < 0) |
| 234 |
{ |
| 235 |
writeWasSuccessful = NO; |
| 236 |
break; |
| 237 |
} |
| 238 |
|
| 239 |
// We update our message's ivars |
| 240 |
[aMessage setFilePosition: position]; |
| 241 |
//[aMessage setBodyFilePosition: (position+headers_length+1)]; |
| 242 |
[aMessage setMessageNumber: messageNumber]; |
| 243 |
|
| 244 |
// We increment our messageNumber local variable |
| 245 |
messageNumber++; |
| 246 |
} |
| 247 |
|
| 248 |
} // for (i = 0; i < count; i++) |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
// We close our output stream |
| 253 |
if (fclose(theOutputStream) != 0) |
| 254 |
{ |
| 255 |
writeWasSuccessful = NO; |
| 256 |
} |
| 257 |
|
| 258 |
// |
| 259 |
// We verify if the last write was successful, if yes, we remove our original mailbox |
| 260 |
// and we replace it by our temporary mailbox. |
| 261 |
// |
| 262 |
if (writeWasSuccessful) |
| 263 |
{ |
| 264 |
// We close the current folder |
| 265 |
[self close_mbox]; |
| 266 |
|
| 267 |
// Now that everything is alright, replace <folder name> by <folder name>.tmp |
| 268 |
[[NSFileManager defaultManager] removeFileAtPath: pathToMailbox |
| 269 |
handler: nil]; |
| 270 |
[[NSFileManager defaultManager] movePath: [pathToMailbox stringByAppendingPathExtension:@"tmp"] |
| 271 |
toPath: pathToMailbox |
| 272 |
handler: nil]; |
| 273 |
|
| 274 |
// We sync our cache |
| 275 |
if (_cacheManager) [_cacheManager expunge]; |
| 276 |
|
| 277 |
// Now we re-open our folder and update the 'allMessages' ivar in the Folder superclass |
| 278 |
[self open_mbox]; |
| 279 |
|
| 280 |
[_allMessages removeObjectsInArray: aMutableArray]; |
| 281 |
//[self setMessages: [_cacheManager cache]]; |
| 282 |
} |
| 283 |
|
| 284 |
// |
| 285 |
// The last write failed, let's remove our temporary file and keep the original mbox which, might |
| 286 |
// contains non-updated status flags or messages that have been transferred/deleted. |
| 287 |
// |
| 288 |
else |
| 289 |
{ |
| 290 |
NSLog(@"Writing to %@ failed. We keep the original mailbox.", pathToMailbox); |
| 291 |
NSLog(@"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."); |
| 292 |
[[NSFileManager defaultManager] removeFileAtPath: [NSString stringWithFormat: @"%@.tmp", pathToMailbox] |
| 293 |
handler: nil]; |
| 294 |
POST_NOTIFICATION(PantomimeFolderExpungeFailed, self, nil); |
| 295 |
PERFORM_SELECTOR_2([[self store] delegate], @selector(folderExpungeFailed:), PantomimeFolderExpungeFailed, self, @"Folder"); |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
#warning also return when invoking the delegate |
| 300 |
POST_NOTIFICATION(PantomimeFolderExpungeCompleted, self, nil); |
| 301 |
PERFORM_SELECTOR_2([[self store] delegate], @selector(folderExpungeCompleted:), PantomimeFolderExpungeCompleted, self, @"Folder"); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
// |
| 307 |
// We use fcntl(2) for locking the local mbox file. This provides a |
| 308 |
// mandatory, kernel-enforced lock that should work over NFS. |
| 309 |
// However, on Linux, fcntl(2) and flock(2) are implemented as to ignore |
| 310 |
// each other and if the local delivery agent uses flock(2), we won't |
| 311 |
// block appropriately. So, if we are on Linux, we lock the mbox file |
| 312 |
// with both fcntl(2) and flock(2). |
| 313 |
// |
| 314 |
- (FILE *) open_mbox |
| 315 |
{ |
| 316 |
#ifndef __MINGW32__ |
| 317 |
struct flock lock; |
| 318 |
#endif |
| 319 |
FILE *aStream; |
| 320 |
|
| 321 |
if (!_path) |
| 322 |
{ |
| 323 |
NSLog(@"Invalid path to the mailbox file."); |
| 324 |
return NULL; |
| 325 |
} |
| 326 |
|
| 327 |
#ifdef __MINGW32__ |
| 328 |
fd = _open([_path UTF8String], _O_BINARY|_O_RDWR); |
| 329 |
#else |
| 330 |
fd = open([_path UTF8String], O_RDWR); |
| 331 |
#endif |
| 332 |
|
| 333 |
if (fd < 0) |
| 334 |
{ |
| 335 |
NSLog(@"LocalFolder: Unable to get folder descriptor at path %@.", _path); |
| 336 |
return NULL; |
| 337 |
} |
| 338 |
|
| 339 |
//NSLog(@"In open_mbox, fd = %d", fd); |
| 340 |
|
| 341 |
#ifndef __MINGW32__ |
| 342 |
lock.l_type = F_WRLCK; |
| 343 |
lock.l_whence = SEEK_SET; |
| 344 |
lock.l_start = 0; |
| 345 |
lock.l_len = 0; |
| 346 |
lock.l_pid = getpid(); |
| 347 |
|
| 348 |
if (fcntl(fd, F_SETLK, &lock) == -1) |
| 349 |
{ |
| 350 |
NSLog(@"CWLocalFolder+mbox: Unable to obtain the mandatory lock on the folder descriptor at path %@.", _path); |
| 351 |
} |
| 352 |
|
| 353 |
#ifdef __linux__ |
| 354 |
if (flock(fd, LOCK_EX|LOCK_NB) < 0) |
| 355 |
{ |
| 356 |
NSLog(@"CWLocalFolder+mbox: Unable to obtain the advisory lock on the folder descriptor at path %@.", _path); |
| 357 |
close(fd); |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
flock(fd, LOCK_UN); |
| 363 |
} |
| 364 |
#endif |
| 365 |
#endif |
| 366 |
|
| 367 |
#ifdef __MINGW32__ |
| 368 |
aStream = _fdopen(fd, "r+"); |
| 369 |
#else |
| 370 |
aStream = fdopen(fd, "r+"); |
| 371 |
#endif |
| 372 |
|
| 373 |
[self setStream: aStream]; |
| 374 |
|
| 375 |
if (aStream == NULL) |
| 376 |
{ |
| 377 |
NSLog(@"LocalFolder: Unable to open the specified mailbox at path %@.", _path); |
| 378 |
return NULL; |
| 379 |
} |
| 380 |
|
| 381 |
#ifdef __linux__ |
| 382 |
flock(fd, LOCK_EX|LOCK_NB); |
| 383 |
#endif |
| 384 |
|
| 385 |
return aStream; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
// |
| 390 |
// |
| 391 |
// |
| 392 |
- (void) parse_mbox: (NSString *) theFile |
| 393 |
stream: (FILE *) theStream |
| 394 |
flags: (CWFlags *) theFlags |
| 395 |
all: (BOOL) theBOOL |
| 396 |
{ |
| 397 |
CWLocalMessage *aMessage; |
| 398 |
NSUInteger size; |
| 399 |
long begin, end; |
| 400 |
cache_record record; |
| 401 |
char aLine[1024]; |
| 402 |
|
| 403 |
// We initialize our variables |
| 404 |
aMessage = [[CWLocalMessage alloc] init]; |
| 405 |
CLEAR_CACHE_RECORD(record); |
| 406 |
|
| 407 |
begin = 0L; |
| 408 |
|
| 409 |
if (_type == PantomimeFormatMbox) |
| 410 |
{ |
| 411 |
begin = ftell(theStream); |
| 412 |
} |
| 413 |
|
| 414 |
while (fgets(aLine, 1024, theStream) != NULL) |
| 415 |
{ |
| 416 |
switch (tolower((int)(unsigned char)aLine[0])) |
| 417 |
{ |
| 418 |
case 'b': |
| 419 |
if (theBOOL && strncasecmp(aLine, "Bcc", 2) == 0) |
| 420 |
{ |
| 421 |
[CWParser parseDestination: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 422 |
forType: PantomimeBccRecipient |
| 423 |
inMessage: aMessage |
| 424 |
quick: NO]; |
| 425 |
} |
| 426 |
break; |
| 427 |
|
| 428 |
case 'c': |
| 429 |
if (strncasecmp(aLine, "Cc", 2) == 0) |
| 430 |
{ |
| 431 |
record.cc = [CWParser parseDestination: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 432 |
forType: PantomimeCcRecipient |
| 433 |
inMessage: aMessage |
| 434 |
quick: NO]; |
| 435 |
} |
| 436 |
else if (theBOOL && strncasecmp(aLine, "Content-Type", 12) == 0) |
| 437 |
{ |
| 438 |
[CWParser parseContentType: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 439 |
inPart: aMessage]; |
| 440 |
} |
| 441 |
break; |
| 442 |
|
| 443 |
case 'd': |
| 444 |
if (strncasecmp(aLine, "Date", 4) == 0) |
| 445 |
{ |
| 446 |
[CWParser parseDate: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 447 |
inMessage: aMessage]; |
| 448 |
|
| 449 |
if ([aMessage receivedDate]) |
| 450 |
{ |
| 451 |
record.date = (uint32_t)round([[aMessage receivedDate] timeIntervalSince1970]); |
| 452 |
} |
| 453 |
} |
| 454 |
break; |
| 455 |
|
| 456 |
case 'f': |
| 457 |
if (strncasecmp(aLine, "From ", 5) == 0) |
| 458 |
{ |
| 459 |
// do nothing, it's our message separator |
| 460 |
} |
| 461 |
else if (strncasecmp(aLine, "From", 4) == 0) |
| 462 |
{ |
| 463 |
record.from = [CWParser parseFrom: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 464 |
inMessage: aMessage |
| 465 |
quick: NO]; |
| 466 |
} |
| 467 |
break; |
| 468 |
|
| 469 |
case 'i': |
| 470 |
if (strncasecmp(aLine, "In-Reply-To", 11) == 0) |
| 471 |
{ |
| 472 |
record.in_reply_to = [CWParser parseInReplyTo: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 473 |
inMessage: aMessage |
| 474 |
quick: NO]; |
| 475 |
} |
| 476 |
break; |
| 477 |
|
| 478 |
case 'm': |
| 479 |
if (strncasecmp(aLine, "Message-ID", 10) == 0) |
| 480 |
{ |
| 481 |
record.message_id = [CWParser parseMessageID: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 482 |
inMessage: aMessage |
| 483 |
quick: NO]; |
| 484 |
} |
| 485 |
else if (strncasecmp(aLine, "MIME-Version", 12) == 0) |
| 486 |
{ |
| 487 |
[CWParser parseMIMEVersion: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 488 |
inMessage: aMessage]; |
| 489 |
} |
| 490 |
break; |
| 491 |
|
| 492 |
case 'o': |
| 493 |
if (theBOOL && strncasecmp(aLine, "Organization", 12) == 0) |
| 494 |
{ |
| 495 |
[CWParser parseOrganization: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 496 |
inMessage: aMessage]; |
| 497 |
} |
| 498 |
break; |
| 499 |
|
| 500 |
case 'r': |
| 501 |
if (strncasecmp(aLine, "References", 10) == 0) |
| 502 |
{ |
| 503 |
record.references = [CWParser parseReferences: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 504 |
inMessage: aMessage |
| 505 |
quick: NO]; |
| 506 |
} |
| 507 |
else if (theBOOL && strncasecmp(aLine, "Reply-To", 8) == 0) |
| 508 |
{ |
| 509 |
[CWParser parseReplyTo: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 510 |
inMessage: aMessage]; |
| 511 |
} |
| 512 |
else if (theBOOL && strncasecmp(aLine, "Resent-From", 11) == 0) |
| 513 |
{ |
| 514 |
[CWParser parseResentFrom: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 515 |
inMessage: aMessage]; |
| 516 |
} |
| 517 |
break; |
| 518 |
|
| 519 |
case 's': |
| 520 |
if (strncasecmp(aLine, "Status", 6) == 0) |
| 521 |
{ |
| 522 |
[CWParser parseStatus: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 523 |
inMessage: aMessage]; |
| 524 |
} |
| 525 |
else if (strncasecmp(aLine, "Subject", 7) == 0) |
| 526 |
{ |
| 527 |
record.subject = [CWParser parseSubject: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 528 |
inMessage: aMessage |
| 529 |
quick: NO]; |
| 530 |
} |
| 531 |
break; |
| 532 |
|
| 533 |
case 't': |
| 534 |
if (strncasecmp(aLine, "To", 2) == 0) |
| 535 |
{ |
| 536 |
record.to = [CWParser parseDestination: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 537 |
forType: PantomimeToRecipient |
| 538 |
inMessage: aMessage |
| 539 |
quick: NO]; |
| 540 |
} |
| 541 |
break; |
| 542 |
|
| 543 |
case 'x': |
| 544 |
if (strncasecmp(aLine, "X-Status", 8) == 0) |
| 545 |
{ |
| 546 |
[CWParser parseXStatus: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 547 |
inMessage: aMessage]; |
| 548 |
} |
| 549 |
break; |
| 550 |
|
| 551 |
case '\n': |
| 552 |
[aMessage setFilePosition: begin]; |
| 553 |
//[aMessage setBodyFilePosition: ftell(theStream)]; |
| 554 |
|
| 555 |
// We must set this in case the last message of our mbox is |
| 556 |
// an "empty message", i.e, a message with all the headers but |
| 557 |
// with an empty content. |
| 558 |
end = ftell(theStream); |
| 559 |
|
| 560 |
while (fgets(aLine, 1024, theStream) != NULL) |
| 561 |
{ |
| 562 |
if (strncmp(aLine, "From ", 5) == 0 && _type == PantomimeFormatMbox) break; |
| 563 |
else end = ftell(theStream); |
| 564 |
} |
| 565 |
|
| 566 |
fseek(theStream, end, SEEK_SET); |
| 567 |
size = (NSUInteger)(end-begin); |
| 568 |
|
| 569 |
// We set the properties of our message object and we add it to our folder. |
| 570 |
[aMessage setSize: size]; |
| 571 |
[aMessage setMessageNumber: [_allMessages count]+1]; |
| 572 |
[aMessage setFolder: self]; |
| 573 |
[aMessage setType: _type]; |
| 574 |
[self appendMessage: aMessage]; |
| 575 |
|
| 576 |
record.filename = (char *)[[theFile lastPathComponent] UTF8String]; |
| 577 |
record.flags = (theFlags ? theFlags->flags : [aMessage flags]->flags); |
| 578 |
record.position = begin; |
| 579 |
record.size = (unsigned int)size; |
| 580 |
[(CWLocalCacheManager *)_cacheManager writeRecord: &record]; |
| 581 |
CLEAR_CACHE_RECORD(record); |
| 582 |
// |
| 583 |
|
| 584 |
// if we are reading a maildir message, check for flag information in the file name |
| 585 |
if (_type == PantomimeFormatMaildir) |
| 586 |
{ |
| 587 |
NSString *info; |
| 588 |
NSUInteger indexOfPatternSeparator; |
| 589 |
|
| 590 |
[aMessage setMailFilename: [theFile lastPathComponent]]; |
| 591 |
|
| 592 |
// The name of file will be unique_pattern:info with the status flags in the info field |
| 593 |
indexOfPatternSeparator = [theFile indexOfCharacter: ':']; |
| 594 |
|
| 595 |
if (indexOfPatternSeparator != NSNotFound && indexOfPatternSeparator > 1) |
| 596 |
{ |
| 597 |
info = [theFile substringFromIndex: indexOfPatternSeparator]; |
| 598 |
} |
| 599 |
else |
| 600 |
{ |
| 601 |
info = @""; |
| 602 |
} |
| 603 |
|
| 604 |
// We remove all the flags and rebuild |
| 605 |
[[aMessage flags] removeAll]; |
| 606 |
[[aMessage flags] addFlagsFromData: [info dataUsingEncoding: NSASCIIStringEncoding] |
| 607 |
format: PantomimeFormatMaildir]; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
RELEASE(aMessage); |
| 612 |
begin = ftell(theStream); |
| 613 |
|
| 614 |
// We re-init our message and our mutable string for the next message we're gonna read |
| 615 |
aMessage = [[CWLocalMessage alloc] init]; |
| 616 |
break; |
| 617 |
|
| 618 |
default: |
| 619 |
if (theBOOL) |
| 620 |
{ |
| 621 |
[CWParser parseUnknownHeader: [self unfoldLinesStartingWith: aLine fileStream: theStream] |
| 622 |
inMessage: aMessage]; |
| 623 |
} |
| 624 |
break; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
// |
| 629 |
// We sync our cache if's an mbox file since we are done parsing. For |
| 630 |
// maildir, we synchronize it in -parse_maildir: |
| 631 |
// |
| 632 |
if (_type == PantomimeFormatMbox) |
| 633 |
{ |
| 634 |
//NSLog(@"Sync cache."); |
| 635 |
[_cacheManager synchronize]; |
| 636 |
} |
| 637 |
|
| 638 |
RELEASE(aMessage); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
// |
| 643 |
// This method is used to unfold the lines that have been folded |
| 644 |
// by starting with the first line. |
| 645 |
// |
| 646 |
- (NSData *) unfoldLinesStartingWith: (char *) firstLine |
| 647 |
fileStream: (FILE*) theStream |
| 648 |
{ |
| 649 |
NSMutableData *aMutableData; |
| 650 |
char aLine[1024], buf[1024]; |
| 651 |
char space; |
| 652 |
long mark; |
| 653 |
|
| 654 |
// We initialize our buffers |
| 655 |
memset(aLine, 0, 1024); |
| 656 |
memset(buf, 0, 1024); |
| 657 |
space = ' '; |
| 658 |
|
| 659 |
mark = ftell(theStream); |
| 660 |
if (fgets(aLine, 1024, theStream) == NULL) |
| 661 |
{ |
| 662 |
return [NSData dataWithBytes: firstLine length: strlen(firstLine)]; |
| 663 |
} |
| 664 |
|
| 665 |
// We create our mutable data |
| 666 |
aMutableData = [[NSMutableData alloc] initWithCapacity: strlen(firstLine)]; |
| 667 |
|
| 668 |
// We remove the trailing \n and we append our first line to our mutable data |
| 669 |
strncpy(buf, firstLine, strlen(firstLine) - 1); |
| 670 |
[aMutableData appendBytes: buf length: strlen(firstLine) - 1]; |
| 671 |
[aMutableData appendBytes: &space length: 1]; |
| 672 |
|
| 673 |
// We loop as long as we have a space or tab character as the first character |
| 674 |
// of the line that we just read |
| 675 |
while (aLine[0] == 9 || aLine[0] == 32) |
| 676 |
{ |
| 677 |
char *ptr; |
| 678 |
|
| 679 |
// We skip the first char |
| 680 |
ptr = aLine; |
| 681 |
ptr++; |
| 682 |
|
| 683 |
// We init our buffer and we copy the data into it by trimming the trailing \n |
| 684 |
memset(buf, 0, 1024); |
| 685 |
strncpy(buf, ptr, strlen(ptr)-1); |
| 686 |
[aMutableData appendBytes: buf length: strlen(ptr)-1]; |
| 687 |
[aMutableData appendBytes: &space length: 1]; |
| 688 |
|
| 689 |
// We set our mark and get the next folded line (if there's one) |
| 690 |
mark = ftell(theStream); |
| 691 |
memset(aLine, 0, 1024); |
| 692 |
if (fgets(aLine, 1024, theStream) == NULL) |
| 693 |
{ |
| 694 |
RELEASE(aMutableData); |
| 695 |
return nil; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
// We reset our file pointer position |
| 700 |
if (fseek(theStream, mark, SEEK_SET) == -1) |
| 701 |
{ |
| 702 |
NSLog(@"Failed to fseek()"); |
| 703 |
} |
| 704 |
|
| 705 |
// We trim our last " " that we added to our data |
| 706 |
[aMutableData setLength: [aMutableData length]-1]; |
| 707 |
return AUTORELEASE(aMutableData); |
| 708 |
} |
| 709 |
|
| 710 |
// |
| 711 |
// |
| 712 |
// |
| 713 |
+ (NSUInteger) numberOfMessagesFromData: (NSData *) theData |
| 714 |
{ |
| 715 |
NSRange aRange; |
| 716 |
NSUInteger count, len; |
| 717 |
|
| 718 |
if (!theData || (len = [theData length]) == 0) |
| 719 |
{ |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
aRange = NSMakeRange(0,0); |
| 724 |
count = 0; |
| 725 |
|
| 726 |
do |
| 727 |
{ |
| 728 |
aRange = [theData rangeOfCString: "\nFrom " options: 0 range: NSMakeRange(NSMaxRange(aRange), len-NSMaxRange(aRange))]; |
| 729 |
count++; |
| 730 |
} while (aRange.location != NSNotFound); |
| 731 |
|
| 732 |
return count; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
// |
| 737 |
// |
| 738 |
// |
| 739 |
- (NSArray *) messagesFromMailSpoolFile |
| 740 |
{ |
| 741 |
NSMutableArray *aMutableArray; |
| 742 |
char aLine[1024]; |
| 743 |
long begin, end; |
| 744 |
|
| 745 |
if (_type == PantomimeFormatMbox || _type == PantomimeFormatMaildir) |
| 746 |
{ |
| 747 |
return nil; |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
begin = 0; |
| 752 |
memset(aLine, 0, 1024); |
| 753 |
|
| 754 |
if (fseek(stream, begin, SEEK_SET) == -1) |
| 755 |
{ |
| 756 |
NSLog(@"failed to fseek()"); |
| 757 |
return nil; |
| 758 |
} |
| 759 |
|
| 760 |
aMutableArray = [[NSMutableArray alloc] init]; |
| 761 |
|
| 762 |
while (fgets(aLine, 1024, stream) != NULL) |
| 763 |
{ |
| 764 |
if (strncasecmp(aLine, "From ", 5) == 0) |
| 765 |
{ |
| 766 |
NSData *aData; |
| 767 |
size_t length; |
| 768 |
char *buf; |
| 769 |
|
| 770 |
// We always 'skip' the "From " line |
| 771 |
begin = ftell(stream); |
| 772 |
end = ftell(stream); |
| 773 |
|
| 774 |
// We read until we reach an other "From " or, the end of the stream |
| 775 |
while (fgets(aLine, 1024, stream) != NULL) |
| 776 |
{ |
| 777 |
if (strncmp(aLine, "From ", 5) == 0) break; |
| 778 |
else end = ftell(stream); |
| 779 |
} |
| 780 |
|
| 781 |
// We get the length of our message |
| 782 |
length = (size_t)(end - begin - 1); |
| 783 |
|
| 784 |
// We allocate our buffer for the message |
| 785 |
buf = (char *)malloc(length * sizeof(char)); |
| 786 |
memset(buf, 0, length); |
| 787 |
|
| 788 |
// We move our fp to the beginning of the message |
| 789 |
if (fseek(stream, begin, SEEK_SET) == -1) |
| 790 |
{ |
| 791 |
NSLog(@"failed to fseek() 2"); |
| 792 |
free(buf); |
| 793 |
[aMutableArray release]; |
| 794 |
return nil; |
| 795 |
} |
| 796 |
|
| 797 |
if (fread(buf, sizeof(char), length, stream) != length) |
| 798 |
{ |
| 799 |
NSLog(@"failed to fread()"); |
| 800 |
free(buf); |
| 801 |
[aMutableArray release]; |
| 802 |
return nil; |
| 803 |
} |
| 804 |
|
| 805 |
aData = [[NSData alloc] initWithBytesNoCopy: buf length: length]; |
| 806 |
[aMutableArray addObject: aData]; |
| 807 |
RELEASE(aData); |
| 808 |
|
| 809 |
// We reset our fp to the right position (end of previous message) |
| 810 |
if (fseek(stream, end, SEEK_SET) == -1) |
| 811 |
{ |
| 812 |
NSLog(@"failed to fseek() 3"); |
| 813 |
} |
| 814 |
memset(aLine, 0, 1024); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
// We now truncate our file to a length of 0. |
| 819 |
if (ftruncate(fd, 0) == -1) |
| 820 |
{ |
| 821 |
NSLog(@"CWLocalFolder+mbox: Could not truncate file: %@", _path); |
| 822 |
} |
| 823 |
|
| 824 |
return AUTORELEASE(aMutableArray); |
| 825 |
} |
| 826 |
|
| 827 |
@end |