/// <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 = new MIME_h_ContentType("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 = new MIME_h_ContentType(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(conentTypeParameters);
                    while (contentTypeParamReader.Available > 0)
                    {
                        string parameterName  = contentTypeParamReader.ReadWord();
                        string parameterValue = MIME_Encoding_EncodedWord.DecodeS(contentTypeParamReader.ReadWord());

                        m_pContentType.Parameters[parameterName] = parameterValue;
                    }
                }
                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 = 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 (string.Equals(this.ContentType.TypeWithSubype, MIME_MediaTypes.Message.rfc822, StringComparison.InvariantCultureIgnoreCase))
                {
                    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>
 /// Default constructor.
 /// </summary>
 public IMAP_BODY()
 {
     m_pMainEntity = new IMAP_BODY_Entity();
 }
        /// <summary>
        /// Gets mime entities, including nested entries. 
        /// </summary>
        /// <param name="entities"></param>
        /// <param name="allEntries"></param>
        private void GetEntities(IMAP_BODY_Entity[] entities,List<IMAP_BODY_Entity> allEntries)
        {
            if(entities != null){
                foreach(IMAP_BODY_Entity ent in entities){
                    allEntries.Add(ent);

                    // Add child entities, if any
                    if(ent.ChildEntities.Length > 0){
                        GetEntities(ent.ChildEntities,allEntries);
                    }
                }
            }
        }
 /// <summary>
 /// Parses IMAP BODYSTRUCTURE from body structure string.
 /// </summary>
 /// <param name="bodyStructureString">Body structure string</param>
 public void Parse(string bodyStructureString)
 {
     m_pMainEntity = new IMAP_BODY_Entity();
     m_pMainEntity.Parse(bodyStructureString);
 }
Exemple #5
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 public IMAP_BODY()
 {
     m_pMainEntity = new IMAP_BODY_Entity();
 }
        /// <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(this.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);
                    }
                }
            }
        }
Exemple #7
0
        /// <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 = new MIME_h_ContentType("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 = new MIME_h_ContentType(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(conentTypeParameters);
                    while(contentTypeParamReader.Available > 0){
                        string parameterName  = contentTypeParamReader.ReadWord();
                        string parameterValue = MIME_Encoding_EncodedWord.DecodeS(contentTypeParamReader.ReadWord());

                        m_pContentType.Parameters[parameterName] = parameterValue;
                    }
                }
                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 = 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(string.Equals(this.ContentType.TypeWithSubype,MIME_MediaTypes.Message.rfc822,StringComparison.InvariantCultureIgnoreCase)){
                    r.ReadToFirstChar();
 
                    // envelope
                    if(r.StartsWith("(")){
                        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);
                    }
                }                
            }            
        }