Nntp Client Library

The world doesn't really need another one of these, but here's lightweight NNTP client library written in C#. The primary purpose of this library was to process a large number of groups and articles without consuming large amounts of memory. To do this I'm not using container data classes, but instead using "iterators" (see examples below).

The NUnit test suite contains a bunch of examples of how one uses the library.

There are two classes that implement the NNTP protocol, the base class Rfc977NntpClient implements the RFC 977 specification, and the Rfc977NntpClientWithExtensions that adds the home brewed extensions that have been added to NNTP servers over the years.

Examples

Create and open a connection to a server and read the help (functions supported by the server), writing the help content to the console.
using (Rfc977NntpClient client = new Rfc977NntpClient())
{
    client.Connect("netnews.server.com");
    client.SelectNewsgroup("alt.test");
    foreach (string s in client.RetrieveHelp())
    {
        Console.WriteLine(s);
    }
}


Download the headers and the body of the first article in a newsgroup.
using (Rfc977NntpClient client = new Rfc977NntpClient())
{
    client.Connect("netnews.server.com");
    client.SelectNewsgroup("alt.test");

    ArticleHeadersDictionary d = client.RetrieveArticleHeader();
    ArticleProcessor ap = new ArticleProcessor();
    client.RetrieveArticle(ap, ap);
}

// Add your own code to these methods to save or decode the article body.
internal class ArticleProcessor : IArticleHeadersProcessor, IArticleBodyProcessor
{
    public void AddHeader(string headerAndValue)
    {
    }

    public void AddHeader(string header, string value)
    {
    }

    public void AddText(string line)
    {
    }
}


Posting a test article to a server that does not require authentication.
using (Rfc977NntpClient client = new Rfc977NntpClient())
{
    client.Connect("netnews.server.com");
    client.SelectNewsgroup("alt.test");

    ArticleHeadersDictionary headers = new ArticleHeadersDictionary();
    headers.AddHeader("From", "anoymous@anon.com (Anonymous)");
    headers.AddHeader("Subject", "Test - Please Ignore.");
    headers.AddHeader("Newsgroups", TestNewsGroup);
    headers.AddHeader("Date", new NntpDateTime(DateTime.Now).ToString());

    List<string> body = new List<string>();
    body.Add("One line test message body.");

    client.PostArticle(new ArticleHeadersDictionaryEnumerator(headers), body);
}


Posting a test article to a server that does require authentication.
using (Rfc977NntpClientWithExtensions client = new Rfc977NntpClientWithExtensions())
{
    client.Connect("netnews.server.com");
    client.AuthenticateUser("userid", "password");

    string newsgroup = "alt.test";
    client.SelectNewsgroup(newsgroup);

    ArticleHeadersDictionary headers = new ArticleHeadersDictionary();
    headers.AddHeader("From", "anoymous@anon.com (Anonymous)");
    headers.AddHeader("Subject", "Test - " + Guid.NewGuid().ToString());
    headers.AddHeader("Newsgroups", newsgroup);
    headers.AddHeader("Date", new NntpDateTime(DateTime.Now).ToString());

    List<string> body = new List<string>();
    body.Add("One line test message body.");

    client.PostArticle(new ArticleHeadersDictionaryEnumerator(headers), body);
}

Last edited Sep 26, 2007 at 12:14 AM by scgarland, version 10