private ArticleBody GetMIMEBody(string messageId, MIMEPart part) { string line = null; ArticleBody body = null; StringBuilder sb = null; ArrayList attachmentList = new ArrayList(); try { NntpUtil.DispatchMIMEContent(sr, part, "."); sb = new StringBuilder(); attachmentList = new ArrayList(); body = new ArticleBody(); body.IsHtml = true; this.ConvertMIMEContent(messageId, part, sb, attachmentList); body.Text = sb.ToString(); body.Attachments = (Attachment[])attachmentList.ToArray(typeof(Attachment)); } finally { if (((NetworkStream)sr.BaseStream).DataAvailable) { while ((line = sr.ReadLine()) != null && line != ".") { ; } } } return(body); }
public static string Base64HeaderDecode(string line) { MemoryStream ms = null; byte[] bytes = null; string oStr = null; string code = null; string content = null; Match m = Regex.Match(line, @"=\?([^?]+)\?[^?]+\?([^?]+)\?="); while (m.Success) { ms = new MemoryStream(); oStr = m.Groups[0].ToString(); code = m.Groups[1].ToString(); content = m.Groups[2].ToString(); NntpUtil.Base64Decode(content, ms); ms.Seek(0, SeekOrigin.Begin); bytes = new byte[ms.Length]; ms.Read(bytes, 0, bytes.Length); line = line.Replace(oStr, Encoding.GetEncoding(code).GetString(bytes)); m = m.NextMatch(); } return(line); }
public ArrayList 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 = MakeRequest("XOVER " + low + "-" + high); if (res.Code != 224) { throw new NntpException(res.Code, res.Request); } ArrayList list = new ArrayList(); Article article = null; string[] values = null; int i; string response = null; while ((response = sr.ReadLine()) != null && response != ".") { try { article = new Article(); article.Header = new ArticleHeader(); values = response.Split('\t'); article.ArticleId = int.Parse(values[0]); article.Header.Subject = NntpUtil.Base64HeaderDecode(values[1]); article.Header.From = NntpUtil.Base64HeaderDecode(values[2]); i = values[3].IndexOf(','); article.Header.Date = DateTime.Parse(values[3].Substring(i + 1, values[3].Length - 7 - i)); article.MessageId = values[4]; if (values[5].Trim().Length == 0) { article.Header.ReferenceIds = new string[0]; } else { article.Header.ReferenceIds = values[5].Split(' '); } if (values.Length < 8 || values[7] == null || values[7].Trim() == "") { article.Header.LineCount = 0; } else { article.Header.LineCount = int.Parse(values[7]); } article.Body = null; } catch (Exception e) { throw new Exception(response, e); } list.Add(article); } return(list); }
private ArticleBody GetNormalBody(string messageId) { char[] buff = new char[1]; string response = null; ArrayList list = new ArrayList(); StringBuilder sb = new StringBuilder(); Attachment attach = null; MemoryStream ms = null; sr.Read(buff, 0, 1); int i = 0; byte[] bytes = null; Match m = null; while ((response = sr.ReadLine()) != null) { if (buff[0] == '.') { if (response == "") { 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 = sr.ReadLine()) != null && (response.Length != 3 || response.ToUpper() != "END")) { NntpUtil.UUDecode(response, ms); } ms.Seek(0, SeekOrigin.Begin); bytes = new byte[ms.Length]; ms.Read(bytes, 0, (int)ms.Length); 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'); sr.Read(buff, 0, 1); } ArticleBody ab = new ArticleBody(); ab.IsHtml = false; ab.Text = sb.ToString(); ab.Attachments = (Attachment[])list.ToArray(typeof(Attachment)); return(ab); }
private ArticleHeader GetHeader(string messageId, out MIMEPart part) { string response = null; ArticleHeader 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 = sr.ReadLine()) != null && response != "") { 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.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": i = value.IndexOf(','); header.Date = DateTime.Parse(value.Substring(i + 1, value.Length - 7 - i)); 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); }
public static MIMEPart DispatchMIMEContent(StreamReader sr, MIMEPart part, string seperator) { string line = null; Match m = null; MemoryStream ms; byte[] bytes; switch (part.ContentType.Substring(0, part.ContentType.IndexOf('/')).ToUpper()) { case "MULTIPART": MIMEPart newPart = null; while ((line = sr.ReadLine()) != null && line != seperator && line != seperator + "--") { m = Regex.Match(line, @"CONTENT-TYPE: ""?([^""\s;]+)", RegexOptions.IgnoreCase); if (!m.Success) { continue; } newPart = new MIMEPart(); newPart.ContentType = m.Groups[1].ToString(); newPart.Charset = "US-ASCII"; newPart.ContentTransferEncoding = "7BIT"; while (line != "") { m = Regex.Match(line, @"BOUNDARY=""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.Boundary = m.Groups[1].ToString(); newPart.EmbeddedPartList = new ArrayList(); } m = Regex.Match(line, @"CHARSET=""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.Charset = m.Groups[1].ToString(); } m = Regex.Match(line, @"CONTENT-TRANSFER-ENCODING: ""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.ContentTransferEncoding = m.Groups[1].ToString(); } m = Regex.Match(line, @"NAME=""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.Filename = Base64HeaderDecode(m.Groups[1].ToString()); newPart.Filename = newPart.Filename.Substring(newPart.Filename.LastIndexOfAny(new char[] { '\\', '/' }) + 1); } line = sr.ReadLine(); } part.EmbeddedPartList.Add(DispatchMIMEContent(sr, newPart, "--" + part.Boundary)); } break; case "TEXT": ms = new MemoryStream(); bytes = null; long pos; StreamReader msr = new StreamReader(ms, Encoding.GetEncoding(part.Charset)); StringBuilder sb = new StringBuilder(); while ((line = sr.ReadLine()) != null && line != seperator && line != seperator + "--") { pos = ms.Position; if (line != "") { switch (part.ContentTransferEncoding.ToUpper()) { case "QUOTED-PRINTABLE": NntpUtil.QuotedPrintableDecode(line, ms); break; case "BASE64": if (line != null && line != "") { NntpUtil.Base64Decode(line, ms); } break; case "UU": if (line != null && line != "") { NntpUtil.UUDecode(line, ms); } break; case "7BIT": bytes = Encoding.ASCII.GetBytes(line); ms.Write(bytes, 0, bytes.Length); ms.WriteByte((byte)'\n'); break; default: bytes = Encoding.ASCII.GetBytes(line); ms.Write(bytes, 0, bytes.Length); ms.WriteByte((byte)'\n'); break; } } ms.Position = pos; if (part.ContentType.ToUpper() == "TEXT/HTML") { sb.Append(msr.ReadToEnd()); } else { sb.Append(HttpUtility.HtmlEncode(msr.ReadToEnd()).Replace("\n", "<br>\n")); } } part.Text = sb.ToString(); break; default: ms = new MemoryStream(); bytes = null; while ((line = sr.ReadLine()) != null && line != seperator && line != seperator + "--") { if (line != "") { switch (part.ContentTransferEncoding.ToUpper()) { case "QUOTED-PRINTABLE": NntpUtil.QuotedPrintableDecode(line, ms); break; case "BASE64": if (line != null && line != "") { NntpUtil.Base64Decode(line, ms); } break; case "UU": if (line != null && line != "") { NntpUtil.UUDecode(line, ms); } break; default: bytes = Encoding.ASCII.GetBytes(line); ms.Write(bytes, 0, bytes.Length); break; } } } ms.Seek(0, SeekOrigin.Begin); part.BinaryData = new byte[ms.Length]; ms.Read(part.BinaryData, 0, (int)ms.Length); break; } return(part); }