# Simple NNTP posting Plug-in for
# Blagg: the Blosxom RSS aggregator
# (c) DJ Adams <dj.adams@pobox.com> 2003
# Version: 0+1a

# changes
# 0+1a 2003-01-28 original version

package blaggplug;

use strict;

use CGI qw{:standard};
use Net::NNTP;

# Configuration

my $nntpserver = "merlix";
my $newsgroups = "public.test";
my $id         = "blagg\@localhost.localdomain";

my $nntp;

sub init {
  $nntp = Net::NNTP->new($nntpserver);
  die "Cannot connect to $nntpserver" unless ref $nntp;
  die "Posting denied at $nntpserver" unless $nntp->postok();
  debug("Connected to $nntpserver and ready to post");
}

sub post {
  my($title, $link, $description, $feed_title, $feed_link, $feed_file, $feed_nick) = @_;
  # Split long lines down to 70 chars per line 
  # to avoid "toolong post" rejections.
  my @lines = split(/\n/, $description);
   map { length > 200 && s/(.{1,70}\s)/$1\n/g } @lines;
  my $ng = join(",", $newsgroups, "public.blog.$feed_nick");
  my @msg = (
    qq{Subject: $title\n},
    qq{Newsgroups: $ng\n},
    qq{From: $feed_title <$id>\n},
    qq{Content-Type: text/html\n},
    qq{\n},
    qq{<p><a href="$link"><strong>$title</strong><a></p>},
    join("\n", @lines), 
    qq{<p>[from <a href="$feed_link">$feed_link</a>]</p>},
  );
  my ($shorttitle) = $title =~ /^(.{1,30})/;
  debug(($nntp->post(@msg) ? "OK : " : "BAD: ").$shorttitle);
}

sub destroy {
  $nntp->quit;
  debug("Disconnected from $nntpserver");
}

sub debug {
  print STDERR scalar(localtime)." @_\n";
}

1;
