Debian Codegraph @

Framework/Pantomime/CWPOP3Store.m

raw
1 /*
2 ** CWPOP3Store.m
3 **
4 ** Copyright (c) 2001-2006 Ludovic Marcotte
5 ** Copyright (C) 2017-2022 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola <[email protected]>
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #import <Pantomime/CWPOP3Store.h>
25
26 #import <Pantomime/CWConstants.h>
27 #import <Pantomime/CWMD5.h>
28 #import <Pantomime/CWMIMEUtility.h>
29 #import <Pantomime/NSData+Extensions.h>
30 #import <Pantomime/CWPOP3CacheManager.h>
31 #import <Pantomime/CWPOP3Folder.h>
32 #import <Pantomime/CWPOP3Message.h>
33 #import <Pantomime/CWStore.h>
34 #import <Pantomime/CWTCPConnection.h>
35 #import <Pantomime/CWURLName.h>
36
37 #import <Foundation/NSArray.h>
38 #import <Foundation/NSBundle.h>
39 #import <Foundation/NSException.h>
40 #import <Foundation/NSNotification.h>
41 #import <Foundation/NSPathUtilities.h>
42
43 #include <stdio.h>
44
45 //
46 // Some static variables used to enhance the performance.
47 //
48 static NSStringEncoding defaultCStringEncoding;
49 static NSData *CRLF;
50
51 //
52 //
53 //
54 @interface CWPOP3QueueObject : NSObject
55 {
56 @public
57 POP3Command command;
58 NSString *arguments;
59 }
60 - (id) initWithCommand: (POP3Command) theCommand
61 arguments: (NSString *) theArguments;
62 @end
63
64 @implementation CWPOP3QueueObject
65
66 - (id) initWithCommand: (POP3Command) theCommand
67 arguments: (NSString *) theArguments
68 {
69 self = [super init];
70 if (self)
71 {
72 command = theCommand;
73 ASSIGN(arguments, theArguments);
74 }
75 return self;
76 }
77
78 - (void) dealloc
79 {
80 RELEASE(arguments);
81 [super dealloc];
82 }
83 @end
84
85
86
87 //
88 // Private methods
89 //
90 @interface CWPOP3Store (Private)
91
92 - (void) _parseAPOP;
93 - (void) _parseAUTHORIZATION;
94 - (void) _parseCAPA;
95 - (void) _parseLIST;
96 - (void) _parseNOOP;
97 - (void) _parsePASS;
98 - (void) _parseQUIT;
99 - (void) _parseRETR;
100 - (void) _parseSTAT;
101 - (void) _parseSTLS;
102 - (void) _parseTOP;
103 - (void) _parseUIDL;
104 - (void) _parseUSER;
105 - (void) _parseServerOutput;
106
107 @end
108
109
110 //
111 //
112 //
113 @implementation CWPOP3Store
114
115 + (void) initialize
116 {
117 defaultCStringEncoding = [NSString defaultCStringEncoding];
118 CRLF = [[NSData alloc] initWithBytes: "\r\n" length: 2];
119 }
120
121
122 //
123 //
124 //
125 - (id) initWithName: (NSString *) theName
126 port: (unsigned int) thePort
127 {
128 if (thePort == 0) thePort = 110;
129
130 self = [super initWithName: theName port: thePort];
131 if (self)
132 {
133 _lastCommand = POP3_AUTHORIZATION;
134 _timestamp = nil;
135
136 // We initialize out POP3Folder object.
137 _folder = [[CWPOP3Folder alloc] initWithName: @"Inbox"];
138 [_folder setStore: self];
139
140 // We queue our first "command".
141 [_queue addObject: AUTORELEASE([[CWPOP3QueueObject alloc] initWithCommand: _lastCommand arguments: @""])];
142 }
143
144 return self;
145 }
146
147
148 //
149 //
150 //
151 - (void) dealloc
152 {
153 //NSLog(@"POP3Store: -dealloc");
154 RELEASE(_folder);
155 RELEASE(_timestamp);
156
157 [super dealloc];
158 }
159
160
161 //
162 //
163 //
164 - (id) initWithURL: (CWURLName *) theURL
165 {
166 return [self initWithName: [theURL host] port: 110];
167 }
168
169
170 //
171 //
172 //
173 - (NSEnumerator *) folderEnumerator
174 {
175 return [[NSArray arrayWithObject: @"Inbox"] objectEnumerator];
176 }
177
178
179 //
180 //
181 //
182 - (NSEnumerator *) subscribedFolderEnumerator
183 {
184 return [self folderEnumerator];
185 }
186
187
188 //
189 //
190 //
191 - (NSEnumerator *) openFoldersEnumerator
192 {
193 return [[NSArray arrayWithObject: _folder] objectEnumerator];
194 }
195
196
197 //
198 //
199 //
200 - (void) removeFolderFromOpenFolders: (CWFolder *) theFolder
201 {
202 // Do nothing
203 }
204
205
206 //
207 // This method authenticates the Store to the POP3 server.
208 // In case of an error, it returns NO.
209 //
210 - (void) authenticate: (NSString *) theUsername
211 password: (NSString *) thePassword
212 mechanism: (NSString *) theMechanism
213 {
214 ASSIGN(_username, theUsername);
215 ASSIGN(_password, thePassword);
216 ASSIGN(_mechanism, theMechanism);
217
218 // We verify if we must use APOP
219 if (theMechanism && [theMechanism caseInsensitiveCompare: @"APOP"] == NSOrderedSame)
220 {
221 NSMutableData *aMutableData;
222 CWMD5 *aMD5;
223
224 aMutableData = [[NSMutableData alloc] init];
225 [aMutableData appendCFormat: @"%@%@", _timestamp, _password];
226 aMD5 = [[CWMD5 alloc] initWithData: aMutableData];
227 RELEASE(aMutableData);
228
229 [aMD5 computeDigest];
230
231 [self sendCommand: POP3_APOP arguments: @"APOP %@ %@", _username, [aMD5 digestAsString]];
232 RELEASE(aMD5);
233 }
234 else
235 {
236 [self sendCommand: POP3_USER arguments: @"USER %@", _username];
237 }
238 }
239
240
241 //
242 //
243 //
244 - (NSArray *) supportedMechanisms
245 {
246 if (_timestamp)
247 {
248 return [NSArray arrayWithObject: @"APOP"];
249 }
250
251 return [NSArray array];
252 }
253
254
255 //
256 // The default folder in POP3 is always Inbox.
257 //
258 - (id) defaultFolder
259 {
260 return _folder;
261 }
262
263
264 //
265 // This method will always return nil if theName is not
266 // equal to Inbox (case-insensitive) since you cannot
267 // access an other mailbox (other than Inbox) using the
268 // POP3 protocol.
269 //
270 - (id) folderForName: (NSString *) theName
271 {
272 if ([theName caseInsensitiveCompare: @"Inbox"] == NSOrderedSame)
273 {
274 return [self defaultFolder];
275 }
276
277 return nil;
278 }
279
280
281 //
282 //
283 //
284 - (id) folderForURL: (NSString *) theURL
285 {
286 return [self defaultFolder];
287 }
288
289
290 //
291 //
292 //
293 - (BOOL) folderForNameIsOpen: (NSString *) theName
294 {
295 if ([theName caseInsensitiveCompare: @"Inbox"] == NSOrderedSame)
296 {
297 return YES;
298 }
299
300 return NO;
301 }
302
303
304 //
305 // No other folder is allowed in POP3 other than the Inbox.
306 // Also, this folder can only hold messages.
307 //
308 - (PantomimeFolderType) folderTypeForFolderName: (NSString *) theName
309 {
310 return PantomimeHoldsMessages;
311 }
312
313
314 //
315 // POP3 has no concept of folder separator.
316 //
317 - (unichar) folderSeparator
318 {
319 return 0;
320 }
321
322
323 //
324 //
325 //
326 - (void) close
327 {
328 [self sendCommand: POP3_QUIT arguments: @"QUIT"];
329 }
330
331
332 //
333 //
334 //
335 - (void) noop
336 {
337 [self sendCommand: POP3_NOOP arguments: @"NOOP"];
338 }
339
340
341 //
342 // In POP3, you are NOT allowed to create a folder.
343 //
344 - (void) createFolderWithName: (NSString *) theName
345 type: (PantomimeFolderFormat) theType
346 contents: (NSData *) theContents
347 {
348 }
349
350
351 //
352 // In POP3, you are NOT allowed to delete a folder.
353 //
354 - (void) deleteFolderWithName: (NSString *) theName
355 {
356 }
357
358
359 //
360 // In POP3, you are NOT allowed to rename a folder.
361 //
362 - (void) renameFolderWithName: (NSString *) theName
363 toName: (NSString *) theNewName
364 {
365 }
366
367
368 //
369 //
370 //
371 - (NSString *) timestamp
372 {
373 return _timestamp;
374 }
375
376
377 //
378 // From RFC1939:
379 //
380 // Responses in the POP3 consist of a status indicator and a keyword
381 // possibly followed by additional information. All responses are
382 // terminated by a CRLF pair. Responses may be up to 512 characters
383 // long, including the terminating CRLF. There are currently two status
384 // indicators: positive ("+OK") and negative ("-ERR"). Servers MUST
385 // send the "+OK" and "-ERR" in upper case.
386 //
387 // Responses to certain commands are multi-line. In these cases, which
388 // are clearly indicated below, after sending the first line of the
389 // response and a CRLF, any additional lines are sent, each terminated
390 // by a CRLF pair. When all lines of the response have been sent, a
391 // final line is sent, consisting of a termination octet (decimal code
392 // 046, ".") and a CRLF pair. If any line of the multi-line response
393 // begins with the termination octet, the line is "byte-stuffed" by
394 // pre-pending the termination octet to that line of the response.
395 // Hence a multi-line response is terminated with the five octets
396 // "CRLF.CRLF". When examining a multi-line response, the client checks
397 // to see if the line begins with the termination octet. If so and if
398 // octets other than CRLF follow, the first octet of the line (the
399 // termination octet) is stripped away. If so and if CRLF immediately
400 // follows the termination character, then the response from the POP
401 // server is ended and the line containing ".CRLF" is not considered
402 // part of the multi-line response.
403 //
404 - (void) updateRead
405 {
406 id aData;
407
408 char *buf;
409 NSUInteger count;
410
411 [super updateRead];
412
413 while ((aData = split_lines(_rbuf)))
414 {
415 buf = (char *)[aData bytes];
416 count = [aData length];
417 [_responsesFromServer addObject: aData];
418
419 if (count)
420 {
421 switch (*buf)
422 {
423 case '.':
424 //
425 // We verify if we must strip the termination octet
426 //
427 if (count > 1)
428 {
429 aData = [NSMutableData dataWithData: aData];
430 buf = [aData mutableBytes];
431 memmove(buf, buf+1, count-2);
432 [aData setLength: count-2];
433 }
434 else
435 {
436 // We are done receiving multi-line response and
437 // we are ready to parse the received bytes. Before
438 // parsing all the received bytes, we remove the
439 // last line added since it corresponds to our
440 // multi-line response terminator.
441 [_responsesFromServer removeLastObject];
442 [self _parseServerOutput];
443 return;
444 }
445 break;
446
447 case '+':
448 //
449 // There's no real way in POP3 to know if we are currently
450 // reading a multi-line response. We assume we are NOT for
451 // some commands.
452 //
453 if (_lastCommand != POP3_CAPA &&
454 _lastCommand != POP3_LIST &&
455 _lastCommand != POP3_TOP &&
456 _lastCommand != POP3_RETR &&
457 _lastCommand != POP3_RETR_AND_INITIALIZE &&
458 _lastCommand != POP3_UIDL &&
459 (count > 2 && strncmp("+OK", buf, 3) == 0))
460 {
461 [self _parseServerOutput];
462 return;
463 }
464 break;
465
466 case '-':
467 if (_lastCommand != POP3_TOP &&
468 _lastCommand != POP3_RETR &&
469 _lastCommand != POP3_RETR_AND_INITIALIZE &&
470 (count > 3 && strncmp("-ERR", buf, 4) == 0))
471 {
472 [self _parseServerOutput];
473 return;
474 }
475 break;
476 }
477 }
478 }
479 }
480
481 //
482 // From RFC1939:
483 //
484 // Commands in the POP3 consist of a case-insensitive keyword, possibly
485 // followed by one or more arguments. All commands are terminated by a
486 // CRLF pair. Keywords and arguments consist of printable ASCII
487 // characters. Keywords and arguments are each separated by a single
488 // SPACE character. Keywords are three or four characters long. Each
489 // argument may be up to 40 characters long.
490 //
491 - (void) sendCommand: (POP3Command) theCommand arguments: (NSString *) theFormat, ...
492 {
493 CWPOP3QueueObject *aQueueObject;
494
495 //NSLog(@"sendCommand invoked, cmd = %i", theCommand);
496
497 if (theCommand == POP3_EMPTY_QUEUE)
498 {
499 if ([_queue count])
500 {
501 // We dequeue the first inserted command from the queue.
502 aQueueObject = [_queue lastObject];
503 }
504 else
505 {
506 // The queue is empty, we have nothing more to do...
507 return;
508 }
509 }
510 else
511 {
512 NSString *aString;
513 va_list args;
514
515 va_start(args, theFormat);
516
517 aString = [[NSString alloc] initWithFormat: theFormat arguments: args];
518
519 aQueueObject = [[CWPOP3QueueObject alloc] initWithCommand: theCommand arguments: aString];
520 RELEASE(aString);
521
522 [_queue insertObject: aQueueObject atIndex: 0];
523 RELEASE(aQueueObject);
524
525 //NSLog(@"queue size = %d:", [_queue count]);
526
527 // If we had queued commands, we return since we'll eventually
528 // dequeue them one by one. Otherwise, we run it immediately.
529 if ([_queue count] > 1)
530 {
531 //NSLog(@"QUEUED |%@|", aString);
532 return;
533 }
534 }
535
536 //NSLog(@"Sending |%@|", aQueueObject->arguments);
537 _lastCommand = aQueueObject->command;
538
539 // We verify if we had queued an indicator to tell us that we were done
540 // wrt expunging the POP3 folder.
541 if (_lastCommand == POP3_EXPUNGE_COMPLETED)
542 {
543 [_queue removeObject: [_queue lastObject]];
544
545 POST_NOTIFICATION(PantomimeFolderExpungeCompleted, self, [NSDictionary dictionaryWithObject: _folder forKey: @"Folder"]);
546 PERFORM_SELECTOR_2(_delegate, @selector(folderExpungeCompleted:), PantomimeFolderExpungeCompleted, _folder, @"Folder");
547 return;
548 }
549
550 // We send the command to the POP3 server.
551 [self writeData: [aQueueObject->arguments dataUsingEncoding: defaultCStringEncoding]];
552 [self writeData: CRLF];
553 }
554
555 //
556 //
557 //
558 - (void) startTLS
559 {
560 [self sendCommand: POP3_STLS arguments: @"STLS"];
561 }
562
563 @end
564
565
566 //
567 // Private methods
568 //
569 @implementation CWPOP3Store (Private)
570
571 - (void) _parseAPOP
572 {
573 NSData *aData;
574
575 aData = [_responsesFromServer lastObject];
576
577 if ([aData hasCPrefix: "+OK"])
578 {
579 AUTHENTICATION_COMPLETED(_delegate, @"APOP");
580 }
581 else
582 {
583 AUTHENTICATION_FAILED(_delegate, @"APOP");
584 }
585 }
586
587
588 //
589 //
590 //
591 - (void) _parseAUTHORIZATION
592 {
593 NSData *aData;
594
595 aData = [_responsesFromServer lastObject];
596
597 if ([aData hasCPrefix: "+OK"])
598 {
599 NSRange range1, range2;
600
601 range1 = [aData rangeOfCString: "<"];
602 range2 = [aData rangeOfCString: ">"];
603
604 if (range1.length && range2.length)
605 {
606 ASSIGN(_timestamp, [[aData subdataWithRange: NSMakeRange(range1.location,range2.location-range1.location+1)] asciiString]);
607 }
608
609 [self sendCommand: POP3_CAPA arguments: @"CAPA"];
610 }
611 else
612 {
613 // FIXME
614 // connectionLost? or should we call [self close]?
615 }
616 }
617
618
619 //
620 // See RFC2449 for details.
621 //
622 - (void) _parseCAPA
623 {
624 NSData *aData;
625 NSUInteger i, count;
626
627 count = [_responsesFromServer count];
628
629 for (i = 1; i < count; i++)
630 {
631 aData = [_responsesFromServer objectAtIndex: i];
632 [_capabilities addObject: AUTORELEASE([[NSString alloc] initWithData: aData encoding: defaultCStringEncoding])];
633 }
634
635 POST_NOTIFICATION(PantomimeServiceInitialized, self, nil);
636 PERFORM_SELECTOR_1(_delegate, @selector(serviceInitialized:), PantomimeServiceInitialized);
637 }
638
639
640 //
641 // From RFC1939:
642 //
643 // If no argument was given and the POP3 server issues a
644 // positive response, then the response given is multi-line.
645 // After the initial +OK, for each message in the maildrop,
646 // the POP3 server responds with a line containing
647 // information for that message. This line is also called a
648 // "scan listing" for that message. If there are no
649 // messages in the maildrop, then the POP3 server responds
650 // with no scan listings--it issues a positive response
651 // followed by a line containing a termination octet and a
652 // CRLF pair.
653 //
654 - (void) _parseLIST
655 {
656 CWPOP3Message *aMessage;
657 NSUInteger i, idx, count;
658 unsigned long size;
659
660 count = [_responsesFromServer count];
661
662 for (i = 1; i < count; i++)
663 {
664 sscanf([[_responsesFromServer objectAtIndex: i] cString], "%lu %lu", (unsigned long*)&idx, &size);
665
666 aMessage = [[_folder messages] objectAtIndex: (idx-1)];
667 [aMessage setSize: size];
668 [aMessage setMessageNumber: i];
669 }
670
671 [self sendCommand: POP3_UIDL arguments: @"UIDL"];
672 }
673
674
675 //
676 //
677 //
678 - (void) _parseNOOP
679 {
680 // Do what?
681 }
682
683
684 //
685 //
686 //
687 - (void) _parsePASS
688 {
689 NSData *aData;
690
691 aData = [_responsesFromServer lastObject];
692
693 if ([aData hasCPrefix: "+OK"])
694 {
695 AUTHENTICATION_COMPLETED(_delegate, @"");
696 }
697 else
698 {
699 AUTHENTICATION_FAILED(_delegate, @"");
700 }
701 }
702
703
704 //
705 //
706 //
707 - (void) _parseQUIT
708 {
709 // We don't need to do anything special here.
710 [super close];
711 }
712
713
714 //
715 //
716 //
717 - (void) _parseRETR
718 {
719 NSData *aData;
720
721 aData = [_responsesFromServer objectAtIndex: 0];
722
723 if ([aData hasCPrefix: "+OK"])
724 {
725 NSMutableData *aMutableData;
726 CWPOP3Message *aMessage;
727 NSUInteger count, i;
728 unsigned idx;
729
730 // We get the idx of the message we are parsing...
731 sscanf([((CWPOP3QueueObject *)[_queue lastObject])->arguments cString], "RETR %u", &idx);
732
733 aMessage = (CWPOP3Message *)[_folder messageAtIndex: (idx-1)];
734 aMutableData = [[NSMutableData alloc] initWithCapacity: [aMessage size]];
735 count = [_responsesFromServer count];
736
737 for (i = 1; i < count; i++)
738 {
739 [aMutableData appendData: [_responsesFromServer objectAtIndex: i]];
740
741 // We do NOT append the last \n.
742 if (i < count-1)
743 {
744 [aMutableData appendBytes: "\n" length: 1];
745 }
746 }
747
748 [aMessage setRawSource: aMutableData];
749
750 if (_lastCommand == POP3_RETR_AND_INITIALIZE)
751 {
752 NSRange aRange;
753
754 aRange = [aMutableData rangeOfCString: "\n\n"];
755
756 if (aRange.length == 0)
757 {
758 [aMessage setInitialized: NO];
759 }
760 else
761 {
762 [aMessage setHeadersFromData: [aMutableData subdataWithRange: NSMakeRange(0,aRange.location)]];
763 [CWMIMEUtility setContentFromRawSource:
764 [aMutableData subdataWithRange:
765 NSMakeRange(aRange.location + 2, [aMutableData length]-(aRange.location+2))]
766 inPart: aMessage];
767 [aMessage setInitialized: YES];
768 }
769 }
770
771 [aMessage setSize: [aMutableData length]];
772 RELEASE(aMutableData);
773
774 // Do that in parse top also?
775 if ([_folder cacheManager])
776 {
777 cache_record r;
778
779 r.date = (uint32_t)round([[aMessage receivedDate] timeIntervalSince1970]);
780 r.pop3_uid = [aMessage UID];
781 [(CWPOP3CacheManager *)[_folder cacheManager] writeRecord: &r];
782 }
783
784 POST_NOTIFICATION(PantomimeMessagePrefetchCompleted, self, [NSDictionary dictionaryWithObject: aMessage forKey: @"Message"]);
785 PERFORM_SELECTOR_2(_delegate, @selector(messagePrefetchCompleted:), PantomimeMessagePrefetchCompleted, aMessage, @"Message");
786 }
787 }
788
789
790 //
791 //
792 //
793 - (void) _parseSTAT
794 {
795 NSData *aData;
796
797 aData = [_responsesFromServer lastObject];
798
799 if ([aData hasCPrefix: "+OK"])
800 {
801 CWPOP3Message *aMessage;
802 int count;
803 long size;
804
805 sscanf([aData cString], "+OK %i %li", &count, &size);
806 //NSLog(@"count = %d size = %li", count, size);
807
808 while (count--)
809 {
810 aMessage = [[CWPOP3Message alloc] init];
811 [aMessage setFolder: _folder];
812 [[_folder messages] addObject: aMessage];
813 RELEASE(aMessage);
814 }
815
816 //NSLog(@"Folder count = %d", [_folder count]);
817
818 [self sendCommand: POP3_LIST arguments: @"LIST"];
819 }
820 else
821 {
822 // FIXME: Do what in case STAT failed?
823 }
824 }
825
826 //
827 //
828 //
829 - (void) _parseSTLS
830 {
831 NSData *aData;
832
833 aData = [_responsesFromServer lastObject];
834
835 if ([aData hasCPrefix: "+OK"])
836 {
837 [(CWTCPConnection *)_connection startSSL];
838
839 POST_NOTIFICATION(PantomimeServiceInitialized, self, nil);
840 PERFORM_SELECTOR_1(_delegate, @selector(serviceInitialized:), PantomimeServiceInitialized);
841 }
842 else
843 {
844 // FIXME: Handle the case where STLS failed
845 }
846 }
847
848 //
849 // This method is very similar to the _parseRETR method.
850 // We don't touch the message's size (set in _parseLIST)
851 // since we must know how much we had to read,
852 //
853 - (void) _parseTOP
854 {
855 NSData *aData;
856
857 aData = [_responsesFromServer objectAtIndex: 0];
858
859 if ([aData hasCPrefix: "+OK"])
860 {
861 NSMutableData *aMutableData;
862 CWPOP3Message *aMessage;
863 NSUInteger i, count;
864 int idx, num;
865
866 // We get the idx of the message we are parsing...
867 sscanf([((CWPOP3QueueObject *)[_queue lastObject])->arguments cString], "TOP %d %d", &idx, &num);
868
869 //NSLog(@"PARTIALLY DECODING MESSAGE no. %d - number of lines %d!", idx, num);
870
871 aMessage = (CWPOP3Message *)[_folder messageAtIndex: (idx-1)];
872 aMutableData = [[NSMutableData alloc] init];
873 count = [_responsesFromServer count];
874
875 for (i = 1; i < count; i++)
876 {
877 [aMutableData appendData: [_responsesFromServer objectAtIndex: i]];
878 [aMutableData appendBytes: "\n" length: 1];
879 }
880
881 [aMessage setRawSource: aMutableData];
882 RELEASE(aMutableData);
883
884 POST_NOTIFICATION(PantomimeMessagePrefetchCompleted, self, [NSDictionary dictionaryWithObject: aMessage forKey: @"Message"]);
885 PERFORM_SELECTOR_2(_delegate, @selector(messagePrefetchCompleted:), PantomimeMessagePrefetchCompleted, aMessage, @"Message");
886 }
887 }
888
889
890 //
891 //
892 //
893 - (void) _parseUIDL
894 {
895 NSUInteger i, idx, count;
896 char buf[71];
897
898 count = [_responsesFromServer count];
899
900 for (i = 1; i < count; i++)
901 {
902 memset(buf, 0, 71);
903 sscanf([[_responsesFromServer objectAtIndex: i] cString],"%lu %s", (unsigned long*)&idx, buf);
904 [[[_folder messages] objectAtIndex: (idx-1)] setUID: [NSString stringWithCString: buf]];
905 }
906
907 POST_NOTIFICATION(PantomimeFolderPrefetchCompleted, self, [NSDictionary dictionaryWithObject: _folder forKey: @"Folder"]);
908 PERFORM_SELECTOR_2(_delegate, @selector(folderPrefetchCompleted:), PantomimeFolderPrefetchCompleted, _folder, @"Folder");
909 }
910
911
912 //
913 // From RFC1939:
914 //
915 // To authenticate using the USER and PASS command
916 // combination, the client must first issue the USER
917 // command. If the POP3 server responds with a positive
918 // status indicator ("+OK"), then the client may issue
919 // either the PASS command to complete the authentication,
920 // or the QUIT command to terminate the POP3 session. If
921 // the POP3 server responds with a negative status indicator
922 // ("-ERR") to the USER command, then the client may either
923 // issue a new authentication command or may issue the QUIT
924 // command.
925 //
926 - (void) _parseUSER
927 {
928 NSData *aData;
929
930 aData = [_responsesFromServer lastObject];
931
932 if ([aData hasCPrefix: "+OK"])
933 {
934 [self sendCommand: POP3_PASS arguments: @"PASS %@", _password];
935 }
936 else
937 {
938 // We must terminate the POP3 session.
939 [self close];
940 }
941 }
942
943
944 //
945 //
946 //
947 - (void) _parseServerOutput
948 {
949 if (![_responsesFromServer count])
950 {
951 return;
952 }
953
954 //NSLog(@"In _parseServerOutput...%d", _lastCommand);
955
956 switch (_lastCommand)
957 {
958 case POP3_APOP:
959 [self _parseAPOP];
960 break;
961
962 case POP3_AUTHORIZATION:
963 [self _parseAUTHORIZATION];
964 break;
965
966 case POP3_CAPA:
967 [self _parseCAPA];
968 break;
969
970 case POP3_LIST:
971 [self _parseLIST];
972 break;
973
974 case POP3_NOOP:
975 [self _parseNOOP];
976 break;
977
978 case POP3_PASS:
979 [self _parsePASS];
980 break;
981
982 case POP3_QUIT:
983 [self _parseQUIT];
984 break;
985
986 case POP3_RETR:
987 case POP3_RETR_AND_INITIALIZE:
988 [self _parseRETR];
989 break;
990
991 case POP3_STAT:
992 [self _parseSTAT];
993 break;
994
995 case POP3_STLS:
996 [self _parseSTLS];
997 break;
998
999 case POP3_TOP:
1000 [self _parseTOP];
1001 break;
1002
1003 case POP3_UIDL:
1004 [self _parseUIDL];
1005 break;
1006
1007 case POP3_USER:
1008 [self _parseUSER];
1009 break;
1010
1011 default:
1012 //NSLog(@"UNKNOWN POP3 LAST COMMAND %d", _lastCommand);
1013 break;
1014 // FIXME
1015 }
1016
1017 // We are done parsing this entry...
1018 [_responsesFromServer removeAllObjects];
1019
1020
1021 // We remove the last object of the queue, if needed.
1022 //NSLog(@"Removing oldest command from queue (%d)...", [_queue count]);
1023 if ([_queue count])
1024 {
1025 [_queue removeLastObject];
1026 [self sendCommand: POP3_EMPTY_QUEUE arguments: @""];
1027 }
1028 }
1029
1030 @end