Perl programmer for hire: download my resume (PDF).
John Bokma MexIT
freelance Perl programmer

Posting to Usenet groups using Perl

Friday, August 22, 2008 | 0 comments

Yesterday I played a bit with the Net::NNTP module, and used it to write a short Perl program that posted a simple test message to the Usenet group alt.test, which is dedicated to exactly such testing. And today I cleaned up the program a little, and decided to put the Perl program online as a short example on how to post using Net::NNTP.

Yesterday, I had already used the Perl Package Manager, which comes with ActivePerl, to install the libnet bundle. This bundle does include the Net::NNTP module, which makes posting to Usenet an easy thing to do.

Using the Perl Package Manager to install libnet.
Using the Perl Package Manager to install libnet.

Next, I wrote a small Perl program which connects to the server, logs in using a username and a password, checks if posting is allowed, and then posts a message. The complete cleaned up version, which includes checking for errors, is given below:

#!/usr/bin/perl

use strict;
use warnings;

use Net::NNTP;

my $SERVER   = 'news.example.com;
my $USERNAME = 'user';
my $PASSWORD = 'password';
my $DEBUG    = 0;


my $nntp = Net::NNTP->new( $SERVER, Debug => $DEBUG );

$nntp->authinfo( $USERNAME, $PASSWORD )
    or die "Authentication failed: ", $nntp->message;

$nntp->postok()
    or die "Posting not allowed:", $nntp->message;

my $headers = <<'HEADERS_END';
Newsgroups: alt.test
Subject: This is a test
From: test <invalid@invalid.invalid>
HEADERS_END

my $body = <<"BODY_END";
This is a test.
BODY_END

my $sig_sep = "-- \n";
my $signature = 'http://johnbokma.com/perl';

my $message = $headers
            . "\n"
            . $body
            . $sig_sep
            . $signature;

$nntp->post( $message )
    or die "nntp_post: posting failed: ", $nntp->message;

$nntp->quit;

Note that you have to replace the dummy values for $SERVER, $USERNAME, and $PASSWORD with working values in order for the Perl program to work.

Moreover, note that this program doesn't use a valid email address. Instead of making up one myself, which is a very bad idea; one might ending up abusing someone else's domain, I decided to use the recommended invalid TLD (top level domain) to make clear that the email address is not a valid one.

XNews showing the test message I posted in alt.test using Net::NNTP.
XNews showing the test message I posted in alt.test using Net::NNTP.

Finally, if you want to use a signature, make sure you're using the correct signature separator: two '-' characters, followed by exactly one space on a line by itself as shown in the above example program.

Usnet posting related

Also today

Please post a comment | read 0 comments | RSS feed