Skip to content

Commit

Permalink
Update MimeVisitor to only use interfaces (e.g. IMimePart)
Browse files Browse the repository at this point in the history
This allows us to include the necessary APIs for things like
IMultipartEncrypted, IMultipartSigned, IApplicationPkcs7Mime, etc.
while only providing the implementation of those types in a future
separate MimeKit.Cryptography extension nuget package.

Classes like ApplicationPgpEEncrypted and ApplicationPgpSignature,
on the other hand, could be part of the core MimeKit package because
they don't actually provide any crypto routines. They are just
convenience classes.

Another partial fix for issue #820
  • Loading branch information
jstedfast committed Jan 27, 2024
1 parent 197f783 commit da9f0d3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 70 deletions.
63 changes: 27 additions & 36 deletions MimeKit/MimeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
// THE SOFTWARE.
//

#if ENABLE_CRYPTO
using MimeKit.Cryptography;
#endif

using MimeKit.Tnef;
using MimeKit.Cryptography;

namespace MimeKit {
/// <summary>
Expand All @@ -50,7 +47,7 @@ public abstract class MimeVisitor
/// Dispatches the entity to one of the more specialized visit methods in this class.
/// </remarks>
/// <param name="entity">The MIME entity.</param>
public virtual void Visit (MimeEntity entity)
public virtual void Visit (IMimeEntity entity)
{
entity?.Accept (this);
}
Expand All @@ -62,12 +59,11 @@ public virtual void Visit (MimeEntity entity)
/// Dispatches the message to one of the more specialized visit methods in this class.
/// </remarks>
/// <param name="message">The MIME message.</param>
public virtual void Visit (MimeMessage message)
public virtual void Visit (IMimeMessage message)
{
message?.Accept (this);
}

#if ENABLE_CRYPTO
/// <summary>
/// Visit the application/pgp-encrypted MIME entity.
/// </summary>
Expand All @@ -76,7 +72,7 @@ public virtual void Visit (MimeMessage message)
/// </remarks>
/// <seealso cref="MimeKit.Cryptography.MultipartEncrypted"/>
/// <param name="entity">The application/pgp-encrypted MIME entity.</param>
protected internal virtual void VisitApplicationPgpEncrypted (ApplicationPgpEncrypted entity)
protected internal virtual void VisitApplicationPgpEncrypted (IApplicationPgpEncrypted entity)
{
VisitMimePart (entity);
}
Expand All @@ -89,7 +85,7 @@ protected internal virtual void VisitApplicationPgpEncrypted (ApplicationPgpEncr
/// </remarks>
/// <seealso cref="MimeKit.Cryptography.MultipartSigned"/>
/// <param name="entity">The application/pgp-signature MIME entity.</param>
protected internal virtual void VisitApplicationPgpSignature (ApplicationPgpSignature entity)
protected internal virtual void VisitApplicationPgpSignature (IApplicationPgpSignature entity)
{
VisitMimePart (entity);
}
Expand All @@ -101,7 +97,7 @@ protected internal virtual void VisitApplicationPgpSignature (ApplicationPgpSign
/// Visits the application/pkcs7-mime MIME entity.
/// </remarks>
/// <param name="entity">The application/pkcs7-mime MIME entity.</param>
protected internal virtual void VisitApplicationPkcs7Mime (ApplicationPkcs7Mime entity)
protected internal virtual void VisitApplicationPkcs7Mime (IApplicationPkcs7Mime entity)
{
VisitMimePart (entity);
}
Expand All @@ -114,11 +110,10 @@ protected internal virtual void VisitApplicationPkcs7Mime (ApplicationPkcs7Mime
/// </remarks>
/// <seealso cref="MimeKit.Cryptography.MultipartSigned"/>
/// <param name="entity">The application/pkcs7-signature MIME entity.</param>
protected internal virtual void VisitApplicationPkcs7Signature (ApplicationPkcs7Signature entity)
protected internal virtual void VisitApplicationPkcs7Signature (IApplicationPkcs7Signature entity)
{
VisitMimePart (entity);
}
#endif

/// <summary>
/// Visit the message/disposition-notification MIME entity.
Expand All @@ -127,7 +122,7 @@ protected internal virtual void VisitApplicationPkcs7Signature (ApplicationPkcs7
/// Visits the message/disposition-notification MIME entity.
/// </remarks>
/// <param name="entity">The message/disposition-notification MIME entity.</param>
protected internal virtual void VisitMessageDispositionNotification (MessageDispositionNotification entity)
protected internal virtual void VisitMessageDispositionNotification (IMessageDispositionNotification entity)
{
VisitMimePart (entity);
}
Expand All @@ -139,7 +134,7 @@ protected internal virtual void VisitMessageDispositionNotification (MessageDisp
/// Visits the message/delivery-status MIME entity.
/// </remarks>
/// <param name="entity">The message/delivery-status MIME entity.</param>
protected internal virtual void VisitMessageDeliveryStatus (MessageDeliveryStatus entity)
protected internal virtual void VisitMessageDeliveryStatus (IMessageDeliveryStatus entity)
{
VisitMimePart (entity);
}
Expand All @@ -151,7 +146,7 @@ protected internal virtual void VisitMessageDeliveryStatus (MessageDeliveryStatu
/// Visits the message/feedback-report MIME entity.
/// </remarks>
/// <param name="entity">The message/feedback-report MIME entity.</param>
protected internal virtual void VisitMessageFeedbackReport (MessageFeedbackReport entity)
protected internal virtual void VisitMessageFeedbackReport (IMessageFeedbackReport entity)
{
VisitMimePart (entity);
}
Expand All @@ -163,7 +158,7 @@ protected internal virtual void VisitMessageFeedbackReport (MessageFeedbackRepor
/// Visits the message contained within a message/rfc822 or message/news MIME entity.
/// </remarks>
/// <param name="entity">The message/rfc822 or message/news MIME entity.</param>
protected virtual void VisitMessage (MessagePart entity)
protected virtual void VisitMessage (IMessagePart entity)
{
entity.Message?.Accept (this);
}
Expand All @@ -178,7 +173,7 @@ protected virtual void VisitMessage (MessagePart entity)
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="entity">The message/rfc822 or message/news MIME entity.</param>
protected internal virtual void VisitMessagePart (MessagePart entity)
protected internal virtual void VisitMessagePart (IMessagePart entity)
{
VisitMimeEntity (entity);
VisitMessage (entity);
Expand All @@ -191,7 +186,7 @@ protected internal virtual void VisitMessagePart (MessagePart entity)
/// Visits the message/partial MIME entity.
/// </remarks>
/// <param name="entity">The message/partial MIME entity.</param>
protected internal virtual void VisitMessagePartial (MessagePartial entity)
protected internal virtual void VisitMessagePartial (IMessagePartial entity)
{
VisitMimePart (entity);
}
Expand All @@ -203,7 +198,7 @@ protected internal virtual void VisitMessagePartial (MessagePartial entity)
/// Visits the abstract MIME entity.
/// </remarks>
/// <param name="entity">The MIME entity.</param>
protected internal virtual void VisitMimeEntity (MimeEntity entity)
protected internal virtual void VisitMimeEntity (IMimeEntity entity)
{
}

Expand All @@ -214,7 +209,7 @@ protected internal virtual void VisitMimeEntity (MimeEntity entity)
/// Visits the body of the message.
/// </remarks>
/// <param name="message">The message.</param>
protected virtual void VisitBody (MimeMessage message)
protected virtual void VisitBody (IMimeMessage message)
{
message.Body?.Accept (this);
}
Expand All @@ -226,7 +221,7 @@ protected virtual void VisitBody (MimeMessage message)
/// Visits the MIME message.
/// </remarks>
/// <param name="message">The MIME message.</param>
protected internal virtual void VisitMimeMessage (MimeMessage message)
protected internal virtual void VisitMimeMessage (IMimeMessage message)
{
VisitBody (message);
}
Expand All @@ -241,7 +236,7 @@ protected internal virtual void VisitMimeMessage (MimeMessage message)
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="entity">The MIME part entity.</param>
protected internal virtual void VisitMimePart (MimePart entity)
protected internal virtual void VisitMimePart (IMimePart entity)
{
VisitMimeEntity (entity);
}
Expand All @@ -253,7 +248,7 @@ protected internal virtual void VisitMimePart (MimePart entity)
/// Visits the children of a <see cref="Multipart"/>.
/// </remarks>
/// <param name="multipart">Multipart.</param>
protected virtual void VisitChildren (Multipart multipart)
protected virtual void VisitChildren (IMultipart multipart)
{
for (int i = 0; i < multipart.Count; i++)
multipart[i].Accept (this);
Expand All @@ -266,7 +261,7 @@ protected virtual void VisitChildren (Multipart multipart)
/// Visits the abstract multipart MIME entity.
/// </remarks>
/// <param name="multipart">The multipart MIME entity.</param>
protected internal virtual void VisitMultipart (Multipart multipart)
protected internal virtual void VisitMultipart (IMultipart multipart)
{
VisitMimeEntity (multipart);
VisitChildren (multipart);
Expand All @@ -282,24 +277,22 @@ protected internal virtual void VisitMultipart (Multipart multipart)
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="alternative">The multipart/alternative MIME entity.</param>
protected internal virtual void VisitMultipartAlternative (MultipartAlternative alternative)
protected internal virtual void VisitMultipartAlternative (IMultipartAlternative alternative)
{
VisitMultipart (alternative);
}

#if ENABLE_CRYPTO
/// <summary>
/// Visit the multipart/encrypted MIME entity.
/// </summary>
/// <remarks>
/// Visits the multipart/encrypted MIME entity.
/// </remarks>
/// <param name="encrypted">The multipart/encrypted MIME entity.</param>
protected internal virtual void VisitMultipartEncrypted (MultipartEncrypted encrypted)
protected internal virtual void VisitMultipartEncrypted (IMultipartEncrypted encrypted)
{
VisitMultipart (encrypted);
}
#endif

/// <summary>
/// Visit the multipart/related MIME entity.
Expand All @@ -311,7 +304,7 @@ protected internal virtual void VisitMultipartEncrypted (MultipartEncrypted encr
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="related">The multipart/related MIME entity.</param>
protected internal virtual void VisitMultipartRelated (MultipartRelated related)
protected internal virtual void VisitMultipartRelated (IMultipartRelated related)
{
VisitMultipart (related);
}
Expand All @@ -326,24 +319,22 @@ protected internal virtual void VisitMultipartRelated (MultipartRelated related)
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="report">The multipart/report MIME entity.</param>
protected internal virtual void VisitMultipartReport (MultipartReport report)
protected internal virtual void VisitMultipartReport (IMultipartReport report)
{
VisitMultipart (report);
}

#if ENABLE_CRYPTO
/// <summary>
/// Visit the multipart/signed MIME entity.
/// </summary>
/// <remarks>
/// Visits the multipart/signed MIME entity.
/// </remarks>
/// <param name="signed">The multipart/signed MIME entity.</param>
protected internal virtual void VisitMultipartSigned (MultipartSigned signed)
protected internal virtual void VisitMultipartSigned (IMultipartSigned signed)
{
VisitMultipart (signed);
}
#endif

/// <summary>
/// Visit the text-based MIME part entity.
Expand All @@ -355,7 +346,7 @@ protected internal virtual void VisitMultipartSigned (MultipartSigned signed)
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="entity">The text-based MIME part entity.</param>
protected internal virtual void VisitTextPart (TextPart entity)
protected internal virtual void VisitTextPart (ITextPart entity)
{
VisitMimePart (entity);
}
Expand All @@ -370,7 +361,7 @@ protected internal virtual void VisitTextPart (TextPart entity)
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="entity">The text/rfc822-headers MIME entity.</param>
protected internal virtual void VisitTextRfc822Headers (TextRfc822Headers entity)
protected internal virtual void VisitTextRfc822Headers (ITextRfc822Headers entity)
{
VisitMessagePart (entity);
}
Expand All @@ -385,7 +376,7 @@ protected internal virtual void VisitTextRfc822Headers (TextRfc822Headers entity
/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
/// </example>
/// <param name="entity">The Microsoft TNEF MIME part entity.</param>
protected internal virtual void VisitTnefPart (TnefPart entity)
protected internal virtual void VisitTnefPart (ITnefPart entity)
{
VisitMimePart (entity);
}
Expand Down
7 changes: 2 additions & 5 deletions MimeKit/ParserOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@
using System.Reflection;
using System.Collections.Generic;

#if ENABLE_CRYPTO
using MimeKit.Cryptography;
#endif

using MimeKit.Tnef;
using MimeKit.Utils;
using MimeKit.Cryptography;

namespace MimeKit {
/// <summary>
Expand Down Expand Up @@ -395,6 +392,7 @@ internal MimeEntity CreateEntity (ContentType contentType, IList<Header> headers
if (subtype.Equals ("pkcs7-signature", StringComparison.OrdinalIgnoreCase) ||
subtype.Equals ("x-pkcs7-signature", StringComparison.OrdinalIgnoreCase))
return new ApplicationPkcs7Signature (args);
#endif

// application/pgp-encrypted
if (subtype.Equals ("pgp-encrypted", StringComparison.OrdinalIgnoreCase) ||
Expand All @@ -405,7 +403,6 @@ internal MimeEntity CreateEntity (ContentType contentType, IList<Header> headers
if (subtype.Equals ("pgp-signature", StringComparison.OrdinalIgnoreCase) ||
subtype.Equals ("x-pgp-signature", StringComparison.OrdinalIgnoreCase))
return new ApplicationPgpSignature (args);
#endif

// application/ms-tnef
if (subtype.Equals ("ms-tnef", StringComparison.OrdinalIgnoreCase) ||
Expand Down
10 changes: 5 additions & 5 deletions UnitTests/HtmlPreviewVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace UnitTests {
/// </summary>
class HtmlPreviewVisitor : MimeVisitor
{
readonly List<MultipartRelated> stack = new List<MultipartRelated> ();
readonly List<IMultipartRelated> stack = new List<IMultipartRelated> ();
string body;

/// <summary>
Expand All @@ -59,14 +59,14 @@ public string HtmlBody {
get { return body ?? string.Empty; }
}

protected internal override void VisitMultipartAlternative (MultipartAlternative alternative)
protected internal override void VisitMultipartAlternative (IMultipartAlternative alternative)
{
// walk the multipart/alternative children backwards from greatest level of faithfulness to the least faithful
for (int i = alternative.Count - 1; i >= 0 && body == null; i--)
alternative[i].Accept (this);
}

protected internal override void VisitMultipartRelated (MultipartRelated related)
protected internal override void VisitMultipartRelated (IMultipartRelated related)
{
var root = related.Root;

Expand Down Expand Up @@ -170,7 +170,7 @@ void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter)
}
}

protected internal override void VisitTextPart (TextPart entity)
protected internal override void VisitTextPart (ITextPart entity)
{
TextConverter converter;

Expand Down Expand Up @@ -200,7 +200,7 @@ protected internal override void VisitTextPart (TextPart entity)
base.VisitTextPart (entity);
}

protected internal override void VisitMessagePart (MessagePart entity)
protected internal override void VisitMessagePart (IMessagePart entity)
{
// don't descend into message/rfc822 parts
}
Expand Down
Loading

0 comments on commit da9f0d3

Please sign in to comment.