/// <summary> /// Creates a new instance of the <see cref="NntpArticleBuilder"/> class. /// </summary> public NntpArticleBuilder() { headers = new MultiValueDictionary <string, string>(); body = new List <string>(); groupsBuilder = new NntpGroupsBuilder(); messageId = NntpMessageId.Empty; }
/// <summary> /// Initialize the <see cref="NntpArticleBuilder"/> from the given <see cref="NntpArticle"/>. /// All properties are overwritten. /// </summary> /// <param name="article">The <see cref="NntpArticle"/> to initialize the <see cref="NntpArticleBuilder"/> with.</param> /// <returns>The <see cref="NntpArticleBuilder"/> so that additional calls can be chained.</returns> public NntpArticleBuilder InitializeFrom(NntpArticle article) { Guard.ThrowIfNull(article, nameof(article)); messageId = new NntpMessageId(article.MessageId.Value); groupsBuilder = new NntpGroupsBuilder().Add(article.Groups); headers = new MultiValueDictionary <string, string>(); from = null; subject = null; dateTime = null; body = null; foreach (KeyValuePair <string, ImmutableHashSet <string> > header in article.Headers) { foreach (string value in header.Value) { switch (header.Key) { case NntpHeaders.MessageId: if (!messageId.HasValue) { messageId = value; } else { log.Warn("Found more than 1 {messageId} header. Skipping it.", NntpHeaders.MessageId); } break; case NntpHeaders.From: if (from == null) { from = value; } else { log.Warn("Found more than 1 {from} header. Skipping it.", NntpHeaders.From); } break; case NntpHeaders.Subject: if (subject == null) { subject = value; } else { log.Warn("Found more than 1 {subject} header. Skipping it.", NntpHeaders.Subject); } break; case NntpHeaders.Date: if (dateTime == null) { if (DateTimeOffset.TryParseExact(value, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset headerDateTime)) { dateTime = headerDateTime; } else { log.Warn("{date} header has invalid value {value}. Skipping it.", NntpHeaders.Date, value); } } else { log.Warn("Found more than 1 {date} header. Skipping it.", NntpHeaders.Date); } break; case NntpHeaders.Newsgroups: // convert group header to list of groups, do not add as header groupsBuilder.Add(value); break; default: headers.Add(header.Key, value); break; } } } // make copy of body body = article.Body.ToList(); return(this); }