Пример #1
0
        /// <summary>
        /// Add a line to the decoder
        /// </summary>
        /// <param name="line">A string containing a message line</param>
        /// <returns>The method return a positive value if the Attachment need more lines. Otherwise it return 0 to indicate the end of attachment body</returns>
        public int AddLine(string line)
        {
            if (m_state == decoder_state.header)
            {
                if (line.TrimStart(' ') != "\r\n")
                {
                    // store the line
                    m_headerBuilder.Append(line);

                    // check if there is field on the current line
                    int r = line.IndexOf(":");
                    if (r != -1 && line[0] != ' ' && line[0] != '\t')
                    {
                        // extract header field and value
                        string field_name  = line.Substring(0, r);
                        string field_value = line.Substring(r + 1);

                        // store header
                        m_headers.Add(field_name, field_value);

                        // store last field name
                        m_last_field = field_name;
                    }
                    else if (m_last_field != null)
                    {
                        m_headers[m_last_field] += line;
                    }
                }
                else
                {
                    // initialize next state
                    m_state = decoder_state.body;

                    // iniitialize body decodig
                    m_bodyBuilder = new StringBuilder();

                    // store headers
                    m_header = m_headerBuilder.ToString();
                    m_headerBuilder.Length = 0;
                    m_headerBuilder        = null;


                    // check multipart value
                    if (IsMultipart())
                    {
                        m_multipart = true;
                        m_boundary  = HeaderBoundary();
                    }
                }
            }
            else if (m_state == decoder_state.body)
            {
                if (line.IndexOf(m_boundary) != -1 && Multipart)
                {
                    // start a new attachment
                    m_state           = decoder_state.attachment;
                    m_last_attachment = new MimeAttachment();
                    m_last_attachment.AddLine(line);
                    m_body        = m_bodyBuilder.ToString();
                    m_bodyBuilder = null;
                }
                else
                {
                    m_bodyBuilder.Append(line);
                }
            }
            else if (m_state == decoder_state.attachment)
            {
                if (m_last_attachment != null)
                {
                    if (line.IndexOf(m_boundary) != -1)
                    {
                        // attachement end
                        m_last_attachment.CheckAttachmentEnd();
                        if (m_last_attachment.Headers.Count > 0)
                        {
                            m_attachments.Add(m_last_attachment);
                        }
                        m_last_attachment = new MimeAttachment();
                        m_last_attachment.AddLine(line);
                    }
                    else
                    {
                        m_last_attachment.AddLine(line);
                    }
                }
                else
                {
                    if (line.IndexOf(m_boundary) != -1)
                    {
                        m_last_attachment = new MimeAttachment();
                        m_last_attachment.AddLine(line);
                    }
                }
            }
            return(1);
        }
Пример #2
0
        /// <summary>
        /// Add a message line to the decoder
        /// </summary>
        /// <param name="line">A string containing a message line</param>
        public void AddLine(string line)
        {
            m_lines_treated++;  // one line more
            if (m_state == decoder_state.header)
            {
                if (line != "\r\n")
                {
                    if (m_headerBuilder == null)
                    {
                        m_headerBuilder = new StringBuilder();
                    }

                    m_headerBuilder.Append(line);
                    int r = line.IndexOf(":");
                    if (r != -1 && line[0] != ' ' && line[0] != '\t')
                    {
                        string field_name  = line.Substring(0, r);
                        string field_value = line.Substring(r + 1);

                        AddHeader(field_name, field_value);

                        m_last_field = field_name;
                    }
                    else if (m_last_field != null)
                    {
                        m_headers[m_last_field] += line;
                    }
                }
                else
                {
                    // decode header
                    m_header        = m_headerBuilder.ToString();
                    m_headerBuilder = null;
                    m_boundary      = HeaderBoundary();
                    m_multipart     = IsMultipart();
                    m_state         = decoder_state.body;
                    m_bodyBuilder   = new StringBuilder();
                }
            }
            else if (m_state == decoder_state.body)
            {
                if (line != "\r\n")
                {
                    if (line.IndexOf(m_boundary) != -1 && m_multipart)
                    {
                        // end last attachment
                        if (m_last_attachment != null)
                        {
                            m_last_attachment.CheckAttachmentEnd();
                            if (m_last_attachment.Body.Length > 0 ||
                                m_last_attachment.Attachments.Count > 0 ||
                                m_last_attachment.Header.Length > 0)
                            {
                                m_attachments.Add(m_last_attachment);
                                m_last_attachment = null;
                            }
                        }
                        // start a new attachment
                        m_state = decoder_state.attachment;
                        m_body  = m_bodyBuilder.ToString();
                        m_bodyBuilder.Length = 0;
                        m_bodyBuilder        = null;
                        m_last_attachment    = new MimeAttachment();
                        m_last_attachment.AddLine(line);
                    }
                    else
                    {
                        m_bodyBuilder.Append(line);     // append the line to the message body
                    }
                }
            }
            else if (m_state == decoder_state.attachment)
            {
                if (m_last_attachment != null)
                {
                    if (line.IndexOf(m_boundary) != -1)
                    {
                        // attachement end
                        m_last_attachment.CheckAttachmentEnd();
                        if (m_last_attachment.Header.Length > 0 ||
                            m_last_attachment.Body.Length > 0 ||
                            m_last_attachment.Attachments.Count > 0)
                        {
                            m_attachments.Add(m_last_attachment);
                        }
                        //m_attachments.Add (m_last_attachment);
                        m_last_attachment = new MimeAttachment();
                        m_last_attachment.AddLine(line);
                    }
                    else
                    {
                        m_last_attachment.AddLine(line); // append the line to current attachment
                    }
                }
                else
                {
                    if (line.IndexOf(m_boundary) != -1)
                    {
                        m_last_attachment = new MimeAttachment();
                        m_last_attachment.AddLine(line);
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public MimeAttachment()
 {
     m_state     = decoder_state.header;
     m_multipart = false;
 }