Debian Codegraph @

Framework/Pantomime/CWSMTP.m

raw
1 /*
2 ** CWSMTP.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2020 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Lesser General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2.1 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Lesser General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #import <Pantomime/CWSMTP.h>
24
25 #import <Pantomime/CWConnection.h>
26 #import <Pantomime/CWConstants.h>
27 #import <Pantomime/CWInternetAddress.h>
28 #import <Pantomime/CWMD5.h>
29 #import <Pantomime/CWMessage.h>
30 #import <Pantomime/CWTCPConnection.h>
31 #import <Pantomime/NSData+Extensions.h>
32
33 #import <Foundation/NSEnumerator.h>
34 #import <Foundation/NSNotification.h>
35
36 //
37 // Some static variables used to enhance the performance.
38 //
39 static NSStringEncoding defaultCStringEncoding;
40 static NSData *CRLF;
41
42
43 //
44 // This function returns the next recipient from the array depending
45 // if the message is redirected or not.
46 //
47 static inline CWInternetAddress *next_recipient(NSMutableArray *theRecipients, BOOL aBOOL)
48 {
49 CWInternetAddress *theAddress;
50 NSUInteger i, count;
51
52 count = [theRecipients count];
53
54 for (i = 0; i < count; i++)
55 {
56 theAddress = [theRecipients objectAtIndex: i];
57
58 if (aBOOL)
59 {
60 if ([theAddress type] > 3)
61 {
62 return theAddress;
63 }
64 }
65 else
66 {
67 if ([theAddress type] < 4)
68 {
69 return theAddress;
70 }
71 }
72 }
73
74 return nil;
75 }
76
77 //
78 //
79 //
80 @interface CWSMTPQueueObject : NSObject
81 {
82 @public
83 SMTPCommand command;
84 NSString *arguments;
85 }
86 - (id) initWithCommand: (SMTPCommand) theCommand
87 arguments: (NSString *) theArguments;
88 @end
89
90 @implementation CWSMTPQueueObject
91
92 - (id) initWithCommand: (SMTPCommand) theCommand
93 arguments: (NSString *) theArguments
94 {
95 self = [super init];
96 if (self)
97 {
98 command = theCommand;
99 ASSIGN(arguments, theArguments);
100 }
101
102 return self;
103 }
104
105 - (void) dealloc
106 {
107 RELEASE(arguments);
108 [super dealloc];
109 }
110 @end
111
112
113 //
114 // Private SMTP methods
115 //
116 @interface CWSMTP (Private)
117
118 - (void) _fail;
119 - (void) _parseAUTH_CRAM_MD5;
120 - (void) _parseAUTH_LOGIN;
121 - (void) _parseAUTH_LOGIN_CHALLENGE;
122 - (void) _parseAUTH_PLAIN;
123 - (void) _parseAUTHORIZATION;
124 - (void) _parseDATA;
125 - (void) _parseEHLO;
126 - (void) _parseHELO;
127 - (void) _parseMAIL;
128 - (void) _parseNOOP;
129 - (void) _parseQUIT;
130 - (void) _parseRCPT;
131 - (void) _parseRSET;
132 - (void) _parseServerOutput;
133
134 @end
135
136
137 //
138 //
139 //
140 @implementation CWSMTP
141
142 + (void) initialize
143 {
144 defaultCStringEncoding = [NSString defaultCStringEncoding];
145 CRLF = [[NSData alloc] initWithBytes: "\r\n" length: 2];
146 }
147
148
149 //
150 // initializers
151 //
152 - (id) initWithName: (NSString *) theName
153 port: (unsigned int) thePort
154 {
155 self = [super initWithName: theName port: thePort];
156 if (self)
157 {
158 _sent_recipients = nil;
159 _recipients = nil;
160 _message = nil;
161 _data = nil;
162 _max_size = 0;
163
164 _lastCommand = SMTP_AUTHORIZATION;
165
166 // We queue our first "command".
167 [_queue addObject: AUTORELEASE([[CWSMTPQueueObject alloc] initWithCommand: _lastCommand arguments: @""])];
168 }
169
170 return self;
171 }
172
173
174 //
175 //
176 //
177 - (void) dealloc
178 {
179 //NSLog(@"SMTP: -dealloc");
180 RELEASE(_message);
181 RELEASE(_data);
182 RELEASE(_recipients);
183 RELEASE(_sent_recipients);
184
185 [super dealloc];
186 }
187
188
189 //
190 // This method returns the last response obtained from the SMTP
191 // server. If the last command issued a multiline response, it'll return the
192 // last text response and none of the previous ones.
193 //
194 - (NSData *) lastResponse
195 {
196 return [_responsesFromServer lastObject];
197 }
198
199
200 //
201 // Same as -lastResponse except it does return only the response code.
202 //
203 - (int) lastResponseCode
204 {
205 if ([_responsesFromServer count] > 0)
206 {
207 return atoi([[[_responsesFromServer lastObject] subdataToIndex: 3] cString]);
208 }
209
210 return 0;
211 }
212
213
214 //
215 // This method is used to authenticate ourself to the SMTP server.
216 //
217 - (void) authenticate: (NSString *) theUsername
218 password: (NSString *) thePassword
219 mechanism: (NSString *) theMechanism
220 {
221 ASSIGN(_username, theUsername);
222 ASSIGN(_password, thePassword);
223 ASSIGN(_mechanism, theMechanism);
224
225 if (!theMechanism)
226 {
227 AUTHENTICATION_FAILED(_delegate, @"");
228 }
229 else if ([theMechanism caseInsensitiveCompare: @"PLAIN"] == NSOrderedSame)
230 {
231 [self sendCommand: SMTP_AUTH_PLAIN arguments: @"AUTH PLAIN"];
232 }
233 else if ([theMechanism caseInsensitiveCompare: @"LOGIN"] == NSOrderedSame)
234 {
235 [self sendCommand: SMTP_AUTH_LOGIN arguments: @"AUTH LOGIN"];
236 }
237 else if ([theMechanism caseInsensitiveCompare: @"CRAM-MD5"] == NSOrderedSame)
238 {
239 [self sendCommand: SMTP_AUTH_CRAM_MD5 arguments: @"AUTH CRAM-MD5"];
240 }
241 else
242 {
243 // Unknown / Unsupported mechanism
244 AUTHENTICATION_FAILED(_delegate, theMechanism);
245 }
246 }
247
248
249 //
250 //
251 //
252 - (void) close
253 {
254 [self sendCommand: SMTP_QUIT arguments: @"QUIT"];
255 }
256
257
258 //
259 // This method sends a NOOP SMTP command.
260 //
261 - (void) noop
262 {
263 [self sendCommand: SMTP_NOOP arguments: @"NOOP"];
264 }
265
266
267 //
268 // This method sends a RSET SMTP command.
269 //
270 - (void) reset
271 {
272 [self sendCommand: SMTP_RSET arguments: @"RSET"];
273 }
274
275
276 //
277 // This method sends a SMTP command to the server.
278 //
279 // It automatically adds the trailing CRLF to every command.
280 //
281 // RFC2821:
282 //
283 // The SMTP commands define the mail transfer or the mail system
284 // function requested by the user. SMTP commands are character strings
285 // terminated by <CRLF>. The commands themselves are alphabetic
286 // characters terminated by <SP> if parameters follow and <CRLF>
287 // otherwise. (In the interest of improved interoperability, SMTP
288 // receivers are encouraged to tolerate trailing white space before the
289 // terminating <CRLF>.) The syntax of the local part of a mailbox must
290 // conform to receiver site conventions and the syntax specified in
291 // section 4.1.2. The SMTP commands are discussed below. The SMTP
292 // replies are discussed in section 4.2.
293 //
294 // The following list of commands is supported:
295 //
296 // - EHLO / HELO
297 // - MAIL
298 // - RCPT
299 // - DATA
300 // - RSET
301 // - QUIT
302 //
303 // Unimplemented commands:
304 //
305 // - VRFY
306 // - EXPN
307 // - HELP
308 // - NOOP
309 //
310 - (void) sendCommand: (SMTPCommand) theCommand arguments: (NSString *) theFormat, ...
311 {
312 CWSMTPQueueObject *aQueueObject;
313
314 if (theCommand == SMTP_EMPTY_QUEUE)
315 {
316 if ([_queue count])
317 {
318 // We dequeue the first inserted command from the queue.
319 aQueueObject = [_queue lastObject];
320 }
321 else
322 {
323 // The queue is empty, we have nothing more to do...
324 return;
325 }
326 }
327 else
328 {
329 NSString *aString;
330 va_list args;
331
332 va_start(args, theFormat);
333
334 aString = [[NSString alloc] initWithFormat: theFormat arguments: args];
335
336 aQueueObject = [[CWSMTPQueueObject alloc] initWithCommand: theCommand arguments: aString];
337 RELEASE(aString);
338
339 [_queue insertObject: aQueueObject atIndex: 0];
340 RELEASE(aQueueObject);
341
342 // If we had queued commands, we return since we'll eventually
343 // dequeue them one by one. Otherwise, we run it immediately.
344 if ([_queue count] > 1)
345 {
346 return;
347 }
348 }
349
350 _lastCommand = aQueueObject->command;
351
352 // We send the command to the POP3 server.
353 [self writeData: [aQueueObject->arguments dataUsingEncoding: defaultCStringEncoding]];
354 [self writeData: CRLF];
355 }
356
357
358 //
359 // To send a message, we need its data value and the recipients at least.
360 //
361 // Depending on what was specified using the "set" methods, we initialize
362 // what we really want and proceed to send the mail.
363 //
364 - (void) sendMessage
365 {
366 NSString *aString;
367
368 if (!_message && !_data)
369 {
370 [self _fail];
371 return;
372 }
373 if (!_recipients && _message)
374 {
375 ASSIGN(_recipients, [NSMutableArray arrayWithArray: [_message recipients]]);
376
377 if (!_data)
378 {
379 ASSIGN(_data, [_message dataValue]);
380 }
381 }
382 else if (!_recipients && _data)
383 {
384 CWMessage *aMessage;
385
386 aMessage = [[CWMessage alloc] initWithData: _data];
387 ASSIGN(_message, aMessage);
388 ASSIGN(_recipients, [NSMutableArray arrayWithArray: [aMessage recipients]]);
389
390 RELEASE(aMessage);
391 }
392
393 DESTROY(_sent_recipients);
394 _sent_recipients = [_recipients mutableCopy];
395
396 // We first verify if it's a redirected message
397 if ([_message resentFrom])
398 {
399 _redirected = YES;
400 aString = [[_message resentFrom] address];
401 }
402 else
403 {
404 _redirected = NO;
405 aString = [[_message from] address];
406 }
407
408 if (_max_size)
409 {
410 [self sendCommand: SMTP_MAIL arguments: @"MAIL FROM:<%@> SIZE=%d", aString, [_data length]];
411 }
412 else
413 {
414 [self sendCommand: SMTP_MAIL arguments: @"MAIL FROM:<%@>", aString];
415 }
416 }
417
418
419 //
420 //
421 //
422 - (void) setMessage: (CWMessage *) theMessage
423 {
424 DESTROY(_data);
425 ASSIGN(_message, theMessage);
426 }
427
428 - (CWMessage *) message
429 {
430 return _message;
431 }
432
433
434 //
435 //
436 //
437 - (void) setMessageData: (NSData *) theData
438 {
439 DESTROY(_message);
440 ASSIGN(_data, theData);
441 }
442
443 - (NSData *) messageData
444 {
445 return _data;
446 }
447
448
449 //
450 //
451 //
452 - (void) setRecipients: (NSArray *) theRecipients
453 {
454 DESTROY(_recipients);
455
456 if (theRecipients)
457 {
458 ASSIGN(_recipients, [NSMutableArray arrayWithArray: theRecipients]);
459 }
460 }
461
462 - (NSArray *) recipients
463 {
464 return _recipients;
465 }
466
467
468 //
469 // This methods reads everything the server sends. Once it judge it has
470 // read a full response, it calls _parseServerOutput in order to react
471 // to what we've just received from the server.
472 //
473 //
474 // RFC2821 rationale:
475 //
476 // An SMTP reply consists of a three digit number (transmitted as three
477 // numeric characters) followed by some text unless specified otherwise
478 // in this document. The number is for use by automata to determine
479 // what state to enter next; the text is for the human user. The three
480 // digits contain enough encoded information that the SMTP client need
481 // not examine the text and may either discard it or pass it on to the
482 // user, as appropriate. Exceptions are as noted elsewhere in this
483 // document. In particular, the 220, 221, 251, 421, and 551 reply codes
484 // are associated with message text that must be parsed and interpreted
485 // by machines
486 //
487 // (...)
488 //
489 // The format for multiline replies requires that every line, except the
490 // last, begin with the reply code, followed immediately by a hyphen,
491 // "-" (also known as minus), followed by text. The last line will
492 // begin with the reply code, followed immediately by <SP>, optionally
493 // some text, and <CRLF>. As noted above, servers SHOULD send the <SP>
494 // if subsequent text is not sent, but clients MUST be prepared for it
495 // to be omitted.
496 //
497 // For example:
498 //
499 // 123-First line
500 // 123-Second line
501 // 123-234 text beginning with numbers
502 // 123 The last line
503 //
504 //
505 // (...)
506 //
507 // ... Only the EHLO, EXPN, and HELP
508 // commands are expected to result in multiline replies in normal
509 // circumstances, however, multiline replies are allowed for any
510 // command.
511 //
512 - (void) updateRead
513 {
514 NSData *aData;
515 char *buf;
516 NSUInteger count;
517
518 //NSLog(@"IN UPDATE READ");
519
520 [super updateRead];
521
522 while ((aData = split_lines(_rbuf)))
523 {
524 [_responsesFromServer addObject: aData];
525
526 buf = (char *)[aData bytes];
527 count = [aData length];
528
529 // If we got only a response code OR if we're done reading
530 // a multiline reply, we parse the output!
531 if (count == 3 || (count > 3 && (*(buf+3) != '-')))
532 {
533 [self _parseServerOutput];
534 }
535 }
536 }
537
538 //
539 //
540 //
541 - (void) startTLS
542 {
543 [self sendCommand: SMTP_STARTTLS arguments: @"STARTTLS"];
544 }
545
546 @end
547
548
549 //
550 // Private methods
551 //
552 @implementation CWSMTP (Private)
553 - (void) _fail
554 {
555 if (_message)
556 POST_NOTIFICATION(PantomimeMessageNotSent, self,
557 [NSDictionary dictionaryWithObject: _message forKey: @"Message"]);
558 else
559 POST_NOTIFICATION(PantomimeMessageNotSent, self,
560 [NSDictionary dictionaryWithObject: AUTORELEASE([CWMessage new]) forKey: @"Message"]);
561 PERFORM_SELECTOR_2(_delegate, @selector(messageNotSent:), PantomimeMessageNotSent, _message, @"Message");
562 }
563
564 - (void) _parseAUTH_CRAM_MD5
565 {
566 NSData *aData;
567
568 aData = [_responsesFromServer lastObject];
569
570 if ([aData hasCPrefix: "334"])
571 {
572 NSString *aString;
573 CWMD5 *aMD5;
574
575 // We trim the "334 ", decode the data using base64 and we keep the challenge phrase
576 aData = [[aData subdataFromIndex: 4] decodeBase64];
577 aMD5 = [[CWMD5 alloc] initWithData: aData];
578 [aMD5 computeDigest];
579
580 aString = [NSString stringWithFormat: @"%@ %@", _username, [aMD5 hmacAsStringUsingPassword: _password]];
581 [self writeData: [[aString dataUsingEncoding: defaultCStringEncoding] encodeBase64WithLineLength: 0]];
582 [self writeData: CRLF];
583 RELEASE(aMD5);
584 }
585 else if ([aData hasCPrefix: "235"])
586 {
587 AUTHENTICATION_COMPLETED(_delegate, @"CRAM-MD5");
588 }
589 else
590 {
591 AUTHENTICATION_FAILED(_delegate, @"CRAM-MD5");
592 }
593 }
594
595
596 //
597 //
598 //
599 - (void) _parseAUTH_LOGIN
600 {
601 NSData *aData;
602
603 aData = [_responsesFromServer lastObject];
604
605 if ([aData hasCPrefix: "334"])
606 {
607 NSString *aString;
608
609 aString = [[NSString alloc] initWithData: [[_username dataUsingEncoding: defaultCStringEncoding] encodeBase64WithLineLength: 0]
610 encoding: defaultCStringEncoding];
611 [self sendCommand: SMTP_AUTH_LOGIN_CHALLENGE arguments: aString];
612 RELEASE(aString);
613 }
614 else
615 {
616 AUTHENTICATION_FAILED(_delegate, @"LOGIN");
617 }
618 }
619
620
621 //
622 // FIXME, rename if needed.
623 //
624 - (void) _parseAUTH_LOGIN_CHALLENGE
625 {
626 NSData *aData;
627
628 aData = [_responsesFromServer lastObject];
629
630 if ([aData hasCPrefix: "334"])
631 {
632 NSString *aString;
633
634 aString = [[NSString alloc] initWithData: [[_password dataUsingEncoding: defaultCStringEncoding] encodeBase64WithLineLength: 0]
635 encoding: defaultCStringEncoding];
636
637 [self sendCommand: SMTP_AUTH_LOGIN_CHALLENGE arguments: aString];
638 RELEASE(aString);
639 }
640 else if ([aData hasCPrefix: "235"])
641 {
642 AUTHENTICATION_COMPLETED(_delegate, @"LOGIN");
643 }
644 else
645 {
646 AUTHENTICATION_FAILED(_delegate, @"LOGIN");
647 }
648 }
649
650
651 //
652 //
653 //
654 - (void) _parseAUTH_PLAIN
655 {
656 NSData *aData;
657
658 aData = [_responsesFromServer lastObject];
659
660 if ([aData hasCPrefix: "334"])
661 {
662 NSMutableData *aMutableData;
663 NSUInteger len_username, len_password;
664
665 len_username = [_username length];
666
667 if (!_password)
668 {
669 len_password = 0;
670 }
671 else
672 {
673 len_password = [_password length];
674 }
675
676 // We create our phrase
677 aMutableData = [NSMutableData dataWithLength: (len_username + len_password + 2)];
678
679 [aMutableData replaceBytesInRange: NSMakeRange(1,len_username)
680 withBytes: [[_username dataUsingEncoding: defaultCStringEncoding] bytes]];
681
682
683 [aMutableData replaceBytesInRange: NSMakeRange(2 + len_username, len_password)
684 withBytes: [[_password dataUsingEncoding: defaultCStringEncoding] bytes]];
685
686 [self writeData: [aMutableData encodeBase64WithLineLength: 0]];
687 [self writeData: CRLF];
688 }
689 else if ([aData hasCPrefix: "235"])
690 {
691 AUTHENTICATION_COMPLETED(_delegate, @"PLAIN");
692 }
693 else
694 {
695 AUTHENTICATION_FAILED(_delegate, @"PLAIN");
696 }
697 }
698
699 //
700 //
701 //
702 - (void) _parseAUTHORIZATION
703 {
704 NSData *aData;
705
706 aData = [_responsesFromServer lastObject];
707
708 // 220 <domain> Service ready
709 if ([aData hasCPrefix: "220"])
710 {
711 [self sendCommand: SMTP_EHLO arguments: @"EHLO localhost.localdomain"];
712 }
713 else
714 {
715 // Handle the fact when a server is loaded and can't handle our requests
716 // right away.
717 #warning FIXME
718 }
719 }
720
721 //
722 //
723 //
724 - (void) _parseDATA
725 {
726 NSData *aData;
727
728 aData = [_responsesFromServer lastObject];
729
730 // If we can proceed to write the message's data, let's do so.
731 if ([aData hasCPrefix: "354"])
732 {
733 NSMutableData *aMutableData;
734 NSRange r1, r2;
735
736 // We first replace all occurences of LF by CRLF in the Message's data.
737 //
738 aMutableData = [[NSMutableData dataWithData: _data] replaceLFWithCRLF];
739
740 //
741 // According to RFC 2821 section 4.5.2, we must check for the character
742 // sequence "<CRLF>.<CRLF>"; any occurrence have its period duplicated
743 // to avoid data transparency.
744 //
745 r1 = [aMutableData rangeOfCString: "\r\n."];
746
747 while (r1.location != NSNotFound)
748 {
749 [aMutableData replaceBytesInRange: r1 withBytes: "\r\n.." length: 4];
750
751 r1 = [aMutableData rangeOfCString: "\r\n."
752 options: 0
753 range: NSMakeRange(NSMaxRange(r1)+1, [aMutableData length]-NSMaxRange(r1)-1)];
754 }
755
756 //
757 // We now look for the Bcc: header. If it is present, we remove it.
758 // Some servers, like qmail, do not remove it automatically.
759 //
760 r1 = [aMutableData rangeOfCString: "\r\n\r\n"];
761 r1 = [aMutableData rangeOfCString: "\r\nBcc: "
762 options: 0
763 range: NSMakeRange(0,r1.location-1)];
764
765 if (r1.location != NSNotFound)
766 {
767 // We search for the first \r\n AFTER the Bcc: header and
768 // replace the whole thing with \r\n.
769 r2 = [aMutableData rangeOfCString: "\r\n"
770 options: 0
771 range: NSMakeRange(NSMaxRange(r1)+1,[aMutableData length]-NSMaxRange(r1)-1)];
772 [aMutableData replaceBytesInRange: NSMakeRange(r1.location, NSMaxRange(r2)-r1.location)
773 withBytes: "\r\n"
774 length: 2];
775 }
776
777 [self writeData: aMutableData];
778 [self writeData: [NSData dataWithBytes: "\r\n.\r\n" length: 5]];
779 }
780 else if ([aData hasCPrefix: "250"])
781 {
782 // The data we wrote in the previous call was sucessfully written.
783 // We inform the delegate that the mail was sucessfully sent.
784 POST_NOTIFICATION(PantomimeMessageSent, self, [NSDictionary dictionaryWithObject: _message forKey: @"Message"]);
785 PERFORM_SELECTOR_2(_delegate, @selector(messageSent:), PantomimeMessageSent, _message, @"Message");
786 }
787 else
788 {
789 [self _fail];
790 }
791 }
792
793
794 //
795 //
796 //
797 - (void) _parseEHLO
798 {
799 NSData *aData;
800 NSUInteger i, count;
801
802 count = [_responsesFromServer count];
803
804 for (i = 0; i < count; i++)
805 {
806 aData = [_responsesFromServer objectAtIndex: i];
807
808 if ([aData hasCPrefix: "250"])
809 {
810 // We parse the SMTP service extensions. For now, we support the SIZE
811 // and the AUTH extensions. We ignore the rest.
812 aData = [aData subdataFromIndex: 4];
813
814 // We add it to our capabilities
815 [_capabilities addObject: AUTORELEASE([[NSString alloc] initWithData: aData encoding: defaultCStringEncoding])];
816
817 // Example of responses:
818 //
819 // AUTH LOGIN
820 // AUTH=PLAIN CRAM-MD5 DIGEST-MD5
821 //
822 if ([aData hasCPrefix: "AUTH"])
823 {
824 NSEnumerator *theEnumerator;
825 id aString;
826
827 // We chomp the "AUTH " or "AUTH=" part and we decode our
828 // supported mechanisms.
829 theEnumerator = [[[aData subdataFromIndex: 5] componentsSeparatedByCString: " "] objectEnumerator];
830
831 while ((aString = [theEnumerator nextObject]))
832 {
833 aString = [aString asciiString];
834
835 if (![_supportedMechanisms containsObject: aString])
836 {
837 [_supportedMechanisms addObject: aString];
838 }
839 }
840 }
841 //
842 // SIZE size-param
843 // size-param ::= [1*DIGIT]
844 //
845 // See RFC1870 for detailed information.
846 //
847 else if ([aData hasCPrefix: "SIZE"])
848 {
849 NSRange aRange;
850
851 // We must be careful here. Some broken servers will send only
852 // 250-SIZE
853 // and we don't want to parse an inexistant value.
854 aRange = [aData rangeOfCString: " "];
855
856 if (aRange.length)
857 {
858 _max_size = atoi([[aData subdataFromIndex: aRange.location+1] cString]);
859 }
860 }
861 }
862 else
863 {
864 // The server doesn't handle EHLO. We send it
865 // a HELO greeting instead.
866 [self sendCommand: SMTP_HELO arguments: @"HELO localhost.localdomain"];
867 break;
868 }
869 }
870
871
872 #warning FIXME - Inform the delegate if it is ready or not, especially if EHLO failed
873 POST_NOTIFICATION(PantomimeServiceInitialized, self, nil);
874 PERFORM_SELECTOR_1(_delegate, @selector(serviceInitialized:), PantomimeServiceInitialized);
875 }
876
877
878 //
879 //
880 //
881 - (void) _parseHELO
882 {
883 // FIXME - Implement. + inform the delegate if it's ready or not.
884 }
885
886
887 //
888 // This method parses the result received from the server
889 // after issuing a "MAIL FROM: <>" command.
890 //
891 // If the result is successful, we proceed by sending the first RCPT.
892 //
893 - (void) _parseMAIL
894 {
895 NSData *aData;
896
897 aData = [_responsesFromServer lastObject];
898
899 if ([aData hasCPrefix: "250"])
900 {
901 // We write the first recipient while respecting the fact
902 // that we are bouncing or not the message.
903 POST_NOTIFICATION(PantomimeTransactionInitiationCompleted, self, [NSDictionary dictionaryWithObject: _message forKey: @"Message"]);
904 PERFORM_SELECTOR_1(_delegate, @selector(transactionInitiationCompleted:), PantomimeTransactionInitiationCompleted);
905
906 [self sendCommand: SMTP_RCPT arguments: @"RCPT TO:<%@>", [next_recipient(_sent_recipients, _redirected) address]];
907 }
908 else
909 {
910 if (PERFORM_SELECTOR_1(_delegate, @selector(transactionInitiationFailed:), PantomimeTransactionInitiationFailed))
911 {
912 POST_NOTIFICATION(PantomimeTransactionInitiationFailed, self, [NSDictionary dictionaryWithObject: _message forKey: @"Message"]);
913 }
914 else
915 {
916 [self _fail];
917 }
918 }
919 }
920
921
922 //
923 //
924 //
925 - (void) _parseNOOP
926 {
927 // Do what?
928 }
929
930
931 //
932 //
933 //
934 - (void) _parseQUIT
935 {
936 NSData *aData;
937
938 aData = [_responsesFromServer lastObject];
939
940 if ([aData hasCPrefix: "221"])
941 {
942 // Do anything special here?
943 }
944
945 [super close];
946 }
947
948
949 //
950 // This method is invoked everytime we sent a recipient to the
951 // server using the RCPT command.
952 //
953 // If it was successful, this command sends the next one, if any
954 // by first removing the previously sent one from _recipients.
955 //
956 - (void) _parseRCPT
957 {
958 NSData *aData;
959
960 aData = [_responsesFromServer lastObject];
961
962 if ([aData hasCPrefix: "250"])
963 {
964 CWInternetAddress *theAddress;
965
966 theAddress = next_recipient(_sent_recipients, _redirected);
967
968 if (theAddress)
969 {
970 [_sent_recipients removeObject: theAddress];
971
972 theAddress = next_recipient(_sent_recipients, _redirected);
973
974 if (theAddress)
975 {
976 [self sendCommand: SMTP_RCPT arguments: @"RCPT TO:<%@>", [theAddress address]];
977 return;
978 }
979 }
980
981 // We are done writing the recipients, we now write the content
982 // of the message.
983 POST_NOTIFICATION(PantomimeRecipientIdentificationCompleted, self, [NSDictionary dictionaryWithObject: _recipients forKey: @"Recipients"]);
984 PERFORM_SELECTOR_2(_delegate, @selector(recipientIdentificationCompleted:), PantomimeRecipientIdentificationCompleted, _recipients, @"Recipients");
985 [self sendCommand: SMTP_DATA arguments: @"DATA"];
986 }
987 else
988 {
989 #warning also send the invalid recipient in perform selector and when posting the notification
990 if (PERFORM_SELECTOR_1(_delegate, @selector(recipientIdentificationFailed:), PantomimeRecipientIdentificationFailed))
991 {
992 POST_NOTIFICATION(PantomimeRecipientIdentificationFailed, self, [NSDictionary dictionaryWithObject: _recipients forKey: @"Recipients"]);
993 }
994 else
995 {
996 [self _fail];
997 }
998 }
999 }
1000
1001 //
1002 //
1003 //
1004 - (void) _parseRSET
1005 {
1006 NSData *aData;
1007
1008 aData = [_responsesFromServer lastObject];
1009
1010 if ([aData hasCPrefix: "250"])
1011 {
1012 POST_NOTIFICATION(PantomimeTransactionResetCompleted, self, nil);
1013 PERFORM_SELECTOR_1(_delegate, @selector(transactionResetCompleted:), PantomimeTransactionResetCompleted);
1014 }
1015 else
1016 {
1017 POST_NOTIFICATION(PantomimeTransactionResetFailed, self, nil);
1018 PERFORM_SELECTOR_1(_delegate, @selector(transactionResetFailed:), PantomimeTransactionResetFailed);
1019 }
1020 }
1021
1022 //
1023 //
1024 //
1025 - (void) _parseSTARTTLS
1026 {
1027 NSData *aData;
1028
1029 aData = [_responsesFromServer lastObject];
1030
1031 if ([aData hasCPrefix: "220"])
1032 {
1033 // We first activate SSL.
1034 [(CWTCPConnection *)_connection startSSL];
1035
1036 // We now forget about the initial negotiated state; see RFC2487 for more details,
1037 [_supportedMechanisms removeAllObjects];
1038 [self sendCommand: SMTP_EHLO arguments: @"EHLO localhost.localdomain"];
1039 }
1040 else
1041 {
1042 // The server probably doesn't support TLS. We inform the delegate that the transaction initiation
1043 // failed or that the message wasn't sent.
1044 if (PERFORM_SELECTOR_1(_delegate, @selector(transactionInitiationFailed:), PantomimeTransactionInitiationFailed))
1045 {
1046 POST_NOTIFICATION(PantomimeTransactionInitiationFailed, self, [NSDictionary dictionaryWithObject: _message forKey: @"Message"]);
1047 }
1048 else
1049 {
1050 [self _fail];
1051 }
1052 }
1053 }
1054
1055 //
1056 //
1057 //
1058 - (void) _parseServerOutput
1059 {
1060 NSData *aData;
1061
1062 if (![_responsesFromServer count])
1063 {
1064 return;
1065 }
1066
1067 // We read only the first response. The _parseXYZ methods
1068 // will handle multiline responses.
1069 aData = [_responsesFromServer objectAtIndex: 0];
1070
1071 if ([aData hasCPrefix: "421"])
1072 {
1073 // FIXME: - lost connection
1074 //NSLog(@"LOST CONNECTION TO THE SERVER");
1075 [super close];
1076 }
1077 else
1078 {
1079 switch (_lastCommand)
1080 {
1081 case SMTP_AUTH_CRAM_MD5:
1082 [self _parseAUTH_CRAM_MD5];
1083 break;
1084
1085 case SMTP_AUTH_LOGIN:
1086 [self _parseAUTH_LOGIN];
1087 break;
1088
1089 case SMTP_AUTH_LOGIN_CHALLENGE:
1090 [self _parseAUTH_LOGIN_CHALLENGE];
1091 break;
1092
1093 case SMTP_AUTH_PLAIN:
1094 [self _parseAUTH_PLAIN];
1095 break;
1096
1097 case SMTP_DATA:
1098 [self _parseDATA];
1099 break;
1100
1101 case SMTP_EHLO:
1102 [self _parseEHLO];
1103 break;
1104
1105 case SMTP_HELO:
1106 [self _parseHELO];
1107 break;
1108
1109 case SMTP_MAIL:
1110 [self _parseMAIL];
1111 break;
1112
1113 case SMTP_NOOP:
1114 [self _parseNOOP];
1115 break;
1116
1117 case SMTP_QUIT:
1118 [self _parseQUIT];
1119 break;
1120
1121 case SMTP_RCPT:
1122 [self _parseRCPT];
1123 break;
1124
1125 case SMTP_RSET:
1126 [self _parseRSET];
1127 break;
1128
1129 case SMTP_STARTTLS:
1130 [self _parseSTARTTLS];
1131 break;
1132
1133 case SMTP_AUTHORIZATION:
1134 [self _parseAUTHORIZATION];
1135 break;
1136
1137 default:
1138 break;
1139 // FIXME
1140 }
1141 }
1142
1143 // We are done parsing this entry...
1144 [_responsesFromServer removeAllObjects];
1145
1146 // We remove the last object of the queue....
1147 if ([_queue lastObject])
1148 {
1149 [_queue removeLastObject];
1150 }
1151
1152 [self sendCommand: SMTP_EMPTY_QUEUE arguments: @""];
1153 }
1154
1155 @end