Example #1
0
        /// <summary>
        /// The get mime body.
        /// </summary>
        /// <param name="messageId">
        /// The message id.
        /// </param>
        /// <param name="part">
        /// The part.
        /// </param>
        /// <returns>
        /// </returns>
        private ArticleBody GetMIMEBody(string messageId, MIMEPart part)
        {
            string        line           = null;
            ArticleBody   body           = null;
            StringBuilder sb             = null;
            var           attachmentList = new ArrayList();

            try
            {
                NntpUtil.DispatchMIMEContent(this.sr, part, ".");
                sb             = new StringBuilder();
                attachmentList = new ArrayList();
                body           = new ArticleBody {
                    IsHtml = true
                };
                this.ConvertMIMEContent(messageId, part, sb, attachmentList);
                body.Text        = sb.ToString();
                body.Attachments = (Attachment[])attachmentList.ToArray(typeof(Attachment));
            }
            finally
            {
                if (((NetworkStream)this.sr.BaseStream).DataAvailable)
                {
                    while ((line = this.sr.ReadLine()) != null && line != ".")
                    {
                        ;
                    }
                }
            }

            return(body);
        }
Example #2
0
        public IList <Article> GetArticleList(int low, int high)
        {
            if (this.connectedServer == null)
            {
                throw new NntpException("No connecting newsserver.");
            }

            if (this.connectedGroup == null)
            {
                throw new NntpException("No connecting newsgroup.");
            }

            Response res = this.MakeRequest("XOVER " + low + "-" + high);

            if (res.Code != 224)
            {
                throw new NntpException(res.Code, res.Request);
            }

            var     list    = new List <Article>();
            Article article = null;

            string[] values   = null;
            string   response = null;

            while ((response = this.sr.ReadLine()) != null && response != ".")
            {
                try
                {
                    article = new Article {
                        Header = new ArticleHeader()
                    };
                    values = response.Split('\t');

                    // article id...
                    article.ArticleId = int.Parse(values[0]);

                    // subject
                    article.Header.Subject = NntpUtil.Base64HeaderDecode(values[1]);

                    // from
                    article.Header.From = NntpUtil.Base64HeaderDecode(values[2]);

                    // date
                    int i = values[3].IndexOf(',');
                    int offTz;
                    article.Header.Date           = NntpUtil.DecodeUTC(values[3].Substring(i + 1, values[3].Length - 7 - i), out offTz);
                    article.Header.TimeZoneOffset = offTz;

                    // messge id
                    article.MessageId = values[4];

                    // reference ids
                    article.Header.ReferenceIds = values[5].Trim().Length == 0 ? new string[0] : values[5].Split(' ');

                    if (values.Length < 8 || values[7] == null || values[7].Trim() == string.Empty)
                    {
                        article.Header.LineCount = 0;
                    }
                    else
                    {
                        article.Header.LineCount = int.Parse(values[7]);
                    }

                    // no body...
                    article.Body = null;
                }
                catch (Exception e)
                {
                    throw new Exception(response, e);
                }

                list.Add(article);
            }

            return(list);
        }
Example #3
0
        /// <summary>
        /// The get normal body.
        /// </summary>
        /// <param name="messageId">
        /// The message id.
        /// </param>
        /// <returns>
        /// </returns>
        private ArticleBody GetNormalBody(string messageId)
        {
            var          buff     = new char[1];
            string       response = null;
            var          list     = new ArrayList();
            var          sb       = new StringBuilder();
            MemoryStream ms       = null;

            this.sr.Read(buff, 0, 1);
            int   i = 0;
            Match m = null;

            while ((response = this.sr.ReadLine()) != null)
            {
                if (buff[0] == '.')
                {
                    if (response == string.Empty)
                    {
                        break;
                    }
                    else
                    {
                        sb.Append(response);
                    }
                }
                else
                {
                    if ((buff[0] == 'B' || buff[0] == 'b') && (m = Regex.Match(response, @"^EGIN \d\d\d (.+)$", RegexOptions.IgnoreCase)).Success)
                    {
                        ms = new MemoryStream();
                        while ((response = this.sr.ReadLine()) != null && (response.Length != 3 || response.ToUpper() != "END"))
                        {
                            NntpUtil.UUDecode(response, ms);
                        }

                        ms.Seek(0, SeekOrigin.Begin);
                        byte[] bytes = new byte[ms.Length];
                        ms.Read(bytes, 0, (int)ms.Length);
                        Attachment attach = new Attachment(messageId + " - " + m.Groups[1].ToString(), m.Groups[1].ToString(), bytes);
                        list.Add(attach);
                        ms.Close();
                        i++;
                    }
                    else
                    {
                        sb.Append(buff[0]);
                        sb.Append(response);
                    }
                }

                sb.Append('\n');
                this.sr.Read(buff, 0, 1);
            }

            var ab = new ArticleBody
            {
                IsHtml = false, Text = sb.ToString(), Attachments = (Attachment[])list.ToArray(typeof(Attachment))
            };

            return(ab);
        }
Example #4
0
        /// <summary>
        /// The get header.
        /// </summary>
        /// <param name="messageId">
        /// The message id.
        /// </param>
        /// <param name="part">
        /// The part.
        /// </param>
        /// <returns>
        /// </returns>
        private ArticleHeader GetHeader(string messageId, out MIMEPart part)
        {
            string response = null;
            var    header   = new ArticleHeader();
            string name     = null;
            string value    = null;

            header.ReferenceIds = new string[0];
            string[] values  = null;
            string[] values2 = null;
            Match    m       = null;

            part = null;
            int i = -1;

            while ((response = this.sr.ReadLine()) != null && response != string.Empty)
            {
                m = Regex.Match(response, @"^\s+(\S+)$");
                if (m.Success)
                {
                    value = m.Groups[1].ToString();
                }
                else
                {
                    i = response.IndexOf(':');
                    if (i == -1)
                    {
                        continue;
                    }

                    name  = response.Substring(0, i).ToUpper();
                    value = response.Substring(i + 1);
                }

                switch (name)
                {
                case "REFERENCES":
                    values              = value.Trim().Split(' ');
                    values2             = header.ReferenceIds;
                    header.ReferenceIds = new string[values.Length + values2.Length];
                    values.CopyTo(header.ReferenceIds, 0);
                    values2.CopyTo(header.ReferenceIds, values.Length);
                    break;

                case "SUBJECT":
                    header.Subject += NntpUtil.Base64HeaderDecode(value);
                    break;

                case "DATE":
                    // vzrus: 31.03.10 dateTime and tz conversion
                    int offTz;
                    header.Date           = NntpUtil.DecodeUTC(value, out offTz);
                    header.TimeZoneOffset = offTz;
                    break;

                case "FROM":
                    header.From += NntpUtil.Base64HeaderDecode(value);
                    break;

                case "NNTP-POSTING-HOST":
                    header.PostingHost += value;
                    break;

                case "LINES":
                    header.LineCount = int.Parse(value);
                    break;

                case "MIME-VERSION":
                    part                         = new MIMEPart();
                    part.ContentType             = "TEXT/PLAIN";
                    part.Charset                 = "US-ASCII";
                    part.ContentTransferEncoding = "7BIT";
                    part.Filename                = null;
                    part.Boundary                = null;
                    break;

                case "CONTENT-TYPE":
                    if (part != null)
                    {
                        m = Regex.Match(response, @"CONTENT-TYPE: ""?([^""\s;]+)", RegexOptions.IgnoreCase);
                        if (m.Success)
                        {
                            part.ContentType = m.Groups[1].ToString();
                        }

                        m = Regex.Match(response, @"BOUNDARY=""?([^""\s;]+)", RegexOptions.IgnoreCase);
                        if (m.Success)
                        {
                            part.Boundary         = m.Groups[1].ToString();
                            part.EmbeddedPartList = new ArrayList();
                        }
                        m = Regex.Match(response, @"CHARSET=""?([^""\s;]+)", RegexOptions.IgnoreCase);
                        if (m.Success)
                        {
                            part.Charset = m.Groups[1].ToString();
                        }

                        m = Regex.Match(response, @"NAME=""?([^""\s;]+)", RegexOptions.IgnoreCase);
                        if (m.Success)
                        {
                            part.Filename = m.Groups[1].ToString();
                        }
                    }

                    break;

                case "CONTENT-TRANSFER-ENCODING":
                    if (part != null)
                    {
                        m = Regex.Match(response, @"CONTENT-TRANSFER-ENCODING: ""?([^""\s;]+)", RegexOptions.IgnoreCase);
                        if (m.Success)
                        {
                            part.ContentTransferEncoding = m.Groups[1].ToString();
                        }
                    }

                    break;
                }
            }

            return(header);
        }