Skip to content

Commit

Permalink
Added support for the IMAP SORT=DISPLAY extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Mar 24, 2015
1 parent 1cbd155 commit d7149f6
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 28 deletions.
23 changes: 14 additions & 9 deletions MailKit/Net/Imap/ImapCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,40 +227,45 @@ public enum ImapCapabilities : long {
/// </summary>
ListStatus = 1L << 36,

/// <summary>
/// The server supports the SORT=DISPLAY extension defined in rfc5957.
/// </summary>
SortDisplay = 1L << 37,

/// <summary>
/// The server supports the CREATE-SPECIAL-USE extension defined in rfc6154.
/// </summary>
CreateSpecialUse = 1L << 37,
CreateSpecialUse = 1L << 38,

/// <summary>
/// The server supports the SPECIAL-USE extension defined in rfc6154.
/// </summary>
SpecialUse = 1L << 38,
SpecialUse = 1L << 39,

/// <summary>
/// The server supports the SEARCH=FUZZY extension defined in rfc6203.
/// </summary>
FuzzySearch = 1L << 39,
FuzzySearch = 1L << 40,

/// <summary>
/// The server supports the MULTISEARCH extension defined in rfc6237.
/// </summary>
MultiSearch = 1L << 40,
MultiSearch = 1L << 41,

/// <summary>
/// The server supports the MOVE extension defined in rfc6851.
/// </summary>
Move = 1L << 41,
Move = 1L << 42,

/// <summary>
/// The server supports the UTF8=ACCEPT extension defined in rfc6855.
/// </summary>
UTF8Accept = 1L << 42,
UTF8Accept = 1L << 43,

/// <summary>
/// The server supports the UTF8=ONLY extension defined in rfc6855.
/// </summary>
UTF8Only = 1L << 43,
UTF8Only = 1L << 44,

#region GMail Extensions

Expand All @@ -269,12 +274,12 @@ public enum ImapCapabilities : long {
/// <summary>
/// The server supports the XLIST extension (GMail).
/// </summary>
XList = 1L << 44,
XList = 1L << 45,

/// <summary>
/// The server supports the X-GM-EXT1 extension (GMail).
/// </summary>
GMailExt1 = 1L << 45
GMailExt1 = 1L << 46

#endregion
}
Expand Down
1 change: 1 addition & 0 deletions MailKit/Net/Imap/ImapEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ void UpdateCapabilities (ImapTokenType sentinel, CancellationToken cancellationT
case "METADATA": Capabilities |= ImapCapabilities.Metadata; break;
case "NOTIFY": Capabilities |= ImapCapabilities.Notify; break;
case "LIST-STATUS": Capabilities |= ImapCapabilities.ListStatus; break;
case "SORT=DISPLAY": Capabilities |= ImapCapabilities.SortDisplay; break;
case "CREATE-SPECIAL-USE": Capabilities |= ImapCapabilities.CreateSpecialUse; break;
case "SPECIAL-USE": Capabilities |= ImapCapabilities.SpecialUse; break;
case "SEARCH=FUZZY": Capabilities |= ImapCapabilities.FuzzySearch; break;
Expand Down
30 changes: 23 additions & 7 deletions MailKit/Net/Imap/ImapFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7007,13 +7007,15 @@ static string BuildSortOrder (IList<OrderBy> orderBy)
builder.Append ("REVERSE ");

switch (orderBy [i].Type) {
case OrderByType.Arrival: builder.Append ("ARRIVAL"); break;
case OrderByType.Cc: builder.Append ("CC"); break;
case OrderByType.Date: builder.Append ("DATE"); break;
case OrderByType.From: builder.Append ("FROM"); break;
case OrderByType.Size: builder.Append ("SIZE"); break;
case OrderByType.Subject: builder.Append ("SUBJECT"); break;
case OrderByType.To: builder.Append ("TO"); break;
case OrderByType.Arrival: builder.Append ("ARRIVAL"); break;
case OrderByType.Cc: builder.Append ("CC"); break;
case OrderByType.Date: builder.Append ("DATE"); break;
case OrderByType.DisplayFrom: builder.Append ("DISPLAYFROM"); break;
case OrderByType.DisplayTo: builder.Append ("DISPLAYTO"); break;
case OrderByType.From: builder.Append ("FROM"); break;
case OrderByType.Size: builder.Append ("SIZE"); break;
case OrderByType.Subject: builder.Append ("SUBJECT"); break;
case OrderByType.To: builder.Append ("TO"); break;
default: throw new ArgumentOutOfRangeException ();
}
}
Expand Down Expand Up @@ -7290,6 +7292,13 @@ static void ESearchMatches (ImapEngine engine, ImapCommand ic, int index)
if ((Engine.Capabilities & ImapCapabilities.Sort) == 0)
throw new NotSupportedException ("The IMAP server does not support the SORT extension.");

if ((Engine.Capabilities & ImapCapabilities.SortDisplay) == 0) {
for (int i = 0; i < orderBy.Count; i++) {
if (orderBy[i].Type == OrderByType.DisplayFrom || orderBy[i].Type == OrderByType.DisplayTo)
throw new NotSupportedException ("The IMAP server does not support the SORT=DISPLAY extension.");
}
}

var optimized = query.Optimize (new ImapSearchQueryOptimizer ());
var expr = BuildQueryExpression (optimized, args, out charset);
var order = BuildSortOrder (orderBy);
Expand Down Expand Up @@ -7492,6 +7501,13 @@ static void ESearchMatches (ImapEngine engine, ImapCommand ic, int index)
if ((Engine.Capabilities & ImapCapabilities.Sort) == 0)
throw new NotSupportedException ("The IMAP server does not support the SORT extension.");

if ((Engine.Capabilities & ImapCapabilities.SortDisplay) == 0) {
for (int i = 0; i < orderBy.Count; i++) {
if (orderBy[i].Type == OrderByType.DisplayFrom || orderBy[i].Type == OrderByType.DisplayTo)
throw new NotSupportedException ("The IMAP server does not support the SORT=DISPLAY extension.");
}
}

if (uids.Count == 0)
return new UniqueId[0];

Expand Down
56 changes: 44 additions & 12 deletions MailKit/Search/OrderBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ internal SortOrder Order {
public static readonly OrderBy ReverseArrival = new OrderBy (OrderByType.Arrival, SortOrder.Descending);

/// <summary>
/// Sort results by the Cc header in ascending order.
/// Sort results by the first email address in the Cc header in ascending order.
/// </summary>
/// <remarks>
/// Sort results by the Cc header in ascending order.
/// Sort results by the first email address in the Cc header in ascending order.
/// </remarks>
public static readonly OrderBy Cc = new OrderBy (OrderByType.Cc, SortOrder.Ascending);

/// <summary>
/// Sort results by the Cc header in descending order.
/// Sort results by the first email address in the Cc header in descending order.
/// </summary>
/// <remarks>
/// Sort results by the Cc header in descending order.
/// Sort results by the first email address in the Cc header in descending order.
/// </remarks>
public static readonly OrderBy ReverseCc = new OrderBy (OrderByType.Cc, SortOrder.Descending);

Expand All @@ -98,21 +98,37 @@ internal SortOrder Order {
public static readonly OrderBy ReverseDate = new OrderBy (OrderByType.Date, SortOrder.Descending);

/// <summary>
/// Sort results by the From header in ascending order.
/// Sort results by the first email address in the From header in ascending order.
/// </summary>
/// <remarks>
/// Sort results by the From header in ascending order.
/// Sort results by the first email address in the From header in ascending order.
/// </remarks>
public static readonly OrderBy From = new OrderBy (OrderByType.From, SortOrder.Ascending);

/// <summary>
/// Sort results by the From header in descending order.
/// Sort results by the first email address in the From header in descending order.
/// </summary>
/// <remarks>
/// Sort results by the From header in descending order.
/// Sort results by the first email address in the From header in descending order.
/// </remarks>
public static readonly OrderBy ReverseFrom = new OrderBy (OrderByType.From, SortOrder.Descending);

/// <summary>
/// Sort results by the first display name in the From header in ascending order.
/// </summary>
/// <remarks>
/// Sort results by the first display name in the From header in ascending order.
/// </remarks>
public static readonly OrderBy DisplayFrom = new OrderBy (OrderByType.DisplayFrom, SortOrder.Ascending);

/// <summary>
/// Sort results by the first display name in the From header in descending order.
/// </summary>
/// <remarks>
/// Sort results by the first display name in the From header in descending order.
/// </remarks>
public static readonly OrderBy ReverseDisplayFrom = new OrderBy (OrderByType.DisplayFrom, SortOrder.Descending);

/// <summary>
/// Sort results by the message size in ascending order.
/// </summary>
Expand Down Expand Up @@ -146,19 +162,35 @@ internal SortOrder Order {
public static readonly OrderBy ReverseSubject = new OrderBy (OrderByType.Subject, SortOrder.Descending);

/// <summary>
/// Sort results by the To header in ascending order.
/// Sort results by the first email address in the To header in ascending order.
/// </summary>
/// <remarks>
/// Sort results by the To header in ascending order.
/// Sort results by the first email address in the To header in ascending order.
/// </remarks>
public static readonly OrderBy To = new OrderBy (OrderByType.To, SortOrder.Ascending);

/// <summary>
/// Sort results by the To header in descending order.
/// Sort results by the first email address in the To header in descending order.
/// </summary>
/// <remarks>
/// Sort results by the To header in descending order.
/// Sort results by the first email address in the To header in descending order.
/// </remarks>
public static readonly OrderBy ReverseTo = new OrderBy (OrderByType.To, SortOrder.Descending);

/// <summary>
/// Sort results by the first display name in the To header in ascending order.
/// </summary>
/// <remarks>
/// Sort results by the first display name in the To header in ascending order.
/// </remarks>
public static readonly OrderBy DisplayTo = new OrderBy (OrderByType.DisplayTo, SortOrder.Ascending);

/// <summary>
/// Sort results by the first display name in the To header in descending order.
/// </summary>
/// <remarks>
/// Sort results by the first display name in the To header in descending order.
/// </remarks>
public static readonly OrderBy ReverseDisplayTo = new OrderBy (OrderByType.DisplayTo, SortOrder.Descending);
}
}
3 changes: 3 additions & 0 deletions MailKit/Search/OrderByType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ enum OrderByType {
Arrival,
Cc,
Date,
DisplayFrom,
DisplayTo,
From,
ModSeq,
Size,
Subject,
To
Expand Down
Loading

0 comments on commit d7149f6

Please sign in to comment.