Skip to content

Commit

Permalink
Added ParserOptions.AllowUnquotedCommasInAddresses and changed Parser…
Browse files Browse the repository at this point in the history
…Options.AllowAdddressesWithoutDomain

ParserOptions.AllowUnquotedCommasInAddresses is now effectively the same
as the old version of AllowAddressesWithoutDomain (but inverted).

ParserOptions.AllowAdddressesWithoutDomain now works as people expected it to work.

Fixes issue #465
  • Loading branch information
jstedfast committed Feb 23, 2019
1 parent ff621d5 commit 788b504
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
9 changes: 8 additions & 1 deletion MimeKit/InternetAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ internal static bool TryParse (ParserOptions options, byte[] text, ref int index
{
bool strict = options.AddressParserComplianceMode == RfcComplianceMode.Strict;
bool throwOnError = (flags & AddressParserFlags.ThrowOnError) != 0;
int minWordCount = options.AllowAddressesWithoutDomain ? 1 : 0;
int minWordCount = options.AllowUnquotedCommasInAddresses ? 0 : 1;

address = null;

Expand Down Expand Up @@ -662,6 +662,13 @@ internal static bool TryParse (ParserOptions options, byte[] text, ref int index
return false;
}

if (!options.AllowAddressesWithoutDomain) {
if (throwOnError)
throw new ParseException (string.Format ("Incomplete addr-spec token at offset {0}", startIndex), startIndex, index);

return false;
}

// rewind back to the beginning of the local-part
index = startIndex;

Expand Down
18 changes: 15 additions & 3 deletions MimeKit/ParserOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ public class ParserOptions
public RfcComplianceMode AddressParserComplianceMode { get; set; }

/// <summary>
/// Gets or sets whether the rfc822 address parser should allow addresses without a domain.
/// Gets or sets whether the rfc822 address parser should ignore unquoted commas in address names.
/// </summary>
/// <remarks>
/// <para>In general, you'll probably want this value to be <c>false</c> (the default) as it allows
/// <para>In general, you'll probably want this value to be <c>true</c> (the default) as it allows
/// maximum interoperability with existing (broken) mail clients and other mail software such as
/// sloppily written perl scripts (aka spambots) that do not properly quote the name when it
/// contains a comma.</para>
/// </remarks>
/// <value><c>true</c> if the address parser should ignore unquoted commas in address names; otherwise, <c>false</c>.</value>
public bool AllowUnquotedCommasInAddresses { get; set; }

/// <summary>
/// Gets or sets whether the rfc822 address parser should allow addresses without a domain.
/// </summary>
/// <remarks>
/// <para>In general, you'll probably want this value to be <c>true</c> (the default) as it allows
/// maximum interoperability with older email messages that may contain local UNIX addresses.</para>
/// <para>This option exists in order to allow parsing of mailbox addresses that do not have an
/// @domain component. These types of addresses are rare and were typically only used when sending
/// mail to other users on the same UNIX system.</para>
Expand Down Expand Up @@ -168,7 +178,8 @@ public ParserOptions ()
ParameterComplianceMode = RfcComplianceMode.Loose;
Rfc2047ComplianceMode = RfcComplianceMode.Loose;
CharsetEncoding = CharsetUtils.UTF8;
AllowAddressesWithoutDomain = false;
AllowUnquotedCommasInAddresses = true;
AllowAddressesWithoutDomain = true;
RespectContentLength = false;
MaxAddressGroupDepth = 3;
}
Expand All @@ -185,6 +196,7 @@ public ParserOptions Clone ()
{
var options = new ParserOptions ();
options.AddressParserComplianceMode = AddressParserComplianceMode;
options.AllowUnquotedCommasInAddresses = AllowUnquotedCommasInAddresses;
options.AllowAddressesWithoutDomain = AllowAddressesWithoutDomain;
options.ParameterComplianceMode = ParameterComplianceMode;
options.Rfc2047ComplianceMode = Rfc2047ComplianceMode;
Expand Down
7 changes: 4 additions & 3 deletions UnitTests/InternetAddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,14 @@ public void TestParseMailboxWithUnquotedCommaInName ()

// this should fail when we allow mailbox addresses w/o a domain
var options = ParserOptions.Default.Clone ();
options.AllowAddressesWithoutDomain = true;
options.AllowUnquotedCommasInAddresses = false;
options.AllowAddressesWithoutDomain = false;

try {
addr = InternetAddress.Parse (options, text);
Assert.Fail ("Should not have parsed \"{0}\" with AllowAddressesWithoutDomain = true", text);
Assert.Fail ("Should not have parsed \"{0}\" with AllowUnquotedCommasInAddresses = false", text);
} catch (ParseException pex) {
Assert.AreEqual (text.IndexOf (','), pex.TokenIndex, "TokenIndex");
Assert.AreEqual (0, pex.TokenIndex, "TokenIndex");
Assert.AreEqual (text.IndexOf (','), pex.ErrorIndex, "ErrorIndex");
} catch (Exception ex) {
Assert.Fail ("Should not have thrown {0}", ex.GetType ().Name);
Expand Down
7 changes: 4 additions & 3 deletions UnitTests/MailboxAddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,14 @@ public void TestParseMailboxWithUnquotedCommaInName ()

// this should fail when we allow mailbox addresses w/o a domain
var options = ParserOptions.Default.Clone ();
options.AllowAddressesWithoutDomain = true;
options.AllowUnquotedCommasInAddresses = false;
options.AllowAddressesWithoutDomain = false;

try {
mailbox = MailboxAddress.Parse (options, text);
Assert.Fail ("Should not have parsed \"{0}\" with AllowAddressesWithoutDomain = true", text);
Assert.Fail ("Should not have parsed \"{0}\" with AllowUnquotedCommasInAddresses = false", text);
} catch (ParseException pex) {
Assert.AreEqual (text.IndexOf (','), pex.TokenIndex, "TokenIndex");
Assert.AreEqual (0, pex.TokenIndex, "TokenIndex");
Assert.AreEqual (text.IndexOf (','), pex.ErrorIndex, "ErrorIndex");
} catch (Exception ex) {
Assert.Fail ("Should not have thrown {0}", ex.GetType ().Name);
Expand Down

0 comments on commit 788b504

Please sign in to comment.