Beispiel #1
0
        static BodyPartMessage CreateMessage(string type, string subtype, string partSpecifier, BodyPart body)
        {
            var message = new BodyPartMessage {
                ContentType = CreateContentType(type, subtype, partSpecifier)
            };

            message.Body = body;
            return(message);
        }
Beispiel #2
0
        static BodyPartMessage CreateMessage(string type, string subtype, string partSpecifier, BodyPart body, bool attachment)
        {
            var message = new BodyPartMessage {
                ContentType = CreateContentType(type, subtype, partSpecifier)
            };

            if (attachment)
            {
                message.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
            }
            message.Body = body;
            return(message);
        }
 /// <summary>
 /// Visit the message/rfc822 or message/news MIME entity.
 /// </summary>
 /// <remarks>
 /// Visits the message/rfc822 or message/news MIME entity.
 /// </remarks>
 /// <param name="entity">The message/rfc822 or message/news body part.</param>
 protected internal virtual void VisitBodyPartMessage(BodyPartMessage entity)
 {
     VisitBodyPartBasic(entity);
     VisitMessage(entity.Body);
 }
Beispiel #4
0
		public static BodyPart ParseBody (ImapEngine engine, string path, CancellationToken cancellationToken)
		{
			var token = engine.ReadToken (cancellationToken);

			if (token.Type == ImapTokenType.Nil)
				return null;

			if (token.Type != ImapTokenType.OpenParen)
				throw ImapEngine.UnexpectedToken (token, false);

			token = engine.PeekToken (cancellationToken);

			if (token.Type == ImapTokenType.OpenParen)
				return ParseMultipart (engine, path, null, cancellationToken);

			ContentType type;
			string value;

			if (!ParseContentType (engine, cancellationToken, out type, out value)) {
				// GMail breakage... yay! What we have is a nested multipart with
				// the same boundary as its parent.
				return ParseMultipart (engine, path, value, cancellationToken);
			}

			var id = ReadNStringToken (engine, false, cancellationToken);
			var desc = ReadNStringToken (engine, true, cancellationToken);
			// Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers...
			var enc = ReadNStringToken (engine, false, cancellationToken);
			var octets = ReadNumber (engine, cancellationToken);
			BodyPartBasic body;

			if (type.Matches ("message", "rfc822")) {
				var mesg = new BodyPartMessage ();

				// Note: GMail (and potentially other IMAP servers) will send body-part-basic
				// expressions instead of body-part-msg expressions when they encounter
				// message/rfc822 MIME parts that are illegally encoded using base64 (or
				// quoted-printable?). According to rfc3501, IMAP servers are REQUIRED to
				// send body-part-msg expressions for message/rfc822 parts, however, it is
				// understandable why GMail (and other IMAP servers?) do what they do in this
				// particular case.
				//
				// For examples, see issue #32 and issue #59.
				//
				// The workaround is to check for the expected '(' signifying an envelope token.
				// If we do not get an '(', then we are likely looking at the Content-MD5 token
				// which gets handled below.
				token = engine.PeekToken (cancellationToken);

				if (token.Type == ImapTokenType.OpenParen) {
					mesg.Envelope = ParseEnvelope (engine, cancellationToken);
					mesg.Body = ParseBody (engine, path, cancellationToken);
					mesg.Lines = ReadNumber (engine, cancellationToken);
				}

				body = mesg;
			} else if (type.Matches ("text", "*")) {
				var text = new BodyPartText ();
				text.Lines = ReadNumber (engine, cancellationToken);
				body = text;
			} else {
				body = new BodyPartBasic ();
			}

			body.ContentTransferEncoding = enc;
			body.ContentDescription = desc;
			body.PartSpecifier = path;
			body.ContentType = type;
			body.ContentId = id;
			body.Octets = octets;

			// if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')'
			token = engine.PeekToken (cancellationToken);

			if (token.Type != ImapTokenType.CloseParen) {
				body.ContentMd5 = ReadNStringToken (engine, false, cancellationToken);
				token = engine.PeekToken (cancellationToken);
			}

			if (token.Type != ImapTokenType.CloseParen) {
				body.ContentDisposition = ParseContentDisposition (engine, cancellationToken);
				token = engine.PeekToken (cancellationToken);
			}

			if (token.Type != ImapTokenType.CloseParen) {
				body.ContentLanguage = ParseContentLanguage (engine, cancellationToken);
				token = engine.PeekToken (cancellationToken);
			}

			if (token.Type != ImapTokenType.CloseParen) {
				body.ContentLocation = ParseContentLocation (engine, cancellationToken);
				token = engine.PeekToken (cancellationToken);
			}

			if (token.Type != ImapTokenType.CloseParen)
				SkipBodyExtensions (engine, cancellationToken);

			// read the ')'
			token = engine.ReadToken (cancellationToken);

			if (token.Type != ImapTokenType.CloseParen)
				throw ImapEngine.UnexpectedToken (token, false);

			return body;
		}
Beispiel #5
0
        public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken)
        {
            var token = engine.ReadToken(cancellationToken);

            if (token.Type == ImapTokenType.Nil)
            {
                return(null);
            }

            if (token.Type != ImapTokenType.OpenParen)
            {
                throw ImapEngine.UnexpectedToken(token, false);
            }

            token = engine.PeekToken(cancellationToken);

            if (token.Type == ImapTokenType.OpenParen)
            {
                return(ParseMultipart(engine, path, cancellationToken));
            }

            var           type   = ParseContentType(engine, cancellationToken);
            var           id     = ReadNStringToken(engine, false, cancellationToken);
            var           desc   = ReadNStringToken(engine, true, cancellationToken);
            var           enc    = ReadStringToken(engine, cancellationToken);
            var           octets = ReadNumber(engine, cancellationToken);
            BodyPartBasic body;

            if (type.Matches("message", "rfc822"))
            {
                var mesg = new BodyPartMessage();
                mesg.Envelope = ParseEnvelope(engine, cancellationToken);
                mesg.Body     = ParseBody(engine, path, cancellationToken);
                mesg.Lines    = ReadNumber(engine, cancellationToken);
                body          = mesg;
            }
            else if (type.Matches("text", "*"))
            {
                var text = new BodyPartText();
                text.Lines = ReadNumber(engine, cancellationToken);
                body       = text;
            }
            else
            {
                body = new BodyPartBasic();
            }

            body.ContentTransferEncoding = enc;
            body.ContentDescription      = desc;
            body.PartSpecifier           = path;
            body.ContentType             = type;
            body.ContentId = id;
            body.Octets    = octets;

            // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')'
            token = engine.PeekToken(cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentMd5 = ReadNStringToken(engine, false, cancellationToken);
                token           = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentDisposition = ParseContentDisposition(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentLanguage = ParseContentLocation(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentLocation = ReadNStringToken(engine, false, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                SkipBodyExtensions(engine, cancellationToken);
            }

            // read the ')'
            token = engine.ReadToken(cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
            {
                throw ImapEngine.UnexpectedToken(token, false);
            }

            return(body);
        }
Beispiel #6
0
        public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken)
        {
            var token = engine.ReadToken (cancellationToken);

            if (token.Type == ImapTokenType.Nil)
                return null;

            if (token.Type != ImapTokenType.OpenParen)
                throw ImapEngine.UnexpectedToken (token, false);

            token = engine.PeekToken (cancellationToken);

            if (token.Type == ImapTokenType.OpenParen)
                return ParseMultipart (engine, path, cancellationToken);

            var type = ParseContentType (engine, cancellationToken);
            var id = ReadNStringToken (engine, false, cancellationToken);
            var desc = ReadNStringToken (engine, true, cancellationToken);
            var enc = ReadStringToken (engine, cancellationToken);
            var octets = ReadNumber (engine, cancellationToken);
            BodyPartBasic body;

            if (type.Matches ("message", "rfc822")) {
                var mesg = new BodyPartMessage ();
                mesg.Envelope = ParseEnvelope (engine, cancellationToken);
                mesg.Body = ParseBody (engine, path, cancellationToken);
                mesg.Lines = ReadNumber (engine, cancellationToken);
                body = mesg;
            } else if (type.Matches ("text", "*")) {
                var text = new BodyPartText ();
                text.Lines = ReadNumber (engine, cancellationToken);
                body = text;
            } else {
                body = new BodyPartBasic ();
            }

            body.ContentTransferEncoding = enc;
            body.ContentDescription = desc;
            body.PartSpecifier = path;
            body.ContentType = type;
            body.ContentId = id;
            body.Octets = octets;

            // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')'
            token = engine.PeekToken (cancellationToken);

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentMd5 = ReadNStringToken (engine, false, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentDisposition = ParseContentDisposition (engine, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentLanguage = ParseContentLocation (engine, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentLocation = ReadNStringToken (engine, false, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
                SkipBodyExtensions (engine, cancellationToken);

            // read the ')'
            token = engine.ReadToken (cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
                throw ImapEngine.UnexpectedToken (token, false);

            return body;
        }
Beispiel #7
0
        public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken)
        {
            var token = engine.ReadToken(cancellationToken);

            if (token.Type == ImapTokenType.Nil)
            {
                return(null);
            }

            if (token.Type != ImapTokenType.OpenParen)
            {
                throw ImapEngine.UnexpectedToken(token, false);
            }

            token = engine.PeekToken(cancellationToken);

            if (token.Type == ImapTokenType.OpenParen)
            {
                return(ParseMultipart(engine, path, cancellationToken));
            }

            var type = ParseContentType(engine, cancellationToken);
            var id   = ReadNStringToken(engine, false, cancellationToken);
            var desc = ReadNStringToken(engine, true, cancellationToken);
            // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers...
            var           enc    = ReadNStringToken(engine, false, cancellationToken);
            var           octets = ReadNumber(engine, cancellationToken);
            BodyPartBasic body;

            if (type.Matches("message", "rfc822"))
            {
                var mesg = new BodyPartMessage();

                // Note: GMail's support for message/rfc822 body parts is broken. Essentially,
                // GMail treats message/rfc822 parts as if they were basic body parts.
                //
                // For examples, see issue #32 and issue #59.
                //
                // The workaround is to check for the expected '(' signifying an envelope token.
                // If we do not get an '(', then we are likely looking at the Content-MD5 token
                // which gets handled below.
                token = engine.PeekToken(cancellationToken);

                if (!engine.IsGMail || token.Type == ImapTokenType.OpenParen)
                {
                    mesg.Envelope = ParseEnvelope(engine, cancellationToken);
                    mesg.Body     = ParseBody(engine, path, cancellationToken);
                    mesg.Lines    = ReadNumber(engine, cancellationToken);
                }

                body = mesg;
            }
            else if (type.Matches("text", "*"))
            {
                var text = new BodyPartText();
                text.Lines = ReadNumber(engine, cancellationToken);
                body       = text;
            }
            else
            {
                body = new BodyPartBasic();
            }

            body.ContentTransferEncoding = enc;
            body.ContentDescription      = desc;
            body.PartSpecifier           = path;
            body.ContentType             = type;
            body.ContentId = id;
            body.Octets    = octets;

            // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')'
            token = engine.PeekToken(cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentMd5 = ReadNStringToken(engine, false, cancellationToken);
                token           = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentDisposition = ParseContentDisposition(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentLanguage = ParseContentLanguage(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentLocation = ParseContentLocation(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                SkipBodyExtensions(engine, cancellationToken);
            }

            // read the ')'
            token = engine.ReadToken(cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
            {
                throw ImapEngine.UnexpectedToken(token, false);
            }

            return(body);
        }
Beispiel #8
0
        public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken)
        {
            var token = engine.ReadToken (cancellationToken);

            if (token.Type == ImapTokenType.Nil)
                return null;

            if (token.Type != ImapTokenType.OpenParen)
                throw ImapEngine.UnexpectedToken (token, false);

            token = engine.PeekToken (cancellationToken);

            if (token.Type == ImapTokenType.OpenParen)
                return ParseMultipart (engine, path, cancellationToken);

            var type = ParseContentType (engine, cancellationToken);
            var id = ReadNStringToken (engine, false, cancellationToken);
            var desc = ReadNStringToken (engine, true, cancellationToken);
            // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers...
            var enc = ReadNStringToken (engine, false, cancellationToken);
            var octets = ReadNumber (engine, cancellationToken);
            BodyPartBasic body;

            if (type.Matches ("message", "rfc822")) {
                var mesg = new BodyPartMessage ();

                // Note: GMail's support for message/rfc822 body parts is broken. Essentially,
                // GMail treats message/rfc822 parts as if they were basic body parts.
                //
                // For examples, see issue #32 and issue #59.
                //
                // The workaround is to check for the expected '(' signifying an envelope token.
                // If we do not get an '(', then we are likely looking at the Content-MD5 token
                // which gets handled below.
                token = engine.PeekToken (cancellationToken);

                if (!engine.IsGMail || token.Type == ImapTokenType.OpenParen) {
                    mesg.Envelope = ParseEnvelope (engine, cancellationToken);
                    mesg.Body = ParseBody (engine, path, cancellationToken);
                    mesg.Lines = ReadNumber (engine, cancellationToken);
                }

                body = mesg;
            } else if (type.Matches ("text", "*")) {
                var text = new BodyPartText ();
                text.Lines = ReadNumber (engine, cancellationToken);
                body = text;
            } else {
                body = new BodyPartBasic ();
            }

            body.ContentTransferEncoding = enc;
            body.ContentDescription = desc;
            body.PartSpecifier = path;
            body.ContentType = type;
            body.ContentId = id;
            body.Octets = octets;

            // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')'
            token = engine.PeekToken (cancellationToken);

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentMd5 = ReadNStringToken (engine, false, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentDisposition = ParseContentDisposition (engine, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentLanguage = ParseContentLanguage (engine, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen) {
                body.ContentLocation = ParseContentLocation (engine, cancellationToken);
                token = engine.PeekToken (cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
                SkipBodyExtensions (engine, cancellationToken);

            // read the ')'
            token = engine.ReadToken (cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
                throw ImapEngine.UnexpectedToken (token, false);

            return body;
        }
Beispiel #9
0
		/// <summary>
		/// Visit the message/rfc822 or message/news MIME entity.
		/// </summary>
		/// <remarks>
		/// Visits the message/rfc822 or message/news MIME entity.
		/// </remarks>
		/// <param name="entity">The message/rfc822 or message/news body part.</param>
		protected internal virtual void VisitBodyPartMessage (BodyPartMessage entity)
		{
			VisitBodyPartBasic (entity);
			VisitMessage (entity.Body);
		}
Beispiel #10
0
        public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken)
        {
            var token = engine.ReadToken(cancellationToken);

            if (token.Type == ImapTokenType.Nil)
            {
                return(null);
            }

            if (token.Type != ImapTokenType.OpenParen)
            {
                throw ImapEngine.UnexpectedToken(token, false);
            }

            token = engine.PeekToken(cancellationToken);

            if (token.Type == ImapTokenType.OpenParen)
            {
                return(ParseMultipart(engine, path, cancellationToken));
            }

            var type = ParseContentType(engine, cancellationToken);
            var id   = ReadNStringToken(engine, false, cancellationToken);
            var desc = ReadNStringToken(engine, true, cancellationToken);
            // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers...
            var           enc    = ReadNStringToken(engine, false, cancellationToken);
            var           octets = ReadNumber(engine, cancellationToken);
            BodyPartBasic body;

            if (type.Matches("message", "rfc822"))
            {
                var mesg = new BodyPartMessage();

                // Note: GMail (and potentially other IMAP servers) will send body-part-basic
                // expressions instead of body-part-msg expressions when they encounter
                // message/rfc822 MIME parts that are illegally encoded using base64 (or
                // quoted-printable?). According to rfc3501, IMAP servers are REQUIRED to
                // send body-part-msg expressions for message/rfc822 parts, however, it is
                // understandable why GMail (and other IMAP servers?) do what they do in this
                // particular case.
                //
                // For examples, see issue #32 and issue #59.
                //
                // The workaround is to check for the expected '(' signifying an envelope token.
                // If we do not get an '(', then we are likely looking at the Content-MD5 token
                // which gets handled below.
                token = engine.PeekToken(cancellationToken);

                if (token.Type == ImapTokenType.OpenParen)
                {
                    mesg.Envelope = ParseEnvelope(engine, cancellationToken);
                    mesg.Body     = ParseBody(engine, path, cancellationToken);
                    mesg.Lines    = ReadNumber(engine, cancellationToken);
                }

                body = mesg;
            }
            else if (type.Matches("text", "*"))
            {
                var text = new BodyPartText();
                text.Lines = ReadNumber(engine, cancellationToken);
                body       = text;
            }
            else
            {
                body = new BodyPartBasic();
            }

            body.ContentTransferEncoding = enc;
            body.ContentDescription      = desc;
            body.PartSpecifier           = path;
            body.ContentType             = type;
            body.ContentId = id;
            body.Octets    = octets;

            // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')'
            token = engine.PeekToken(cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentMd5 = ReadNStringToken(engine, false, cancellationToken);
                token           = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentDisposition = ParseContentDisposition(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentLanguage = ParseContentLanguage(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                body.ContentLocation = ParseContentLocation(engine, cancellationToken);
                token = engine.PeekToken(cancellationToken);
            }

            if (token.Type != ImapTokenType.CloseParen)
            {
                SkipBodyExtensions(engine, cancellationToken);
            }

            // read the ')'
            token = engine.ReadToken(cancellationToken);

            if (token.Type != ImapTokenType.CloseParen)
            {
                throw ImapEngine.UnexpectedToken(token, false);
            }

            return(body);
        }