Пример #1
0
        /// <summary>
        /// Converts MediaType_enum to string. NOTE: Returns null for MediaType_enum.NotSpecified.
        /// </summary>
        /// <param name="mediaType">MediaType_enum value to convert.</param>
        /// <returns></returns>
        public static string MediaTypeToString(MediaType_enum mediaType)
        {
            //--- Text/xxx --------------------------------//
            if (mediaType == MediaType_enum.Text_plain)
            {
                return("text/plain");
            }
            else if (mediaType == MediaType_enum.Text_html)
            {
                return("text/html");
            }
            else if (mediaType == MediaType_enum.Text_xml)
            {
                return("text/xml");
            }
            else if (mediaType == MediaType_enum.Text_rtf)
            {
                return("text/rtf");
            }
            else if (mediaType == MediaType_enum.Text)
            {
                return("text");
            }
            //---------------------------------------------//

            //--- Image/xxx -------------------------------//
            else if (mediaType == MediaType_enum.Image_gif)
            {
                return("image/gif");
            }
            else if (mediaType == MediaType_enum.Image_tiff)
            {
                return("image/tiff");
            }
            else if (mediaType == MediaType_enum.Image_jpeg)
            {
                return("image/jpeg");
            }
            else if (mediaType == MediaType_enum.Image)
            {
                return("image");
            }
            //---------------------------------------------//

            //--- Audio/xxx -------------------------------//
            else if (mediaType == MediaType_enum.Audio)
            {
                return("audio");
            }
            //---------------------------------------------//

            //--- Video/xxx -------------------------------//
            else if (mediaType == MediaType_enum.Video)
            {
                return("video");
            }
            //---------------------------------------------//

            //--- Application/xxx -------------------------//
            else if (mediaType == MediaType_enum.Application_octet_stream)
            {
                return("application/octet-stream");
            }
            else if (mediaType == MediaType_enum.Application)
            {
                return("application");
            }
            //---------------------------------------------//

            //--- Multipart/xxx ---------------------------//
            else if (mediaType == MediaType_enum.Multipart_mixed)
            {
                return("multipart/mixed");
            }
            else if (mediaType == MediaType_enum.Multipart_alternative)
            {
                return("multipart/alternative");
            }
            else if (mediaType == MediaType_enum.Multipart_parallel)
            {
                return("multipart/parallel");
            }
            else if (mediaType == MediaType_enum.Multipart_related)
            {
                return("multipart/related");
            }
            else if (mediaType == MediaType_enum.Multipart_signed)
            {
                return("multipart/signed");
            }
            else if (mediaType == MediaType_enum.Multipart)
            {
                return("multipart");
            }
            //---------------------------------------------//

            //--- Message/xxx -----------------------------//
            else if (mediaType == MediaType_enum.Message_rfc822)
            {
                return("message/rfc822");
            }
            else if (mediaType == MediaType_enum.Message)
            {
                return("message");
            }
            //---------------------------------------------//

            else if (mediaType == MediaType_enum.Unknown)
            {
                return("unknown");
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Converts MediaType_enum to string. NOTE: Returns null for MediaType_enum.NotSpecified.
        /// </summary>
        /// <param name="mediaType">MediaType_enum value to convert.</param>
        /// <returns></returns>
        public static string MediaTypeToString(MediaType_enum mediaType)
        {
            //--- Text/xxx --------------------------------//
            if(mediaType == MediaType_enum.Text_plain){
                return "text/plain";
            }
            else if(mediaType == MediaType_enum.Text_html){
                return "text/html";
            }
            else if(mediaType == MediaType_enum.Text_xml){
                return "text/xml";
            }
            else if(mediaType == MediaType_enum.Text_rtf){
                return "text/rtf";
            }
            else if(mediaType == MediaType_enum.Text){
                return "text";
            }
            //---------------------------------------------//

            //--- Image/xxx -------------------------------//
            else if(mediaType == MediaType_enum.Image_gif){
                return "image/gif";
            }
            else if(mediaType == MediaType_enum.Image_tiff){
                return "image/tiff";
            }
            else if(mediaType == MediaType_enum.Image_jpeg){
                return "image/jpeg";
            }
            else if(mediaType == MediaType_enum.Image){
                return "image";
            }
            //---------------------------------------------//

            //--- Audio/xxx -------------------------------//
            else if(mediaType == MediaType_enum.Audio){
                return "audio";
            }
            //---------------------------------------------//

            //--- Video/xxx -------------------------------//
            else if(mediaType == MediaType_enum.Video){
                return "video";
            }
            //---------------------------------------------//

            //--- Application/xxx -------------------------//
            else if(mediaType == MediaType_enum.Application_octet_stream){
                return "application/octet-stream";
            }
            else if(mediaType == MediaType_enum.Application){
                return "application";
            }
            //---------------------------------------------//

            //--- Multipart/xxx ---------------------------//
            else if(mediaType == MediaType_enum.Multipart_mixed){
                return "multipart/mixed";
            }
            else if(mediaType == MediaType_enum.Multipart_alternative){
                return "multipart/alternative";
            }
            else if(mediaType == MediaType_enum.Multipart_parallel){
                return "multipart/parallel";
            }
            else if(mediaType == MediaType_enum.Multipart_related){
                return "multipart/related";
            }
            else if(mediaType == MediaType_enum.Multipart_signed){
                return "multipart/signed";
            }
            else if(mediaType == MediaType_enum.Multipart){
                return "multipart";
            }
            //---------------------------------------------//

            //--- Message/xxx -----------------------------//
            else if(mediaType == MediaType_enum.Message_rfc822){
                return "message/rfc822";
            }
            else if(mediaType == MediaType_enum.Message){
                return "message";
            }
            //---------------------------------------------//

            else if(mediaType == MediaType_enum.Unknown){
                return "unknown";
            }
            else{
                return null;
            }
        }
        /// <summary>
        /// Parses entity and it's child entities.
        /// </summary>
        internal void Parse(string text)
        {
            StringReader r = new StringReader(text);

            r.ReadToFirstChar();

            // If starts with ( then multipart/xxx, otherwise normal single part entity
            if (r.StartsWith("("))
            {
                // Entities are (entity1)(entity2)(...) <SP> ContentTypeSubType
                while (r.StartsWith("("))
                {
                    IMAP_BODY_Entity entity = new IMAP_BODY_Entity();
                    entity.Parse(r.ReadParenthesized());
                    entity.m_pParentEntity = this;
                    m_pChildEntities.Add(entity);

                    r.ReadToFirstChar();
                }

                // Read multipart values. (nestedMimeEntries) contentTypeSubMediaType
                string contentTypeSubMediaType = r.ReadWord();

                m_pContentType = MimeUtils.ParseMediaType("multipart/" + contentTypeSubMediaType);
            }
            else
            {
                // Basic fields for non-multipart
                // contentTypeMainMediaType contentTypeSubMediaType (conentTypeParameters) contentID contentDescription contentEncoding contentSize [envelope] [contentLine]

                // Content-Type
                string contentTypeMainMediaType = r.ReadWord();
                string contentTypeSubMediaType  = r.ReadWord();
                if (contentTypeMainMediaType.ToUpper() != "NIL" && contentTypeSubMediaType.ToUpper() != "NIL")
                {
                    m_pContentType =
                        MimeUtils.ParseMediaType(contentTypeMainMediaType + "/" + contentTypeSubMediaType);
                }

                // Content-Type header field parameters
                // Parameters syntax: "name" <SP> "value" <SP> "name" <SP> "value" <SP> ... .
                r.ReadToFirstChar();
                string conentTypeParameters = "NIL";
                if (r.StartsWith("("))
                {
                    conentTypeParameters = r.ReadParenthesized();

                    StringReader contentTypeParamReader =
                        new StringReader(MimeUtils.DecodeWords(conentTypeParameters));
                    List <HeaderFieldParameter> parameters = new List <HeaderFieldParameter>();
                    while (contentTypeParamReader.Available > 0)
                    {
                        string parameterName  = contentTypeParamReader.ReadWord();
                        string parameterValue = contentTypeParamReader.ReadWord();

                        parameters.Add(new HeaderFieldParameter(parameterName, parameterValue));
                    }
                    m_pContentTypeParams = parameters.ToArray();
                }
                else
                {
                    // Skip NIL
                    r.ReadWord();
                }

                // Content-ID:
                string contentID = r.ReadWord();
                if (contentID.ToUpper() != "NIL")
                {
                    m_ContentID = contentID;
                }

                // Content-Description:
                string contentDescription = r.ReadWord();
                if (contentDescription.ToUpper() != "NIL")
                {
                    m_ContentDescription = contentDescription;
                }

                // Content-Transfer-Encoding:
                string contentEncoding = r.ReadWord();
                if (contentEncoding.ToUpper() != "NIL")
                {
                    m_ContentEncoding = MimeUtils.ParseContentTransferEncoding(contentEncoding);
                }

                // Content Encoded data size in bytes
                string contentSize = r.ReadWord();
                if (contentSize.ToUpper() != "NIL")
                {
                    m_ContentSize = Convert.ToInt32(contentSize);
                }

                // Only for ContentType message/rfc822
                if (ContentType == MediaType_enum.Message_rfc822)
                {
                    r.ReadToFirstChar();

                    // envelope
                    if (r.StartsWith("("))
                    {
                        m_pEnvelope = new IMAP_Envelope();
                        m_pEnvelope.Parse(r.ReadParenthesized());
                    }
                    else
                    {
                        // Skip NIL, ENVELOPE wasn't included
                        r.ReadWord();
                    }

                    // TODO:
                    // BODYSTRUCTURE

                    // contentLine
                }

                // Only for ContentType text/xxx
                if (contentTypeMainMediaType.ToLower() == "text")
                {
                    // contentLine
                    string contentLines = r.ReadWord();
                    if (contentLines.ToUpper() != "NIL")
                    {
                        m_ContentLines = Convert.ToInt32(contentLines);
                    }
                }
            }
        }
        /// <summary>
        /// Parses entity and it's child entities.
        /// </summary>
        internal void Parse(string text)
        {
            StringReader r = new StringReader(text);
            r.ReadToFirstChar();

            // If starts with ( then multipart/xxx, otherwise normal single part entity
            if (r.StartsWith("("))
            {
                // Entities are (entity1)(entity2)(...) <SP> ContentTypeSubType
                while (r.StartsWith("("))
                {
                    IMAP_BODY_Entity entity = new IMAP_BODY_Entity();
                    entity.Parse(r.ReadParenthesized());
                    entity.m_pParentEntity = this;
                    m_pChildEntities.Add(entity);

                    r.ReadToFirstChar();
                }

                // Read multipart values. (nestedMimeEntries) contentTypeSubMediaType
                string contentTypeSubMediaType = r.ReadWord();

                m_pContentType = MimeUtils.ParseMediaType("multipart/" + contentTypeSubMediaType);
            }
            else
            {
                // Basic fields for non-multipart
                // contentTypeMainMediaType contentTypeSubMediaType (conentTypeParameters) contentID contentDescription contentEncoding contentSize [envelope] [contentLine]

                // Content-Type
                string contentTypeMainMediaType = r.ReadWord();
                string contentTypeSubMediaType = r.ReadWord();
                if (contentTypeMainMediaType.ToUpper() != "NIL" && contentTypeSubMediaType.ToUpper() != "NIL")
                {
                    m_pContentType =
                        MimeUtils.ParseMediaType(contentTypeMainMediaType + "/" + contentTypeSubMediaType);
                }

                // Content-Type header field parameters
                // Parameters syntax: "name" <SP> "value" <SP> "name" <SP> "value" <SP> ... .
                r.ReadToFirstChar();
                string conentTypeParameters = "NIL";
                if (r.StartsWith("("))
                {
                    conentTypeParameters = r.ReadParenthesized();

                    StringReader contentTypeParamReader =
                        new StringReader(MimeUtils.DecodeWords(conentTypeParameters));
                    List<HeaderFieldParameter> parameters = new List<HeaderFieldParameter>();
                    while (contentTypeParamReader.Available > 0)
                    {
                        string parameterName = contentTypeParamReader.ReadWord();
                        string parameterValue = contentTypeParamReader.ReadWord();

                        parameters.Add(new HeaderFieldParameter(parameterName, parameterValue));
                    }
                    m_pContentTypeParams = parameters.ToArray();
                }
                else
                {
                    // Skip NIL
                    r.ReadWord();
                }

                // Content-ID:
                string contentID = r.ReadWord();
                if (contentID.ToUpper() != "NIL")
                {
                    m_ContentID = contentID;
                }

                // Content-Description:
                string contentDescription = r.ReadWord();
                if (contentDescription.ToUpper() != "NIL")
                {
                    m_ContentDescription = contentDescription;
                }

                // Content-Transfer-Encoding:
                string contentEncoding = r.ReadWord();
                if (contentEncoding.ToUpper() != "NIL")
                {
                    m_ContentEncoding = MimeUtils.ParseContentTransferEncoding(contentEncoding);
                }

                // Content Encoded data size in bytes
                string contentSize = r.ReadWord();
                if (contentSize.ToUpper() != "NIL")
                {
                    m_ContentSize = Convert.ToInt32(contentSize);
                }

                // Only for ContentType message/rfc822
                if (ContentType == MediaType_enum.Message_rfc822)
                {
                    r.ReadToFirstChar();

                    // envelope
                    if (r.StartsWith("("))
                    {
                        m_pEnvelope = new IMAP_Envelope();
                        m_pEnvelope.Parse(r.ReadParenthesized());
                    }
                    else
                    {
                        // Skip NIL, ENVELOPE wasn't included
                        r.ReadWord();
                    }

                    // TODO:
                    // BODYSTRUCTURE

                    // contentLine
                }

                // Only for ContentType text/xxx
                if (contentTypeMainMediaType.ToLower() == "text")
                {
                    // contentLine
                    string contentLines = r.ReadWord();
                    if (contentLines.ToUpper() != "NIL")
                    {
                        m_ContentLines = Convert.ToInt32(contentLines);
                    }
                }
            }
        }