Fix: Save status information only after successful posting.

* New Function: updatestaus
  Move status information save to updatestatus.

* post() now returns exit code.

* postfaq() will update status information only
  when post() was successful.

Signed-off-by: Thomas Hochstein <thh@inter.net>
This commit is contained in:
Thomas Hochstein 2010-04-11 23:23:12 +02:00
parent 227afd47f6
commit 5a6670c7ff

View file

@ -233,6 +233,21 @@ sub calcdelta {
return ($NYear, $NMonth, $NDay); return ($NYear, $NMonth, $NDay);
} }
################################ updatestatus ###############################
# Takes a MID and a status file name
# and writes status information to disk
sub updatestatus {
my ($ActName, $File, $date, $MID) = @_;
print "$$ActName: Save status information.\n" if($Options{'v'});
open (FH, ">$$File.cfg") or die "$0: E: Can't open $$File.cfg: $!";
print FH "##;; Lastpost: $date\n";
print FH "##;; LastMID: $MID\n";
close FH;
}
################################## postfaq ################################## ################################## postfaq ##################################
# Takes a filename and many other vars. # Takes a filename and many other vars.
# #
@ -321,18 +336,13 @@ sub postfaq {
# post article # post article
print "$$ActName: Posting article ...\n" if($Options{'v'}); print "$$ActName: Posting article ...\n" if($Options{'v'});
post(\@Article); my $failure = post(\@Article);
# Test mode? if ($failure) {
return if($Options{'t'}); print "$$ActName: Posting failed, ERROR.dat may have more information.\n" if($Options{'v'} && (!defined($Options{'t'}) || $Options{'t'} !~ /console/i));
} else {
# otherwise: update status data updatestatus($ActName, $File, "$day.$month.$year", $MID) if !defined($Options{'t'});
print "$$ActName: Save status information.\n" if($Options{'v'}); }
open (FH, ">$$File.cfg") or die "$0: E: Can't open $$File.cfg: $!";
print FH "##;; Lastpost: $day.$month.$year\n";
print FH "##;; LastMID: $MID\n";
close FH;
} }
################################## post ################################## ################################## post ##################################
@ -342,31 +352,37 @@ sub postfaq {
sub post { sub post {
my ($ArticleR) = @_; my ($ArticleR) = @_;
my ($failure) = -1;
# Test mode? # test mode - print article to console
if(defined($Options{'t'}) and $Options{'t'} =~ /console/i) { if(defined($Options{'t'}) and $Options{'t'} =~ /console/i) {
print "-----BEGIN--------------------------------------------------\n"; print "-----BEGIN--------------------------------------------------\n";
print @$ArticleR; print @$ArticleR;
print "------END---------------------------------------------------\n"; print "------END---------------------------------------------------\n";
return; # pipe article to script
} } elsif(defined($Options{'s'})) {
# pipe to script?
if(defined($Options{'s'})) {
open (POST, "| $Options{'s'}") or die "$0: E: Cannot fork $Options{'s'}: $!\n"; open (POST, "| $Options{'s'}") or die "$0: E: Cannot fork $Options{'s'}: $!\n";
print POST @$ArticleR; print POST @$ArticleR;
close POST; close POST;
return; if ($? == 0) {
$failure = 0;
} else {
warn "$0: W: $Options{'s'} exited with status ", ($? >> 8), "\n";
$failure = $?;
} }
# post article
} else {
my $NewsConnection = Net::NNTP->new($Config{'NNTPServer'}, Reader => 1) or die "$0: E: Can't connect to news server '$Config{'NNTPServer'}'!\n"; my $NewsConnection = Net::NNTP->new($Config{'NNTPServer'}, Reader => 1) or die "$0: E: Can't connect to news server '$Config{'NNTPServer'}'!\n";
$NewsConnection->authinfo ($Config{'NNTPUser'}, $Config{'NNTPPass'}) if (defined($Config{'NNTPUser'})); $NewsConnection->authinfo ($Config{'NNTPUser'}, $Config{'NNTPPass'}) if (defined($Config{'NNTPUser'}));
$NewsConnection->post(); $NewsConnection->post();
$NewsConnection->datasend (@$ArticleR); $NewsConnection->datasend (@$ArticleR);
$NewsConnection->dataend(); $NewsConnection->dataend();
if ($NewsConnection->ok()) {
$failure = 0;
# Posting failed? Save to ERROR.dat # Posting failed? Save to ERROR.dat
if (!$NewsConnection->ok()) { } else {
warn "$0: W: Posting failed!\n";
open FH, ">>ERROR.dat"; open FH, ">>ERROR.dat";
print FH "\nPosting failed! Saving to ERROR.dat. Response from news server:\n"; print FH "\nPosting failed! Saving to ERROR.dat. Response from news server:\n";
print FH $NewsConnection->code(); print FH $NewsConnection->code();
@ -376,8 +392,9 @@ sub post {
print FH "-" x 80, "\n"; print FH "-" x 80, "\n";
close FH; close FH;
} }
$NewsConnection->quit(); $NewsConnection->quit();
}
return $failure;
} }
#-------- sub getpgpcommand #-------- sub getpgpcommand