Skip to content

Commit

Permalink
Added MailboxAddress.Parse() methods
Browse files Browse the repository at this point in the history
Fixes issue #197
  • Loading branch information
jstedfast committed Dec 8, 2015
1 parent 6a09af8 commit e75ff54
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 57 deletions.
11 changes: 3 additions & 8 deletions MimeKit/Cryptography/OpenPgpDigitalCertificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,14 @@ internal OpenPgpDigitalCertificate (PgpPublicKey pubkey)

foreach (string userId in pubkey.GetUserIds ()) {
data = Encoding.UTF8.GetBytes (userId);
InternetAddress address;
MailboxAddress mailbox;
int index = 0;

if (!InternetAddress.TryParse (ParserOptions.Default, data, ref index, data.Length, false, out address))
continue;

Name = address.Name;

var mailbox = address as MailboxAddress;
if (mailbox == null)
if (!MailboxAddress.TryParse (ParserOptions.Default, data, ref index, data.Length, false, out mailbox))
continue;

Email = mailbox.Address;
Name = mailbox.Name;
break;
}
}
Expand Down
48 changes: 39 additions & 9 deletions MimeKit/InternetAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,20 @@ static bool TryParseGroup (ParserOptions options, byte[] text, int startIndex, r
return true;
}

internal static bool TryParse (ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out InternetAddress address)
[Flags]
internal enum TryParseFlags {
AllowMailboxAddress = 1 << 0,
AllowGroupAddress = 1 << 1,
ThrowOnError = 1 << 2,

TryParse = AllowMailboxAddress | AllowGroupAddress,
Parse = TryParse | ThrowOnError
}

internal static bool TryParse (ParserOptions options, byte[] text, ref int index, int endIndex, TryParseFlags flags, out InternetAddress address)
{
bool throwOnError = (flags & TryParseFlags.ThrowOnError) != 0;

address = null;

if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
Expand Down Expand Up @@ -544,6 +556,13 @@ internal static bool TryParse (ParserOptions options, byte[] text, ref int index
int codepage = -1;
string name;

if ((flags & TryParseFlags.AllowGroupAddress) == 0) {
if (throwOnError)
throw new ParseException (string.Format ("group address token at offset {0}", startIndex), startIndex, index);

return false;
}

if (length > 0) {
name = Rfc2047.DecodePhrase (options, text, startIndex, length, out codepage);
} else {
Expand All @@ -556,6 +575,13 @@ internal static bool TryParse (ParserOptions options, byte[] text, ref int index
return TryParseGroup (options, text, startIndex, ref index, endIndex, MimeUtils.Unquote (name), codepage, throwOnError, out address);
}

if ((flags & TryParseFlags.AllowMailboxAddress) == 0) {
if (throwOnError)
throw new ParseException (string.Format ("mailbox address token at offset {0}", startIndex), startIndex, index);

return false;
}

if (text[index] == (byte) '<') {
// rfc2822 angle-addr token
int codepage = -1;
Expand Down Expand Up @@ -652,7 +678,7 @@ public static bool TryParse (ParserOptions options, byte[] buffer, int startInde
int endIndex = startIndex + length;
int index = startIndex;

if (!TryParse (options, buffer, ref index, endIndex, false, out address))
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.TryParse, out address))
return false;

if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
Expand Down Expand Up @@ -726,7 +752,7 @@ public static bool TryParse (ParserOptions options, byte[] buffer, int startInde
int endIndex = buffer.Length;
int index = startIndex;

if (!TryParse (options, buffer, ref index, endIndex, false, out address))
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.TryParse, out address))
return false;

if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
Expand Down Expand Up @@ -791,7 +817,7 @@ public static bool TryParse (ParserOptions options, byte[] buffer, out InternetA
int endIndex = buffer.Length;
int index = 0;

if (!TryParse (options, buffer, ref index, endIndex, false, out address))
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.TryParse, out address))
return false;

if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
Expand Down Expand Up @@ -851,7 +877,7 @@ public static bool TryParse (ParserOptions options, string text, out InternetAdd
int endIndex = buffer.Length;
int index = 0;

if (!TryParse (options, buffer, ref index, endIndex, false, out address))
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.TryParse, out address))
return false;

if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, false)) {
Expand Down Expand Up @@ -927,7 +953,8 @@ public static InternetAddress Parse (ParserOptions options, byte[] buffer, int s
InternetAddress address;
int index = startIndex;

TryParse (options, buffer, ref index, endIndex, true, out address);
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.Parse, out address))
throw new ParseException ("No address found.", startIndex, startIndex);

ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, true);

Expand Down Expand Up @@ -1000,7 +1027,8 @@ public static InternetAddress Parse (ParserOptions options, byte[] buffer, int s
InternetAddress address;
int index = startIndex;

TryParse (options, buffer, ref index, endIndex, true, out address);
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.Parse, out address))
throw new ParseException ("No address found.", startIndex, startIndex);

ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, true);

Expand Down Expand Up @@ -1064,7 +1092,8 @@ public static InternetAddress Parse (ParserOptions options, byte[] buffer)
InternetAddress address;
int index = 0;

TryParse (options, buffer, ref index, endIndex, true, out address);
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.Parse, out address))
throw new ParseException ("No address found.", 0, 0);

ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, true);

Expand Down Expand Up @@ -1125,7 +1154,8 @@ public static InternetAddress Parse (ParserOptions options, string text)
InternetAddress address;
int index = 0;

TryParse (options, buffer, ref index, endIndex, true, out address);
if (!TryParse (options, buffer, ref index, endIndex, TryParseFlags.Parse, out address))
throw new ParseException ("No address found.", 0, 0);

ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, true);

Expand Down
5 changes: 3 additions & 2 deletions MimeKit/InternetAddressList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ void AddressChanged (object sender, EventArgs e)

internal static bool TryParse (ParserOptions options, byte[] text, ref int index, int endIndex, bool isGroup, bool throwOnError, out List<InternetAddress> addresses)
{
List<InternetAddress> list = new List<InternetAddress> ();
var flags = throwOnError ? InternetAddress.TryParseFlags.Parse : InternetAddress.TryParseFlags.TryParse;
var list = new List<InternetAddress> ();
InternetAddress address;

addresses = null;
Expand All @@ -564,7 +565,7 @@ internal static bool TryParse (ParserOptions options, byte[] text, ref int index
if (isGroup && text[index] == (byte) ';')
break;

if (!InternetAddress.TryParse (options, text, ref index, endIndex, throwOnError, out address)) {
if (!InternetAddress.TryParse (options, text, ref index, endIndex, flags, out address)) {
// skip this address...
while (index < endIndex && text[index] != (byte) ',' && (!isGroup || text[index] != (byte) ';'))
index++;
Expand Down
Loading

0 comments on commit e75ff54

Please sign in to comment.