A basic message body part.
Represents any message body part that is not a multipart, message/rfc822 part, or a text part.
Inheritance: BodyPart
コード例 #1
0
        static bool TryParse(string text, ref int index, string path, out BodyPart part)
        {
            ContentDisposition disposition;
            ContentType        contentType;

            string[] array;
            string   nstring;
            Uri      location;
            uint     number;

            part = null;

            while (index < text.Length && text[index] == ' ')
            {
                index++;
            }

            if (index >= text.Length || text[index] != '(')
            {
                if (index + 3 <= text.Length && text.Substring(index, 3) == "NIL")
                {
                    index += 3;
                    return(true);
                }

                return(false);
            }

            index++;

            if (index >= text.Length)
            {
                return(false);
            }

            if (text[index] == '(')
            {
                var prefix    = path.Length > 0 ? path + "." : string.Empty;
                var multipart = new BodyPartMultipart();
                IList <BodyPart> children;

                if (!TryParse(text, ref index, prefix, out children))
                {
                    return(false);
                }

                foreach (var child in children)
                {
                    multipart.BodyParts.Add(child);
                }

                if (!TryParse(text, ref index, true, out contentType))
                {
                    return(false);
                }

                multipart.ContentType = contentType;

                if (!TryParse(text, ref index, out disposition))
                {
                    return(false);
                }

                multipart.ContentDisposition = disposition;

                if (!TryParse(text, ref index, out array))
                {
                    return(false);
                }

                multipart.ContentLanguage = array;

                if (!TryParse(text, ref index, out location))
                {
                    return(false);
                }

                multipart.ContentLocation = location;

                part = multipart;
            }
            else
            {
                BodyPartMessage message = null;
                BodyPartText    txt     = null;
                BodyPartBasic   basic;

                if (!TryParse(text, ref index, false, out contentType))
                {
                    return(false);
                }

                if (contentType.IsMimeType("message", "rfc822"))
                {
                    basic = message = new BodyPartMessage();
                }
                else if (contentType.IsMimeType("text", "*"))
                {
                    basic = txt = new BodyPartText();
                }
                else
                {
                    basic = new BodyPartBasic();
                }

                basic.ContentType = contentType;

                if (!TryParse(text, ref index, out nstring))
                {
                    return(false);
                }

                basic.ContentId = nstring;

                if (!TryParse(text, ref index, out nstring))
                {
                    return(false);
                }

                basic.ContentDescription = nstring;

                if (!TryParse(text, ref index, out nstring))
                {
                    return(false);
                }

                basic.ContentTransferEncoding = nstring;

                if (!TryParse(text, ref index, out number))
                {
                    return(false);
                }

                basic.Octets = number;

                if (!TryParse(text, ref index, out nstring))
                {
                    return(false);
                }

                basic.ContentMd5 = nstring;

                if (!TryParse(text, ref index, out disposition))
                {
                    return(false);
                }

                basic.ContentDisposition = disposition;

                if (!TryParse(text, ref index, out array))
                {
                    return(false);
                }

                basic.ContentLanguage = array;

                if (!TryParse(text, ref index, out location))
                {
                    return(false);
                }

                basic.ContentLocation = location;

                if (message != null)
                {
                    Envelope envelope;
                    BodyPart body;

                    if (!Envelope.TryParse(text, ref index, out envelope))
                    {
                        return(false);
                    }

                    message.Envelope = envelope;

                    if (!TryParse(text, ref index, path, out body))
                    {
                        return(false);
                    }

                    message.Body = body;

                    if (!TryParse(text, ref index, out number))
                    {
                        return(false);
                    }

                    message.Lines = number;
                }
                else if (txt != null)
                {
                    if (!TryParse(text, ref index, out number))
                    {
                        return(false);
                    }

                    txt.Lines = number;
                }

                part = basic;
            }

            part.PartSpecifier = path;

            if (index >= text.Length || text[index] != ')')
            {
                return(false);
            }

            index++;

            return(true);
        }
コード例 #2
0
ファイル: BodyPart.cs プロジェクト: BehnamEmamian/MailKit
		static bool TryParse (string text, ref int index, string path, out BodyPart part)
		{
			ContentDisposition disposition;
			ContentType contentType;
			string[] array;
			string nstring;
			Uri location;
			uint number;

			part = null;

			while (index < text.Length && text[index] == ' ')
				index++;

			if (index >= text.Length || text[index] != '(') {
				if (index + 3 <= text.Length && text.Substring (index, 3) == "NIL") {
					index += 3;
					return true;
				}

				return false;
			}

			index++;

			if (index >= text.Length)
				return false;

			if (text[index] == '(') {
				var prefix = path.Length > 0 ? path + "." : string.Empty;
				var multipart = new BodyPartMultipart ();
				IList<BodyPart> children;

				if (!TryParse (text, ref index, prefix, out children))
					return false;

				foreach (var child in children)
					multipart.BodyParts.Add (child);

				if (!TryParse (text, ref index, true, out contentType))
					return false;

				multipart.ContentType = contentType;

				if (!TryParse (text, ref index, out disposition))
					return false;

				multipart.ContentDisposition = disposition;

				if (!TryParse (text, ref index, out array))
					return false;

				multipart.ContentLanguage = array;

				if (!TryParse (text, ref index, out location))
					return false;

				multipart.ContentLocation = location;

				part = multipart;
			} else {
				BodyPartMessage message = null;
				BodyPartText txt = null;
				BodyPartBasic basic;

				if (!TryParse (text, ref index, false, out contentType))
					return false;

				if (contentType.Matches ("message", "rfc822"))
					basic = message = new BodyPartMessage ();
				else if (contentType.Matches ("text", "*"))
					basic = txt = new BodyPartText ();
				else
					basic = new BodyPartBasic ();

				basic.ContentType = contentType;

				if (!TryParse (text, ref index, out nstring))
					return false;

				basic.ContentId = nstring;

				if (!TryParse (text, ref index, out nstring))
					return false;

				basic.ContentDescription = nstring;

				if (!TryParse (text, ref index, out nstring))
					return false;

				basic.ContentTransferEncoding = nstring;

				if (!TryParse (text, ref index, out number))
					return false;

				basic.Octets = number;

				if (!TryParse (text, ref index, out nstring))
					return false;

				basic.ContentMd5 = nstring;

				if (!TryParse (text, ref index, out disposition))
					return false;

				basic.ContentDisposition = disposition;

				if (!TryParse (text, ref index, out array))
					return false;

				basic.ContentLanguage = array;

				if (!TryParse (text, ref index, out location))
					return false;

				basic.ContentLocation = location;

				if (message != null) {
					Envelope envelope;
					BodyPart body;

					if (!Envelope.TryParse (text, ref index, out envelope))
						return false;

					message.Envelope = envelope;

					if (!TryParse (text, ref index, path, out body))
						return false;

					message.Body = body;

					if (!TryParse (text, ref index, out number))
						return false;

					message.Lines = number;
				} else if (txt != null) {
					if (!TryParse (text, ref index, out number))
						return false;

					txt.Lines = number;
				}

				part = basic;
			}

			part.PartSpecifier = path;

			if (index >= text.Length || text[index] != ')')
				return false;

			index++;

			return true;
		}