Skip to content

Commit

Permalink
minor code sharing improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Jan 6, 2024
1 parent 49f1b4e commit 67e1549
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions MailKit/Net/Imap/ImapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2150,7 +2150,7 @@ public GroupAddress ToGroupAddress (ImapEngine engine)
}
}

static bool TryAddEnvelopeAddressToken (ImapToken token, ref int index, string[] values, bool[] qstrings)
static bool TryAddEnvelopeAddressToken (ImapToken token, ref int index, string[] values, bool[] qstrings, string format)
{
// This is a work-around for mail servers which output too many tokens for an ENVELOPE address. In at least 1 case, this happened
// because the server sent a literal token as the name component and miscalculated the literal length as 38 when it was actually 69
Expand All @@ -2174,6 +2174,10 @@ static bool TryAddEnvelopeAddressToken (ImapToken token, ref int index, string[]
}

switch (token.Type) {
case ImapTokenType.Literal:
// Return control to our caller so that it can read the literal token.
qstrings[index] = false;
return false;
case ImapTokenType.QString:
values[index] = (string) token.Value;
qstrings[index] = true;
Expand All @@ -2187,8 +2191,7 @@ static bool TryAddEnvelopeAddressToken (ImapToken token, ref int index, string[]
qstrings[index] = false;
break;
default:
qstrings[index] = false;
return false;
throw ImapEngine.UnexpectedToken (format, token);
}

return true;
Expand All @@ -2207,13 +2210,8 @@ static EnvelopeAddress ParseEnvelopeAddress (ImapEngine engine, string format, C
if (token.Type == ImapTokenType.CloseParen)
break;

if (!TryAddEnvelopeAddressToken (token, ref index, values, qstrings)) {
if (token.Type == ImapTokenType.Literal) {
values[index] = engine.ReadLiteral (cancellationToken);
} else {
throw ImapEngine.UnexpectedToken (format, token);
}
}
if (!TryAddEnvelopeAddressToken (token, ref index, values, qstrings, format))
values[index] = engine.ReadLiteral (cancellationToken);

index++;
} while (true);
Expand All @@ -2236,13 +2234,8 @@ static async Task<EnvelopeAddress> ParseEnvelopeAddressAsync (ImapEngine engine,
if (token.Type == ImapTokenType.CloseParen)
break;

if (!TryAddEnvelopeAddressToken (token, ref index, values, qstrings)) {
if (token.Type == ImapTokenType.Literal) {
values[index] = await engine.ReadLiteralAsync (cancellationToken).ConfigureAwait (false);
} else {
throw ImapEngine.UnexpectedToken (format, token);
}
}
if (!TryAddEnvelopeAddressToken (token, ref index, values, qstrings, format))
values[index] = await engine.ReadLiteralAsync (cancellationToken).ConfigureAwait (false);

index++;
} while (true);
Expand Down

0 comments on commit 67e1549

Please sign in to comment.