示例#1
0
文件: BodyParser.cs 项目: mo5h/omeo
        private void ProcessMutipartBody()
        {
            /**
             * extract boundary from content-type
             */
            string boundary      = string.Empty;
            int    boundaryBegin = _content_type.ToLower().IndexOf("boundary=");

            if (boundaryBegin > 0)
            {
                boundaryBegin += 9;
                int boundaryEnd = _content_type.IndexOfAny(_boundaryDelimiters, boundaryBegin);
                if (boundaryEnd < 0)
                {
                    boundaryEnd = _content_type.Length;
                }
                if (boundaryEnd > boundaryBegin)
                {
                    boundary = _content_type.Substring(
                        boundaryBegin, boundaryEnd - boundaryBegin).Replace("\"", string.Empty).Trim();
                }
            }
            if (boundary.Length == 0)
            {
                _bodyBuilder.Append("Bad multipart message: the \"Content-Type\" header doesn't contain valid boundary. Message source follows as plain text.\r\n\r\n");
                _bodyBuilder.Append(_body);
                return;
            }
            boundary = "--" + boundary;
            string finishingBoundary = boundary + "--";

            string[]            lines         = _body.Split('\n');
            string              line          = string.Empty;
            int                 i             = 0;
            bool                inHeaders     = true;
            bool                isHtmlBody    = false;
            bool                isAttachment  = false;
            bool                isInline      = false;
            bool                isAlternative = false;
            MessagePartEncoding MIME_encoding = MessagePartEncoding.None;
            string              bodyCharset   = string.Empty;
            string              filename      = string.Empty;
            string              altBoundary   = string.Empty;
            string              contentId     = string.Empty;

            _partBuilder.Length = 0;

            /**
             * search for the first boundary
             */
            for ( ; i < lines.Length; ++i)
            {
                line = lines[i].TrimEnd('\r');
                if (line.StartsWith(boundary))
                {
                    ++i;
                    break;
                }
            }

            for ( ; i < lines.Length && !line.StartsWith(finishingBoundary); ++i)
            {
                line = lines[i].TrimEnd('\r');

                /**
                 * if not in headers then process next part
                 */
                if (!inHeaders)
                {
                    /**
                     * if filename not present process body
                     */
                    if (filename.Length == 0)
                    {
                        if (isHtmlBody)
                        {
                            i = ProcessHTMLBody(lines, i, boundary, MIME_encoding, bodyCharset);
                        }
                        else
                        {
                            i = ProcessBody(lines, i, boundary, MIME_encoding, bodyCharset);

                            /**
                             * if body is nested multipart/alternative then parse it in its turn
                             */
                            if (isAlternative && altBoundary.Length > 0)
                            {
                                MultiPartBodyParser parser = new MultiPartBodyParser(
                                    _bodyBuilder.ToString(), "multipart/alternative; boundary=" +
                                    altBoundary, _content_transfer_encoding, _charset);
                                _bodyBuilder.Length     = 0;
                                _htmlBodyBuilder.Length = 0;
                                _parts.AddRange(parser.GetParts());
                            }
                        }
                    }
                    else

                    /**
                     * if filename present then we are to extract attacment
                     */
                    {
                        i = ProcessAttachment(lines, i, boundary, MIME_encoding);
                        if (MIMEParser.IsMIMEString(filename))
                        {
                            filename = MIMEParser.DecodeMIMEString(filename);
                        }
                        string      partStr    = _partBuilder.ToString();
                        MessagePart attachment = null;
                        switch (MIME_encoding)
                        {
                        case MessagePartEncoding.QuotedPrintable:
                        {
                            attachment = new MessagePart(MIMEParser.DecodeQuotedPrintable(partStr), filename);
                            break;
                        }

                        case MessagePartEncoding.Base64:
                        {
                            byte[] bytes = MIMEParser.DecodeBase64(partStr);
                            if (bytes != null)
                            {
                                attachment = new MessagePart(bytes, filename);
                            }
                            break;
                        }

                        default:
                        {
                            attachment = new MessagePart(
                                MIMEParser.GetEncodingExceptionSafe(bodyCharset).GetBytes(partStr), filename);
                            break;
                        }
                        }
                        if (attachment != null)
                        {
                            if (isInline)
                            {
                                attachment.PartType = MessagePartTypes.Inline;
                            }
                            else
                            {
                                if (contentId.Length > 0)
                                {
                                    attachment.PartType  = MessagePartTypes.Embedded;
                                    attachment.ContentId = contentId;
                                }
                            }
                            _parts.Add(attachment);
                        }
                        _partBuilder.Length = 0;
                    }

                    /**
                     * reset encoding, charset, filename, alternative boundary, content-id and flags
                     */
                    MIME_encoding = MessagePartEncoding.None;
                    bodyCharset   = _charset;
                    filename      = altBoundary = contentId = string.Empty;
                    inHeaders     = true;
                    isHtmlBody    = isAttachment = isInline = false;
                }
                else
                {
                    if (line.Length == 0)
                    {
                        inHeaders = false;
                    }
                    else
                    {
                        string lowerLine = line.ToLower();
                        if (lowerLine.StartsWith("content-type:"))
                        {
                            string content_type = lowerLine.Substring(14);
                            if (content_type.StartsWith("multipart/alternative") ||
                                content_type.StartsWith("multipart/related"))
                            {
                                isAlternative = true;
                            }
                            else if (content_type.StartsWith("application/") || content_type.StartsWith("image/"))
                            {
                                isAttachment = true;
                            }
                            else
                            {
                                if (content_type.StartsWith("text/html"))
                                {
                                    isHtmlBody = true;
                                }
                                bodyCharset = DetectCharset(lowerLine, line, _charset);
                            }
                        }
                        else if (lowerLine.StartsWith("content-transfer-encoding:"))
                        {
                            string encoding = lowerLine.Substring(27);
                            if (encoding.StartsWith("quoted-printable"))
                            {
                                MIME_encoding = MessagePartEncoding.QuotedPrintable;
                            }
                            else if (encoding.StartsWith("base64"))
                            {
                                MIME_encoding = MessagePartEncoding.Base64;
                            }
                        }
                        else if (lowerLine.StartsWith("content-disposition:"))
                        {
                            string headerValue = lowerLine.Substring(21);
                            if (headerValue.StartsWith("attachment"))
                            {
                                isAttachment = true;
                            }
                            else if (headerValue.StartsWith("inline"))
                            {
                                isInline = true;
                            }
                        }
                        else if (lowerLine.StartsWith("content-id:"))
                        {
                            contentId = line.Substring(12).Replace("<", "").Replace(">", "");
                        }
                        if ((isAttachment || isInline) && filename.Length == 0)
                        {
                            int nameBegin;
                            if ((nameBegin = lowerLine.IndexOf("filename=")) > 0)
                            {
                                filename = line.Substring(nameBegin + 9).Replace("\"", string.Empty).Trim();
                            }
                            if ((nameBegin = lowerLine.IndexOf("name=")) > 0)
                            {
                                filename = line.Substring(nameBegin + 5).Replace("\"", string.Empty).Trim();
                            }
                        }
                        if (isAlternative && altBoundary.Length == 0)
                        {
                            if ((boundaryBegin = lowerLine.IndexOf("boundary=")) > 0)
                            {
                                altBoundary = line.Substring(boundaryBegin + 9).Replace("\"", string.Empty).Trim();
                            }
                        }
                    }
                }
            }
        }
示例#2
0
文件: BodyParser.cs 项目: mo5h/omeo
        private void ProcessSinglePlainBody(string[] lines)
        {
            string filename = string.Empty;
            bool   inBody   = true;
            int    partLen  = 0;
            string line;

            for (int i = 0; i < lines.Length; ++i)
            {
                line = lines[i].TrimEnd('\r');
                if (inBody && line.StartsWith("begin"))
                {
                    string[] attachHeaders = line.TrimEnd(' ').Split(' ');
                    if (attachHeaders.Length > 2)
                    {
                        string fattr   = attachHeaders[1];
                        bool   fattrOk = fattr.Length >= 3;
                        if (fattrOk)
                        {
                            for (int j = 0; j < fattr.Length; ++j)
                            {
                                if (!(fattrOk = Char.IsDigit(fattr[j])))
                                {
                                    break;
                                }
                            }
                        }
                        if (fattrOk)
                        {
                            filename = attachHeaders[2];
                            for (int j = 3; j < attachHeaders.Length; ++j)
                            {
                                filename += attachHeaders[j];
                            }
                            inBody  = false;
                            partLen = 0;
                            continue;
                        }
                    }
                }
                else if (!inBody && line.StartsWith("end"))
                {
                    if (_partBuilder.Length > 0)
                    {
                        if (partLen > 0)
                        {
                            byte[] bytes = UUParser.Decode(_partBuilder.ToString());
                            if (partLen > bytes.Length)
                            {
                                partLen = bytes.Length;
                            }
                            MessagePart attachment = new MessagePart(bytes, partLen, filename);
                            attachment.PartType = MessagePartTypes.Inline;
                            _parts.Add(attachment);
                        }
                        _partBuilder.Length = 0;
                    }
                    inBody = true;
                    continue;
                }
                if (inBody)
                {
                    _bodyBuilder.Append(line);
                    _bodyBuilder.Append("\r\n");
                }
                else
                {
                    if (line.Length > 1 && line[0] != '`')
                    {
                        partLen += UUParser._UUAlphabet.IndexOf(line[0]);
                        _partBuilder.Append(line, 1, line.Length - 1);
                    }
                }
            }
        }