Exemplo n.º 1
0
        private string ParseMessage(ref Stream dataStream, ref Message message, IList <RFC822.Field> fields)
        {
            foreach (RFC822.Field contentField in fields)
            {
                if (contentField is ContentTypeField)
                {
                    ContentTypeField contentTypeField = contentField as ContentTypeField;

                    if (m_CompositePattern.RegularExpression.IsMatch(contentTypeField.Type))
                    {
                        IMultipartEntity e = message as IMultipartEntity;
                        e.Delimiter = ReadDelimiter(ref contentTypeField);
                        return(ReadCompositeEntity(ref dataStream, ref e));
                    }

                    if (m_DiscretePattern.RegularExpression.IsMatch(contentTypeField.Type))
                    {
                        IEntity e = message as IEntity;
                        message.BodyParts.Add(e);// This is a message witch body lies within its own entity
                        return(ReadDiscreteEntity(ref dataStream, ref e));
                    }
                }
            }
            return(string.Empty);
        }
Exemplo n.º 2
0
        public static IAttachment AsAttachment(this IEntity entity)
        {
            ContentDispositionField dispositionField = entity.FindField <ContentDispositionField>();
            ContentTypeField        contentTypeField = entity.FindField <ContentTypeField>();

            if (dispositionField == null && contentTypeField == null)
            {
                return(new NullAttachment());
            }

            if (contentTypeField != null)
            {
                IAttachment attachment = new Attachment();

                if (dispositionField != null)
                {
                    attachment.Disposition = dispositionField.Disposition;
                }

                attachment.Name    = entity.GetAttachmentName();
                attachment.Data    = entity.Body;
                attachment.Type    = contentTypeField.Type;
                attachment.SubType = contentTypeField.SubType;
                return(attachment);
            }

            return(new NullAttachment());
        }
Exemplo n.º 3
0
 private string ReadDelimiter(ref ContentTypeField contentTypeField)
 {
     if (contentTypeField.Parameters["boundary"] != null)
     {
         return(contentTypeField.Parameters["boundary"]);
     }
     return(string.Empty);
 }
Exemplo n.º 4
0
 public SMIMETypeField(ContentTypeField field)
 {
     Type = field.Type;
     SubType = field.SubType;
     Body = field.Body;
     Name = field.Name;
     Parameters = field.Parameters;
 }
Exemplo n.º 5
0
        private string CreateEntity(ref Stream dataStream, ref IMultipartEntity parent, out IEntity entity)
        {
            entity = null;
            IList <RFC822.Field> fields;
            int cause = ParseFields(ref dataStream, out fields);

            if (cause >= 0)
            {
                foreach (RFC822.Field contentField in fields)
                {
                    if (contentField is ContentTypeField)
                    {
                        ContentTypeField contentTypeField = contentField as ContentTypeField;

                        if (m_CompositePattern.RegularExpression.IsMatch(contentTypeField.Type))
                        {
                            MultipartEntity mEntity = new MultipartEntity();
                            mEntity.Fields = fields;
                            entity         = mEntity;
                            entity.Parent  = parent;
                            parent.BodyParts.Add(entity);

                            if (Regex.IsMatch(contentTypeField.Type, "(?i)message") &&
                                Regex.IsMatch(contentTypeField.SubType, "(?i)rfc822"))
                            {
                                Message message = new Message();
                                IList <RFC822.Field> messageFields;
                                cause          = ParseFields(ref dataStream, out messageFields);
                                message.Fields = messageFields;
                                mEntity.BodyParts.Add(message);
                                message.Parent = mEntity;
                                if (cause > 0)
                                {
                                    return(ParseMessage(ref dataStream, ref message, messageFields));
                                }
                                break;
                            }

                            mEntity.Delimiter = ReadDelimiter(ref contentTypeField);
                            return(parent.Delimiter);
                        }

                        if (m_DiscretePattern.RegularExpression.IsMatch(contentTypeField.Type))
                        {
                            entity        = new Entity();
                            entity.Fields = fields;
                            entity.Parent = parent;
                            parent.BodyParts.Add(entity);
                            return(parent.Delimiter);
                        }
                    }
                }
            }
            return(string.Empty);
        }
Exemplo n.º 6
0
        private void LoadBody(IEntity parent)
        {
            if (parent is MultipartEntity)
            {
                MultipartEntity mpe = parent as MultipartEntity;

                mpe.BodyParts
                .Where(child => child != parent && !(child is Message))
                .ToList()
                .ForEach(LoadBody);
            }



            ContentTypeField        contentTypeField        = null;
            ContentDispositionField contentDispositionField = null;

            foreach (RFC822.Field field in parent.Fields)
            {
                if (field is ContentTypeField)
                {
                    ContentTypeField contentField = field as ContentTypeField;
                    if (contentField.Type.ToLower().Equals("text"))
                    {
                        contentTypeField = contentField;
                    }
                }

                if (field is ContentDispositionField)
                {
                    contentDispositionField = field as ContentDispositionField;
                }
            }

            if (contentTypeField != null &&
                (contentDispositionField == null ||
                 contentDispositionField.Disposition.ToLower().Equals("inline")))
            {
                string text = string.Empty;
                if (parent.Encoding != null)
                {
                    if (parent.Body != null)
                    {
                        text = parent.Encoding.GetString(parent.Body);
                    }
                }
                else
                {
                    text = ToString(parent.Body);
                }

                string key = GetKey(contentTypeField);
                m_Body.Add(key, text);
            }
        }
Exemplo n.º 7
0
        private string GetKey(ContentTypeField contentTypeField)
        {
            string key = string.Format("{0}/{1}", contentTypeField.Type, contentTypeField.SubType);
            int    i   = 0;

            while (m_Body.Keys.Contains(key))
            {
                key = string.Format("{0}/{1}-{2}", contentTypeField.Type, contentTypeField.SubType, i);
                i++;
            }
            return(key);
        }
Exemplo n.º 8
0
 private void LoadEncoding()
 {
     foreach (RFC822.Field field in Fields)
     {
         if (field is RFC2045.ContentTypeField)
         {
             ContentTypeField contentTypeField = field as ContentTypeField;
             if (contentTypeField.Parameters["charset"] != null)
             {
                 m_Encoding = Encoding.GetEncoding(contentTypeField.Parameters["charset"]);
             }
         }
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Parses the any content type fields found within argument fieldstring.
        /// </summary>
        /// <param name="fields">The target list for the parsed fields.</param>
        /// <param name="fieldString">The source field string</param>
        public override void Parse(ref IList <RFC822.Field> fields, ref string fieldString)
        {
            CompilePattern();

            if (fields.Count == 0)
            {
                DecoratedFieldParser.Parse(ref fields, ref fieldString);
            }

            MatchCollection matches = m_ContentPattern.RegularExpression.Matches(fieldString);

            foreach (Match match in matches)
            {
                ContentTypeField tmpContent = new ContentTypeField();
                tmpContent.Name = "Content-Type";
                MatchCollection parameters;
                string          key, val;

                Match tmpMatch = Regex.Match(match.Value, ":.+");

                tmpContent.Body    = tmpMatch.Value.TrimStart(new char[] { ':' });
                tmpMatch           = m_TypePattern.RegularExpression.Match(match.Value);
                tmpContent.Type    = tmpMatch.Value;
                tmpMatch           = SubTypePattern.RegularExpression.Match(match.Value);
                tmpContent.SubType = tmpMatch.Value;
                parameters         = m_ParameterPattern.RegularExpression.Matches(match.Value);
                foreach (Match m in parameters)
                {
                    tmpMatch = Regex.Match(m.Value, m_TokenPattern.TextPattern + "=");
                    key      = tmpMatch.Value.TrimEnd(new char[] { '=' });
                    tmpMatch = Regex.Match(m.Value, "(?<==)" + m_ValuePattern.TextPattern);
                    val      = tmpMatch.Value.Trim(new char[] { '\\', '"' });
                    AddParamerter(tmpContent.Parameters, key, val);
                }

                fields.Add(tmpContent);
            }
        }
Exemplo n.º 10
0
 private string ReadDelimiter(ref ContentTypeField contentTypeField)
 {
     if (contentTypeField.Parameters["boundary"] != null)
     {
         return contentTypeField.Parameters["boundary"];
     }
     return string.Empty;
 }
Exemplo n.º 11
0
 private string GetKey(ContentTypeField contentTypeField)
 {
     string key = string.Format("{0}/{1}", contentTypeField.Type, contentTypeField.SubType);
     int i = 0;
     while (m_Body.Keys.Contains(key))
     {
         key = string.Format("{0}/{1}-{2}", contentTypeField.Type, contentTypeField.SubType, i);
         i++;
     }
     return key;
 }
Exemplo n.º 12
0
        /// <summary>
        /// Parses the any content type fields found within argument fieldstring.
        /// </summary>
        /// <param name="fields">The target list for the parsed fields.</param>
        /// <param name="fieldString">The source field string</param>
        public override void Parse(ref IList<RFC822.Field> fields, ref string fieldString)
        {
            CompilePattern();

            if (fields.Count == 0)
                DecoratedFieldParser.Parse(ref fields, ref fieldString);

            MatchCollection matches = m_ContentPattern.RegularExpression.Matches(fieldString);

            foreach (Match match in matches)
            {
                ContentTypeField tmpContent = new ContentTypeField();
                tmpContent.Name = "Content-Type";
                MatchCollection parameters;
                string key, val;

                Match tmpMatch = Regex.Match(match.Value, ":.+");

                tmpContent.Body = tmpMatch.Value.TrimStart(new char[] { ':' });
                tmpMatch = m_TypePattern.RegularExpression.Match(match.Value);
                tmpContent.Type = tmpMatch.Value;
                tmpMatch = SubTypePattern.RegularExpression.Match(match.Value);
                tmpContent.SubType = tmpMatch.Value;
                parameters = m_ParameterPattern.RegularExpression.Matches(match.Value);
                foreach (Match m in parameters)
                {
                    tmpMatch = Regex.Match(m.Value, m_TokenPattern.TextPattern + "=");
                    key = tmpMatch.Value.TrimEnd(new char[] { '=' });
                    tmpMatch = Regex.Match(m.Value, "(?<==)" + m_ValuePattern.TextPattern);
                    val = tmpMatch.Value.Trim(new char[] { '\\', '"' });
                    AddParamerter(tmpContent.Parameters, key, val);
                }

                fields.Add(tmpContent);
            }
        }