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
|
/*
** CWIMAPFolder.m
**
** Copyright (c) 2001-2007 Ludovic Marcotte
**
** Author: Ludovic Marcotte <ludovic@Sophos.ca>
**
** 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 General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Pantomime/CWIMAPFolder.h>
#import <Pantomime/CWConnection.h>
#import <Pantomime/CWConstants.h>
#import <Pantomime/CWFlags.h>
#import <Pantomime/CWIMAPCacheManager.h>
#import <Pantomime/CWIMAPStore.h>
#import <Pantomime/CWIMAPMessage.h>
#import <Pantomime/CWTCPConnection.h>
#import <Pantomime/NSData+Extensions.h>
#import <Pantomime/NSString+Extensions.h>
//
// Private methods
//
@interface CWIMAPFolder (Private)
- (NSString *) _flagsAsStringFromFlags: (CWFlags *) theFlags;
- (NSData *) _removeInvalidHeadersFromMessage: (NSData *) theMessage;
@end
//
//
//
@implementation CWIMAPFolder
- (id) initWithName: (NSString *) theName
{
self = [super initWithName: theName];
if (self)
{
[self setSelected: NO];
}
return self;
}
//
//
//
- (id) initWithName: (NSString *) theName
mode: (PantomimeFolderMode) theMode
{
self = [self initWithName: theName];
if (self)
{
_mode = theMode;
}
return self;
}
//
//
//
- (void) appendMessageFromRawSource: (NSData *) theData
flags: (CWFlags *) theFlags
{
[self appendMessageFromRawSource: theData
flags: theFlags
internalDate: nil];
}
//
//
//
- (void) appendMessageFromRawSource: (NSData *) theData
flags: (CWFlags *) theFlags
internalDate: (NSCalendarDate *) theDate
{
NSDictionary *aDictionary;
NSString *flagsAsString;
NSData *aData;
if (theFlags)
{
flagsAsString = [self _flagsAsStringFromFlags: theFlags];
}
else
{
flagsAsString = @"";
}
// We remove any invalid headers from our message
aData = [self _removeInvalidHeadersFromMessage: theData];
if (theFlags)
{
aDictionary = [NSDictionary dictionaryWithObjectsAndKeys: aData, @"NSDataToAppend", theData, @"NSData", self, @"Folder", theFlags, @"Flags", nil];
}
else
{
aDictionary = [NSDictionary dictionaryWithObjectsAndKeys: aData, @"NSDataToAppend", theData, @"NSData", self, @"Folder", nil];
}
if (theDate)
{
[_store sendCommand: IMAP_APPEND
info: aDictionary
arguments: @"APPEND \"%@\" (%@) \"%@\" {%d}", // IMAP command
[_name modifiedUTF7String], // folder name
flagsAsString, // flags
[theDate descriptionWithCalendarFormat:@"%d-%b-%Y %H:%M:%S %z"], // internal date
[aData length]]; // length of the data to write
}
else
{
[_store sendCommand: IMAP_APPEND
info: aDictionary
arguments: @"APPEND \"%@\" (%@) {%d}", // IMAP command
[_name modifiedUTF7String], // folder name
flagsAsString, // flags
[aData length]]; // length of the data to write
}
}
//
//
//
- (void) copyMessages: (NSArray *) theMessages
toFolder: (NSString *) theFolder
{
NSMutableString *aMutableString;
NSUInteger i, count;
// We create our message's UID set
aMutableString = [[NSMutableString alloc] init];
count = [theMessages count];
for (i = 0; i < count; i++)
{
if (i == count-1)
{
[aMutableString appendFormat: @"%lu",
(unsigned long)[[theMessages objectAtIndex: i] UID]];
}
else
{
[aMutableString appendFormat: @"%lu,",
(unsigned long)[[theMessages objectAtIndex: i] UID]];
}
}
// We send our IMAP command
[_store sendCommand: IMAP_UID_COPY
info: [NSDictionary dictionaryWithObjectsAndKeys: theMessages, @"Messages", theFolder, @"Name", self, @"Folder", nil]
arguments: @"UID COPY %@ \"%@\"",
aMutableString,
[theFolder modifiedUTF7String]];
RELEASE(aMutableString);
}
//
//
//
- (void) prefetch
{
// We first update the messages in our cache, if we need to.
if (_cacheManager && [self count])
{
[_store sendCommand: IMAP_UID_SEARCH info: nil arguments: @"UID SEARCH 1:*"];
}
else
{
//
// We must send this command since our IMAP cache might be empty (or have been removed).
// In that case, we much fetch again all messages, starting at UID 1.
//
[_store sendCommand: IMAP_UID_FETCH_HEADER_FIELDS info: nil arguments: @"UID FETCH %u:* (UID FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (From To Cc Subject Date Message-ID References In-Reply-To)])", 1];
}
}
//
// This method simply close the selected mailbox (ie. folder)
//
- (void) close
{
IMAPCommand theCommand;
if (![self selected])
{
[_store removeFolderFromOpenFolders: self];
return;
}
// If we are opening a mailbox but -close was called before we
// finished opening it, we close the connection immediately.
theCommand = [[self store] lastCommand];
if (theCommand == IMAP_SELECT || theCommand == IMAP_UID_SEARCH || theCommand == IMAP_UID_SEARCH_ANSWERED ||
theCommand == IMAP_UID_SEARCH_FLAGGED || theCommand == IMAP_UID_SEARCH_UNSEEN)
{
[_store removeFolderFromOpenFolders: self];
[[self store] cancelRequest];
[[self store] reconnect];
return;
}
if (_cacheManager)
{
[_cacheManager synchronize];
}
// We set the _folder ivar to nil for all messages. This is required in case
// an IMAPMessage instance was retained and we invoke -setFlags: on it, which
// will try to access the _folder ivar in order to communicate with the IMAP server.
[allMessages makeObjectsPerformSelector: @selector(setFolder:) withObject: nil];
// We close the selected IMAP folder to _expunge_ messages marked as \Deleted
// if and only we are NOT showing DELETED messages. We also don't send the command
// if we are NOT connected since a MUA using Pantomime needs to call -close
// on IMAPFolder to clean-up the "open" folder.
if ([_store isConnected] && ![self showDeleted])
{
[_store sendCommand: IMAP_CLOSE
info: [NSDictionary dictionaryWithObject: self forKey: @"Folder"]
arguments: @"CLOSE"];
}
else
{
PERFORM_SELECTOR_2([_store delegate], @selector(folderCloseCompleted:), PantomimeFolderCloseCompleted, self, @"Folder");
POST_NOTIFICATION(PantomimeFolderCloseCompleted, _store, [NSDictionary dictionaryWithObject: self forKey: @"Folder"]);
}
[_store removeFolderFromOpenFolders: self];
}
//
// This method returns all messages that have the flag PantomimeDeleted.
//
- (void) expunge
{
//
// We send our EXPUNGE command. The responses will be processed in IMAPStore and
// the MSN will be updated in IMAPStore: -_parseExpunge.
//
[_store sendCommand: IMAP_EXPUNGE info: nil arguments: @"EXPUNGE"];
}
//
//
//
- (unsigned int) UIDValidity
{
return _uid_validity;
}
//
//
//
- (void) setUIDValidity: (unsigned int) theUIDValidity
{
_uid_validity = theUIDValidity;
if (_cacheManager)
{
if ([_cacheManager UIDValidity] == 0 || [_cacheManager UIDValidity] != _uid_validity)
{
[_cacheManager invalidate];
[_cacheManager setUIDValidity: _uid_validity];
}
}
}
//
//
//
- (BOOL) selected
{
return _selected;
}
//
//
//
- (void) setSelected: (BOOL) theBOOL
{
_selected = theBOOL;
}
//
//
//
- (void) setFlags: (CWFlags *) theFlags
messages: (NSArray *) theMessages
{
NSMutableString *aMutableString, *aSequenceSet;
CWIMAPMessage *aMessage;
if ([theMessages count] == 1)
{
aMessage = [theMessages lastObject];
// We set the flags right away, just in case someone asks for them
// just after invoking this method. Nevertheless, they WILL be set
// in IMAPStore: -_parseOK:.
// We do the same below, when the count > 1
[[aMessage flags] replaceWithFlags: theFlags];
aSequenceSet = [NSMutableString stringWithFormat: @"%lu:%lu",
(unsigned long)[aMessage UID], (unsigned long)[aMessage UID]];
}
else
{
NSUInteger i, count;
aSequenceSet = AUTORELEASE([[NSMutableString alloc] init]);
count = [theMessages count];
for (i = 0; i < count; i++)
{
aMessage = [theMessages objectAtIndex: i];
[[aMessage flags] replaceWithFlags: theFlags];
if (aMessage == [theMessages lastObject])
{
[aSequenceSet appendFormat: @"%lu", (unsigned long)[aMessage UID]];
}
else
{
[aSequenceSet appendFormat: @"%lu,", (unsigned long)[aMessage UID]];
}
}
}
aMutableString = [[NSMutableString alloc] init];
//
// If we're removing all flags, we rather send a STORE -FLAGS (<current flags>)
// than a STORE FLAGS (<new flags>) since some broken servers might not
// support it (like Cyrus v1.5.19 and v1.6.24).
//
if (theFlags->flags == 0)
{
[aMutableString appendFormat: @"UID STORE %@ -FLAGS.SILENT (", aSequenceSet];
[aMutableString appendString: [self _flagsAsStringFromFlags: theFlags]];
[aMutableString appendString: @")"];
}
else
{
[aMutableString appendFormat: @"UID STORE %@ FLAGS.SILENT (", aSequenceSet];
[aMutableString appendString: [self _flagsAsStringFromFlags: theFlags]];
[aMutableString appendString: @")"];
}
[_store sendCommand: IMAP_UID_STORE
info: [NSDictionary dictionaryWithObjectsAndKeys: theMessages, @"Messages", theFlags, @"Flags", nil]
arguments: aMutableString];
RELEASE(aMutableString);
}
//
// Using IMAP, we ignore most parameters.
//
- (void) search: (NSString *) theString
mask: (PantomimeSearchMask) theMask
options: (PantomimeSearchOption) theOptions
{
NSString *aString;
switch (theMask)
{
case PantomimeFrom:
aString = [NSString stringWithFormat: @"UID SEARCH ALL FROM \"%@\"", theString];
break;
case PantomimeTo:
aString = [NSString stringWithFormat: @"UID SEARCH ALL TO \"%@\"", theString];
break;
case PantomimeContent:
aString = [NSString stringWithFormat: @"UID SEARCH ALL BODY \"%@\"", theString];
break;
case PantomimeSubject:
default:
aString = [NSString stringWithFormat: @"UID SEARCH ALL SUBJECT \"%@\"", theString];
}
// We send our SEARCH command. Store->searchResponse will have the result.
[_store sendCommand: IMAP_UID_SEARCH_ALL info: [NSDictionary dictionaryWithObject: self forKey: @"Folder"] arguments: aString];
}
@end
//
// Private methods
//
@implementation CWIMAPFolder (Private)
- (NSString *) _flagsAsStringFromFlags: (CWFlags *) theFlags
{
NSMutableString *aMutableString;
aMutableString = [[NSMutableString alloc] init];
AUTORELEASE(aMutableString);
if ([theFlags contain: PantomimeAnswered])
{
[aMutableString appendString: @"\\Answered "];
}
if ([theFlags contain: PantomimeDraft] )
{
[aMutableString appendString: @"\\Draft "];
}
if ([theFlags contain: PantomimeFlagged])
{
[aMutableString appendString: @"\\Flagged "];
}
if ([theFlags contain: PantomimeSeen])
{
[aMutableString appendString: @"\\Seen "];
}
if ([theFlags contain: PantomimeDeleted])
{
[aMutableString appendString: @"\\Deleted "];
}
return [aMutableString stringByTrimmingWhiteSpaces];
}
//
//
//
- (NSData *) _removeInvalidHeadersFromMessage: (NSData *) theMessage
{
NSMutableData *aMutableData;
NSArray *allLines;
NSUInteger i, count;
// We allocate our mutable data object
aMutableData = [[NSMutableData alloc] initWithCapacity: [theMessage length]];
// We now replace all \n by \r\n
allLines = [theMessage componentsSeparatedByCString: "\n"];
count = [allLines count];
for (i = 0; i < count; i++)
{
NSData *aLine;
// We get a line...
aLine = [allLines objectAtIndex: i];
// We skip dumb headers
if ([aLine hasCPrefix: "From "])
{
continue;
}
[aMutableData appendData: aLine];
[aMutableData appendCString: "\r\n"];
}
return AUTORELEASE(aMutableData);
}
@end
|