| 1 |
/* |
| 2 |
** CWIMAPStore.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2016-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/CWIMAPStore.h> |
| 25 |
|
| 26 |
#import <Pantomime/CWConstants.h> |
| 27 |
#import <Pantomime/CWFlags.h> |
| 28 |
#import <Pantomime/CWFolderInformation.h> |
| 29 |
#import <Pantomime/CWIMAPCacheManager.h> |
| 30 |
#import <Pantomime/CWIMAPFolder.h> |
| 31 |
#import <Pantomime/CWIMAPMessage.h> |
| 32 |
#import <Pantomime/CWMD5.h> |
| 33 |
#import <Pantomime/CWMIMEUtility.h> |
| 34 |
#import <Pantomime/NSData+Extensions.h> |
| 35 |
#import <Pantomime/NSScanner+Extensions.h> |
| 36 |
#import <Pantomime/NSString+Extensions.h> |
| 37 |
#import <Pantomime/CWTCPConnection.h> |
| 38 |
#import <Pantomime/CWURLName.h> |
| 39 |
|
| 40 |
#import <Foundation/NSAutoreleasePool.h> |
| 41 |
#import <Foundation/NSBundle.h> |
| 42 |
#import <Foundation/NSCharacterSet.h> |
| 43 |
#import <Foundation/NSException.h> |
| 44 |
#import <Foundation/NSNotification.h> |
| 45 |
#import <Foundation/NSPathUtilities.h> |
| 46 |
#import <Foundation/NSScanner.h> |
| 47 |
#import <Foundation/NSValue.h> |
| 48 |
|
| 49 |
#include <ctype.h> |
| 50 |
#include <stdio.h> |
| 51 |
|
| 52 |
// |
| 53 |
// Some static variables used to enhance the performance. |
| 54 |
// |
| 55 |
static NSStringEncoding defaultCStringEncoding; |
| 56 |
static NSData *CRLF; |
| 57 |
|
| 58 |
// |
| 59 |
// This C function is used to verify if a line (specified in |
| 60 |
// "buf", with length "c") has a literal. If it does, the |
| 61 |
// value of the literal is returned. |
| 62 |
// |
| 63 |
// "0" means no literal. |
| 64 |
// |
| 65 |
static inline int has_literal(char *buf, unsigned c) |
| 66 |
{ |
| 67 |
char *s; |
| 68 |
|
| 69 |
if (c == 0 || *buf != '*') return 0; |
| 70 |
|
| 71 |
s = buf+c-1; |
| 72 |
|
| 73 |
if (*s == '}') |
| 74 |
{ |
| 75 |
int value, d; |
| 76 |
|
| 77 |
value = 0; |
| 78 |
d = 1; |
| 79 |
s--; |
| 80 |
|
| 81 |
while (isdigit((int)(unsigned char)*s)) |
| 82 |
{ |
| 83 |
value += ((*s-48) * d); |
| 84 |
d *= 10; |
| 85 |
s--; |
| 86 |
} |
| 87 |
|
| 88 |
//NSLog(@"LITERAL = %d", value); |
| 89 |
|
| 90 |
return value; |
| 91 |
} |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
// |
| 97 |
// |
| 98 |
// |
| 99 |
@interface CWIMAPQueueObject : NSObject |
| 100 |
{ |
| 101 |
@public |
| 102 |
NSMutableDictionary *info; |
| 103 |
IMAPCommand command; |
| 104 |
NSString *arguments; |
| 105 |
NSData *tag; |
| 106 |
int literal; |
| 107 |
} |
| 108 |
- (id) initWithCommand: (IMAPCommand) theCommand |
| 109 |
arguments: (NSString *) theArguments |
| 110 |
tag: (NSData *) theTag |
| 111 |
info: (NSDictionary *) theInfo; |
| 112 |
@end |
| 113 |
|
| 114 |
@implementation CWIMAPQueueObject |
| 115 |
|
| 116 |
- (id) initWithCommand: (IMAPCommand) theCommand |
| 117 |
arguments: (NSString *) theArguments |
| 118 |
tag: (NSData *) theTag |
| 119 |
info: (NSDictionary *) theInfo |
| 120 |
{ |
| 121 |
self = [super init]; |
| 122 |
if (self) |
| 123 |
{ |
| 124 |
command = theCommand; |
| 125 |
literal = 0; |
| 126 |
|
| 127 |
ASSIGN(arguments, theArguments); |
| 128 |
ASSIGN(tag, theTag); |
| 129 |
|
| 130 |
if (theInfo) |
| 131 |
{ |
| 132 |
info = [[NSMutableDictionary alloc] initWithDictionary: theInfo]; |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
info = [[NSMutableDictionary alloc] init]; |
| 137 |
} |
| 138 |
} |
| 139 |
return self; |
| 140 |
} |
| 141 |
|
| 142 |
- (void) dealloc |
| 143 |
{ |
| 144 |
RELEASE(arguments); |
| 145 |
RELEASE(info); |
| 146 |
RELEASE(tag); |
| 147 |
[super dealloc]; |
| 148 |
} |
| 149 |
|
| 150 |
- (NSString *) description |
| 151 |
{ |
| 152 |
return [NSString stringWithFormat: @"%d %@", command, arguments]; |
| 153 |
} |
| 154 |
@end |
| 155 |
|
| 156 |
|
| 157 |
// |
| 158 |
// Private methods |
| 159 |
// |
| 160 |
@interface CWIMAPStore (Private) |
| 161 |
- (NSString *) _folderNameFromString: (NSString *) theString; |
| 162 |
- (void) _parseFlags: (NSString *) aString |
| 163 |
message: (CWIMAPMessage *) theMessage |
| 164 |
record: (cache_record *) theRecord; |
| 165 |
- (void) _renameFolder; |
| 166 |
- (NSArray *) _uniqueIdentifiersFromData: (NSData *) theData; |
| 167 |
- (void) _parseAUTHENTICATE_CRAM_MD5; |
| 168 |
- (void) _parseAUTHENTICATE_LOGIN; |
| 169 |
- (void) _parseBAD; |
| 170 |
- (void) _parseBYE; |
| 171 |
- (void) _parseCAPABILITY; |
| 172 |
- (void) _parseEXISTS; |
| 173 |
- (void) _parseEXPUNGE; |
| 174 |
- (void) _parseFETCH: (unsigned) theMSN; |
| 175 |
- (void) _parseLIST; |
| 176 |
- (void) _parseLSUB; |
| 177 |
- (void) _parseNO; |
| 178 |
- (void) _parseNOOP; |
| 179 |
- (void) _parseOK; |
| 180 |
- (void) _parseRECENT; |
| 181 |
- (void) _parseSEARCH; |
| 182 |
- (void) _parseSEARCH_CACHE; |
| 183 |
- (void) _parseSELECT; |
| 184 |
- (void) _parseSTATUS; |
| 185 |
- (void) _parseSTARTTLS; |
| 186 |
- (void) _parseUIDVALIDITY: (const char *) theString; |
| 187 |
- (void) _restoreQueue; |
| 188 |
@end |
| 189 |
|
| 190 |
// |
| 191 |
// |
| 192 |
// |
| 193 |
@implementation CWIMAPStore |
| 194 |
|
| 195 |
+ (void) initialize |
| 196 |
{ |
| 197 |
defaultCStringEncoding = [NSString defaultCStringEncoding]; |
| 198 |
CRLF = [[NSData alloc] initWithBytes: "\r\n" length: 2]; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
// |
| 203 |
// |
| 204 |
// |
| 205 |
- (id) initWithName: (NSString *) theName |
| 206 |
port: (unsigned int) thePort |
| 207 |
{ |
| 208 |
if (thePort == 0) thePort = 143; |
| 209 |
|
| 210 |
self = [super initWithName: theName port: thePort]; |
| 211 |
if (self) |
| 212 |
{ |
| 213 |
_folderSeparator = 0; |
| 214 |
_selectedFolder = nil; |
| 215 |
_tag = 1; |
| 216 |
|
| 217 |
_folders = [[NSMutableDictionary alloc] init]; |
| 218 |
_openFolders = [[NSMutableDictionary alloc] init]; |
| 219 |
_subscribedFolders = [[NSMutableArray alloc] init]; |
| 220 |
_folderStatus = [[NSMutableDictionary alloc] init]; |
| 221 |
|
| 222 |
_lastCommand = IMAP_AUTHORIZATION; |
| 223 |
_currentQueueObject = nil; |
| 224 |
} |
| 225 |
return self; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
// |
| 230 |
// |
| 231 |
// |
| 232 |
- (id) initWithURL: (CWURLName *) theURL |
| 233 |
{ |
| 234 |
return [self initWithName: [theURL host] port: 143]; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
// |
| 239 |
// |
| 240 |
// |
| 241 |
- (void) dealloc |
| 242 |
{ |
| 243 |
//NSLog(@"IMAPStore: -dealloc"); |
| 244 |
RELEASE(_folders); |
| 245 |
RELEASE(_folderStatus); |
| 246 |
RELEASE(_openFolders); |
| 247 |
RELEASE(_subscribedFolders); |
| 248 |
[super dealloc]; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
// |
| 253 |
// When this method is called, we are receiving bytes |
| 254 |
// from the _lastCommand. |
| 255 |
// |
| 256 |
// Rationale: |
| 257 |
// |
| 258 |
// This command accumulates the responses (split into lines) |
| 259 |
// from the server in _responsesFromServer. |
| 260 |
// |
| 261 |
// It will NOT add untagged reponses but rather process them right-away. |
| 262 |
// |
| 263 |
// If it's receiving a FETCH response, it will NOT verify for |
| 264 |
// tag line ('0123 OK', '0123 BAD', '0123 NO') for the duration |
| 265 |
// of reading the literal length. For example, if we got {400}, |
| 266 |
// we will not consider a '0123 OK' response if we read less |
| 267 |
// than 400 bytes. This prevent us from reading a '0123 OK' that |
| 268 |
// could occur in a message. |
| 269 |
// |
| 270 |
- (void) updateRead |
| 271 |
{ |
| 272 |
id aData; |
| 273 |
|
| 274 |
NSUInteger i, count; |
| 275 |
char *buf; |
| 276 |
|
| 277 |
[super updateRead]; |
| 278 |
|
| 279 |
//NSLog(@"_rbul len == %d |%@|", [_rbuf length], [_rbuf asciiString]); |
| 280 |
|
| 281 |
if (![_rbuf length]) return; |
| 282 |
|
| 283 |
while ((aData = split_lines(_rbuf))) |
| 284 |
{ |
| 285 |
//NSLog(@"aLine = |%@|", [aData asciiString]); |
| 286 |
buf = (char *)[aData bytes]; |
| 287 |
count = [aData length]; |
| 288 |
|
| 289 |
// If we are reading a literal, do so. |
| 290 |
if (_currentQueueObject && _currentQueueObject->literal) |
| 291 |
{ |
| 292 |
_currentQueueObject->literal -= (count+2); |
| 293 |
//NSLog(@"literal = %d, count = %d", _currentQueueObject->literal, count); |
| 294 |
|
| 295 |
if (_currentQueueObject->literal < 0) |
| 296 |
{ |
| 297 |
NSUInteger x; |
| 298 |
|
| 299 |
NSAssert(_currentQueueObject->literal <= 2, @"Negative literal too small"); |
| 300 |
x = (NSUInteger)(-2 - _currentQueueObject->literal); |
| 301 |
[[_currentQueueObject->info objectForKey: @"NSData"] appendData: [aData subdataToIndex: x]]; |
| 302 |
[_responsesFromServer addObject: [aData subdataFromIndex: x]]; |
| 303 |
//NSLog(@"orig = |%@|, chooped = |%@| |%@|", [aData asciiString], [[aData subdataToIndex: x] asciiString], [[aData subdataFromIndex: x] asciiString]); |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
[[_currentQueueObject->info objectForKey: @"NSData"] appendData: aData]; |
| 308 |
} |
| 309 |
|
| 310 |
// We are done reading a literal. Let's read again |
| 311 |
// to see if we got a full response. |
| 312 |
if (_currentQueueObject->literal <= 0) |
| 313 |
{ |
| 314 |
//NSLog(@"DONE ACCUMULATING LITTERAL!\nread = |%@|", [[_currentQueueObject->info objectForKey: @"NSData"] asciiString]); |
| 315 |
// |
| 316 |
// Let's see, if we can, what does the next line contain. If we got |
| 317 |
// something, we add this to the remaining _responsesFromServer |
| 318 |
// and we are ready to parse that response (_responsesFromServer + bytes of literal). |
| 319 |
// |
| 320 |
// If it's nil, that's because we have nothing to read. In that case, just loop |
| 321 |
// and call -updateRead in order to read the rest of the response. |
| 322 |
// |
| 323 |
// We must also be careful about what we read. Microsoft Exchange sometimes send us |
| 324 |
// stuff like this: |
| 325 |
// |
| 326 |
// * 5 FETCH (BODY[TEXT] {1175} |
| 327 |
// <!DOCTYPE HTML ... |
| 328 |
// ... |
| 329 |
// </HTML> UID 5) |
| 330 |
// 0010 OK FETCH completed. |
| 331 |
// |
| 332 |
// The "</HTML> UID 5)" line will result in a _negative_ literal. Which we |
| 333 |
// handle well here and just a couple of lines above this one. |
| 334 |
// |
| 335 |
if (_currentQueueObject->literal < 0) |
| 336 |
{ |
| 337 |
_currentQueueObject->literal = 0; |
| 338 |
} |
| 339 |
else |
| 340 |
{ |
| 341 |
// We MUST wait until we are done reading our full |
| 342 |
// FETCH response. _rbuf could end immediately at the |
| 343 |
// end of our literal response and we need to call |
| 344 |
// [super updateRead] to get more bytes from the socket |
| 345 |
// in order to read the rest (")" or " UID 123)" for example). |
| 346 |
while (!(aData = split_lines(_rbuf))) |
| 347 |
{ |
| 348 |
//SLog(@"NOTHING TO READ! WAITING..."); |
| 349 |
[super updateRead]; |
| 350 |
} |
| 351 |
[_responsesFromServer addObject: aData]; |
| 352 |
} |
| 353 |
|
| 354 |
// |
| 355 |
// Let's rollback in what are processing/read in order to |
| 356 |
// reparse our initial response. It's if it's FETCH response, |
| 357 |
// the literal will now be 0 so the parsing of this response |
| 358 |
// will occur. |
| 359 |
// |
| 360 |
aData = [_responsesFromServer objectAtIndex: 0]; |
| 361 |
buf = (char *)[aData bytes]; |
| 362 |
count = [aData length]; |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
//NSLog(@"Accumulating... %d remaining...", _currentQueueObject->literal); |
| 367 |
// |
| 368 |
// We are still accumulating bytes of the literal. Once we have appended |
| 369 |
// our CRLF, we just continue the loop since there's no need to try to |
| 370 |
// parse anything, as we don't have the complete response yet. |
| 371 |
// |
| 372 |
[[_currentQueueObject->info objectForKey: @"NSData"] appendData: CRLF]; |
| 373 |
continue; |
| 374 |
} |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
//NSLog(@"aLine = |%@|", [aData asciiString]); |
| 379 |
[_responsesFromServer addObject: aData]; |
| 380 |
|
| 381 |
if (_currentQueueObject && (_currentQueueObject->literal = has_literal(buf, count))) |
| 382 |
{ |
| 383 |
//NSLog(@"literal = %d", _currentQueueObject->literal); |
| 384 |
[_currentQueueObject->info setObject: [NSMutableData dataWithCapacity: _currentQueueObject->literal] |
| 385 |
forKey: @"NSData"]; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
// Now search for the position of the first space in our response. |
| 390 |
i = 0; |
| 391 |
while (i < count && *buf != ' ') |
| 392 |
{ |
| 393 |
buf++; i++; |
| 394 |
} |
| 395 |
|
| 396 |
//NSLog(@"i = %d count = %d", i, count); |
| 397 |
|
| 398 |
// |
| 399 |
// We got an untagged response or a command continuation request. |
| 400 |
// |
| 401 |
if (i == 1) |
| 402 |
{ |
| 403 |
unsigned d, msn, len; |
| 404 |
unsigned j; |
| 405 |
BOOL b; |
| 406 |
|
| 407 |
// |
| 408 |
// We verify if we received a command continuation request. |
| 409 |
// This response is used in the AUTHENTICATE command or |
| 410 |
// in any argument to the command is a literal. In the current |
| 411 |
// code, the only command which has a literal argument is |
| 412 |
// the APPEND command. We must NOT use "break;" at the very |
| 413 |
// end of this block since we could read a line in a mail |
| 414 |
// that begins with a '+'. |
| 415 |
// |
| 416 |
if (*(buf-i) == '+') |
| 417 |
{ |
| 418 |
if (_currentQueueObject && _lastCommand == IMAP_APPEND) |
| 419 |
{ |
| 420 |
[self writeData: [_currentQueueObject->info objectForKey: @"NSDataToAppend"]]; |
| 421 |
[self writeData: CRLF]; |
| 422 |
break; |
| 423 |
} |
| 424 |
else if (_lastCommand == IMAP_AUTHENTICATE_CRAM_MD5) |
| 425 |
{ |
| 426 |
[self _parseAUTHENTICATE_CRAM_MD5]; |
| 427 |
break; |
| 428 |
} |
| 429 |
else if (_lastCommand == IMAP_AUTHENTICATE_LOGIN) |
| 430 |
{ |
| 431 |
[self _parseAUTHENTICATE_LOGIN]; |
| 432 |
break; |
| 433 |
} |
| 434 |
else if (_currentQueueObject && _lastCommand == IMAP_LOGIN) |
| 435 |
{ |
| 436 |
//NSLog(@"writing password |%s|", [[_currentQueueObject->info objectForKey: @"Password"] cString]); |
| 437 |
[self writeData: [_currentQueueObject->info objectForKey: @"Password"]]; |
| 438 |
[self writeData: CRLF]; |
| 439 |
break; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
msn = 0; b = YES; d = 1; |
| 444 |
j = i+1; buf++; |
| 445 |
|
| 446 |
// Let's see if we can read a MSN |
| 447 |
while (j < count && *buf != ' ') |
| 448 |
{ |
| 449 |
if (!isdigit((int)(unsigned char)*buf)) b = NO; |
| 450 |
buf++; j++; |
| 451 |
} |
| 452 |
|
| 453 |
//NSLog(@"j = %d, b = %d", j, b); |
| 454 |
|
| 455 |
// |
| 456 |
// The token following our "*" is all-digit. Let's |
| 457 |
// decode the MSN and get the kind of response. |
| 458 |
// |
| 459 |
// We will also read the untagged responses we get |
| 460 |
// when SELECT'ing a mailbox ("* 4 EXISTS" for example). |
| 461 |
// |
| 462 |
// We parse those results but we ignore the "MSN" since |
| 463 |
// it bears no relation to an actual MSN. |
| 464 |
// |
| 465 |
if (b) |
| 466 |
{ |
| 467 |
unsigned k; |
| 468 |
|
| 469 |
k = j; |
| 470 |
|
| 471 |
// We compute the MSN |
| 472 |
while (k > i+1) |
| 473 |
{ |
| 474 |
buf--; k--; |
| 475 |
//NSLog(@"msn c = %c", *buf); |
| 476 |
msn += ((*buf-48) * d); |
| 477 |
d *= 10; |
| 478 |
} |
| 479 |
|
| 480 |
//NSLog(@"Done computing the msn = %d k = %d", msn, k); |
| 481 |
|
| 482 |
// We now get what kind of response we read (FETCH, etc?) |
| 483 |
buf += (j-i); |
| 484 |
k = j+1; |
| 485 |
|
| 486 |
while (k < count && isalpha((int)(unsigned char)*buf)) |
| 487 |
{ |
| 488 |
//NSLog(@"response after c = %c", *buf); |
| 489 |
buf++; k++; |
| 490 |
} |
| 491 |
|
| 492 |
//NSLog(@"Done reading response: i = %d j = %d k = %d", i, j, k); |
| 493 |
|
| 494 |
buf = buf-k+j+1; |
| 495 |
len = k-j-1; |
| 496 |
} |
| 497 |
// |
| 498 |
// It's NOT all-digit. |
| 499 |
// |
| 500 |
else |
| 501 |
{ |
| 502 |
buf = buf-j+i+1; |
| 503 |
len = j-i-1; |
| 504 |
} |
| 505 |
|
| 506 |
//NSData *foo; |
| 507 |
//foo = [NSData dataWithBytes: buf length: len]; |
| 508 |
//NSLog(@"DONE!!! foo after * = |%@| b = %d, msn = %d", [foo asciiString], b, msn); |
| 509 |
//NSLog(@"len = %d", len); |
| 510 |
|
| 511 |
// |
| 512 |
// We got an untagged OK response. We handle only the one used in the IMAP authorization |
| 513 |
// state and ignore the ones required during a SELECT command (like OK [UNSEEN <n>]). |
| 514 |
// |
| 515 |
if (len && strncasecmp("OK", buf, 2) == 0 && _lastCommand == IMAP_AUTHORIZATION) |
| 516 |
{ |
| 517 |
[self _parseOK]; |
| 518 |
} |
| 519 |
// |
| 520 |
// We check if we got disconnected from the IMAP server. |
| 521 |
// If it's the case, we invoke -reconnect. |
| 522 |
// |
| 523 |
else if (len && strncasecmp("BYE", buf, 3) == 0) |
| 524 |
{ |
| 525 |
[self _parseBYE]; |
| 526 |
} |
| 527 |
// |
| 528 |
// |
| 529 |
// |
| 530 |
else if (len && strncasecmp("LIST", buf, 4) == 0) |
| 531 |
{ |
| 532 |
[self _parseLIST]; |
| 533 |
} |
| 534 |
// |
| 535 |
// |
| 536 |
// |
| 537 |
else if (len && strncasecmp("LSUB", buf, 4) == 0) |
| 538 |
{ |
| 539 |
[self _parseLSUB]; |
| 540 |
} |
| 541 |
// |
| 542 |
// We got a FETCH response and we are done reading all |
| 543 |
// bytes specified by our literal. We also handle |
| 544 |
// untagged responses coming AFTER a tagged response, |
| 545 |
// like that: |
| 546 |
// |
| 547 |
// 000c UID FETCH 3071053:3071053 BODY[TEXT] |
| 548 |
// * 1 FETCH (UID 3071053 BODY[TEXT] {859} |
| 549 |
// f00 bar zarb |
| 550 |
// .. |
| 551 |
// ) |
| 552 |
// 000c OK UID FETCH completed |
| 553 |
// * 1 FETCH (FLAGS (\Seen)) |
| 554 |
// |
| 555 |
// Responses like that must be carefully handled since |
| 556 |
// _currentQueueObject would nil after getting the |
| 557 |
// tagged response. |
| 558 |
// |
| 559 |
else if (len && strncasecmp("FETCH", buf, 5) == 0 && |
| 560 |
(!_currentQueueObject || (_currentQueueObject && _currentQueueObject->literal == 0))) |
| 561 |
{ |
| 562 |
[self _parseFETCH: msn]; |
| 563 |
} |
| 564 |
// |
| 565 |
// |
| 566 |
// |
| 567 |
else if (len && strncasecmp("EXISTS", buf, 6) == 0) |
| 568 |
{ |
| 569 |
[self _parseEXISTS]; |
| 570 |
[_responsesFromServer removeLastObject]; |
| 571 |
} |
| 572 |
// |
| 573 |
// |
| 574 |
// |
| 575 |
else if (len && strncasecmp("RECENT", buf, 6) == 0) |
| 576 |
{ |
| 577 |
[self _parseRECENT]; |
| 578 |
[_responsesFromServer removeLastObject]; |
| 579 |
} |
| 580 |
// |
| 581 |
// |
| 582 |
// |
| 583 |
else if (len && strncasecmp("SEARCH", buf, 6) == 0) |
| 584 |
{ |
| 585 |
switch (_lastCommand) |
| 586 |
{ |
| 587 |
case IMAP_UID_SEARCH: |
| 588 |
case IMAP_UID_SEARCH_ANSWERED: |
| 589 |
case IMAP_UID_SEARCH_FLAGGED: |
| 590 |
case IMAP_UID_SEARCH_UNSEEN: |
| 591 |
[self _parseSEARCH_CACHE]; |
| 592 |
break; |
| 593 |
|
| 594 |
default: |
| 595 |
[self _parseSEARCH]; |
| 596 |
} |
| 597 |
} |
| 598 |
// |
| 599 |
// |
| 600 |
// |
| 601 |
else if (len && strncasecmp("STATUS", buf, 6) == 0) |
| 602 |
{ |
| 603 |
[self _parseSTATUS]; |
| 604 |
} |
| 605 |
// |
| 606 |
// |
| 607 |
// |
| 608 |
else if (len && strncasecmp("EXPUNGE", buf, 7) == 0) |
| 609 |
{ |
| 610 |
[self _parseEXPUNGE]; |
| 611 |
} |
| 612 |
// |
| 613 |
// |
| 614 |
// |
| 615 |
else if (len && strncasecmp("CAPABILITY", buf, 10) == 0) |
| 616 |
{ |
| 617 |
[self _parseCAPABILITY]; |
| 618 |
} |
| 619 |
else |
| 620 |
{ |
| 621 |
NSDebugLog(@"IMAP Unknown tagged response: %s", buf); |
| 622 |
} |
| 623 |
} |
| 624 |
// |
| 625 |
// We got a tagged response |
| 626 |
// |
| 627 |
else |
| 628 |
{ |
| 629 |
unsigned j; |
| 630 |
|
| 631 |
//NSData *foo; |
| 632 |
//foo = [NSData dataWithBytes: buf-i length: i]; |
| 633 |
//NSLog(@"tag = |%@|", [foo asciiString]); |
| 634 |
|
| 635 |
j = i+1; |
| 636 |
buf++; |
| 637 |
|
| 638 |
// We read past our tag, in order to find |
| 639 |
// the type of response (OK/NO/BAD). |
| 640 |
while (j < count && *buf != ' ') |
| 641 |
{ |
| 642 |
//NSLog(@"IN OK: %c", *buf); |
| 643 |
buf++; j++; |
| 644 |
} |
| 645 |
|
| 646 |
//NSLog(@"OK/NO/BAD response = |%@|", [[NSData dataWithBytes: buf-j+i+1 length: j-i-1] asciiString]); |
| 647 |
buf = buf-j+i+1; |
| 648 |
|
| 649 |
// From RFC3501: |
| 650 |
// |
| 651 |
// The server completion result response indicates the success or |
| 652 |
// failure of the operation. It is tagged with the same tag as the |
| 653 |
// client command which began the operation. Thus, if more than one |
| 654 |
// command is in progress, the tag in a server completion response |
| 655 |
// identifies the command to which the response applies. There are |
| 656 |
// three possible server completion responses: OK (indicating success), |
| 657 |
// NO (indicating failure), or BAD (indicating a protocol error such as |
| 658 |
// unrecognized command or command syntax error). |
| 659 |
// |
| 660 |
if (strncasecmp("OK", buf, 2) == 0) |
| 661 |
{ |
| 662 |
[self _parseOK]; |
| 663 |
} |
| 664 |
// |
| 665 |
// RFC3501 says: |
| 666 |
// |
| 667 |
// The NO response indicates an operational error message from the |
| 668 |
// server. When tagged, it indicates unsuccessful completion of the |
| 669 |
// associated command. The untagged form indicates a warning; the |
| 670 |
// command can still complete successfully. The human-readable text |
| 671 |
// describes the condition. |
| 672 |
// |
| 673 |
else if (strncasecmp("NO", buf, 2) == 0) |
| 674 |
{ |
| 675 |
[self _parseNO]; |
| 676 |
} |
| 677 |
else |
| 678 |
{ |
| 679 |
[self _parseBAD]; |
| 680 |
} |
| 681 |
} |
| 682 |
} // while ((aData = split_lines... |
| 683 |
|
| 684 |
//NSLog(@"While loop broken!"); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
// |
| 689 |
// This method authenticates the Store to the IMAP server. |
| 690 |
// In case of an error, it returns NO. |
| 691 |
// |
| 692 |
// FIXME: We MUST NOT send a login command if LOGINDISABLED is |
| 693 |
// enforced by the server (6.2.3). |
| 694 |
// |
| 695 |
- (void) authenticate: (NSString*) theUsername |
| 696 |
password: (NSString*) thePassword |
| 697 |
mechanism: (NSString*) theMechanism |
| 698 |
{ |
| 699 |
ASSIGN(_username, theUsername); |
| 700 |
ASSIGN(_password, thePassword); |
| 701 |
ASSIGN(_mechanism, theMechanism); |
| 702 |
|
| 703 |
if (theMechanism && [theMechanism caseInsensitiveCompare: @"CRAM-MD5"] == NSOrderedSame) |
| 704 |
{ |
| 705 |
[self sendCommand: IMAP_AUTHENTICATE_CRAM_MD5 info: nil arguments: @"AUTHENTICATE CRAM-MD5"]; |
| 706 |
return; |
| 707 |
} |
| 708 |
else if (theMechanism && [theMechanism caseInsensitiveCompare: @"LOGIN"] == NSOrderedSame) |
| 709 |
{ |
| 710 |
[self sendCommand: IMAP_AUTHENTICATE_LOGIN info: nil arguments: @"AUTHENTICATE LOGIN"]; |
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
// We must verify if we must quote the password |
| 715 |
if ( nil != thePassword && |
| 716 |
([thePassword rangeOfCharacterFromSet: [NSCharacterSet punctuationCharacterSet]].length || |
| 717 |
[thePassword rangeOfCharacterFromSet: [NSCharacterSet whitespaceCharacterSet]].length)) |
| 718 |
{ |
| 719 |
thePassword = [NSString stringWithFormat: @"\"%@\"", thePassword]; |
| 720 |
} |
| 721 |
else if (![thePassword is7bitSafe]) |
| 722 |
{ |
| 723 |
NSData *aData; |
| 724 |
|
| 725 |
// |
| 726 |
// We support non-ASCII password by using the 8-bit ISO Latin 1 encoding. |
| 727 |
// FIXME: Is there any standard on which encoding to use? |
| 728 |
// |
| 729 |
aData = [thePassword dataUsingEncoding: NSISOLatin1StringEncoding]; |
| 730 |
|
| 731 |
[self sendCommand: IMAP_LOGIN |
| 732 |
info: [NSDictionary dictionaryWithObject: aData forKey: @"Password"] |
| 733 |
arguments: @"LOGIN %@ {%d}", _username, [aData length]]; |
| 734 |
return; |
| 735 |
} |
| 736 |
|
| 737 |
[self sendCommand: IMAP_LOGIN info: nil arguments: @"LOGIN %@ %@", _username, thePassword]; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
// |
| 742 |
// |
| 743 |
// |
| 744 |
- (NSArray *) supportedMechanisms |
| 745 |
{ |
| 746 |
NSMutableArray *aMutableArray; |
| 747 |
NSString *aString; |
| 748 |
NSUInteger i, count;; |
| 749 |
|
| 750 |
aMutableArray = [NSMutableArray array]; |
| 751 |
count = [_capabilities count]; |
| 752 |
|
| 753 |
for (i = 0; i < count; i++) |
| 754 |
{ |
| 755 |
aString = [_capabilities objectAtIndex: i]; |
| 756 |
|
| 757 |
if ([aString hasCaseInsensitivePrefix: @"AUTH="]) |
| 758 |
{ |
| 759 |
[aMutableArray addObject: [aString substringFromIndex: 5]]; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
return aMutableArray; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
// |
| 768 |
// The default folder in IMAP is always Inbox. This method will prefetch |
| 769 |
// the messages of an IMAP folder if they haven't been prefetched before. |
| 770 |
// |
| 771 |
- (id) defaultFolder |
| 772 |
{ |
| 773 |
return [self folderForName: @"INBOX"]; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
// |
| 778 |
// |
| 779 |
// |
| 780 |
- (id) folderForName: (NSString *) theName |
| 781 |
{ |
| 782 |
return [self folderForName: theName |
| 783 |
mode: PantomimeReadWriteMode |
| 784 |
prefetch: YES]; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
// |
| 789 |
// |
| 790 |
// |
| 791 |
- (CWIMAPFolder *) folderForName: (NSString *) theName |
| 792 |
select: (BOOL) aBOOL |
| 793 |
{ |
| 794 |
if ([_openFolders objectForKey: theName]) |
| 795 |
{ |
| 796 |
return [_openFolders objectForKey: theName]; |
| 797 |
} |
| 798 |
|
| 799 |
if (aBOOL) |
| 800 |
{ |
| 801 |
return [self folderForName: theName]; |
| 802 |
} |
| 803 |
else |
| 804 |
{ |
| 805 |
CWIMAPFolder *aFolder; |
| 806 |
|
| 807 |
aFolder = [[CWIMAPFolder alloc] initWithName: theName]; |
| 808 |
|
| 809 |
[aFolder setStore: self]; |
| 810 |
[aFolder setSelected: NO]; |
| 811 |
return AUTORELEASE(aFolder); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
// |
| 817 |
// |
| 818 |
// |
| 819 |
#warning VERIFY FOR NoSelect |
| 820 |
- (CWIMAPFolder *) folderForName: (NSString *) theName |
| 821 |
mode: (PantomimeFolderMode) theMode |
| 822 |
prefetch: (BOOL) aBOOL |
| 823 |
{ |
| 824 |
CWIMAPFolder *aFolder; |
| 825 |
|
| 826 |
aFolder = [_openFolders objectForKey: theName]; |
| 827 |
|
| 828 |
// Careful here, we might return a non-selected mailbox. |
| 829 |
if (aFolder) |
| 830 |
{ |
| 831 |
return aFolder; |
| 832 |
} |
| 833 |
|
| 834 |
aFolder = [[CWIMAPFolder alloc] initWithName: theName mode: theMode]; |
| 835 |
[aFolder setStore: self]; |
| 836 |
[_openFolders setObject: aFolder forKey: theName]; |
| 837 |
RELEASE(aFolder); |
| 838 |
|
| 839 |
//NSLog(@"_connection_state.opening_mailbox = %d", _connection_state.opening_mailbox); |
| 840 |
|
| 841 |
// If we are already opening a mailbox, we must interrupt the process |
| 842 |
// and open the preferred one instead. |
| 843 |
if (_connection_state.opening_mailbox) |
| 844 |
{ |
| 845 |
// Safety measure - in case close (so -removeFolderFromOpenFolders) |
| 846 |
// on the selected folder wasn't called. |
| 847 |
if (_selectedFolder) |
| 848 |
{ |
| 849 |
[_openFolders removeObjectForKey: [_selectedFolder name]]; |
| 850 |
} |
| 851 |
|
| 852 |
[super cancelRequest]; |
| 853 |
[self reconnect]; |
| 854 |
|
| 855 |
_selectedFolder = aFolder; |
| 856 |
return _selectedFolder; |
| 857 |
} |
| 858 |
|
| 859 |
_connection_state.opening_mailbox = YES; |
| 860 |
|
| 861 |
if (theMode == PantomimeReadOnlyMode) |
| 862 |
{ |
| 863 |
[self sendCommand: IMAP_EXAMINE info: nil arguments: @"EXAMINE \"%@\"", [theName modifiedUTF7String]]; |
| 864 |
} |
| 865 |
else |
| 866 |
{ |
| 867 |
[self sendCommand: IMAP_SELECT info: nil arguments: @"SELECT \"%@\"", [theName modifiedUTF7String]]; |
| 868 |
} |
| 869 |
|
| 870 |
// This folder becomes the selected one. This will have to be improved in the future. |
| 871 |
// No need to retain "aFolder" here. The "_openFolders" dictionary already retains it. |
| 872 |
_selectedFolder = aFolder; |
| 873 |
|
| 874 |
if (aBOOL) |
| 875 |
{ |
| 876 |
[_selectedFolder prefetch]; |
| 877 |
} |
| 878 |
|
| 879 |
return _selectedFolder; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
// |
| 884 |
// |
| 885 |
// |
| 886 |
- (id) folderForURL: (NSString *) theURL |
| 887 |
{ |
| 888 |
CWURLName *theURLName; |
| 889 |
id aFolder; |
| 890 |
|
| 891 |
theURLName = [[CWURLName alloc] initWithString: theURL]; |
| 892 |
|
| 893 |
aFolder = [self folderForName: [theURLName foldername]]; |
| 894 |
|
| 895 |
RELEASE(theURLName); |
| 896 |
|
| 897 |
return aFolder; |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
// |
| 902 |
// When this method is invoked for the first time, it sends a LIST |
| 903 |
// command to the IMAP server and cache the results for subsequent |
| 904 |
// queries. The IMAPStore notifies the delegate once it has parsed |
| 905 |
// all server's responses. |
| 906 |
// |
| 907 |
- (NSEnumerator *) folderEnumerator |
| 908 |
{ |
| 909 |
if (![_folders count]) |
| 910 |
{ |
| 911 |
[self sendCommand: IMAP_LIST info: nil arguments: @"LIST \"\" \"*\""]; |
| 912 |
return nil; |
| 913 |
} |
| 914 |
|
| 915 |
return [_folders keyEnumerator]; |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
// |
| 920 |
// This method works the same way as the -folderEnumerator method. |
| 921 |
// |
| 922 |
- (NSEnumerator *) subscribedFolderEnumerator |
| 923 |
{ |
| 924 |
if (![_subscribedFolders count]) |
| 925 |
{ |
| 926 |
[self sendCommand: IMAP_LSUB info: nil arguments: @"LSUB \"\" \"*\""]; |
| 927 |
return nil; |
| 928 |
} |
| 929 |
|
| 930 |
return [_subscribedFolders objectEnumerator]; |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
// |
| 935 |
// |
| 936 |
// |
| 937 |
- (NSDictionary *) folderStatus: (NSArray *) theArray |
| 938 |
{ |
| 939 |
NSUInteger i; |
| 940 |
|
| 941 |
[_folderStatus removeAllObjects]; |
| 942 |
|
| 943 |
// C: A042 STATUS blurdybloop (UIDNEXT MESSAGES) |
| 944 |
// S: * STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292) |
| 945 |
// S: A042 OK STATUS completed |
| 946 |
// |
| 947 |
// We send: MESSAGES UNSEEN |
| 948 |
for (i = 0; i < [theArray count]; i++) |
| 949 |
{ |
| 950 |
// RFC3501 says we SHOULD NOT call STATUS on the selected mailbox - so we won't do it. |
| 951 |
if (_selectedFolder && [[_selectedFolder name] isEqualToString: [theArray objectAtIndex: i]]) |
| 952 |
{ |
| 953 |
continue; |
| 954 |
} |
| 955 |
|
| 956 |
[self sendCommand: IMAP_STATUS |
| 957 |
info: [NSDictionary dictionaryWithObject: [theArray objectAtIndex: i] forKey: @"Name"] |
| 958 |
arguments: @"STATUS \"%@\" (MESSAGES UNSEEN)", [[theArray objectAtIndex: i] modifiedUTF7String]]; |
| 959 |
} |
| 960 |
|
| 961 |
return _folderStatus; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
// |
| 966 |
// This method appends theCommand to the IMAP tag |
| 967 |
// and sends it to the server. |
| 968 |
// |
| 969 |
// It then sets the last command (w/o the tag) that has been sent. |
| 970 |
// |
| 971 |
// If the server is already processing a query, it queues it in _queue. |
| 972 |
// |
| 973 |
- (void) sendCommand: (IMAPCommand) theCommand info: (NSDictionary *) theInfo arguments: (NSString *) theFormat, ... |
| 974 |
{ |
| 975 |
if (theCommand == IMAP_EMPTY_QUEUE) |
| 976 |
{ |
| 977 |
if ([_queue count]) |
| 978 |
{ |
| 979 |
// We dequeue the first inserted command from the queue. |
| 980 |
_currentQueueObject = [_queue lastObject]; |
| 981 |
} |
| 982 |
else |
| 983 |
{ |
| 984 |
// The queue is empty, we have nothing more to do... |
| 985 |
_currentQueueObject = nil; |
| 986 |
return; |
| 987 |
} |
| 988 |
} |
| 989 |
else |
| 990 |
{ |
| 991 |
CWIMAPQueueObject *aQueueObject; |
| 992 |
NSString *aString; |
| 993 |
va_list args; |
| 994 |
NSUInteger i, count; |
| 995 |
|
| 996 |
//NSLog(@"sendCommand invoked, cmd = %i", theCommand); |
| 997 |
va_start(args, theFormat); |
| 998 |
|
| 999 |
aString = [[NSString alloc] initWithFormat: theFormat arguments: args]; |
| 1000 |
|
| 1001 |
// |
| 1002 |
// We must check in the queue if we aren't trying to add a command that is already there. |
| 1003 |
// This could happend if -rawSource is called in IMAPMessage multiple times before |
| 1004 |
// PantomimeMessageFetchCompleted is sent. |
| 1005 |
// |
| 1006 |
// We skip this verification for the IMAP_APPEND command as a messages with the same size |
| 1007 |
// could be quickly appended to the folder and we do NOT want to skip the second one. |
| 1008 |
// |
| 1009 |
count = [_queue count]; |
| 1010 |
|
| 1011 |
for (i = 0; i < count; i++) |
| 1012 |
{ |
| 1013 |
aQueueObject = [_queue objectAtIndex: i]; |
| 1014 |
if (aQueueObject->command == theCommand && theCommand != IMAP_APPEND && [aQueueObject->arguments isEqualToString: aString]) |
| 1015 |
{ |
| 1016 |
RELEASE(aString); |
| 1017 |
//NSLog(@"A COMMAND ALREADY EXIST!!!!"); |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
aQueueObject = [[CWIMAPQueueObject alloc] initWithCommand: theCommand arguments: aString tag: [self nextTag] info: theInfo]; |
| 1023 |
RELEASE(aString); |
| 1024 |
|
| 1025 |
[_queue insertObject: aQueueObject atIndex: 0]; |
| 1026 |
RELEASE(aQueueObject); |
| 1027 |
|
| 1028 |
//NSLog(@"queue size = %d", [_queue count]); |
| 1029 |
|
| 1030 |
// If we had queued commands, we return since we'll eventually |
| 1031 |
// dequeue them one by one. Otherwise, we run it immediately. |
| 1032 |
if ([_queue count] > 1) |
| 1033 |
{ |
| 1034 |
//NSLog(@"QUEUED |%@|", aString); |
| 1035 |
return; |
| 1036 |
} |
| 1037 |
|
| 1038 |
_currentQueueObject = aQueueObject; |
| 1039 |
} |
| 1040 |
|
| 1041 |
//NSLog(@"Sending |%@|", _currentQueueObject->arguments); |
| 1042 |
_lastCommand = _currentQueueObject->command; |
| 1043 |
|
| 1044 |
[self writeData: _currentQueueObject->tag]; |
| 1045 |
[self writeData: [NSData dataWithBytes: " " length: 1]]; |
| 1046 |
[self writeData: [_currentQueueObject->arguments dataUsingEncoding: defaultCStringEncoding]]; |
| 1047 |
[self writeData: CRLF]; |
| 1048 |
|
| 1049 |
POST_NOTIFICATION(@"PantomimeCommandSent", self, _currentQueueObject->info); |
| 1050 |
PERFORM_SELECTOR_2(_delegate, @selector(commandSent:), @"PantomimeCommandSent", [NSNumber numberWithUnsignedInt: _lastCommand], @"Command"); |
| 1051 |
} |
| 1052 |
|
| 1053 |
// |
| 1054 |
// |
| 1055 |
// |
| 1056 |
- (NSEnumerator *) openFoldersEnumerator |
| 1057 |
{ |
| 1058 |
return [_openFolders objectEnumerator]; |
| 1059 |
} |
| 1060 |
|
| 1061 |
// |
| 1062 |
// |
| 1063 |
// |
| 1064 |
- (void) removeFolderFromOpenFolders: (CWFolder *) theFolder |
| 1065 |
{ |
| 1066 |
if (_selectedFolder == (CWIMAPFolder *)theFolder) |
| 1067 |
{ |
| 1068 |
_selectedFolder = nil; |
| 1069 |
} |
| 1070 |
|
| 1071 |
[_openFolders removeObjectForKey: [theFolder name]]; |
| 1072 |
} |
| 1073 |
|
| 1074 |
// |
| 1075 |
// |
| 1076 |
// |
| 1077 |
- (BOOL) folderForNameIsOpen: (NSString *) theName |
| 1078 |
{ |
| 1079 |
NSEnumerator *anEnumerator; |
| 1080 |
CWIMAPFolder *aFolder; |
| 1081 |
|
| 1082 |
anEnumerator = [self openFoldersEnumerator]; |
| 1083 |
|
| 1084 |
while ((aFolder = [anEnumerator nextObject])) |
| 1085 |
{ |
| 1086 |
if ([[aFolder name] compare: theName |
| 1087 |
options: NSCaseInsensitiveSearch] == NSOrderedSame) |
| 1088 |
{ |
| 1089 |
return YES; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
return NO; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
// |
| 1098 |
// This method verifies in the cache if theName is present. |
| 1099 |
// If so, it returns the associated value. |
| 1100 |
// |
| 1101 |
// If it's not present, it sends a LIST command to the server |
| 1102 |
// and the delegate will eventually be notified when the LIST |
| 1103 |
// command completed. It also returns 0 if it's not present. |
| 1104 |
// |
| 1105 |
- (PantomimeFolderType) folderTypeForFolderName: (NSString *) theName |
| 1106 |
{ |
| 1107 |
id o; |
| 1108 |
|
| 1109 |
o = [_folders objectForKey: theName]; |
| 1110 |
|
| 1111 |
if (o) |
| 1112 |
{ |
| 1113 |
return [o intValue]; |
| 1114 |
} |
| 1115 |
|
| 1116 |
[self sendCommand: IMAP_LIST info: nil arguments: @"LIST \"\" \"%@\"", [theName modifiedUTF7String]]; |
| 1117 |
|
| 1118 |
return 0; |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
// |
| 1123 |
// |
| 1124 |
// |
| 1125 |
- (unichar) folderSeparator |
| 1126 |
{ |
| 1127 |
return _folderSeparator; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
// |
| 1132 |
// |
| 1133 |
// |
| 1134 |
- (NSData *) nextTag |
| 1135 |
{ |
| 1136 |
_tag++; |
| 1137 |
return [self lastTag]; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
// |
| 1142 |
// |
| 1143 |
// |
| 1144 |
- (NSData *) lastTag |
| 1145 |
{ |
| 1146 |
char str[5]; |
| 1147 |
sprintf(str, "%04x", _tag); |
| 1148 |
return [NSData dataWithBytes: str length: 4]; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
// |
| 1153 |
// |
| 1154 |
// |
| 1155 |
- (void) subscribeToFolderWithName: (NSString *) theName |
| 1156 |
{ |
| 1157 |
[self sendCommand: IMAP_SUBSCRIBE |
| 1158 |
info: [NSDictionary dictionaryWithObject: theName forKey: @"Name"] |
| 1159 |
arguments: @"SUBSCRIBE \"%@\"", [theName modifiedUTF7String]]; |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
// |
| 1164 |
// |
| 1165 |
// |
| 1166 |
- (void) unsubscribeToFolderWithName: (NSString *) theName |
| 1167 |
{ |
| 1168 |
[self sendCommand: IMAP_UNSUBSCRIBE |
| 1169 |
info: [NSDictionary dictionaryWithObject: theName forKey: @"Name"] |
| 1170 |
arguments: @"UNSUBSCRIBE \"%@\"", [theName modifiedUTF7String]]; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
// |
| 1175 |
// |
| 1176 |
// |
| 1177 |
- (void) close |
| 1178 |
{ |
| 1179 |
[self sendCommand: IMAP_LOGOUT info: nil arguments: @"LOGOUT"]; |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
// |
| 1184 |
// Create the mailbox and subscribe to it. The full path to the mailbox must |
| 1185 |
// be provided. |
| 1186 |
// |
| 1187 |
// The delegate will be notified when the folder has been created (or not). |
| 1188 |
// |
| 1189 |
- (void) createFolderWithName: (NSString *) theName |
| 1190 |
type: (PantomimeFolderFormat) theType |
| 1191 |
contents: (NSData *) theContents |
| 1192 |
{ |
| 1193 |
[self sendCommand: IMAP_CREATE |
| 1194 |
info: [NSDictionary dictionaryWithObject: theName forKey: @"Name"] |
| 1195 |
arguments: @"CREATE \"%@\"", [theName modifiedUTF7String]]; |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
// |
| 1200 |
// Delete the mailbox. The full path to the mailbox must be provided. |
| 1201 |
// |
| 1202 |
// The delegate will be notified when the folder has been deleted (or not). |
| 1203 |
// |
| 1204 |
- (void) deleteFolderWithName: (NSString *) theName |
| 1205 |
{ |
| 1206 |
[self sendCommand: IMAP_DELETE |
| 1207 |
info: [NSDictionary dictionaryWithObject: theName forKey: @"Name"] |
| 1208 |
arguments: @"DELETE \"%@\"", [theName modifiedUTF7String]]; |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
// |
| 1213 |
// This method is used to rename a folder. |
| 1214 |
// |
| 1215 |
// theName and theNewName MUST be the full path of those mailboxes. |
| 1216 |
// If they begin with the folder separator (ie., '/'), the character is |
| 1217 |
// automatically stripped. |
| 1218 |
// |
| 1219 |
// This method supports renaming SELECT'ed mailboxes. |
| 1220 |
// |
| 1221 |
// The delegate will be notified when the folder has been renamed (or not). |
| 1222 |
// |
| 1223 |
- (void) renameFolderWithName: (NSString *) theName |
| 1224 |
toName: (NSString *) theNewName |
| 1225 |
{ |
| 1226 |
NSDictionary *info; |
| 1227 |
|
| 1228 |
theName = [theName stringByDeletingFirstPathSeparator: _folderSeparator]; |
| 1229 |
theNewName = [theNewName stringByDeletingFirstPathSeparator: _folderSeparator]; |
| 1230 |
info = [NSDictionary dictionaryWithObjectsAndKeys: theName, @"Name", theNewName, @"NewName", nil]; |
| 1231 |
|
| 1232 |
if ([[theName stringByTrimmingWhiteSpaces] length] == 0 || |
| 1233 |
[[theNewName stringByTrimmingWhiteSpaces] length] == 0) |
| 1234 |
{ |
| 1235 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 1236 |
PERFORM_SELECTOR_3(_delegate, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 1237 |
} |
| 1238 |
|
| 1239 |
[self sendCommand: IMAP_RENAME |
| 1240 |
info: info |
| 1241 |
arguments: @"RENAME \"%@\" \"%@\"", [theName modifiedUTF7String], [theNewName modifiedUTF7String]]; |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
// |
| 1246 |
// This method NOOPs the IMAP store. |
| 1247 |
// |
| 1248 |
- (void) noop |
| 1249 |
{ |
| 1250 |
[self sendCommand: IMAP_NOOP info: nil arguments: @"NOOP"]; |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
// |
| 1255 |
// |
| 1256 |
// |
| 1257 |
- (int) reconnect |
| 1258 |
{ |
| 1259 |
//NSLog(@"CWIMAPStore: -reconnect"); |
| 1260 |
|
| 1261 |
[_connection_state.previous_queue addObjectsFromArray: _queue]; |
| 1262 |
_connection_state.reconnecting = YES; |
| 1263 |
|
| 1264 |
// We flush our read/write buffers. |
| 1265 |
[_rbuf setLength: 0]; |
| 1266 |
[_wbuf setLength: 0]; |
| 1267 |
|
| 1268 |
// |
| 1269 |
// We first empty our queue and set again our _lastCommand ivar to |
| 1270 |
// the IMAP_AUTHORIZATION command |
| 1271 |
// |
| 1272 |
//NSLog(@"queue count = %d", [_queue count]); |
| 1273 |
//NSLog(@"%@", [_queue description]); |
| 1274 |
[_queue removeAllObjects]; |
| 1275 |
_lastCommand = IMAP_AUTHORIZATION; |
| 1276 |
_currentQueueObject = nil; |
| 1277 |
_counter = 0; |
| 1278 |
|
| 1279 |
[super close]; |
| 1280 |
[super connectInBackgroundAndNotify]; |
| 1281 |
|
| 1282 |
return 0; |
| 1283 |
} |
| 1284 |
|
| 1285 |
|
| 1286 |
// |
| 1287 |
// |
| 1288 |
// |
| 1289 |
- (void) startTLS |
| 1290 |
{ |
| 1291 |
[self sendCommand: IMAP_STARTTLS info: nil arguments: @"STARTTLS"]; |
| 1292 |
} |
| 1293 |
|
| 1294 |
@end |
| 1295 |
|
| 1296 |
|
| 1297 |
// |
| 1298 |
// Private methods |
| 1299 |
// |
| 1300 |
@implementation CWIMAPStore (Private) |
| 1301 |
|
| 1302 |
// |
| 1303 |
// This method is used to parse the name of a mailbox. |
| 1304 |
// |
| 1305 |
// If the string was encoded using mUTF-7, it'll also |
| 1306 |
// decode it. |
| 1307 |
// |
| 1308 |
- (NSString *) _folderNameFromString: (NSString *) theString |
| 1309 |
{ |
| 1310 |
NSString *aString, *decodedString; |
| 1311 |
NSRange aRange; |
| 1312 |
|
| 1313 |
aRange = [theString rangeOfString: @"\""]; |
| 1314 |
|
| 1315 |
if (aRange.length) |
| 1316 |
{ |
| 1317 |
NSUInteger mark; |
| 1318 |
|
| 1319 |
mark = aRange.location + 1; |
| 1320 |
|
| 1321 |
aRange = [theString rangeOfString: @"\"" |
| 1322 |
options: 0 |
| 1323 |
range: NSMakeRange(mark, [theString length] - mark)]; |
| 1324 |
|
| 1325 |
aString = [theString substringWithRange: NSMakeRange(mark, aRange.location - mark)]; |
| 1326 |
|
| 1327 |
// Check if we got "NIL" or a real separator. |
| 1328 |
if ([aString length] == 1) |
| 1329 |
{ |
| 1330 |
_folderSeparator = [aString characterAtIndex: 0]; |
| 1331 |
} |
| 1332 |
else |
| 1333 |
{ |
| 1334 |
_folderSeparator = 0; |
| 1335 |
} |
| 1336 |
|
| 1337 |
mark = aRange.location + 2; |
| 1338 |
aString = [theString substringFromIndex: mark]; |
| 1339 |
} |
| 1340 |
else |
| 1341 |
{ |
| 1342 |
aRange = [theString rangeOfString: @"NIL" options: NSCaseInsensitiveSearch]; |
| 1343 |
|
| 1344 |
if (aRange.length) |
| 1345 |
{ |
| 1346 |
aString = [theString substringFromIndex: aRange.location + aRange.length + 1]; |
| 1347 |
} |
| 1348 |
else |
| 1349 |
{ |
| 1350 |
return theString; |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
aString = [aString stringFromQuotedString]; |
| 1355 |
decodedString = [aString stringFromModifiedUTF7]; |
| 1356 |
|
| 1357 |
return (decodedString != nil ? decodedString : aString); |
| 1358 |
} |
| 1359 |
|
| 1360 |
|
| 1361 |
// |
| 1362 |
// This method parses the flags received in theString and builds |
| 1363 |
// a corresponding Flags object for them. |
| 1364 |
// |
| 1365 |
- (void) _parseFlags: (NSString *) theString |
| 1366 |
message: (CWIMAPMessage *) theMessage |
| 1367 |
record: (cache_record *) theRecord |
| 1368 |
{ |
| 1369 |
CWFlags *theFlags; |
| 1370 |
NSRange aRange; |
| 1371 |
|
| 1372 |
theFlags = [[CWFlags alloc] init]; |
| 1373 |
|
| 1374 |
// We check if the message has the Seen flag |
| 1375 |
aRange = [theString rangeOfString: @"\\Seen" |
| 1376 |
options: NSCaseInsensitiveSearch]; |
| 1377 |
|
| 1378 |
if (aRange.length > 0) |
| 1379 |
{ |
| 1380 |
[theFlags add: PantomimeSeen]; |
| 1381 |
} |
| 1382 |
|
| 1383 |
// We check if the message has the Recent flag |
| 1384 |
aRange = [theString rangeOfString: @"\\Recent" |
| 1385 |
options: NSCaseInsensitiveSearch]; |
| 1386 |
|
| 1387 |
if (aRange.length > 0) |
| 1388 |
{ |
| 1389 |
[theFlags add: PantomimeRecent]; |
| 1390 |
} |
| 1391 |
|
| 1392 |
// We check if the message has the Deleted flag |
| 1393 |
aRange = [theString rangeOfString: @"\\Deleted" |
| 1394 |
options: NSCaseInsensitiveSearch]; |
| 1395 |
|
| 1396 |
if (aRange.length > 0) |
| 1397 |
{ |
| 1398 |
[theFlags add: PantomimeDeleted]; |
| 1399 |
} |
| 1400 |
|
| 1401 |
// We check if the message has the Answered flag |
| 1402 |
aRange = [theString rangeOfString: @"\\Answered" |
| 1403 |
options: NSCaseInsensitiveSearch]; |
| 1404 |
|
| 1405 |
if (aRange.length > 0) |
| 1406 |
{ |
| 1407 |
[theFlags add: PantomimeAnswered]; |
| 1408 |
} |
| 1409 |
|
| 1410 |
// We check if the message has the Flagged flag |
| 1411 |
aRange = [theString rangeOfString: @"\\Flagged" |
| 1412 |
options: NSCaseInsensitiveSearch]; |
| 1413 |
|
| 1414 |
if (aRange.length > 0) |
| 1415 |
{ |
| 1416 |
[theFlags add: PantomimeFlagged]; |
| 1417 |
} |
| 1418 |
|
| 1419 |
// We check if the message has the Draft flag |
| 1420 |
aRange = [theString rangeOfString: @"\\Draft" |
| 1421 |
options: NSCaseInsensitiveSearch]; |
| 1422 |
|
| 1423 |
if (aRange.length > 0) |
| 1424 |
{ |
| 1425 |
[theFlags add: PantomimeDraft]; |
| 1426 |
} |
| 1427 |
|
| 1428 |
[[theMessage flags] replaceWithFlags: theFlags]; |
| 1429 |
theRecord->flags = theFlags->flags; |
| 1430 |
RELEASE(theFlags); |
| 1431 |
|
| 1432 |
// |
| 1433 |
// If our previous command is NOT the FETCH command, we must inform our |
| 1434 |
// delegate that messages flags have changed. The delegate SHOULD refresh |
| 1435 |
// its view and does NOT have to issue any command to update the state |
| 1436 |
// of the messages (since it has been done). |
| 1437 |
// |
| 1438 |
if (_lastCommand != IMAP_UID_FETCH_BODY_TEXT && _lastCommand != IMAP_UID_FETCH_HEADER_FIELDS && |
| 1439 |
_lastCommand != IMAP_UID_FETCH_HEADER_FIELDS_NOT && _lastCommand != IMAP_UID_FETCH_RFC822) |
| 1440 |
{ |
| 1441 |
POST_NOTIFICATION(PantomimeMessageChanged, self, [NSDictionary dictionaryWithObject: theMessage forKey: @"Message"]); |
| 1442 |
PERFORM_SELECTOR_1(_delegate, @selector(messageChanged:), PantomimeMessageChanged); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
// |
| 1448 |
// |
| 1449 |
// |
| 1450 |
- (void) _renameFolder |
| 1451 |
{ |
| 1452 |
CWFolderInformation *aFolderInformation; |
| 1453 |
NSString *aName, *aNewName; |
| 1454 |
CWIMAPFolder *aFolder; |
| 1455 |
|
| 1456 |
aName = [_currentQueueObject->info objectForKey: @"Name"]; |
| 1457 |
aNewName = [_currentQueueObject->info objectForKey: @"NewName"]; |
| 1458 |
|
| 1459 |
// If the folder was open, we change its name and recache its entry. |
| 1460 |
aFolder = [_openFolders objectForKey: aName]; |
| 1461 |
|
| 1462 |
if (aFolder) |
| 1463 |
{ |
| 1464 |
RETAIN(aFolder); |
| 1465 |
[aFolder setName: aNewName]; |
| 1466 |
[_openFolders removeObjectForKey: aName]; |
| 1467 |
[_openFolders setObject: aFolder forKey: aNewName]; |
| 1468 |
RELEASE(aFolder); |
| 1469 |
} |
| 1470 |
|
| 1471 |
// We then do the same thing for our list of folders / suscribed folders |
| 1472 |
aFolderInformation = RETAIN([_folders objectForKey: aName]); |
| 1473 |
[_folders removeObjectForKey: aName]; |
| 1474 |
|
| 1475 |
if (aFolderInformation) |
| 1476 |
{ |
| 1477 |
[_folders setObject: aFolderInformation forKey: aNewName]; |
| 1478 |
RELEASE(aFolderInformation); |
| 1479 |
} |
| 1480 |
|
| 1481 |
if ([_subscribedFolders containsObject: aName]) |
| 1482 |
{ |
| 1483 |
[_subscribedFolders removeObject: aName]; |
| 1484 |
[_subscribedFolders addObject: aNewName]; |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
|
| 1489 |
// |
| 1490 |
// This method parses a SEARCH response in order to decode |
| 1491 |
// all UIDs in the result. |
| 1492 |
// |
| 1493 |
// Examples of theData: |
| 1494 |
// |
| 1495 |
// "* SEARCH 1 4 59 81" |
| 1496 |
// "* SEARCH" |
| 1497 |
// |
| 1498 |
- (NSArray *) _uniqueIdentifiersFromData: (NSData *) theData |
| 1499 |
{ |
| 1500 |
NSMutableArray *aMutableArray; |
| 1501 |
NSScanner *aScanner; |
| 1502 |
unsigned int value; |
| 1503 |
|
| 1504 |
aMutableArray = [NSMutableArray array]; |
| 1505 |
|
| 1506 |
theData = [theData subdataFromIndex: 8]; |
| 1507 |
|
| 1508 |
// If we have no results, let's return right away. |
| 1509 |
if (![theData length]) |
| 1510 |
{ |
| 1511 |
return aMutableArray; |
| 1512 |
} |
| 1513 |
|
| 1514 |
// We scan all our UIDs. |
| 1515 |
aScanner = [[NSScanner alloc] initWithString: [theData asciiString]]; |
| 1516 |
|
| 1517 |
while (![aScanner isAtEnd]) |
| 1518 |
{ |
| 1519 |
[aScanner scanUnsignedInt: &value]; |
| 1520 |
[aMutableArray addObject: [NSNumber numberWithUnsignedInt: value]]; |
| 1521 |
} |
| 1522 |
|
| 1523 |
RELEASE(aScanner); |
| 1524 |
|
| 1525 |
return aMutableArray; |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
// |
| 1530 |
// |
| 1531 |
// |
| 1532 |
- (void) _parseAUTHENTICATE_CRAM_MD5 |
| 1533 |
{ |
| 1534 |
NSData *aData; |
| 1535 |
|
| 1536 |
aData = [_responsesFromServer lastObject]; |
| 1537 |
|
| 1538 |
// |
| 1539 |
// We first verify if we got our challenge response from the IMAP server. |
| 1540 |
// If so, we use it and send back a response to proceed with the authentication. |
| 1541 |
// |
| 1542 |
if ([aData hasCPrefix: "+"]) |
| 1543 |
{ |
| 1544 |
NSString *aString; |
| 1545 |
CWMD5 *aMD5; |
| 1546 |
|
| 1547 |
// We trim the "+ " and we keep the challenge phrase |
| 1548 |
aData = [aData subdataFromIndex: 2]; |
| 1549 |
|
| 1550 |
//NSLog(@"Challenge phrase = |%@|", [aData asciiString]); |
| 1551 |
aMD5 = [[CWMD5 alloc] initWithData: [aData decodeBase64]]; |
| 1552 |
[aMD5 computeDigest]; |
| 1553 |
|
| 1554 |
aString = [NSString stringWithFormat: @"%@ %@", _username, [aMD5 hmacAsStringUsingPassword: _password]]; |
| 1555 |
aString = [[NSString alloc] initWithData: [[aString dataUsingEncoding: NSASCIIStringEncoding] encodeBase64WithLineLength: 0] |
| 1556 |
encoding: NSASCIIStringEncoding]; |
| 1557 |
|
| 1558 |
[self writeData: [aString dataUsingEncoding: defaultCStringEncoding]]; |
| 1559 |
[self writeData: CRLF]; |
| 1560 |
|
| 1561 |
RELEASE(aMD5); |
| 1562 |
RELEASE(aString); |
| 1563 |
} |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
// |
| 1568 |
// LOGIN is a very lame authentication method but we support it anyway. We basically |
| 1569 |
// wait for a challenge, send the username (in base64), wait for an other challenge |
| 1570 |
// and finally send the password (in base64). The challenges aren't even used. |
| 1571 |
// |
| 1572 |
- (void) _parseAUTHENTICATE_LOGIN |
| 1573 |
{ |
| 1574 |
NSData *aData; |
| 1575 |
|
| 1576 |
aData = [_responsesFromServer lastObject]; |
| 1577 |
|
| 1578 |
// |
| 1579 |
// We first verify if we got our challenge response from the IMAP server. |
| 1580 |
// If so, we use it and send back a response to proceed with the authentication. |
| 1581 |
// Based on what we sent before, we can either send the username or the password. |
| 1582 |
// |
| 1583 |
if ([aData hasCPrefix: "+"]) |
| 1584 |
{ |
| 1585 |
NSData *aResponse; |
| 1586 |
|
| 1587 |
// Have we read the initial challenge? If not, we must send the username! |
| 1588 |
if (_currentQueueObject && ![_currentQueueObject->info |
| 1589 |
objectForKey: @"Challenge"]) |
| 1590 |
{ |
| 1591 |
aResponse = [[_username dataUsingEncoding: NSASCIIStringEncoding] |
| 1592 |
encodeBase64WithLineLength: 0]; |
| 1593 |
[_currentQueueObject->info setObject: aData forKey: @"Challenge"]; |
| 1594 |
} |
| 1595 |
else |
| 1596 |
{ |
| 1597 |
aResponse = [[_password dataUsingEncoding: NSASCIIStringEncoding] |
| 1598 |
encodeBase64WithLineLength: 0]; |
| 1599 |
} |
| 1600 |
|
| 1601 |
[self writeData: aResponse]; |
| 1602 |
[self writeData: CRLF]; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
// |
| 1608 |
// |
| 1609 |
// |
| 1610 |
- (void) _parseBAD |
| 1611 |
{ |
| 1612 |
NSData *aData; |
| 1613 |
|
| 1614 |
aData = [_responsesFromServer lastObject]; |
| 1615 |
|
| 1616 |
switch (_lastCommand) |
| 1617 |
{ |
| 1618 |
case IMAP_LOGIN: |
| 1619 |
// This can happen if we got an empty username or password. |
| 1620 |
AUTHENTICATION_FAILED(_delegate, _mechanism); |
| 1621 |
break; |
| 1622 |
|
| 1623 |
default: |
| 1624 |
// We got a BAD response that we could not handle. Raise an exception for now |
| 1625 |
// and remove the command that caused this from the queue. |
| 1626 |
[_queue removeLastObject]; |
| 1627 |
[_responsesFromServer removeAllObjects]; |
| 1628 |
[NSException raise: PantomimeProtocolException |
| 1629 |
format: @"Unable to handle IMAP response (%@).", [aData asciiString]]; |
| 1630 |
} |
| 1631 |
|
| 1632 |
if (![aData hasCPrefix: "*"]) |
| 1633 |
{ |
| 1634 |
[_queue removeLastObject]; |
| 1635 |
[self sendCommand: IMAP_EMPTY_QUEUE info: nil arguments: @""]; |
| 1636 |
} |
| 1637 |
|
| 1638 |
[_responsesFromServer removeAllObjects]; |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
// |
| 1643 |
// |
| 1644 |
// |
| 1645 |
- (void) _parseBYE |
| 1646 |
{ |
| 1647 |
// |
| 1648 |
// We check if we sent the IMAP_LOGOUT command. |
| 1649 |
// |
| 1650 |
// If we got an untagged BYE response, it means |
| 1651 |
// that the server disconnected us. We will |
| 1652 |
// handle that in CWService: -updateRead. |
| 1653 |
// |
| 1654 |
if (_lastCommand == IMAP_LOGOUT) |
| 1655 |
{ |
| 1656 |
return; |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
|
| 1661 |
// |
| 1662 |
// This method parses an * CAPABILITY IMAP4 IMAP4rev1 ACL AUTH=LOGIN NAMESPACE .. |
| 1663 |
// untagged response (6.1.1) |
| 1664 |
// |
| 1665 |
// FIXME: check for OK/BAD |
| 1666 |
// |
| 1667 |
- (void) _parseCAPABILITY |
| 1668 |
{ |
| 1669 |
NSString *aString; |
| 1670 |
NSData *aData; |
| 1671 |
|
| 1672 |
aData = [_responsesFromServer objectAtIndex: 0]; |
| 1673 |
aString = [[NSString alloc] initWithData: aData encoding: defaultCStringEncoding]; |
| 1674 |
|
| 1675 |
[_capabilities addObjectsFromArray: [[aString substringFromIndex: 13] componentsSeparatedByString: @" "]]; |
| 1676 |
RELEASE(aString); |
| 1677 |
|
| 1678 |
if (_connection_state.reconnecting) |
| 1679 |
{ |
| 1680 |
[self authenticate: _username password: _password mechanism: _mechanism]; |
| 1681 |
} |
| 1682 |
else |
| 1683 |
{ |
| 1684 |
POST_NOTIFICATION(PantomimeServiceInitialized, self, nil); |
| 1685 |
PERFORM_SELECTOR_1(_delegate, @selector(serviceInitialized:), PantomimeServiceInitialized); |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
// |
| 1691 |
// This method parses an * 23 EXISTS untagged response. (7.3.1) |
| 1692 |
// |
| 1693 |
// If we were NOT issueing a SELECT command, it fetches the |
| 1694 |
// new messages (if any) and informs the folder's delegate that |
| 1695 |
// new messages have arrived. |
| 1696 |
// |
| 1697 |
- (void) _parseEXISTS |
| 1698 |
{ |
| 1699 |
NSData *aData; |
| 1700 |
unsigned n; |
| 1701 |
|
| 1702 |
aData = [_responsesFromServer lastObject]; |
| 1703 |
|
| 1704 |
sscanf([aData cString], "* %u EXISTS", &n); |
| 1705 |
|
| 1706 |
//NSLog(@"_parseExists: %d", n); |
| 1707 |
|
| 1708 |
if (_currentQueueObject && _currentQueueObject->command != IMAP_SELECT && |
| 1709 |
_selectedFolder && |
| 1710 |
n > [[_selectedFolder messages] count]) |
| 1711 |
{ |
| 1712 |
NSUInteger uid; |
| 1713 |
|
| 1714 |
uid = 0; |
| 1715 |
|
| 1716 |
// We prefetch the new messages from the last UID+1 |
| 1717 |
if ([[_selectedFolder messages] lastObject]) |
| 1718 |
{ |
| 1719 |
uid = [[[_selectedFolder messages] lastObject] UID]; |
| 1720 |
} |
| 1721 |
|
| 1722 |
[self sendCommand: IMAP_UID_FETCH_HEADER_FIELDS info: nil arguments: @"UID FETCH %u:* (FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (From To Cc Subject Date Message-ID References In-Reply-To)])", (uid+1)]; |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
|
| 1727 |
// |
| 1728 |
// Example: * 44 EXPUNGE |
| 1729 |
// |
| 1730 |
- (void) _parseEXPUNGE |
| 1731 |
{ |
| 1732 |
CWIMAPMessage *aMessage; |
| 1733 |
NSData *aData; |
| 1734 |
NSUInteger i; |
| 1735 |
unsigned msn; |
| 1736 |
|
| 1737 |
// It looks like some servers send untagged expunge reponses |
| 1738 |
// _after_ the selected folder has been closed. |
| 1739 |
if (!_selectedFolder) |
| 1740 |
{ |
| 1741 |
return; |
| 1742 |
} |
| 1743 |
|
| 1744 |
aData = [_responsesFromServer lastObject]; |
| 1745 |
|
| 1746 |
sscanf([aData cString], "* %u EXPUNGE", &msn); |
| 1747 |
|
| 1748 |
// |
| 1749 |
// Messages CAN be expunged before we really had time to FETCH them. |
| 1750 |
// We simply proceed by skipping over MSN that are bigger than we |
| 1751 |
// we have so far. It should be safe since the view hasn't even |
| 1752 |
// had the chance to display them. |
| 1753 |
// |
| 1754 |
if (msn > [[_selectedFolder messages] count]) return; |
| 1755 |
|
| 1756 |
aMessage = [[_selectedFolder messages] objectAtIndex: (msn-1)]; |
| 1757 |
RETAIN(aMessage); |
| 1758 |
|
| 1759 |
// We do NOT use [_selectedFolder removeMessage: aMessage] since it'll |
| 1760 |
// thread the messages everytime we invoke it. We rather thread messages |
| 1761 |
// if: |
| 1762 |
// * We got an untagged EXPUNGE response but the last command was NOT |
| 1763 |
// an EXPUNGE one (see below, near the end of the method) |
| 1764 |
// * We sent an EXPUNGE command - we'll do the threading of the |
| 1765 |
// messages in _parseOK: |
| 1766 |
// |
| 1767 |
[[_selectedFolder messages] removeObject: aMessage]; |
| 1768 |
[_selectedFolder updateCache]; |
| 1769 |
|
| 1770 |
// We remove its entry in our cache |
| 1771 |
if ([_selectedFolder cacheManager]) |
| 1772 |
{ |
| 1773 |
[(CWIMAPCacheManager *)[_selectedFolder cacheManager] removeMessageWithUID: [aMessage UID]]; |
| 1774 |
} |
| 1775 |
|
| 1776 |
// We update all MSNs starting from the message that has been expunged. |
| 1777 |
for (i = (msn-1); i < [[_selectedFolder messages] count]; i++) |
| 1778 |
{ |
| 1779 |
[[[_selectedFolder messages] objectAtIndex: i] setMessageNumber: (i+1)]; |
| 1780 |
} |
| 1781 |
|
| 1782 |
// |
| 1783 |
// If our previous command is NOT the EXPUNGE command, we must inform our |
| 1784 |
// delegate that messages have been expunged. The delegate SHOULD refresh |
| 1785 |
// its view and does NOT have to issue any command to update the state |
| 1786 |
// of the messages (since it has been done). |
| 1787 |
// |
| 1788 |
if (_lastCommand != IMAP_EXPUNGE) |
| 1789 |
{ |
| 1790 |
if ([_selectedFolder allContainers]) |
| 1791 |
{ |
| 1792 |
[_selectedFolder thread]; |
| 1793 |
} |
| 1794 |
|
| 1795 |
if ([_selectedFolder cacheManager]) |
| 1796 |
{ |
| 1797 |
[[_selectedFolder cacheManager] expunge]; |
| 1798 |
} |
| 1799 |
|
| 1800 |
POST_NOTIFICATION(PantomimeMessageExpunged, self, [NSDictionary dictionaryWithObject: aMessage forKey: @"Message"]); |
| 1801 |
PERFORM_SELECTOR_1(_delegate, @selector(messageExpunged:), PantomimeMessageExpunged); |
| 1802 |
} |
| 1803 |
|
| 1804 |
RELEASE(aMessage); |
| 1805 |
|
| 1806 |
//NSLog(@"Expunged %d", msn); |
| 1807 |
} |
| 1808 |
|
| 1809 |
|
| 1810 |
// |
| 1811 |
// |
| 1812 |
// Examples of FETCH responses: |
| 1813 |
// |
| 1814 |
// * 50 FETCH (UID 50 RFC822 {6718} |
| 1815 |
// Return-Path: <... |
| 1816 |
// ) |
| 1817 |
// |
| 1818 |
// |
| 1819 |
// * 418 FETCH (FLAGS (\Seen) UID 418 RFC822.SIZE 3565452 BODY[HEADER.FIELDS (From To Cc Subject Date Message-ID |
| 1820 |
// References In-Reply-To MIME-Version)] {666} |
| 1821 |
// Subject: abc |
| 1822 |
// ... |
| 1823 |
// ) |
| 1824 |
// |
| 1825 |
// |
| 1826 |
// * 50 FETCH (UID 50 BODY[HEADER.FIELDS.NOT (From To Cc Subject Date Message-ID References In-Reply-To MIME-Version)] {1412} |
| 1827 |
// Return-Path: <... |
| 1828 |
// ) |
| 1829 |
// |
| 1830 |
// * 50 FETCH (BODY[TEXT] {5009} |
| 1831 |
// Hi, ... |
| 1832 |
// ) |
| 1833 |
// |
| 1834 |
// |
| 1835 |
// "Twisted" response from Microsoft Exchange 2000: |
| 1836 |
// |
| 1837 |
// * 549 FETCH (FLAGS (\Recent) RFC822.SIZE 970 BODY[HEADER.FIELDS (From To Cc Subject Date Message-ID References In-Reply-To MIME-Version)] {196} |
| 1838 |
// From: <[email protected]> |
| 1839 |
// To: [email protected] |
| 1840 |
// Subject: Test mail |
| 1841 |
// Date: Tue, 16 Dec 2003 15:52:23 GMT |
| 1842 |
// Message-Id: <[email protected]> |
| 1843 |
// |
| 1844 |
// UID 29905) |
| 1845 |
// |
| 1846 |
// |
| 1847 |
// Yet an other "twisted" response, likely coming from UW IMAP Server (2001.315rh) |
| 1848 |
// |
| 1849 |
// * 741 FETCH (UID 23628 BODY[TEXT] {818} |
| 1850 |
// f00bar baz |
| 1851 |
// ... |
| 1852 |
// ) |
| 1853 |
// * 741 FETCH (FLAGS (\Seen) UID 23628) |
| 1854 |
// 000b OK UID FETCH completed |
| 1855 |
// |
| 1856 |
// |
| 1857 |
// Other examples: |
| 1858 |
// |
| 1859 |
// * 1 FETCH (FLAGS (\Seen) UID 97 RFC822.SIZE 19123 BODY[HEADER.FIELDS (From To Cc Subject Date |
| 1860 |
// Message-ID References In-Reply-To MIME-Version)] {216} |
| 1861 |
// |
| 1862 |
// This method can be called more than on times for a message. For example, Exchange sends |
| 1863 |
// answers like this one: |
| 1864 |
// |
| 1865 |
// * 9 FETCH (BODY[HEADER.FIELDS.NOT (From To Cc Subject Date Message-ID References In-Reply-To MIME-Version)] {408} |
| 1866 |
// Received: by nt1.inverse.qc.ca |
| 1867 |
// .id <[email protected]>; Tue, 15 Jul 2003 09:24:36 -0500 |
| 1868 |
// content-class: urn:content-classes:message |
| 1869 |
// Content-Type: multipart/mixed; |
| 1870 |
// .boundary="----_=_NextPart_001_01C34ADC.D13E2A20" |
| 1871 |
// X-MS-Has-Attach: yes |
| 1872 |
// X-MS-TNEF-Correlator: |
| 1873 |
// Thread-Topic: test5 |
| 1874 |
// X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 |
| 1875 |
// Thread-Index: AcNK3NDIIAS/1aRKSYC4x2N4Zj3GGg== |
| 1876 |
// |
| 1877 |
// UID 9) |
| 1878 |
// |
| 1879 |
// And we MUST parse the UID correctly. |
| 1880 |
// |
| 1881 |
- (void) _parseFETCH: (unsigned) theMSN |
| 1882 |
{ |
| 1883 |
NSMutableString *aMutableString; |
| 1884 |
NSCharacterSet *aCharacterSet; |
| 1885 |
CWIMAPMessage *aMessage; |
| 1886 |
NSScanner *aScanner; |
| 1887 |
|
| 1888 |
NSMutableArray *aMutableArray; |
| 1889 |
NSString *aWord, *aString; |
| 1890 |
NSRange aRange; |
| 1891 |
|
| 1892 |
BOOL done, seen_fetch, must_flush_record; |
| 1893 |
NSUInteger i, j, count, len; |
| 1894 |
cache_record r; |
| 1895 |
|
| 1896 |
// |
| 1897 |
// The folder might have been closed so we must not try to |
| 1898 |
// update it for no good reason. |
| 1899 |
// |
| 1900 |
if (!_selectedFolder) return; |
| 1901 |
|
| 1902 |
count = [_responsesFromServer count]-1; |
| 1903 |
|
| 1904 |
//NSLog(@"RESPONSES FROM SERVER: %d", count); |
| 1905 |
|
| 1906 |
aMutableString = [[NSMutableString alloc] init]; |
| 1907 |
aMutableArray = [[NSMutableArray alloc] init]; |
| 1908 |
|
| 1909 |
// |
| 1910 |
// Note: |
| 1911 |
// |
| 1912 |
// We must be careful here to NOT consider all responses from the server. For example, |
| 1913 |
// UW IMAP might send us: |
| 1914 |
// 1 UID SEARCH ANSWERED |
| 1915 |
// * SEARCH |
| 1916 |
// * 1 FETCH (FLAGS (\Recent \Seen) UID 1) |
| 1917 |
// 1 OK UID SEARCH completed |
| 1918 |
// |
| 1919 |
// In such response, we must NOT consider the "* SEARCH" response. |
| 1920 |
// |
| 1921 |
must_flush_record = seen_fetch = NO; |
| 1922 |
|
| 1923 |
for (i = 0; i <= count; i++) |
| 1924 |
{ |
| 1925 |
aString = [[_responsesFromServer objectAtIndex: i] asciiString]; |
| 1926 |
//NSLog(@"%i: %@", i, aString); |
| 1927 |
if (!seen_fetch && [aString hasCaseInsensitivePrefix: [NSString stringWithFormat: @"* %u FETCH", theMSN]]) |
| 1928 |
{ |
| 1929 |
seen_fetch = YES; |
| 1930 |
} |
| 1931 |
|
| 1932 |
if (seen_fetch) |
| 1933 |
{ |
| 1934 |
[aMutableArray addObject: [_responsesFromServer objectAtIndex: i]]; |
| 1935 |
[aMutableString appendString: aString]; |
| 1936 |
if (i < count-1) |
| 1937 |
{ |
| 1938 |
[aMutableString appendString: @" "]; |
| 1939 |
} |
| 1940 |
} |
| 1941 |
} |
| 1942 |
|
| 1943 |
//NSLog(@"GOT TO PARSE: |%@|", aMutableString); |
| 1944 |
|
| 1945 |
aCharacterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; |
| 1946 |
len = [aMutableString length]; |
| 1947 |
aMessage = nil; |
| 1948 |
i = 0; |
| 1949 |
|
| 1950 |
aScanner = [[NSScanner alloc] initWithString: aMutableString]; |
| 1951 |
[aScanner setScanLocation: i]; |
| 1952 |
|
| 1953 |
done = ![aScanner scanUpToCharactersFromSet: aCharacterSet intoString: NULL]; |
| 1954 |
|
| 1955 |
// |
| 1956 |
// We tokenize our string into words |
| 1957 |
// |
| 1958 |
while (!done) |
| 1959 |
{ |
| 1960 |
j = [aScanner scanLocation]; |
| 1961 |
aWord = [[aMutableString substringWithRange: NSMakeRange(i,j-i)] stringByTrimmingWhiteSpaces]; |
| 1962 |
|
| 1963 |
//NSLog(@"WORD |%@|", aWord); |
| 1964 |
|
| 1965 |
if ([aWord characterAtIndex: 0] == '(') |
| 1966 |
{ |
| 1967 |
aWord = [aWord substringFromIndex: 1]; |
| 1968 |
} |
| 1969 |
|
| 1970 |
// |
| 1971 |
// We read the MSN |
| 1972 |
// |
| 1973 |
if ([aWord characterAtIndex: 0] == '*') |
| 1974 |
{ |
| 1975 |
unsigned msn; |
| 1976 |
|
| 1977 |
[aScanner scanUnsignedInt: &msn]; |
| 1978 |
//NSLog(@"msn = %d", msn); |
| 1979 |
|
| 1980 |
// |
| 1981 |
// If the MSN is > then the folder's count, that means it's |
| 1982 |
// a new message. |
| 1983 |
// |
| 1984 |
// We can safely assume this since what we have in _selectedFolder->allMessages |
| 1985 |
// is really the messages in our IMAP folder. That is true since we |
| 1986 |
// synchronized our cache when opening the folder, in IMAPFolder: -prefetch. |
| 1987 |
// |
| 1988 |
if (msn > [[_selectedFolder messages] count]) |
| 1989 |
{ |
| 1990 |
//NSLog(@"============ NEW MESSAGE ======================"); |
| 1991 |
aMessage = [[CWIMAPMessage alloc] init]; |
| 1992 |
|
| 1993 |
// We set some initial properties to our message; |
| 1994 |
[aMessage setInitialized: NO]; |
| 1995 |
[aMessage setFolder: _selectedFolder]; |
| 1996 |
[aMessage setMessageNumber: msn]; |
| 1997 |
[_selectedFolder appendMessage: aMessage]; |
| 1998 |
|
| 1999 |
// We add the new message to our cache. |
| 2000 |
if ([_selectedFolder cacheManager]) |
| 2001 |
{ |
| 2002 |
if (must_flush_record) |
| 2003 |
{ |
| 2004 |
[(CWIMAPCacheManager *)[_selectedFolder cacheManager] writeRecord: &r message: aMessage]; |
| 2005 |
} |
| 2006 |
|
| 2007 |
CLEAR_CACHE_RECORD(r); |
| 2008 |
must_flush_record = YES; |
| 2009 |
|
| 2010 |
//[[_selectedFolder cacheManager] addObject: aMessage]; |
| 2011 |
} |
| 2012 |
|
| 2013 |
RELEASE(aMessage); |
| 2014 |
} |
| 2015 |
else |
| 2016 |
{ |
| 2017 |
aMessage = [[_selectedFolder messages] objectAtIndex: (msn-1)]; |
| 2018 |
[aMessage setMessageNumber: msn]; |
| 2019 |
[aMessage setFolder: _selectedFolder]; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
if (!aMessage) |
| 2023 |
{ |
| 2024 |
RELEASE(aMutableString); |
| 2025 |
RELEASE(aMutableArray); |
| 2026 |
RELEASE(aScanner); |
| 2027 |
return; |
| 2028 |
} |
| 2029 |
// |
| 2030 |
// We read our UID |
| 2031 |
// |
| 2032 |
if ([aWord caseInsensitiveCompare: @"UID"] == NSOrderedSame) |
| 2033 |
{ |
| 2034 |
unsigned int uid; |
| 2035 |
|
| 2036 |
[aScanner scanUnsignedInt: &uid]; |
| 2037 |
//NSLog(@"uid %d j = %d, scanLoc = %d", uid, j, [aScanner scanLocation]); |
| 2038 |
|
| 2039 |
if ([aMessage UID] == 0) |
| 2040 |
{ |
| 2041 |
[aMessage setUID: uid]; |
| 2042 |
r.imap_uid = uid; |
| 2043 |
} |
| 2044 |
|
| 2045 |
j = [aScanner scanLocation]; |
| 2046 |
} |
| 2047 |
// |
| 2048 |
// We read our flags. We usually get something like FLAGS (\Seen) |
| 2049 |
// |
| 2050 |
else if ([aWord caseInsensitiveCompare: @"FLAGS"] == NSOrderedSame) |
| 2051 |
{ |
| 2052 |
// We get the substring inside our ( ) |
| 2053 |
aRange = [aMutableString rangeOfString: @")" options: 0 range: NSMakeRange(j,len-j)]; |
| 2054 |
//NSLog(@"Flags = |%@|", [aMutableString substringWithRange: NSMakeRange(j+2, aRange.location-j-2)]); |
| 2055 |
[self _parseFlags: [aMutableString substringWithRange: NSMakeRange(j+2, aRange.location-j-2)] |
| 2056 |
message: aMessage |
| 2057 |
record: &r]; |
| 2058 |
|
| 2059 |
j = aRange.location + 1; |
| 2060 |
[aScanner setScanLocation: j]; |
| 2061 |
} |
| 2062 |
// |
| 2063 |
// We read the RFC822 message size |
| 2064 |
// |
| 2065 |
else if ([aWord caseInsensitiveCompare: @"RFC822.SIZE"] == NSOrderedSame) |
| 2066 |
{ |
| 2067 |
unsigned size; |
| 2068 |
|
| 2069 |
[aScanner scanUnsignedInt: &size]; |
| 2070 |
//NSLog(@"size = %u", size); |
| 2071 |
[aMessage setSize: size]; |
| 2072 |
r.size = size; |
| 2073 |
|
| 2074 |
j = [aScanner scanLocation]; |
| 2075 |
} |
| 2076 |
// |
| 2077 |
// Note: |
| 2078 |
// |
| 2079 |
// Novell's IMAP server doesn't distinguish a NOT BODY.PEEK from a standard one (ie., no NOT). So we can have: |
| 2080 |
// |
| 2081 |
// 000b UID FETCH 3071053:3071053 BODY.PEEK[HEADER.FIELDS.NOT (From To Cc Subject Date Message-ID References In-Reply-To MIME-Version)] |
| 2082 |
// * 1 FETCH (UID 3071053 BODY[HEADER.FIELDS ("From" "To" "Cc" "Subject" "Date" "Message-ID" "References" "In-Reply-To" "MIME-Version")] {1030} |
| 2083 |
// |
| 2084 |
else if ([aWord caseInsensitiveCompare: @"BODY[HEADER.FIELDS.NOT"] == NSOrderedSame || |
| 2085 |
_lastCommand == IMAP_UID_FETCH_HEADER_FIELDS_NOT) |
| 2086 |
{ |
| 2087 |
[[_currentQueueObject->info objectForKey: @"NSData"] replaceCRLFWithLF]; |
| 2088 |
[aMessage addHeadersFromData: [_currentQueueObject->info objectForKey: @"NSData"] record: NULL]; |
| 2089 |
break; |
| 2090 |
} |
| 2091 |
// |
| 2092 |
// We must not break immediately after parsing this information. It's very important |
| 2093 |
// since servers like Exchange might send us responses like: |
| 2094 |
// |
| 2095 |
// * 1 FETCH (FLAGS (\Seen) RFC822.SIZE 4491 BODY[HEADER.FIELDS (From To Cc Subject Date Message-ID References In-Reply-To Content-Type)] {337} UID 614348) |
| 2096 |
// |
| 2097 |
// If we break right away, we'll skip the size and more importantly, the UID. |
| 2098 |
// |
| 2099 |
else if ([aWord caseInsensitiveCompare: @"BODY[HEADER.FIELDS"] == NSOrderedSame) |
| 2100 |
{ |
| 2101 |
[[_currentQueueObject->info objectForKey: @"NSData"] replaceCRLFWithLF]; |
| 2102 |
[aMessage setHeadersFromData: [_currentQueueObject->info objectForKey: @"NSData"] record: &r]; |
| 2103 |
} |
| 2104 |
// |
| 2105 |
// |
| 2106 |
// |
| 2107 |
else if ([aWord caseInsensitiveCompare: @"BODY[TEXT]"] == NSOrderedSame) |
| 2108 |
{ |
| 2109 |
[[_currentQueueObject->info objectForKey: @"NSData"] replaceCRLFWithLF]; |
| 2110 |
if (![aMessage content]) |
| 2111 |
{ |
| 2112 |
NSData *aData; |
| 2113 |
|
| 2114 |
// |
| 2115 |
// We do an initial check for the message body. If we haven't read a literal, |
| 2116 |
// [_currentQueueObject->info objectForKey: @"NSData"] returns nil. This can |
| 2117 |
// happen with messages having a totally emtpy body. For those messages, |
| 2118 |
// we simply set a default content, being an empty NSData instance. |
| 2119 |
// |
| 2120 |
aData = [_currentQueueObject->info objectForKey: @"NSData"]; |
| 2121 |
|
| 2122 |
if (!aData) aData = [NSData data]; |
| 2123 |
|
| 2124 |
[CWMIMEUtility setContentFromRawSource: aData inPart: aMessage]; |
| 2125 |
[aMessage setInitialized: YES]; |
| 2126 |
|
| 2127 |
[_currentQueueObject->info setObject: aMessage forKey: @"Message"]; |
| 2128 |
|
| 2129 |
POST_NOTIFICATION(PantomimeMessagePrefetchCompleted, self, [NSDictionary dictionaryWithObject: aMessage forKey: @"Message"]); |
| 2130 |
PERFORM_SELECTOR_2(_delegate, @selector(messagePrefetchCompleted:), PantomimeMessagePrefetchCompleted, aMessage, @"Message"); |
| 2131 |
} |
| 2132 |
break; |
| 2133 |
} |
| 2134 |
// |
| 2135 |
// |
| 2136 |
// |
| 2137 |
else if ([aWord caseInsensitiveCompare: @"RFC822"] == NSOrderedSame) |
| 2138 |
{ |
| 2139 |
[[_currentQueueObject->info objectForKey: @"NSData"] replaceCRLFWithLF]; |
| 2140 |
[aMessage setRawSource: [_currentQueueObject->info objectForKey: @"NSData"]]; |
| 2141 |
POST_NOTIFICATION(PantomimeMessageFetchCompleted, self, [NSDictionary dictionaryWithObject: aMessage forKey: @"Message"]); |
| 2142 |
PERFORM_SELECTOR_2(_delegate, @selector(messageFetchCompleted:), PantomimeMessageFetchCompleted, aMessage, @"Message"); |
| 2143 |
break; |
| 2144 |
} |
| 2145 |
|
| 2146 |
i = j; |
| 2147 |
done = ![aScanner scanUpToCharactersFromSet: aCharacterSet intoString: NULL]; |
| 2148 |
|
| 2149 |
if (done && must_flush_record) |
| 2150 |
{ |
| 2151 |
[(CWIMAPCacheManager *)[_selectedFolder cacheManager] writeRecord: &r message: aMessage]; |
| 2152 |
} |
| 2153 |
} |
| 2154 |
|
| 2155 |
|
| 2156 |
RELEASE(aScanner); |
| 2157 |
RELEASE(aMutableString); |
| 2158 |
|
| 2159 |
// |
| 2160 |
// It is important that we remove the responses we have processed. This is particularly |
| 2161 |
// useful if we are caching an IMAP mailbox. We could receive thousands of untagged |
| 2162 |
// FETCH responses and we don't want to go over them again and again everytime |
| 2163 |
// this method is invoked. |
| 2164 |
// |
| 2165 |
[_responsesFromServer removeObjectsInArray: aMutableArray]; |
| 2166 |
RELEASE(aMutableArray); |
| 2167 |
} |
| 2168 |
|
| 2169 |
|
| 2170 |
// |
| 2171 |
// This command parses the result of a LIST command. See 7.2.2 for the complete |
| 2172 |
// description of the LIST response. |
| 2173 |
// |
| 2174 |
// Rationale: |
| 2175 |
// |
| 2176 |
// In IMAP, all mailboxes can hold messages and folders. Thus, the HOLDS_MESSAGES |
| 2177 |
// flag is ALWAYS set for a mailbox that has been parsed. |
| 2178 |
// |
| 2179 |
// We also support RFC3348 \HasChildren and \HasNoChildren flags. In fact, we |
| 2180 |
// directly map \HasChildren to HOLDS_FOLDERS. |
| 2181 |
// |
| 2182 |
// We support the following standard flags (from RFC3501): |
| 2183 |
// |
| 2184 |
// \Noinferiors |
| 2185 |
// It is not possible for any child levels of hierarchy to exist |
| 2186 |
// under this name; no child levels exist now and none can be |
| 2187 |
// created in the future. |
| 2188 |
// |
| 2189 |
// \Noselect |
| 2190 |
// It is not possible to use this name as a selectable mailbox. |
| 2191 |
// |
| 2192 |
// \Marked |
| 2193 |
// The mailbox has been marked "interesting" by the server; the |
| 2194 |
// mailbox probably contains messages that have been added since |
| 2195 |
// the last time the mailbox was selected. |
| 2196 |
// |
| 2197 |
// \Unmarked |
| 2198 |
// The mailbox does not contain any additional messages since the |
| 2199 |
// last time the mailbox was selected. |
| 2200 |
// |
| 2201 |
- (void) _parseLIST |
| 2202 |
{ |
| 2203 |
NSString *aFolderName, *aString, *theString; |
| 2204 |
NSRange r1, r2; |
| 2205 |
int flags; |
| 2206 |
NSUInteger len; |
| 2207 |
|
| 2208 |
theString = [[_responsesFromServer lastObject] asciiString]; |
| 2209 |
|
| 2210 |
// |
| 2211 |
// We verify if we got the number of bytes to read instead of the real mailbox name. |
| 2212 |
// That happens if we couldn't get the ASCII string of what we read. |
| 2213 |
// |
| 2214 |
// Some servers seem to send that when the mailbox name is 8-bit. Those 8-bit mailbox |
| 2215 |
// names were undefined in earlier versions of the IMAP protocol (now deprecated). |
| 2216 |
// See section 5.1. (Mailbox Naming) of RFC3051. |
| 2217 |
// |
| 2218 |
// The RFC says we SHOULD interpret that as UTF-8. |
| 2219 |
// |
| 2220 |
// If we got a 8-bit string, we rollback to get the previous answer in order |
| 2221 |
// to also decode the mailbox attribute. |
| 2222 |
// |
| 2223 |
if (!theString) |
| 2224 |
{ |
| 2225 |
aFolderName = AUTORELEASE([[NSString alloc] initWithData: [_responsesFromServer lastObject] encoding: NSUTF8StringEncoding]); |
| 2226 |
|
| 2227 |
// We get the "previous" line which contains our mailbox attributes |
| 2228 |
theString = [[_responsesFromServer objectAtIndex: [_responsesFromServer count]-2] asciiString]; |
| 2229 |
} |
| 2230 |
else |
| 2231 |
{ |
| 2232 |
// We get the folder name and the mailbox name attributes |
| 2233 |
aFolderName = [self _folderNameFromString: theString]; |
| 2234 |
} |
| 2235 |
|
| 2236 |
// |
| 2237 |
// If the folder name starts/ends with {}, that means it was "wrongly" encoded using |
| 2238 |
// 8-bit characters which are not allowed. We just return since we'll re-enter in |
| 2239 |
// _parseLIST whenever the real mailbox name will be read. |
| 2240 |
// |
| 2241 |
len = [aFolderName length]; |
| 2242 |
if (len > 0 && [aFolderName characterAtIndex: 0] == '{' && [aFolderName characterAtIndex: len-1] == '}') |
| 2243 |
{ |
| 2244 |
return; |
| 2245 |
} |
| 2246 |
|
| 2247 |
// We try to get our name attributes. |
| 2248 |
r1 = [theString rangeOfString: @"("]; |
| 2249 |
|
| 2250 |
if (r1.location == NSNotFound) |
| 2251 |
{ |
| 2252 |
return; |
| 2253 |
} |
| 2254 |
|
| 2255 |
r2 = [theString rangeOfString: @")" options: 0 range: NSMakeRange(r1.location+1, [theString length]-r1.location-1)]; |
| 2256 |
|
| 2257 |
if (r2.location == NSNotFound) |
| 2258 |
{ |
| 2259 |
return; |
| 2260 |
} |
| 2261 |
|
| 2262 |
aString = [theString substringWithRange: NSMakeRange(r1.location+1, r2.location-r1.location-1)]; |
| 2263 |
|
| 2264 |
// We get all the supported flags, starting with the flags of RFC3348 |
| 2265 |
flags = PantomimeHoldsMessages; |
| 2266 |
|
| 2267 |
if ([aString length]) |
| 2268 |
{ |
| 2269 |
if ([aString rangeOfString: @"\\HasChildren" options: NSCaseInsensitiveSearch].length) |
| 2270 |
{ |
| 2271 |
flags = flags|PantomimeHoldsFolders; |
| 2272 |
} |
| 2273 |
|
| 2274 |
if ([aString rangeOfString: @"\\NoInferiors" options: NSCaseInsensitiveSearch].length) |
| 2275 |
{ |
| 2276 |
flags = flags|PantomimeNoInferiors; |
| 2277 |
} |
| 2278 |
|
| 2279 |
if ([aString rangeOfString: @"\\NoSelect" options: NSCaseInsensitiveSearch].length) |
| 2280 |
{ |
| 2281 |
flags = flags|PantomimeNoSelect; |
| 2282 |
} |
| 2283 |
|
| 2284 |
if ([aString rangeOfString: @"\\Marked" options: NSCaseInsensitiveSearch].length) |
| 2285 |
{ |
| 2286 |
flags = flags|PantomimeMarked; |
| 2287 |
} |
| 2288 |
|
| 2289 |
if ([aString rangeOfString: @"\\Unmarked" options: NSCaseInsensitiveSearch].length) |
| 2290 |
{ |
| 2291 |
flags = flags|PantomimeUnmarked; |
| 2292 |
} |
| 2293 |
} |
| 2294 |
|
| 2295 |
[_folders setObject: [NSNumber numberWithInt: flags] forKey: aFolderName]; |
| 2296 |
} |
| 2297 |
|
| 2298 |
|
| 2299 |
// |
| 2300 |
// |
| 2301 |
// |
| 2302 |
- (void) _parseLSUB |
| 2303 |
{ |
| 2304 |
NSString *aString, *aFolderName; |
| 2305 |
NSUInteger len; |
| 2306 |
|
| 2307 |
aString = [[NSString alloc] initWithData: [_responsesFromServer lastObject] encoding: defaultCStringEncoding]; |
| 2308 |
|
| 2309 |
if (!aString) |
| 2310 |
{ |
| 2311 |
aFolderName = AUTORELEASE([[NSString alloc] initWithData: [_responsesFromServer lastObject] encoding: NSUTF8StringEncoding]); |
| 2312 |
} |
| 2313 |
else |
| 2314 |
{ |
| 2315 |
aFolderName = [self _folderNameFromString: [aString retain]]; |
| 2316 |
RELEASE(aString); |
| 2317 |
} |
| 2318 |
|
| 2319 |
|
| 2320 |
// Check the rationale in _parseLIST. |
| 2321 |
len = [aFolderName length]; |
| 2322 |
if (len > 0 && [aFolderName characterAtIndex: 0] == '{' && [aFolderName characterAtIndex: len-1] == '}') |
| 2323 |
{ |
| 2324 |
RELEASE(aString); |
| 2325 |
return; |
| 2326 |
} |
| 2327 |
|
| 2328 |
[_subscribedFolders addObject: aFolderName]; |
| 2329 |
RELEASE(aString); |
| 2330 |
} |
| 2331 |
|
| 2332 |
|
| 2333 |
// |
| 2334 |
// |
| 2335 |
// |
| 2336 |
- (void) _parseNO |
| 2337 |
{ |
| 2338 |
NSData *aData; |
| 2339 |
|
| 2340 |
aData = [_responsesFromServer lastObject]; |
| 2341 |
|
| 2342 |
//NSLog(@"IN _parseNO: |%@| %d", [aData asciiString], _lastCommand); |
| 2343 |
|
| 2344 |
switch (_lastCommand) |
| 2345 |
{ |
| 2346 |
case IMAP_APPEND: |
| 2347 |
POST_NOTIFICATION(PantomimeFolderAppendFailed, self, _currentQueueObject->info); |
| 2348 |
PERFORM_SELECTOR_3(_delegate, @selector(folderAppendFailed:), PantomimeFolderAppendFailed, _currentQueueObject->info); |
| 2349 |
break; |
| 2350 |
|
| 2351 |
case IMAP_AUTHENTICATE_CRAM_MD5: |
| 2352 |
case IMAP_AUTHENTICATE_LOGIN: |
| 2353 |
case IMAP_LOGIN: |
| 2354 |
AUTHENTICATION_FAILED(_delegate, _mechanism); |
| 2355 |
break; |
| 2356 |
|
| 2357 |
case IMAP_CREATE: |
| 2358 |
POST_NOTIFICATION(PantomimeFolderCreateFailed, self, _currentQueueObject->info); |
| 2359 |
PERFORM_SELECTOR_1(_delegate, @selector(folderCreateFailed:), PantomimeFolderCreateFailed); |
| 2360 |
break; |
| 2361 |
|
| 2362 |
case IMAP_DELETE: |
| 2363 |
POST_NOTIFICATION(PantomimeFolderDeleteFailed, self, _currentQueueObject->info); |
| 2364 |
PERFORM_SELECTOR_1(_delegate, @selector(folderDeleteFailed:), PantomimeFolderDeleteFailed); |
| 2365 |
break; |
| 2366 |
|
| 2367 |
case IMAP_EXPUNGE: |
| 2368 |
POST_NOTIFICATION(PantomimeFolderExpungeFailed, self, _currentQueueObject->info); |
| 2369 |
PERFORM_SELECTOR_2(_delegate, @selector(folderExpungeFailed:), PantomimeFolderExpungeFailed, _selectedFolder, @"Folder"); |
| 2370 |
break; |
| 2371 |
|
| 2372 |
case IMAP_RENAME: |
| 2373 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, _currentQueueObject->info); |
| 2374 |
PERFORM_SELECTOR_1(_delegate, @selector(folderRenameFailed:), PantomimeFolderRenameFailed); |
| 2375 |
break; |
| 2376 |
|
| 2377 |
case IMAP_SELECT: |
| 2378 |
_connection_state.opening_mailbox = NO; |
| 2379 |
POST_NOTIFICATION(PantomimeFolderOpenFailed, self, [NSDictionary dictionaryWithObject: _selectedFolder forKey: @"Folder"]); |
| 2380 |
PERFORM_SELECTOR_2(_delegate, @selector(folderOpenFailed:), PantomimeFolderOpenFailed, _selectedFolder, @"Folder"); |
| 2381 |
[_openFolders removeObjectForKey: [_selectedFolder name]]; |
| 2382 |
_selectedFolder = nil; |
| 2383 |
break; |
| 2384 |
|
| 2385 |
case IMAP_SUBSCRIBE: |
| 2386 |
POST_NOTIFICATION(PantomimeFolderSubscribeFailed, self, _currentQueueObject->info); |
| 2387 |
PERFORM_SELECTOR_2(_delegate, @selector(folderSubscribeFailed:), PantomimeFolderSubscribeFailed, [_currentQueueObject->info objectForKey: @"Name"], @"Name"); |
| 2388 |
break; |
| 2389 |
|
| 2390 |
case IMAP_UID_COPY: |
| 2391 |
POST_NOTIFICATION(PantomimeMessagesCopyFailed, self, _currentQueueObject->info); |
| 2392 |
PERFORM_SELECTOR_3(_delegate, @selector(messagesCopyFailed:), PantomimeMessagesCopyFailed, _currentQueueObject->info); |
| 2393 |
break; |
| 2394 |
|
| 2395 |
case IMAP_UID_SEARCH_ALL: |
| 2396 |
POST_NOTIFICATION(PantomimeFolderSearchFailed, self, _currentQueueObject->info); |
| 2397 |
PERFORM_SELECTOR_1(_delegate, @selector(folderSearchFailed:), PantomimeFolderSearchFailed); |
| 2398 |
break; |
| 2399 |
|
| 2400 |
case IMAP_STATUS: |
| 2401 |
POST_NOTIFICATION(PantomimeFolderStatusFailed, self, _currentQueueObject->info); |
| 2402 |
PERFORM_SELECTOR_2(_delegate, @selector(folderStatusFailed:), PantomimeFolderStatusFailed, [_currentQueueObject->info objectForKey: @"Name"], @"Name"); |
| 2403 |
break; |
| 2404 |
|
| 2405 |
case IMAP_UID_STORE: |
| 2406 |
POST_NOTIFICATION(PantomimeMessageStoreFailed, self, _currentQueueObject->info); |
| 2407 |
PERFORM_SELECTOR_3(_delegate, @selector(messageStoreFailed:), PantomimeMessageStoreFailed, _currentQueueObject->info); |
| 2408 |
break; |
| 2409 |
|
| 2410 |
case IMAP_UNSUBSCRIBE: |
| 2411 |
POST_NOTIFICATION(PantomimeFolderUnsubscribeFailed, self, _currentQueueObject->info); |
| 2412 |
PERFORM_SELECTOR_2(_delegate, @selector(folderUnsubscribeFailed:), PantomimeFolderUnsubscribeFailed, [_currentQueueObject->info objectForKey: @"Name"], @"Name"); |
| 2413 |
break; |
| 2414 |
|
| 2415 |
default: |
| 2416 |
break; |
| 2417 |
} |
| 2418 |
|
| 2419 |
// |
| 2420 |
// If the NO response is tagged response, we remove the current |
| 2421 |
// queued object from the queue since it reached completion. |
| 2422 |
// |
| 2423 |
if (![aData hasCPrefix: "*"])//|| _lastCommand == IMAP_AUTHORIZATION) |
| 2424 |
{ |
| 2425 |
//NSLog(@"REMOVING QUEUE OBJECT"); |
| 2426 |
|
| 2427 |
[_currentQueueObject->info setObject: [NSNumber numberWithUnsignedInt: _lastCommand] forKey: @"Command"]; |
| 2428 |
POST_NOTIFICATION(@"PantomimeCommandCompleted", self, _currentQueueObject->info); |
| 2429 |
PERFORM_SELECTOR_3(_delegate, @selector(commandCompleted:), @"PantomimeCommandCompleted", _currentQueueObject->info); |
| 2430 |
|
| 2431 |
[_queue removeLastObject]; |
| 2432 |
[self sendCommand: IMAP_EMPTY_QUEUE info: nil arguments: @""]; |
| 2433 |
} |
| 2434 |
|
| 2435 |
[_responsesFromServer removeAllObjects]; |
| 2436 |
} |
| 2437 |
|
| 2438 |
|
| 2439 |
// |
| 2440 |
// After sending a NOOP to the IMAP server, we might read untagged |
| 2441 |
// responses like * 5 RECENT that will eventually be processed. |
| 2442 |
// |
| 2443 |
- (void) _parseNOOP |
| 2444 |
{ |
| 2445 |
//NSLog(@"Parsing noop responses..."); |
| 2446 |
// FIXME |
| 2447 |
} |
| 2448 |
|
| 2449 |
|
| 2450 |
// |
| 2451 |
// |
| 2452 |
// |
| 2453 |
- (void) _parseOK |
| 2454 |
{ |
| 2455 |
NSData *aData; |
| 2456 |
|
| 2457 |
aData = [_responsesFromServer lastObject]; |
| 2458 |
|
| 2459 |
//NSLog(@"IN _parseOK: |%@|", [aData asciiString]); |
| 2460 |
|
| 2461 |
switch (_lastCommand) |
| 2462 |
{ |
| 2463 |
case IMAP_APPEND: |
| 2464 |
// |
| 2465 |
// No need to do add the newly append messages to our internal messages holder as |
| 2466 |
// we will get an untagged * EXISTS response that will trigger the new FETCH |
| 2467 |
// RFC3501 says: |
| 2468 |
// |
| 2469 |
// If the mailbox is currently selected, the normal new message |
| 2470 |
// actions SHOULD occur. Specifically, the server SHOULD notify the |
| 2471 |
// client immediately via an untagged EXISTS response. If the server |
| 2472 |
// does not do so, the client MAY issue a NOOP command (or failing |
| 2473 |
// that, a CHECK command) after one or more APPEND commands. |
| 2474 |
// |
| 2475 |
POST_NOTIFICATION(PantomimeFolderAppendCompleted, self, _currentQueueObject->info); |
| 2476 |
PERFORM_SELECTOR_3(_delegate, @selector(folderAppendCompleted:), PantomimeFolderAppendCompleted, _currentQueueObject->info); |
| 2477 |
break; |
| 2478 |
|
| 2479 |
case IMAP_AUTHENTICATE_CRAM_MD5: |
| 2480 |
case IMAP_AUTHENTICATE_LOGIN: |
| 2481 |
case IMAP_LOGIN: |
| 2482 |
if (_connection_state.reconnecting) |
| 2483 |
{ |
| 2484 |
if (_selectedFolder) |
| 2485 |
{ |
| 2486 |
if ([_selectedFolder mode] == PantomimeReadOnlyMode) |
| 2487 |
{ |
| 2488 |
[self sendCommand: IMAP_EXAMINE info: nil arguments: @"EXAMINE \"%@\"", [[_selectedFolder name] modifiedUTF7String]]; |
| 2489 |
} |
| 2490 |
else |
| 2491 |
{ |
| 2492 |
[self sendCommand: IMAP_SELECT info: nil arguments: @"SELECT \"%@\"", [[_selectedFolder name] modifiedUTF7String]]; |
| 2493 |
} |
| 2494 |
|
| 2495 |
if (_connection_state.opening_mailbox) [_selectedFolder prefetch]; |
| 2496 |
} |
| 2497 |
else |
| 2498 |
{ |
| 2499 |
[self _restoreQueue]; |
| 2500 |
} |
| 2501 |
} |
| 2502 |
else |
| 2503 |
{ |
| 2504 |
AUTHENTICATION_COMPLETED(_delegate, _mechanism); |
| 2505 |
} |
| 2506 |
break; |
| 2507 |
|
| 2508 |
case IMAP_AUTHORIZATION: |
| 2509 |
if ([aData hasCPrefix: "* OK"]) |
| 2510 |
{ |
| 2511 |
[self sendCommand: IMAP_CAPABILITY info: nil arguments: @"CAPABILITY"]; |
| 2512 |
} |
| 2513 |
else |
| 2514 |
{ |
| 2515 |
// FIXME |
| 2516 |
// connectionLost? or should we call [self close]? |
| 2517 |
} |
| 2518 |
break; |
| 2519 |
|
| 2520 |
case IMAP_CLOSE: |
| 2521 |
POST_NOTIFICATION(PantomimeFolderCloseCompleted, self, _currentQueueObject->info); |
| 2522 |
PERFORM_SELECTOR_3(_delegate, @selector(folderCloseCompleted:), PantomimeFolderCloseCompleted, _currentQueueObject->info); |
| 2523 |
break; |
| 2524 |
|
| 2525 |
case IMAP_CREATE: |
| 2526 |
[_folders setObject: [NSNumber numberWithInt: 0] forKey: [_currentQueueObject->info objectForKey: @"Name"]]; |
| 2527 |
POST_NOTIFICATION(PantomimeFolderCreateCompleted, self, _currentQueueObject->info); |
| 2528 |
PERFORM_SELECTOR_1(_delegate, @selector(folderCreateCompleted:), PantomimeFolderCreateCompleted); |
| 2529 |
break; |
| 2530 |
|
| 2531 |
case IMAP_DELETE: |
| 2532 |
[_folders removeObjectForKey: [_currentQueueObject->info objectForKey: @"Name"]]; |
| 2533 |
POST_NOTIFICATION(PantomimeFolderDeleteCompleted, self, _currentQueueObject->info); |
| 2534 |
PERFORM_SELECTOR_1(_delegate, @selector(folderDeleteCompleted:), PantomimeFolderDeleteCompleted); |
| 2535 |
break; |
| 2536 |
|
| 2537 |
case IMAP_EXPUNGE: |
| 2538 |
// |
| 2539 |
// No need to synchronize our IMAP cache here since, at worst, the |
| 2540 |
// expunged messages will get removed once we reopen the mailbox. |
| 2541 |
// |
| 2542 |
if ([_selectedFolder allContainers]) |
| 2543 |
{ |
| 2544 |
[_selectedFolder thread]; |
| 2545 |
} |
| 2546 |
|
| 2547 |
if ([_selectedFolder cacheManager]) |
| 2548 |
{ |
| 2549 |
[[_selectedFolder cacheManager] expunge]; |
| 2550 |
} |
| 2551 |
POST_NOTIFICATION(PantomimeFolderExpungeCompleted, self, _currentQueueObject->info); |
| 2552 |
PERFORM_SELECTOR_2(_delegate, @selector(folderExpungeCompleted:), PantomimeFolderExpungeCompleted, _selectedFolder, @"Folder"); |
| 2553 |
break; |
| 2554 |
|
| 2555 |
case IMAP_LIST: |
| 2556 |
POST_NOTIFICATION(PantomimeFolderListCompleted, self, [NSDictionary dictionaryWithObject: [_folders keyEnumerator] forKey: @"NSEnumerator"]); |
| 2557 |
PERFORM_SELECTOR_2(_delegate, @selector(folderListCompleted:), PantomimeFolderListCompleted, [_folders keyEnumerator], @"NSEnumerator"); |
| 2558 |
break; |
| 2559 |
|
| 2560 |
case IMAP_LOGOUT: |
| 2561 |
// FIXME: What should we do here? |
| 2562 |
[super close]; |
| 2563 |
break; |
| 2564 |
|
| 2565 |
case IMAP_LSUB: |
| 2566 |
POST_NOTIFICATION(PantomimeFolderListSubscribedCompleted, self, [NSDictionary dictionaryWithObject: [_subscribedFolders objectEnumerator] forKey: @"NSEnumerator"]); |
| 2567 |
PERFORM_SELECTOR_2(_delegate, @selector(folderListSubscribedCompleted:), PantomimeFolderListSubscribedCompleted, [_subscribedFolders objectEnumerator], @"NSEnumerator"); |
| 2568 |
break; |
| 2569 |
|
| 2570 |
case IMAP_RENAME: |
| 2571 |
[self _renameFolder]; |
| 2572 |
POST_NOTIFICATION(PantomimeFolderRenameCompleted, self, _currentQueueObject->info); |
| 2573 |
PERFORM_SELECTOR_1(_delegate, @selector(folderRenameCompleted:), PantomimeFolderRenameCompleted); |
| 2574 |
break; |
| 2575 |
|
| 2576 |
case IMAP_SELECT: |
| 2577 |
[self _parseSELECT]; |
| 2578 |
break; |
| 2579 |
|
| 2580 |
case IMAP_STARTTLS: |
| 2581 |
[self _parseSTARTTLS]; |
| 2582 |
break; |
| 2583 |
|
| 2584 |
case IMAP_SUBSCRIBE: |
| 2585 |
// We must add the folder to our list of subscribed folders. |
| 2586 |
[_subscribedFolders addObject: [_currentQueueObject->info objectForKey: @"Name"]]; |
| 2587 |
POST_NOTIFICATION(PantomimeFolderSubscribeCompleted, self, _currentQueueObject->info); |
| 2588 |
PERFORM_SELECTOR_2(_delegate, @selector(folderSubscribeCompleted:), PantomimeFolderSubscribeCompleted, [_currentQueueObject->info objectForKey: @"Name"], @"Name"); |
| 2589 |
break; |
| 2590 |
|
| 2591 |
case IMAP_UID_COPY: |
| 2592 |
POST_NOTIFICATION(PantomimeMessagesCopyCompleted, self, _currentQueueObject->info); |
| 2593 |
PERFORM_SELECTOR_3(_delegate, @selector(messagesCopyCompleted:), PantomimeMessagesCopyCompleted, _currentQueueObject->info); |
| 2594 |
break; |
| 2595 |
|
| 2596 |
case IMAP_UID_FETCH_HEADER_FIELDS: |
| 2597 |
{ |
| 2598 |
_connection_state.opening_mailbox = NO; |
| 2599 |
|
| 2600 |
if ([_selectedFolder cacheManager]) |
| 2601 |
{ |
| 2602 |
[[_selectedFolder cacheManager] synchronize]; |
| 2603 |
} |
| 2604 |
|
| 2605 |
//NSLog(@"DONE PREFETCHING FOLDER"); |
| 2606 |
POST_NOTIFICATION(PantomimeFolderPrefetchCompleted, self, [NSDictionary dictionaryWithObject: _selectedFolder forKey: @"Folder"]); |
| 2607 |
PERFORM_SELECTOR_2(_delegate, @selector(folderPrefetchCompleted:), PantomimeFolderPrefetchCompleted, _selectedFolder, @"Folder"); |
| 2608 |
} |
| 2609 |
break; |
| 2610 |
|
| 2611 |
case IMAP_UID_SEARCH_ALL: |
| 2612 |
// |
| 2613 |
// Before assuming we got a result and initialized everything in _parseSEARCH, |
| 2614 |
// we do a basic check. This is to prevent a rather weird behavior from |
| 2615 |
// UW IMAP Server, like this: |
| 2616 |
// |
| 2617 |
// . UID SEARCH ALL FROM "collaboration-world" |
| 2618 |
// * OK [PARSE] Unexpected characters at end of address: <>, [email protected]... |
| 2619 |
// * SEARCH |
| 2620 |
// 000d OK UID SEARCH completed^ |
| 2621 |
// |
| 2622 |
if ([_currentQueueObject->info objectForKey: @"Results"]) |
| 2623 |
{ |
| 2624 |
NSDictionary *userInfo; |
| 2625 |
|
| 2626 |
userInfo = [NSDictionary dictionaryWithObjectsAndKeys: _selectedFolder, @"Folder", [_currentQueueObject->info objectForKey: @"Results"], @"Results", nil]; |
| 2627 |
POST_NOTIFICATION(PantomimeFolderSearchCompleted, self, userInfo); |
| 2628 |
PERFORM_SELECTOR_3(_delegate, @selector(folderSearchCompleted:), PantomimeFolderSearchCompleted, userInfo); |
| 2629 |
} |
| 2630 |
break; |
| 2631 |
|
| 2632 |
case IMAP_UID_STORE: |
| 2633 |
{ |
| 2634 |
// Once the STORE has completed, we update the messages. |
| 2635 |
NSArray *theMessages; |
| 2636 |
CWFlags *theFlags; |
| 2637 |
NSUInteger i, count; |
| 2638 |
|
| 2639 |
theMessages = [_currentQueueObject->info objectForKey: @"Messages"]; |
| 2640 |
theFlags = [_currentQueueObject->info objectForKey: @"Flags"]; |
| 2641 |
count = [theMessages count]; |
| 2642 |
|
| 2643 |
for (i = 0; i < count; i++) |
| 2644 |
{ |
| 2645 |
[[[theMessages objectAtIndex: i] flags] replaceWithFlags: theFlags]; |
| 2646 |
} |
| 2647 |
|
| 2648 |
POST_NOTIFICATION(PantomimeMessageStoreCompleted, self, _currentQueueObject->info); |
| 2649 |
PERFORM_SELECTOR_3(_delegate, @selector(messageStoreCompleted:), PantomimeMessageStoreCompleted, _currentQueueObject->info); |
| 2650 |
} |
| 2651 |
break; |
| 2652 |
|
| 2653 |
case IMAP_UNSUBSCRIBE: |
| 2654 |
// We must remove the folder from our list of subscribed folders. |
| 2655 |
[_subscribedFolders removeObject: [_currentQueueObject->info objectForKey: @"Name"]]; |
| 2656 |
POST_NOTIFICATION(PantomimeFolderUnsubscribeCompleted, self, _currentQueueObject->info); |
| 2657 |
PERFORM_SELECTOR_2(_delegate, @selector(folderUnsubscribeCompleted:), PantomimeFolderUnsubscribeCompleted, [_currentQueueObject->info objectForKey: @"Name"], @"Name"); |
| 2658 |
break; |
| 2659 |
|
| 2660 |
default: |
| 2661 |
break; |
| 2662 |
} |
| 2663 |
|
| 2664 |
// |
| 2665 |
// If the OK response is tagged response, we remove the current |
| 2666 |
// queued object from the queue since it reached completion. |
| 2667 |
// |
| 2668 |
if (![aData hasCPrefix: "*"])// || _lastCommand == IMAP_AUTHORIZATION) |
| 2669 |
{ |
| 2670 |
//NSLog(@"REMOVING QUEUE OBJECT"); |
| 2671 |
[_currentQueueObject->info setObject: [NSNumber numberWithUnsignedInt: _lastCommand] forKey: @"Command"]; |
| 2672 |
POST_NOTIFICATION(@"PantomimeCommandCompleted", self, _currentQueueObject->info); |
| 2673 |
PERFORM_SELECTOR_3(_delegate, @selector(commandCompleted:), @"PantomimeCommandCompleted", _currentQueueObject->info); |
| 2674 |
|
| 2675 |
[_queue removeLastObject]; |
| 2676 |
[self sendCommand: IMAP_EMPTY_QUEUE info: nil arguments: @""]; |
| 2677 |
} |
| 2678 |
|
| 2679 |
[_responsesFromServer removeAllObjects]; |
| 2680 |
} |
| 2681 |
|
| 2682 |
|
| 2683 |
// |
| 2684 |
// This method receives a * 5 RECENT parameter and parses it. |
| 2685 |
// |
| 2686 |
- (void) _parseRECENT |
| 2687 |
{ |
| 2688 |
// Do nothing for now. This breaks 7.3.2 since the response |
| 2689 |
// is not recorded. |
| 2690 |
} |
| 2691 |
|
| 2692 |
|
| 2693 |
// |
| 2694 |
// |
| 2695 |
// |
| 2696 |
- (void) _parseSEARCH |
| 2697 |
{ |
| 2698 |
NSMutableArray *aMutableArray; |
| 2699 |
CWIMAPMessage *aMessage; |
| 2700 |
NSArray *allResults; |
| 2701 |
NSUInteger i, count; |
| 2702 |
|
| 2703 |
allResults = [self _uniqueIdentifiersFromData: [_responsesFromServer lastObject]]; |
| 2704 |
count = [allResults count]; |
| 2705 |
|
| 2706 |
aMutableArray = [NSMutableArray array]; |
| 2707 |
|
| 2708 |
|
| 2709 |
for (i = 0; i < count; i++) |
| 2710 |
{ |
| 2711 |
aMessage = [(CWIMAPCacheManager *)[_selectedFolder cacheManager] messageWithUID: |
| 2712 |
[(NSNumber *)[allResults objectAtIndex: i] unsignedIntValue]]; |
| 2713 |
|
| 2714 |
if (aMessage) |
| 2715 |
{ |
| 2716 |
[aMutableArray addObject: aMessage]; |
| 2717 |
} |
| 2718 |
else |
| 2719 |
{ |
| 2720 |
//NSLog(@"Message with UID = %u not found in cache.", |
| 2721 |
// [[allResults objectAtIndex: i] unsignedIntValue]); |
| 2722 |
} |
| 2723 |
} |
| 2724 |
|
| 2725 |
// We store the results in our command queue (ie., in the current queue object). |
| 2726 |
// aMutableArray may be empty if no result was found |
| 2727 |
if (_currentQueueObject) |
| 2728 |
[_currentQueueObject->info setObject: aMutableArray forKey: @"Results"]; |
| 2729 |
} |
| 2730 |
|
| 2731 |
|
| 2732 |
// |
| 2733 |
// This methods updates all FLAGS and MSNs for messages in the cache. |
| 2734 |
// |
| 2735 |
// It also purges the messages that have been deleted on the IMAP server |
| 2736 |
// but that are still present in the folder cache. |
| 2737 |
// |
| 2738 |
// Nota bene: We can safely assume our cacheManager exists since this method |
| 2739 |
// wouldn't otherwise have been invoked. |
| 2740 |
// |
| 2741 |
// |
| 2742 |
- (void) _parseSEARCH_CACHE |
| 2743 |
{ |
| 2744 |
CWIMAPMessage *aMessage; |
| 2745 |
NSArray *allResults; |
| 2746 |
NSUInteger i, count; |
| 2747 |
BOOL b; |
| 2748 |
|
| 2749 |
allResults = [self _uniqueIdentifiersFromData: [_responsesFromServer objectAtIndex: 0]]; |
| 2750 |
count = [allResults count]; |
| 2751 |
|
| 2752 |
switch (_lastCommand) |
| 2753 |
{ |
| 2754 |
case IMAP_UID_SEARCH: |
| 2755 |
// |
| 2756 |
// We can now read our SEARCH results from our IMAP store. The result contains |
| 2757 |
// all MSN->UID mappings. New messages weren't added to the search result as |
| 2758 |
// we couldn't find them in IMAPStore: -_parseSearch:. |
| 2759 |
// |
| 2760 |
for (i = 0; i < count; i++) |
| 2761 |
{ |
| 2762 |
aMessage = [(CWIMAPCacheManager *)[_selectedFolder cacheManager] messageWithUID: [(NSNumber *)[allResults objectAtIndex: i] unsignedIntValue]]; |
| 2763 |
|
| 2764 |
if (aMessage) |
| 2765 |
{ |
| 2766 |
[aMessage setFolder: _selectedFolder]; |
| 2767 |
[aMessage setMessageNumber: (i+1)]; |
| 2768 |
} |
| 2769 |
} |
| 2770 |
|
| 2771 |
// |
| 2772 |
// We purge our cache from all deleted messages and we keep the |
| 2773 |
// good ones to our folder. |
| 2774 |
// |
| 2775 |
//for (i = ([theCache count]-1); i >= 0; i--) |
| 2776 |
//NSLog(@"Folder count (to remove UID) = %d", [_selectedFolder->allMessages count]); |
| 2777 |
b = NO; |
| 2778 |
|
| 2779 |
for (i = ([[_selectedFolder messages] count]); i > 0; i--) |
| 2780 |
{ |
| 2781 |
aMessage = [[_selectedFolder messages] objectAtIndex: i-1]; |
| 2782 |
//aMessage = [theCache objectAtIndex: i]; |
| 2783 |
|
| 2784 |
if ([aMessage folder] == nil) |
| 2785 |
{ |
| 2786 |
[(CWIMAPCacheManager *)[_selectedFolder cacheManager] removeMessageWithUID: [aMessage UID]]; |
| 2787 |
//NSLog(@"Removed message |%@| UID = %d", [aMessage subject], [aMessage UID]); |
| 2788 |
[[_selectedFolder messages] removeObject: aMessage]; |
| 2789 |
b = YES; |
| 2790 |
} |
| 2791 |
|
| 2792 |
} |
| 2793 |
|
| 2794 |
// We check to see if we must expunge deleted messages from our cache. |
| 2795 |
// It's important to do this here. Otherwise, calling -synchronize on |
| 2796 |
// our cache manager could lead to offset problems as the number of |
| 2797 |
// records in our cache would be greater than the amount of entries |
| 2798 |
// in our _selectedFolder->allMessages ivar. |
| 2799 |
if (b && [_selectedFolder cacheManager]) |
| 2800 |
{ |
| 2801 |
[[_selectedFolder cacheManager] expunge]; |
| 2802 |
} |
| 2803 |
|
| 2804 |
[_selectedFolder updateCache]; |
| 2805 |
[self sendCommand: IMAP_UID_SEARCH_ANSWERED info: nil arguments: @"UID SEARCH ANSWERED"]; |
| 2806 |
break; |
| 2807 |
|
| 2808 |
case IMAP_UID_SEARCH_ANSWERED: |
| 2809 |
// |
| 2810 |
// We now update our \Answered flag, for all messages. |
| 2811 |
// |
| 2812 |
for (i = 0; i < count; i++) |
| 2813 |
{ |
| 2814 |
[[[(CWIMAPCacheManager *)[_selectedFolder cacheManager] messageWithUID: [[allResults objectAtIndex: i] unsignedIntValue]] flags] add: PantomimeAnswered]; |
| 2815 |
} |
| 2816 |
[self sendCommand: IMAP_UID_SEARCH_FLAGGED info: nil arguments: @"UID SEARCH FLAGGED"]; |
| 2817 |
break; |
| 2818 |
|
| 2819 |
case IMAP_UID_SEARCH_FLAGGED: |
| 2820 |
// |
| 2821 |
// We now update our \Flagged flag, for all messages. |
| 2822 |
// |
| 2823 |
for (i = 0; i < count; i++) |
| 2824 |
{ |
| 2825 |
[[[(CWIMAPCacheManager *)[_selectedFolder cacheManager] messageWithUID: [[allResults objectAtIndex: i] unsignedIntValue]] flags] add: PantomimeFlagged]; |
| 2826 |
} |
| 2827 |
[self sendCommand: IMAP_UID_SEARCH_UNSEEN info: nil arguments: @"UID SEARCH UNSEEN"]; |
| 2828 |
break; |
| 2829 |
|
| 2830 |
case IMAP_UID_SEARCH_UNSEEN: |
| 2831 |
// |
| 2832 |
// We now update our \Seen flag, for all messages. |
| 2833 |
// |
| 2834 |
for (i = 0; i < count; i++) |
| 2835 |
{ |
| 2836 |
//NSLog(@"removing for UID %d", [[allResults objectAtIndex: i] unsignedIntValue]); |
| 2837 |
[[[(CWIMAPCacheManager *)[_selectedFolder cacheManager] messageWithUID: [[allResults objectAtIndex: i] unsignedIntValue]] flags] remove: PantomimeSeen]; |
| 2838 |
} |
| 2839 |
|
| 2840 |
// |
| 2841 |
// We obtain the last UID of our cache. |
| 2842 |
// Messages will be fetched starting from that UID + 1. |
| 2843 |
// |
| 2844 |
//NSLog(@"LAST UID IN CACHE: %u", [[_selectedFolder->allMessages lastObject] UID]); |
| 2845 |
[self 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)])", ([[[_selectedFolder messages] lastObject] UID]+1)]; |
| 2846 |
break; |
| 2847 |
|
| 2848 |
default: |
| 2849 |
//NSLog(@"Unknown command for updating the cache file. Ignored."); |
| 2850 |
break; |
| 2851 |
} |
| 2852 |
} |
| 2853 |
|
| 2854 |
|
| 2855 |
// |
| 2856 |
// |
| 2857 |
// |
| 2858 |
- (void) _parseSELECT |
| 2859 |
{ |
| 2860 |
NSData *aData; |
| 2861 |
NSUInteger i, count; |
| 2862 |
|
| 2863 |
// The last object in _responsesFromServer is a tagged OK response. |
| 2864 |
// We need to parse it here. |
| 2865 |
count = [_responsesFromServer count]; |
| 2866 |
|
| 2867 |
for (i = 0; i < count; i++) |
| 2868 |
{ |
| 2869 |
aData = [[_responsesFromServer objectAtIndex: i] dataByTrimmingWhiteSpaces]; |
| 2870 |
|
| 2871 |
//NSLog(@"|%@|", [aData asciiString]); |
| 2872 |
// * OK [UIDVALIDITY 1052146864] |
| 2873 |
if ([aData hasCPrefix: "* OK [UIDVALIDITY"] && [aData hasCSuffix: "]"]) |
| 2874 |
{ |
| 2875 |
[self _parseUIDVALIDITY: [aData cString]]; |
| 2876 |
} |
| 2877 |
|
| 2878 |
// 3c4d OK [READ-ONLY] Completed |
| 2879 |
if ([aData rangeOfCString: "OK [READ-ONLY]"].length) |
| 2880 |
{ |
| 2881 |
[_selectedFolder setMode: PantomimeReadOnlyMode]; |
| 2882 |
} |
| 2883 |
|
| 2884 |
// 1a2b OK [READ-WRITE] Completed |
| 2885 |
if ([aData rangeOfCString: "OK [READ-WRITE]"].length) |
| 2886 |
{ |
| 2887 |
[_selectedFolder setMode: PantomimeReadWriteMode]; |
| 2888 |
} |
| 2889 |
} |
| 2890 |
|
| 2891 |
if (_connection_state.reconnecting) |
| 2892 |
{ |
| 2893 |
[self _restoreQueue]; |
| 2894 |
} |
| 2895 |
else |
| 2896 |
{ |
| 2897 |
[_selectedFolder setSelected: YES]; |
| 2898 |
POST_NOTIFICATION(PantomimeFolderOpenCompleted, self, [NSDictionary dictionaryWithObject: _selectedFolder forKey: @"Folder"]); |
| 2899 |
PERFORM_SELECTOR_2(_delegate, @selector(folderOpenCompleted:), PantomimeFolderOpenCompleted, _selectedFolder, @"Folder"); |
| 2900 |
} |
| 2901 |
} |
| 2902 |
|
| 2903 |
|
| 2904 |
// |
| 2905 |
// |
| 2906 |
// |
| 2907 |
- (void) _parseSTARTTLS |
| 2908 |
{ |
| 2909 |
[(CWTCPConnection *)_connection startSSL]; |
| 2910 |
POST_NOTIFICATION(PantomimeServiceInitialized, self, nil); |
| 2911 |
PERFORM_SELECTOR_1(_delegate, @selector(serviceInitialized:), PantomimeServiceInitialized); |
| 2912 |
} |
| 2913 |
|
| 2914 |
// |
| 2915 |
// |
| 2916 |
// This method receives a * STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292) |
| 2917 |
// parameter and parses it. It then put the decoded values in the |
| 2918 |
// folderStatus dictionary. |
| 2919 |
// |
| 2920 |
// |
| 2921 |
- (void) _parseSTATUS |
| 2922 |
{ |
| 2923 |
CWFolderInformation *aFolderInformation; |
| 2924 |
NSString *aFolderName; |
| 2925 |
NSDictionary *info; |
| 2926 |
NSData *aData; |
| 2927 |
|
| 2928 |
NSRange aRange; |
| 2929 |
unsigned messages, unseen; |
| 2930 |
|
| 2931 |
aData = [_responsesFromServer lastObject]; |
| 2932 |
|
| 2933 |
aRange = [aData rangeOfCString: "(" options: NSBackwardsSearch]; |
| 2934 |
aFolderName = [[[aData subdataToIndex: (aRange.location-1)] subdataFromIndex: 9] asciiString]; |
| 2935 |
|
| 2936 |
sscanf([[aData subdataFromIndex: aRange.location] cString], "(MESSAGES %u UNSEEN %u)", &messages, &unseen); |
| 2937 |
|
| 2938 |
aFolderInformation = [[CWFolderInformation alloc] init]; |
| 2939 |
[aFolderInformation setNbOfMessages: messages]; |
| 2940 |
[aFolderInformation setNbOfUnreadMessages: unseen]; |
| 2941 |
|
| 2942 |
// Before putting the folder in our dictionary, we unquote it. |
| 2943 |
aFolderName = [aFolderName stringFromQuotedString]; |
| 2944 |
[_folderStatus setObject: aFolderInformation forKey: aFolderName]; |
| 2945 |
|
| 2946 |
info = [NSDictionary dictionaryWithObjectsAndKeys: aFolderInformation, @"FolderInformation", aFolderName, @"FolderName", nil]; |
| 2947 |
POST_NOTIFICATION(PantomimeFolderStatusCompleted, self, info); |
| 2948 |
|
| 2949 |
if (_delegate && [_delegate respondsToSelector: @selector(folderStatusCompleted:)]) |
| 2950 |
{ |
| 2951 |
[_delegate performSelector: @selector(folderStatusCompleted:) |
| 2952 |
withObject: [NSNotification notificationWithName: PantomimeFolderStatusCompleted |
| 2953 |
object: self |
| 2954 |
userInfo: info]]; |
| 2955 |
} |
| 2956 |
|
| 2957 |
RELEASE(aFolderInformation); |
| 2958 |
} |
| 2959 |
|
| 2960 |
|
| 2961 |
// |
| 2962 |
// Example: * OK [UIDVALIDITY 948394385] |
| 2963 |
// |
| 2964 |
- (void) _parseUIDVALIDITY: (const char *) theString |
| 2965 |
{ |
| 2966 |
unsigned int n; |
| 2967 |
sscanf(theString, "* OK [UIDVALIDITY %u]", &n); |
| 2968 |
[_selectedFolder setUIDValidity: n]; |
| 2969 |
} |
| 2970 |
|
| 2971 |
|
| 2972 |
// |
| 2973 |
// |
| 2974 |
// |
| 2975 |
- (void) _restoreQueue |
| 2976 |
{ |
| 2977 |
// We restore our list of pending commands |
| 2978 |
[_queue addObjectsFromArray: _connection_state.previous_queue]; |
| 2979 |
|
| 2980 |
// We clean the state |
| 2981 |
[_connection_state.previous_queue removeAllObjects]; |
| 2982 |
_connection_state.reconnecting = NO; |
| 2983 |
|
| 2984 |
POST_NOTIFICATION(PantomimeServiceReconnected, self, nil); |
| 2985 |
PERFORM_SELECTOR_1(_delegate, @selector(serviceReconnected:), PantomimeServiceReconnected); |
| 2986 |
} |
| 2987 |
|
| 2988 |
@end |