Skip to content

Being more robust for Mail Servers, that send incomplete SMTP responses. #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions sope-mime/NGMail/NGSmtpClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -473,33 +473,40 @@ - (void)abortTransaction {
// replies

- (NGSmtpResponse *)receiveReply {
NSMutableString *desc = nil;
NSString *line = nil;
NGSmtpReplyCode code = -1;
NSMutableString *desc = nil;
NSString *line = nil;
NGSmtpReplyCode code = -1;

line = [self->text readLineAsString];
if ([line length] < 4) {
NSLog(@"SMTP: reply has invalid format (%@)", line);
return nil;
}
code = [[line substringToIndex:3] intValue];
//if (self->isDebuggingEnabled)
// [NGTextErr writeFormat:@"S: reply with code %i follows ..\n", code];
line = [self->text readLineAsString];
if ([line length] < 3) {
NSLog(@"SMTP: reply has invalid format (%@)", line);
return nil;
}

desc = [NSMutableString stringWithCapacity:[line length]];
while ([line characterAtIndex:3] == '-') {
if ([line length] < 4) {
NSLog(@"SMTP: reply has invalid format (text=%@, line=%@)", desc, line);
break;
// Check if the first three characters are digits
NSCharacterSet *digitSet = [NSCharacterSet decimalDigitCharacterSet];
NSString *codeString = [line substringToIndex:3];

if ([codeString rangeOfCharacterFromSet:[digitSet invertedSet]].location != NSNotFound) {
NSLog(@"SMTP: reply code is not valid (%@)", line);
return nil;
}

code = [codeString intValue];

desc = [NSMutableString stringWithCapacity:[line length]];
while ([line length] > 3 && [line characterAtIndex:3] == '-') {
[desc appendString:[line substringFromIndex:4]];
[desc appendString:@"\n"];
line = [self->text readLineAsString];
}

// If the line is valid and has more than 3 characters, append the last part
if ([line length] >= 4) {
[desc appendString:[line substringFromIndex:4]];
}
[desc appendString:[line substringFromIndex:4]];
[desc appendString:@"\n"];
line = [self->text readLineAsString];
}
if ([line length] >= 4)
[desc appendString:[line substringFromIndex:4]];

return [NGSmtpResponse responseWithCode:code text:desc];
return [NGSmtpResponse responseWithCode:code text:desc];
}

// commands
Expand Down